2019-09-07 06:42:03 +02:00
|
|
|
import * as assert from 'assert';
|
|
|
|
import { beforeEach, describe, it } from 'mocha';
|
|
|
|
import { Block } from '..';
|
2014-10-16 06:30:57 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
import * as fixtures from './fixtures/block.json';
|
2014-10-16 06:30:57 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('Block', () => {
|
|
|
|
describe('version', () => {
|
|
|
|
it('should be interpreted as an int32le', () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const blockHex =
|
2019-09-12 10:35:08 +02:00
|
|
|
'ffffffff000000000000000000000000000000000000000000000000000000000000' +
|
|
|
|
'00004141414141414141414141414141414141414141414141414141414141414141' +
|
|
|
|
'01000000020000000300000000';
|
2019-09-07 06:42:03 +02:00
|
|
|
const block = Block.fromHex(blockHex);
|
|
|
|
assert.strictEqual(-1, block.version);
|
|
|
|
assert.strictEqual(1, block.timestamp);
|
|
|
|
});
|
|
|
|
});
|
2016-10-12 16:04:03 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('calculateTarget', () => {
|
|
|
|
fixtures.targets.forEach(f => {
|
|
|
|
it('returns ' + f.expected + ' for 0x' + f.bits, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const bits = parseInt(f.bits, 16);
|
2015-12-09 03:35:29 +01:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(
|
|
|
|
Block.calculateTarget(bits).toString('hex'),
|
|
|
|
f.expected,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-12-09 03:35:29 +01:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('fromBuffer/fromHex', () => {
|
|
|
|
fixtures.valid.forEach(f => {
|
|
|
|
it('imports ' + f.description, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const block = Block.fromHex(f.hex);
|
2014-10-16 06:30:57 +02:00
|
|
|
|
2019-09-07 06:42:03 +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) {
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(
|
|
|
|
block.witnessCommit.toString('hex'),
|
|
|
|
f.witnessCommit,
|
|
|
|
);
|
2018-12-27 10:26:08 +01:00
|
|
|
}
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(block.timestamp, f.timestamp);
|
|
|
|
assert.strictEqual(block.bits, f.bits);
|
|
|
|
assert.strictEqual(block.nonce, f.nonce);
|
|
|
|
assert.strictEqual(!block.transactions, f.hex.length === 160);
|
2019-11-29 07:30:23 +01:00
|
|
|
if (f.size && f.strippedSize && f.weight) {
|
|
|
|
assert.strictEqual(block.byteLength(false, true), f.size);
|
|
|
|
assert.strictEqual(block.byteLength(false, false), f.strippedSize);
|
|
|
|
assert.strictEqual(block.weight(), f.weight);
|
|
|
|
}
|
2019-09-07 06:42:03 +02:00
|
|
|
});
|
|
|
|
});
|
2014-10-16 06:30:57 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
fixtures.invalid.forEach(f => {
|
|
|
|
it('throws on ' + f.exception, () => {
|
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
Block.fromHex(f.hex);
|
|
|
|
}, new RegExp(f.exception));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-10-16 06:30:57 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('toBuffer/toHex', () => {
|
|
|
|
fixtures.valid.forEach(f => {
|
2019-09-07 06:42:03 +02:00
|
|
|
let block: Block;
|
2014-10-16 06:30:57 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
beforeEach(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
block = Block.fromHex(f.hex);
|
|
|
|
});
|
2014-10-16 06:30:57 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('exports ' + f.description, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(block.toHex(true), f.hex.slice(0, 160));
|
|
|
|
assert.strictEqual(block.toHex(), f.hex);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-10-16 06:30:57 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('getHash/getId', () => {
|
|
|
|
fixtures.valid.forEach(f => {
|
2019-09-07 06:42:03 +02:00
|
|
|
let block: Block;
|
2014-10-16 06:30:57 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
beforeEach(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
block = Block.fromHex(f.hex);
|
|
|
|
});
|
2014-10-16 06:30:57 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('returns ' + f.id + ' for ' + f.description, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(block.getHash().toString('hex'), f.hash);
|
|
|
|
assert.strictEqual(block.getId(), f.id);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-10-29 02:12:12 +01:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('getUTCDate', () => {
|
|
|
|
fixtures.valid.forEach(f => {
|
2019-09-07 06:42:03 +02:00
|
|
|
let block: Block;
|
2014-10-29 02:12:12 +01:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
beforeEach(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
block = Block.fromHex(f.hex);
|
|
|
|
});
|
2014-10-29 02:12:12 +01:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('returns UTC date of ' + f.id, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
const utcDate = block.getUTCDate().getTime();
|
2014-10-29 02:12:12 +01:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(utcDate, f.timestamp * 1e3);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-12-08 08:51:35 +01:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('calculateMerkleRoot', () => {
|
|
|
|
it('should throw on zero-length transaction array', () => {
|
|
|
|
assert.throws(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
Block.calculateMerkleRoot([]);
|
|
|
|
}, /Cannot compute merkle root for zero transactions/);
|
|
|
|
});
|
2016-05-04 16:42:05 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
fixtures.valid.forEach(f => {
|
2019-09-07 06:42:03 +02:00
|
|
|
if (f.hex.length === 160) return;
|
2016-05-04 16:42:05 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
let block: Block;
|
2016-05-04 16:42:05 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
beforeEach(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
block = Block.fromHex(f.hex);
|
|
|
|
});
|
2016-05-04 16:42:05 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('returns ' + f.merkleRoot + ' for ' + f.id, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(
|
|
|
|
Block.calculateMerkleRoot(block.transactions!).toString('hex'),
|
|
|
|
f.merkleRoot,
|
|
|
|
);
|
|
|
|
});
|
2018-12-27 10:26:08 +01:00
|
|
|
|
|
|
|
if (f.witnessCommit) {
|
2019-04-09 08:09:50 +02:00
|
|
|
it('returns witness commit ' + f.witnessCommit + ' for ' + f.id, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(
|
|
|
|
Block.calculateMerkleRoot(block.transactions!, true).toString(
|
|
|
|
'hex',
|
|
|
|
),
|
|
|
|
f.witnessCommit,
|
|
|
|
);
|
|
|
|
});
|
2018-12-27 10:26:08 +01:00
|
|
|
}
|
2019-09-07 06:42:03 +02:00
|
|
|
});
|
|
|
|
});
|
2016-05-04 16:42:05 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('checkTxRoots', () => {
|
|
|
|
fixtures.valid.forEach(f => {
|
2019-09-07 06:42:03 +02:00
|
|
|
if (f.hex.length === 160) return;
|
2016-05-04 16:42:05 +02:00
|
|
|
|
2019-09-07 06:42:03 +02:00
|
|
|
let block: Block;
|
2016-05-04 16:42:05 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
beforeEach(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
block = Block.fromHex(f.hex);
|
|
|
|
});
|
2016-05-04 16:42:05 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('returns ' + f.valid + ' for ' + f.id, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(block.checkTxRoots(), true);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2016-05-04 16:42:05 +02:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
describe('checkProofOfWork', () => {
|
|
|
|
fixtures.valid.forEach(f => {
|
2019-09-07 06:42:03 +02:00
|
|
|
let block: Block;
|
2015-12-08 08:51:35 +01:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
beforeEach(() => {
|
2019-09-07 06:42:03 +02:00
|
|
|
block = Block.fromHex(f.hex);
|
|
|
|
});
|
2015-12-08 08:51:35 +01:00
|
|
|
|
2019-04-09 08:09:50 +02:00
|
|
|
it('returns ' + f.valid + ' for ' + f.id, () => {
|
2019-09-07 06:42:03 +02:00
|
|
|
assert.strictEqual(block.checkProofOfWork(), f.valid);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|