wallet: optimize syncOutputDepth.

This commit is contained in:
Christopher Jeffrey 2016-08-03 17:00:42 -07:00
parent edb9c5afc6
commit 2ada7b9e90
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 42 additions and 56 deletions

View File

@ -986,91 +986,77 @@ Wallet.prototype.getOutputPaths = function getOutputPaths(tx, callback) {
* Sync address depths based on a transaction's outputs. * Sync address depths based on a transaction's outputs.
* This is used for deriving new addresses when * This is used for deriving new addresses when
* a confirmed transaction is seen. * a confirmed transaction is seen.
* @param {TX} tx * @param {WalletMap} map
* @param {Function} callback - Returns [Errr, Boolean] * @param {Function} callback - Returns [Errr, Boolean]
* (true if new addresses were allocated). * (true if new addresses were allocated).
*/ */
Wallet.prototype.syncOutputDepth = function syncOutputDepth(tx, callback) { Wallet.prototype.syncOutputDepth = function syncOutputDepth(map, callback) {
var self = this; var self = this;
var accounts = {}; var accounts = {};
var change = []; var change = [];
var receive = []; var receive = [];
var i, path, unlock; var i, path, unlock;
unlock = this.writeLock.lock(syncOutputDepth, [tx, callback]); unlock = this.writeLock.lock(syncOutputDepth, [map, callback]);
if (!unlock) if (!unlock)
return; return;
callback = utils.wrap(callback, unlock); callback = utils.wrap(callback, unlock);
this.getOutputPaths(tx, function(err, paths) { this.start();
if (err)
return callback(err); utils.forEachSerial(map.outputs, function(output, next) {
var paths = output.paths;
var receiveDepth = -1;
var changeDepth = -1;
for (i = 0; i < paths.length; i++) { for (i = 0; i < paths.length; i++) {
path = paths[i]; path = paths[i];
if (!accounts[path.account]) if (path.change) {
accounts[path.account] = []; if (path.index > changeDepth)
changeDepth = path.index;
accounts[path.account].push(path); } else {
if (path.index > receiveDepth)
receiveDepth = path.index;
}
} }
self.start(); receiveDepth += 2;
changeDepth += 2;
utils.forEachSerial(Object.keys(accounts), function(index, next) { self.getAccount(output.account, function(err, account) {
var paths = accounts[index]; if (err)
var receiveDepth = -1; return next(err);
var changeDepth = -1;
for (i = 0; i < paths.length; i++) { if (!account)
path = paths[i]; return next();
if (path.change) { account.setDepth(receiveDepth, changeDepth, function(err, rcv, chng) {
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) {
if (err) if (err)
return next(err); return next(err);
if (!account) if (rcv)
return next(); receive.push(rcv);
account.setDepth(receiveDepth, changeDepth, function(err, rcv, chng) { if (chng)
if (err) change.push(chng);
return next(err);
if (rcv) next();
receive.push(rcv);
if (chng)
change.push(chng);
next();
});
}); });
}, function(err) { });
if (err) { }, function(err) {
self.drop(); if (err) {
self.drop();
return callback(err);
}
self.commit(function(err) {
if (err)
return callback(err); return callback(err);
} return callback(null, receive, change);
self.commit(function(err) {
if (err)
return callback(err);
return callback(null, receive, change);
});
}); });
}); });
}; };

View File

@ -300,7 +300,7 @@ WalletDB.prototype.syncOutputs = function syncOutputs(tx, map, callback) {
var self = this; var self = this;
utils.forEachSerial(map.getOutputWallets(), function(id, next) { 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) if (err)
return next(err); return next(err);
self.fire(id, 'address', receive, change); 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) { this.fetchWallet(id, callback, function(wallet, callback) {
wallet.syncOutputDepth(tx, callback); wallet.syncOutputDepth(map, callback);
}); });
}; };