From a67084d1768cfb257ebba0df60a31c3b98f0b33f Mon Sep 17 00:00:00 2001 From: William Wolf Date: Sun, 22 Feb 2015 21:18:40 -0800 Subject: [PATCH] cleanup --- lib/block/merkleblock.js | 21 ++++++++------------- test/merkleblock.js | 24 +++++++++++------------- 2 files changed, 19 insertions(+), 26 deletions(-) diff --git a/lib/block/merkleblock.js b/lib/block/merkleblock.js index 311adcf..bebbf17 100644 --- a/lib/block/merkleblock.js +++ b/lib/block/merkleblock.js @@ -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)); } /** diff --git a/test/merkleblock.js b/test/merkleblock.js index 8b1a506..4c34a0c 100644 --- a/test/merkleblock.js +++ b/test/merkleblock.js @@ -80,16 +80,6 @@ describe('MerkleBlock', function() { }); - // TODO - //describe('#fromString/#toString', function() { - - //it('should output/input a block hex string', function() { - //var b = MerkleBlock.fromString(blockhex); - //b.toString().should.equal(blockhex); - //}); - - //}); - describe('#fromBuffer', function() { it('should make a block from this known buffer', function() { @@ -139,11 +129,20 @@ 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 json = data.JSON[0]; var b = MerkleBlock(JSON.stringify(data.JSON[0])); // Add too many hashes var i = 0; @@ -154,8 +153,7 @@ describe('MerkleBlock', function() { }); it('should not validate merkleblocks with too few bit flags', function() { - var json = JSON.stringify(data.JSON[0]); - var b = MerkleBlock(json); + var b = MerkleBlock(blockJSON); b.flags.pop() b.validMerkleTree().should.equal(false); });