chain: different checkpoints handling.

This commit is contained in:
Christopher Jeffrey 2017-01-23 12:41:07 -08:00
parent 3374bffd14
commit 77032f758d
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 28 additions and 37 deletions

View File

@ -71,6 +71,7 @@ function Chain(options) {
this.network = this.options.network; this.network = this.options.network;
this.logger = this.options.logger; this.logger = this.options.logger;
this.checkpoints = this.options.checkpoints;
this.locker = new Lock(true); this.locker = new Lock(true);
this.invalid = new LRU(100); this.invalid = new LRU(100);
@ -1012,7 +1013,7 @@ Chain.prototype._reset = co(function* reset(block, silent) {
// Reset state. // Reset state.
this.tip = tip; this.tip = tip;
this.height = tip.height; this.height = tip.height;
this.synced = this.isFull(); this.synced = false;
state = yield this.getDeploymentState(); 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 // have been some orphans on a forked chain we
// no longer need. // no longer need.
this.purgeOrphans(); this.purgeOrphans();
this.maybeSync();
}); });
/** /**
@ -1344,7 +1347,7 @@ Chain.prototype.verifyCheckpoint = function verifyCheckpoint(prev, hash) {
var height = prev.height + 1; var height = prev.height + 1;
var checkpoint; var checkpoint;
if (!this.options.checkpoints) if (!this.checkpoints)
return true; return true;
checkpoint = this.network.checkpointMap[height]; checkpoint = this.network.checkpointMap[height];
@ -1670,30 +1673,7 @@ Chain.prototype.getCoinView = co(function* getCoinView(tx) {
*/ */
Chain.prototype.isFull = function isFull() { Chain.prototype.isFull = function isFull() {
return !this.isInitial(); return this.synced;
};
/**
* 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;
}; };
/** /**
@ -1702,10 +1682,22 @@ Chain.prototype.isInitial = function isInitial() {
*/ */
Chain.prototype.maybeSync = function maybeSync() { Chain.prototype.maybeSync = function maybeSync() {
if (!this.synced && this.isFull()) { if (this.synced)
this.synced = true; return;
this.emit('full');
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() { 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; return this.tip.chainwork.cmp(this.network.pow.chainwork) >= 0;
}; };

View File

@ -353,7 +353,7 @@ ChainEntry.prototype.getMedianTimeAsync = co(function* getMedianTimeAsync() {
*/ */
ChainEntry.prototype.isHistorical = function isHistorical() { ChainEntry.prototype.isHistorical = function isHistorical() {
if (this.chain.options.checkpoints) { if (this.chain.checkpoints) {
if (this.height + 1 <= this.chain.network.lastCheckpoint) if (this.height + 1 <= this.chain.network.lastCheckpoint)
return true; return true;
} }

View File

@ -99,7 +99,8 @@ main.checkpointMap = {
382320: 'b28afdde92b0899715e40362f56afdb20e3d135bedc68d0a0000000000000000', 382320: 'b28afdde92b0899715e40362f56afdb20e3d135bedc68d0a0000000000000000',
// Custom checkpoints // Custom checkpoints
401465: 'eed16cb3e893ed9366f27c39a9ecd95465d02e3ef40e45010000000000000000', 401465: 'eed16cb3e893ed9366f27c39a9ecd95465d02e3ef40e45010000000000000000',
420000: 'a1ff746b2d42b834cb7d6b8981b09c265c2cabc016e8cc020000000000000000' 420000: 'a1ff746b2d42b834cb7d6b8981b09c265c2cabc016e8cc020000000000000000',
440000: '9bf296b8de5f834f7635d5e258a434ad51b4dbbcf7c08c030000000000000000'
}; };
/** /**
@ -180,7 +181,7 @@ main.pow = {
*/ */
chainwork: new BN( chainwork: new BN(
'0000000000000000000000000000000000000000003a315fa3a5ef47f4384cf2', '0000000000000000000000000000000000000000002fa4573e5f9cf6ca3e5e75',
'hex' 'hex'
), ),
@ -505,7 +506,8 @@ testnet.checkpointMap = {
700000: 'c14d3f6a1e7c7d66fd940951e44f3c3be1273bea4d2ab1786140000000000000', 700000: 'c14d3f6a1e7c7d66fd940951e44f3c3be1273bea4d2ab1786140000000000000',
780000: '0381582e34c3755964dc2813e2b33e521e5596367144e1670851050000000000', 780000: '0381582e34c3755964dc2813e2b33e521e5596367144e1670851050000000000',
840000: 'dac1648107bd4394e57e4083c86d42b548b1cfb119665f179ea80a0000000000', 840000: 'dac1648107bd4394e57e4083c86d42b548b1cfb119665f179ea80a0000000000',
900000: '9bd8ac418beeb1a2cf5d68c8b5c6ebaa947a5b766e5524898d6f350000000000' 900000: '9bd8ac418beeb1a2cf5d68c8b5c6ebaa947a5b766e5524898d6f350000000000',
1050000: 'd8190cf0af7f08e179cab51d67db0b44b87951a78f7fdc31b4a01a0000000000'
}; };
testnet.lastCheckpoint = 900000; testnet.lastCheckpoint = 900000;
@ -541,7 +543,7 @@ testnet.pow = {
), ),
bits: 486604799, bits: 486604799,
chainwork: new BN( chainwork: new BN(
'00000000000000000000000000000000000000000000001e345893fa639796e9', '00000000000000000000000000000000000000000000001a461538dc48da1a06',
'hex' 'hex'
), ),
targetTimespan: 14 * 24 * 60 * 60, targetTimespan: 14 * 24 * 60 * 60,