From f3c791d2e39133f33b04ebf790c307b0f63bb578 Mon Sep 17 00:00:00 2001 From: Sebastian Falbesoner Date: Thu, 12 Jun 2025 12:21:12 +0200 Subject: [PATCH] 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. --- test/functional/test_framework/messages.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/test/functional/test_framework/messages.py b/test/functional/test_framework/messages.py index 774775c00e1..ef2e4eedaec 100755 --- a/test/functional/test_framework/messages.py +++ b/test/functional/test_framework/messages.py @@ -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()