From 5a149fcad086de94792ef1a6b4e41313c0cb903b Mon Sep 17 00:00:00 2001 From: Jernej Kos Date: Thu, 1 Sep 2022 10:09:07 +0200 Subject: [PATCH] go/genesis: Cache computed genesis document hash --- .changelog/4919.feature.md | 1 + go/genesis/api/api.go | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 .changelog/4919.feature.md diff --git a/.changelog/4919.feature.md b/.changelog/4919.feature.md new file mode 100644 index 00000000000..7a64388fd67 --- /dev/null +++ b/.changelog/4919.feature.md @@ -0,0 +1 @@ +go/genesis: Cache computed genesis document hash diff --git a/go/genesis/api/api.go b/go/genesis/api/api.go index 1d2d48f8ba6..25faf6fe2ba 100644 --- a/go/genesis/api/api.go +++ b/go/genesis/api/api.go @@ -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