From 29a4f5475258710f3eac49ce8f4713ba0d9052b2 Mon Sep 17 00:00:00 2001 From: Johnathan James Date: Fri, 13 Jan 2023 11:30:39 -0700 Subject: [PATCH 1/4] Added unit tests for consensus.fromCompact function --- test/consensus-test.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/consensus-test.js b/test/consensus-test.js index 03503d442..9fc2995fc 100644 --- a/test/consensus-test.js +++ b/test/consensus-test.js @@ -64,4 +64,28 @@ 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()); + }); + }); From 581380dc631bbf3afca5ec60dc4b997a5f86b4e4 Mon Sep 17 00:00:00 2001 From: Johnathan James Date: Fri, 13 Jan 2023 11:43:45 -0700 Subject: [PATCH 2/4] Added unit tests for consensus.toCompact function --- test/consensus-test.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/consensus-test.js b/test/consensus-test.js index 9fc2995fc..a02307e3e 100644 --- a/test/consensus-test.js +++ b/test/consensus-test.js @@ -88,4 +88,28 @@ describe('Consensus', function() { 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); + }); + }); From 186f0415460838895c738dd0edf7cc79d323da45 Mon Sep 17 00:00:00 2001 From: Johnathan James Date: Fri, 13 Jan 2023 12:18:07 -0700 Subject: [PATCH 3/4] Added unit tests for consensus.verifyPOW() --- test/consensus-test.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/test/consensus-test.js b/test/consensus-test.js index a02307e3e..e4411a15f 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); @@ -112,4 +145,6 @@ describe('Consensus', function() { assert.strictEqual(bits, expected); }); + it('/ver') + }); From d4d8637596bdd61b5d65bb8b8f3625bbad83e8e1 Mon Sep 17 00:00:00 2001 From: Johnathan James Date: Wed, 25 Jan 2023 20:57:52 -0700 Subject: [PATCH 4/4] Minor, typo. --- test/consensus-test.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/test/consensus-test.js b/test/consensus-test.js index e4411a15f..678733942 100644 --- a/test/consensus-test.js +++ b/test/consensus-test.js @@ -144,7 +144,4 @@ describe('Consensus', function() { assert.strictEqual(bits, expected); }); - - it('/ver') - });