diff --git a/x/interchainstaking/keeper/ibc_packet_handlers.go b/x/interchainstaking/keeper/ibc_packet_handlers.go index 6974f403f..e4c22ab10 100644 --- a/x/interchainstaking/keeper/ibc_packet_handlers.go +++ b/x/interchainstaking/keeper/ibc_packet_handlers.go @@ -314,14 +314,25 @@ func (k *Keeper) HandleMsgTransfer(ctx sdk.Context, msg sdk.Msg) error { receivedCoin := sMsg.Token zone, found := k.GetZoneForWithdrawalAccount(ctx, sMsg.Sender) + if !found { + return fmt.Errorf("zone not found for withdrawal account %s", sMsg.Sender) + } - channel, cfound := k.IBCKeeper.ChannelKeeper.GetChannel(ctx, sMsg.SourcePort, sMsg.SourceChannel) - if !cfound { + var channel *channeltypes.IdentifiedChannel + k.IBCKeeper.ChannelKeeper.IterateChannels(ctx, func(ic channeltypes.IdentifiedChannel) bool { + if ic.Counterparty.ChannelId == sMsg.SourceChannel && ic.Counterparty.PortId == sMsg.SourcePort && len(ic.ConnectionHops) == 1 && ic.ConnectionHops[0] == zone.ConnectionId { + channel = &ic + return true + } + return false + }) + + if channel == nil { k.Logger(ctx).Error("channel not found for the packet", "port", sMsg.SourcePort, "channel", sMsg.SourceChannel) return errors.New("channel not found for the packet") } - denomTrace := utils.DeriveIbcDenomTrace(channel.Counterparty.PortId, channel.Counterparty.ChannelId, receivedCoin.Denom) + denomTrace := utils.DeriveIbcDenomTrace(channel.PortId, channel.ChannelId, receivedCoin.Denom) receivedCoin.Denom = denomTrace.IBCDenom() if found && denomTrace.BaseDenom != zone.BaseDenom { @@ -374,7 +385,7 @@ func (k *Keeper) HandleCompleteSend(ctx sdk.Context, msg sdk.Msg, memo string) e case zone.IsDelegateAddress(sMsg.ToAddress) && zone.DepositAddress.Address == sMsg.FromAddress: return k.handleSendToDelegate(ctx, zone, sMsg, memo) default: - err = errors.New("unexpected completed send") + err = fmt.Errorf("unexpected completed send (2) from %s to %s (amount: %s)", sMsg.FromAddress, sMsg.ToAddress, sMsg.Amount) k.Logger(ctx).Error(err.Error()) return err } @@ -758,9 +769,9 @@ func (k *Keeper) HandleFailedBankSend(ctx sdk.Context, msg sdk.Msg, memo string) // MsgSend from deposit account to delegate account for deposit. k.Logger(ctx).Error("MsgSend from deposit account to delegate account failed") default: - err = errors.New("unexpected completed send") + err = fmt.Errorf("unexpected completed send (1) from %s to %s (amount: %s)", sMsg.FromAddress, sMsg.ToAddress, sMsg.Amount) k.Logger(ctx).Error(err.Error()) - return err + return nil } return nil diff --git a/x/interchainstaking/keeper/ibc_packet_handlers_test.go b/x/interchainstaking/keeper/ibc_packet_handlers_test.go index 2b6160695..a254bc4ef 100644 --- a/x/interchainstaking/keeper/ibc_packet_handlers_test.go +++ b/x/interchainstaking/keeper/ibc_packet_handlers_test.go @@ -27,8 +27,8 @@ import ( var TestChannel = channeltypes.Channel{ State: channeltypes.OPEN, Ordering: channeltypes.UNORDERED, - Counterparty: channeltypes.NewCounterparty("transfer", "channel-4"), - ConnectionHops: []string{"connection-2"}, + Counterparty: channeltypes.NewCounterparty("transfer", "channel-0"), + ConnectionHops: []string{"connection-0"}, } func (suite *KeeperTestSuite) TestHandleMsgTransferGood() { @@ -1463,24 +1463,23 @@ func (suite *KeeperTestSuite) Test_v045Callback() { { name: "msg response with some data", setStatements: func(ctx sdk.Context, quicksilver *app.Quicksilver) ([]sdk.Msg, []byte) { - sender := addressutils.GenerateAccAddressForTest() - senderAddr, err := sdk.Bech32ifyAddressBytes("cosmos", sender) - suite.Require().NoError(err) + zone, found := quicksilver.InterchainstakingKeeper.GetZone(ctx, suite.chainB.ChainID) + if !found { + suite.Fail("unable to retrieve zone for test") + } + sender := zone.WithdrawalAddress.Address quicksilver.InterchainstakingKeeper.IBCKeeper.ChannelKeeper.SetChannel(ctx, "transfer", "channel-0", TestChannel) - channel, cfound := quicksilver.InterchainstakingKeeper.IBCKeeper.ChannelKeeper.GetChannel(ctx, "transfer", "channel-0") - suite.Require().True(cfound) - - ibcDenom := utils.DeriveIbcDenom(channel.Counterparty.PortId, channel.Counterparty.ChannelId, "denom") - err = quicksilver.BankKeeper.MintCoins(ctx, icstypes.ModuleName, sdk.NewCoins(sdk.NewCoin(ibcDenom, sdk.NewInt(100)))) + ibcDenom := utils.DeriveIbcDenom("transfer", "channel-0", zone.BaseDenom) + err := quicksilver.BankKeeper.MintCoins(ctx, icstypes.ModuleName, sdk.NewCoins(sdk.NewCoin(ibcDenom, sdk.NewInt(100)))) suite.Require().NoError(err) transferMsg := ibctransfertypes.MsgTransfer{ SourcePort: "transfer", SourceChannel: "channel-0", - Token: sdk.NewCoin("denom", sdk.NewInt(100)), - Sender: senderAddr, + Token: sdk.NewCoin(zone.BaseDenom, sdk.NewInt(100)), + Sender: sender, Receiver: quicksilver.AccountKeeper.GetModuleAddress(icstypes.ModuleName).String(), } response := ibctransfertypes.MsgTransferResponse{ @@ -1491,16 +1490,17 @@ func (suite *KeeperTestSuite) Test_v045Callback() { return []sdk.Msg{&transferMsg}, respBytes }, assertStatements: func(ctx sdk.Context, quicksilver *app.Quicksilver) bool { + zone, found := quicksilver.InterchainstakingKeeper.GetZone(ctx, suite.chainB.ChainID) + if !found { + suite.Fail("unable to retrieve zone for test") + } + txMacc := quicksilver.AccountKeeper.GetModuleAddress(icstypes.ModuleName) feeMacc := quicksilver.AccountKeeper.GetModuleAddress(authtypes.FeeCollectorName) txMaccBalance2 := quicksilver.BankKeeper.GetAllBalances(ctx, txMacc) feeMaccBalance2 := quicksilver.BankKeeper.GetAllBalances(ctx, feeMacc) - // assert that ics module balance is now 100denom less than before HandleMsgTransfer() - channel, cfound := quicksilver.InterchainstakingKeeper.IBCKeeper.ChannelKeeper.GetChannel(ctx, "transfer", "channel-0") - suite.Require().True(cfound) - - ibcDenom := utils.DeriveIbcDenom(channel.Counterparty.PortId, channel.Counterparty.ChannelId, "denom") + ibcDenom := utils.DeriveIbcDenom("transfer", "channel-0", zone.BaseDenom) if txMaccBalance2.AmountOf(ibcDenom).Equal(sdk.ZeroInt()) && feeMaccBalance2.AmountOf(ibcDenom).Equal(sdk.NewInt(100)) { return true } @@ -1576,7 +1576,7 @@ func (suite *KeeperTestSuite) Test_v045Callback() { packet := channeltypes.Packet{ Data: packetBytes, } - + ctx = suite.chainA.GetContext() suite.Require().NoError(quicksilver.InterchainstakingKeeper.HandleAcknowledgement(ctx, packet, icatypes.ModuleCdc.MustMarshalJSON(&acknowledgement))) suite.Require().True(test.assertStatements(ctx, quicksilver)) @@ -1593,24 +1593,23 @@ func (suite *KeeperTestSuite) Test_v046Callback() { { name: "msg response with some data", setStatements: func(ctx sdk.Context, quicksilver *app.Quicksilver) ([]sdk.Msg, *codectypes.Any) { - // since SendPacket did not prefix the denomination, we must prefix denomination here - sender := addressutils.GenerateAccAddressForTest() - senderAddr, err := sdk.Bech32ifyAddressBytes("cosmos", sender) - suite.Require().NoError(err) + zone, found := quicksilver.InterchainstakingKeeper.GetZone(ctx, suite.chainB.ChainID) + if !found { + suite.Fail("unable to retrieve zone for test") + } + sender := zone.WithdrawalAddress.Address quicksilver.InterchainstakingKeeper.IBCKeeper.ChannelKeeper.SetChannel(ctx, "transfer", "channel-0", TestChannel) - channel, cfound := quicksilver.InterchainstakingKeeper.IBCKeeper.ChannelKeeper.GetChannel(ctx, "transfer", "channel-0") - suite.Require().True(cfound) - ibcDenom := utils.DeriveIbcDenom(channel.Counterparty.PortId, channel.Counterparty.ChannelId, "denom") - err = quicksilver.BankKeeper.MintCoins(ctx, icstypes.ModuleName, sdk.NewCoins(sdk.NewCoin(ibcDenom, sdk.NewInt(100)))) + ibcDenom := utils.DeriveIbcDenom("transfer", "channel-0", zone.BaseDenom) + err := quicksilver.BankKeeper.MintCoins(ctx, icstypes.ModuleName, sdk.NewCoins(sdk.NewCoin(ibcDenom, sdk.NewInt(100)))) suite.Require().NoError(err) transferMsg := ibctransfertypes.MsgTransfer{ SourcePort: "transfer", SourceChannel: "channel-0", - Token: sdk.NewCoin("denom", sdk.NewInt(100)), - Sender: senderAddr, + Token: sdk.NewCoin(zone.BaseDenom, sdk.NewInt(100)), + Sender: sender, Receiver: quicksilver.AccountKeeper.GetModuleAddress(icstypes.ModuleName).String(), } response := ibctransfertypes.MsgTransferResponse{ @@ -1622,16 +1621,17 @@ func (suite *KeeperTestSuite) Test_v046Callback() { return []sdk.Msg{&transferMsg}, anyResponse }, assertStatements: func(ctx sdk.Context, quicksilver *app.Quicksilver) bool { + zone, found := quicksilver.InterchainstakingKeeper.GetZone(ctx, suite.chainB.ChainID) + if !found { + suite.Fail("unable to retrieve zone for test") + } + txMacc := quicksilver.AccountKeeper.GetModuleAddress(icstypes.ModuleName) feeMacc := quicksilver.AccountKeeper.GetModuleAddress(authtypes.FeeCollectorName) txMaccBalance2 := quicksilver.BankKeeper.GetAllBalances(ctx, txMacc) feeMaccBalance2 := quicksilver.BankKeeper.GetAllBalances(ctx, feeMacc) - // assert that ics module balance is now 100denom less than before HandleMsgTransfer() - channel, cfound := quicksilver.InterchainstakingKeeper.IBCKeeper.ChannelKeeper.GetChannel(ctx, "transfer", "channel-0") - suite.Require().True(cfound) - - ibcDenom := utils.DeriveIbcDenom(channel.Counterparty.PortId, channel.Counterparty.ChannelId, "denom") + ibcDenom := utils.DeriveIbcDenom("transfer", "channel-0", zone.BaseDenom) if txMaccBalance2.AmountOf(ibcDenom).Equal(sdk.ZeroInt()) && feeMaccBalance2.AmountOf(ibcDenom).Equal(sdk.NewInt(100)) { return true }