From e3f023142f9c9e04fb315268332b139a9b7a9619 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 30 Jun 2016 17:48:46 -0700 Subject: [PATCH] wallet events. --- lib/bcoin/fullnode.js | 2 + lib/bcoin/spvnode.js | 2 + lib/bcoin/txdb.js | 2 +- lib/bcoin/walletdb.js | 94 +++++++++++++++++++------------------------ test/wallet-test.js | 7 ++-- 5 files changed, 49 insertions(+), 58 deletions(-) diff --git a/lib/bcoin/fullnode.js b/lib/bcoin/fullnode.js index e86f7f3e..fae46f32 100644 --- a/lib/bcoin/fullnode.js +++ b/lib/bcoin/fullnode.js @@ -281,6 +281,8 @@ Fullnode.prototype._open = function open(callback) { Fullnode.prototype._close = function close(callback) { var self = this; + this.wallet = null; + utils.serial([ function(next) { if (!self.http) diff --git a/lib/bcoin/spvnode.js b/lib/bcoin/spvnode.js index f74eb2ae..d7d94665 100644 --- a/lib/bcoin/spvnode.js +++ b/lib/bcoin/spvnode.js @@ -248,6 +248,8 @@ SPVNode.prototype._open = function open(callback) { SPVNode.prototype._close = function close(callback) { var self = this; + this.wallet = null; + utils.parallel([ function(next) { if (!self.http) diff --git a/lib/bcoin/txdb.js b/lib/bcoin/txdb.js index 83308e65..0c0f3e2a 100644 --- a/lib/bcoin/txdb.js +++ b/lib/bcoin/txdb.js @@ -543,7 +543,7 @@ TXDB.prototype._add = function add(tx, map, callback, force) { if (err) return callback(err); - self.walletdb.syncOutputs(tx, map, function(err) { + self.walletdb.handleTX(tx, map, function(err) { if (err) return callback(err); diff --git a/lib/bcoin/walletdb.js b/lib/bcoin/walletdb.js index c6dc3581..0fa5dcb8 100644 --- a/lib/bcoin/walletdb.js +++ b/lib/bcoin/walletdb.js @@ -49,7 +49,6 @@ function WalletDB(options) { this.options = options; this.network = bcoin.network.get(options.network); - this.watchers = {}; this.db = bcoin.ldb({ network: this.network, @@ -66,6 +65,8 @@ function WalletDB(options) { useFilter: true }); + this.watchers = {}; + this._init(); } @@ -83,75 +84,35 @@ WalletDB.prototype._init = function _init() { self.emit('error', err); }); - this.tx.on('tx', function(tx, map) { + function handleEvent(event, tx, map) { var i, path; - self.emit('tx', tx, map); + self.emit(event, tx, map); for (i = 0; i < map.accounts.length; i++) { path = map.accounts[i]; - self.fire(path.id, 'tx', tx, path.name); + self.fire(path.id, event, tx, path.name); } + } + + this.tx.on('tx', function(tx, map) { + handleEvent('tx', tx, map); }); this.tx.on('conflict', function(tx, map) { - var i, path; - - self.emit('conflict', tx, map); - - for (i = 0; i < map.accounts.length; i++) { - path = map.accounts[i]; - self.fire(path.id, 'conflict', tx, path.name); - } + handleEvent('conflict', tx, map); }); this.tx.on('confirmed', function(tx, map) { - var i, path; - - self.emit('confirmed', tx, map); - - for (i = 0; i < map.accounts.length; i++) { - path = map.accounts[i]; - self.fire(path.id, 'confirmed', tx, path.name); - } + handleEvent('confirmed', tx, map); }); this.tx.on('unconfirmed', function(tx, map) { - var i, path; - - self.emit('unconfirmed', tx, map); - - for (i = 0; i < map.accounts.length; i++) { - path = map.accounts[i]; - self.fire(path.id, 'unconfirmed', tx, path.name); - } + handleEvent('unconfirmed', tx, map); }); this.tx.on('updated', function(tx, map) { - var i, path, keys, id; - - self.emit('updated', tx, map); - - for (i = 0; i < map.accounts.length; i++) { - path = map.accounts[i]; - self.fire(path.id, 'updated', tx, path.name); - } - - self.updateBalances(tx, map, function(err, balances) { - if (err) { - self.emit('error', err); - return; - } - - keys = Object.keys(balances); - - for (i = 0; i < keys.length; i++) { - id = keys[i]; - self.fire(id, 'balance', balances[id]); - } - - self.emit('balances', balances, map); - }); + handleEvent('updated', tx, map); }); }; @@ -206,7 +167,7 @@ WalletDB.prototype._close = function close(callback) { WalletDB.prototype.updateBalances = function updateBalances(tx, map, callback) { var self = this; var balances = {}; - var id; + var i, id, keys; utils.forEachSerial(map.outputs, function(output, next) { id = output.id; @@ -231,6 +192,15 @@ WalletDB.prototype.updateBalances = function updateBalances(tx, map, callback) { if (err) return callback(err); + keys = Object.keys(balances); + + for (i = 0; i < keys.length; i++) { + id = keys[i]; + self.fire(id, 'balance', balances[id]); + } + + self.emit('balances', balances, map); + return callback(null, balances); }); }; @@ -260,6 +230,24 @@ WalletDB.prototype.syncOutputs = function syncOutputs(tx, map, callback) { }, callback); }; +/** + * Derive new addresses and emit balance. + * @private + * @param {TX} tx + * @param {WalletMap} map + * @param {Function} callback + */ + +WalletDB.prototype.handleTX = function handleTX(tx, map, callback) { + var self = this; + this.syncOutputs(tx, map, function(err) { + if (err) + return callback(err); + + self.updateBalances(tx, map, callback); + }); +}; + /** * Dump database (for debugging). * @param {Function} callback - Returns [Error, Object]. diff --git a/test/wallet-test.js b/test/wallet-test.js index eaf7a967..6e5773ab 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -433,10 +433,9 @@ describe('Wallet', function() { var t3 = bcoin.mtx().addOutput(w2, 15000); w1.fill(t3, { rate: 10000 }, function(err) { assert(err); - setTimeout(function() { - assert(balance.total === 5460); - cb(); - }, 100); + assert(balance); + assert(balance.total === 5460); + cb(); }); }); });