improve getMerkleRoot.
This commit is contained in:
parent
68f3ef599d
commit
5bba5d43c6
@ -204,14 +204,13 @@ Block.prototype._verifyPartial = function _verifyPartial() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
Block.prototype.getMerkleRoot = function getMerkleRoot() {
|
Block.prototype.getMerkleRoot = function getMerkleRoot() {
|
||||||
var merkleTree = [];
|
var tree = [];
|
||||||
var i, j, size, i2, hash;
|
var i, j, size, i2, hash;
|
||||||
|
|
||||||
assert(this.subtype === 'block');
|
assert(this.subtype === 'block');
|
||||||
|
|
||||||
for (i = 0; i < this.txs.length; i++) {
|
for (i = 0; i < this.txs.length; i++)
|
||||||
merkleTree.push(this.txs[i].hash('hex'));
|
tree.push(this.txs[i].hash());
|
||||||
}
|
|
||||||
|
|
||||||
j = 0;
|
j = 0;
|
||||||
size = this.txs.length;
|
size = this.txs.length;
|
||||||
@ -220,19 +219,20 @@ Block.prototype.getMerkleRoot = function getMerkleRoot() {
|
|||||||
for (i = 0; i < size; i += 2) {
|
for (i = 0; i < size; i += 2) {
|
||||||
i2 = Math.min(i + 1, size - 1);
|
i2 = Math.min(i + 1, size - 1);
|
||||||
if (i2 === i + 1 && i2 + 1 === size
|
if (i2 === i + 1 && i2 + 1 === size
|
||||||
&& merkleTree[j + i] === merkleTree[j + i2]) {
|
&& tree[j + i] === tree[j + i2]) {
|
||||||
return utils.toHex(constants.zeroHash);
|
return utils.toHex(constants.zeroHash);
|
||||||
}
|
}
|
||||||
hash = utils.dsha256(merkleTree[j + i] + merkleTree[j + i2], 'hex');
|
hash = Buffer.concat([tree[j + i], tree[j + i2]]);
|
||||||
merkleTree.push(utils.toHex(hash));
|
hash = utils.dsha256(hash);
|
||||||
|
tree.push(hash);
|
||||||
}
|
}
|
||||||
j += size;
|
j += size;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!merkleTree.length)
|
if (!tree.length)
|
||||||
return utils.toHex(constants.zeroHash);
|
return utils.toHex(constants.zeroHash);
|
||||||
|
|
||||||
return merkleTree[merkleTree.length - 1];
|
return utils.toHex(tree[tree.length - 1]);
|
||||||
};
|
};
|
||||||
|
|
||||||
Block.prototype._verify = function _verify() {
|
Block.prototype._verify = function _verify() {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user