Skip to content

Commit b5e5de6

Browse files
committed
src/primitives/block.cpp: endian compatibility in GetHash
adapted from bitcoin/bitcoin@81aeb28
1 parent b6b4b3e commit b5e5de6

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/primitives/block.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,46 @@
1515

1616
uint256 CBlockHeader::GetHash() const
1717
{
18+
#if defined(WORDS_BIGENDIAN)
19+
if (nVersion < 4) {
20+
uint8_t data[80];
21+
WriteLE32(&data[0], nVersion);
22+
memcpy(&data[4], hashPrevBlock.begin(), hashPrevBlock.size());
23+
memcpy(&data[36], hashMerkleRoot.begin(), hashMerkleRoot.size());
24+
WriteLE32(&data[68], nTime);
25+
WriteLE32(&data[72], nBits);
26+
WriteLE32(&data[76], nNonce);
27+
return HashQuark(data, data + 80);
28+
} else if (nVersion < 7) {
29+
uint8_t data[112];
30+
WriteLE32(&data[0], nVersion);
31+
memcpy(&data[4], hashPrevBlock.begin(), hashPrevBlock.size());
32+
memcpy(&data[36], hashMerkleRoot.begin(), hashMerkleRoot.size());
33+
WriteLE32(&data[68], nTime);
34+
WriteLE32(&data[72], nBits);
35+
WriteLE32(&data[76], nNonce);
36+
memcpy(&data[80], hashMerkleRoot.begin(), hashMerkleRoot.size());
37+
return Hash(data, data + 80);
38+
} else {
39+
uint8_t data[80];
40+
WriteLE32(&data[0], nVersion);
41+
memcpy(&data[4], hashPrevBlock.begin(), hashPrevBlock.size());
42+
memcpy(&data[36], hashMerkleRoot.begin(), hashMerkleRoot.size());
43+
WriteLE32(&data[68], nTime);
44+
WriteLE32(&data[72], nBits);
45+
WriteLE32(&data[76], nNonce);
46+
return Hash(data, data + 80);
47+
}
48+
49+
#else // Can take shortcut for little endian
1850
if (nVersion < 4)
1951
return HashQuark(BEGIN(nVersion), END(nNonce));
2052

2153
if (nVersion < 7)
2254
return Hash(BEGIN(nVersion), END(nAccumulatorCheckpoint));
2355

2456
return Hash(BEGIN(nVersion), END(nNonce));
57+
#endif
2558
}
2659

2760
std::string CBlock::ToString() const

0 commit comments

Comments
 (0)