parser: parse transactions in regular blocks.

This commit is contained in:
Christopher Jeffrey 2014-05-23 08:49:52 -05:00
parent faca3454b2
commit f4dbc0ee33
3 changed files with 39 additions and 2 deletions

View File

@ -23,6 +23,21 @@ function Block(data, subtype) {
this.tx = [];
this.invalid = false;
if (this.subtype === 'block') {
var self = this;
this.txs = data.txs || [];
this.txs = this.txs.map(function(tx) {
tx = bcoin.tx(tx);
tx.block = self.hash('hex');
tx.ts = tx.ts || self.ts;
return tx;
});
this.hashes = this.txs.map(function(tx) {
return tx.hash('hex');
});
this.tx = this.hashes.slice();
}
this._hash = null;
// Verify partial merkle tree and fill `ts` array
@ -64,6 +79,11 @@ Block.prototype.hasTX = function hasTX(hash) {
Block.prototype._verifyMerkle = function verifyMerkle() {
var height = 0;
if (this.subtype === 'block') {
this.invalid = !utils.testTarget(this.bits, this.hash());
return;
}
// Count leafs
for (var i = this.totalTX; i > 0; i >>= 1)
height++;

View File

@ -293,6 +293,10 @@ Pool.prototype._addPeer = function _addPeer(backoff) {
self.emit('block', block, peer);
});
peer.on('block', function(block) {
self.emit('block', block, peer);
});
// Just FYI
peer.on('reject', function(payload) {
self.emit('reject', payload, peer);

View File

@ -218,6 +218,17 @@ Parser.prototype.parseBlock = function parseBlock(p) {
if (p.length < 84)
return this._error('Invalid block size');
var result = readIntv(p, 80);
var off = result.off;
var totalTX = result.r;
var txs = [];
for (var i = 0; i < totalTX; i++) {
var tx = this.parseTX(p.slice(off));
off += tx._off;
txs.push(tx);
}
return {
version: readU32(p, 0),
prevBlock: p.slice(4, 36),
@ -225,7 +236,8 @@ Parser.prototype.parseBlock = function parseBlock(p) {
ts: readU32(p, 68),
bits: readU32(p, 72),
nonce: readU32(p, 76),
totalTX: readU32(p, 80)
totalTX: totalTX,
txs: txs
};
};
@ -316,7 +328,8 @@ Parser.prototype.parseTX = function parseTX(p) {
version: readU32(p, 0),
inputs: txIn,
outputs: txOut,
lock: readU32(p, off)
lock: readU32(p, off),
_off: off + 4
};
};