From 9c6c30028933fe54ce35c7d7847a514c8b7cdd30 Mon Sep 17 00:00:00 2001 From: Ruben de Vries Date: Tue, 8 Apr 2014 10:26:30 +0200 Subject: [PATCH 1/2] fixed calcDifficulty by making sure the MAX_TARGET is also locally available. added tests for 2 difficulty calculations. --- test/test.util.js | 13 +++++++++++++ util/util.js | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/test/test.util.js b/test/test.util.js index b8c422d..edb1182 100644 --- a/test/test.util.js +++ b/test/test.util.js @@ -187,4 +187,17 @@ describe('util', function() { }); }); }); + describe('#calcDifficulty', function() { + var bitsgenesis = 486604799; + it('should work for ' + bitsgenesis, function() { + var difficulty = coinUtil.calcDifficulty(bitsgenesis); + difficulty.should.equal(1); + }); + var bitslater = 419476394; + it('should work for ' + bitslater, function() { + var difficulty = coinUtil.calcDifficulty(bitslater); + difficulty.should.equal(6119726089); + }); + }); + }); diff --git a/util/util.js b/util/util.js index eec49b2..f89dce8 100644 --- a/util/util.js +++ b/util/util.js @@ -510,4 +510,4 @@ exports.INT64_MAX = INT64_MAX; // makes 1 BTC exports.COIN = 100000000; -exports.MAX_TARGET = new Buffer('00000000FFFF0000000000000000000000000000000000000000000000000000', 'hex'); +var MAX_TARGET = exports.MAX_TARGET = new Buffer('00000000FFFF0000000000000000000000000000000000000000000000000000', 'hex'); From 3118ab1d0c1337a669ca248982b04b008dd24613 Mon Sep 17 00:00:00 2001 From: Ruben de Vries Date: Tue, 8 Apr 2014 16:08:16 +0200 Subject: [PATCH 2/2] updated the calcDifficulty test to make it clear with what we're testing --- test/test.util.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/test.util.js b/test/test.util.js index edb1182..22180ff 100644 --- a/test/test.util.js +++ b/test/test.util.js @@ -189,13 +189,13 @@ describe('util', function() { }); describe('#calcDifficulty', function() { var bitsgenesis = 486604799; - it('should work for ' + bitsgenesis, function() { + it('should work for the bits from the genesis block; ' + bitsgenesis, function() { var difficulty = coinUtil.calcDifficulty(bitsgenesis); difficulty.should.equal(1); }); - var bitslater = 419476394; - it('should work for ' + bitslater, function() { - var difficulty = coinUtil.calcDifficulty(bitslater); + var randomotherbits = 419476394; + it('should work for the bits in a randomly chosen block, eg [00000000000000001fef2bbc6da9b65e16f9187b7d88f15a308490bf2c9b8e1d] ' + randomotherbits, function() { + var difficulty = coinUtil.calcDifficulty(randomotherbits); difficulty.should.equal(6119726089); }); });