From 09f449167f01221209b4d55b0e0aea091bd7d58c Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Thu, 17 Nov 2016 04:12:25 -0800 Subject: [PATCH] chain: comments. --- lib/chain/chain.js | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/lib/chain/chain.js b/lib/chain/chain.js index 267f4f4e..5ece74e7 100644 --- a/lib/chain/chain.js +++ b/lib/chain/chain.js @@ -315,7 +315,8 @@ Chain.prototype.verify = co(function* verify(block, prev) { if (this.options.spv) return this.state; - // Skip any blocks below the last checkpoint. + // Skip any blocks below the + // last checkpoint. if (!this.options.witness) { // We can't skip this with segwit // enabled since the block may have @@ -330,7 +331,7 @@ Chain.prototype.verify = co(function* verify(block, prev) { ancestors = yield prev.getRetargetAncestors(); medianTime = prev.getMedianTime(ancestors); - // Ensure the timestamp is correct + // Ensure the timestamp is correct. if (block.ts <= medianTime) { throw new VerifyError(block, 'invalid', @@ -338,6 +339,7 @@ Chain.prototype.verify = co(function* verify(block, prev) { 0); } + // Ensure the POW is what we expect. if (block.bits !== this.getTarget(block, prev, ancestors)) { throw new VerifyError(block, 'invalid', @@ -345,9 +347,11 @@ Chain.prototype.verify = co(function* verify(block, prev) { 100); } + // Get the new deployment state. state = yield this.getDeployments(block, prev); - // Make sure the height contained in the coinbase is correct. + // Make sure the height contained + // in the coinbase is correct. if (state.hasBIP34()) { if (block.getCoinbaseHeight() !== height) { throw new VerifyError(block, @@ -412,12 +416,11 @@ Chain.prototype.verify = co(function* verify(block, prev) { // Get timestamp for tx.isFinal(). ts = state.hasMTP() ? medianTime : block.ts; - // Check all transactions + // Transactions must be finalized with + // regards to nSequence and nLockTime. for (i = 0; i < block.txs.length; i++) { tx = block.txs[i]; - // Transactions must be finalized with - // regards to nSequence and nLockTime. if (!tx.isFinal(height, ts)) { throw new VerifyError(block, 'invalid', @@ -478,14 +481,14 @@ Chain.prototype.getDeployments = co(function* getDeployments(block, prev) { throw new VerifyError(block, 'obsolete', 'bad-version', 0); } - // Make sure the height contained in the coinbase is correct. + // Coinbase heights are now enforced (bip34). if (height >= this.network.block.bip34height) { state.bip34 = true; if (!this.state.hasBIP34()) this.logger.warning('BIP34 has been activated.'); } - // Signature validation is now enforced (bip66) + // Signature validation is now enforced (bip66). if (height >= this.network.block.bip66height) { state.flags |= constants.flags.VERIFY_DERSIG; if (!this.state.hasBIP66()) @@ -708,10 +711,12 @@ Chain.prototype.verifyInputs = co(function* verifyInputs(block, prev, state) { */ Chain.prototype.checkHeight = function checkHeight(hash) { - if (this.db.hasCache(hash)) - return this.db.getCache(hash).height; + var entry = this.db.getCache(hash); - return -1; + if (!entry) + return -1; + + return entry.height; }; /** @@ -849,7 +854,6 @@ Chain.prototype.disconnect = co(function* disconnect(entry) { this.tip = prev; this.height = prev.height; - this.bestHeight = prev.height; this.emit('tip', prev); @@ -895,7 +899,6 @@ Chain.prototype.reconnect = co(function* reconnect(entry) { this.tip = entry; this.height = entry.height; this.state = result.state; - this.bestHeight = entry.height; this.emit('tip', entry);