Skip to content

Commit

Permalink
fix(transfer): add forwarded packets to genesis. (#6861)
Browse files Browse the repository at this point in the history
* fix: add forwarded packets to genesis.

* nit: add comment, add if for checking part is as expected.
  • Loading branch information
DimitrisJim committed Aug 1, 2024
1 parent 314bbc5 commit b8f944e
Show file tree
Hide file tree
Showing 8 changed files with 455 additions and 32 deletions.
10 changes: 10 additions & 0 deletions modules/apps/transfer/keeper/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 11 additions & 4 deletions modules/apps/transfer/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
18 changes: 18 additions & 0 deletions modules/apps/transfer/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
}
48 changes: 48 additions & 0 deletions modules/apps/transfer/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
32 changes: 32 additions & 0 deletions modules/apps/transfer/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit b8f944e

Please sign in to comment.