Skip to content

Commit

Permalink
Replace CBlockHeader::GetHash with call to SerializeHash
Browse files Browse the repository at this point in the history
Removes variability between LE and BE.
As suggested by @sipa.

adapted from bitcoin/bitcoin@a0ae79d
  • Loading branch information
random-zebra committed Apr 24, 2020
1 parent 49a7716 commit 25224a8
Showing 1 changed file with 4 additions and 28 deletions.
32 changes: 4 additions & 28 deletions src/primitives/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

uint256 CBlockHeader::GetHash() const
{
#if defined(WORDS_BIGENDIAN)
if (nVersion < 4) {
#if defined(WORDS_BIGENDIAN)
uint8_t data[80];
WriteLE32(&data[0], nVersion);
memcpy(&data[4], hashPrevBlock.begin(), hashPrevBlock.size());
Expand All @@ -25,36 +25,12 @@ uint256 CBlockHeader::GetHash() const
WriteLE32(&data[72], nBits);
WriteLE32(&data[76], nNonce);
return HashQuark(data, data + 80);
} else if (nVersion < 7) {
uint8_t data[112];
WriteLE32(&data[0], nVersion);
memcpy(&data[4], hashPrevBlock.begin(), hashPrevBlock.size());
memcpy(&data[36], hashMerkleRoot.begin(), hashMerkleRoot.size());
WriteLE32(&data[68], nTime);
WriteLE32(&data[72], nBits);
WriteLE32(&data[76], nNonce);
memcpy(&data[80], hashMerkleRoot.begin(), hashMerkleRoot.size());
return Hash(data, data + 80);
} else {
uint8_t data[80];
WriteLE32(&data[0], nVersion);
memcpy(&data[4], hashPrevBlock.begin(), hashPrevBlock.size());
memcpy(&data[36], hashMerkleRoot.begin(), hashMerkleRoot.size());
WriteLE32(&data[68], nTime);
WriteLE32(&data[72], nBits);
WriteLE32(&data[76], nNonce);
return Hash(data, data + 80);
}

#else // Can take shortcut for little endian
if (nVersion < 4)
return HashQuark(BEGIN(nVersion), END(nNonce));

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

return Hash(BEGIN(nVersion), END(nNonce));
#endif
}
// version >= 4
return SerializeHash(*this);
}

std::string CBlock::ToString() const
Expand Down

0 comments on commit 25224a8

Please sign in to comment.