Skip to content

Commit

Permalink
multi: rebase bcc onto staging
Browse files Browse the repository at this point in the history
  • Loading branch information
tuxcanfly committed May 1, 2018
2 parents ead34aa + afd34c4 commit be2e702
Show file tree
Hide file tree
Showing 12 changed files with 211 additions and 1,853 deletions.
146 changes: 77 additions & 69 deletions lib/blockchain/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,13 +420,6 @@ class Chain extends AsyncEmitter {
// Get the new deployment state.
const state = await this.getDeployments(block.time, prev);

// Enforce BIP91/BIP148.
if (state.hasBIP91() || state.hasBIP148()) {
const {segwit} = this.network.deployments;
if (!consensus.hasBit(block.version, segwit.bit))
throw new VerifyError(block, 'invalid', 'bad-no-segwit', 0);
}

// Get timestamp for tx.isFinal().
const time = state.hasMTP() ? mtp : block.time;

Expand Down Expand Up @@ -493,9 +486,9 @@ class Chain extends AsyncEmitter {
}
}

// Check block weight (different from block size
// Check block size (different from block size
// check in non-contextual verification).
if (block.getWeight() > consensus.MAX_BLOCK_WEIGHT) {
if (block.getBaseSize() > state.maxBlockSize()) {
throw new VerifyError(block,
'invalid',
'bad-blk-weight',
Expand Down Expand Up @@ -540,6 +533,13 @@ class Chain extends AsyncEmitter {
if (height >= this.network.block.bip65height)
state.flags |= Script.flags.VERIFY_CHECKLOCKTIMEVERIFY;

// UAHF is now enabled.
const mtp = await prev.getMedianTime();
if (mtp >= consensus.UAHF_TIME) {
state.flags = Script.flags.VERIFY_STRICTENC;
state.flags = Script.flags.VERIFY_SIGHASH_FORKID;
}

// CHECKSEQUENCEVERIFY and median time
// past locktimes are now usable (bip9 & bip113).
if (await this.isActive(prev, deployments.csv)) {
Expand All @@ -548,41 +548,6 @@ class Chain extends AsyncEmitter {
state.lockFlags |= common.lockFlags.MEDIAN_TIME_PAST;
}

// Check the state of the segwit deployment.
const witness = await this.getState(prev, deployments.segwit);

// Segregrated witness (bip141) is now usable
// along with SCRIPT_VERIFY_NULLDUMMY (bip147).
if (witness === thresholdStates.ACTIVE) {
state.flags |= Script.flags.VERIFY_WITNESS;
state.flags |= Script.flags.VERIFY_NULLDUMMY;
}

// Segsignal is now enforced (bip91).
if (this.options.bip91) {
if (witness === thresholdStates.STARTED) {
if (await this.isActive(prev, deployments.segsignal))
state.bip91 = true;
}
}

// UASF is now enforced (bip148) (mainnet-only).
if (this.options.bip148 && this.network === Network.main) {
if (witness !== thresholdStates.LOCKED_IN
&& witness !== thresholdStates.ACTIVE) {
// The BIP148 MTP check is nonsensical in
// that it includes the _current_ entry's
// timestamp. This requires some hackery,
// since bcoin only operates on the sane
// assumption that deployment checks should
// only ever examine the values of the
// previous block (necessary for mining).
const mtp = await this.getMedianTime(prev, time);
if (mtp >= 1501545600 && mtp <= 1510704000)
state.bip148 = true;
}
}

return state;
}

Expand Down Expand Up @@ -621,6 +586,9 @@ class Chain extends AsyncEmitter {
if (!this.state.hasBIP148() && state.hasBIP148())
this.logger.warning('BIP148 has been activated.');

if (!this.state.hasUAHF() && state.hasUAHF())
this.logger.warning('UAHF has been activated.');

this.state = state;
}

Expand Down Expand Up @@ -2262,38 +2230,58 @@ class Chain extends AsyncEmitter {
return pow.bits;
}

if ((prev.height + 1) % pow.retargetInterval === 0) {
// Back 2 weeks
const height = prev.height - (pow.retargetInterval - 1);
assert(height >= 0);

prev = await prev.getPrevious();
assert(prev);
const first = await prev.getAncestor(height);
assert(first);

return this.retarget(prev, first);
}

// Do not retarget
if ((prev.height + 1) % pow.retargetInterval !== 0) {
if (pow.targetReset) {
// Special behavior for testnet:
if (time > prev.time + pow.targetSpacing * 2)
return pow.bits;

while (prev.height !== 0
&& prev.height % pow.retargetInterval !== 0
&& prev.bits === pow.bits) {
const cache = this.getPrevCache(prev);

if (cache)
prev = cache;
else
prev = await this.getPrevious(prev);

assert(prev);
if (pow.targetReset) {
// Special behavior for testnet:
if (time > prev.time + pow.targetSpacing * 2)
return pow.bits;

while (prev.height !== 0
&& prev.height % pow.retargetInterval !== 0
&& prev.bits === pow.bits) {
const cache = prev.getPrevCache();

if (cache) {
prev = cache;
continue;
}

prev = await prev.getPrevious();
assert(prev);
}
return prev.bits;
}

// Back 2 weeks
const height = prev.height - (pow.retargetInterval - 1);
assert(height >= 0);
if (prev.bits === pow.bits)
return prev.bits;

const first = await this.getAncestor(prev, height);
assert(first);
const prev6 = await prev.getAncestor((prev.height + 1) - 7);
assert(prev6);
const mtp6 = (await prev.getMedianTime()) - (await prev6.getMedianTime());

return this.retarget(prev, first);
}
if (mtp6 < 12 * 3600)
return prev.bits;

const target = consensus.fromCompact(prev.bits);
target.iadd(target.ushrn(2));

if (target.cmp(pow.limit) > 0)
return pow.bits;

return consensus.toCompact(target);
};

/**
* Retarget. This is called when the chain height
Expand Down Expand Up @@ -2894,6 +2882,26 @@ class DeploymentState {
}
}

/**
* Test whether UAHF is active.
* @returns {Boolean}
*/

DeploymentState.prototype.hasUAHF = function hasUAHF() {
return (this.flags & Script.flags.VERIFY_SIGHASH_FORKID) !== 0;
};

/**
* Test whether UAHF is active.
* @returns {Boolean}
*/

DeploymentState.prototype.maxBlockSize = function maxBlockSize() {
return this.hasUAHF()
? consensus.MAX_FORK_BLOCK_SIZE
: consensus.MAX_BLOCK_SIZE;
};

/**
* Orphan
* @ignore
Expand Down
2 changes: 1 addition & 1 deletion lib/blockchain/chaindb.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ChainDB {
this.logger.info('Opening ChainDB...');

await this.db.open();
await this.db.verify(layout.V.build(), 'chain', 5);
await this.db.verify(layout.V.build(), 'chain', 0x80 | 5);

const state = await this.getState();

Expand Down
2 changes: 1 addition & 1 deletion lib/net/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ exports.USER_AGENT = `/bcoin:${pkg.version}/`;
* @default
*/

exports.MAX_MESSAGE = 4 * 1000 * 1000;
exports.MAX_MESSAGE = 8 * 1000 * 1000;

/**
* Amount of time to ban misbheaving peers.
Expand Down
Loading

0 comments on commit be2e702

Please sign in to comment.