diff --git a/test/consensus-test.js b/test/consensus-test.js index 03503d442..678733942 100644 --- a/test/consensus-test.js +++ b/test/consensus-test.js @@ -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); @@ -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); + }); });