From b563fe5de1ea58d0d6abe47f70a14d0caef360f5 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 24 Feb 2016 21:21:14 -0800 Subject: [PATCH] tx indicies. merkle things. --- lib/bcoin/abstractblock.js | 32 ++++++++++++++++++++++++++++++++ lib/bcoin/block.js | 4 ++-- lib/bcoin/mtx.js | 1 + lib/bcoin/tx.js | 12 +++++++----- 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/lib/bcoin/abstractblock.js b/lib/bcoin/abstractblock.js index 416860b3..dcdffe89 100644 --- a/lib/bcoin/abstractblock.js +++ b/lib/bcoin/abstractblock.js @@ -47,6 +47,38 @@ function AbstractBlock(data) { this.lowVersion = this.version & 7; } +AbstractBlock.prototype.getMerkleBranch = function getMerkleBranch(index) { + var tree = utils.buildMerkleTree(this.merkleRoot); + var branch = []; + var j = 0; + for (var size = this.totalTX; size > 1; size = (size + 1) / 2 | 0) { + var i = Math.min(index ^ 1, size - 1); + branch.push(tree[j + i]); + index >>= 1; + j += size; + } + return branch; +}; + +AbstractBlock.prototype.hasMerkleItem = function hasMerkleItem(hash, branch, index) { + if (index === -1) + return false; + + if (typeof hash === 'string') + hash = new Buffer(hash, 'hex'); + + for (var i = 0; i < branch.length; i++) { + var otherside = branch[i]; + if (index & 1) + hash = utils.dsha256(Buffer.concat([otherside, hash])); + else + hash = utils.dsha256(Buffer.concat([hash, otherside])); + index >>= 1; + } + + return utils.toHex(hash) === this.merkleRoot; +}; + AbstractBlock.prototype.hash = function hash(enc) { if (!this._hash) this._hash = utils.dsha256(this.abbr()); diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index c8b0ddb6..bdf2a2fb 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -29,11 +29,11 @@ function Block(data) { this._cbHeight = null; - this.txs = this.txs.map(function(data) { + this.txs = this.txs.map(function(data, i) { if (data instanceof bcoin.tx) return data; - return bcoin.tx(data, self); + return bcoin.tx(data, self, i); }); if (!this._raw) diff --git a/lib/bcoin/mtx.js b/lib/bcoin/mtx.js index df9b3a64..52abe82e 100644 --- a/lib/bcoin/mtx.js +++ b/lib/bcoin/mtx.js @@ -31,6 +31,7 @@ function MTX(options) { this.locktime = 0; this.ts = 0; this.block = null; + this.index = -1; this._hash = null; this._raw = null; diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 34baefcc..93f422a6 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -15,9 +15,9 @@ var constants = bcoin.protocol.constants; * TX */ -function TX(data, block) { +function TX(data, block, index) { if (!(this instanceof TX)) - return new TX(data, block); + return new TX(data, block, index); if (!data) data = {}; @@ -29,6 +29,7 @@ function TX(data, block) { this.locktime = data.locktime || 0; this.ts = data.ts || 0; this.block = data.block || null; + this.index = data.index || -1; this._hash = null; this._raw = data._raw || null; @@ -53,9 +54,9 @@ function TX(data, block) { if (block && this.ts === 0) { if (block.type === 'merkleblock') { if (block.hasTX(this.hash('hex'))) - this.setBlock(block); + this.setBlock(block, index); } else { - this.setBlock(block); + this.setBlock(block, index); } } @@ -66,10 +67,11 @@ function TX(data, block) { this._size = this._raw.length; } -TX.prototype.setBlock = function setBlock(block) { +TX.prototype.setBlock = function setBlock(block, index) { this.ts = block.ts; this.block = block.hash('hex'); this.height = block.height; + this.index = index; }; TX.prototype.clone = function clone() {