chain: determine sync state using last checkpoint's chainwork.

This commit is contained in:
Christopher Jeffrey 2017-08-01 15:17:55 -07:00
parent 860e51e390
commit ee7ad9dc86
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
4 changed files with 42 additions and 7 deletions

View File

@ -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.

View File

@ -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);

View File

@ -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;

View File

@ -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;