merkle refactor. misc.

This commit is contained in:
Christopher Jeffrey 2016-05-25 01:57:19 -07:00
parent afeb1d1d34
commit 2738a4ed5d
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 39 additions and 30 deletions

View File

@ -83,13 +83,19 @@ Headers.prototype.getRaw = function getRaw() {
*/ */
Headers.prototype.inspect = function inspect() { Headers.prototype.inspect = function inspect() {
var copy = bcoin.headers(this); return {
copy.__proto__ = null; type: 'headers',
delete copy._raw; hash: this.rhash,
copy.hash = this.hash('hex'); height: this.height,
copy.rhash = this.rhash; date: utils.date(this.ts),
copy.date = utils.date(copy.ts); version: this.version,
return copy; prevBlock: utils.revHex(this.prevBlock),
merkleRoot: utils.revHex(this.merkleRoot),
ts: this.ts,
bits: this.bits,
nonce: this.nonce,
totalTX: this.totalTX
};
}; };
/** /**

View File

@ -7,6 +7,7 @@
var bcoin = require('./env'); var bcoin = require('./env');
var utils = require('./utils'); var utils = require('./utils');
var assert = utils.assert;
var constants = bcoin.protocol.constants; var constants = bcoin.protocol.constants;
/** /**
@ -41,13 +42,11 @@ function MerkleBlock(data) {
bcoin.abstractblock.call(this, data); bcoin.abstractblock.call(this, data);
this.hashes = (data.hashes || []).map(function(hash) { assert(Array.isArray(data.hashes));
if (typeof hash === 'string') assert(Buffer.isBuffer(data.flags));
hash = new Buffer(hash, 'hex');
return hash;
});
this.flags = data.flags || []; this.hashes = data.hashes;
this.flags = data.flags;
// List of matched TXs // List of matched TXs
this.map = {}; this.map = {};
@ -165,15 +164,14 @@ MerkleBlock.prototype.verifyPartial = function verifyPartial() {
*/ */
MerkleBlock.prototype.extractTree = function extractTree() { MerkleBlock.prototype.extractTree = function extractTree() {
var hashes = this.hashes;
var flags = this.flags;
var bitsUsed = 0; var bitsUsed = 0;
var hashUsed = 0; var hashUsed = 0;
var matches = []; var matches = [];
var indexes = []; var indexes = [];
var map = {}; var map = {};
var failed = false; var failed = false;
var bits = new Array(flags.length * 8); var hashes = new Array(this.hashes.length);
var bits = new Array(this.flags.length * 8);
var totalTX = this.totalTX; var totalTX = this.totalTX;
var height = 0; var height = 0;
var root, p; var root, p;
@ -219,8 +217,11 @@ MerkleBlock.prototype.extractTree = function extractTree() {
return utils.dsha256(Buffer.concat([left, right])); return utils.dsha256(Buffer.concat([left, right]));
} }
for (p = 0; p < hashes.length; p++)
hashes[p] = new Buffer(this.hashes[p], 'hex');
for (p = 0; p < bits.length; p++) for (p = 0; p < bits.length; p++)
bits[p] = +((flags[p / 8 | 0] & (1 << (p % 8))) !== 0); bits[p] = +((this.flags[p / 8 | 0] & (1 << (p % 8))) !== 0);
if (totalTX === 0) if (totalTX === 0)
return; return;
@ -310,11 +311,9 @@ MerkleBlock.prototype.inspect = function inspect() {
bits: this.bits, bits: this.bits,
nonce: this.nonce, nonce: this.nonce,
totalTX: this.totalTX, totalTX: this.totalTX,
hashes: this.hashes.map(function(hash) { hashes: this.hashes,
return hash.toString('hex'); flags: this.flags,
}), map: this.map
map: this.map,
flags: this.flags
}; };
}; };
@ -419,7 +418,7 @@ MerkleBlock.fromBlock = function fromBlock(block, bloom) {
bits.push(parent); bits.push(parent);
if (height === 0 || !parent) { if (height === 0 || !parent) {
hashes.push(hash(height, pos, leaves)); hashes.push(hash(height, pos, leaves).toString('hex'));
return; return;
} }

View File

@ -49,6 +49,8 @@ describe('Block', function() {
var mblock = bcoin.merkleblock.fromRaw(raw2, 'hex'); var mblock = bcoin.merkleblock.fromRaw(raw2, 'hex');
this.timeout(10000);
it('should parse partial merkle tree', function() { it('should parse partial merkle tree', function() {
assert(mblock.verify()); assert(mblock.verify());
assert.equal(mblock.matches.length, 2); assert.equal(mblock.matches.length, 2);
@ -137,13 +139,14 @@ describe('Block', function() {
}); });
it('should fail with a bad merkle root', function() { it('should fail with a bad merkle root', function() {
var merkle = block.merkleRoot; var block2 = new bcoin.block(block);
block.merkleRoot = constants.NULL_HASH; block2.merkleRoot = constants.NULL_HASH;
delete block._valid; delete block2._valid;
assert(!block.verify()); assert(!block2.verify());
delete block._valid; delete block2._valid;
block.merkleRoot = merkle; delete block2._hash;
assert(block.verify()); block2.merkleRoot = block.merkleRoot;
assert(block2.verify());
}); });
it('should fail on merkle block with a bad merkle root', function() { it('should fail on merkle block with a bad merkle root', function() {
@ -162,6 +165,7 @@ describe('Block', function() {
block2.bits = 403014710; block2.bits = 403014710;
assert(!block2.verify()); assert(!block2.verify());
delete block2._valid; delete block2._valid;
delete block2._hash;
block2.bits = block.bits; block2.bits = block.bits;
assert(block2.verify()); assert(block2.verify());
}); });