From ecd18b961376f49a888dfbe0d582e3fd79f132b7 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 11 Nov 2016 18:16:18 -0800 Subject: [PATCH] walletdb: refactor. --- lib/wallet/walletdb.js | 56 +++++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/lib/wallet/walletdb.js b/lib/wallet/walletdb.js index 59841d4d..9905e6ca 100644 --- a/lib/wallet/walletdb.js +++ b/lib/wallet/walletdb.js @@ -1424,6 +1424,38 @@ WalletDB.prototype.resend = co(function* resend() { } }); +/** + * Insert all wallet ids for an input + * (special behavior for SPV). + * @private + * @param {Input} input + * @param {WalletID[]} result + * @returns {Promise} + */ + +WalletDB.prototype.testInput = co(function* testInput(input, result) { + var i, hash, map; + + if (!this.options.resolution) + return; + + hash = input.getHash('hex'); + + if (!hash) + return; + + if (!this.testFilter(hash)) + return; + + map = yield this.getPathMap(hash); + + if (!map) + return; + + for (i = 0; i < map.wids.length; i++) + utils.binaryInsert(result, map.wids[i], cmp, true); +}); + /** * Get all wallet ids by output addresses and outpoints. * @param {Hash[]} hashes @@ -1441,32 +1473,16 @@ WalletDB.prototype.getWalletsByTX = co(function* getWalletsByTX(tx) { prevout = input.prevout; if (!this.testFilter(prevout.toRaw())) { - // Special behavior for SPV. - if (this.options.resolution) { - hash = input.getHash('hex'); - - if (!hash) - continue; - - if (!this.testFilter(hash)) - continue; - - map = yield this.getPathMap(hash); - - if (!map) - continue; - - for (j = 0; j < map.wids.length; j++) - utils.binaryInsert(result, map.wids[j], cmp, true); - } - + yield this.testInput(input, result); continue; } map = yield this.getOutpointMap(prevout.hash, prevout.index); - if (!map) + if (!map) { + yield this.testInput(input, result); continue; + } for (j = 0; j < map.wids.length; j++) utils.binaryInsert(result, map.wids[j], cmp, true);