Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(trie): export LoadFromProof #2455

Merged
merged 2 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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