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