Move to checkTxRoots and warn checkMerkleRoot deprecation

This commit is contained in:
junderw 2019-01-17 17:01:15 +09:00
parent 581ab5f136
commit e52abecee2
No known key found for this signature in database
GPG key ID: B256185D3A971908
4 changed files with 27 additions and 11 deletions

View file

@ -167,13 +167,22 @@ class Block {
toHex(headersOnly) { toHex(headersOnly) {
return this.toBuffer(headersOnly).toString('hex'); return this.toBuffer(headersOnly).toString('hex');
} }
checkTxRoots() {
return this.__checkMerkleRoot() &&
(this.hasWitnessCommit() ? this.__checkWitnessCommit() : true);
}
checkMerkleRoot() { checkMerkleRoot() {
console.warn('Deprecation Warning: Block method checkMerkleRoot will be ' +
'deprecated in v5. Please use checkTxRoots instead.');
return this.checkTxRoots();
}
__checkMerkleRoot() {
if (!this.transactions) if (!this.transactions)
throw errorMerkleNoTxes; throw errorMerkleNoTxes;
const actualMerkleRoot = Block.calculateMerkleRoot(this.transactions); const actualMerkleRoot = Block.calculateMerkleRoot(this.transactions);
return this.merkleRoot.compare(actualMerkleRoot) === 0; return this.merkleRoot.compare(actualMerkleRoot) === 0;
} }
checkWitnessCommit() { __checkWitnessCommit() {
if (!this.transactions) if (!this.transactions)
throw errorMerkleNoTxes; throw errorMerkleNoTxes;
if (!this.hasWitnessCommit()) if (!this.hasWitnessCommit())

View file

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

View file

@ -208,14 +208,25 @@ export class Block {
return this.toBuffer(headersOnly).toString('hex') return this.toBuffer(headersOnly).toString('hex')
} }
checkTxRoots (): boolean {
return this.__checkMerkleRoot() &&
(this.hasWitnessCommit() ? this.__checkWitnessCommit() : true)
}
checkMerkleRoot (): boolean { 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 if (!this.transactions) throw errorMerkleNoTxes
const actualMerkleRoot = Block.calculateMerkleRoot(this.transactions) const actualMerkleRoot = Block.calculateMerkleRoot(this.transactions)
return this.merkleRoot!.compare(actualMerkleRoot) === 0 return this.merkleRoot!.compare(actualMerkleRoot) === 0
} }
checkWitnessCommit (): boolean { __checkWitnessCommit (): boolean {
if (!this.transactions) throw errorMerkleNoTxes if (!this.transactions) throw errorMerkleNoTxes
if (!this.hasWitnessCommit()) throw errorWitnessNotSegwit if (!this.hasWitnessCommit()) throw errorWitnessNotSegwit

4
types/block.d.ts vendored
View file

@ -21,7 +21,9 @@ export declare class Block {
getUTCDate(): Date; getUTCDate(): Date;
toBuffer(headersOnly: boolean): Buffer; toBuffer(headersOnly: boolean): Buffer;
toHex(headersOnly: boolean): string; toHex(headersOnly: boolean): string;
checkTxRoots(): boolean;
checkMerkleRoot(): boolean; checkMerkleRoot(): boolean;
checkWitnessCommit(): boolean; __checkMerkleRoot(): boolean;
__checkWitnessCommit(): boolean;
checkProofOfWork(): boolean; checkProofOfWork(): boolean;
} }