refactor merkleblock.

This commit is contained in:
Christopher Jeffrey 2016-05-23 22:30:39 -07:00
parent 9afb141dce
commit da701cbef4
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
4 changed files with 33 additions and 21 deletions

View File

@ -360,10 +360,6 @@ RollingFilter.prototype.added = function added(val, enc) {
return false;
};
/*
* Murmur3
*/
/**
* Murmur3 hash.
* @memberof Bloom

View File

@ -30,8 +30,8 @@ var constants = bcoin.protocol.constants;
* @property {Buffer[]} hashes
* @property {Buffer} flags
* @property {TX[]} txs - Transaction vector.
* @property {Hash[]} tx - List of matched tx hashes.
* @property {Object} txMap - Map of matched tx hashes.
* @property {Hash[]} matches - List of matched tx hashes.
* @property {Object} map - Map of matched tx hashes.
* @property {ReversedHash} rhash - Reversed block hash (uint256le).
*/
@ -50,8 +50,8 @@ function MerkleBlock(data) {
this.flags = data.flags || [];
// List of matched TXs
this.txMap = {};
this.tx = [];
this.map = {};
this.matches = [];
this._validPartial = null;
// TXs that will be pushed on
@ -100,12 +100,17 @@ MerkleBlock.prototype.getRaw = function getRaw() {
*/
MerkleBlock.prototype.addTX = function addTX(tx) {
var index, hash;
if (!(tx instanceof bcoin.tx))
tx = new bcoin.tx(tx);
hash = tx.hash('hex');
index = this.map[hash];
this.txs.push(tx);
tx.setBlock(this, -1);
tx.setBlock(this, index);
return tx;
};
@ -122,12 +127,12 @@ MerkleBlock.prototype.hasTX = function hasTX(hash) {
this.verifyPartial();
return this.txMap[hash] === true;
return this.map[hash] != null;
};
/**
* Verify the partial merkletree. Push leaves onto
* {@link MerkleBlock#tx} and into {@link MerkleBlock#txMap}.
* {@link MerkleBlock#tx} and into {@link MerkleBlock#map}.
* @private
* @returns {Boolean}
*/
@ -145,8 +150,8 @@ MerkleBlock.prototype.verifyPartial = function verifyPartial() {
return false;
}
this.tx = tree.matches;
this.txMap = utils.toMap(tree.matches);
this.matches = tree.matches;
this.map = tree.map;
this._validPartial = true;
return true;
@ -167,6 +172,7 @@ MerkleBlock.prototype.extractTree = function extractTree() {
var hashUsed = 0;
var matches = [];
var indexes = [];
var map = {};
var failed = false;
var bits = new Array(flags.length * 8);
var totalTX = this.totalTX;
@ -179,7 +185,7 @@ MerkleBlock.prototype.extractTree = function extractTree() {
}
function traverse(height, pos) {
var parent, hash, left, right;
var parent, hash, left, right, txid;
if (bitsUsed >= bits.length) {
failed = true;
@ -195,8 +201,10 @@ MerkleBlock.prototype.extractTree = function extractTree() {
}
hash = hashes[hashUsed++];
if (height === 0 && parent) {
matches.push(hash.toString('hex'));
txid = hash.toString('hex');
matches.push(txid);
indexes.push(pos);
map[txid] = pos;
}
return hash;
}
@ -219,7 +227,7 @@ MerkleBlock.prototype.extractTree = function extractTree() {
if (totalTX == 0)
return;
if (totalTX > (constants.block.MAX_SIZE / 60 | 0))
if (totalTX > constants.block.MAX_SIZE / 60)
return;
if (hashes.length > totalTX)
@ -246,7 +254,8 @@ MerkleBlock.prototype.extractTree = function extractTree() {
return {
root: root.toString('hex'),
matches: matches,
indexes: indexes
indexes: indexes,
map: map
};
};
@ -306,7 +315,7 @@ MerkleBlock.prototype.inspect = function inspect() {
hashes: this.hashes.map(function(hash) {
return hash.toString('hex');
}),
tx: this.tx,
map: this.map,
flags: this.flags
};
};

View File

@ -51,16 +51,16 @@ describe('Block', function() {
it('should parse partial merkle tree', function() {
assert(mblock.verify());
assert.equal(mblock.tx.length, 2);
assert.equal(mblock.matches.length, 2);
assert.equal(mblock.hash('hex'),
'8cc72c02a958de5a8b35a23bb7e3bced8bf840cc0a4e1c820000000000000000');
assert.equal(mblock.rhash,
'0000000000000000821c4e0acc40f88bedbce3b73ba2358b5ade58a9022cc78c');
assert.equal(
mblock.tx[0],
mblock.matches[0],
'7393f84cd04ca8931975c66282ebf1847c78d8de6c2578d4f9bae23bc6f30857');
assert.equal(
mblock.tx[1],
mblock.matches[1],
'ec8c51de3170301430ec56f6703533d9ea5b05c6fa7068954bcb90eed8c2ee5c');
});

View File

@ -631,5 +631,12 @@ describe('TX', function() {
assert.equal(wtx.getSize(), 62138);
assert.equal(wtx.getVirtualSize(), 61813);
assert.equal(wtx.getCost(), 247250);
var raw1 = wtx.render();
clearCache(wtx, true);
var raw2 = wtx.render();
assert.deepEqual(raw1, raw2);
var wtx2 = bcoin.tx.fromRaw(raw2);
assert.equal(wtx.hash('hex'), wtx2.hash('hex'));
assert.equal(wtx.witnessHash('hex'), wtx2.witnessHash('hex'));
});
});