bitcore-lib-doge
Version:
A pure and powerful JavaScript Dogecoin library.
298 lines (232 loc) • 8.5 kB
JavaScript
;
var bitcore = require('../..');
var BN = require('../../lib/crypto/bn');
var BufferReader = bitcore.encoding.BufferReader;
var BufferWriter = bitcore.encoding.BufferWriter;
var BlockHeader = bitcore.BlockHeader;
var fs = require('fs');
var should = require('chai').should();
// https://bitpay.com/insight/#/DOGE/testnet/block/c2039bfaf5e3b903e2668b100abfb18727f7f4f4a05d42c619382f55b7fa7909
const rawBlockBin = fs.readFileSync('test/data/blk160775-testnet.dat');
const rawBlock = rawBlockBin.toString('hex');
var dataRawId = 'c2039bfaf5e3b903e2668b100abfb18727f7f4f4a05d42c619382f55b7fa7909';
var data = require('../data/blk160775-testnet');
describe('BlockHeader', function() {
var version = data.version;
var prevblockidbuf = Buffer.from(data.prevblockidhex, 'hex');
var merklerootbuf = Buffer.from(data.merkleroothex, 'hex');
var time = data.time;
var bits = data.bits;
var nonce = data.nonce;
var bh = new BlockHeader({
version: version,
prevHash: prevblockidbuf,
merkleRoot: merklerootbuf,
time: time,
bits: bits,
nonce: nonce
});
var b = bitcore.Block.fromString(rawBlock);
var dataRawBlockBuffer = b.toBuffer();
var dataRawBlockBinary = dataRawBlockBuffer;
var bhhex = b.header.toString();
var bhbuf = Buffer.from(bhhex, 'hex');
it('should make a new blockheader', function() {
BlockHeader(bhbuf).toBuffer().toString('hex').should.equal(bhhex);
});
it('should not make an empty block', function() {
(function() {
BlockHeader();
}).should.throw('Unrecognized argument for BlockHeader');
});
it('coverage: caches the "_id" property', function() {
var blockHeader = BlockHeader.fromRawBlock(dataRawBlockBuffer);
blockHeader.id.should.equal(blockHeader.id);
});
describe('#constructor', function() {
it('should set all the variables', function() {
var bh = new BlockHeader({
version: version,
prevHash: prevblockidbuf,
merkleRoot: merklerootbuf,
time: time,
bits: bits,
nonce: nonce
});
should.exist(bh.version);
should.exist(bh.prevHash);
should.exist(bh.merkleRoot);
should.exist(bh.time);
should.exist(bh.bits);
should.exist(bh.nonce);
});
it('will throw an error if the argument object hash property doesn\'t match', function() {
(function() {
var bh = new BlockHeader({
hash: '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f',
version: version,
prevHash: prevblockidbuf,
merkleRoot: merklerootbuf,
time: time,
bits: bits,
nonce: nonce
});
}).should.throw('Argument object hash property does not match block hash.');
});
});
describe('#fromObject', function() {
it('should set all the variables', function() {
var bh = BlockHeader.fromObject({
version: version,
prevHash: prevblockidbuf.toString('hex'),
merkleRoot: merklerootbuf.toString('hex'),
time: time,
bits: bits,
nonce: nonce
});
should.exist(bh.version);
should.exist(bh.prevHash);
should.exist(bh.merkleRoot);
should.exist(bh.time);
should.exist(bh.bits);
should.exist(bh.nonce);
});
});
describe('#toJSON', function() {
it('should set all the variables', function() {
var json = bh.toJSON();
should.exist(json.version);
should.exist(json.prevHash);
should.exist(json.merkleRoot);
should.exist(json.time);
should.exist(json.bits);
should.exist(json.nonce);
});
});
describe('#fromJSON', function() {
it('should parse this known json string', function() {
var jsonString = JSON.stringify({
version: version,
prevHash: prevblockidbuf,
merkleRoot: merklerootbuf,
time: time,
bits: bits,
nonce: nonce
});
var json = new BlockHeader(JSON.parse(jsonString));
should.exist(json.version);
should.exist(json.prevHash);
should.exist(json.merkleRoot);
should.exist(json.time);
should.exist(json.bits);
should.exist(json.nonce);
});
});
describe('#fromString/#toString', function() {
it('should output/input a block hex string', function() {
var b = BlockHeader.fromString(bhhex);
b.toString().should.equal(bhhex);
});
});
describe('#fromBuffer', function() {
it('should parse this known buffer', function() {
BlockHeader.fromBuffer(bhbuf).toBuffer().toString('hex').should.equal(bhhex);
});
});
describe('#fromBufferReader', function() {
it('should parse this known buffer', function() {
BlockHeader.fromBufferReader(BufferReader(bhbuf)).toBuffer().toString('hex').should.equal(bhhex);
});
});
describe('#toBuffer', function() {
it('should output this known buffer', function() {
BlockHeader.fromBuffer(bhbuf).toBuffer().toString('hex').should.equal(bhhex);
});
});
describe('#toBufferWriter', function() {
it('should output this known buffer', function() {
BlockHeader.fromBuffer(bhbuf).toBufferWriter().concat().toString('hex').should.equal(bhhex);
});
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);
});
});
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.toString('binary'));
x.version.should.equal(data.version);
x.bits.should.equal(data.bits);
});
it('should instantiate from raw block buffer', function() {
var x = BlockHeader.fromRawBlock(dataRawBlockBuffer);
x.version.should.equal(data.version);
x.bits.should.equal(data.bits);
});
});
describe('#validTimestamp', function() {
var x = BlockHeader.fromRawBlock(dataRawBlockBuffer);
it('should validate timestamp 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() {
var x = BlockHeader.fromRawBlock(dataRawBlockBuffer);
var valid = x.validProofOfWork(x);
valid.should.equal(true);
});
it('should validate proof of work as false because incorrect proof of work', function() {
var x = BlockHeader.fromRawBlock(dataRawBlockBuffer);
var nonce = x.nonce;
x.nonce = 0;
var valid = x.validProofOfWork(x);
valid.should.equal(false);
x.nonce = nonce;
});
});
describe('#getDifficulty', function() {
it('should get the correct difficulty for testnet block 160775', function() {
var x = BlockHeader.fromRawBlock(dataRawBlockBuffer);
x.bits.should.equal(0x1e0fffff);
x.getDifficulty().should.equal(data.difficulty);
});
it('should get the correct difficulty for testnet block 2402952', function() {
var x = new BlockHeader({
bits: 0x1d702e1f
});
x.getDifficulty().should.equal(0.008914096187362101);
});
it('should get the correct difficulty for livenet block 3637988', function() {
var x = new BlockHeader({
bits: 0x1a02ba1b
});
x.getDifficulty().should.equal(6152224.774417369);
});
it('should get the correct difficulty for livenet block 831920', function() {
var x = new BlockHeader({
bits: 0x1b056760
});
x.getDifficulty().should.equal(12127.557603686635);
});
it('should use exponent notation if difficulty is larger than Javascript number', function() {
var x = new BlockHeader({
bits: 0x0900c2a8
});
x.getDifficulty().should.equal(1.9220482782645836 * 1e48);
});
});
});