block: add block tests. frame correct version for blocks.

Signed-off-by: Fedor Indutny <fedor@indutny.com>
This commit is contained in:
Christopher Jeffrey 2014-05-18 13:03:27 -05:00 committed by Fedor Indutny
parent 745473784f
commit db69c882f2
3 changed files with 47 additions and 26 deletions

View File

@ -53,8 +53,8 @@ Block.prototype.verify = function verify() {
return !this.invalid && utils.testTarget(this.bits, this.hash()); return !this.invalid && utils.testTarget(this.bits, this.hash());
}; };
Block.prototype.render = function render(framer) { Block.prototype.render = function render() {
return []; return bcoin.protocol.framer.block(this, this.subtype);
}; };
Block.prototype.hasTX = function hasTX(hash) { Block.prototype.hasTX = function hasTX(hash) {

View File

@ -259,7 +259,7 @@ Framer.block = function _block(block, type) {
// version // version
assert.equal(off, 0); assert.equal(off, 0);
off += writeU32(p, constants.version, off); off += writeU32(p, block.version, off);
// prev_block // prev_block
assert.equal(off, 4); assert.equal(off, 4);

View File

@ -3,7 +3,7 @@ var bn = require('bn.js');
var bcoin = require('../'); var bcoin = require('../');
describe('Block', function() { describe('Block', function() {
it('should parse partial merkle tree', function() { var parser = bcoin.protocol.parser();
var block = bcoin.block({ var block = bcoin.block({
type: 'block', type: 'block',
version: 2, version: 2,
@ -26,7 +26,9 @@ describe('Block', function() {
'33825657ba32afe269819f01993bd77baba86379043168c94845d32370e53562' ], '33825657ba32afe269819f01993bd77baba86379043168c94845d32370e53562' ],
flags: [ 245, 90, 0 ] flags: [ 245, 90, 0 ]
}, 'merkleblock'); }, 'merkleblock');
var raw = block.toJSON().block;
it('should parse partial merkle tree', function() {
assert(block.verify()); assert(block.verify());
assert.equal(block.tx.length, 2); assert.equal(block.tx.length, 2);
assert.equal( assert.equal(
@ -36,4 +38,23 @@ describe('Block', function() {
block.tx[1], block.tx[1],
'ec8c51de3170301430ec56f6703533d9ea5b05c6fa7068954bcb90eed8c2ee5c'); '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());
});
}); });