2018-07-23 09:45:01 +02:00
|
|
|
const { describe, it, beforeEach } = require('mocha')
|
2018-06-25 08:25:12 +02:00
|
|
|
const assert = require('assert')
|
2018-12-27 09:49:53 +01:00
|
|
|
const Block = require('..').Block
|
2014-10-16 06:30:57 +02:00
|
|
|
|
2018-06-25 08:25:12 +02:00
|
|
|
const fixtures = require('./fixtures/block')
|
2014-10-16 06:30:57 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('Block', function () {
|
2016-10-12 16:04:03 +02:00
|
|
|
describe('version', function () {
|
|
|
|
it('should be interpreted as an int32le', function () {
|
2018-06-25 08:37:45 +02:00
|
|
|
const blockHex = 'ffffffff0000000000000000000000000000000000000000000000000000000000000000414141414141414141414141414141414141414141414141414141414141414101000000020000000300000000'
|
|
|
|
const block = Block.fromHex(blockHex)
|
2016-10-12 16:04:03 +02:00
|
|
|
assert.equal(-1, block.version)
|
|
|
|
assert.equal(1, block.timestamp)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-12-09 03:35:29 +01:00
|
|
|
describe('calculateTarget', function () {
|
|
|
|
fixtures.targets.forEach(function (f) {
|
|
|
|
it('returns ' + f.expected + ' for 0x' + f.bits, function () {
|
2018-06-25 08:37:45 +02:00
|
|
|
const bits = parseInt(f.bits, 16)
|
2015-12-09 03:35:29 +01:00
|
|
|
|
|
|
|
assert.equal(Block.calculateTarget(bits).toString('hex'), f.expected)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
describe('fromBuffer/fromHex', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
2017-04-18 03:52:11 +02:00
|
|
|
it('imports ' + f.description, function () {
|
2018-06-25 08:37:45 +02:00
|
|
|
const block = Block.fromHex(f.hex)
|
2014-10-16 06:30:57 +02:00
|
|
|
|
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)
|
2018-12-27 10:26:08 +01:00
|
|
|
if (block.witnessCommit) {
|
|
|
|
assert.strictEqual(block.witnessCommit.toString('hex'), f.witnessCommit)
|
|
|
|
}
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(block.timestamp, f.timestamp)
|
|
|
|
assert.strictEqual(block.bits, f.bits)
|
|
|
|
assert.strictEqual(block.nonce, f.nonce)
|
2017-04-18 03:52:11 +02:00
|
|
|
assert.strictEqual(!block.transactions, f.hex.length === 160)
|
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) {
|
2018-06-25 08:37:45 +02:00
|
|
|
let block
|
2014-10-16 06:30:57 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
beforeEach(function () {
|
2014-10-16 06:30:57 +02:00
|
|
|
block = Block.fromHex(f.hex)
|
|
|
|
})
|
|
|
|
|
2017-04-18 03:52:11 +02:00
|
|
|
it('exports ' + f.description, function () {
|
2017-04-18 03:56:35 +02:00
|
|
|
assert.strictEqual(block.toHex(true), f.hex.slice(0, 160))
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(block.toHex(), f.hex)
|
2014-10-16 06:30:57 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-04-18 03:52:11 +02:00
|
|
|
describe('getHash/getId', function () {
|
2015-02-23 00:36:57 +01:00
|
|
|
fixtures.valid.forEach(function (f) {
|
2018-06-25 08:37:45 +02:00
|
|
|
let block
|
2014-10-16 06:30:57 +02:00
|
|
|
|
2015-02-23 00:36:57 +01:00
|
|
|
beforeEach(function () {
|
2014-10-16 06:30:57 +02:00
|
|
|
block = Block.fromHex(f.hex)
|
|
|
|
})
|
|
|
|
|
2017-04-18 03:52:11 +02:00
|
|
|
it('returns ' + f.id + ' for ' + f.description, function () {
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(block.getHash().toString('hex'), f.hash)
|
|
|
|
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) {
|
2018-06-25 08:37:45 +02:00
|
|
|
let block
|
2014-10-29 02:12:12 +01:00
|
|
|
|
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 () {
|
2018-06-25 08:37:45 +02:00
|
|
|
const utcDate = block.getUTCDate().getTime()
|
2014-10-29 02:12:12 +01:00
|
|
|
|
2015-05-07 03:29:20 +02:00
|
|
|
assert.strictEqual(utcDate, f.timestamp * 1e3)
|
2014-10-29 02:12:12 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2015-12-08 08:51:35 +01:00
|
|
|
|
2016-05-04 16:42:05 +02:00
|
|
|
describe('calculateMerkleRoot', function () {
|
|
|
|
it('should throw on zero-length transaction array', function () {
|
|
|
|
assert.throws(function () {
|
|
|
|
Block.calculateMerkleRoot([])
|
|
|
|
}, /Cannot compute merkle root for zero transactions/)
|
|
|
|
})
|
|
|
|
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
if (f.hex.length === 160) return
|
|
|
|
|
2018-06-25 08:37:45 +02:00
|
|
|
let block
|
2016-05-04 16:42:05 +02:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
block = Block.fromHex(f.hex)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('returns ' + f.merkleRoot + ' for ' + f.id, function () {
|
|
|
|
assert.strictEqual(Block.calculateMerkleRoot(block.transactions).toString('hex'), f.merkleRoot)
|
|
|
|
})
|
2018-12-27 10:26:08 +01:00
|
|
|
|
|
|
|
if (f.witnessCommit) {
|
|
|
|
it('returns witness commit ' + f.witnessCommit + ' for ' + f.id, function () {
|
|
|
|
assert.strictEqual(Block.calculateMerkleRoot(block.transactions, true).toString('hex'), f.witnessCommit)
|
|
|
|
})
|
|
|
|
}
|
2016-05-04 16:42:05 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('checkMerkleRoot', function () {
|
|
|
|
fixtures.valid.forEach(function (f) {
|
|
|
|
if (f.hex.length === 160) return
|
|
|
|
|
2018-06-25 08:37:45 +02:00
|
|
|
let block
|
2016-05-04 16:42:05 +02:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
block = Block.fromHex(f.hex)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('returns ' + f.valid + ' for ' + f.id, function () {
|
|
|
|
assert.strictEqual(block.checkMerkleRoot(), true)
|
|
|
|
})
|
2018-12-27 10:26:08 +01:00
|
|
|
|
|
|
|
if (f.witnessCommit) {
|
|
|
|
it('validates witness commit for ' + f.id, function () {
|
|
|
|
assert.strictEqual(block.checkWitnessCommit(), true)
|
|
|
|
})
|
|
|
|
}
|
2016-05-04 16:42:05 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2016-01-04 02:49:28 +01:00
|
|
|
describe('checkProofOfWork', function () {
|
2015-12-08 08:51:35 +01:00
|
|
|
fixtures.valid.forEach(function (f) {
|
2018-06-25 08:37:45 +02:00
|
|
|
let block
|
2015-12-08 08:51:35 +01:00
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
block = Block.fromHex(f.hex)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('returns ' + f.valid + ' for ' + f.id, function () {
|
2016-01-04 02:49:28 +01:00
|
|
|
assert.strictEqual(block.checkProofOfWork(), f.valid)
|
2015-12-08 08:51:35 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
2014-10-16 06:30:57 +02:00
|
|
|
})
|