From 2ada7b9e903b0dfd8b03c7d93168666606a03aa8 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 3 Aug 2016 17:00:42 -0700 Subject: [PATCH] wallet: optimize syncOutputDepth. --- lib/bcoin/wallet.js | 92 ++++++++++++++++++------------------------- lib/bcoin/walletdb.js | 6 +-- 2 files changed, 42 insertions(+), 56 deletions(-) diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 0c1bb6e3..a0ac0d8d 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -986,91 +986,77 @@ Wallet.prototype.getOutputPaths = function getOutputPaths(tx, callback) { * Sync address depths based on a transaction's outputs. * This is used for deriving new addresses when * a confirmed transaction is seen. - * @param {TX} tx + * @param {WalletMap} map * @param {Function} callback - Returns [Errr, Boolean] * (true if new addresses were allocated). */ -Wallet.prototype.syncOutputDepth = function syncOutputDepth(tx, callback) { +Wallet.prototype.syncOutputDepth = function syncOutputDepth(map, callback) { var self = this; var accounts = {}; var change = []; var receive = []; var i, path, unlock; - unlock = this.writeLock.lock(syncOutputDepth, [tx, callback]); + unlock = this.writeLock.lock(syncOutputDepth, [map, callback]); if (!unlock) return; callback = utils.wrap(callback, unlock); - this.getOutputPaths(tx, function(err, paths) { - if (err) - return callback(err); + this.start(); + + utils.forEachSerial(map.outputs, function(output, next) { + var paths = output.paths; + var receiveDepth = -1; + var changeDepth = -1; for (i = 0; i < paths.length; i++) { path = paths[i]; - if (!accounts[path.account]) - accounts[path.account] = []; - - accounts[path.account].push(path); + if (path.change) { + if (path.index > changeDepth) + changeDepth = path.index; + } else { + if (path.index > receiveDepth) + receiveDepth = path.index; + } } - self.start(); + receiveDepth += 2; + changeDepth += 2; - utils.forEachSerial(Object.keys(accounts), function(index, next) { - var paths = accounts[index]; - var receiveDepth = -1; - var changeDepth = -1; + self.getAccount(output.account, function(err, account) { + if (err) + return next(err); - for (i = 0; i < paths.length; i++) { - path = paths[i]; + if (!account) + return next(); - if (path.change) { - if (path.index > changeDepth) - changeDepth = path.index; - } else { - if (path.index > receiveDepth) - receiveDepth = path.index; - } - } - - receiveDepth += 2; - changeDepth += 2; - - self.getAccount(+index, function(err, account) { + account.setDepth(receiveDepth, changeDepth, function(err, rcv, chng) { if (err) return next(err); - if (!account) - return next(); + if (rcv) + receive.push(rcv); - account.setDepth(receiveDepth, changeDepth, function(err, rcv, chng) { - if (err) - return next(err); + if (chng) + change.push(chng); - if (rcv) - receive.push(rcv); - - if (chng) - change.push(chng); - - next(); - }); + next(); }); - }, function(err) { - if (err) { - self.drop(); + }); + }, function(err) { + if (err) { + self.drop(); + return callback(err); + } + + self.commit(function(err) { + if (err) return callback(err); - } - - self.commit(function(err) { - if (err) - return callback(err); - return callback(null, receive, change); - }); + return callback(null, receive, change); }); }); }; diff --git a/lib/bcoin/walletdb.js b/lib/bcoin/walletdb.js index 72511ef9..d578e743 100644 --- a/lib/bcoin/walletdb.js +++ b/lib/bcoin/walletdb.js @@ -300,7 +300,7 @@ WalletDB.prototype.syncOutputs = function syncOutputs(tx, map, callback) { var self = this; utils.forEachSerial(map.getOutputWallets(), function(id, next) { - self.syncOutputDepth(id, tx, function(err, receive, change) { + self.syncOutputDepth(id, map, function(err, receive, change) { if (err) return next(err); self.fire(id, 'address', receive, change); @@ -1366,9 +1366,9 @@ WalletDB.prototype.fetchWallet = function fetchWallet(id, callback, handler) { }); }; -WalletDB.prototype.syncOutputDepth = function syncOutputDepth(id, tx, callback) { +WalletDB.prototype.syncOutputDepth = function syncOutputDepth(id, map, callback) { this.fetchWallet(id, callback, function(wallet, callback) { - wallet.syncOutputDepth(tx, callback); + wallet.syncOutputDepth(map, callback); }); };