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

Commit f09b274

Browse files
author
Braydon Fuller
committed
Represent block prevHash and merkleRoot in expected order
1 parent 969aec2 commit f09b274

File tree

5 files changed

+37
-14
lines changed

5 files changed

+37
-14
lines changed

lib/block/block.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ Block.prototype.getMerkleRoot = function getMerkleRoot() {
232232
*/
233233
Block.prototype.validMerkleRoot = function validMerkleRoot() {
234234

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

238238
if (h.cmp(c) !== 0) {

lib/block/blockheader.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ var BufferUtil = require('../util/buffer');
66
var BufferReader = require('../encoding/bufferreader');
77
var BufferWriter = require('../encoding/bufferwriter');
88
var Hash = require('../crypto/hash');
9-
var JSUtil = require('../util/js');
109
var $ = require('../util/preconditions');
1110

1211
var GENESIS_BITS = 0x1d00ffff;
@@ -70,10 +69,10 @@ BlockHeader._fromObject = function _fromObject(data) {
7069
var prevHash = data.prevHash;
7170
var merkleRoot = data.merkleRoot;
7271
if (_.isString(data.prevHash)) {
73-
prevHash = BufferUtil.reverse(new Buffer(data.prevHash, 'hex'));
72+
prevHash = new Buffer(data.prevHash, 'hex');
7473
}
7574
if (_.isString(data.merkleRoot)) {
76-
merkleRoot = BufferUtil.reverse(new Buffer(data.merkleRoot, 'hex'));
75+
merkleRoot = new Buffer(data.merkleRoot, 'hex');
7776
}
7877
var info = {
7978
hash: data.hash,
@@ -137,8 +136,8 @@ BlockHeader.fromString = function fromString(str) {
137136
BlockHeader._fromBufferReader = function _fromBufferReader(br) {
138137
var info = {};
139138
info.version = br.readUInt32LE();
140-
info.prevHash = br.read(32);
141-
info.merkleRoot = br.read(32);
139+
info.prevHash = BufferUtil.reverse(br.read(32));
140+
info.merkleRoot = BufferUtil.reverse(br.read(32));
142141
info.time = br.readUInt32LE();
143142
info.bits = br.readUInt32LE();
144143
info.nonce = br.readUInt32LE();
@@ -161,8 +160,8 @@ BlockHeader.prototype.toObject = BlockHeader.prototype.toJSON = function toObjec
161160
return {
162161
hash: this.hash,
163162
version: this.version,
164-
prevHash: BufferUtil.reverse(this.prevHash).toString('hex'),
165-
merkleRoot: BufferUtil.reverse(this.merkleRoot).toString('hex'),
163+
prevHash: this.prevHash.toString('hex'),
164+
merkleRoot: this.merkleRoot.toString('hex'),
166165
time: this.time,
167166
bits: this.bits,
168167
nonce: this.nonce
@@ -192,8 +191,8 @@ BlockHeader.prototype.toBufferWriter = function toBufferWriter(bw) {
192191
bw = new BufferWriter();
193192
}
194193
bw.writeUInt32LE(this.version);
195-
bw.write(this.prevHash);
196-
bw.write(this.merkleRoot);
194+
bw.write(BufferUtil.reverse(this.prevHash));
195+
bw.write(BufferUtil.reverse(this.merkleRoot));
197196
bw.writeUInt32LE(this.time);
198197
bw.writeUInt32LE(this.bits);
199198
bw.writeUInt32LE(this.nonce);

lib/block/merkleblock.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ MerkleBlock.prototype.validMerkleTree = function validMerkleTree() {
146146
if(opts.hashesUsed !== this.hashes.length) {
147147
return false;
148148
}
149-
return BufferUtil.equals(root, this.header.merkleRoot);
149+
return BufferUtil.equals(root, BufferUtil.reverse(this.header.merkleRoot));
150150
};
151151

152152
/**

test/block/blockheader.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ describe('BlockHeader', function() {
9696
should.exist(bh.time);
9797
should.exist(bh.bits);
9898
should.exist(bh.nonce);
99+
var expectedPrevHash = '000000003c35b5e70b13d5b938fef4e998a977c17bea978390273b7c50a9aa4b';
100+
bh.prevHash.toString('hex').should.equal(expectedPrevHash);
101+
var expectedMerkleRoot = '58e6d52d1eb00470ae1ab4d5a3375c0f51382c6f249fff84e9888286974cfc97';
102+
bh.merkleRoot.toString('hex').should.equal(expectedMerkleRoot);
103+
bh.timestamp.should.equal(1371410638);
104+
bh.bits.should.equal(473956288);
105+
bh.version.should.equal(2);
99106
});
100107

101108
});
@@ -110,6 +117,15 @@ describe('BlockHeader', function() {
110117
should.exist(json.time);
111118
should.exist(json.bits);
112119
should.exist(json.nonce);
120+
json.should.deep.equal({
121+
hash: '000000000b99b16390660d79fcc138d2ad0c89a0d044c4201a02bdf1f61ffa11',
122+
version: 2,
123+
prevHash: '000000003c35b5e70b13d5b938fef4e998a977c17bea978390273b7c50a9aa4b',
124+
merkleRoot: '58e6d52d1eb00470ae1ab4d5a3375c0f51382c6f249fff84e9888286974cfc97',
125+
time: 1371410638,
126+
bits: 473956288,
127+
nonce: 3594009557
128+
});
113129
});
114130

115131
});
@@ -150,7 +166,15 @@ describe('BlockHeader', function() {
150166
describe('#fromBuffer', function() {
151167

152168
it('should parse this known buffer', function() {
153-
BlockHeader.fromBuffer(bhbuf).toBuffer().toString('hex').should.equal(bhhex);
169+
var blockHeader = BlockHeader.fromBuffer(bhbuf);
170+
var expectedPrevHash = '000000003c35b5e70b13d5b938fef4e998a977c17bea978390273b7c50a9aa4b';
171+
blockHeader.prevHash.toString('hex').should.equal(expectedPrevHash);
172+
var expectedMerkleRoot = '58e6d52d1eb00470ae1ab4d5a3375c0f51382c6f249fff84e9888286974cfc97';
173+
blockHeader.merkleRoot.toString('hex').should.equal(expectedMerkleRoot);
174+
blockHeader.timestamp.should.equal(1371410638);
175+
blockHeader.bits.should.equal(473956288);
176+
blockHeader.version.should.equal(2);
177+
blockHeader.toBuffer().toString('hex').should.equal(bhhex);
154178
});
155179

156180
});

0 commit comments

Comments
 (0)