Remove _validMerkleTree caching

This commit is contained in:
William Wolf 2015-02-23 19:40:52 -08:00
parent a478e39524
commit bb0efd2108
2 changed files with 4 additions and 29 deletions

View File

@ -60,7 +60,6 @@ function MerkleBlock(arg) {
throw new TypeError('Unrecognized argument for MerkleBlock');
}
_.extend(this,info);
this._validMerkleTree = null;
this._flagBitsUsed = 0;
this._hashesUsed = 0;
return this;
@ -144,18 +143,15 @@ MerkleBlock.prototype.toJSON = function toJSON() {
MerkleBlock.prototype.validMerkleTree = function validMerkleTree() {
$.checkState(_.isArray(this.flags), 'MerkleBlock flags is not an array');
$.checkState(_.isArray(this.hashes), 'MerkleBlock hashes is not an array');
if(_.isBoolean(this._validMerkleTree)) {
return this._validMerkleTree;
}
// Can't have more hashes than numTransactions
if(this.hashes.length > this.numTransactions) {
return this._setValidMerkleTree(false);
return false;
}
// Can't have more flag bits than num hashes
if(this.flags.length * 8 < this.hashes.length) {
return this._setValidMerkleTree(false);
return false;
}
var height = 0;
@ -167,9 +163,9 @@ MerkleBlock.prototype.validMerkleTree = function validMerkleTree() {
this._hashesUsed = 0;
var root = this._traverseMerkleTree(height, 0);
if(this._hashesUsed !== this.hashes.length) {
return this._setValidMerkleTree(false);
false;
}
return this._setValidMerkleTree(BufferUtil.equals(root, this.header.merkleRoot));
return BufferUtil.equals(root, this.header.merkleRoot);
}
/** Traverse a the tree in this MerkleBlock, validating it along the way
@ -229,17 +225,6 @@ MerkleBlock.prototype.hasTransaction = function hasTransaction(tx) {
|| this.hashes.indexOf(revHash) !== -1);
}
/**
* @param {Bool} - set the merkle tree validity
* @returns {Bool} - return true/false
* @private
*/
MerkleBlock.prototype._setValidMerkleTree = function(valid) {
this._validMerkleTree = valid;
return valid;
}
/**
* @param {Buffer} - MerkleBlock data
* @returns {Object} - An Object representing merkleblock data

View File

@ -129,19 +129,9 @@ describe('MerkleBlock', function() {
data.JSON.forEach(function(json) {
var b = MerkleBlock(JSON.stringify(json));
b.validMerkleTree().should.equal(true);
b._validMerkleTree.should.equal(true);
});
});
it('should respect _validMerkleTrees', function() {
var b = MerkleBlock(blockJSON);
b._validMerkleTree = false;
b.validMerkleTree().should.equal(false);
b._validMerkleTree = true;
b._validMerkleTree.should.equal(true);
b.validMerkleTree().should.equal(true);
});
it('should not validate merkleblocks with too many hashes', function() {
var b = MerkleBlock(JSON.stringify(data.JSON[0]));
// Add too many hashes