parser: parse transactions in regular blocks.
This commit is contained in:
parent
faca3454b2
commit
f4dbc0ee33
@ -23,6 +23,21 @@ function Block(data, subtype) {
|
|||||||
this.tx = [];
|
this.tx = [];
|
||||||
this.invalid = false;
|
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;
|
this._hash = null;
|
||||||
|
|
||||||
// Verify partial merkle tree and fill `ts` array
|
// Verify partial merkle tree and fill `ts` array
|
||||||
@ -64,6 +79,11 @@ Block.prototype.hasTX = function hasTX(hash) {
|
|||||||
Block.prototype._verifyMerkle = function verifyMerkle() {
|
Block.prototype._verifyMerkle = function verifyMerkle() {
|
||||||
var height = 0;
|
var height = 0;
|
||||||
|
|
||||||
|
if (this.subtype === 'block') {
|
||||||
|
this.invalid = !utils.testTarget(this.bits, this.hash());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Count leafs
|
// Count leafs
|
||||||
for (var i = this.totalTX; i > 0; i >>= 1)
|
for (var i = this.totalTX; i > 0; i >>= 1)
|
||||||
height++;
|
height++;
|
||||||
|
|||||||
@ -293,6 +293,10 @@ Pool.prototype._addPeer = function _addPeer(backoff) {
|
|||||||
self.emit('block', block, peer);
|
self.emit('block', block, peer);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
peer.on('block', function(block) {
|
||||||
|
self.emit('block', block, peer);
|
||||||
|
});
|
||||||
|
|
||||||
// Just FYI
|
// Just FYI
|
||||||
peer.on('reject', function(payload) {
|
peer.on('reject', function(payload) {
|
||||||
self.emit('reject', payload, peer);
|
self.emit('reject', payload, peer);
|
||||||
|
|||||||
@ -218,6 +218,17 @@ Parser.prototype.parseBlock = function parseBlock(p) {
|
|||||||
if (p.length < 84)
|
if (p.length < 84)
|
||||||
return this._error('Invalid block size');
|
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 {
|
return {
|
||||||
version: readU32(p, 0),
|
version: readU32(p, 0),
|
||||||
prevBlock: p.slice(4, 36),
|
prevBlock: p.slice(4, 36),
|
||||||
@ -225,7 +236,8 @@ Parser.prototype.parseBlock = function parseBlock(p) {
|
|||||||
ts: readU32(p, 68),
|
ts: readU32(p, 68),
|
||||||
bits: readU32(p, 72),
|
bits: readU32(p, 72),
|
||||||
nonce: readU32(p, 76),
|
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),
|
version: readU32(p, 0),
|
||||||
inputs: txIn,
|
inputs: txIn,
|
||||||
outputs: txOut,
|
outputs: txOut,
|
||||||
lock: readU32(p, off)
|
lock: readU32(p, off),
|
||||||
|
_off: off + 4
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user