diff --git a/lib/trie/trie.go b/lib/trie/trie.go index 62c75d1f6b..e98d6b27dd 100644 --- a/lib/trie/trie.go +++ b/lib/trie/trie.go @@ -9,6 +9,7 @@ import ( "github.com/ChainSafe/gossamer/internal/trie/codec" "github.com/ChainSafe/gossamer/internal/trie/node" + "github.com/ChainSafe/gossamer/internal/trie/pools" "github.com/ChainSafe/gossamer/lib/common" ) @@ -154,8 +155,16 @@ func (t *Trie) MustHash() common.Hash { // Hash returns the hashed root of the trie. func (t *Trie) Hash() (rootHash common.Hash, err error) { - _, hash, err := t.root.EncodeAndHash(true) - return common.BytesToHash(hash), err + buffer := pools.EncodingBuffers.Get().(*bytes.Buffer) + buffer.Reset() + defer pools.EncodingBuffers.Put(buffer) + + err = encodeRoot(t.root, buffer) + if err != nil { + return [32]byte{}, err + } + + return common.Blake2bHash(buffer.Bytes()) // TODO optimisation: use hashers sync pools } // Entries returns all the key-value pairs in the trie as a map of keys to values