add Block.calculateTarget, use Buffer comparison rather than bigi

This commit is contained in:
Daniel Cousens 2015-12-09 13:35:29 +11:00
parent 26ccb43047
commit 86b51b2d62
4 changed files with 45 additions and 10 deletions

View file

@ -62,6 +62,7 @@
"bigi": "^1.4.0", "bigi": "^1.4.0",
"bip66": "^1.1.0", "bip66": "^1.1.0",
"bs58check": "^1.0.5", "bs58check": "^1.0.5",
"buffer-compare": "^1.1.0",
"buffer-equals": "^1.0.3", "buffer-equals": "^1.0.3",
"buffer-reverse": "^1.0.0", "buffer-reverse": "^1.0.0",
"create-hash": "^1.1.0", "create-hash": "^1.1.0",

View file

@ -1,6 +1,6 @@
var BigInteger = require('bigi')
var bufferutils = require('./bufferutils') var bufferutils = require('./bufferutils')
var bcrypto = require('./crypto') var bcrypto = require('./crypto')
var compare = require('buffer-compare')
var Transaction = require('./transaction') var Transaction = require('./transaction')
@ -116,14 +116,21 @@ Block.prototype.toHex = function (headersOnly) {
return this.toBuffer(headersOnly).toString('hex') return this.toBuffer(headersOnly).toString('hex')
} }
Block.prototype.verifyPow = function () { var ZEROS = '0000000000000000000000000000000000000000000000000000000000000000'
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)
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 module.exports = Block

View file

@ -1,12 +1,21 @@
/* global describe, it, beforeEach */ /* global describe, it, beforeEach */
var assert = require('assert') var assert = require('assert')
var Block = require('../src/block') var Block = require('../src/block')
var fixtures = require('./fixtures/block') var fixtures = require('./fixtures/block')
describe('Block', function () { 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 () { describe('fromBuffer/fromHex', function () {
fixtures.valid.forEach(function (f) { fixtures.valid.forEach(function (f) {
it('imports the block: ' + f.description + ' correctly', function () { it('imports the block: ' + f.description + ' correctly', function () {

View file

@ -1,4 +1,22 @@
{
"targets": [
{
"bits": "1d00ffff",
"expected": "00000000ffff0000000000000000000000000000000000000000000000000000"
},
{
"bits": "1b80ffff",
"expected": "000000000080ffff000000000000000000000000000000000000000000000000"
},
{
"bits": "1b0404cb",
"expected": "00000000000404cb000000000000000000000000000000000000000000000000"
},
{
"bits": "1814dd04",
"expected": "000000000000000014dd04000000000000000000000000000000000000000000"
}
],
"valid": [ "valid": [
{ {
"description": "Headers only", "description": "Headers only",