Consistency with "id", "hash", and "_getHash"

This commit is contained in:
Esteban Ordano 2014-12-10 15:09:37 -03:00
parent 0c28bc1786
commit 1579eed9af
4 changed files with 46 additions and 23 deletions

View File

@ -244,22 +244,32 @@ Block.prototype.validMerkleRoot = function validMerkleRoot() {
/** /**
* @returns {Buffer} - The little endian hash buffer of the header * @returns {Buffer} - The little endian hash buffer of the header
*/ */
Block.prototype.hash = function hash() { Block.prototype._getHash = function() {
return this.blockheader.hash(); return this.blockheader._getHash();
}; };
/** var idProperty = {
* @returns {Buffer} - The big endian hash buffer of the header configurable: false,
*/ writeable: false,
Block.prototype.id = function id() { /**
return this.blockheader.id(); * @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 * @returns {String} - A string formated for the console
*/ */
Block.prototype.inspect = function inspect() { Block.prototype.inspect = function inspect() {
return '<Block ' + this.id().toString('hex') + '>'; return '<Block ' + this.id + '>';
}; };
Block.Values = { Block.Values = {

View File

@ -183,17 +183,27 @@ BlockHeader.prototype.getTargetDifficulty = function getTargetDifficulty(info) {
/** /**
* @returns {Buffer} - The little endian hash buffer of the header * @returns {Buffer} - The little endian hash buffer of the header
*/ */
BlockHeader.prototype.hash = function hash() { BlockHeader.prototype._getHash = function hash() {
var buf = this.toBuffer(); var buf = this.toBuffer();
return Hash.sha256sha256(buf); return Hash.sha256sha256(buf);
}; };
/** var idProperty = {
* @returns {Buffer} - The big endian hash buffer of the header configurable: false,
*/ writeable: false,
BlockHeader.prototype.id = function id() { /**
return BufferReader(this.hash()).reverse().read(); * @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 * @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 * @returns {Boolean} - If the proof-of-work hash satisfies the target difficulty
*/ */
BlockHeader.prototype.validProofOfWork = function validProofOfWork() { BlockHeader.prototype.validProofOfWork = function validProofOfWork() {
var hash = this.id().toString('hex'); var pow = new BN(this.id, 'hex');
var pow = new BN(hash, 'hex');
var target = this.getTargetDifficulty(); var target = this.getTargetDifficulty();
if (pow.cmp(target) > 0) { if (pow.cmp(target) > 0) {
return false; return false;
} }
@ -223,7 +233,7 @@ BlockHeader.prototype.validProofOfWork = function validProofOfWork() {
* @returns {String} - A string formated for the console * @returns {String} - A string formated for the console
*/ */
BlockHeader.prototype.inspect = function inspect() { BlockHeader.prototype.inspect = function inspect() {
return '<BlockHeader ' + this.id().toString('hex') + '>'; return '<BlockHeader ' + this.id + '>';
}; };
BlockHeader.Constants = { BlockHeader.Constants = {

View File

@ -154,21 +154,24 @@ describe('Block', function() {
}); });
describe('#hash', function() { describe('#_getHash', function() {
it('should return the correct hash of the genesis block', function() { it('should return the correct hash of the genesis block', function() {
var block = Block.fromBuffer(genesisbuf); var block = Block.fromBuffer(genesisbuf);
var blockhash = new Buffer(Array.apply([], new Buffer(genesisidhex, 'hex')).reverse()); 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() { describe('#id', function() {
it('should return the correct id of the genesis block', function() { it('should return the correct id of the genesis block', function() {
var block = Block.fromBuffer(genesisbuf); 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);
}); });
}); });

View File

@ -199,15 +199,15 @@ describe('BlockHeader', function() {
describe('#validProofOfWork', function() { describe('#validProofOfWork', function() {
var x = BlockHeader.fromRawBlock(dataRawBlockBuffer);
it('should validate proof-of-work as true', function() { it('should validate proof-of-work as true', function() {
var x = BlockHeader.fromRawBlock(dataRawBlockBuffer);
var valid = x.validProofOfWork(x); var valid = x.validProofOfWork(x);
valid.should.equal(true); valid.should.equal(true);
}); });
it('should validate proof of work as false because incorrect proof of work', function() { it('should validate proof of work as false because incorrect proof of work', function() {
var x = BlockHeader.fromRawBlock(dataRawBlockBuffer);
var nonce = x.nonce; var nonce = x.nonce;
x.nonce = 0; x.nonce = 0;
var valid = x.validProofOfWork(x); var valid = x.validProofOfWork(x);