Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added unit tests for consensus.js #1125

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions test/consensus-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,39 @@ describe('Consensus', function() {
assert(consensus.verifyPOW(hash, bits));
});

it('should not verify proof-of-work if bits is negative', () => {
const bits = 0x1d00ffff * -1;

const hash = Buffer.from(
'672b3f1bb11a994267ea4171069ba0aa4448a840f38e8f340000000000000000',
'hex'
);

assert(!consensus.verifyPOW(hash, bits));
});

it('should not verify proof-of-work if bits is zero', () => {
const bits = 0;

const hash = Buffer.from(
'672b3f1bb11a994267ea4171069ba0aa4448a840f38e8f340000000000000000',
'hex'
);

assert(!consensus.verifyPOW(hash, bits));
});

it('should not verify proof-of-work if bits is too high', () => {
const bits = 0x1d00ffff * 2;

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);
Expand Down Expand Up @@ -64,4 +97,51 @@ describe('Consensus', function() {
assert(consensus.hasBit(0x20000003, 1));
assert(consensus.hasBit(0x20000003, 0));
});

it('should return BN(0) from fromCompact when called with a zero', () => {
const target = consensus.fromCompact(0);
const bnZero = new BN(0);

// assert that the return object is correct by type and value
assert.strictEqual(target.constructor.name, bnZero.constructor.name);
assert.strictEqual(target.toString(), bnZero.toString());
});

it('should return BN(0) from fromCompact when the passed in value right shifted 24 is less than three', () => {
const target = consensus.fromCompact(0x01000000);
const expected = new BN(0x00000000);

assert.strictEqual(target.toString(), expected.toString());
});

it('should return the correct value from fromCompact when called with 0x0fffffff', () => {
const target = consensus.fromCompact(0x0fffffff);
const expected = new BN(-664613918664295422187565936596221952n);

assert.strictEqual(target.toString(), expected.toString());
});

it('should return 0 from toCompact when called with BN(0)', () => {
const target = new BN(0);
const bits = consensus.toCompact(target);
const expected = 0;

assert.strictEqual(bits, expected);
});

it('should return 16842752 when the byteLength of the passed in value is less than three', () => {
const target = new BN(0x00000001);
const bits = consensus.toCompact(target);
const expected = 16842752;

assert.strictEqual(bits, expected);
});

it('should return correct value from toCompact when the param is negative and its byteLength is less than three', () => {
const target = new BN(-10000n);
const bits = consensus.toCompact(target);
const expected = 4294957296;

assert.strictEqual(bits, expected);
});
});