From 99822674f329ab765903490df37093c68519e34b Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Fri, 8 Jul 2022 14:51:41 +0000 Subject: [PATCH] Fix rebase conflicts --- lib/trie/proof/generate.go | 12 +++++++----- lib/trie/proof/verify.go | 21 +++++++++++++++------ 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/lib/trie/proof/generate.go b/lib/trie/proof/generate.go index 157e7dbffd..6638d42707 100644 --- a/lib/trie/proof/generate.go +++ b/lib/trie/proof/generate.go @@ -10,6 +10,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" "github.com/ChainSafe/gossamer/lib/trie" ) @@ -36,6 +37,9 @@ func Generate(rootHash []byte, fullKeys [][]byte, database Database) ( } rootNode := trie.RootNode() + buffer := pools.DigestBuffers.Get().(*bytes.Buffer) + defer pools.DigestBuffers.Put(buffer) + merkleValuesSeen := make(map[string]struct{}) for _, fullKey := range fullKeys { fullKeyNibbles := codec.KeyLEToNibbles(fullKey) @@ -48,14 +52,12 @@ func Generate(rootHash []byte, fullKeys [][]byte, database Database) ( } for _, encodedProofNode := range newEncodedProofNodes { - // No need to force-hash the root node if it smaller than 32B since - // we use this merkle value for deduplication only, so isRoot is false. - const isRoot = false - merkleValue, err := node.MerkleValue(encodedProofNode, isRoot) + buffer.Reset() + err := node.MerkleValue(encodedProofNode, buffer) if err != nil { return nil, fmt.Errorf("blake2b hash: %w", err) } - merkleValueString := string(merkleValue) + merkleValueString := buffer.String() _, seen := merkleValuesSeen[merkleValueString] if seen { diff --git a/lib/trie/proof/verify.go b/lib/trie/proof/verify.go index f447ba9057..5aab830d12 100644 --- a/lib/trie/proof/verify.go +++ b/lib/trie/proof/verify.go @@ -10,6 +10,7 @@ import ( "strings" "github.com/ChainSafe/gossamer/internal/trie/node" + "github.com/ChainSafe/gossamer/internal/trie/pools" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/lib/trie" ) @@ -58,25 +59,33 @@ func buildTrie(encodedProofNodes [][]byte, rootHash []byte) (t *trie.Trie, err e } digestToEncoding := make(map[string][]byte, len(encodedProofNodes)) + var root *node.Node + + // note we can use a buffer from the pool since + // the calculated root hash digest is not used after + // the function completes. + buffer := pools.DigestBuffers.Get().(*bytes.Buffer) + defer pools.DigestBuffers.Put(buffer) // This loop does two things: // 1. It finds the root node by comparing it with the root hash and decodes it. // 2. It stores other encoded nodes in a mapping from their encoding digest to // their encoding. They are only decoded later if the root or one of its // descendant nodes reference their hash digest. - var root *node.Node for _, encodedProofNode := range encodedProofNodes { // Note all encoded proof nodes are one of the following: // - trie root node // - child trie root node // - child node with an encoding larger than 32 bytes - // In all cases, their Merkle value is the encoding hash digest. - const isRoot = true // force hashing of the node - digestHash, err := node.MerkleValue(encodedProofNode, isRoot) + // In all cases, their Merkle value is the encoding hash digest, + // so we use MerkleValueRoot to force hashing the node in case + // it is a root node smaller or equal to 32 bytes. + buffer.Reset() + err = node.MerkleValueRoot(encodedProofNode, buffer) if err != nil { - return nil, fmt.Errorf("blake2b hash: %w", err) + return nil, fmt.Errorf("calculating Merkle value: %w", err) } - digest := digestHash[:] + digest := buffer.Bytes() if root != nil || !bytes.Equal(digest, rootHash) { // root node already found or the hash doesn't match the root hash.