framer: add tx, block, and merkleblock packet sending to the framer.

Signed-off-by: Fedor Indutny <fedor@indutny.com>
This commit is contained in:
Christopher Jeffrey 2014-05-18 09:53:52 -05:00 committed by Fedor Indutny
parent 3998146046
commit 2b5f8693bf

View File

@ -248,3 +248,65 @@ Framer.tx = function tx(tx) {
return p;
};
Framer.prototype.tx = function tx(tx) {
return this.packet('tx', Framer.tx(tx));
};
Framer.block = function _block(block, type) {
var p = [];
var off = 0;
off += writeU32(p, constants.version, off);
// prev_block
util.toArray(block.prevBlock, 'hex').forEach(function(ch) {
p[off++] = ch;
});
// merkle_root
util.toArray(block.merkleRoot, 'hex').forEach(function(ch) {
p[off++] = ch;
});
// timestamp
off += writeU32(p, block.ts, off);
// bits
off += writeU32(p, block.bits, off);
// nonce
off += writeU32(p, block.nonce, off);
// txn_count
off += varint(p, block.totalTX, off);
if (type === 'merkleblock') {
// hash count
off += varint(p, block.hashes.length, off);
// hashes
block.hashes.forEach(function(hash) {
utils.toArray(hash, 'hex').forEach(function(ch) {
p[off++] = ch;
});
});
// flag count
off += varint(p, block.flags.length, off);
// flags
block.flags.forEach(function(flag) {
p[off++] = flag;
});
}
return p;
};
Framer.prototype.block = function _block(block) {
return this.packet(Framer.block(block, 'block'));
};
Framer.prototype.merkleBlock = function merkleBlock(block) {
// XXX Technically we're also supposed to send `tx` packets accompanying the
// merkleblock here if we have them, as per the offical bitcoin client.
return this.packet(Framer.block(block, 'merkleblock'));
};