block: use buffer, faster, verified
This commit is contained in:
parent
86b51b2d62
commit
bac700f52c
2 changed files with 16 additions and 7 deletions
17
src/block.js
17
src/block.js
|
@ -116,15 +116,20 @@ Block.prototype.toHex = function (headersOnly) {
|
||||||
return this.toBuffer(headersOnly).toString('hex')
|
return this.toBuffer(headersOnly).toString('hex')
|
||||||
}
|
}
|
||||||
|
|
||||||
var ZEROS = '0000000000000000000000000000000000000000000000000000000000000000'
|
|
||||||
|
|
||||||
Block.calculateTarget = function (bits) {
|
Block.calculateTarget = function (bits) {
|
||||||
var exponent = ((bits & 0xff000000) >> 24) - 3
|
var exponent = ((bits & 0xff000000) >> 24) - 3
|
||||||
var mantissa = bits & 0x00ffffff
|
var mantissa = bits & 0x007fffff
|
||||||
var target = mantissa * Math.pow(2, 8 * exponent)
|
var i = 31 - exponent
|
||||||
var targetHex = Math.floor(target).toString(16)
|
|
||||||
|
|
||||||
return new Buffer(ZEROS.slice(0, 64 - targetHex.length) + targetHex, 'hex')
|
var target = new Buffer(32)
|
||||||
|
target.fill(0)
|
||||||
|
|
||||||
|
target[i] = mantissa & 0xff
|
||||||
|
target[i - 1] = mantissa >> 8
|
||||||
|
target[i - 2] = mantissa >> 16
|
||||||
|
target[i - 3] = mantissa >> 24
|
||||||
|
|
||||||
|
return target
|
||||||
}
|
}
|
||||||
|
|
||||||
Block.prototype.verifyPow = function () {
|
Block.prototype.verifyPow = function () {
|
||||||
|
|
6
test/fixtures/block.json
vendored
6
test/fixtures/block.json
vendored
|
@ -6,7 +6,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"bits": "1b80ffff",
|
"bits": "1b80ffff",
|
||||||
"expected": "000000000080ffff000000000000000000000000000000000000000000000000"
|
"expected": "000000000000ffff000000000000000000000000000000000000000000000000"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"bits": "1b0404cb",
|
"bits": "1b0404cb",
|
||||||
|
@ -15,6 +15,10 @@
|
||||||
{
|
{
|
||||||
"bits": "1814dd04",
|
"bits": "1814dd04",
|
||||||
"expected": "000000000000000014dd04000000000000000000000000000000000000000000"
|
"expected": "000000000000000014dd04000000000000000000000000000000000000000000"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"bits": "cffca00",
|
||||||
|
"expected": "00000000000000000000000000000000000000007fca00000000000000000000"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"valid": [
|
"valid": [
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue