Browse Source

block: add Block.prototype.verifyPow

hk-custom-address
Daniel Cousens 9 years ago
parent
commit
26ccb43047
  1. 11
      src/block.js
  2. 14
      test/block.js

11
src/block.js

@ -1,3 +1,4 @@
var BigInteger = require('bigi')
var bufferutils = require('./bufferutils')
var bcrypto = require('./crypto')
@ -115,4 +116,14 @@ 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)
return hash.compareTo(target) <= 0
}
module.exports = Block

14
test/block.js

@ -87,4 +87,18 @@ describe('Block', function () {
})
})
})
describe('verifyPow', function () {
fixtures.valid.forEach(function (f) {
var block
beforeEach(function () {
block = Block.fromHex(f.hex)
})
it('returns ' + f.valid + ' for ' + f.id, function () {
assert.strictEqual(block.verifyPow(), f.valid)
})
})
})
})

Loading…
Cancel
Save