From 97703e4c7d9e84123bef2aea88866125d2fb933c Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 17 Nov 2016 03:02:46 -0800 Subject: [PATCH] chain: only expose state if block is successfully written. --- lib/chain/chain.js | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/lib/chain/chain.js b/lib/chain/chain.js index 8cd23c73..27007cfa 100644 --- a/lib/chain/chain.js +++ b/lib/chain/chain.js @@ -249,17 +249,18 @@ Chain.prototype._close = function close() { */ Chain.prototype.verifyContext = co(function* verifyContext(block, prev) { - var state = yield this.verify(block, prev); - var view; + var state, view; + // Initial non-contextual verification. + state = yield this.verify(block, prev); + + // BIP30 - Verify there are no duplicate txids. yield this.verifyDuplicates(block, prev, state); + // Verify scripts, spend and add coins. view = yield this.verifyInputs(block, prev, state); - // Expose the state globally. - this.state = state; - - return view; + return new ContextResult(view, state); }); /** @@ -869,7 +870,7 @@ Chain.prototype.disconnect = co(function* disconnect(entry) { Chain.prototype.reconnect = co(function* reconnect(entry) { var block = yield this.db.getBlock(entry.hash); - var prev, view; + var prev, result; if (this.options.spv) { assert(!block); @@ -882,7 +883,7 @@ Chain.prototype.reconnect = co(function* reconnect(entry) { assert(prev); try { - view = yield this.verifyContext(block, prev); + result = yield this.verifyContext(block, prev); } catch (e) { if (e.type === 'VerifyError') { if (!e.malleated) @@ -892,10 +893,11 @@ Chain.prototype.reconnect = co(function* reconnect(entry) { throw e; } - yield this.db.reconnect(entry, block, view); + yield this.db.reconnect(entry, block, result.view); this.tip = entry; this.height = entry.height; + this.state = result.state; this.bestHeight = entry.height; this.network.updateHeight(entry.height); @@ -918,7 +920,7 @@ Chain.prototype.reconnect = co(function* reconnect(entry) { */ Chain.prototype.setBestChain = co(function* setBestChain(entry, block, prev) { - var view; + var result; // A higher fork has arrived. // Time to reorganize the chain. @@ -938,7 +940,7 @@ Chain.prototype.setBestChain = co(function* setBestChain(entry, block, prev) { // now that we're certain its previous // block is in the chain. try { - view = yield this.verifyContext(block, prev); + result = yield this.verifyContext(block, prev); } catch (e) { // Couldn't verify block. // Revert the height. @@ -954,10 +956,12 @@ Chain.prototype.setBestChain = co(function* setBestChain(entry, block, prev) { } // Save block and connect inputs. - yield this.db.save(entry, block, view); + yield this.db.save(entry, block, result.view); + // Expose the new state. this.tip = entry; this.height = entry.height; + this.state = result.state; this.emit('tip', entry); }); @@ -2290,6 +2294,15 @@ function LockTimes(height, time) { this.time = time; } +/* + * ContextResult + */ + +function ContextResult(view, state) { + this.view = view; + this.state = state; +} + /* * Expose */