From 77032f758d713d43f62cc4134cf8b2a55cbda76c Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 23 Jan 2017 12:41:07 -0800 Subject: [PATCH] chain: different checkpoints handling. --- lib/blockchain/chain.js | 53 ++++++++++++++---------------------- lib/blockchain/chainentry.js | 2 +- lib/protocol/networks.js | 10 ++++--- 3 files changed, 28 insertions(+), 37 deletions(-) diff --git a/lib/blockchain/chain.js b/lib/blockchain/chain.js index 37ebc397..28d9c903 100644 --- a/lib/blockchain/chain.js +++ b/lib/blockchain/chain.js @@ -71,6 +71,7 @@ function Chain(options) { this.network = this.options.network; this.logger = this.options.logger; + this.checkpoints = this.options.checkpoints; this.locker = new Lock(true); this.invalid = new LRU(100); @@ -1012,7 +1013,7 @@ Chain.prototype._reset = co(function* reset(block, silent) { // Reset state. this.tip = tip; this.height = tip.height; - this.synced = this.isFull(); + this.synced = false; state = yield this.getDeploymentState(); @@ -1027,6 +1028,8 @@ Chain.prototype._reset = co(function* reset(block, silent) { // have been some orphans on a forked chain we // no longer need. this.purgeOrphans(); + + this.maybeSync(); }); /** @@ -1344,7 +1347,7 @@ Chain.prototype.verifyCheckpoint = function verifyCheckpoint(prev, hash) { var height = prev.height + 1; var checkpoint; - if (!this.options.checkpoints) + if (!this.checkpoints) return true; checkpoint = this.network.checkpointMap[height]; @@ -1670,30 +1673,7 @@ Chain.prototype.getCoinView = co(function* getCoinView(tx) { */ Chain.prototype.isFull = function isFull() { - return !this.isInitial(); -}; - -/** - * Test the chain to see if it is still in the initial - * syncing phase. Mimic's bitcoind's `IsInitialBlockDownload()` - * function. - * @see IsInitalBlockDownload() - * @returns {Boolean} - */ - -Chain.prototype.isInitial = function isInitial() { - if (this.options.checkpoints) { - if (this.height < this.network.lastCheckpoint) - return true; - } - - if (this.tip.ts < util.now() - this.network.block.maxTipAge) - return true; - - if (this.tip.chainwork.cmp(this.network.pow.chainwork) < 0) - return true; - - return false; + return this.synced; }; /** @@ -1702,10 +1682,22 @@ Chain.prototype.isInitial = function isInitial() { */ Chain.prototype.maybeSync = function maybeSync() { - if (!this.synced && this.isFull()) { - this.synced = true; - this.emit('full'); + if (this.synced) + return; + + if (!this.hasChainwork()) + return; + + if (this.checkpoints) { + this.logger.info('Minimum chainwork reached. Disabling checkpoints.'); + this.checkpoints = false; } + + if (this.tip.ts < util.now() - this.network.block.maxTipAge) + return; + + this.synced = true; + this.emit('full'); }; /** @@ -1716,9 +1708,6 @@ Chain.prototype.maybeSync = function maybeSync() { */ Chain.prototype.hasChainwork = function hasChainwork() { - if (this.options.checkpoints) - return this.height >= this.network.lastCheckpoint; - return this.tip.chainwork.cmp(this.network.pow.chainwork) >= 0; }; diff --git a/lib/blockchain/chainentry.js b/lib/blockchain/chainentry.js index ffec6195..1721df13 100644 --- a/lib/blockchain/chainentry.js +++ b/lib/blockchain/chainentry.js @@ -353,7 +353,7 @@ ChainEntry.prototype.getMedianTimeAsync = co(function* getMedianTimeAsync() { */ ChainEntry.prototype.isHistorical = function isHistorical() { - if (this.chain.options.checkpoints) { + if (this.chain.checkpoints) { if (this.height + 1 <= this.chain.network.lastCheckpoint) return true; } diff --git a/lib/protocol/networks.js b/lib/protocol/networks.js index d5dc6f16..89ed44d9 100644 --- a/lib/protocol/networks.js +++ b/lib/protocol/networks.js @@ -99,7 +99,8 @@ main.checkpointMap = { 382320: 'b28afdde92b0899715e40362f56afdb20e3d135bedc68d0a0000000000000000', // Custom checkpoints 401465: 'eed16cb3e893ed9366f27c39a9ecd95465d02e3ef40e45010000000000000000', - 420000: 'a1ff746b2d42b834cb7d6b8981b09c265c2cabc016e8cc020000000000000000' + 420000: 'a1ff746b2d42b834cb7d6b8981b09c265c2cabc016e8cc020000000000000000', + 440000: '9bf296b8de5f834f7635d5e258a434ad51b4dbbcf7c08c030000000000000000' }; /** @@ -180,7 +181,7 @@ main.pow = { */ chainwork: new BN( - '0000000000000000000000000000000000000000003a315fa3a5ef47f4384cf2', + '0000000000000000000000000000000000000000002fa4573e5f9cf6ca3e5e75', 'hex' ), @@ -505,7 +506,8 @@ testnet.checkpointMap = { 700000: 'c14d3f6a1e7c7d66fd940951e44f3c3be1273bea4d2ab1786140000000000000', 780000: '0381582e34c3755964dc2813e2b33e521e5596367144e1670851050000000000', 840000: 'dac1648107bd4394e57e4083c86d42b548b1cfb119665f179ea80a0000000000', - 900000: '9bd8ac418beeb1a2cf5d68c8b5c6ebaa947a5b766e5524898d6f350000000000' + 900000: '9bd8ac418beeb1a2cf5d68c8b5c6ebaa947a5b766e5524898d6f350000000000', + 1050000: 'd8190cf0af7f08e179cab51d67db0b44b87951a78f7fdc31b4a01a0000000000' }; testnet.lastCheckpoint = 900000; @@ -541,7 +543,7 @@ testnet.pow = { ), bits: 486604799, chainwork: new BN( - '00000000000000000000000000000000000000000000001e345893fa639796e9', + '00000000000000000000000000000000000000000000001a461538dc48da1a06', 'hex' ), targetTimespan: 14 * 24 * 60 * 60,