test: refactor: dedup CBlockHeader serialization

Note that we can't call `.serialize()` directly in
the `.calc_sha256()` method, as this could wrongly lead
to the serialization of the derived class (CBlock) if
called from an instance there.
This commit is contained in:
Sebastian Falbesoner committed 2025-07-17 11:59:08 +02:00
1 parent 9f713b83dc
commit f3c791d2e3
1 file changed
+4 -7
+4 -7
View File
@@ -742,6 +742,9 @@ class CBlockHeader:
self.hash = None
def serialize(self):
return self._serialize_header()
def _serialize_header(self):
r = b""
r += self.nVersion.to_bytes(4, "little", signed=True)
r += ser_uint256(self.hashPrevBlock)
@@ -753,13 +756,7 @@ class CBlockHeader:
def calc_sha256(self):
if self.sha256 is None:
r = b""
r += self.nVersion.to_bytes(4, "little", signed=True)
r += ser_uint256(self.hashPrevBlock)
r += ser_uint256(self.hashMerkleRoot)
r += self.nTime.to_bytes(4, "little")
r += self.nBits.to_bytes(4, "little")
r += self.nNonce.to_bytes(4, "little")
r = self._serialize_header()
self.sha256 = uint256_from_str(hash256(r))
self.hash = hash256(r)[::-1].hex()