2015-02-23 00:36:57 +01:00
|
|
|
/* global describe, it, beforeEach */
|
|
|
|
|
2014-10-16 06:30:57 +02:00
|
|
|
var assert = require('assert')
|
|
|
|
|
|
|
|
var Block = require('../src/block')
|
|
|
|
|
|
|
|
var fixtures = require('./fixtures/block')
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('Block', function () {
|
|
|
|
describe('fromBuffer/fromHex', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
it('imports the block: ' + f.description + ' correctly', function () {
|
2014-10-16 06:30:57 +02:00
|
|
|
var block = Block.fromHex(f.hex)
|
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(block.version, f.version)
|
|
|
|
assert.strictEqual(block.prevHash.toString('hex'), f.prevHash)
|
|
|
|
assert.strictEqual(block.merkleRoot.toString('hex'), f.merkleRoot)
|
|
|
|
assert.strictEqual(block.timestamp, f.timestamp)
|
|
|
|
assert.strictEqual(block.bits, f.bits)
|
|
|
|
assert.strictEqual(block.nonce, f.nonce)
|
2014-10-16 06:30:57 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
fixtures.invalid.forEach(function (f) {
|
|
|
|
it('throws on ' + f.exception, function () {
|
|
|
|
assert.throws(function () {
|
2014-10-16 06:30:57 +02:00
|
|
|
Block.fromHex(f.hex)
|
|
|
|
}, new RegExp(f.exception))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('toBuffer/toHex', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2014-10-16 06:30:57 +02:00
|
|
|
var block
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
beforeEach(function () {
|
2014-10-16 06:30:57 +02:00
|
|
|
block = Block.fromHex(f.hex)
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('exports the block: ' + f.description + ' correctly', function () {
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(block.toHex(), f.hex)
|
2014-10-16 06:30:57 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('getHash', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2014-10-16 06:30:57 +02:00
|
|
|
var block
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
beforeEach(function () {
|
2014-10-16 06:30:57 +02:00
|
|
|
block = Block.fromHex(f.hex)
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('calculates ' + f.hash + ' for the block: ' + f.description, function () {
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(block.getHash().toString('hex'), f.hash)
|
2014-10-16 06:30:57 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('getId', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2014-10-16 06:30:57 +02:00
|
|
|
var block
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
beforeEach(function () {
|
2014-10-16 06:30:57 +02:00
|
|
|
block = Block.fromHex(f.hex)
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('calculates ' + f.id + ' for the block: ' + f.description, function () {
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(block.getId(), f.id)
|
2014-10-16 06:30:57 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-10-29 02:12:12 +01:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('getUTCDate', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2014-10-29 02:12:12 +01:00
|
|
|
var block
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
beforeEach(function () {
|
2014-10-29 02:12:12 +01:00
|
|
|
block = Block.fromHex(f.hex)
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
it('returns UTC date of ' + f.id, function () {
|
2014-10-29 02:12:12 +01:00
|
|
|
var utcDate = block.getUTCDate().getTime()
|
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(utcDate, f.timestamp * 1e3)
|
2014-10-29 02:12:12 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-10-16 06:30:57 +02:00
|
|
|
})
|