Skip to content

Commit

Permalink
Move to checkTxRoots and warn checkMerkleRoot deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
junderw committed Jan 17, 2019
1 parent 581ab5f commit e52abec
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
11 changes: 10 additions & 1 deletion src/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,22 @@ class Block {
toHex(headersOnly) {
return this.toBuffer(headersOnly).toString('hex');
}
checkTxRoots() {
return this.__checkMerkleRoot() &&
(this.hasWitnessCommit() ? this.__checkWitnessCommit() : true);
}
checkMerkleRoot() {
console.warn('Deprecation Warning: Block method checkMerkleRoot will be ' +
'deprecated in v5. Please use checkTxRoots instead.');
return this.checkTxRoots();
}
__checkMerkleRoot() {
if (!this.transactions)
throw errorMerkleNoTxes;
const actualMerkleRoot = Block.calculateMerkleRoot(this.transactions);
return this.merkleRoot.compare(actualMerkleRoot) === 0;
}
checkWitnessCommit() {
__checkWitnessCommit() {
if (!this.transactions)
throw errorMerkleNoTxes;
if (!this.hasWitnessCommit())
Expand Down
10 changes: 2 additions & 8 deletions test/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe('Block', function () {
})
})

describe('checkMerkleRoot', function () {
describe('checkTxRoots', function () {
fixtures.valid.forEach(function (f) {
if (f.hex.length === 160) return

Expand All @@ -136,14 +136,8 @@ describe('Block', function () {
})

it('returns ' + f.valid + ' for ' + f.id, function () {
assert.strictEqual(block.checkMerkleRoot(), true)
assert.strictEqual(block.checkTxRoots(), true)
})

if (f.witnessCommit) {
it('validates witness commit for ' + f.id, function () {
assert.strictEqual(block.checkWitnessCommit(), true)
})
}
})
})

Expand Down
13 changes: 12 additions & 1 deletion ts_src/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,14 +208,25 @@ export class Block {
return this.toBuffer(headersOnly).toString('hex')
}

checkTxRoots (): boolean {
return this.__checkMerkleRoot() &&
(this.hasWitnessCommit() ? this.__checkWitnessCommit() : true)
}

checkMerkleRoot (): boolean {
console.warn('Deprecation Warning: Block method checkMerkleRoot will be ' +
'deprecated in v5. Please use checkTxRoots instead.')
return this.checkTxRoots()
}

__checkMerkleRoot (): boolean {
if (!this.transactions) throw errorMerkleNoTxes

const actualMerkleRoot = Block.calculateMerkleRoot(this.transactions)
return this.merkleRoot!.compare(actualMerkleRoot) === 0
}

checkWitnessCommit (): boolean {
__checkWitnessCommit (): boolean {
if (!this.transactions) throw errorMerkleNoTxes
if (!this.hasWitnessCommit()) throw errorWitnessNotSegwit

Expand Down
4 changes: 3 additions & 1 deletion types/block.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export declare class Block {
getUTCDate(): Date;
toBuffer(headersOnly: boolean): Buffer;
toHex(headersOnly: boolean): string;
checkTxRoots(): boolean;
checkMerkleRoot(): boolean;
checkWitnessCommit(): boolean;
__checkMerkleRoot(): boolean;
__checkWitnessCommit(): boolean;
checkProofOfWork(): boolean;
}

0 comments on commit e52abec

Please sign in to comment.