From bc4e9e73878fba59ea3fc4264adc5271d2a2b2e7 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 5 Oct 2016 04:29:10 -0700 Subject: [PATCH] node: scan method. --- lib/node/fullnode.js | 12 +++++++++++- lib/node/spvnode.js | 28 ++++++++++++++++++++++++---- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/lib/node/fullnode.js b/lib/node/fullnode.js index e92ea094..e36405c8 100644 --- a/lib/node/fullnode.js +++ b/lib/node/fullnode.js @@ -294,7 +294,17 @@ Fullnode.prototype.rescan = function rescan() { // Always rescan to make sure we didn't // miss anything: there is no atomicity // between the chaindb and walletdb. - return this.walletdb.rescan(this.chain.db); + return this.scan(); +}; + +/** + * Rescan for any missed transactions. + * @param {Number} height + * @returns {Promise} + */ + +Fullnode.prototype.scan = function scan(height) { + return this.walletdb.rescan(this.chain.db, height); }; /** diff --git a/lib/node/spvnode.js b/lib/node/spvnode.js index 87be0fce..173ec5f6 100644 --- a/lib/node/spvnode.js +++ b/lib/node/spvnode.js @@ -227,16 +227,36 @@ SPVNode.prototype.rescan = function rescan() { this.chain.height); } - if (this.walletdb.height === 0) - return Promise.resolve(null); - // Always replay the last block to make // sure we didn't miss anything: there // is no atomicity between the chaindb // and walletdb. - return this.chain.reset(this.walletdb.height - 1); + return this.scan(); }; +/** + * Scan for any missed transactions. + * Note that this will replay the blockchain sync. + * @param {Number|Hash} height + * @returns {Promise} + */ + +SPVNode.prototype.scan = co(function* scan(height) { + if (height == null) + height = this.walletdb.height; + + if (typeof height === 'string') { + height = yield this.chain.db.getHeight(height); + if (height === -1) + return; + } + + if (height === 0) + return; + + yield this.chain.reset(height - 1); +}); + /** * Broadcast a transaction (note that this will _not_ be verified * by the mempool - use with care, lest you get banned from