Skip to content

Commit

Permalink
Fix rebase conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 committed Jul 22, 2022
1 parent 7ecb212 commit 9982267
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
12 changes: 7 additions & 5 deletions lib/trie/proof/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
21 changes: 15 additions & 6 deletions lib/trie/proof/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 9982267

Please sign in to comment.