Skip to content

Commit 8d2fef6

Browse files
committed
update and changelog
1 parent 192b483 commit 8d2fef6

2 files changed

Lines changed: 10 additions & 0 deletions

File tree

types/hashing.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ func (h *Header) Hash() Hash {
5757
// MemoizeHash computes the header hash and stores it on the header for future
5858
// Hash() calls. Call this before publishing the header to shared goroutines or
5959
// caches.
60+
//
61+
// If a Header struct is reused (e.g. overwritten via FromProto or field
62+
// assignment), call InvalidateHash() first to clear the cached value before
63+
// calling MemoizeHash again. Failure to do so will return the stale cached hash.
6064
func (h *Header) MemoizeHash() Hash {
6165
if h == nil {
6266
return nil
@@ -73,6 +77,8 @@ func (h *Header) MemoizeHash() Hash {
7377
}
7478

7579
func (h *Header) computeHash() Hash {
80+
// Legacy hash takes precedence when legacy fields are present (backwards
81+
// compatibility). Slim hash is the canonical hash for all other headers.
7682
if h.Legacy != nil && !h.Legacy.IsZero() {
7783
if legacyHash, err := h.HashLegacy(); err == nil {
7884
return legacyHash

types/header.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ func HeaderFromContext(ctx context.Context) (Header, bool) {
2323
return Header{}, false
2424
}
2525

26+
// Clear the memoized hash on the returned copy. The caller may mutate
27+
// fields and call Hash() without knowing to call InvalidateHash() first,
28+
// which would return a stale cached value.
29+
h.InvalidateHash()
2630
return h, true
2731
}
2832

0 commit comments

Comments
 (0)