@@ -21,6 +21,11 @@ func TestHeaderHash(t *testing.T) {
2121 }
2222
2323 hash1 := header .Hash ()
24+ assert .Nil (t , header .cachedHash , "Hash() should not memoize" )
25+
26+ memoizedHash := header .MemoizeHash ()
27+ assert .Equal (t , hash1 , memoizedHash )
28+ assert .NotNil (t , header .cachedHash , "MemoizeHash() should store the computed value" )
2429
2530 headerBytes , err := header .MarshalBinary ()
2631 require .NoError (t , err )
@@ -31,6 +36,8 @@ func TestHeaderHash(t *testing.T) {
3136 assert .Equal (t , Hash (expectedHash [:]), hash1 , "Header hash should match manual calculation" )
3237
3338 header .BaseHeader .Height = 2
39+ header .InvalidateHash ()
40+ assert .Nil (t , header .cachedHash )
3441 hash2 := header .Hash ()
3542 assert .NotEqual (t , hash1 , hash2 , "Different headers should have different hashes" )
3643}
@@ -143,3 +150,17 @@ func TestHeaderHashWithBytes(t *testing.T) {
143150 require .NoError (t , targetHeader .UnmarshalBinary (headerBytes ))
144151 assert .Equal (t , hash1 , targetHeader .Hash (), "HeaderHash should produce same result as Header.Hash()" )
145152}
153+
154+ func TestHeaderCloneDropsCachedHash (t * testing.T ) {
155+ header := & Header {
156+ BaseHeader : BaseHeader {Height : 1 , Time : 1234567890 },
157+ DataHash : []byte ("datahash" ),
158+ }
159+
160+ header .MemoizeHash ()
161+ require .NotNil (t , header .cachedHash )
162+
163+ clone := header .Clone ()
164+ assert .Nil (t , clone .cachedHash , "Clone should not copy memoized hash state" )
165+ assert .Equal (t , header .Hash (), clone .Hash ())
166+ }
0 commit comments