Skip to content

Commit

Permalink
Fixes comments and error wrappings
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 authored Jun 15, 2022
1 parent b897c7c commit 2132ac2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
6 changes: 3 additions & 3 deletions lib/trie/proof/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ func Generate(rootHash []byte, fullKey []byte, database Database) (
encodedProofNodes [][]byte, err error) {
trie := trie.NewEmptyTrie()
if err := trie.Load(database, common.BytesToHash(rootHash)); err != nil {
return nil, fmt.Errorf("cannot load trie: %w", err)
return nil, fmt.Errorf("loading trie: %w", err)
}

rootNode := trie.RootNode()
fullKeyNibbles := codec.KeyLEToNibbles(fullKey)
encodedProofNodes, err = walk(rootNode, fullKeyNibbles)
if err != nil {
// Note we wrap the full key context here since find is recursive and
// Note we wrap the full key context here since walk is recursive and
// may not be aware of the initial full key.
return nil, fmt.Errorf("cannot find node at key 0x%x in trie: %w", fullKey, err)
return nil, fmt.Errorf("walk to node at key 0x%x: %w", fullKey, err)
}

return encodedProofNodes, nil
Expand Down
12 changes: 6 additions & 6 deletions lib/trie/proof/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var (
func Verify(encodedProofNodes [][]byte, rootHash, key, value []byte) (err error) {
proofTrie, err := buildTrie(encodedProofNodes, rootHash)
if err != nil {
return fmt.Errorf("cannot build trie from proof encoded nodes: %w", err)
return fmt.Errorf("build trie from proof encoded nodes: %w", err)
}

proofTrieValue := proofTrie.Get(key)
Expand All @@ -53,8 +53,8 @@ var (
// buildTrie sets a partial trie based on the proof slice of encoded nodes.
func buildTrie(encodedProofNodes [][]byte, rootHash []byte) (t *trie.Trie, err error) {
if len(encodedProofNodes) == 0 {
return nil, fmt.Errorf("%w: for Merkle root hash %s",
ErrEmptyProof, bytesToString(rootHash))
return nil, fmt.Errorf("%w: for Merkle root hash 0x%x",
ErrEmptyProof, rootHash)
}

proofHashToNode := make(map[string]*node.Node, len(encodedProofNodes))
Expand All @@ -63,7 +63,7 @@ func buildTrie(encodedProofNodes [][]byte, rootHash []byte) (t *trie.Trie, err e
for i, encodedProofNode := range encodedProofNodes {
decodedNode, err := node.Decode(bytes.NewReader(encodedProofNode))
if err != nil {
return nil, fmt.Errorf("cannot decode node at index %d: %w (node encoded is 0x%x)",
return nil, fmt.Errorf("decoding node at index %d: %w (node encoded is 0x%x)",
i, err, encodedProofNode)
}

Expand All @@ -79,7 +79,7 @@ func buildTrie(encodedProofNodes [][]byte, rootHash []byte) (t *trie.Trie, err e
const isRoot = false
decodedNode.HashDigest, err = node.MerkleValue(encodedProofNode, isRoot)
if err != nil {
return nil, fmt.Errorf("cannot calculate Merkle value of node at index %d: %w", i, err)
return nil, fmt.Errorf("merkle value of node at index %d: %w", i, err)
}

proofHash := common.BytesToHex(decodedNode.HashDigest)
Expand Down Expand Up @@ -125,7 +125,7 @@ func buildTrie(encodedProofNodes [][]byte, rootHash []byte) (t *trie.Trie, err e
}

// loadProof is a recursive function that will create all the trie paths based
// on the mapped proofs slice starting at the root
// on the map from node hash to node starting at the root.
func loadProof(proofHashToNode map[string]*node.Node, n *node.Node) {
if n.Type() != node.Branch {
return
Expand Down

0 comments on commit 2132ac2

Please sign in to comment.