Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Commit

Permalink
Represent block prevHash and merkleRoot in expected order
Browse files Browse the repository at this point in the history
  • Loading branch information
Braydon Fuller committed Sep 12, 2016
1 parent 969aec2 commit f09b274
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/block/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ Block.prototype.getMerkleRoot = function getMerkleRoot() {
*/
Block.prototype.validMerkleRoot = function validMerkleRoot() {

var h = new BN(this.header.merkleRoot.toString('hex'), 'hex');
var h = new BN(BufferUtil.reverse(this.header.merkleRoot).toString('hex'), 'hex');
var c = new BN(this.getMerkleRoot().toString('hex'), 'hex');

if (h.cmp(c) !== 0) {
Expand Down
17 changes: 8 additions & 9 deletions lib/block/blockheader.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var BufferUtil = require('../util/buffer');
var BufferReader = require('../encoding/bufferreader');
var BufferWriter = require('../encoding/bufferwriter');
var Hash = require('../crypto/hash');
var JSUtil = require('../util/js');
var $ = require('../util/preconditions');

var GENESIS_BITS = 0x1d00ffff;
Expand Down Expand Up @@ -70,10 +69,10 @@ BlockHeader._fromObject = function _fromObject(data) {
var prevHash = data.prevHash;
var merkleRoot = data.merkleRoot;
if (_.isString(data.prevHash)) {
prevHash = BufferUtil.reverse(new Buffer(data.prevHash, 'hex'));
prevHash = new Buffer(data.prevHash, 'hex');
}
if (_.isString(data.merkleRoot)) {
merkleRoot = BufferUtil.reverse(new Buffer(data.merkleRoot, 'hex'));
merkleRoot = new Buffer(data.merkleRoot, 'hex');
}
var info = {
hash: data.hash,
Expand Down Expand Up @@ -137,8 +136,8 @@ BlockHeader.fromString = function fromString(str) {
BlockHeader._fromBufferReader = function _fromBufferReader(br) {
var info = {};
info.version = br.readUInt32LE();
info.prevHash = br.read(32);
info.merkleRoot = br.read(32);
info.prevHash = BufferUtil.reverse(br.read(32));
info.merkleRoot = BufferUtil.reverse(br.read(32));
info.time = br.readUInt32LE();
info.bits = br.readUInt32LE();
info.nonce = br.readUInt32LE();
Expand All @@ -161,8 +160,8 @@ BlockHeader.prototype.toObject = BlockHeader.prototype.toJSON = function toObjec
return {
hash: this.hash,
version: this.version,
prevHash: BufferUtil.reverse(this.prevHash).toString('hex'),
merkleRoot: BufferUtil.reverse(this.merkleRoot).toString('hex'),
prevHash: this.prevHash.toString('hex'),
merkleRoot: this.merkleRoot.toString('hex'),
time: this.time,
bits: this.bits,
nonce: this.nonce
Expand Down Expand Up @@ -192,8 +191,8 @@ BlockHeader.prototype.toBufferWriter = function toBufferWriter(bw) {
bw = new BufferWriter();
}
bw.writeUInt32LE(this.version);
bw.write(this.prevHash);
bw.write(this.merkleRoot);
bw.write(BufferUtil.reverse(this.prevHash));
bw.write(BufferUtil.reverse(this.merkleRoot));
bw.writeUInt32LE(this.time);
bw.writeUInt32LE(this.bits);
bw.writeUInt32LE(this.nonce);
Expand Down
2 changes: 1 addition & 1 deletion lib/block/merkleblock.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ MerkleBlock.prototype.validMerkleTree = function validMerkleTree() {
if(opts.hashesUsed !== this.hashes.length) {
return false;
}
return BufferUtil.equals(root, this.header.merkleRoot);
return BufferUtil.equals(root, BufferUtil.reverse(this.header.merkleRoot));
};

/**
Expand Down
26 changes: 25 additions & 1 deletion test/block/blockheader.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ describe('BlockHeader', function() {
should.exist(bh.time);
should.exist(bh.bits);
should.exist(bh.nonce);
var expectedPrevHash = '000000003c35b5e70b13d5b938fef4e998a977c17bea978390273b7c50a9aa4b';
bh.prevHash.toString('hex').should.equal(expectedPrevHash);
var expectedMerkleRoot = '58e6d52d1eb00470ae1ab4d5a3375c0f51382c6f249fff84e9888286974cfc97';
bh.merkleRoot.toString('hex').should.equal(expectedMerkleRoot);
bh.timestamp.should.equal(1371410638);
bh.bits.should.equal(473956288);
bh.version.should.equal(2);
});

});
Expand All @@ -110,6 +117,15 @@ describe('BlockHeader', function() {
should.exist(json.time);
should.exist(json.bits);
should.exist(json.nonce);
json.should.deep.equal({
hash: '000000000b99b16390660d79fcc138d2ad0c89a0d044c4201a02bdf1f61ffa11',
version: 2,
prevHash: '000000003c35b5e70b13d5b938fef4e998a977c17bea978390273b7c50a9aa4b',
merkleRoot: '58e6d52d1eb00470ae1ab4d5a3375c0f51382c6f249fff84e9888286974cfc97',
time: 1371410638,
bits: 473956288,
nonce: 3594009557
});
});

});
Expand Down Expand Up @@ -150,7 +166,15 @@ describe('BlockHeader', function() {
describe('#fromBuffer', function() {

it('should parse this known buffer', function() {
BlockHeader.fromBuffer(bhbuf).toBuffer().toString('hex').should.equal(bhhex);
var blockHeader = BlockHeader.fromBuffer(bhbuf);
var expectedPrevHash = '000000003c35b5e70b13d5b938fef4e998a977c17bea978390273b7c50a9aa4b';
blockHeader.prevHash.toString('hex').should.equal(expectedPrevHash);
var expectedMerkleRoot = '58e6d52d1eb00470ae1ab4d5a3375c0f51382c6f249fff84e9888286974cfc97';
blockHeader.merkleRoot.toString('hex').should.equal(expectedMerkleRoot);
blockHeader.timestamp.should.equal(1371410638);
blockHeader.bits.should.equal(473956288);
blockHeader.version.should.equal(2);
blockHeader.toBuffer().toString('hex').should.equal(bhhex);
});

});
Expand Down
Loading

0 comments on commit f09b274

Please sign in to comment.