Consistency with "id", "hash", and "_getHash"
This commit is contained in:
parent
0c28bc1786
commit
1579eed9af
26
lib/block.js
26
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 '<Block ' + this.id().toString('hex') + '>';
|
||||
return '<Block ' + this.id + '>';
|
||||
};
|
||||
|
||||
Block.Values = {
|
||||
|
||||
@ -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 = {
|
||||
|
||||
@ -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);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user