Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: writing test case for module account incentivizing packet #1397

Merged
merged 4 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 43 additions & 7 deletions modules/apps/29-fee/keeper/escrow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/cosmos/ibc-go/v3/modules/apps/29-fee/types"
transfertypes "github.com/cosmos/ibc-go/v3/modules/apps/transfer/types"
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
"github.com/cosmos/ibc-go/v3/testing/mock"
"github.com/tendermint/tendermint/crypto/secp256k1"
)

Expand All @@ -18,6 +19,7 @@ func (suite *KeeperTestSuite) TestDistributeFee() {
refundAccBal sdk.Coin
packetFee types.PacketFee
packetFees []types.PacketFee
fee types.Fee
)

testCases := []struct {
Expand All @@ -27,7 +29,10 @@ func (suite *KeeperTestSuite) TestDistributeFee() {
}{
{
"success",
func() {},
func() {
packetFee = types.NewPacketFee(fee, refundAcc.String(), []string{})
packetFees = []types.PacketFee{packetFee, packetFee}
},
func() {
// check if fees has been deleted
packetID := channeltypes.NewPacketId(suite.path.EndpointA.ChannelConfig.PortID, suite.path.EndpointA.ChannelID, 1)
Expand Down Expand Up @@ -56,8 +61,30 @@ func (suite *KeeperTestSuite) TestDistributeFee() {
suite.Require().Equal(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(0)), balance)
},
},
{
"success: refund account is module account",
func() {
refundAcc = suite.chainA.GetSimApp().AccountKeeper.GetModuleAddress(mock.ModuleName)

packetFee = types.NewPacketFee(fee, refundAcc.String(), []string{})
packetFees = []types.PacketFee{packetFee, packetFee}

// fund mock account
err := suite.chainA.GetSimApp().BankKeeper.SendCoinsFromAccountToModule(suite.chainA.GetContext(), suite.chainA.SenderAccount.GetAddress(), mock.ModuleName, packetFee.Fee.Total().Add(packetFee.Fee.Total()...))
suite.Require().NoError(err)
},
func() {
// check if the refund acc has been refunded the timeoutFee
expectedRefundAccBal := defaultTimeoutFee[0].Add(defaultTimeoutFee[0])
balance := suite.chainA.GetSimApp().BankKeeper.GetBalance(suite.chainA.GetContext(), refundAcc, sdk.DefaultBondDenom)
suite.Require().Equal(expectedRefundAccBal, balance)
},
},
{
"escrow account out of balance, fee module becomes locked - no distribution", func() {
packetFee = types.NewPacketFee(fee, refundAcc.String(), []string{})
packetFees = []types.PacketFee{packetFee, packetFee}

// pass in an extra packet fee
packetFees = append(packetFees, packetFee)
},
Expand All @@ -76,6 +103,9 @@ func (suite *KeeperTestSuite) TestDistributeFee() {
{
"invalid forward address",
func() {
packetFee = types.NewPacketFee(fee, refundAcc.String(), []string{})
packetFees = []types.PacketFee{packetFee, packetFee}

forwardRelayer = "invalid address"
},
func() {
Expand All @@ -88,6 +118,9 @@ func (suite *KeeperTestSuite) TestDistributeFee() {
{
"invalid forward address: blocked address",
func() {
packetFee = types.NewPacketFee(fee, refundAcc.String(), []string{})
packetFees = []types.PacketFee{packetFee, packetFee}

forwardRelayer = suite.chainA.GetSimApp().AccountKeeper.GetModuleAccount(suite.chainA.GetContext(), transfertypes.ModuleName).GetAddress().String()
},
func() {
Expand All @@ -100,6 +133,9 @@ func (suite *KeeperTestSuite) TestDistributeFee() {
{
"invalid receiver address: ack fee returned to sender",
func() {
packetFee = types.NewPacketFee(fee, refundAcc.String(), []string{})
packetFees = []types.PacketFee{packetFee, packetFee}

reverseRelayer = suite.chainA.GetSimApp().AccountKeeper.GetModuleAccount(suite.chainA.GetContext(), transfertypes.ModuleName).GetAddress()
},
func() {
Expand All @@ -112,6 +148,9 @@ func (suite *KeeperTestSuite) TestDistributeFee() {
{
"invalid refund address: no-op, timeout fee remains in escrow",
func() {
packetFee = types.NewPacketFee(fee, refundAcc.String(), []string{})
packetFees = []types.PacketFee{packetFee, packetFee}

packetFees[0].RefundAddress = suite.chainA.GetSimApp().AccountKeeper.GetModuleAccount(suite.chainA.GetContext(), transfertypes.ModuleName).GetAddress().String()
packetFees[1].RefundAddress = suite.chainA.GetSimApp().AccountKeeper.GetModuleAccount(suite.chainA.GetContext(), transfertypes.ModuleName).GetAddress().String()
},
Expand All @@ -137,18 +176,15 @@ func (suite *KeeperTestSuite) TestDistributeFee() {
refundAcc = suite.chainA.SenderAccount.GetAddress()

packetID := channeltypes.NewPacketId(suite.path.EndpointA.ChannelConfig.PortID, suite.path.EndpointA.ChannelID, 1)
fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee)
fee = types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee)

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

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

tc.malleate()

// fetch the account balances before fee distribution (forward, reverse, refund)
forwardAccAddress, _ := sdk.AccAddressFromBech32(forwardRelayer)
forwardRelayerBal = suite.chainA.GetSimApp().BankKeeper.GetBalance(suite.chainA.GetContext(), forwardAccAddress, sdk.DefaultBondDenom)
Expand Down
13 changes: 12 additions & 1 deletion modules/apps/29-fee/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package keeper_test
import (
sdk "github.com/cosmos/cosmos-sdk/types"

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

testCases := []struct {
Expand Down Expand Up @@ -182,6 +184,15 @@ func (suite *KeeperTestSuite) TestPayPacketFee() {
},
true,
},
{
"refund account is module account",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking at it now, it might make more sense to put this in the escrow test suite

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it'd be nice if there was a test for distributing to a module acc

func() {
msg.Signer = suite.chainA.GetSimApp().AccountKeeper.GetModuleAddress(disttypes.ModuleName).String()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can maybe remove this test case now?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth keeping this in, and just change disttypes.ModuleName to mock.ModuleName?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. It's worth keeping it.

expPacketFee := types.NewPacketFee(fee, msg.Signer, nil)
expFeesInEscrow = []types.PacketFee{expPacketFee}
},
true,
},
{
"fee module is locked",
func() {
Expand Down Expand Up @@ -241,7 +252,7 @@ func (suite *KeeperTestSuite) TestPayPacketFee() {
suite.SetupTest()
suite.coordinator.Setup(suite.path) // setup channel

fee := types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee)
fee = types.NewFee(defaultRecvFee, defaultAckFee, defaultTimeoutFee)
msg = types.NewMsgPayPacketFee(
fee,
suite.path.EndpointA.ChannelConfig.PortID,
Expand Down
8 changes: 8 additions & 0 deletions testing/simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"github.com/cosmos/cosmos-sdk/x/capability"
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
"github.com/cosmos/ibc-go/v3/testing/mock"
simappparams "github.com/cosmos/ibc-go/v3/testing/simapp/params"

"github.com/cosmos/cosmos-sdk/x/crisis"
Expand Down Expand Up @@ -164,6 +165,7 @@ var (
ibctransfertypes.ModuleName: {authtypes.Minter, authtypes.Burner},
ibcfeetypes.ModuleName: nil,
icatypes.ModuleName: nil,
mock.ModuleName: nil,
}
)

Expand Down Expand Up @@ -654,6 +656,12 @@ func (app *SimApp) LoadHeight(height int64) error {
func (app *SimApp) ModuleAccountAddrs() map[string]bool {
modAccAddrs := make(map[string]bool)
for acc := range maccPerms {
// do not add mock module to blocked addresses
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is required so that we can use mock module as a non blocked module account addr

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice we should add a changelog for this!

// this is only used for testing
if acc == mock.ModuleName {
continue
}

modAccAddrs[authtypes.NewModuleAddress(acc).String()] = true
}

Expand Down