|
|
@ -150,13 +150,11 @@ MerkleBlock.prototype.validMerkleTree = function validMerkleTree() { |
|
|
|
} |
|
|
|
|
|
|
|
// Can't have more hashes than numTransactions
|
|
|
|
// TODO: Test for this condition
|
|
|
|
if(this.hashes.length.length > this.numTransactions) { |
|
|
|
if(this.hashes.length > this.numTransactions) { |
|
|
|
return this._setValidMerkleTree(false); |
|
|
|
} |
|
|
|
|
|
|
|
// Can't have more flag bits than num hashes
|
|
|
|
// TODO: Test for this condition
|
|
|
|
if(this.flags.length * 8 < this.hashes.length) { |
|
|
|
return this._setValidMerkleTree(false); |
|
|
|
} |
|
|
@ -168,15 +166,13 @@ MerkleBlock.prototype.validMerkleTree = function validMerkleTree() { |
|
|
|
height++; |
|
|
|
} |
|
|
|
|
|
|
|
var txs = []; |
|
|
|
var flagBitsUsed = 0; |
|
|
|
var hashesUsed = 0; |
|
|
|
|
|
|
|
var root = traverse(height, 0); |
|
|
|
if(hashesUsed !== this.hashes.length) { |
|
|
|
return this._setValidMerkleTree(false); |
|
|
|
// Modeled after Bitcoin Core merkleblock.h CalcTreeWidth()
|
|
|
|
function calcTreeWidth(height) { |
|
|
|
return (self.numTransactions + (1 << height) - 1) >> height; |
|
|
|
} |
|
|
|
return this._setValidMerkleTree(BufferUtil.equals(root, this.header.merkleRoot)); |
|
|
|
|
|
|
|
// Modeled after Bitcoin Core merkleblock.cpp TraverseAndExtract()
|
|
|
|
function traverse(depth, pos) { |
|
|
@ -189,9 +185,6 @@ MerkleBlock.prototype.validMerkleTree = function validMerkleTree() { |
|
|
|
return null; |
|
|
|
} |
|
|
|
var hash = self.hashes[hashesUsed++]; |
|
|
|
if (depth === 0 && isParentOfMatch) { |
|
|
|
txs.push(hash) |
|
|
|
} |
|
|
|
return new Buffer(hash, 'hex'); |
|
|
|
} else { |
|
|
|
var left = traverse(depth-1, pos*2); |
|
|
@ -205,9 +198,11 @@ MerkleBlock.prototype.validMerkleTree = function validMerkleTree() { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
function calcTreeWidth(height) { |
|
|
|
return (self.numTransactions + (1 << height) - 1) >> height; |
|
|
|
var root = traverse(height, 0); |
|
|
|
if(hashesUsed !== this.hashes.length) { |
|
|
|
return this._setValidMerkleTree(false); |
|
|
|
} |
|
|
|
return this._setValidMerkleTree(BufferUtil.equals(root, this.header.merkleRoot)); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|