Browse Source

Move to checkTxRoots and warn checkMerkleRoot deprecation

fixTypes
junderw 6 years ago
parent
commit
e52abecee2
No known key found for this signature in database GPG Key ID: B256185D3A971908
  1. 11
      src/block.js
  2. 10
      test/block.js
  3. 13
      ts_src/block.ts
  4. 4
      types/block.d.ts

11
src/block.js

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

10
test/block.js

@ -125,7 +125,7 @@ describe('Block', function () {
})
})
describe('checkMerkleRoot', function () {
describe('checkTxRoots', function () {
fixtures.valid.forEach(function (f) {
if (f.hex.length === 160) return
@ -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)
})
}
})
})

13
ts_src/block.ts

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

4
types/block.d.ts

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

Loading…
Cancel
Save