Skip to content

Commit

Permalink
go/genesis: Cache computed genesis document hash
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Sep 1, 2022
1 parent 15c1d95 commit 2d6d941
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions .changelog/4919.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
go/genesis: Cache computed genesis document hash
12 changes: 11 additions & 1 deletion go/genesis/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,21 @@ type Document struct {
// Extra data is arbitrary extra data that is part of the
// genesis block but is otherwise ignored by the protocol.
ExtraData map[string][]byte `json:"extra_data"`

cachedHash *hash.Hash
}

// Hash returns the cryptographic hash of the encoded genesis document.
//
// Calling this method will cause the computed hash to be cached so make sure
// that the document is not modified later.
func (d *Document) Hash() hash.Hash {
return hash.NewFrom(d)
if d.cachedHash != nil {
return *d.cachedHash
}
h := hash.NewFrom(d)
d.cachedHash = &h
return h
}

// ChainContext returns a string that can be used as a chain domain separation
Expand Down

0 comments on commit 2d6d941

Please sign in to comment.