Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wallet websocket covenant events #195

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/wallet/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -1303,6 +1303,21 @@ class HTTP extends Server {
this.to('w:*', event, wallet.id, json);
};

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably use handleTX instead of creating a new function here, but the difference is in this.to

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handleTX doesn't pass along enough arguments, we need to add another method or refactor handleTX. The problem is that handleTX does nothing with the tx argument, and the covenant related events pass along the argument at that position. A new function called handleCovenant was created.

const handleCovenant = (event, wallet, ns, details) => {
const name = `w:${wallet.id}`;

if (!this.channel(name) && !this.channel('w:*'))
return;

const json = details.getJSON(this.network, this.wdb.height);

if (this.channel(name))
this.to(name, event, wallet.id, ns, json);

if (this.channel('w:*'))
this.to('w:*', event, wallet.id, ns, json);
};

this.wdb.on('tx', (wallet, tx, details) => {
handleTX('tx', wallet, tx, details);
});
Expand Down Expand Up @@ -1351,6 +1366,16 @@ class HTTP extends Server {
if (this.channel('w:*'))
this.to('w:*', 'address', wallet.id, json);
});

// set up listener for each covenant type
for (const type of Object.values(rules.typesByVal))
if (type !== 'NONE') {
const action = type.toLowerCase();
const channel = `${action} covenant`;
this.wdb.on(channel, (wallet, data, details) => {
handleCovenant(channel, wallet, data, details);
});
}
}

/**
Expand Down
60 changes: 59 additions & 1 deletion lib/wallet/txdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const rules = require('../covenants/rules');
const NameState = require('../covenants/namestate');
const NameUndo = require('../covenants/undo');
const {TXRecord} = records;
const {types} = rules;
const {types, typesByVal} = rules;

/*
* Constants
Expand Down Expand Up @@ -79,6 +79,63 @@ class TXDB {
this.wallet.emit(event, data, details);
}

/**
* Emit events for each type of covenant.
* @private
* @param {TXRecord} wtx
* @param {Details} details
*/

async emitCovenants(tx, details, view, height) {
const network = this.wdb.network;
if (height < 0)
height = null;

const {outputs} = tx;
for (const output of outputs) {
const {covenant} = output;
tynes marked this conversation as resolved.
Show resolved Hide resolved
if (!covenant.isName())
continue;

// view accumulates state in connectNames
const nameHash = covenant.get(0);
const ns = await view.getNameState(this, nameHash);

// only look up the name if it isn't already populated
if (EMPTY.equals(ns.name) || !ns.name) {
Copy link
Contributor Author

@tynes tynes Jun 22, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The view accumulates state (the name itself is what we want) during connectNames. If the name was added to the NameState object, then skip the i/o involved with looking it up. Look it up in the covenant data if its present, otherwise query the wallet database for the name based on the nameHash.

switch (covenant.type) {
case types.CLAIM:
case types.OPEN:
case types.BID:
case types.FINALIZE: {
ns.name = covenant.get(2);
break;
}
case types.REVEAL:
case types.REDEEM:
case types.REGISTER:
case types.UPDATE:
case types.RENEW:
case types.TRANSFER:
case types.REVOKE: {
const nameState = await this.wallet.getNameState(nameHash);
ns.name = nameState.name;
break;
}
}
}

if (ns.name)
tynes marked this conversation as resolved.
Show resolved Hide resolved
ns.name = ns.name.toString();

const nameState = ns.getJSON(height, network);
const {type} = covenant;
const action = typesByVal[type].toLowerCase();

this.emit(`${action} covenant`, nameState, details);
}
}

/**
* Get wallet path for output.
* @param {Output} output
Expand Down Expand Up @@ -1064,6 +1121,7 @@ class TXDB {
// successfully written to disk.
this.emit('tx', tx, details);
this.emit('balance', balance);
this.emitCovenants(tx, details, view, height);

return details;
}
Expand Down
128 changes: 127 additions & 1 deletion test/wallet-http-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'use strict';

const {NodeClient, WalletClient} = require('hs-client');
const bsock = require('bsock');
const Network = require('../lib/protocol/network');
const FullNode = require('../lib/node/fullnode');
const MTX = require('../lib/primitives/mtx');
Expand All @@ -21,9 +22,9 @@ const Output = require('../lib/primitives/output');
const rules = require('../lib/covenants/rules');
const {types} = rules;
const secp256k1 = require('bcrypto/lib/secp256k1');
const network = Network.get('regtest');
const assert = require('bsert');
const common = require('./util/common');
const network = Network.get('regtest');

const node = new FullNode({
network: 'regtest',
Expand All @@ -49,6 +50,7 @@ const wallet2 = wclient.wallet('secondary');

let name, cbAddress;
const accountTwo = 'foobar';
let wsocket, wsocket2;

const {
treeInterval,
Expand All @@ -70,9 +72,25 @@ describe('Wallet HTTP', function() {
await wclient.createWallet('secondary');
cbAddress = (await wallet.createAddress('default')).address;
await wallet.createAccount(accountTwo);

tynes marked this conversation as resolved.
Show resolved Hide resolved
// wsocket listens on wallet channel
wsocket = bsock.connect(network.walletPort);
wsocket.on('connect', async () => {
await wsocket.call('auth', 'foo');
await wsocket.call('join', wallet.id);
});

// wsocket2 listens on wallet2 channel
wsocket2 = bsock.connect(network.walletPort);
wsocket2.on('connect', async () => {
await wsocket2.call('auth', 'foo');
await wsocket2.call('join', wallet2.id);
});
});

after(async () => {
await wsocket.destroy();
await wsocket2.destroy();
await nclient.close();
await wclient.close();
await node.close();
Expand Down Expand Up @@ -1092,6 +1110,114 @@ describe('Wallet HTTP', function() {
assert.equal(ns.info.name, name);
assert.equal(ns.info.state, 'REVOKED');
});

it('should emit events for covenants', async () => {
const seen = {
open: false,
tynes marked this conversation as resolved.
Show resolved Hide resolved
bid: false,
finalize: false,
reveal: false,
register: false,
update: false,
renew: false,
transfer: false,
revoke: false,
redeem: false
};

// Assert that the data is correct that is coming
// over the websocket. Check to make sure the correct
// covenant type is sent, the corret wallet id is sent
// and that the correct namestate is sent
function assertSocketData(wallet, action, walletid, ns, details) {
const covenants = details.outputs.map(o => o.covenant);
assert.ok(covenants.some(c => c.action === action));
assert.ok(covenants.some(c => c.type === types[action]));
assert.equal(walletid, wallet.id);
assert.equal(ns.name, name);
seen[action.toLowerCase()] = true;
}

for (const t of Object.keys(seen)) {
const channel = `${t} covenant`;
const action = t.toUpperCase();
wsocket.bind(channel, (walletid, ns, details) => {
assertSocketData(wallet, action, walletid, ns, details);
});

wsocket2.bind(channel, (walletid, ns, details) => {
assertSocketData(wallet2, action, walletid, ns, details);
});
}

await wallet.client.post(`/wallet/${wallet.id}/open`, {
name: name
});
await mineBlocks(treeInterval + 1, cbAddress);

await wallet.client.post(`/wallet/${wallet.id}/bid`, {
name: name,
bid: 1000,
lockup: 2000
});
await wallet.client.post(`/wallet/${wallet2.id}/bid`, {
name: name,
bid: 500,
lockup: 1500
});
await mineBlocks(biddingPeriod + 1, cbAddress);

await wallet.client.post(`/wallet/${wallet.id}/reveal`, {
name: name
});
await wallet.client.post(`/wallet/${wallet2.id}/reveal`, {
name: name
});
await mineBlocks(revealPeriod + 1, cbAddress);

// first update is REGISTER
await wallet.client.post(`/wallet/${wallet.id}/update`, {
name: name,
data: {text: ['foobar']}
});
await wallet.client.post(`/wallet/${wallet2.id}/redeem`, {
name: name
});
await mineBlocks(treeInterval + 1, cbAddress);

const {receiveAddress} = await wallet.getAccount('default');

await wallet.client.post(`/wallet/${wallet.id}/transfer`, {
name,
address: receiveAddress
});
await mineBlocks(transferLockup + 1, cbAddress);

await wallet.client.post(`/wallet/${wallet.id}/finalize`, {
name
});
await mineBlocks(1, cbAddress);

// second update is UPDATE
await wallet.client.post(`/wallet/${wallet.id}/update`, {
name: name,
data: {text: ['foo']}
});
await mineBlocks(treeInterval + 1, cbAddress);

await wallet.client.post(`/wallet/${wallet.id}/renewal`, {
name
});
await mineBlocks(1, cbAddress);

await wallet.client.post(`/wallet/${wallet.id}/revoke`, {
name
});
await mineBlocks(1, cbAddress);

for (const [event, triggered] of Object.entries(seen))
assert(triggered, `Covenant type ${event} not seen`);
});
});

async function sleep(time) {
Expand Down