Skip to content

Commit 0716382

Browse files
committed
test: remove header hash caching in CBlockHeader class
Rather than block hashes (represented by the fields `.sha256` and `.hash`) being stateful, simply compute them on-the-fly. This ensures that the correct values are always returned and takes the burden of rehashing from test writers, making the code shorter overall. In a first step, the fields are kept at the same name with @Property functions as drop-in replacements, for a minimal diff. In later commits, the names are changed to be more descriptive and indicating the return type of the block hash.
1 parent 0f044e8 commit 0716382

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

test/functional/test_framework/messages.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -704,8 +704,8 @@ def __repr__(self):
704704

705705

706706
class CBlockHeader:
707-
__slots__ = ("hash", "hashMerkleRoot", "hashPrevBlock", "nBits", "nNonce",
708-
"nTime", "nVersion", "sha256")
707+
__slots__ = ("hashMerkleRoot", "hashPrevBlock", "nBits", "nNonce",
708+
"nTime", "nVersion")
709709

710710
def __init__(self, header=None):
711711
if header is None:
@@ -717,8 +717,6 @@ def __init__(self, header=None):
717717
self.nTime = header.nTime
718718
self.nBits = header.nBits
719719
self.nNonce = header.nNonce
720-
self.sha256 = header.sha256
721-
self.hash = header.hash
722720
self.calc_sha256()
723721

724722
def set_null(self):
@@ -728,8 +726,6 @@ def set_null(self):
728726
self.nTime = 0
729727
self.nBits = 0
730728
self.nNonce = 0
731-
self.sha256 = None
732-
self.hash = None
733729

734730
def deserialize(self, f):
735731
self.nVersion = int.from_bytes(f.read(4), "little", signed=True)
@@ -738,8 +734,6 @@ def deserialize(self, f):
738734
self.nTime = int.from_bytes(f.read(4), "little")
739735
self.nBits = int.from_bytes(f.read(4), "little")
740736
self.nNonce = int.from_bytes(f.read(4), "little")
741-
self.sha256 = None
742-
self.hash = None
743737

744738
def serialize(self):
745739
return self._serialize_header()
@@ -754,15 +748,22 @@ def _serialize_header(self):
754748
r += self.nNonce.to_bytes(4, "little")
755749
return r
756750

751+
@property
752+
def hash(self):
753+
"""Return block header hash as hex string."""
754+
return hash256(self._serialize_header())[::-1].hex()
755+
756+
@property
757+
def sha256(self):
758+
"""Return block header hash as integer."""
759+
return uint256_from_str(hash256(self._serialize_header()))
760+
761+
# TODO: get rid of this method, remove call-sites
757762
def calc_sha256(self):
758-
if self.sha256 is None:
759-
r = self._serialize_header()
760-
self.sha256 = uint256_from_str(hash256(r))
761-
self.hash = hash256(r)[::-1].hex()
763+
pass
762764

765+
# TODO: get rid of this method, replace call-sites by .sha256 access (if return value is used)
763766
def rehash(self):
764-
self.sha256 = None
765-
self.calc_sha256()
766767
return self.sha256
767768

768769
def __repr__(self):

0 commit comments

Comments
 (0)