verify partial merkle tree lazily.
This commit is contained in:
parent
1c7d3082dc
commit
45115995f6
@ -50,6 +50,7 @@ function MerkleBlock(data) {
|
||||
// List of matched TXs
|
||||
this.txMap = {};
|
||||
this.tx = [];
|
||||
this._partialVerified = null;
|
||||
|
||||
// TXs that will be pushed on
|
||||
this.txs = [];
|
||||
@ -118,6 +119,8 @@ MerkleBlock.prototype.hasTX = function hasTX(hash) {
|
||||
if (hash instanceof bcoin.tx)
|
||||
hash = hash.hash('hex');
|
||||
|
||||
this.verifyPartial();
|
||||
|
||||
return this.txMap[hash] === true;
|
||||
};
|
||||
|
||||
@ -128,7 +131,7 @@ MerkleBlock.prototype.hasTX = function hasTX(hash) {
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
|
||||
MerkleBlock.prototype._verifyPartial = function _verifyPartial() {
|
||||
MerkleBlock.prototype.verifyPartial = function verifyPartial() {
|
||||
var height = 0;
|
||||
var tx = [];
|
||||
var txMap = {};
|
||||
@ -137,6 +140,9 @@ MerkleBlock.prototype._verifyPartial = function _verifyPartial() {
|
||||
var flags = this.flags;
|
||||
var i, root;
|
||||
|
||||
if (this._partialVerified != null)
|
||||
return this._partialVerified;
|
||||
|
||||
// Count leaves
|
||||
for (i = this.totalTX; i > 0; i >>= 1)
|
||||
height++;
|
||||
@ -178,11 +184,14 @@ MerkleBlock.prototype._verifyPartial = function _verifyPartial() {
|
||||
|
||||
root = utils.toHex(visit(1));
|
||||
|
||||
if (!root || root !== this.merkleRoot)
|
||||
if (!root || root !== this.merkleRoot) {
|
||||
this._partialVerified = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
this.tx = tx;
|
||||
this.txMap = txMap;
|
||||
this._partialVerified = true;
|
||||
|
||||
return true;
|
||||
};
|
||||
@ -204,7 +213,7 @@ MerkleBlock.prototype._verify = function _verify(ret) {
|
||||
return false;
|
||||
|
||||
// Verify the partial merkle tree if we are a merkleblock.
|
||||
if (!this._verifyPartial()) {
|
||||
if (!this.verifyPartial()) {
|
||||
ret.reason = 'bad-txnmrklroot';
|
||||
ret.score = 100;
|
||||
return false;
|
||||
@ -389,7 +398,7 @@ MerkleBlock.fromBlock = function fromBlock(block, bloom) {
|
||||
MerkleBlock.isMerkleBlock = function isMerkleBlock(obj) {
|
||||
return obj
|
||||
&& Array.isArray(obj.flags)
|
||||
&& typeof obj._verifyPartial === 'function';
|
||||
&& typeof obj.verifyPartial === 'function';
|
||||
};
|
||||
|
||||
return MerkleBlock;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user