Skip to content

Commit

Permalink
test: add additional test for iterator on fee mw migration (cosmos#5667)
Browse files Browse the repository at this point in the history
  • Loading branch information
charleenfei authored Jan 23, 2024
1 parent 41501a2 commit f4a618c
Showing 1 changed file with 45 additions and 11 deletions.
56 changes: 45 additions & 11 deletions modules/apps/29-fee/keeper/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ func (suite *KeeperTestSuite) TestLegacyTotal() {

func (suite *KeeperTestSuite) TestMigrate1to2() {
var (
packetFee types.PacketFee
packetFees []types.PacketFee
packetID channeltypes.PacketId
packetID2 channeltypes.PacketId
moduleAcc sdk.AccAddress
refundAcc sdk.AccAddress
initRefundAccBal sdk.Coins
initModuleAccBal sdk.Coins
packetFees []types.PacketFee
)

fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee)

testCases := []struct {
name string
malleate func()
Expand All @@ -52,8 +56,6 @@ func (suite *KeeperTestSuite) TestMigrate1to2() {
{
"success: one fee in escrow",
func() {
fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee)
packetFee := types.NewPacketFee(fee, refundAcc.String(), []string(nil))
packetFees = []types.PacketFee{packetFee}
},
func(err error) {
Expand All @@ -65,9 +67,10 @@ func (suite *KeeperTestSuite) TestMigrate1to2() {
}
suite.Require().Equal(expPacketFees, suite.chainA.GetSimApp().IBCFeeKeeper.GetAllIdentifiedPacketFees(suite.chainA.GetContext()))

unusedFee := sdk.NewCoin(sdk.DefaultBondDenom, sdkmath.NewInt(300))
unusedFee := keeper.LegacyTotal(fee).Sub(packetFee.Fee.Total()...)[0]
// refund account balance should increase
refundAccBal := suite.chainA.GetSimApp().BankKeeper.GetBalance(suite.chainA.GetContext(), refundAcc, sdk.DefaultBondDenom)

suite.Require().Equal(initRefundAccBal.Add(unusedFee)[0], refundAccBal)

// module account balance should decrease
Expand All @@ -78,21 +81,19 @@ func (suite *KeeperTestSuite) TestMigrate1to2() {
{
"success: many fees with multiple denoms in escrow",
func() {
fee1 := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee)
packetFee1 := types.NewPacketFee(fee1, refundAcc.String(), []string(nil))

// mint some tokens to the refund account
// mint second denom tokens to the refund account
denom2 := "denom"
err := suite.chainA.GetSimApp().MintKeeper.MintCoins(suite.chainA.GetContext(), sdk.NewCoins(sdk.NewCoin(denom2, sdkmath.NewInt(1000))))
suite.Require().NoError(err)
err = suite.chainA.GetSimApp().BankKeeper.SendCoinsFromModuleToAccount(suite.chainA.GetContext(), minttypes.ModuleName, refundAcc, sdk.NewCoins(sdk.NewCoin(denom2, sdkmath.NewInt(1000))))
suite.Require().NoError(err)

// assemble second denom type packet
defaultFee2 := sdk.NewCoins(sdk.NewCoin(denom2, sdkmath.NewInt(100)))
fee2 := types.NewFee(defaultFee2, defaultFee2, defaultFee2)
packetFee2 := types.NewPacketFee(fee2, refundAcc.String(), []string(nil))

packetFees = []types.PacketFee{packetFee1, packetFee2, packetFee1}
packetFees = []types.PacketFee{packetFee, packetFee2, packetFee}
},
func(err error) {
denom2 := "denom"
Expand All @@ -118,11 +119,43 @@ func (suite *KeeperTestSuite) TestMigrate1to2() {
suite.Require().Equal(initModuleAccBal.Sub(unusedFee...).Sort(), moduleAccBal)
},
},
{
"success: more than one packet",
func() {
packetFees = []types.PacketFee{packetFee}

// add second packet to have escrowed fees
packetID2 = channeltypes.NewPacketID(suite.path.EndpointA.ChannelConfig.PortID, suite.path.EndpointA.ChannelID, 2)
// escrow the packet fee for the second packet & store the fee in state
suite.chainA.GetSimApp().IBCFeeKeeper.SetFeesInEscrow(suite.chainA.GetContext(), packetID2, types.NewPacketFees(packetFees))
err := suite.chainA.GetSimApp().BankKeeper.SendCoinsFromAccountToModule(suite.chainA.GetContext(), refundAcc, types.ModuleName, keeper.LegacyTotal(packetFee.Fee))
suite.Require().NoError(err)
},
func(err error) {
suite.Require().NoError(err)

// ensure that the packet fees are unmodified
expPacketFees := []types.IdentifiedPacketFees{
types.NewIdentifiedPacketFees(packetID, packetFees),
types.NewIdentifiedPacketFees(packetID2, packetFees),
}
suite.Require().Equal(expPacketFees, suite.chainA.GetSimApp().IBCFeeKeeper.GetAllIdentifiedPacketFees(suite.chainA.GetContext()))

// 300 for each packet
unusedFee := keeper.LegacyTotal(fee).Sub(packetFee.Fee.Total()...).MulInt(sdkmath.NewInt(2))[0]
// refund account balance should increase
refundAccBal := suite.chainA.GetSimApp().BankKeeper.GetBalance(suite.chainA.GetContext(), refundAcc, sdk.DefaultBondDenom)
suite.Require().Equal(initRefundAccBal.Add(unusedFee)[0], refundAccBal)

// module account balance should decrease
moduleAccBal := suite.chainA.GetSimApp().BankKeeper.GetBalance(suite.chainA.GetContext(), moduleAcc, sdk.DefaultBondDenom)
suite.Require().Equal(initModuleAccBal.Sub(unusedFee)[0], moduleAccBal)
},
},
{
"failure: invalid refund address",
func() {
fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee)
packetFee := types.NewPacketFee(fee, "invalid", []string{})
packetFee = types.NewPacketFee(fee, "invalid", []string{})
packetFees = []types.PacketFee{packetFee}
},
func(err error) {
Expand All @@ -138,6 +171,7 @@ func (suite *KeeperTestSuite) TestMigrate1to2() {
suite.coordinator.Setup(suite.path)

refundAcc = suite.chainA.SenderAccount.GetAddress()
packetFee = types.NewPacketFee(fee, refundAcc.String(), []string(nil))
moduleAcc = suite.chainA.GetSimApp().AccountKeeper.GetModuleAddress(types.ModuleName)
packetID = channeltypes.NewPacketID(suite.path.EndpointA.ChannelConfig.PortID, suite.path.EndpointA.ChannelID, 1)
packetFees = nil
Expand Down

0 comments on commit f4a618c

Please sign in to comment.