Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

go/genesis: Cache computed genesis document hash #4919

Merged
merged 1 commit into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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