2014-11-20 13:21:13 -08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var bitcore = require('..');
|
2014-12-06 12:43:31 -08:00
|
|
|
var BN = require('../lib/crypto/bn');
|
2014-11-21 08:26:30 -08:00
|
|
|
var BufferReader = bitcore.encoding.BufferReader;
|
2014-12-19 11:36:57 -08:00
|
|
|
var BufferWriter = bitcore.encoding.BufferWriter;
|
|
|
|
|
2014-12-06 12:43:31 -08:00
|
|
|
var BlockHeader = bitcore.BlockHeader;
|
|
|
|
var fs = require('fs');
|
|
|
|
var should = require('chai').should();
|
|
|
|
|
|
|
|
// 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 dataRawId = '000000000b99b16390660d79fcc138d2ad0c89a0d044c4201a02bdf1f61ffa11';
|
|
|
|
var data = require('./data/blk86756-testnet');
|
|
|
|
|
|
|
|
describe('BlockHeader', function() {
|
|
|
|
|
|
|
|
var version = data.version;
|
|
|
|
var prevblockidbuf = new Buffer(data.prevblockidhex, 'hex');
|
|
|
|
var merklerootbuf = new Buffer(data.merkleroothex, 'hex');
|
|
|
|
var time = data.time;
|
|
|
|
var bits = data.bits;
|
|
|
|
var nonce = data.nonce;
|
|
|
|
var bh = new BlockHeader({
|
2014-09-16 17:28:00 -07:00
|
|
|
version: version,
|
2014-12-16 15:16:05 -08:00
|
|
|
prevHash: prevblockidbuf,
|
|
|
|
merkleRoot: merklerootbuf,
|
2014-09-16 17:28:00 -07:00
|
|
|
time: time,
|
|
|
|
bits: bits,
|
|
|
|
nonce: nonce
|
|
|
|
});
|
2014-12-06 12:43:31 -08:00
|
|
|
var bhhex = data.blockheaderhex;
|
2014-11-20 13:21:13 -08:00
|
|
|
var bhbuf = new Buffer(bhhex, 'hex');
|
2014-09-16 17:28:00 -07:00
|
|
|
|
2014-09-22 13:07:39 -07:00
|
|
|
it('should make a new blockheader', function() {
|
2014-12-06 12:43:31 -08:00
|
|
|
BlockHeader(bhbuf).toBuffer().toString('hex').should.equal(bhhex);
|
2014-09-22 13:07:39 -07:00
|
|
|
});
|
|
|
|
|
2014-12-06 12:43:31 -08:00
|
|
|
it('should not make an empty block', function() {
|
|
|
|
(function() {
|
|
|
|
BlockHeader();
|
|
|
|
}).should.throw('Unrecognized argument for BlockHeader');
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#constructor', function() {
|
2014-09-16 17:28:00 -07:00
|
|
|
|
|
|
|
it('should set all the variables', function() {
|
2014-12-06 12:43:31 -08:00
|
|
|
var bh = new BlockHeader({
|
2014-09-16 17:28:00 -07:00
|
|
|
version: version,
|
2014-12-16 15:16:05 -08:00
|
|
|
prevHash: prevblockidbuf,
|
|
|
|
merkleRoot: merklerootbuf,
|
2014-09-16 17:28:00 -07:00
|
|
|
time: time,
|
|
|
|
bits: bits,
|
|
|
|
nonce: nonce
|
|
|
|
});
|
|
|
|
should.exist(bh.version);
|
2014-12-16 15:16:05 -08:00
|
|
|
should.exist(bh.prevHash);
|
|
|
|
should.exist(bh.merkleRoot);
|
2014-09-16 17:28:00 -07:00
|
|
|
should.exist(bh.time);
|
|
|
|
should.exist(bh.bits);
|
|
|
|
should.exist(bh.nonce);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-09-18 17:52:02 -07:00
|
|
|
describe('#fromJSON', function() {
|
|
|
|
|
|
|
|
it('should set all the variables', function() {
|
2015-04-28 13:10:06 -07:00
|
|
|
var bh = BlockHeader.fromJSON(JSON.stringify({
|
2014-09-18 17:52:02 -07:00
|
|
|
version: version,
|
2014-12-16 15:16:05 -08:00
|
|
|
prevHash: prevblockidbuf.toString('hex'),
|
|
|
|
merkleRoot: merklerootbuf.toString('hex'),
|
2014-09-18 17:52:02 -07:00
|
|
|
time: time,
|
|
|
|
bits: bits,
|
|
|
|
nonce: nonce
|
2015-04-28 13:10:06 -07:00
|
|
|
}));
|
2014-09-18 17:52:02 -07:00
|
|
|
should.exist(bh.version);
|
2014-12-16 15:16:05 -08:00
|
|
|
should.exist(bh.prevHash);
|
|
|
|
should.exist(bh.merkleRoot);
|
2014-09-18 17:52:02 -07:00
|
|
|
should.exist(bh.time);
|
|
|
|
should.exist(bh.bits);
|
|
|
|
should.exist(bh.nonce);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#toJSON', function() {
|
|
|
|
|
|
|
|
it('should set all the variables', function() {
|
2014-12-12 15:34:43 -08:00
|
|
|
var json = JSON.parse(bh.toJSON());
|
2014-09-18 17:52:02 -07:00
|
|
|
should.exist(json.version);
|
2014-12-16 15:16:05 -08:00
|
|
|
should.exist(json.prevHash);
|
|
|
|
should.exist(json.merkleRoot);
|
2014-09-18 17:52:02 -07:00
|
|
|
should.exist(json.time);
|
|
|
|
should.exist(json.bits);
|
|
|
|
should.exist(json.nonce);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-12-06 12:43:31 -08:00
|
|
|
describe('#fromJSON', function() {
|
|
|
|
|
|
|
|
it('should parse this known json string', function() {
|
|
|
|
|
|
|
|
var jsonString = JSON.stringify({
|
|
|
|
version: version,
|
2014-12-16 15:16:05 -08:00
|
|
|
prevHash: prevblockidbuf,
|
|
|
|
merkleRoot: merklerootbuf,
|
2014-12-06 12:43:31 -08:00
|
|
|
time: time,
|
|
|
|
bits: bits,
|
|
|
|
nonce: nonce
|
|
|
|
});
|
|
|
|
|
|
|
|
var json = new BlockHeader(jsonString);
|
|
|
|
should.exist(json.version);
|
2014-12-16 15:16:05 -08:00
|
|
|
should.exist(json.prevHash);
|
|
|
|
should.exist(json.merkleRoot);
|
2014-12-06 12:43:31 -08:00
|
|
|
should.exist(json.time);
|
|
|
|
should.exist(json.bits);
|
|
|
|
should.exist(json.nonce);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-12-11 22:01:14 -08:00
|
|
|
describe('#fromString/#toString', function() {
|
|
|
|
|
|
|
|
it('should output/input a block hex string', function() {
|
|
|
|
var b = BlockHeader.fromString(bhhex);
|
|
|
|
b.toString().should.equal(bhhex);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-09-16 17:28:00 -07:00
|
|
|
describe('#fromBuffer', function() {
|
|
|
|
|
|
|
|
it('should parse this known buffer', function() {
|
2014-12-06 12:43:31 -08:00
|
|
|
BlockHeader.fromBuffer(bhbuf).toBuffer().toString('hex').should.equal(bhhex);
|
2014-09-16 17:28:00 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#fromBufferReader', function() {
|
|
|
|
|
|
|
|
it('should parse this known buffer', function() {
|
2014-12-06 12:43:31 -08:00
|
|
|
BlockHeader.fromBufferReader(BufferReader(bhbuf)).toBuffer().toString('hex').should.equal(bhhex);
|
2014-09-16 17:28:00 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#toBuffer', function() {
|
|
|
|
|
|
|
|
it('should output this known buffer', function() {
|
2014-12-06 12:43:31 -08:00
|
|
|
BlockHeader.fromBuffer(bhbuf).toBuffer().toString('hex').should.equal(bhhex);
|
2014-09-16 17:28:00 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#toBufferWriter', function() {
|
|
|
|
|
|
|
|
it('should output this known buffer', function() {
|
2014-12-06 12:43:31 -08:00
|
|
|
BlockHeader.fromBuffer(bhbuf).toBufferWriter().concat().toString('hex').should.equal(bhhex);
|
|
|
|
});
|
|
|
|
|
2014-12-19 11:36:57 -08:00
|
|
|
it('doesn\'t create a bufferWriter if one provided', function() {
|
|
|
|
var writer = new BufferWriter();
|
|
|
|
var blockHeader = BlockHeader.fromBuffer(bhbuf);
|
|
|
|
blockHeader.toBufferWriter(writer).should.equal(writer);
|
|
|
|
});
|
|
|
|
|
2014-12-06 12:43:31 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('#inspect', function() {
|
|
|
|
|
|
|
|
it('should return the correct inspect of the genesis block', function() {
|
|
|
|
var block = BlockHeader.fromRawBlock(dataRawBlockBinary);
|
|
|
|
block.inspect().should.equal('<BlockHeader '+dataRawId+'>');
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#fromRawBlock', function() {
|
|
|
|
|
|
|
|
it('should instantiate from a raw block binary', function() {
|
|
|
|
var x = BlockHeader.fromRawBlock(dataRawBlockBinary);
|
|
|
|
x.version.should.equal(2);
|
2015-02-05 11:46:18 -08:00
|
|
|
new BN(x.bits).toString('hex').should.equal('1c3fffc0');
|
2014-12-06 12:43:31 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should instantiate from raw block buffer', function() {
|
|
|
|
var x = BlockHeader.fromRawBlock(dataRawBlockBuffer);
|
|
|
|
x.version.should.equal(2);
|
2015-02-05 11:46:18 -08:00
|
|
|
new BN(x.bits).toString('hex').should.equal('1c3fffc0');
|
2014-12-06 12:43:31 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#validTimestamp', function() {
|
|
|
|
|
|
|
|
var x = BlockHeader.fromRawBlock(dataRawBlockBuffer);
|
|
|
|
|
|
|
|
it('should validate timpstamp as true', function() {
|
|
|
|
var valid = x.validTimestamp(x);
|
|
|
|
valid.should.equal(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('should validate timestamp as false', function() {
|
|
|
|
x.time = Math.round(new Date().getTime() / 1000) + BlockHeader.Constants.MAX_TIME_OFFSET + 100;
|
|
|
|
var valid = x.validTimestamp(x);
|
|
|
|
valid.should.equal(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('#validProofOfWork', function() {
|
|
|
|
|
|
|
|
it('should validate proof-of-work as true', function() {
|
2014-12-10 10:09:37 -08:00
|
|
|
var x = BlockHeader.fromRawBlock(dataRawBlockBuffer);
|
2014-12-06 12:43:31 -08:00
|
|
|
var valid = x.validProofOfWork(x);
|
|
|
|
valid.should.equal(true);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should validate proof of work as false because incorrect proof of work', function() {
|
2014-12-10 10:09:37 -08:00
|
|
|
var x = BlockHeader.fromRawBlock(dataRawBlockBuffer);
|
2014-12-06 12:43:31 -08:00
|
|
|
var nonce = x.nonce;
|
|
|
|
x.nonce = 0;
|
|
|
|
var valid = x.validProofOfWork(x);
|
|
|
|
valid.should.equal(false);
|
|
|
|
x.nonce = nonce;
|
2014-09-16 17:28:00 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2014-12-19 11:36:57 -08:00
|
|
|
it('coverage: caches the "_id" property', function() {
|
|
|
|
var blockHeader = BlockHeader.fromRawBlock(dataRawBlockBuffer);
|
|
|
|
blockHeader.id.should.equal(blockHeader.id);
|
|
|
|
});
|
|
|
|
|
2014-09-16 17:28:00 -07:00
|
|
|
});
|