|
|
@ -183,17 +183,27 @@ BlockHeader.prototype.getTargetDifficulty = function getTargetDifficulty(info) { |
|
|
|
/** |
|
|
|
* @returns {Buffer} - The little endian hash buffer of the header |
|
|
|
*/ |
|
|
|
BlockHeader.prototype.hash = function hash() { |
|
|
|
BlockHeader.prototype._getHash = function hash() { |
|
|
|
var buf = this.toBuffer(); |
|
|
|
return Hash.sha256sha256(buf); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* @returns {Buffer} - The big endian hash buffer of the header |
|
|
|
*/ |
|
|
|
BlockHeader.prototype.id = function id() { |
|
|
|
return BufferReader(this.hash()).reverse().read(); |
|
|
|
var idProperty = { |
|
|
|
configurable: false, |
|
|
|
writeable: false, |
|
|
|
/** |
|
|
|
* @returns {string} - The big endian hash buffer of the header |
|
|
|
*/ |
|
|
|
get: function() { |
|
|
|
if (!this._id) { |
|
|
|
this._id = BufferReader(this._getHash()).readReverse().toString('hex'); |
|
|
|
} |
|
|
|
return this._id; |
|
|
|
}, |
|
|
|
set: _.noop |
|
|
|
}; |
|
|
|
Object.defineProperty(BlockHeader.prototype, 'id', idProperty); |
|
|
|
Object.defineProperty(BlockHeader.prototype, 'hash', idProperty); |
|
|
|
|
|
|
|
/** |
|
|
|
* @returns {Boolean} - If timestamp is not too far in the future |
|
|
@ -210,9 +220,9 @@ BlockHeader.prototype.validTimestamp = function validTimestamp() { |
|
|
|
* @returns {Boolean} - If the proof-of-work hash satisfies the target difficulty |
|
|
|
*/ |
|
|
|
BlockHeader.prototype.validProofOfWork = function validProofOfWork() { |
|
|
|
var hash = this.id().toString('hex'); |
|
|
|
var pow = new BN(hash, 'hex'); |
|
|
|
var pow = new BN(this.id, 'hex'); |
|
|
|
var target = this.getTargetDifficulty(); |
|
|
|
|
|
|
|
if (pow.cmp(target) > 0) { |
|
|
|
return false; |
|
|
|
} |
|
|
@ -223,7 +233,7 @@ BlockHeader.prototype.validProofOfWork = function validProofOfWork() { |
|
|
|
* @returns {String} - A string formated for the console |
|
|
|
*/ |
|
|
|
BlockHeader.prototype.inspect = function inspect() { |
|
|
|
return '<BlockHeader ' + this.id().toString('hex') + '>'; |
|
|
|
return '<BlockHeader ' + this.id + '>'; |
|
|
|
}; |
|
|
|
|
|
|
|
BlockHeader.Constants = { |
|
|
|