From 2b5f8693bf157e3d41214b3695c748c8313fb877 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 18 May 2014 09:53:52 -0500 Subject: [PATCH] framer: add tx, block, and merkleblock packet sending to the framer. Signed-off-by: Fedor Indutny --- lib/bcoin/protocol/framer.js | 62 ++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/lib/bcoin/protocol/framer.js b/lib/bcoin/protocol/framer.js index 4d92074b..8acafddc 100644 --- a/lib/bcoin/protocol/framer.js +++ b/lib/bcoin/protocol/framer.js @@ -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')); +};