From 166fe0ad6ea3442c1149ca3264e063224d1a8eb0 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 13 Jan 2017 12:50:50 -0800 Subject: [PATCH] chain: remove bestHeight tracking. --- lib/blockchain/chain.js | 50 +++++++++++++---------------------------- lib/net/pool.js | 3 +-- 2 files changed, 16 insertions(+), 37 deletions(-) diff --git a/lib/blockchain/chain.js b/lib/blockchain/chain.js index 46480802..5652eca8 100644 --- a/lib/blockchain/chain.js +++ b/lib/blockchain/chain.js @@ -43,7 +43,6 @@ var VerifyResult = errors.VerifyResult; * @property {Number} orphanLimit * @property {Lock} locker * @property {Object} invalid - * @property {Number} bestHeight * @property {ChainEntry?} tip * @property {Number} height * @property {DeploymentState} state @@ -82,7 +81,6 @@ function Chain(options) { this.orphanLimit = options.orphanLimit || (20 << 20); this.locker = new Lock(true); this.invalid = new LRU(100); - this.bestHeight = -1; this.tip = null; this.height = -1; this.synced = false; @@ -198,9 +196,6 @@ Chain.prototype._open = co(function* open() { this.logger.info('Chain Height: %d', tip.height); - if (tip.height > this.bestHeight) - this.bestHeight = tip.height; - this.logger.memory(); state = yield this.getDeploymentState(); @@ -262,9 +257,9 @@ Chain.prototype.isGenesis = function isGenesis(block) { * version deployments (IsSuperMajority), versionbits, * coinbase height, finality checks. * @private - * @param {Block|MerkleBlock} block - * @param {ChainEntry} entry - * @returns {Promise} - Returns [{@link VerifyError}, {@link VerifyFlags}]. + * @param {Block} block + * @param {ChainEntry} prev + * @returns {Promise} - Returns {@link DeploymentState}. */ Chain.prototype.verify = co(function* verify(block, prev) { @@ -841,7 +836,6 @@ Chain.prototype.disconnect = co(function* disconnect(entry) { this.tip = prev; this.height = prev.height; - this.bestHeight = prev.height; this.emit('tip', prev); this.emit('disconnect', entry, block, view); @@ -884,7 +878,6 @@ Chain.prototype.reconnect = co(function* reconnect(entry) { this.tip = entry; this.height = entry.height; - this.bestHeight = entry.height; this.setDeploymentState(result.state); this.emit('tip', entry); @@ -1148,7 +1141,7 @@ Chain.prototype.isBusy = function isBusy() { /** * Add a block to the chain, perform all necessary verification. - * @param {Block|MerkleBlock|MemBlock} block + * @param {Block} block * @returns {Promise} */ @@ -1165,14 +1158,14 @@ Chain.prototype.add = co(function* add(block) { /** * Add a block to the chain without a lock. * @private - * @param {Block|MerkleBlock|MemBlock} block + * @param {Block} block * @returns {Promise} */ Chain.prototype._add = co(function* add(block) { var ret = new VerifyResult(); var initial = true; - var hash, height, entry, prev, result; + var hash, entry, prev, result; while (block) { hash = block.hash('hex'); @@ -1184,7 +1177,7 @@ Chain.prototype._add = co(function* add(block) { if (hash === this.network.genesis.hash) break; - // Do we already have this block? + // Do we already have this block in the queue? if (this.hasPending(hash)) { this.emit('exists', block, block.getCoinbaseHeight()); throw new VerifyError(block, 'duplicate', 'duplicate', 0); @@ -1203,10 +1196,9 @@ Chain.prototype._add = co(function* add(block) { throw new VerifyError(block, 'duplicate', 'duplicate', 100); } - // Validate the block we want to add. - // This is only necessary for new - // blocks coming in, not the resolving - // orphans. + // Non-contextual verification. + // If this is a memblock, it will + // only be a POW validation. if (!block.verify(ret)) { this.emit('invalid', block, block.getCoinbaseHeight()); throw new VerifyError(block, 'invalid', ret.reason, ret.score); @@ -1218,7 +1210,7 @@ Chain.prototype._add = co(function* add(block) { throw new VerifyError(block, 'duplicate', 'duplicate', 0); } - // Find the previous block height/index. + // Find the previous block entry. prev = yield this.db.getEntry(block.prevBlock); // If previous block wasn't ever seen, @@ -1228,14 +1220,8 @@ Chain.prototype._add = co(function* add(block) { throw new VerifyError(block, 'invalid', 'bad-prevblk', 0); } - height = prev.height + 1; - - // Update best height seen. - if (height > this.bestHeight) - this.bestHeight = height; - // Verify a checkpoint if there is one. - if (!this.verifyCheckpoint(hash, height)) { + if (!this.verifyCheckpoint(prev, hash)) { throw new VerifyError(block, 'checkpoint', 'checkpoint mismatch', @@ -1371,12 +1357,13 @@ Chain.prototype.finish = function finish(block, entry) { /** * Verify a block hash and height against the checkpoints. * @private + * @param {ChainEntry} prev * @param {Hash} hash - * @param {Number} height * @returns {Boolean} */ -Chain.prototype.verifyCheckpoint = function verifyCheckpoint(hash, height) { +Chain.prototype.verifyCheckpoint = function verifyCheckpoint(prev, hash) { + var height = prev.height + 1; var checkpoint; if (!this.options.useCheckpoints) @@ -1453,13 +1440,6 @@ Chain.prototype.storeOrphan = function storeOrphan(block) { this.orphanPrev[block.prevBlock] = block; this.orphanMap[hash] = block; - // Update the best height based on the coinbase. - // We do this even for orphans (peers will send - // us their highest block during the initial - // getblocks sync, making it an orphan). - if (height > this.bestHeight) - this.bestHeight = height; - this.emit('orphan', block, height); }; diff --git a/lib/net/pool.js b/lib/net/pool.js index 4281f8b5..70fd2968 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -1002,13 +1002,12 @@ Pool.prototype.handleBlock = co(function* handleBlock(peer, block) { if (this.logger.level >= 4 && this.chain.total % 20 === 0) { this.logger.debug('Status:' - + ' ts=%s height=%d highest=%d progress=%s' + + ' ts=%s height=%d progress=%s' + ' blocks=%d orphans=%d active=%d' + ' queue=%d target=%s peers=%d' + ' pending=%d jobs=%d', util.date(block.ts), this.chain.height, - this.chain.bestHeight, (this.chain.getProgress() * 100).toFixed(2) + '%', this.chain.total, this.chain.orphanCount,