From 46af7f5760dc347df698d1f4179eb9e702618b8d Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sun, 13 Aug 2017 13:01:43 -0700 Subject: [PATCH] test: move consensus tests out of utils tests. --- test/block-test.js | 17 ----------- test/consensus-test.js | 67 ++++++++++++++++++++++++++++++++++++++++++ test/utils-test.js | 12 -------- 3 files changed, 67 insertions(+), 29 deletions(-) create mode 100644 test/consensus-test.js diff --git a/test/block-test.js b/test/block-test.js index 8a2258bf..349514be 100644 --- a/test/block-test.js +++ b/test/block-test.js @@ -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()); diff --git a/test/consensus-test.js b/test/consensus-test.js new file mode 100644 index 00000000..9cd15b37 --- /dev/null +++ b/test/consensus-test.js @@ -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)); + }); +}); diff --git a/test/utils-test.js b/test/utils-test.js index 9bd2b60b..ac5223c7 100644 --- a/test/utils-test.js +++ b/test/utils-test.js @@ -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');