Merge pull request #938 from braydonf/tests/block

Fixed an issue with block deserialization
This commit is contained in:
Manuel Aráoz 2015-01-18 14:15:07 -03:00
commit 92b2e1e614
6 changed files with 77 additions and 90 deletions

View File

@ -9,7 +9,6 @@ var BufferWriter = require('./encoding/bufferwriter');
var Hash = require('./crypto/hash');
var JSUtil = require('./util/js');
var Transaction = require('./transaction');
var Varint = require('./encoding/varint');
/**
* Instantiate a Block from a Buffer, JSON object, or Object with
@ -44,27 +43,16 @@ Block._from = function _from(arg) {
info = Block._fromJSON(arg);
} else if (_.isObject(arg)) {
info = {
/**
* @name Block#magicnum
* @type number
*/
magicnum: arg.magicnum,
/**
* @name Block#size
* @type number
*/
size: arg.size,
/**
* @name Block#header
* @type {BlockHeader}
*/
header: arg.header,
txsvi: arg.txsvi,
/**
* @name Block#txs
* @name Block#transactions
* @type {Transaction[]}
*/
txs: arg.txs
transactions: arg.transactions
};
} else {
throw new TypeError('Unrecognized argument for Block');
@ -81,16 +69,13 @@ Block._fromJSON = function _fromJSON(data) {
if (JSUtil.isValidJSON(data)) {
data = JSON.parse(data);
}
var txs = [];
data.txs.forEach(function(tx) {
txs.push(Transaction().fromJSON(tx));
var transactions = [];
data.transactions.forEach(function(data) {
transactions.push(Transaction().fromJSON(data));
});
var info = {
magicnum: data.magicnum,
size: data.size,
header: BlockHeader.fromJSON(data.header),
txsvi: Varint().fromString(data.txsvi),
txs: txs
transactions: transactions
};
return info;
};
@ -111,14 +96,11 @@ Block.fromJSON = function fromJSON(json) {
*/
Block._fromBufferReader = function _fromBufferReader(br) {
var info = {};
info.magicnum = br.readUInt32LE();
info.size = br.readUInt32LE();
info.header = BlockHeader.fromBufferReader(br);
info.txsvi = Varint(br.readVarintBuf());
var txslen = info.txsvi.toNumber();
info.txs = [];
for (var i = 0; i < txslen; i++) {
info.txs.push(Transaction().fromBufferReader(br));
var transactions = br.readVarintNum();
info.transactions = [];
for (var i = 0; i < transactions; i++) {
info.transactions.push(Transaction().fromBufferReader(br));
}
return info;
};
@ -158,6 +140,7 @@ Block.fromRawBlock = function fromRawBlock(data) {
data = new Buffer(data, 'binary');
}
var br = BufferReader(data);
br.pos = Block.Values.START_OF_BLOCK;
var info = Block._fromBufferReader(br);
return new Block(info);
};
@ -166,16 +149,13 @@ Block.fromRawBlock = function fromRawBlock(data) {
* @returns {Object} - A plain object with the block properties
*/
Block.prototype.toObject = function toObject() {
var txs = [];
this.txs.forEach(function(tx) {
txs.push(tx.toObject());
var transactions = [];
this.transactions.forEach(function(tx) {
transactions.push(tx.toObject());
});
return {
magicnum: this.magicnum,
size: this.size,
header: this.header.toObject(),
txsvi: this.txsvi.toString(),
txs: txs
transactions: transactions
};
};
@ -208,13 +188,10 @@ Block.prototype.toBufferWriter = function toBufferWriter(bw) {
if (!bw) {
bw = new BufferWriter();
}
bw.writeUInt32LE(this.magicnum);
bw.writeUInt32LE(this.size);
bw.write(this.header.toBuffer());
bw.write(this.txsvi.buf);
var txslen = this.txsvi.toNumber();
for (var i = 0; i < txslen; i++) {
this.txs[i].toBufferWriter(bw);
bw.writeVarintNum(this.transactions.length);
for (var i = 0; i < this.transactions.length; i++) {
this.transactions[i].toBufferWriter(bw);
}
return bw;
};
@ -225,11 +202,11 @@ Block.prototype.toBufferWriter = function toBufferWriter(bw) {
*/
Block.prototype.getTransactionHashes = function getTransactionHashes() {
var hashes = [];
if (this.txs.length === 0) {
if (this.transactions.length === 0) {
return [Block.Values.NULL_HASH];
}
for (var t = 0; t < this.txs.length; t++) {
hashes.push(this.txs[t]._getHash());
for (var t = 0; t < this.transactions.length; t++) {
hashes.push(this.transactions[t]._getHash());
}
return hashes;
};
@ -245,7 +222,7 @@ Block.prototype.getMerkleTree = function getMerkleTree() {
var tree = this.getTransactionHashes();
var j = 0;
for (var size = this.txs.length; size > 1; size = Math.floor((size + 1) / 2)) {
for (var size = this.transactions.length; size > 1; size = Math.floor((size + 1) / 2)) {
for (var i = 0; i < size; i += 2) {
var i2 = Math.min(i + 1, size - 1);
var buf = Buffer.concat([tree[j + i], tree[j + i2]]);
@ -314,6 +291,7 @@ Block.prototype.inspect = function inspect() {
};
Block.Values = {
START_OF_BLOCK: 8, // Start of block in raw block data
NULL_HASH: new Buffer('0000000000000000000000000000000000000000000000000000000000000000', 'hex')
};

View File

@ -119,14 +119,13 @@ BlockHeader.fromString = function fromString(str) {
* @private
*/
BlockHeader._fromBufferReader = function _fromBufferReader(br) {
var info = {
version: br.readUInt32LE(),
prevHash: br.read(32),
merkleRoot: br.read(32),
time: br.readUInt32LE(),
bits: br.readUInt32LE(),
nonce: br.readUInt32LE()
};
var info = {};
info.version = br.readUInt32LE();
info.prevHash = br.read(32);
info.merkleRoot = br.read(32);
info.time = br.readUInt32LE();
info.bits = br.readUInt32LE();
info.nonce = br.readUInt32LE();
return info;
};

View File

@ -10,29 +10,26 @@ var chai = require('chai');
var fs = require('fs');
var should = chai.should();
var Transaction = bitcore.Transaction;
var Varint = bitcore.encoding.Varint;
// https://test-insight.bitpay.com/block/000000000b99b16390660d79fcc138d2ad0c89a0d044c4201a02bdf1f61ffa11
var dataRawBlockBuffer = fs.readFileSync('test/data/blk86756-testnet.dat');
var dataRawBlockBinary = fs.readFileSync('test/data/blk86756-testnet.dat', 'binary');
var dataJson = fs.readFileSync('test/data/blk86756-testnet.json').toString();
var data = require('./data/blk86756-testnet');
var dataBlocks = require('./data/bitcoind/blocks');
describe('Block', function() {
var magicnum = data.magicnum;
var blockhex = data.blockhex;
var blockbuf = new Buffer(blockhex, 'hex');
var size = data.blocksize;
var bh = BlockHeader.fromBuffer(new Buffer(data.blockheaderhex, 'hex'));
var txsvi = Varint().fromNumber(data.txsvi);
var txs = [];
JSON.parse(dataJson).txs.forEach(function(tx){
JSON.parse(dataJson).transactions.forEach(function(tx){
txs.push(new Transaction().fromJSON(tx));
});
var json = dataJson;
var genesishex = 'f9beb4d91d0100000100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000';
var genesishex = '0100000000000000000000000000000000000000000000000000000000000000000000003ba3edfd7a7b12b27ac72c3e67768f617fc81bc3888a51323a9fb8aa4b1e5e4a29ab5f49ffff001d1dac2b7c0101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff4d04ffff001d0104455468652054696d65732030332f4a616e2f32303039204368616e63656c6c6f72206f6e206272696e6b206f66207365636f6e64206261696c6f757420666f722062616e6b73ffffffff0100f2052a01000000434104678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5fac00000000';
var genesisbuf = new Buffer(genesishex, 'hex');
var genesisidhex = '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f';
@ -43,7 +40,7 @@ describe('Block', function() {
it('should not make an empty block', function() {
(function() {
var b = new Block();
return new Block();
}).should.throw('Unrecognized argument for Block');
});
@ -51,17 +48,18 @@ describe('Block', function() {
it('should set these known values', function() {
var b = new Block({
magicnum: magicnum,
size: size,
header: bh,
txsvi: txsvi,
txs: txs
transactions: txs
});
should.exist(b.magicnum);
should.exist(b.size);
should.exist(b.txsvi);
should.exist(b.header);
should.exist(b.txs);
should.exist(b.transactions);
});
it('should properly deserialize blocks', function() {
dataBlocks.forEach(function(block){
var b = Block.fromBuffer(new Buffer(block.data, 'hex'));
b.transactions.length.should.equal(block.transactions);
});
});
});
@ -86,21 +84,15 @@ describe('Block', function() {
it('should set these known values', function() {
var block = Block.fromJSON(json);
should.exist(block.magicnum);
should.exist(block.size);
should.exist(block.header);
should.exist(block.txsvi);
should.exist(block.txs);
should.exist(block.transactions);
});
it('should set these known values', function() {
var block = Block(json);
should.exist(block.magicnum);
should.exist(block.size);
should.exist(block.header);
should.exist(block.txsvi);
should.exist(block.txs);
should.exist(block.transactions);
});
it('accepts an object as argument', function() {
@ -115,11 +107,8 @@ describe('Block', function() {
it('should recover these known values', function() {
var block = Block.fromJSON(json);
var b = JSON.parse(block.toJSON());
should.exist(b.magicnum);
should.exist(b.size);
should.exist(b.header);
should.exist(b.txsvi);
should.exist(b.txs);
should.exist(b.transactions);
});
});
@ -216,14 +205,14 @@ describe('Block', function() {
it('should describe as invalid merkle root', function() {
var x = Block.fromRawBlock(dataRawBlockBinary);
x.txs.push(new Transaction());
x.transactions.push(new Transaction());
var valid = x.validMerkleRoot();
valid.should.equal(false);
});
it('should get a null hash merkle root', function() {
var x = Block.fromRawBlock(dataRawBlockBinary);
x.txs = []; // empty the txs
x.transactions = []; // empty the txs
var mr = x.getMerkleRoot();
mr.should.deep.equal(Block.Values.NULL_HASH);
});

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,6 +1,4 @@
{
"magicnum": 118034699,
"size": 8003,
"header": {
"version": 2,
"prevHash": "4baaa9507c3b27908397ea7bc177a998e9f4fe38b9d5130be7b5353c00000000",
@ -9,8 +7,7 @@
"bits": 473956288,
"nonce": 3594009557
},
"txsvi": "16",
"txs": [{
"transactions": [{
"version": 1,
"txinsvi": "01",
"txins": [{