From 97b248d08a18ba36e165db2d5bd98cd2a1a8fbe7 Mon Sep 17 00:00:00 2001 From: Damian Nolan Date: Wed, 19 Jun 2024 12:22:05 +0200 Subject: [PATCH] refactor!: adapt merkle path to use repeated bytes in favour of strings (#6633) * refactor: adapt merkle path to use repeated bytes in favour of strings * chore: add changelog --- CHANGELOG.md | 1 + .../core/23-commitment/types/commitment.pb.go | 62 +++++++++---------- modules/core/23-commitment/types/merkle.go | 14 ++++- .../core/23-commitment/types/merkle_test.go | 2 +- proto/ibc/core/commitment/v1/commitment.proto | 2 +- 5 files changed, 45 insertions(+), 36 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec0ff639f78..28e9a2d637a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/modules/core/23-commitment/types/commitment.pb.go b/modules/core/23-commitment/types/commitment.pb.go index bf95874a8ac..b517747e01e 100644 --- a/modules/core/23-commitment/types/commitment.pb.go +++ b/modules/core/23-commitment/types/commitment.pb.go @@ -114,7 +114,7 @@ func (m *MerklePrefix) GetKeyPrefix() []byte { // arbitrary structured object (defined by a commitment type). // MerklePath is represented from root-to-leaf type MerklePath struct { - KeyPath []string `protobuf:"bytes,1,rep,name=key_path,json=keyPath,proto3" json:"key_path,omitempty"` + KeyPath [][]byte `protobuf:"bytes,1,rep,name=key_path,json=keyPath,proto3" json:"key_path,omitempty"` } func (m *MerklePath) Reset() { *m = MerklePath{} } @@ -150,7 +150,7 @@ func (m *MerklePath) XXX_DiscardUnknown() { var xxx_messageInfo_MerklePath proto.InternalMessageInfo -func (m *MerklePath) GetKeyPath() []string { +func (m *MerklePath) GetKeyPath() [][]byte { if m != nil { return m.KeyPath } @@ -218,27 +218,27 @@ func init() { } var fileDescriptor_7921d88972a41469 = []byte{ - // 312 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x4c, 0x90, 0xb1, 0x4e, 0xeb, 0x30, - 0x14, 0x86, 0x13, 0xdd, 0xaa, 0x97, 0xba, 0x9d, 0x2c, 0x84, 0xa0, 0x02, 0x53, 0x65, 0xa0, 0x5d, - 0x6a, 0xab, 0xed, 0x52, 0x21, 0x26, 0x18, 0x98, 0x90, 0xaa, 0x0c, 0x0c, 0x2c, 0x28, 0x31, 0x6e, - 0x62, 0xb5, 0xe1, 0x44, 0xb1, 0x1b, 0xd1, 0x37, 0x60, 0xe4, 0x11, 0x78, 0x1c, 0xc6, 0x8e, 0x8c, - 0xa8, 0x79, 0x11, 0x64, 0xbb, 0x41, 0xd9, 0xce, 0xd1, 0xf9, 0xfc, 0xeb, 0xf7, 0x87, 0x86, 0x32, - 0xe6, 0x8c, 0x43, 0x21, 0x18, 0x87, 0x2c, 0x93, 0x3a, 0x13, 0xaf, 0x9a, 0x95, 0x93, 0xc6, 0x46, - 0xf3, 0x02, 0x34, 0xe0, 0x13, 0x19, 0x73, 0x6a, 0x40, 0xda, 0x38, 0x95, 0x93, 0xfe, 0x71, 0x02, - 0x09, 0x58, 0x84, 0x99, 0xc9, 0xd1, 0xfd, 0x73, 0x0e, 0x2a, 0x03, 0xc5, 0x24, 0x57, 0xd3, 0x99, - 0xc9, 0xcb, 0x0b, 0x80, 0xa5, 0x72, 0xd7, 0xe0, 0x0a, 0xa1, 0x07, 0x51, 0xac, 0xd6, 0x22, 0x04, - 0xd0, 0x18, 0xa3, 0x56, 0x1a, 0xa9, 0xf4, 0xd4, 0x1f, 0xf8, 0xa3, 0x5e, 0x68, 0xe7, 0xeb, 0xd6, - 0xfb, 0xe7, 0xa5, 0x17, 0x8c, 0x51, 0xcf, 0x71, 0x8b, 0x42, 0x2c, 0xe5, 0x1b, 0xbe, 0x40, 0x68, - 0x25, 0xb6, 0xcf, 0xb9, 0xdd, 0x0e, 0x7c, 0x67, 0x25, 0xb6, 0xee, 0x1c, 0x0c, 0xeb, 0xd8, 0x45, - 0xa4, 0x53, 0x7c, 0x86, 0x8e, 0x2c, 0x1c, 0x69, 0x13, 0xfd, 0x6f, 0xd4, 0x09, 0xff, 0x1b, 0x34, - 0xd2, 0x69, 0x70, 0x8f, 0xba, 0x75, 0x2e, 0xc0, 0x12, 0xcf, 0x51, 0xdb, 0xd5, 0xb3, 0x5c, 0x77, - 0x3a, 0xa0, 0xae, 0x3d, 0xb5, 0xed, 0x69, 0x39, 0xa1, 0x77, 0x7f, 0x5f, 0xb6, 0x2f, 0xc2, 0x03, - 0x7f, 0xfb, 0xf8, 0xb5, 0x27, 0xfe, 0x6e, 0x4f, 0xfc, 0x9f, 0x3d, 0xf1, 0x3f, 0x2a, 0xe2, 0xed, - 0x2a, 0xe2, 0x7d, 0x57, 0xc4, 0x7b, 0xba, 0x49, 0xa4, 0x4e, 0x37, 0xb1, 0x91, 0xc5, 0x6a, 0x17, - 0x31, 0x1f, 0x27, 0xc0, 0xca, 0x39, 0xcb, 0xe0, 0x65, 0xb3, 0x16, 0xca, 0x79, 0x9f, 0xce, 0xc6, - 0x0d, 0xf5, 0x7a, 0x9b, 0x0b, 0x15, 0xb7, 0xad, 0xa7, 0xd9, 0x6f, 0x00, 0x00, 0x00, 0xff, 0xff, - 0x55, 0x02, 0x17, 0xe1, 0x9e, 0x01, 0x00, 0x00, + // 310 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x4c, 0x90, 0x31, 0x4e, 0xc3, 0x30, + 0x14, 0x86, 0x13, 0x51, 0x15, 0x70, 0x3b, 0x59, 0x08, 0x41, 0x05, 0xa6, 0xca, 0x40, 0xbb, 0xd4, + 0x56, 0xdb, 0xa5, 0x42, 0x4c, 0x30, 0x30, 0x21, 0x55, 0x19, 0x18, 0x58, 0x50, 0x62, 0xdc, 0xc4, + 0x6a, 0xc3, 0x8b, 0x62, 0x37, 0xa2, 0x37, 0x60, 0xe4, 0x08, 0x1c, 0x87, 0xb1, 0x23, 0x23, 0x6a, + 0x2e, 0x82, 0x6c, 0x37, 0x28, 0xdb, 0x7b, 0x7a, 0x9f, 0x7f, 0xfd, 0xfe, 0xd0, 0x40, 0xc6, 0x9c, + 0x71, 0x28, 0x04, 0xe3, 0x90, 0x65, 0x52, 0x67, 0xe2, 0x4d, 0xb3, 0x72, 0xdc, 0xd8, 0x68, 0x5e, + 0x80, 0x06, 0x7c, 0x2a, 0x63, 0x4e, 0x0d, 0x48, 0x1b, 0xa7, 0x72, 0xdc, 0x3b, 0x49, 0x20, 0x01, + 0x8b, 0x30, 0x33, 0x39, 0xba, 0x77, 0xc1, 0x41, 0x65, 0xa0, 0x98, 0xe4, 0x6a, 0x32, 0x35, 0x79, + 0x79, 0x01, 0xb0, 0x50, 0xee, 0x1a, 0x5c, 0x23, 0xf4, 0x28, 0x8a, 0xe5, 0x4a, 0x84, 0x00, 0x1a, + 0x63, 0xd4, 0x4a, 0x23, 0x95, 0x9e, 0xf9, 0x7d, 0x7f, 0xd8, 0x0d, 0xed, 0x7c, 0xd3, 0xfa, 0xf8, + 0xba, 0xf2, 0x82, 0x11, 0xea, 0x3a, 0x6e, 0x5e, 0x88, 0x85, 0x7c, 0xc7, 0x97, 0x08, 0x2d, 0xc5, + 0xe6, 0x25, 0xb7, 0xdb, 0x9e, 0x3f, 0x5e, 0x8a, 0x8d, 0x3b, 0x07, 0x83, 0x3a, 0x76, 0x1e, 0xe9, + 0x14, 0x9f, 0xa3, 0x23, 0x0b, 0x47, 0xda, 0x44, 0x1f, 0x0c, 0xbb, 0xe1, 0xa1, 0x41, 0x23, 0x9d, + 0x06, 0x0f, 0xa8, 0x53, 0xe7, 0x02, 0x2c, 0xf0, 0x0c, 0xb5, 0x5d, 0x3d, 0xcb, 0x75, 0x26, 0x7d, + 0xea, 0xda, 0x53, 0xdb, 0x9e, 0x96, 0x63, 0x7a, 0xff, 0xff, 0x65, 0xfb, 0x22, 0xdc, 0xf3, 0x77, + 0x4f, 0xdf, 0x3b, 0xe2, 0x6f, 0x77, 0xc4, 0xff, 0xdd, 0x11, 0xff, 0xb3, 0x22, 0xde, 0xb6, 0x22, + 0xde, 0x4f, 0x45, 0xbc, 0xe7, 0xdb, 0x44, 0xea, 0x74, 0x1d, 0x1b, 0x59, 0xac, 0x76, 0x11, 0xf3, + 0x51, 0x02, 0xac, 0x9c, 0xb1, 0x0c, 0x5e, 0xd7, 0x2b, 0xa1, 0x9c, 0xf7, 0xc9, 0x74, 0xd4, 0x50, + 0xaf, 0x37, 0xb9, 0x50, 0x71, 0xdb, 0x7a, 0x9a, 0xfe, 0x05, 0x00, 0x00, 0xff, 0xff, 0x0c, 0xb3, + 0xc0, 0xc0, 0x9e, 0x01, 0x00, 0x00, } func (m *MerkleRoot) Marshal() (dAtA []byte, err error) { @@ -414,8 +414,8 @@ func (m *MerklePath) Size() (n int) { var l int _ = l if len(m.KeyPath) > 0 { - for _, s := range m.KeyPath { - l = len(s) + for _, b := range m.KeyPath { + l = len(b) n += 1 + l + sovCommitment(uint64(l)) } } @@ -644,7 +644,7 @@ func (m *MerklePath) Unmarshal(dAtA []byte) error { if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field KeyPath", wireType) } - var stringLen uint64 + var byteLen int for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowCommitment @@ -654,23 +654,23 @@ func (m *MerklePath) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - stringLen |= uint64(b&0x7F) << shift + byteLen |= int(b&0x7F) << shift if b < 0x80 { break } } - intStringLen := int(stringLen) - if intStringLen < 0 { + if byteLen < 0 { return ErrInvalidLengthCommitment } - postIndex := iNdEx + intStringLen + postIndex := iNdEx + byteLen if postIndex < 0 { return ErrInvalidLengthCommitment } if postIndex > l { return io.ErrUnexpectedEOF } - m.KeyPath = append(m.KeyPath, string(dAtA[iNdEx:postIndex])) + m.KeyPath = append(m.KeyPath, make([]byte, postIndex-iNdEx)) + copy(m.KeyPath[len(m.KeyPath)-1], dAtA[iNdEx:postIndex]) iNdEx = postIndex default: iNdEx = preIndex diff --git a/modules/core/23-commitment/types/merkle.go b/modules/core/23-commitment/types/merkle.go index 0242f74e012..9e28936bbd9 100644 --- a/modules/core/23-commitment/types/merkle.go +++ b/modules/core/23-commitment/types/merkle.go @@ -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, } } @@ -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 @@ -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. diff --git a/modules/core/23-commitment/types/merkle_test.go b/modules/core/23-commitment/types/merkle_test.go index 17181358743..b8aedb0e967 100644 --- a/modules/core/23-commitment/types/merkle_test.go +++ b/modules/core/23-commitment/types/merkle_test.go @@ -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) diff --git a/proto/ibc/core/commitment/v1/commitment.proto b/proto/ibc/core/commitment/v1/commitment.proto index b4753be2d92..3cdaadffd04 100644 --- a/proto/ibc/core/commitment/v1/commitment.proto +++ b/proto/ibc/core/commitment/v1/commitment.proto @@ -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.