From ee7ad9dc86e89bc57c75d10b8fc13ca41c79d820 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 1 Aug 2017 15:17:55 -0700 Subject: [PATCH] chain: determine sync state using last checkpoint's chainwork. --- lib/blockchain/chain.js | 20 ++++++++++++++++---- lib/net/pool.js | 5 ++--- lib/protocol/network.js | 1 + lib/protocol/networks.js | 23 +++++++++++++++++++++++ 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/lib/blockchain/chain.js b/lib/blockchain/chain.js index 695e5799..85ab2259 100644 --- a/lib/blockchain/chain.js +++ b/lib/blockchain/chain.js @@ -1802,10 +1802,11 @@ Chain.prototype.maybeSync = function maybeSync() { return; if (this.checkpoints) { - if (this.tip.height >= this.network.lastCheckpoint || this.hasChainwork()) { - this.logger.info('Last checkpoint reached. Disabling checkpoints.'); - this.checkpoints = false; - } + if (!this.hasCheckpoints()) + return; + + this.logger.info('Last checkpoint reached. Disabling checkpoints.'); + this.checkpoints = false; } if (this.tip.time < util.now() - this.network.block.maxTipAge) @@ -1829,6 +1830,17 @@ Chain.prototype.hasChainwork = function hasChainwork() { return this.tip.chainwork.cmp(this.network.pow.chainwork) >= 0; }; +/** + * Test the chain to see if it has the + * minimum required chainwork for the + * last checkpointed block. + * @returns {Boolean} + */ + +Chain.prototype.hasCheckpoints = function hasCheckpoints() { + return this.tip.chainwork.cmp(this.network.lastChainwork) >= 0; +}; + /** * Get the fill percentage. * @returns {Number} percent - Ranges from 0.0 to 1.0. diff --git a/lib/net/pool.js b/lib/net/pool.js index e5a1338d..05ed24bb 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -251,9 +251,8 @@ Pool.prototype.resetChain = function resetChain() { this.headerChain.reset(); this.headerNext = null; - const tip = this.chain.tip; - - if (tip.height < this.network.lastCheckpoint && !this.chain.hasChainwork()) { + if (!this.chain.hasCheckpoints()) { + const tip = this.chain.tip; this.checkpoints = true; this.chain.checkpoints = true; this.headerTip = this.getNextTip(tip.height); diff --git a/lib/protocol/network.js b/lib/protocol/network.js index 88599b43..cb36346d 100644 --- a/lib/protocol/network.js +++ b/lib/protocol/network.js @@ -32,6 +32,7 @@ function Network(options) { this.port = options.port; this.checkpointMap = options.checkpointMap; this.lastCheckpoint = options.lastCheckpoint; + this.lastChainwork = options.lastChainwork; this.checkpoints = []; this.halvingInterval = options.halvingInterval; this.genesis = options.genesis; diff --git a/lib/protocol/networks.js b/lib/protocol/networks.js index e035baf2..9611c948 100644 --- a/lib/protocol/networks.js +++ b/lib/protocol/networks.js @@ -112,6 +112,17 @@ main.checkpointMap = { main.lastCheckpoint = 470000; +/** + * Last checkpoint chainwork. + * @const {BN} + * @default + */ + +main.lastChainwork = new BN( + '0000000000000000000000000000000000000000005dcc420d5bcce89a5d0d04', + 'hex' +); + /** * @const {Number} * @default @@ -513,6 +524,10 @@ testnet.checkpointMap = { }; testnet.lastCheckpoint = 1050000; +testnet.lastChainwork = new BN( + '00000000000000000000000000000000000000000000001a461538dc48da1a06', + 'hex' +); testnet.halvingInterval = 210000; @@ -674,6 +689,10 @@ regtest.port = 48444; regtest.checkpointMap = {}; regtest.lastCheckpoint = 0; +regtest.lastChainwork = new BN( + '0000000000000000000000000000000000000000000000000000000000000002', + 'hex' +); regtest.halvingInterval = 150; @@ -836,6 +855,10 @@ simnet.port = 18555; simnet.checkpointMap = {}; simnet.lastCheckpoint = 0; +simnet.lastChainwork = new BN( + '0000000000000000000000000000000000000000000000000000000000000002', + 'hex' +); simnet.halvingInterval = 210000;