Skip to content

Commit

Permalink
refactor!: adapt merkle path to use repeated bytes in favour of strin…
Browse files Browse the repository at this point in the history
…gs (#6633)

* refactor: adapt merkle path to use repeated bytes in favour of strings

* chore: add changelog
  • Loading branch information
damiannolan authored Jun 19, 2024
1 parent 10aa0ef commit 97b248d
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 36 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (apps/27-interchain-accounts) [\#6433](https://github.com/cosmos/ibc-go/pull/6433) Use UNORDERED as the default ordering for new ICA channels.
* (apps/transfer) [\#6440](https://github.com/cosmos/ibc-go/pull/6440) Remove `GetPrefixedDenom`.
* (apps/transfer) [\#6508](https://github.com/cosmos/ibc-go/pull/6508) Remove the `DenomTrace` type.
* (23-commmitment) [\#6633](https://github.com/cosmos/ibc-go/pull/6633) MerklePath has been changed to use `repeated bytes` in favour of `repeated strings`.

### State Machine Breaking

Expand Down
62 changes: 31 additions & 31 deletions modules/core/23-commitment/types/commitment.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 11 additions & 3 deletions modules/core/23-commitment/types/merkle.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,13 @@ var _ exported.Path = (*MerklePath)(nil)
// NewMerklePath creates a new MerklePath instance
// The keys must be passed in from root-to-leaf order
func NewMerklePath(keyPath ...string) MerklePath {
var path [][]byte
for _, key := range keyPath {
path = append(path, []byte(key))
}

return MerklePath{
KeyPath: keyPath,
KeyPath: path,
}
}

Expand All @@ -81,7 +86,7 @@ func (mp MerklePath) GetKey(i uint64) ([]byte, error) {
if i >= uint64(len(mp.KeyPath)) {
return nil, fmt.Errorf("index out of range. %d (index) >= %d (len)", i, len(mp.KeyPath))
}
return []byte(mp.KeyPath[i]), nil
return mp.KeyPath[i], nil
}

// Empty returns true if the path is empty
Expand All @@ -95,7 +100,10 @@ func ApplyPrefix(prefix exported.Prefix, path MerklePath) (MerklePath, error) {
if prefix == nil || prefix.Empty() {
return MerklePath{}, errorsmod.Wrap(ErrInvalidPrefix, "prefix can't be empty")
}
return NewMerklePath(append([]string{string(prefix.Bytes())}, path.KeyPath...)...), nil

return MerklePath{
KeyPath: append([][]byte{prefix.Bytes()}, path.KeyPath...),
}, nil
}

// VerifyMembership verifies the membership of a merkle proof against the given root, path, and value.
Expand Down
2 changes: 1 addition & 1 deletion modules/core/23-commitment/types/merkle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func TestApplyPrefix(t *testing.T) {

pathStr := "pathone/pathtwo/paththree/key"
path := types.MerklePath{
KeyPath: []string{pathStr},
KeyPath: [][]byte{[]byte(pathStr)},
}

prefixedPath, err := types.ApplyPrefix(prefix, path)
Expand Down
2 changes: 1 addition & 1 deletion proto/ibc/core/commitment/v1/commitment.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ message MerklePrefix {
// arbitrary structured object (defined by a commitment type).
// MerklePath is represented from root-to-leaf
message MerklePath {
repeated string key_path = 1;
repeated bytes key_path = 1;
}

// MerkleProof is a wrapper type over a chain of CommitmentProofs.
Expand Down

0 comments on commit 97b248d

Please sign in to comment.