From db69c882f2128e73fcc79c330a60aab7835c63d5 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 18 May 2014 13:03:27 -0500 Subject: [PATCH] block: add block tests. frame correct version for blocks. Signed-off-by: Fedor Indutny --- lib/bcoin/block.js | 4 +-- lib/bcoin/protocol/framer.js | 2 +- test/block-test.js | 67 +++++++++++++++++++++++------------- 3 files changed, 47 insertions(+), 26 deletions(-) diff --git a/lib/bcoin/block.js b/lib/bcoin/block.js index 929e634c..2b4d67f0 100644 --- a/lib/bcoin/block.js +++ b/lib/bcoin/block.js @@ -53,8 +53,8 @@ Block.prototype.verify = function verify() { return !this.invalid && utils.testTarget(this.bits, this.hash()); }; -Block.prototype.render = function render(framer) { - return []; +Block.prototype.render = function render() { + return bcoin.protocol.framer.block(this, this.subtype); }; Block.prototype.hasTX = function hasTX(hash) { diff --git a/lib/bcoin/protocol/framer.js b/lib/bcoin/protocol/framer.js index 55ae74de..5bfcd7af 100644 --- a/lib/bcoin/protocol/framer.js +++ b/lib/bcoin/protocol/framer.js @@ -259,7 +259,7 @@ Framer.block = function _block(block, type) { // version assert.equal(off, 0); - off += writeU32(p, constants.version, off); + off += writeU32(p, block.version, off); // prev_block assert.equal(off, 4); diff --git a/test/block-test.js b/test/block-test.js index 391c0783..0aa3a600 100644 --- a/test/block-test.js +++ b/test/block-test.js @@ -3,30 +3,32 @@ var bn = require('bn.js'); var bcoin = require('../'); describe('Block', function() { - it('should parse partial merkle tree', function() { - var block = bcoin.block({ - type: 'block', - version: 2, - prevBlock: 'd1831d4411bdfda89d9d8c842b541beafd1437fc560dbe5c0000000000000000', - merkleRoot: '28bec1d35af480ba3884553d72694f6ba6c163a5c081d7e6edaec15f373f19af', - ts: 1399713634, - bits: 419465580, - nonce: 1186968784, - totalTX: 461, - hashes:[ - '7d22e53bce1bbb3294d1a396c5acc45bdcc8f192cb492f0d9f55421fd4c62de1', - '9d6d585fdaf3737b9a54aaee1dd003f498328d699b7dfb42dd2b44b6ebde2333', - '8b61da3053d6f382f2145bdd856bc5dcf052c3a11c1784d3d51b2cbe0f6d0923', - 'd7bbaae4716cb0d329d755b707cee588cddc68601f99bc05fef1fabeb8dfe4a0', - '7393f84cd04ca8931975c66282ebf1847c78d8de6c2578d4f9bae23bc6f30857', - 'ec8c51de3170301430ec56f6703533d9ea5b05c6fa7068954bcb90eed8c2ee5c', - 'c7c152869db09a5ae2291fa03142912d9d7aba75be7d491a8ac4230ee9a920cb', - '5adbf04583354515a225f2c418de7c5cdac4cef211820c79717cd2c50412153f', - '1f5e46b9da3a8b1241f4a1501741d3453bafddf6135b600b926e3f4056c6d564', - '33825657ba32afe269819f01993bd77baba86379043168c94845d32370e53562' ], - flags: [ 245, 90, 0 ] - }, 'merkleblock'); + var parser = bcoin.protocol.parser(); + var block = bcoin.block({ + type: 'block', + version: 2, + prevBlock: 'd1831d4411bdfda89d9d8c842b541beafd1437fc560dbe5c0000000000000000', + merkleRoot: '28bec1d35af480ba3884553d72694f6ba6c163a5c081d7e6edaec15f373f19af', + ts: 1399713634, + bits: 419465580, + nonce: 1186968784, + totalTX: 461, + hashes:[ + '7d22e53bce1bbb3294d1a396c5acc45bdcc8f192cb492f0d9f55421fd4c62de1', + '9d6d585fdaf3737b9a54aaee1dd003f498328d699b7dfb42dd2b44b6ebde2333', + '8b61da3053d6f382f2145bdd856bc5dcf052c3a11c1784d3d51b2cbe0f6d0923', + 'd7bbaae4716cb0d329d755b707cee588cddc68601f99bc05fef1fabeb8dfe4a0', + '7393f84cd04ca8931975c66282ebf1847c78d8de6c2578d4f9bae23bc6f30857', + 'ec8c51de3170301430ec56f6703533d9ea5b05c6fa7068954bcb90eed8c2ee5c', + 'c7c152869db09a5ae2291fa03142912d9d7aba75be7d491a8ac4230ee9a920cb', + '5adbf04583354515a225f2c418de7c5cdac4cef211820c79717cd2c50412153f', + '1f5e46b9da3a8b1241f4a1501741d3453bafddf6135b600b926e3f4056c6d564', + '33825657ba32afe269819f01993bd77baba86379043168c94845d32370e53562' ], + flags: [ 245, 90, 0 ] + }, 'merkleblock'); + var raw = block.toJSON().block; + it('should parse partial merkle tree', function() { assert(block.verify()); assert.equal(block.tx.length, 2); assert.equal( @@ -36,4 +38,23 @@ describe('Block', function() { block.tx[1], 'ec8c51de3170301430ec56f6703533d9ea5b05c6fa7068954bcb90eed8c2ee5c'); }); + + it('should decode/encode with parser/framer', function() { + var b = bcoin.block(parser.parseMerkleBlock(bcoin.utils.toArray(raw, 'hex')), 'merkleblock'); + assert.equal(bcoin.utils.toHex(b.render()), raw); + }); + + it('should be verifiable', function() { + var b = bcoin.block(parser.parseMerkleBlock(bcoin.utils.toArray(raw, 'hex')), 'merkleblock'); + assert(b.verify()); + }); + + it('should be jsonified and unjsonified and still verify', function() { + var json = block.toJSON(); + assert.equal(json.subtype, 'merkleblock'); + assert.equal(typeof json.block, 'string'); + var b = bcoin.block(bcoin.block.fromJSON(json), json.subtype); + assert.equal(bcoin.utils.toHex(b.render()), json.block); + assert(b.verify()); + }); });