From 1a4268544f309340a960aa5a932aa06fabee3e4b Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Tue, 1 Aug 2017 01:38:04 -0700 Subject: [PATCH] chain: better checkpoint handling. --- lib/blockchain/chain.js | 20 ++++++++++++++------ lib/net/pool.js | 6 +++--- lib/node/fullnode.js | 1 + lib/node/spvnode.js | 1 + 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/blockchain/chain.js b/lib/blockchain/chain.js index b57fde6a..695e5799 100644 --- a/lib/blockchain/chain.js +++ b/lib/blockchain/chain.js @@ -1469,7 +1469,10 @@ Chain.prototype.logStatus = function logStatus(start, block, entry) { */ Chain.prototype.verifyCheckpoint = function verifyCheckpoint(prev, hash) { - if (!this.checkpoints) + if (!this.options.checkpoints) + return true; + + if (!this.checkpoints && !this.options.enforceCheckpoints) return true; const height = prev.height + 1; @@ -1799,11 +1802,10 @@ Chain.prototype.maybeSync = function maybeSync() { return; if (this.checkpoints) { - if (this.tip.height < this.network.lastCheckpoint) - return; - - this.logger.info('Last checkpoint reached. Disabling checkpoints.'); - this.checkpoints = false; + if (this.tip.height >= this.network.lastCheckpoint || this.hasChainwork()) { + this.logger.info('Last checkpoint reached. Disabling checkpoints.'); + this.checkpoints = false; + } } if (this.tip.time < util.now() - this.network.block.maxTipAge) @@ -2404,6 +2406,7 @@ function ChainOptions(options) { this.entryCache = 5000; this.maxOrphans = 20; this.checkpoints = true; + this.enforceCheckpoints = false; if (options) this.fromOptions(options); @@ -2518,6 +2521,11 @@ ChainOptions.prototype.fromOptions = function fromOptions(options) { this.checkpoints = options.checkpoints; } + if (options.enforceCheckpoints != null) { + assert(typeof options.enforceCheckpoints === 'boolean'); + this.enforceCheckpoints = options.enforceCheckpoints; + } + return this; }; diff --git a/lib/net/pool.js b/lib/net/pool.js index f8c99870..e5a1338d 100644 --- a/lib/net/pool.js +++ b/lib/net/pool.js @@ -242,8 +242,6 @@ Pool.prototype._open = async function _open() { */ Pool.prototype.resetChain = function resetChain() { - const tip = this.chain.tip; - if (!this.options.checkpoints) return; @@ -253,7 +251,9 @@ Pool.prototype.resetChain = function resetChain() { this.headerChain.reset(); this.headerNext = null; - if (tip.height < this.network.lastCheckpoint) { + const tip = this.chain.tip; + + if (tip.height < this.network.lastCheckpoint && !this.chain.hasChainwork()) { this.checkpoints = true; this.chain.checkpoints = true; this.headerTip = this.getNextTip(tip.height); diff --git a/lib/node/fullnode.js b/lib/node/fullnode.js index 68b66dc0..2cb03b42 100644 --- a/lib/node/fullnode.js +++ b/lib/node/fullnode.js @@ -61,6 +61,7 @@ function FullNode(options) { bip148: this.config.bool('bip148'), prune: this.config.bool('prune'), checkpoints: this.config.bool('checkpoints'), + enforceCheckpoints: this.config.bool('enforce-checkpoints'), coinCache: this.config.mb('coin-cache'), entryCache: this.config.num('entry-cache'), indexTX: this.config.bool('index-tx'), diff --git a/lib/node/spvnode.js b/lib/node/spvnode.js index 966dce16..ba4710f0 100644 --- a/lib/node/spvnode.js +++ b/lib/node/spvnode.js @@ -54,6 +54,7 @@ function SPVNode(options) { entryCache: this.config.num('entry-cache'), forceFlags: this.config.bool('force-flags'), checkpoints: this.config.bool('checkpoints'), + enforceCheckpoints: this.config.bool('enforce-checkpoints'), bip91: this.config.bool('bip91'), bip148: this.config.bool('bip148'), spv: true