Browse Source

cleanup

patch-2
William Wolf 10 years ago
parent
commit
a67084d176
  1. 21
      lib/block/merkleblock.js
  2. 24
      test/merkleblock.js

21
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));
}
/**

24
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);
});

Loading…
Cancel
Save