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

Validate MerklePathPrefix and Path #7193

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions modules/core/02-client/types/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ func (c Counterparty) Validate() error {
return err
}

if c.MerklePathPrefix.Empty() {
return errorsmod.Wrap(ErrInvalidCounterparty, "prefix cannot be empty")
if err := c.MerklePathPrefix.ValidateAsPrefix(); err != nil {
return errorsmod.Wrap(ErrInvalidCounterparty, err.Error())
}

return nil
Expand Down
24 changes: 24 additions & 0 deletions modules/core/02-client/types/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ func TestValidateCounterparty(t *testing.T) {
commitmenttypes.NewMerklePath([]byte("ibc")),
nil,
},
{
"success with multiple element prefix",
ibctesting.FirstClientID,
commitmenttypes.NewMerklePath([]byte("ibc"), []byte("address")),
nil,
},
{
"success with multiple element prefix, last prefix empty",
ibctesting.FirstClientID,
commitmenttypes.NewMerklePath([]byte("ibc"), []byte("")),
nil,
},
{
"success with single empty key prefix",
ibctesting.FirstClientID,
commitmenttypes.NewMerklePath([]byte("")),
nil,
},
{
"failure: invalid client id",
"",
Expand All @@ -114,6 +132,12 @@ func TestValidateCounterparty(t *testing.T) {
commitmenttypes.NewMerklePath(),
types.ErrInvalidCounterparty,
},
{
"failure: empty key in merkle path prefix first element",
ibctesting.FirstClientID,
commitmenttypes.NewMerklePath([]byte(""), []byte("ibc")),
types.ErrInvalidCounterparty,
},
}

for _, tc := range testCases {
Expand Down
33 changes: 33 additions & 0 deletions modules/core/23-commitment/types/v2/merkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,36 @@ func (mp MerklePath) GetKey(i uint64) ([]byte, error) {
func (mp MerklePath) Empty() bool {
return len(mp.KeyPath) == 0
}

// ValidateAsPrefix validates the MerklePath to ensure it is a valid prefix
// Thus every element of the merkle path must be non-empty except for the last element
// which may be empty. In this case, the ICS24 path will be appended to the last element
// to form the full path.
func (mp MerklePath) ValidateAsPrefix() error {
if mp.Empty() {
return fmt.Errorf("path cannot have lenth 0")
}

for i, key := range mp.KeyPath {
if len(key) == 0 && i != len(mp.KeyPath)-1 {
return fmt.Errorf("key at index %d cannot be empty", i)
}
}
return nil
}

// ValidateFullPath validates the MerklePath as a fully constructed path.
// Here every element must be non-empty since the MerklePath is no longer
// acting as a prefix but is instead the full path intended for verification.
func (mp MerklePath) ValidateFullPath() error {
if mp.Empty() {
return fmt.Errorf("path cannot have lenth 0")
}

for i, key := range mp.KeyPath {
if len(key) == 0 {
return fmt.Errorf("key at index %d cannot be empty", i)
}
}
return nil
}
61 changes: 61 additions & 0 deletions modules/core/23-commitment/types/v2/merkle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package v2

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestMerklePathValidation(t *testing.T) {
cases := []struct {
name string
path MerklePath
expValidPrefix bool
expValidFullPath bool
}{
{
"success: prefix and path",
NewMerklePath([]byte("key1"), []byte("key2")),
true,
true,
},
{
"success: prefix with empty last key",
NewMerklePath([]byte("key1"), []byte("")),
true,
false,
},
{
"success: prefix with single empty key",
NewMerklePath([]byte("")),
true,
false,
},
{
"failure: empty path",
NewMerklePath(),
false,
false,
},
{
"failure: empty key in start prefix",
NewMerklePath([]byte(""), []byte("key2")),
false,
false,
},
}

for _, tc := range cases {
if tc.expValidPrefix {
require.NoError(t, tc.path.ValidateAsPrefix(), tc.name)
} else {
require.Error(t, tc.path.ValidateAsPrefix(), tc.name)
}

if tc.expValidFullPath {
require.NoError(t, tc.path.ValidateFullPath(), tc.name)
} else {
require.Error(t, tc.path.ValidateFullPath(), tc.name)
}
}
}
Loading