Skip to content

Commit

Permalink
feat(trie): export LoadFromProof (#2455)
Browse files Browse the repository at this point in the history
  • Loading branch information
qdm12 authored Apr 5, 2022
1 parent 620535d commit 0b4f33d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
12 changes: 7 additions & 5 deletions lib/trie/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,17 @@ func (t *Trie) store(db chaindb.Batch, n Node) error {
return nil
}

// loadFromProof create a partial trie based on the proof slice, as it only contains nodes that are in the proof afaik.
func (t *Trie) loadFromProof(rawProof [][]byte, rootHash []byte) error {
if len(rawProof) == 0 {
// LoadFromProof sets a partial trie based on the proof slice of encoded nodes.
// Note this is exported because it is imported is used by:
// https://github.com/ComposableFi/ibc-go/blob/6d62edaa1a3cb0768c430dab81bb195e0b0c72db/modules/light-clients/11-beefy/types/client_state.go#L78
func (t *Trie) LoadFromProof(proofEncodedNodes [][]byte, rootHash []byte) error {
if len(proofEncodedNodes) == 0 {
return ErrEmptyProof
}

proofHashToNode := make(map[string]Node, len(rawProof))
proofHashToNode := make(map[string]Node, len(proofEncodedNodes))

for i, rawNode := range rawProof {
for i, rawNode := range proofEncodedNodes {
decodedNode, err := node.Decode(bytes.NewReader(rawNode))
if err != nil {
return fmt.Errorf("%w: at index %d: 0x%x",
Expand Down
2 changes: 1 addition & 1 deletion lib/trie/proof.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func VerifyProof(proof [][]byte, root []byte, items []Pair) (bool, error) {
}

proofTrie := NewEmptyTrie()
if err := proofTrie.loadFromProof(proof, root); err != nil {
if err := proofTrie.LoadFromProof(proof, root); err != nil {
return false, fmt.Errorf("%w: %s", ErrLoadFromProof, err)
}

Expand Down
2 changes: 1 addition & 1 deletion lib/trie/proof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func TestVerifyProof_ShouldReturnTrueWithouCompareValues(t *testing.T) {
require.NoError(t, err)
}

func TestBranchNodes_SameHash_DiferentPaths_GenerateAndVerifyProof(t *testing.T) {
func TestBranchNodes_SameHash_DifferentPaths_GenerateAndVerifyProof(t *testing.T) {
value := []byte("somevalue")
entries := []Pair{
{Key: []byte("d"), Value: value},
Expand Down

0 comments on commit 0b4f33d

Please sign in to comment.