|
|
@ -9,6 +9,8 @@ var Hash = require('../crypto/hash'); |
|
|
|
var JSUtil = require('../util/js'); |
|
|
|
var $ = require('../util/preconditions'); |
|
|
|
|
|
|
|
var GENESIS_BITS = 0x1d00ffff; |
|
|
|
|
|
|
|
/** |
|
|
|
* Instantiate a BlockHeader from a Buffer, JSON object, or Object with |
|
|
|
* the properties of the BlockHeader |
|
|
@ -199,18 +201,36 @@ BlockHeader.prototype.toBufferWriter = function toBufferWriter(bw) { |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* @link https://en.bitcoin.it/wiki/Difficulty
|
|
|
|
* @returns {BN} - An instance of BN with the decoded difficulty bits |
|
|
|
* Returns the target difficulty for this block |
|
|
|
* @param {Number} bits |
|
|
|
* @returns {BN} An instance of BN with the decoded difficulty bits |
|
|
|
*/ |
|
|
|
BlockHeader.prototype.getTargetDifficulty = function getTargetDifficulty(info) { |
|
|
|
var target = new BN(this.bits & 0xffffff); |
|
|
|
var mov = 8 * ((this.bits >>> 24) - 3); |
|
|
|
BlockHeader.prototype.getTargetDifficulty = function getTargetDifficulty(bits) { |
|
|
|
bits = bits || this.bits; |
|
|
|
|
|
|
|
var target = new BN(bits & 0xffffff); |
|
|
|
var mov = 8 * ((bits >>> 24) - 3); |
|
|
|
while (mov-- > 0) { |
|
|
|
target = target.mul(new BN(2)); |
|
|
|
} |
|
|
|
return target; |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* @link https://en.bitcoin.it/wiki/Difficulty
|
|
|
|
* @return {Number} |
|
|
|
*/ |
|
|
|
BlockHeader.prototype.getDifficulty = function getDifficulty() { |
|
|
|
var difficulty1TargetBN = this.getTargetDifficulty(GENESIS_BITS).mul(new BN(Math.pow(10, 8))); |
|
|
|
var currentTargetBN = this.getTargetDifficulty(); |
|
|
|
|
|
|
|
var difficultyString = difficulty1TargetBN.div(currentTargetBN).toString(10); |
|
|
|
var decimalPos = difficultyString.length - 8; |
|
|
|
difficultyString = difficultyString.slice(0, decimalPos) + '.' + difficultyString.slice(decimalPos); |
|
|
|
|
|
|
|
return parseFloat(difficultyString); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* @returns {Buffer} - The little endian hash buffer of the header |
|
|
|
*/ |
|
|
|