Browse Source

Check for consitency with block header argument hash and calculated hash.

patch-2
Braydon Fuller 10 years ago
parent
commit
496edf9109
  1. 18
      lib/block/blockheader.js
  2. 14
      test/block/blockheader.js

18
lib/block/blockheader.js

@ -21,7 +21,22 @@ var BlockHeader = function BlockHeader(arg) {
if (!(this instanceof BlockHeader)) {
return new BlockHeader(arg);
}
_.extend(this, BlockHeader._from(arg));
var info = BlockHeader._from(arg);
this.version = info.version;
this.prevHash = info.prevHash;
this.merkleRoot = info.merkleRoot;
this.time = info.time;
this.timestamp = info.time;
this.bits = info.bits;
this.nonce = info.nonce;
if (info.hash) {
$.checkState(
this.hash === info.hash,
'Argument object hash property does not match block hash.'
);
}
return this;
};
@ -72,6 +87,7 @@ BlockHeader._fromObject = function _fromObject(data) {
merkleRoot = BufferUtil.reverse(new Buffer(data.merkleRoot, 'hex'));
}
var info = {
hash: data.hash,
version: data.version,
prevHash: prevHash,
merkleRoot: merkleRoot,

14
test/block/blockheader.js

@ -63,6 +63,20 @@ describe('BlockHeader', function() {
should.exist(bh.nonce);
});
it('will throw an error if the argument object hash property doesn\'t match', function() {
(function() {
var bh = new BlockHeader({
hash: '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f',
version: version,
prevHash: prevblockidbuf,
merkleRoot: merklerootbuf,
time: time,
bits: bits,
nonce: nonce
});
}).should.throw('Argument object hash property does not match block hash.');
});
});
describe('#fromJSON', function() {

Loading…
Cancel
Save