block: add Block.prototype.verifyPow
This commit is contained in:
parent
15227d39bf
commit
26ccb43047
2 changed files with 25 additions and 0 deletions
11
src/block.js
11
src/block.js
|
@ -1,3 +1,4 @@
|
||||||
|
var BigInteger = require('bigi')
|
||||||
var bufferutils = require('./bufferutils')
|
var bufferutils = require('./bufferutils')
|
||||||
var bcrypto = require('./crypto')
|
var bcrypto = require('./crypto')
|
||||||
|
|
||||||
|
@ -115,4 +116,14 @@ Block.prototype.toHex = function (headersOnly) {
|
||||||
return this.toBuffer(headersOnly).toString('hex')
|
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
|
module.exports = Block
|
||||||
|
|
|
@ -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…
Add table
Add a link
Reference in a new issue