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