diff --git a/modules/apps/transfer/keeper/export_test.go b/modules/apps/transfer/keeper/export_test.go index 468a1ddda38..399fd3967cf 100644 --- a/modules/apps/transfer/keeper/export_test.go +++ b/modules/apps/transfer/keeper/export_test.go @@ -44,6 +44,16 @@ func (k Keeper) GetForwardedPacket(ctx sdk.Context, portID, channelID string, se return k.getForwardedPacket(ctx, portID, channelID, sequence) } +// SetForwardedPacket is a wrapper around setForwardedPacket for testing purposes. +func (k Keeper) SetForwardedPacket(ctx sdk.Context, portID, channelID string, sequence uint64, packet channeltypes.Packet) { + k.setForwardedPacket(ctx, portID, channelID, sequence, packet) +} + +// GetAllForwardedPackets is a wrapper around getAllForwardedPackets for testing purposes. +func (k Keeper) GetAllForwardedPackets(ctx sdk.Context) []types.ForwardedPacket { + return k.getAllForwardedPackets(ctx) +} + // IsBlockedAddr is a wrapper around isBlockedAddr for testing purposes func (k Keeper) IsBlockedAddr(addr sdk.AccAddress) bool { return k.isBlockedAddr(addr) diff --git a/modules/apps/transfer/keeper/genesis.go b/modules/apps/transfer/keeper/genesis.go index e0267512753..4239bf3afd8 100644 --- a/modules/apps/transfer/keeper/genesis.go +++ b/modules/apps/transfer/keeper/genesis.go @@ -35,14 +35,21 @@ func (k Keeper) InitGenesis(ctx sdk.Context, state types.GenesisState) { for _, denomEscrow := range state.TotalEscrowed { k.SetTotalEscrowForDenom(ctx, denomEscrow) } + + // Set any forwarded packets imported. + for _, forwardPacketState := range state.ForwardedPackets { + forwardKey := forwardPacketState.ForwardKey + k.setForwardedPacket(ctx, forwardKey.PortId, forwardKey.ChannelId, forwardKey.Sequence, forwardPacketState.Packet) + } } // ExportGenesis exports ibc-transfer module's portID and denom trace info into its genesis state. func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState { return &types.GenesisState{ - PortId: k.GetPort(ctx), - Denoms: k.GetAllDenoms(ctx), - Params: k.GetParams(ctx), - TotalEscrowed: k.GetAllTotalEscrowed(ctx), + PortId: k.GetPort(ctx), + Denoms: k.GetAllDenoms(ctx), + Params: k.GetParams(ctx), + TotalEscrowed: k.GetAllTotalEscrowed(ctx), + ForwardedPackets: k.getAllForwardedPackets(ctx), } } diff --git a/modules/apps/transfer/keeper/genesis_test.go b/modules/apps/transfer/keeper/genesis_test.go index 61fa75ebf3c..2a44b65610d 100644 --- a/modules/apps/transfer/keeper/genesis_test.go +++ b/modules/apps/transfer/keeper/genesis_test.go @@ -8,6 +8,9 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/ibc-go/v9/modules/apps/transfer/types" + clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types" + channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types" + ibctesting "github.com/cosmos/ibc-go/v9/testing" ) func (suite *KeeperTestSuite) TestGenesis() { @@ -28,6 +31,7 @@ func (suite *KeeperTestSuite) TestGenesis() { {[]types.Hop{getHop(3), getHop(2), getHop(1), getHop(0)}, "1000000000000000"}, {[]types.Hop{getHop(4), getHop(3), getHop(2), getHop(1), getHop(0)}, "100000000000000000000"}, } + forwardPackets []types.ForwardedPacket ) for _, traceAndEscrowAmount := range traceAndEscrowAmounts { @@ -42,6 +46,17 @@ func (suite *KeeperTestSuite) TestGenesis() { suite.chainA.GetSimApp().TransferKeeper.SetTotalEscrowForDenom(suite.chainA.GetContext(), escrow) } + // Store forward packets on transfer/channel-1 and transfer/channel-2 + for _, channelID := range []string{"channel-1", "channel-2"} { + // go across '10' to test numerical order + for sequence := uint64(5); sequence <= 15; sequence++ { + packet := channeltypes.NewPacket(ibctesting.MockPacketData, sequence, ibctesting.TransferPort, channelID, "", "", clienttypes.ZeroHeight(), 0) + forwardPackets = append(forwardPackets, types.ForwardedPacket{ForwardKey: channeltypes.NewPacketID(ibctesting.TransferPort, channelID, sequence), Packet: packet}) + + suite.chainA.GetSimApp().TransferKeeper.SetForwardedPacket(suite.chainA.GetContext(), ibctesting.TransferPort, channelID, sequence, packet) + } + } + genesis := suite.chainA.GetSimApp().TransferKeeper.ExportGenesis(suite.chainA.GetContext()) suite.Require().Equal(types.PortID, genesis.PortId) @@ -56,4 +71,7 @@ func (suite *KeeperTestSuite) TestGenesis() { _, found := suite.chainA.GetSimApp().BankKeeper.GetDenomMetaData(suite.chainA.GetContext(), denom.IBCDenom()) suite.Require().True(found) } + + storedForwardedPackets := suite.chainA.GetSimApp().TransferKeeper.GetAllForwardedPackets(suite.chainA.GetContext()) + suite.Require().Equal(storedForwardedPackets, forwardPackets) } diff --git a/modules/apps/transfer/keeper/keeper.go b/modules/apps/transfer/keeper/keeper.go index 997d1e7b83f..0ce1399513d 100644 --- a/modules/apps/transfer/keeper/keeper.go +++ b/modules/apps/transfer/keeper/keeper.go @@ -337,6 +337,54 @@ func (k Keeper) deleteForwardedPacket(ctx sdk.Context, portID, channelID string, store.Delete(packetKey) } +// getAllForwardedPackets gets all forward packets stored in state. +func (k Keeper) getAllForwardedPackets(ctx sdk.Context) []types.ForwardedPacket { + var packets []types.ForwardedPacket + k.iterateForwardedPackets(ctx, func(packet types.ForwardedPacket) bool { + packets = append(packets, packet) + return false + }) + + return packets +} + +// iterateForwardedPackets iterates over the forward packets in the store and performs a callback function. +func (k Keeper) iterateForwardedPackets(ctx sdk.Context, cb func(packet types.ForwardedPacket) bool) { + store := ctx.KVStore(k.storeKey) + iterator := storetypes.KVStorePrefixIterator(store, types.ForwardedPacketKey) + + defer sdk.LogDeferred(ctx.Logger(), func() error { return iterator.Close() }) + for ; iterator.Valid(); iterator.Next() { + var forwardPacket types.ForwardedPacket + k.cdc.MustUnmarshal(iterator.Value(), &forwardPacket.Packet) + + // Iterator key consists of types.ForwardedPacketKey/portID/channelID/sequence + parts := strings.Split(string(iterator.Key()), "/") + if len(parts) != 4 { + panic(fmt.Errorf("key path should always have 4 elements")) + } + if parts[0] != string(types.ForwardedPacketKey) { + panic(fmt.Errorf("key path does not start with expected prefix: %s", types.ForwardedPacketKey)) + } + + portID, channelID := parts[1], parts[2] + if err := host.PortIdentifierValidator(portID); err != nil { + panic(fmt.Errorf("port identifier validation failed while parsing forward key path")) + } + if err := host.ChannelIdentifierValidator(channelID); err != nil { + panic(fmt.Errorf("channel identifier validation failed while parsing forward key path")) + } + + forwardPacket.ForwardKey.Sequence = sdk.BigEndianToUint64([]byte(parts[3])) + forwardPacket.ForwardKey.ChannelId = channelID + forwardPacket.ForwardKey.PortId = portID + + if cb(forwardPacket) { + break + } + } +} + // IsBlockedAddr checks if the given address is allowed to send or receive tokens. // The module account is always allowed to send and receive tokens. func (k Keeper) isBlockedAddr(addr sdk.AccAddress) bool { diff --git a/modules/apps/transfer/keeper/keeper_test.go b/modules/apps/transfer/keeper/keeper_test.go index ce8a7dcd1d7..683bf8175ab 100644 --- a/modules/apps/transfer/keeper/keeper_test.go +++ b/modules/apps/transfer/keeper/keeper_test.go @@ -17,7 +17,9 @@ import ( "github.com/cosmos/ibc-go/v9/modules/apps/transfer/keeper" "github.com/cosmos/ibc-go/v9/modules/apps/transfer/types" + clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types" channelkeeper "github.com/cosmos/ibc-go/v9/modules/core/04-channel/keeper" + channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types" ibctesting "github.com/cosmos/ibc-go/v9/testing" ) @@ -291,6 +293,36 @@ func (suite *KeeperTestSuite) TestGetAllDenomEscrows() { } } +func (suite *KeeperTestSuite) TestGetAllForwardedPackets() { + suite.SetupTest() + + // Store forward packets on transfer/channel-1 and transfer/channel-2 + for _, channelID := range []string{"channel-1", "channel-2"} { + // go across '10' to test numerical order + for sequence := uint64(5); sequence <= 15; sequence++ { + packet := channeltypes.NewPacket(ibctesting.MockPacketData, sequence, ibctesting.TransferPort, channelID, "", "", clienttypes.ZeroHeight(), 0) + suite.chainA.GetSimApp().TransferKeeper.SetForwardedPacket(suite.chainA.GetContext(), ibctesting.TransferPort, channelID, sequence, packet) + } + } + + packets := suite.chainA.GetSimApp().TransferKeeper.GetAllForwardedPackets(suite.chainA.GetContext()) + // Assert each packets is as expected + i := 0 + for _, channelID := range []string{"channel-1", "channel-2"} { + for sequence := uint64(5); sequence <= 15; sequence++ { + forwardedPacket := packets[i] + + expForwardKey := channeltypes.NewPacketID(ibctesting.TransferPort, channelID, sequence) + suite.Require().Equal(forwardedPacket.ForwardKey, expForwardKey) + + expPacket := channeltypes.NewPacket(ibctesting.MockPacketData, sequence, ibctesting.TransferPort, channelID, "", "", clienttypes.ZeroHeight(), 0) + suite.Require().Equal(forwardedPacket.Packet, expPacket) + + i++ + } + } +} + func (suite *KeeperTestSuite) TestParams() { testCases := []struct { name string diff --git a/modules/apps/transfer/types/genesis.pb.go b/modules/apps/transfer/types/genesis.pb.go index 0fb1afc6fe9..e2ca67140b9 100644 --- a/modules/apps/transfer/types/genesis.pb.go +++ b/modules/apps/transfer/types/genesis.pb.go @@ -9,6 +9,7 @@ import ( types "github.com/cosmos/cosmos-sdk/types" _ "github.com/cosmos/gogoproto/gogoproto" proto "github.com/cosmos/gogoproto/proto" + types1 "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types" io "io" math "math" math_bits "math/bits" @@ -33,6 +34,9 @@ type GenesisState struct { // total_escrowed contains the total amount of tokens escrowed // by the transfer module TotalEscrowed github_com_cosmos_cosmos_sdk_types.Coins `protobuf:"bytes,4,rep,name=total_escrowed,json=totalEscrowed,proto3,castrepeated=github.com/cosmos/cosmos-sdk/types.Coins" json:"total_escrowed"` + // forwarded_packets contains the forwarded packets stored as part of the + // packet forwarding lifecycle + ForwardedPackets []ForwardedPacket `protobuf:"bytes,5,rep,name=forwarded_packets,json=forwardedPackets,proto3" json:"forwarded_packets"` } func (m *GenesisState) Reset() { *m = GenesisState{} } @@ -96,8 +100,69 @@ func (m *GenesisState) GetTotalEscrowed() github_com_cosmos_cosmos_sdk_types.Coi return nil } +func (m *GenesisState) GetForwardedPackets() []ForwardedPacket { + if m != nil { + return m.ForwardedPackets + } + return nil +} + +// ForwardedPacket defines the genesis type necessary to retrieve and store forwarded packets. +type ForwardedPacket struct { + ForwardKey types1.PacketId `protobuf:"bytes,1,opt,name=forward_key,json=forwardKey,proto3" json:"forward_key"` + Packet types1.Packet `protobuf:"bytes,2,opt,name=packet,proto3" json:"packet"` +} + +func (m *ForwardedPacket) Reset() { *m = ForwardedPacket{} } +func (m *ForwardedPacket) String() string { return proto.CompactTextString(m) } +func (*ForwardedPacket) ProtoMessage() {} +func (*ForwardedPacket) Descriptor() ([]byte, []int) { + return fileDescriptor_62efebb47a9093ed, []int{1} +} +func (m *ForwardedPacket) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *ForwardedPacket) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_ForwardedPacket.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *ForwardedPacket) XXX_Merge(src proto.Message) { + xxx_messageInfo_ForwardedPacket.Merge(m, src) +} +func (m *ForwardedPacket) XXX_Size() int { + return m.Size() +} +func (m *ForwardedPacket) XXX_DiscardUnknown() { + xxx_messageInfo_ForwardedPacket.DiscardUnknown(m) +} + +var xxx_messageInfo_ForwardedPacket proto.InternalMessageInfo + +func (m *ForwardedPacket) GetForwardKey() types1.PacketId { + if m != nil { + return m.ForwardKey + } + return types1.PacketId{} +} + +func (m *ForwardedPacket) GetPacket() types1.Packet { + if m != nil { + return m.Packet + } + return types1.Packet{} +} + func init() { proto.RegisterType((*GenesisState)(nil), "ibc.applications.transfer.v2.GenesisState") + proto.RegisterType((*ForwardedPacket)(nil), "ibc.applications.transfer.v2.ForwardedPacket") } func init() { @@ -105,31 +170,38 @@ func init() { } var fileDescriptor_62efebb47a9093ed = []byte{ - // 378 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x51, 0xc1, 0x4e, 0xdb, 0x40, - 0x10, 0xb5, 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31, 0xcb, 0x20, 0x15, 0x76, 0x67, 0xdc, 0x75, - 0xfb, 0xfe, 0x6f, 0xd4, 0xf6, 0x4b, 0x74, 0x54, 0x61, 0x83, 0xc1, 0xf2, 0x79, 0xa4, 0x3d, 0xbc, - 0x8c, 0x0c, 0xd5, 0x8a, 0xb0, 0xa1, 0xb0, 0x02, 0xd3, 0xc8, 0x49, 0x41, 0x52, 0x61, 0x77, 0xc7, - 0xba, 0xdb, 0xf7, 0xff, 0xb4, 0x91, 0x79, 0xe8, 0x5c, 0x61, 0x83, 0x5e, 0xc5, 0x16, 0x36, 0x93, - 0x56, 0x61, 0x0e, 0x24, 0x48, 0x32, 0x9f, 0x32, 0x41, 0x0b, 0xb8, 0x65, 0xb1, 0xdd, 0x53, 0x8b, - 0xfd, 0x40, 0xb5, 0x13, 0xa8, 0x72, 0x02, 0x35, 0x4e, 0xa0, 0x43, 0xe0, 0x59, 0xf0, 0xbf, 0x59, - 0xc7, 0x4d, 0xb8, 0xbc, 0x59, 0x44, 0x88, 0x42, 0x8a, 0x1b, 0xdb, 0xea, 0x63, 0x22, 0xe2, 0x19, - 0x96, 0x77, 0x39, 0x13, 0x6a, 0x40, 0x84, 0x5f, 0x95, 0xc4, 0x71, 0xa3, 0x10, 0x5c, 0x2c, 0xd7, - 0x8e, 0xbe, 0x5a, 0x3b, 0xfa, 0xeb, 0xda, 0xd1, 0xef, 0x37, 0x8e, 0xb6, 0xda, 0x38, 0xda, 0xd3, - 0xc6, 0xd1, 0xae, 0xf6, 0x3f, 0x52, 0xf2, 0x88, 0x4e, 0x12, 0xc0, 0xe5, 0x01, 0x4e, 0x21, 0x5e, - 0xcc, 0x99, 0xa8, 0x82, 0xdc, 0x09, 0x50, 0xe9, 0x44, 0x86, 0x0a, 0x62, 0xef, 0x2d, 0x00, 0x00, - 0xff, 0xff, 0x87, 0x4d, 0x84, 0xc3, 0x61, 0x02, 0x00, 0x00, + // 485 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0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58, 0xef, + 0x7d, 0x09, 0xd0, 0xcd, 0x7f, 0xb4, 0xd1, 0x01, 0xea, 0x7a, 0xdd, 0x78, 0x2a, 0x16, 0xee, 0xbb, + 0x74, 0xb3, 0x87, 0x6e, 0x9e, 0xdd, 0x09, 0xbc, 0x5e, 0x04, 0x47, 0xca, 0x3a, 0x0e, 0xb9, 0xcf, + 0x47, 0xde, 0x77, 0x24, 0x16, 0xd1, 0xbe, 0x65, 0x6e, 0xff, 0x8d, 0x37, 0x5c, 0xc0, 0xfd, 0x96, + 0x80, 0x3f, 0xa8, 0x5d, 0xf7, 0xee, 0x7c, 0x99, 0x04, 0x17, 0xcb, 0x24, 0xf8, 0xb9, 0x4c, 0x82, + 0xcf, 0xab, 0xa4, 0x73, 0xb1, 0x4a, 0x3a, 0xdf, 0x56, 0x49, 0xe7, 0xc3, 0xcb, 0xff, 0x49, 0xca, + 0x9c, 0xf5, 0x0b, 0x20, 0xf3, 0x7d, 0x52, 0x02, 0x3f, 0x9d, 0x09, 0x6d, 0x57, 0xfc, 0xd2, 0x6a, + 0x3b, 0xbc, 0x79, 0xe8, 0xf6, 0xef, 0xc5, 0xaf, 0x00, 0x00, 0x00, 0xff, 0xff, 0xfb, 0x97, 0x02, + 0x93, 0x7b, 0x03, 0x00, 0x00, } func (m *GenesisState) Marshal() (dAtA []byte, err error) { @@ -152,6 +224,20 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if len(m.ForwardedPackets) > 0 { + for iNdEx := len(m.ForwardedPackets) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.ForwardedPackets[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintGenesis(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x2a + } + } if len(m.TotalEscrowed) > 0 { for iNdEx := len(m.TotalEscrowed) - 1; iNdEx >= 0; iNdEx-- { { @@ -200,6 +286,49 @@ func (m *GenesisState) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } +func (m *ForwardedPacket) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *ForwardedPacket) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *ForwardedPacket) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + { + size, err := m.Packet.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintGenesis(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + { + size, err := m.ForwardKey.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintGenesis(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0xa + return len(dAtA) - i, nil +} + func encodeVarintGenesis(dAtA []byte, offset int, v uint64) int { offset -= sovGenesis(v) base := offset @@ -235,6 +364,25 @@ func (m *GenesisState) Size() (n int) { n += 1 + l + sovGenesis(uint64(l)) } } + if len(m.ForwardedPackets) > 0 { + for _, e := range m.ForwardedPackets { + l = e.Size() + n += 1 + l + sovGenesis(uint64(l)) + } + } + return n +} + +func (m *ForwardedPacket) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = m.ForwardKey.Size() + n += 1 + l + sovGenesis(uint64(l)) + l = m.Packet.Size() + n += 1 + l + sovGenesis(uint64(l)) return n } @@ -406,6 +554,156 @@ func (m *GenesisState) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 5: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field ForwardedPackets", 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.ForwardedPackets = append(m.ForwardedPackets, ForwardedPacket{}) + if err := m.ForwardedPackets[len(m.ForwardedPackets)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipGenesis(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthGenesis + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func (m *ForwardedPacket) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: ForwardedPacket: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: ForwardedPacket: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field ForwardKey", 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 + } + if err := m.ForwardKey.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Packet", 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 + } + if err := m.Packet.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipGenesis(dAtA[iNdEx:]) diff --git a/modules/apps/transfer/types/keys.go b/modules/apps/transfer/types/keys.go index 34369e3b051..23a53f2ea46 100644 --- a/modules/apps/transfer/types/keys.go +++ b/modules/apps/transfer/types/keys.go @@ -53,8 +53,8 @@ var ( DenomTraceKey = []byte{0x02} // DenomKey defines the key to store the token denomination in store DenomKey = []byte{0x03} - // forwardPacketKey defines the key to store the forwarded packet in store - forwardPacketKey = []byte{0x04} + // ForwardedPacketKey defines the key to store the forwarded packet in store + ForwardedPacketKey = []byte{0x04} // SupportedVersions defines all versions that are supported by the module SupportedVersions = []string{V2, V1} @@ -85,5 +85,5 @@ func TotalEscrowForDenomKey(denom string) []byte { // PacketForwardKey returns the store key under which the forwarded packet is stored // for the provided portID, channelID, and packet sequence. func PacketForwardKey(portID, channelID string, sequence uint64) []byte { - return []byte(fmt.Sprintf("%s/%s/%s/%s", forwardPacketKey, portID, channelID, sdk.Uint64ToBigEndian(sequence))) + return []byte(fmt.Sprintf("%s/%s/%s/%s", ForwardedPacketKey, portID, channelID, sdk.Uint64ToBigEndian(sequence))) } diff --git a/proto/ibc/applications/transfer/v2/genesis.proto b/proto/ibc/applications/transfer/v2/genesis.proto index 3ebc3559e96..6b3545d2f39 100644 --- a/proto/ibc/applications/transfer/v2/genesis.proto +++ b/proto/ibc/applications/transfer/v2/genesis.proto @@ -6,6 +6,7 @@ option go_package = "github.com/cosmos/ibc-go/v9/modules/apps/transfer/types"; import "ibc/applications/transfer/v1/transfer.proto"; import "ibc/applications/transfer/v2/token.proto"; +import "ibc/core/channel/v1/channel.proto"; import "cosmos/base/v1beta1/coin.proto"; import "gogoproto/gogo.proto"; @@ -18,4 +19,13 @@ message GenesisState { // by the transfer module repeated cosmos.base.v1beta1.Coin total_escrowed = 4 [(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins", (gogoproto.nullable) = false]; + // forwarded_packets contains the forwarded packets stored as part of the + // packet forwarding lifecycle + repeated ForwardedPacket forwarded_packets = 5 [(gogoproto.nullable) = false]; +} + +// ForwardedPacket defines the genesis type necessary to retrieve and store forwarded packets. +message ForwardedPacket { + ibc.core.channel.v1.PacketId forward_key = 1 [(gogoproto.nullable) = false]; + ibc.core.channel.v1.Packet packet = 2 [(gogoproto.nullable) = false]; }