Skip to content

Commit 71d7480

Browse files
authored
refactor: writing test case for module account incentivizing packet (#1397)
* refactor: using SendCoins & writing test case for module account incentivizing packet * updating ModuleAccountAddrs helper fn and adding additional test for refunding to module acc * chore: changelog
1 parent 22a51ec commit 71d7480

File tree

4 files changed

+64
-8
lines changed

4 files changed

+64
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
5757
* (modules/core/04-channel) [\#1232](https://github.com/cosmos/ibc-go/pull/1232) Updating params on `NewPacketId` and moving to bottom of file.
5858
* (app/29-fee) [\#1305](https://github.com/cosmos/ibc-go/pull/1305) Change version string for fee module to `ics29-1`
5959
* (app/29-fee) [\#1341](https://github.com/cosmos/ibc-go/pull/1341) Check if the fee module is locked and if the fee module is enabled before refunding all fees
60+
* (testing/simapp) [\#1397](https://github.com/cosmos/ibc-go/pull/1397) Adding mock module to maccperms and adding check to ensure mock module is not a blocked account address.
6061

6162
### Features
6263

modules/apps/29-fee/keeper/escrow_test.go

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/cosmos/ibc-go/v3/modules/apps/29-fee/types"
66
transfertypes "github.com/cosmos/ibc-go/v3/modules/apps/transfer/types"
77
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
8+
"github.com/cosmos/ibc-go/v3/testing/mock"
89
"github.com/tendermint/tendermint/crypto/secp256k1"
910
)
1011

@@ -18,6 +19,7 @@ func (suite *KeeperTestSuite) TestDistributeFee() {
1819
refundAccBal sdk.Coin
1920
packetFee types.PacketFee
2021
packetFees []types.PacketFee
22+
fee types.Fee
2123
)
2224

2325
testCases := []struct {
@@ -27,7 +29,10 @@ func (suite *KeeperTestSuite) TestDistributeFee() {
2729
}{
2830
{
2931
"success",
30-
func() {},
32+
func() {
33+
packetFee = types.NewPacketFee(fee, refundAcc.String(), []string{})
34+
packetFees = []types.PacketFee{packetFee, packetFee}
35+
},
3136
func() {
3237
// check if fees has been deleted
3338
packetID := channeltypes.NewPacketId(suite.path.EndpointA.ChannelConfig.PortID, suite.path.EndpointA.ChannelID, 1)
@@ -56,8 +61,30 @@ func (suite *KeeperTestSuite) TestDistributeFee() {
5661
suite.Require().Equal(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(0)), balance)
5762
},
5863
},
64+
{
65+
"success: refund account is module account",
66+
func() {
67+
refundAcc = suite.chainA.GetSimApp().AccountKeeper.GetModuleAddress(mock.ModuleName)
68+
69+
packetFee = types.NewPacketFee(fee, refundAcc.String(), []string{})
70+
packetFees = []types.PacketFee{packetFee, packetFee}
71+
72+
// fund mock account
73+
err := suite.chainA.GetSimApp().BankKeeper.SendCoinsFromAccountToModule(suite.chainA.GetContext(), suite.chainA.SenderAccount.GetAddress(), mock.ModuleName, packetFee.Fee.Total().Add(packetFee.Fee.Total()...))
74+
suite.Require().NoError(err)
75+
},
76+
func() {
77+
// check if the refund acc has been refunded the timeoutFee
78+
expectedRefundAccBal := defaultTimeoutFee[0].Add(defaultTimeoutFee[0])
79+
balance := suite.chainA.GetSimApp().BankKeeper.GetBalance(suite.chainA.GetContext(), refundAcc, sdk.DefaultBondDenom)
80+
suite.Require().Equal(expectedRefundAccBal, balance)
81+
},
82+
},
5983
{
6084
"escrow account out of balance, fee module becomes locked - no distribution", func() {
85+
packetFee = types.NewPacketFee(fee, refundAcc.String(), []string{})
86+
packetFees = []types.PacketFee{packetFee, packetFee}
87+
6188
// pass in an extra packet fee
6289
packetFees = append(packetFees, packetFee)
6390
},
@@ -76,6 +103,9 @@ func (suite *KeeperTestSuite) TestDistributeFee() {
76103
{
77104
"invalid forward address",
78105
func() {
106+
packetFee = types.NewPacketFee(fee, refundAcc.String(), []string{})
107+
packetFees = []types.PacketFee{packetFee, packetFee}
108+
79109
forwardRelayer = "invalid address"
80110
},
81111
func() {
@@ -88,6 +118,9 @@ func (suite *KeeperTestSuite) TestDistributeFee() {
88118
{
89119
"invalid forward address: blocked address",
90120
func() {
121+
packetFee = types.NewPacketFee(fee, refundAcc.String(), []string{})
122+
packetFees = []types.PacketFee{packetFee, packetFee}
123+
91124
forwardRelayer = suite.chainA.GetSimApp().AccountKeeper.GetModuleAccount(suite.chainA.GetContext(), transfertypes.ModuleName).GetAddress().String()
92125
},
93126
func() {
@@ -100,6 +133,9 @@ func (suite *KeeperTestSuite) TestDistributeFee() {
100133
{
101134
"invalid receiver address: ack fee returned to sender",
102135
func() {
136+
packetFee = types.NewPacketFee(fee, refundAcc.String(), []string{})
137+
packetFees = []types.PacketFee{packetFee, packetFee}
138+
103139
reverseRelayer = suite.chainA.GetSimApp().AccountKeeper.GetModuleAccount(suite.chainA.GetContext(), transfertypes.ModuleName).GetAddress()
104140
},
105141
func() {
@@ -112,6 +148,9 @@ func (suite *KeeperTestSuite) TestDistributeFee() {
112148
{
113149
"invalid refund address: no-op, timeout fee remains in escrow",
114150
func() {
151+
packetFee = types.NewPacketFee(fee, refundAcc.String(), []string{})
152+
packetFees = []types.PacketFee{packetFee, packetFee}
153+
115154
packetFees[0].RefundAddress = suite.chainA.GetSimApp().AccountKeeper.GetModuleAccount(suite.chainA.GetContext(), transfertypes.ModuleName).GetAddress().String()
116155
packetFees[1].RefundAddress = suite.chainA.GetSimApp().AccountKeeper.GetModuleAccount(suite.chainA.GetContext(), transfertypes.ModuleName).GetAddress().String()
117156
},
@@ -137,18 +176,15 @@ func (suite *KeeperTestSuite) TestDistributeFee() {
137176
refundAcc = suite.chainA.SenderAccount.GetAddress()
138177

139178
packetID := channeltypes.NewPacketId(suite.path.EndpointA.ChannelConfig.PortID, suite.path.EndpointA.ChannelID, 1)
140-
fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee)
179+
fee = types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee)
141180

142-
// escrow the packet fees & store the fees in state
143-
packetFee = types.NewPacketFee(fee, refundAcc.String(), []string{})
144-
packetFees = []types.PacketFee{packetFee, packetFee}
181+
tc.malleate()
145182

183+
// escrow the packet fees & store the fees in state
146184
suite.chainA.GetSimApp().IBCFeeKeeper.SetFeesInEscrow(suite.chainA.GetContext(), packetID, types.NewPacketFees(packetFees))
147185
err := suite.chainA.GetSimApp().BankKeeper.SendCoinsFromAccountToModule(suite.chainA.GetContext(), refundAcc, types.ModuleName, packetFee.Fee.Total().Add(packetFee.Fee.Total()...))
148186
suite.Require().NoError(err)
149187

150-
tc.malleate()
151-
152188
// fetch the account balances before fee distribution (forward, reverse, refund)
153189
forwardAccAddress, _ := sdk.AccAddressFromBech32(forwardRelayer)
154190
forwardRelayerBal = suite.chainA.GetSimApp().BankKeeper.GetBalance(suite.chainA.GetContext(), forwardAccAddress, sdk.DefaultBondDenom)

modules/apps/29-fee/keeper/msg_server_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package keeper_test
33
import (
44
sdk "github.com/cosmos/cosmos-sdk/types"
55

6+
disttypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
67
"github.com/cosmos/ibc-go/v3/modules/apps/29-fee/types"
78
clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types"
89
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
@@ -152,6 +153,7 @@ func (suite *KeeperTestSuite) TestPayPacketFee() {
152153
expEscrowBalance sdk.Coins
153154
expFeesInEscrow []types.PacketFee
154155
msg *types.MsgPayPacketFee
156+
fee types.Fee
155157
)
156158

157159
testCases := []struct {
@@ -182,6 +184,15 @@ func (suite *KeeperTestSuite) TestPayPacketFee() {
182184
},
183185
true,
184186
},
187+
{
188+
"refund account is module account",
189+
func() {
190+
msg.Signer = suite.chainA.GetSimApp().AccountKeeper.GetModuleAddress(disttypes.ModuleName).String()
191+
expPacketFee := types.NewPacketFee(fee, msg.Signer, nil)
192+
expFeesInEscrow = []types.PacketFee{expPacketFee}
193+
},
194+
true,
195+
},
185196
{
186197
"fee module is locked",
187198
func() {
@@ -241,7 +252,7 @@ func (suite *KeeperTestSuite) TestPayPacketFee() {
241252
suite.SetupTest()
242253
suite.coordinator.Setup(suite.path) // setup channel
243254

244-
fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee)
255+
fee = types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee)
245256
msg = types.NewMsgPayPacketFee(
246257
fee,
247258
suite.path.EndpointA.ChannelConfig.PortID,

testing/simapp/app.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
"github.com/cosmos/cosmos-sdk/x/capability"
4444
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
4545
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
46+
"github.com/cosmos/ibc-go/v3/testing/mock"
4647
simappparams "github.com/cosmos/ibc-go/v3/testing/simapp/params"
4748

4849
"github.com/cosmos/cosmos-sdk/x/crisis"
@@ -164,6 +165,7 @@ var (
164165
ibctransfertypes.ModuleName: {authtypes.Minter, authtypes.Burner},
165166
ibcfeetypes.ModuleName: nil,
166167
icatypes.ModuleName: nil,
168+
mock.ModuleName: nil,
167169
}
168170
)
169171

@@ -654,6 +656,12 @@ func (app *SimApp) LoadHeight(height int64) error {
654656
func (app *SimApp) ModuleAccountAddrs() map[string]bool {
655657
modAccAddrs := make(map[string]bool)
656658
for acc := range maccPerms {
659+
// do not add mock module to blocked addresses
660+
// this is only used for testing
661+
if acc == mock.ModuleName {
662+
continue
663+
}
664+
657665
modAccAddrs[authtypes.NewModuleAddress(acc).String()] = true
658666
}
659667

0 commit comments

Comments
 (0)