tx indicies. merkle things.
This commit is contained in:
parent
2c2fa3913b
commit
b563fe5de1
@ -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());
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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() {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user