test: move consensus tests out of utils tests.

This commit is contained in:
Christopher Jeffrey 2017-08-13 13:01:43 -07:00
parent b021f7cdd4
commit 46af7f5760
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 67 additions and 29 deletions

View File

@ -92,23 +92,6 @@ describe('Block', function() {
assert(block2.verify());
});
it('should calculate reward properly', () => {
let height = 0;
let total = 0;
for (;;) {
const reward = consensus.getReward(height, 210000);
assert(reward <= consensus.COIN * 50);
total += reward;
if (reward === 0)
break;
height++;
}
assert.strictEqual(height, 6930000);
assert.strictEqual(total, 2099999997690000);
});
it('should parse JSON', () => {
const [block1] = block300025.getBlock();
const block2 = Block.fromJSON(block1.toJSON());

67
test/consensus-test.js Normal file
View File

@ -0,0 +1,67 @@
/* eslint-env mocha */
/* eslint prefer-arrow-callback: "off" */
'use strict';
const assert = require('./util/assert');
const consensus = require('../lib/protocol/consensus');
const BN = require('../lib/crypto/bn');
describe('Consensus', function() {
it('should calculate reward properly', () => {
let height = 0;
let total = 0;
for (;;) {
const reward = consensus.getReward(height, 210000);
assert(reward <= consensus.COIN * 50);
total += reward;
if (reward === 0)
break;
height++;
}
assert.strictEqual(height, 6930000);
assert.strictEqual(total, 2099999997690000);
});
it('should verify proof-of-work', () => {
const bits = 0x1900896c;
const hash = Buffer.from(
'672b3f1bb11a994267ea4171069ba0aa4448a840f38e8f340000000000000000',
'hex'
);
assert(consensus.verifyPOW(hash, bits));
});
it('should convert bits to target', () => {
const bits = 0x1900896c;
const target = consensus.fromCompact(bits);
const expected = new BN(
'0000000000000000896c00000000000000000000000000000000000000000000',
'hex');
assert.strictEqual(target.toString('hex'), expected.toString('hex'));
});
it('should convert target to bits', () => {
const target = new BN(
'0000000000000000896c00000000000000000000000000000000000000000000',
'hex');
const bits = consensus.toCompact(target);
const expected = 0x1900896c;
assert.strictEqual(bits, expected);
});
it('should check version bit', () => {
assert(consensus.hasBit(0x20000001, 0));
assert(!consensus.hasBit(0x20000000, 0));
assert(!consensus.hasBit(0x10000001, 0));
assert(consensus.hasBit(0x20000003, 1));
assert(consensus.hasBit(0x20000003, 0));
});
});

View File

@ -8,7 +8,6 @@ const BN = require('../lib/crypto/bn');
const base58 = require('../lib/utils/base58');
const encoding = require('../lib/utils/encoding');
const Amount = require('../lib/btc/amount');
const consensus = require('../lib/protocol/consensus');
const Validator = require('../lib/utils/validator');
const util = require('../lib/utils/util');
@ -72,17 +71,6 @@ describe('Utils', function() {
}
});
it('should verify proof-of-work', () => {
const bits = 0x1900896c;
const hash = Buffer.from(
'672b3f1bb11a994267ea4171069ba0aa4448a840f38e8f340000000000000000',
'hex'
);
assert.strictEqual(consensus.verifyPOW(hash, bits), true);
});
it('should convert satoshi to btc', () => {
let btc = Amount.btc(5460);
assert.strictEqual(btc, '0.0000546');