From 15fa81bf62031bd44500b6fda93e7a3d52981ce9 Mon Sep 17 00:00:00 2001 From: Aditya Sripal <14364734+AdityaSripal@users.noreply.github.com> Date: Mon, 10 Feb 2025 20:38:32 -0500 Subject: [PATCH 1/2] add async packet genesis --- modules/core/04-channel/v2/genesis.go | 16 +++ modules/core/04-channel/v2/genesis_test.go | 15 +++ modules/core/04-channel/v2/keeper/keeper.go | 16 ++- modules/core/04-channel/v2/types/genesis.go | 13 +- .../core/04-channel/v2/types/genesis.pb.go | 115 ++++++++++++++---- .../core/04-channel/v2/types/genesis_test.go | 10 ++ modules/core/04-channel/v2/types/keys.go | 12 +- modules/core/genesis_test.go | 16 +++ proto/ibc/core/channel/v2/genesis.proto | 3 +- 9 files changed, 181 insertions(+), 35 deletions(-) diff --git a/modules/core/04-channel/v2/genesis.go b/modules/core/04-channel/v2/genesis.go index 29515d46bf0..4df9d61bf5c 100644 --- a/modules/core/04-channel/v2/genesis.go +++ b/modules/core/04-channel/v2/genesis.go @@ -3,6 +3,8 @@ package channelv2 import ( "context" + "github.com/cosmos/gogoproto/proto" + "github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/keeper" "github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types" ) @@ -23,6 +25,16 @@ func InitGenesis(ctx context.Context, k *keeper.Keeper, gs types.GenesisState) { k.SetPacketReceipt(ctx, receipt.ClientId, receipt.Sequence) } + // set async packets + for _, gs := range gs.AsyncPackets { + var packet types.Packet + err := proto.Unmarshal(gs.Data, &packet) + if err != nil { + panic(err) + } + k.SetAsyncPacket(ctx, gs.ClientId, gs.Sequence, packet) + } + // set send sequences for _, seq := range gs.SendSequences { k.SetNextSequenceSend(ctx, seq.ClientId, seq.Sequence) @@ -35,6 +47,7 @@ func ExportGenesis(ctx context.Context, k *keeper.Keeper) types.GenesisState { Acknowledgements: make([]types.PacketState, 0), Commitments: make([]types.PacketState, 0), Receipts: make([]types.PacketState, 0), + AsyncPackets: make([]types.PacketState, 0), SendSequences: make([]types.PacketSequence, 0), } for _, clientState := range clientStates { @@ -47,6 +60,9 @@ func ExportGenesis(ctx context.Context, k *keeper.Keeper) types.GenesisState { receipts := k.GetAllPacketReceiptsForClient(ctx, clientState.ClientId) gs.Receipts = append(gs.Receipts, receipts...) + asyncPackets := k.GetAllAsyncPacketsForClient(ctx, clientState.ClientId) + gs.AsyncPackets = append(gs.AsyncPackets, asyncPackets...) + seq, ok := k.GetNextSequenceSend(ctx, clientState.ClientId) if ok { gs.SendSequences = append(gs.SendSequences, types.NewPacketSequence(clientState.ClientId, seq)) diff --git a/modules/core/04-channel/v2/genesis_test.go b/modules/core/04-channel/v2/genesis_test.go index e56be7b23a2..53fd7f8d389 100644 --- a/modules/core/04-channel/v2/genesis_test.go +++ b/modules/core/04-channel/v2/genesis_test.go @@ -1,9 +1,12 @@ package channelv2_test import ( + proto "github.com/cosmos/gogoproto/proto" + channelv2 "github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2" "github.com/cosmos/ibc-go/v9/modules/core/04-channel/v2/types" ibctesting "github.com/cosmos/ibc-go/v9/testing" + mockv2 "github.com/cosmos/ibc-go/v9/testing/mock/v2" ) // TestInitExportGenesis tests the import and export flow for the channel v2 keeper. @@ -27,10 +30,22 @@ func (suite *ModuleTestSuite) TestInitExportGenesis() { commitment := types.NewPacketState(clientState.ClientId, uint64(i+1), []byte("commit_hash")) seq := types.NewPacketSequence(clientState.ClientId, uint64(i+1)) + packet := types.NewPacket( + uint64(i+1), + clientState.ClientId, + clientState.ClientId, + uint64(suite.chainA.GetContext().BlockTime().Unix()), + mockv2.NewMockPayload("src", "dst"), + ) + bz, err := proto.Marshal(&packet) + suite.Require().NoError(err) + asyncPacket := types.NewPacketState(clientState.ClientId, uint64(i+1), bz) + validGs.Acknowledgements = append(validGs.Acknowledgements, ack) validGs.Receipts = append(validGs.Receipts, receipt) validGs.Commitments = append(validGs.Commitments, commitment) validGs.SendSequences = append(validGs.SendSequences, seq) + validGs.AsyncPackets = append(validGs.AsyncPackets, asyncPacket) emptyGenesis.SendSequences = append(emptyGenesis.SendSequences, seq) } diff --git a/modules/core/04-channel/v2/keeper/keeper.go b/modules/core/04-channel/v2/keeper/keeper.go index 59bea345638..0e112466cf5 100644 --- a/modules/core/04-channel/v2/keeper/keeper.go +++ b/modules/core/04-channel/v2/keeper/keeper.go @@ -213,31 +213,37 @@ func extractSequenceFromKey(key, storePrefix []byte) uint64 { // GetAllPacketCommitmentsForClient returns all stored PacketCommitments objects for a specified // client ID. func (k *Keeper) GetAllPacketCommitmentsForClient(ctx context.Context, clientID string) []types.PacketState { - return k.getAllPacketsForClientStore(ctx, clientID, hostv2.PacketCommitmentPrefixKey) + return k.getAllPacketsForChannelStore(ctx, clientID, hostv2.PacketCommitmentPrefixKey) } // GetAllPacketAcknowledgementsForClient returns all stored PacketAcknowledgements objects for a specified // client ID. func (k *Keeper) GetAllPacketAcknowledgementsForClient(ctx context.Context, clientID string) []types.PacketState { - return k.getAllPacketsForClientStore(ctx, clientID, hostv2.PacketAcknowledgementPrefixKey) + return k.getAllPacketsForChannelStore(ctx, clientID, hostv2.PacketAcknowledgementPrefixKey) } // GetAllPacketReceiptsForClient returns all stored PacketReceipts objects for a specified // client ID. func (k *Keeper) GetAllPacketReceiptsForClient(ctx context.Context, clientID string) []types.PacketState { - return k.getAllPacketsForClientStore(ctx, clientID, hostv2.PacketReceiptPrefixKey) + return k.getAllPacketsForChannelStore(ctx, clientID, hostv2.PacketReceiptPrefixKey) +} + +// GetAllAsyncPacketsForClient returns all stored AsyncPackets objects for a specified +// client ID. +func (k *Keeper) GetAllAsyncPacketsForClient(ctx context.Context, clientID string) []types.PacketState { + return k.getAllPacketsForChannelStore(ctx, clientID, types.AsyncPacketPrefixKey) } // prefixKeyConstructor is a function that constructs a store key for a specific packet store using the provided // clientID. type prefixKeyConstructor func(clientID string) []byte -// getAllPacketsForClientStore gets all PacketState objects for the specified clientID using a provided +// getAllPacketsForChannelStore gets all PacketState objects for the specified clientID using a provided // function for constructing the key prefix for the store. // // For example, to get all PacketReceipts for a clientID the hostv2.PacketReceiptPrefixKey function can be // passed to get the PacketReceipt store key prefix. -func (k *Keeper) getAllPacketsForClientStore(ctx context.Context, clientID string, prefixFn prefixKeyConstructor) []types.PacketState { +func (k *Keeper) getAllPacketsForChannelStore(ctx context.Context, clientID string, prefixFn prefixKeyConstructor) []types.PacketState { store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) storePrefix := prefixFn(clientID) iterator := storetypes.KVStorePrefixIterator(store, storePrefix) diff --git a/modules/core/04-channel/v2/types/genesis.go b/modules/core/04-channel/v2/types/genesis.go index d94365b15de..aeef861000f 100644 --- a/modules/core/04-channel/v2/types/genesis.go +++ b/modules/core/04-channel/v2/types/genesis.go @@ -39,13 +39,14 @@ func (ps PacketSequence) Validate() error { // NewGenesisState creates a GenesisState instance. func NewGenesisState( - acks, receipts, commitments []PacketState, + acks, receipts, commitments, asyncPackets []PacketState, sendSeqs []PacketSequence, ) GenesisState { return GenesisState{ Acknowledgements: acks, Receipts: receipts, Commitments: commitments, + AsyncPackets: asyncPackets, SendSequences: sendSeqs, } } @@ -56,6 +57,7 @@ func DefaultGenesisState() GenesisState { Acknowledgements: []PacketState{}, Receipts: []PacketState{}, Commitments: []PacketState{}, + AsyncPackets: []PacketState{}, SendSequences: []PacketSequence{}, } } @@ -86,6 +88,15 @@ func (gs GenesisState) Validate() error { } } + for i, ap := range gs.AsyncPackets { + if err := ap.Validate(); err != nil { + return fmt.Errorf("invalid async packet %v index %d: %w", ap, i, err) + } + if len(ap.Data) == 0 { + return fmt.Errorf("invalid async packet %v index %d: data bytes cannot be empty", ap, i) + } + } + for i, ss := range gs.SendSequences { if err := ss.Validate(); err != nil { return fmt.Errorf("invalid send sequence %v index %d: %w", ss, i, err) diff --git a/modules/core/04-channel/v2/types/genesis.pb.go b/modules/core/04-channel/v2/types/genesis.pb.go index bb06101aec1..637bf7b8449 100644 --- a/modules/core/04-channel/v2/types/genesis.pb.go +++ b/modules/core/04-channel/v2/types/genesis.pb.go @@ -28,7 +28,8 @@ type GenesisState struct { Acknowledgements []PacketState `protobuf:"bytes,2,rep,name=acknowledgements,proto3" json:"acknowledgements"` Commitments []PacketState `protobuf:"bytes,3,rep,name=commitments,proto3" json:"commitments"` Receipts []PacketState `protobuf:"bytes,4,rep,name=receipts,proto3" json:"receipts"` - SendSequences []PacketSequence `protobuf:"bytes,5,rep,name=send_sequences,json=sendSequences,proto3" json:"send_sequences"` + AsyncPackets []PacketState `protobuf:"bytes,5,rep,name=async_packets,json=asyncPackets,proto3" json:"async_packets"` + SendSequences []PacketSequence `protobuf:"bytes,6,rep,name=send_sequences,json=sendSequences,proto3" json:"send_sequences"` } func (m *GenesisState) Reset() { *m = GenesisState{} } @@ -85,6 +86,13 @@ func (m *GenesisState) GetReceipts() []PacketState { return nil } +func (m *GenesisState) GetAsyncPackets() []PacketState { + if m != nil { + return m.AsyncPackets + } + return nil +} + func (m *GenesisState) GetSendSequences() []PacketSequence { if m != nil { return m.SendSequences @@ -202,31 +210,32 @@ func init() { func init() { proto.RegisterFile("ibc/core/channel/v2/genesis.proto", fileDescriptor_b5d374f126f051c3) } var fileDescriptor_b5d374f126f051c3 = []byte{ - // 370 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x92, 0x31, 0x4f, 0xfa, 0x40, - 0x18, 0xc6, 0x5b, 0xe8, 0xff, 0x1f, 0x38, 0x90, 0x98, 0xd3, 0xa1, 0xc1, 0xa4, 0x54, 0x5c, 0xba, - 0xd0, 0x33, 0xe8, 0xa2, 0x71, 0x62, 0x51, 0x36, 0x52, 0x13, 0x07, 0x17, 0x6c, 0xaf, 0x6f, 0xca, - 0x85, 0xf6, 0x0e, 0xb9, 0x03, 0xe3, 0x37, 0x70, 0xf4, 0x23, 0xf8, 0x59, 0x9c, 0x18, 0x19, 0x9d, - 0x8c, 0x81, 0x2f, 0x62, 0x68, 0x81, 0x60, 0x34, 0x26, 0xb8, 0xbd, 0xf7, 0xde, 0xf3, 0xfc, 0x9e, - 0x77, 0x78, 0xd0, 0x21, 0x0b, 0x28, 0xa1, 0x62, 0x08, 0x84, 0xf6, 0x7c, 0xce, 0x21, 0x26, 0xe3, - 0x26, 0x89, 0x80, 0x83, 0x64, 0xd2, 0x1d, 0x0c, 0x85, 0x12, 0x78, 0x8f, 0x05, 0xd4, 0x5d, 0x48, - 0xdc, 0xa5, 0xc4, 0x1d, 0x37, 0xab, 0xfb, 0x91, 0x88, 0x44, 0xfa, 0x4f, 0x16, 0x53, 0x26, 0xad, - 0xbf, 0xe6, 0x50, 0xf9, 0x32, 0x33, 0x5f, 0x2b, 0x5f, 0x01, 0xf6, 0xd0, 0xae, 0x4f, 0xfb, 0x5c, - 0x3c, 0xc4, 0x10, 0x46, 0x90, 0x00, 0x57, 0xd2, 0xcc, 0xd9, 0x79, 0xa7, 0xd4, 0xb4, 0xdd, 0x1f, - 0xb0, 0x6e, 0xc7, 0xa7, 0x7d, 0x50, 0xa9, 0xb7, 0x65, 0x4c, 0xde, 0x6b, 0x9a, 0xf7, 0xcd, 0x8f, - 0xaf, 0x50, 0x89, 0x8a, 0x24, 0x61, 0x2a, 0xc3, 0xe5, 0xb7, 0xc2, 0x6d, 0x5a, 0x71, 0x0b, 0x15, - 0x86, 0x40, 0x81, 0x0d, 0x94, 0x34, 0x8d, 0xad, 0x30, 0x6b, 0x1f, 0xee, 0xa0, 0x8a, 0x04, 0x1e, - 0x76, 0x25, 0xdc, 0x8f, 0x80, 0x53, 0x90, 0xe6, 0xbf, 0x94, 0x74, 0xf4, 0x1b, 0x69, 0xa9, 0x5d, - 0xc2, 0x76, 0x16, 0x80, 0xd5, 0x4e, 0xd6, 0xef, 0x50, 0x69, 0x23, 0x10, 0x1f, 0xa0, 0x22, 0x8d, - 0x19, 0x70, 0xd5, 0x65, 0xa1, 0xa9, 0xdb, 0xba, 0x53, 0xf4, 0x0a, 0xd9, 0xa2, 0x1d, 0xe2, 0x2a, - 0x2a, 0xac, 0x82, 0xcd, 0x9c, 0xad, 0x3b, 0x86, 0xb7, 0x7e, 0x63, 0x8c, 0x8c, 0xd0, 0x57, 0xbe, - 0x99, 0xb7, 0x75, 0xa7, 0xec, 0xa5, 0xf3, 0xb9, 0xf1, 0xf4, 0x52, 0xd3, 0xea, 0x6d, 0x54, 0xf9, - 0x7a, 0xc8, 0x9f, 0x43, 0x5a, 0x37, 0x93, 0x99, 0xa5, 0x4f, 0x67, 0x96, 0xfe, 0x31, 0xb3, 0xf4, - 0xe7, 0xb9, 0xa5, 0x4d, 0xe7, 0x96, 0xf6, 0x36, 0xb7, 0xb4, 0xdb, 0x8b, 0x88, 0xa9, 0xde, 0x28, - 0x70, 0xa9, 0x48, 0x08, 0x15, 0x32, 0x11, 0x92, 0xb0, 0x80, 0x36, 0x22, 0x41, 0xc6, 0x67, 0x24, - 0x11, 0xe1, 0x28, 0x06, 0x99, 0x35, 0xef, 0xf8, 0xb4, 0xb1, 0x51, 0x3e, 0xf5, 0x38, 0x00, 0x19, - 0xfc, 0x4f, 0x0b, 0x75, 0xf2, 0x19, 0x00, 0x00, 0xff, 0xff, 0xee, 0xb6, 0xa0, 0xcd, 0xa0, 0x02, - 0x00, 0x00, + // 393 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x92, 0x3f, 0x6f, 0xda, 0x40, + 0x18, 0xc6, 0x6d, 0xec, 0x22, 0x38, 0xfe, 0xa8, 0xba, 0x76, 0xb0, 0xa8, 0x64, 0x5c, 0xba, 0x78, + 0xc1, 0x57, 0xd1, 0x2e, 0xad, 0x3a, 0xb1, 0xb4, 0xa8, 0x0b, 0x72, 0xa5, 0x0e, 0x59, 0x88, 0x7d, + 0x7e, 0x65, 0x4e, 0xd8, 0x77, 0x0e, 0x77, 0x10, 0xf1, 0x0d, 0xb2, 0x25, 0x1f, 0x21, 0x1f, 0x87, + 0x91, 0x31, 0x53, 0x14, 0xc1, 0x17, 0x89, 0xb0, 0x01, 0x11, 0x25, 0x8a, 0x44, 0xb6, 0xd7, 0xaf, + 0x9f, 0xdf, 0xef, 0xbd, 0xe1, 0x41, 0x9f, 0x59, 0x48, 0x09, 0x15, 0x53, 0x20, 0x74, 0x1c, 0x70, + 0x0e, 0x09, 0x99, 0xf7, 0x48, 0x0c, 0x1c, 0x24, 0x93, 0x5e, 0x36, 0x15, 0x4a, 0xe0, 0x0f, 0x2c, + 0xa4, 0xde, 0x36, 0xe2, 0xed, 0x22, 0xde, 0xbc, 0xd7, 0xfa, 0x18, 0x8b, 0x58, 0xe4, 0xff, 0xc9, + 0x76, 0x2a, 0xa2, 0x9d, 0x6b, 0x03, 0xd5, 0x7f, 0x17, 0xf0, 0x3f, 0x15, 0x28, 0xc0, 0x3e, 0x7a, + 0x1f, 0xd0, 0x09, 0x17, 0x97, 0x09, 0x44, 0x31, 0xa4, 0xc0, 0x95, 0xb4, 0x4a, 0x8e, 0xe1, 0xd6, + 0x7a, 0x8e, 0xf7, 0x82, 0xd6, 0x1b, 0x06, 0x74, 0x02, 0x2a, 0x67, 0xfb, 0xe6, 0xf2, 0xbe, 0xad, + 0xf9, 0xcf, 0x78, 0xfc, 0x07, 0xd5, 0xa8, 0x48, 0x53, 0xa6, 0x0a, 0x9d, 0x71, 0x92, 0xee, 0x18, + 0xc5, 0x7d, 0x54, 0x99, 0x02, 0x05, 0x96, 0x29, 0x69, 0x99, 0x27, 0x69, 0x0e, 0x1c, 0xfe, 0x8b, + 0x1a, 0x81, 0x5c, 0x70, 0x3a, 0xca, 0xf2, 0x90, 0xb4, 0xde, 0x9d, 0x24, 0xaa, 0xe7, 0x70, 0xb1, + 0x97, 0x78, 0x88, 0x9a, 0x12, 0x78, 0x34, 0x92, 0x70, 0x31, 0x03, 0x4e, 0x41, 0x5a, 0xe5, 0xdc, + 0xf6, 0xe5, 0x35, 0xdb, 0x2e, 0xbb, 0x13, 0x36, 0xb6, 0x82, 0xfd, 0x4e, 0x76, 0xce, 0x51, 0xed, + 0xe8, 0x28, 0xfe, 0x84, 0xaa, 0x34, 0x61, 0xc0, 0xd5, 0x88, 0x45, 0x96, 0xee, 0xe8, 0x6e, 0xd5, + 0xaf, 0x14, 0x8b, 0x41, 0x84, 0x5b, 0xa8, 0xb2, 0x3f, 0x6c, 0x95, 0x1c, 0xdd, 0x35, 0xfd, 0xc3, + 0x37, 0xc6, 0xc8, 0x8c, 0x02, 0x15, 0x58, 0x86, 0xa3, 0xbb, 0x75, 0x3f, 0x9f, 0x7f, 0x9a, 0x57, + 0xb7, 0x6d, 0xad, 0x33, 0x40, 0xcd, 0xa7, 0x0f, 0x79, 0xf3, 0x91, 0xfe, 0xff, 0xe5, 0xda, 0xd6, + 0x57, 0x6b, 0x5b, 0x7f, 0x58, 0xdb, 0xfa, 0xcd, 0xc6, 0xd6, 0x56, 0x1b, 0x5b, 0xbb, 0xdb, 0xd8, + 0xda, 0xd9, 0xaf, 0x98, 0xa9, 0xf1, 0x2c, 0xf4, 0xa8, 0x48, 0x09, 0x15, 0x32, 0x15, 0x92, 0xb0, + 0x90, 0x76, 0x63, 0x41, 0xe6, 0x3f, 0x48, 0x2a, 0xa2, 0x59, 0x02, 0xb2, 0xa8, 0xf1, 0xd7, 0xef, + 0xdd, 0xa3, 0x26, 0xab, 0x45, 0x06, 0x32, 0x2c, 0xe7, 0xed, 0xfc, 0xf6, 0x18, 0x00, 0x00, 0xff, + 0xff, 0xc5, 0xf3, 0x8b, 0x75, 0xed, 0x02, 0x00, 0x00, } func (m *GenesisState) Marshal() (dAtA []byte, err error) { @@ -260,6 +269,20 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { i = encodeVarintGenesis(dAtA, i, uint64(size)) } i-- + dAtA[i] = 0x32 + } + } + if len(m.AsyncPackets) > 0 { + for iNdEx := len(m.AsyncPackets) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.AsyncPackets[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintGenesis(dAtA, i, uint64(size)) + } + i-- dAtA[i] = 0x2a } } @@ -420,6 +443,12 @@ func (m *GenesisState) Size() (n int) { n += 1 + l + sovGenesis(uint64(l)) } } + if len(m.AsyncPackets) > 0 { + for _, e := range m.AsyncPackets { + l = e.Size() + n += 1 + l + sovGenesis(uint64(l)) + } + } if len(m.SendSequences) > 0 { for _, e := range m.SendSequences { l = e.Size() @@ -603,6 +632,40 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { } iNdEx = postIndex case 5: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field AsyncPackets", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.AsyncPackets = append(m.AsyncPackets, PacketState{}) + if err := m.AsyncPackets[len(m.AsyncPackets)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 6: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field SendSequences", wireType) } diff --git a/modules/core/04-channel/v2/types/genesis_test.go b/modules/core/04-channel/v2/types/genesis_test.go index 7b22f9c5719..d539c19a262 100644 --- a/modules/core/04-channel/v2/types/genesis_test.go +++ b/modules/core/04-channel/v2/types/genesis_test.go @@ -27,6 +27,7 @@ func TestValidateGenesis(t *testing.T) { []types.PacketState{types.NewPacketState(ibctesting.FirstChannelID, 1, []byte("ack"))}, []types.PacketState{types.NewPacketState(ibctesting.SecondChannelID, 1, []byte(""))}, []types.PacketState{types.NewPacketState(ibctesting.FirstChannelID, 1, []byte("commit_hash"))}, + []types.PacketState{types.NewPacketState(ibctesting.SecondChannelID, 1, []byte("async_packet"))}, []types.PacketSequence{types.NewPacketSequence(ibctesting.SecondChannelID, 1)}, ), nil, @@ -49,6 +50,15 @@ func TestValidateGenesis(t *testing.T) { }, errors.New("data bytes cannot be nil"), }, + { + "invalid async packet", + types.GenesisState{ + AsyncPackets: []types.PacketState{ + types.NewPacketState(ibctesting.FirstChannelID, 1, nil), + }, + }, + errors.New("data bytes cannot be nil"), + }, { "invalid send seq", types.GenesisState{ diff --git a/modules/core/04-channel/v2/types/keys.go b/modules/core/04-channel/v2/types/keys.go index 68a55d0e073..c1a1eca7019 100644 --- a/modules/core/04-channel/v2/types/keys.go +++ b/modules/core/04-channel/v2/types/keys.go @@ -1,6 +1,8 @@ package types -import "fmt" +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) const ( // SubModuleName defines the channelv2 module name. @@ -13,5 +15,11 @@ const ( // AsyncPacketKey returns the key under which the packet is stored // if the receiving application returns an async acknowledgement. func AsyncPacketKey(clientID string, sequence uint64) []byte { - return []byte(fmt.Sprintf("%s/%s/%d", KeyAsyncPacket, clientID, sequence)) + return append(AsyncPacketPrefixKey(clientID), sdk.Uint64ToBigEndian(sequence)...) +} + +// AsyncPacketPrefixKey returns the prefix key under which all async packets are stored +// for a given clientID. +func AsyncPacketPrefixKey(clientID string) []byte { + return append([]byte(clientID), []byte(KeyAsyncPacket)...) } diff --git a/modules/core/genesis_test.go b/modules/core/genesis_test.go index 8d07cb5acd4..f963ce1664a 100644 --- a/modules/core/genesis_test.go +++ b/modules/core/genesis_test.go @@ -5,6 +5,8 @@ import ( "fmt" "testing" + proto "github.com/cosmos/gogoproto/proto" + testifysuite "github.com/stretchr/testify/suite" "github.com/cosmos/cosmos-sdk/codec" @@ -19,6 +21,7 @@ import ( "github.com/cosmos/ibc-go/v9/modules/core/types" ibctm "github.com/cosmos/ibc-go/v9/modules/light-clients/07-tendermint" ibctesting "github.com/cosmos/ibc-go/v9/testing" + mockv2 "github.com/cosmos/ibc-go/v9/testing/mock/v2" "github.com/cosmos/ibc-go/v9/testing/simapp" ) @@ -156,6 +159,9 @@ func (suite *IBCTestSuite) TestValidateGenesis() { []channelv2types.PacketState{ channelv2types.NewPacketState(channel1, 1, []byte("commit_hash")), }, + []channelv2types.PacketState{ + channelv2types.NewPacketState(channel2, 1, []byte("async_packet")), + }, []channelv2types.PacketSequence{ channelv2types.NewPacketSequence(channel1, 1), }, @@ -254,6 +260,13 @@ func (suite *IBCTestSuite) TestValidateGenesis() { func (suite *IBCTestSuite) TestInitGenesis() { header := suite.chainA.CreateTMClientHeader(suite.chainA.ChainID, suite.chainA.ProposedHeader.Height, clienttypes.NewHeight(0, uint64(suite.chainA.ProposedHeader.Height-1)), suite.chainA.ProposedHeader.Time, suite.chainA.Vals, suite.chainA.Vals, suite.chainA.Vals, suite.chainA.Signers) + packet := channelv2types.NewPacket( + 1, "07-tendermint-0", "07-tendermint-1", + uint64(suite.chainA.GetContext().BlockTime().Unix()), mockv2.NewMockPayload("src", "dst"), + ) + bz, err := proto.Marshal(&packet) + suite.Require().NoError(err) + testCases := []struct { name string genState *types.GenesisState @@ -347,6 +360,9 @@ func (suite *IBCTestSuite) TestInitGenesis() { []channelv2types.PacketState{ channelv2types.NewPacketState(channel1, 1, []byte("commit_hash")), }, + []channelv2types.PacketState{ + channelv2types.NewPacketState(channel2, 1, bz), + }, []channelv2types.PacketSequence{ channelv2types.NewPacketSequence(channel1, 1), }, diff --git a/proto/ibc/core/channel/v2/genesis.proto b/proto/ibc/core/channel/v2/genesis.proto index cb32dca06ea..b7ac3f6f467 100644 --- a/proto/ibc/core/channel/v2/genesis.proto +++ b/proto/ibc/core/channel/v2/genesis.proto @@ -11,7 +11,8 @@ message GenesisState { repeated PacketState acknowledgements = 2 [(gogoproto.nullable) = false]; repeated PacketState commitments = 3 [(gogoproto.nullable) = false]; repeated PacketState receipts = 4 [(gogoproto.nullable) = false]; - repeated PacketSequence send_sequences = 5 [(gogoproto.nullable) = false]; + repeated PacketState async_packets = 5 [(gogoproto.nullable) = false]; + repeated PacketSequence send_sequences = 6 [(gogoproto.nullable) = false]; } // PacketState defines the generic type necessary to retrieve and store From b465c8d260e7c8ad4dd6d23ffffb541dc6092828 Mon Sep 17 00:00:00 2001 From: Aditya Sripal <14364734+AdityaSripal@users.noreply.github.com> Date: Mon, 10 Feb 2025 20:46:42 -0500 Subject: [PATCH 2/2] lint and name change --- modules/core/04-channel/v2/keeper/keeper.go | 12 ++++++------ modules/core/genesis_test.go | 1 - 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/modules/core/04-channel/v2/keeper/keeper.go b/modules/core/04-channel/v2/keeper/keeper.go index 0e112466cf5..916feaaf8f9 100644 --- a/modules/core/04-channel/v2/keeper/keeper.go +++ b/modules/core/04-channel/v2/keeper/keeper.go @@ -213,37 +213,37 @@ func extractSequenceFromKey(key, storePrefix []byte) uint64 { // GetAllPacketCommitmentsForClient returns all stored PacketCommitments objects for a specified // client ID. func (k *Keeper) GetAllPacketCommitmentsForClient(ctx context.Context, clientID string) []types.PacketState { - return k.getAllPacketsForChannelStore(ctx, clientID, hostv2.PacketCommitmentPrefixKey) + return k.getAllPacketStateForClient(ctx, clientID, hostv2.PacketCommitmentPrefixKey) } // GetAllPacketAcknowledgementsForClient returns all stored PacketAcknowledgements objects for a specified // client ID. func (k *Keeper) GetAllPacketAcknowledgementsForClient(ctx context.Context, clientID string) []types.PacketState { - return k.getAllPacketsForChannelStore(ctx, clientID, hostv2.PacketAcknowledgementPrefixKey) + return k.getAllPacketStateForClient(ctx, clientID, hostv2.PacketAcknowledgementPrefixKey) } // GetAllPacketReceiptsForClient returns all stored PacketReceipts objects for a specified // client ID. func (k *Keeper) GetAllPacketReceiptsForClient(ctx context.Context, clientID string) []types.PacketState { - return k.getAllPacketsForChannelStore(ctx, clientID, hostv2.PacketReceiptPrefixKey) + return k.getAllPacketStateForClient(ctx, clientID, hostv2.PacketReceiptPrefixKey) } // GetAllAsyncPacketsForClient returns all stored AsyncPackets objects for a specified // client ID. func (k *Keeper) GetAllAsyncPacketsForClient(ctx context.Context, clientID string) []types.PacketState { - return k.getAllPacketsForChannelStore(ctx, clientID, types.AsyncPacketPrefixKey) + return k.getAllPacketStateForClient(ctx, clientID, types.AsyncPacketPrefixKey) } // prefixKeyConstructor is a function that constructs a store key for a specific packet store using the provided // clientID. type prefixKeyConstructor func(clientID string) []byte -// getAllPacketsForChannelStore gets all PacketState objects for the specified clientID using a provided +// getAllPacketStateForClient gets all PacketState objects for the specified clientID using a provided // function for constructing the key prefix for the store. // // For example, to get all PacketReceipts for a clientID the hostv2.PacketReceiptPrefixKey function can be // passed to get the PacketReceipt store key prefix. -func (k *Keeper) getAllPacketsForChannelStore(ctx context.Context, clientID string, prefixFn prefixKeyConstructor) []types.PacketState { +func (k *Keeper) getAllPacketStateForClient(ctx context.Context, clientID string, prefixFn prefixKeyConstructor) []types.PacketState { store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)) storePrefix := prefixFn(clientID) iterator := storetypes.KVStorePrefixIterator(store, storePrefix) diff --git a/modules/core/genesis_test.go b/modules/core/genesis_test.go index f963ce1664a..43a04a52f21 100644 --- a/modules/core/genesis_test.go +++ b/modules/core/genesis_test.go @@ -6,7 +6,6 @@ import ( "testing" proto "github.com/cosmos/gogoproto/proto" - testifysuite "github.com/stretchr/testify/suite" "github.com/cosmos/cosmos-sdk/codec"