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.
* 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);
});
});
};

View File

@ -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);
});
};