chain: earlier sanity checks.

This commit is contained in:
Christopher Jeffrey 2018-07-03 18:28:41 -07:00
parent 1ca3738699
commit 22830cf6f3
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 27 additions and 13 deletions

View File

@ -347,12 +347,26 @@ class Chain extends AsyncEmitter {
if (this.options.spv)
return this.state;
// Check merkle root.
if (flags & common.flags.VERIFY_BODY) {
assert(typeof block.createMerkleRoot === 'function');
const root = block.createMerkleRoot('hex');
if (!root || block.merkleRoot !== root) {
throw new VerifyError(block,
'invalid',
'bad-txnmrklroot',
100);
}
flags &= ~common.flags.VERIFY_BODY;
}
// Once segwit is active, we will still
// need to check for block mutability.
if (!block.hasWitness() && !block.getCommitmentHash())
return new DeploymentState();
flags &= ~common.flags.VERIFY_BODY;
}
// Non-contextual checks.

View File

@ -416,17 +416,6 @@ class Block extends AbstractBlock {
*/
checkBody() {
// Check merkle root.
const root = this.createMerkleRoot('hex');
// If the merkle is mutated,
// we have duplicate txs.
if (!root)
return [false, 'bad-txns-duplicate', 100];
if (this.merkleRoot !== root)
return [false, 'bad-txnmrklroot', 100];
// Check base size.
if (this.txs.length === 0
|| this.txs.length > consensus.MAX_BLOCK_SIZE
@ -438,6 +427,17 @@ class Block extends AbstractBlock {
if (this.txs.length === 0 || !this.txs[0].isCoinbase())
return [false, 'bad-cb-missing', 100];
// Check merkle root.
const root = this.createMerkleRoot('hex');
// If the merkle is mutated,
// we have duplicate txs.
if (!root)
return [false, 'bad-txns-duplicate', 100];
if (this.merkleRoot !== root)
return [false, 'bad-txnmrklroot', 100];
// Test all transactions.
const scale = consensus.WITNESS_SCALE_FACTOR;