add Block.calculateTarget, use Buffer comparison rather than bigi
This commit is contained in:
parent
26ccb43047
commit
86b51b2d62
4 changed files with 45 additions and 10 deletions
|
@ -62,6 +62,7 @@
|
|||
"bigi": "^1.4.0",
|
||||
"bip66": "^1.1.0",
|
||||
"bs58check": "^1.0.5",
|
||||
"buffer-compare": "^1.1.0",
|
||||
"buffer-equals": "^1.0.3",
|
||||
"buffer-reverse": "^1.0.0",
|
||||
"create-hash": "^1.1.0",
|
||||
|
|
23
src/block.js
23
src/block.js
|
@ -1,6 +1,6 @@
|
|||
var BigInteger = require('bigi')
|
||||
var bufferutils = require('./bufferutils')
|
||||
var bcrypto = require('./crypto')
|
||||
var compare = require('buffer-compare')
|
||||
|
||||
var Transaction = require('./transaction')
|
||||
|
||||
|
@ -116,14 +116,21 @@ Block.prototype.toHex = function (headersOnly) {
|
|||
return this.toBuffer(headersOnly).toString('hex')
|
||||
}
|
||||
|
||||
Block.prototype.verifyPow = function () {
|
||||
var hash = BigInteger.fromBuffer([].reverse.call(this.getHash()))
|
||||
var mov = ((this.bits >>> 24) - 3) << 3
|
||||
var target = new BigInteger()
|
||||
target.fromInt(this.bits & 0x000ffffff)
|
||||
target = target.shiftLeft(mov)
|
||||
var ZEROS = '0000000000000000000000000000000000000000000000000000000000000000'
|
||||
|
||||
return hash.compareTo(target) <= 0
|
||||
Block.calculateTarget = function (bits) {
|
||||
var exponent = ((bits & 0xff000000) >> 24) - 3
|
||||
var mantissa = bits & 0x00ffffff
|
||||
var target = mantissa * Math.pow(2, 8 * exponent)
|
||||
var targetHex = Math.floor(target).toString(16)
|
||||
|
||||
return new Buffer(ZEROS.slice(0, 64 - targetHex.length) + targetHex, 'hex')
|
||||
}
|
||||
|
||||
Block.prototype.verifyPow = function () {
|
||||
var hash = [].reverse.call(this.getHash())
|
||||
|
||||
return compare(hash, Block.calculateTarget(this.bits)) <= 0
|
||||
}
|
||||
|
||||
module.exports = Block
|
||||
|
|
|
@ -1,12 +1,21 @@
|
|||
/* global describe, it, beforeEach */
|
||||
|
||||
var assert = require('assert')
|
||||
|
||||
var Block = require('../src/block')
|
||||
|
||||
var fixtures = require('./fixtures/block')
|
||||
|
||||
describe('Block', function () {
|
||||
describe('calculateTarget', function () {
|
||||
fixtures.targets.forEach(function (f) {
|
||||
it('returns ' + f.expected + ' for 0x' + f.bits, function () {
|
||||
var bits = parseInt(f.bits, 16)
|
||||
|
||||
assert.equal(Block.calculateTarget(bits).toString('hex'), f.expected)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('fromBuffer/fromHex', function () {
|
||||
fixtures.valid.forEach(function (f) {
|
||||
it('imports the block: ' + f.description + ' correctly', function () {
|
||||
|
|
20
test/fixtures/block.json
vendored
20
test/fixtures/block.json
vendored
|
@ -1,4 +1,22 @@
|
|||
|
||||
{
|
||||
"targets": [
|
||||
{
|
||||
"bits": "1d00ffff",
|
||||
"expected": "00000000ffff0000000000000000000000000000000000000000000000000000"
|
||||
},
|
||||
{
|
||||
"bits": "1b80ffff",
|
||||
"expected": "000000000080ffff000000000000000000000000000000000000000000000000"
|
||||
},
|
||||
{
|
||||
"bits": "1b0404cb",
|
||||
"expected": "00000000000404cb000000000000000000000000000000000000000000000000"
|
||||
},
|
||||
{
|
||||
"bits": "1814dd04",
|
||||
"expected": "000000000000000014dd04000000000000000000000000000000000000000000"
|
||||
}
|
||||
],
|
||||
"valid": [
|
||||
{
|
||||
"description": "Headers only",
|
||||
|
|
Loading…
Reference in a new issue