diff --git a/lib/block.js b/lib/block.js index 2b2d2de..a9c807e 100644 --- a/lib/block.js +++ b/lib/block.js @@ -244,22 +244,32 @@ Block.prototype.validMerkleRoot = function validMerkleRoot() { /** * @returns {Buffer} - The little endian hash buffer of the header */ -Block.prototype.hash = function hash() { - return this.blockheader.hash(); +Block.prototype._getHash = function() { + return this.blockheader._getHash(); }; -/** - * @returns {Buffer} - The big endian hash buffer of the header - */ -Block.prototype.id = function id() { - return this.blockheader.id(); +var idProperty = { + configurable: false, + writeable: false, + /** + * @returns {string} - The big endian hash buffer of the header + */ + get: function() { + if (!this._id) { + this._id = this.blockheader.id; + } + return this._id; + }, + set: _.noop }; +Object.defineProperty(Block.prototype, 'id', idProperty); +Object.defineProperty(Block.prototype, 'hash', idProperty); /** * @returns {String} - A string formated for the console */ Block.prototype.inspect = function inspect() { - return ''; + return ''; }; Block.Values = { diff --git a/lib/blockheader.js b/lib/blockheader.js index 175e529..9531609 100644 --- a/lib/blockheader.js +++ b/lib/blockheader.js @@ -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 ''; + return ''; }; BlockHeader.Constants = { diff --git a/test/block.js b/test/block.js index 61d8cc3..a72f541 100644 --- a/test/block.js +++ b/test/block.js @@ -154,21 +154,24 @@ describe('Block', function() { }); - describe('#hash', function() { + describe('#_getHash', function() { it('should return the correct hash of the genesis block', function() { var block = Block.fromBuffer(genesisbuf); var blockhash = new Buffer(Array.apply([], new Buffer(genesisidhex, 'hex')).reverse()); - block.hash().toString('hex').should.equal(blockhash.toString('hex')); + block._getHash().toString('hex').should.equal(blockhash.toString('hex')); }); - }); describe('#id', function() { it('should return the correct id of the genesis block', function() { var block = Block.fromBuffer(genesisbuf); - block.id().toString('hex').should.equal(genesisidhex); + block.id.should.equal(genesisidhex); + }); + it('"hash" should be the same as "id"', function() { + var block = Block.fromBuffer(genesisbuf); + block.id.should.equal(block.hash); }); }); diff --git a/test/blockheader.js b/test/blockheader.js index b07a6d2..dc0127c 100644 --- a/test/blockheader.js +++ b/test/blockheader.js @@ -199,15 +199,15 @@ describe('BlockHeader', function() { describe('#validProofOfWork', function() { - var x = BlockHeader.fromRawBlock(dataRawBlockBuffer); - it('should validate proof-of-work as true', function() { + var x = BlockHeader.fromRawBlock(dataRawBlockBuffer); var valid = x.validProofOfWork(x); valid.should.equal(true); }); it('should validate proof of work as false because incorrect proof of work', function() { + var x = BlockHeader.fromRawBlock(dataRawBlockBuffer); var nonce = x.nonce; x.nonce = 0; var valid = x.validProofOfWork(x);