From 8717641a773a5ea177befc77eefee71bb852daf4 Mon Sep 17 00:00:00 2001 From: Damian Nolan Date: Fri, 11 Feb 2022 01:08:50 +0100 Subject: [PATCH 01/10] [WIP] allow multiple addresses to incentivize a packet --- docs/ibc/proto-docs.md | 16 ++ modules/apps/29-fee/ibc_module.go | 10 +- modules/apps/29-fee/ibc_module_test.go | 3 +- modules/apps/29-fee/keeper/escrow.go | 95 ++++--- modules/apps/29-fee/keeper/escrow_test.go | 10 +- modules/apps/29-fee/keeper/genesis_test.go | 30 +-- modules/apps/29-fee/keeper/grpc_query_test.go | 8 +- modules/apps/29-fee/keeper/keeper.go | 70 +++++ modules/apps/29-fee/keeper/keeper_test.go | 15 +- modules/apps/29-fee/types/fee.go | 6 + modules/apps/29-fee/types/fee.pb.go | 248 +++++++++++++++--- modules/apps/29-fee/types/fee_test.go | 11 + modules/apps/29-fee/types/keys.go | 13 + modules/apps/29-fee/types/tx.pb.go | 75 +++--- proto/ibc/applications/fee/v1/fee.proto | 6 + proto/ibc/applications/fee/v1/tx.proto | 1 - 16 files changed, 465 insertions(+), 152 deletions(-) diff --git a/docs/ibc/proto-docs.md b/docs/ibc/proto-docs.md index e51f12b9d56..84127283e32 100644 --- a/docs/ibc/proto-docs.md +++ b/docs/ibc/proto-docs.md @@ -31,6 +31,7 @@ - [ibc/applications/fee/v1/fee.proto](#ibc/applications/fee/v1/fee.proto) - [Fee](#ibc.applications.fee.v1.Fee) - [IdentifiedPacketFee](#ibc.applications.fee.v1.IdentifiedPacketFee) + - [IdentifiedPacketFees](#ibc.applications.fee.v1.IdentifiedPacketFees) - [ibc/applications/fee/v1/genesis.proto](#ibc/applications/fee/v1/genesis.proto) - [FeeEnabledChannel](#ibc.applications.fee.v1.FeeEnabledChannel) @@ -736,6 +737,21 @@ and an optional list of relayers that are permitted to receive the fee. + + + +### IdentifiedPacketFees +IdentifiedPacketFees contains a list of packet fees + + +| Field | Type | Label | Description | +| ----- | ---- | ----- | ----------- | +| `packet_fees` | [IdentifiedPacketFee](#ibc.applications.fee.v1.IdentifiedPacketFee) | repeated | | + + + + + diff --git a/modules/apps/29-fee/ibc_module.go b/modules/apps/29-fee/ibc_module.go index cb5eb71b6d3..d1663ee2226 100644 --- a/modules/apps/29-fee/ibc_module.go +++ b/modules/apps/29-fee/ibc_module.go @@ -223,15 +223,17 @@ func (im IBCModule) OnAcknowledgementPacket( return sdkerrors.Wrapf(err, "cannot unmarshal ICS-29 incentivized packet acknowledgement: %v", ack) } - packetId := channeltypes.NewPacketId(packet.SourceChannel, packet.SourcePort, packet.Sequence) - - identifiedPacketFee, found := im.keeper.GetFeeInEscrow(ctx, packetId) + packetID := channeltypes.NewPacketId(packet.SourceChannel, packet.SourcePort, packet.Sequence) + identifiedPacketFees, found := im.keeper.GetFeesInEscrow(ctx, packetID) if !found { // return underlying callback if no fee found for given packetID return im.app.OnAcknowledgementPacket(ctx, packet, ack.Result, relayer) } - im.keeper.DistributePacketFees(ctx, identifiedPacketFee.RefundAddress, ack.ForwardRelayerAddress, relayer, identifiedPacketFee) + im.keeper.DistributePacketFees(ctx, ack.ForwardRelayerAddress, relayer, identifiedPacketFees.PacketFees) + + // removes the fee from the store as fee is now paid + im.keeper.DeleteFeesInEscrow(ctx, packetID) // call underlying callback return im.app.OnAcknowledgementPacket(ctx, packet, ack.Result, relayer) diff --git a/modules/apps/29-fee/ibc_module_test.go b/modules/apps/29-fee/ibc_module_test.go index 5c4b198efb3..f59b6074182 100644 --- a/modules/apps/29-fee/ibc_module_test.go +++ b/modules/apps/29-fee/ibc_module_test.go @@ -620,8 +620,7 @@ func (suite *FeeTestSuite) TestOnAcknowledgementPacket() { originalBalance = sdk.NewCoins(suite.chainA.GetSimApp().BankKeeper.GetBalance(suite.chainA.GetContext(), suite.chainA.SenderAccount.GetAddress(), ibctesting.TestCoin.Denom)) // default to success case - expectedBalance = originalBalance. - Add(identifiedFee.Fee.TimeoutFee[0]) + expectedBalance = originalBalance.Add(identifiedFee.Fee.TimeoutFee[0]) // malleate test case tc.malleate() diff --git a/modules/apps/29-fee/keeper/escrow.go b/modules/apps/29-fee/keeper/escrow.go index 1eb24cedc86..47378ab3163 100644 --- a/modules/apps/29-fee/keeper/escrow.go +++ b/modules/apps/29-fee/keeper/escrow.go @@ -30,39 +30,45 @@ func (k Keeper) EscrowPacketFee(ctx sdk.Context, identifiedFee types.IdentifiedP coins = coins.Add(identifiedFee.Fee.AckFee...) coins = coins.Add(identifiedFee.Fee.TimeoutFee...) - if err := k.bankKeeper.SendCoinsFromAccountToModule( - ctx, refundAcc, types.ModuleName, coins, - ); err != nil { + if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, refundAcc, types.ModuleName, coins); err != nil { return err } - // Store fee in state for reference later - k.SetFeeInEscrow(ctx, identifiedFee) + var packetFees []types.IdentifiedPacketFee + if feesInEscrow, found := k.GetFeesInEscrow(ctx, identifiedFee.PacketId); found { + packetFees = append(packetFees, feesInEscrow.PacketFees...) + } + + identifiedFees := types.IdentifiedPacketFees{ + PacketFees: append(packetFees, identifiedFee), // append the new fee + } + + k.SetFeesInEscrow(ctx, identifiedFee.PacketId, identifiedFees) + return nil } // DistributePacketFees pays the acknowledgement fee & receive fee for a given packetId while refunding the timeout fee to the refund account associated with the Fee. -func (k Keeper) DistributePacketFees(ctx sdk.Context, refundAcc, forwardRelayer string, reverseRelayer sdk.AccAddress, feeInEscrow types.IdentifiedPacketFee) { - // distribute fee for forward relaying - forward, err := sdk.AccAddressFromBech32(forwardRelayer) - if err == nil { - k.distributeFee(ctx, forward, feeInEscrow.Fee.RecvFee) - } - - // distribute fee for reverse relaying - k.distributeFee(ctx, reverseRelayer, feeInEscrow.Fee.AckFee) +func (k Keeper) DistributePacketFees(ctx sdk.Context, forwardRelayer string, reverseRelayer sdk.AccAddress, feesInEscrow []types.IdentifiedPacketFee) { + for _, packetFee := range feesInEscrow { + // distribute fee for forward relaying + forward, err := sdk.AccAddressFromBech32(forwardRelayer) + if err == nil { + k.distributeFee(ctx, forward, packetFee.Fee.RecvFee) + } - // refund timeout fee refund, - refundAddr, err := sdk.AccAddressFromBech32(refundAcc) - if err != nil { - panic(fmt.Sprintf("could not parse refundAcc %s to sdk.AccAddress", refundAcc)) - } + // distribute fee for reverse relaying + k.distributeFee(ctx, reverseRelayer, packetFee.Fee.AckFee) - // refund timeout fee for unused timeout - k.distributeFee(ctx, refundAddr, feeInEscrow.Fee.TimeoutFee) + // refund timeout fee refund, + refundAddr, err := sdk.AccAddressFromBech32(packetFee.RefundAddress) + if err != nil { + panic(fmt.Sprintf("could not parse refundAcc %s to sdk.AccAddress", packetFee.RefundAddress)) + } - // removes the fee from the store as fee is now paid - k.DeleteFeeInEscrow(ctx, feeInEscrow.PacketId) + // refund timeout fee for unused timeout + k.distributeFee(ctx, refundAddr, packetFee.Fee.TimeoutFee) + } } // DistributePacketsFeesTimeout pays the timeout fee for a given packetId while refunding the acknowledgement fee & receive fee to the refund account associated with the Fee @@ -107,28 +113,31 @@ func (k Keeper) RefundFeesOnChannel(ctx sdk.Context, portID, channelID string) e var refundErr error - k.IterateChannelFeesInEscrow(ctx, portID, channelID, func(identifiedFee types.IdentifiedPacketFee) (stop bool) { - refundAccAddr, err := sdk.AccAddressFromBech32(identifiedFee.RefundAddress) - if err != nil { - refundErr = err - return true + k.IterateIdentifiedChannelFeesInEscrow(ctx, portID, channelID, func(identifiedFees types.IdentifiedPacketFees) (stop bool) { + for _, identifiedFee := range identifiedFees.PacketFees { + refundAccAddr, err := sdk.AccAddressFromBech32(identifiedFee.RefundAddress) + if err != nil { + refundErr = err + return true + } + + // refund all fees to refund address + // Use SendCoins rather than the module account send functions since refund address may be a user account or module address. + // if any `SendCoins` call returns an error, we return error and stop iteration + if err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, refundAccAddr, identifiedFee.Fee.RecvFee); err != nil { + refundErr = err + return true + } + if err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, refundAccAddr, identifiedFee.Fee.AckFee); err != nil { + refundErr = err + return true + } + if err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, refundAccAddr, identifiedFee.Fee.TimeoutFee); err != nil { + refundErr = err + return true + } } - // refund all fees to refund address - // Use SendCoins rather than the module account send functions since refund address may be a user account or module address. - // if any `SendCoins` call returns an error, we return error and stop iteration - if err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, refundAccAddr, identifiedFee.Fee.RecvFee); err != nil { - refundErr = err - return true - } - if err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, refundAccAddr, identifiedFee.Fee.AckFee); err != nil { - refundErr = err - return true - } - if err = k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, refundAccAddr, identifiedFee.Fee.TimeoutFee); err != nil { - refundErr = err - return true - } return false }) diff --git a/modules/apps/29-fee/keeper/escrow_test.go b/modules/apps/29-fee/keeper/escrow_test.go index 8642cca14d9..20f94ade5a2 100644 --- a/modules/apps/29-fee/keeper/escrow_test.go +++ b/modules/apps/29-fee/keeper/escrow_test.go @@ -84,11 +84,11 @@ func (suite *KeeperTestSuite) TestEscrowPacketFee() { err = suite.chainA.GetSimApp().IBCFeeKeeper.EscrowPacketFee(suite.chainA.GetContext(), identifiedPacketFee) if tc.expPass { - feeInEscrow, _ := suite.chainA.GetSimApp().IBCFeeKeeper.GetFeeInEscrow(suite.chainA.GetContext(), packetId) + feesInEscrow, _ := suite.chainA.GetSimApp().IBCFeeKeeper.GetFeesInEscrow(suite.chainA.GetContext(), packetId) // check if the escrowed fee is set in state - suite.Require().True(feeInEscrow.Fee.AckFee.IsEqual(fee.AckFee)) - suite.Require().True(feeInEscrow.Fee.RecvFee.IsEqual(fee.RecvFee)) - suite.Require().True(feeInEscrow.Fee.TimeoutFee.IsEqual(fee.TimeoutFee)) + suite.Require().True(feesInEscrow.PacketFees[0].Fee.AckFee.IsEqual(fee.AckFee)) + suite.Require().True(feesInEscrow.PacketFees[0].Fee.RecvFee.IsEqual(fee.RecvFee)) + suite.Require().True(feesInEscrow.PacketFees[0].Fee.TimeoutFee.IsEqual(fee.TimeoutFee)) // check if the fee is escrowed correctly hasBalance := suite.chainA.GetSimApp().BankKeeper.HasBalance(suite.chainA.GetContext(), suite.chainA.GetSimApp().IBCFeeKeeper.GetFeeModuleAddress(), sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(600)}) suite.Require().True(hasBalance) @@ -158,7 +158,7 @@ func (suite *KeeperTestSuite) TestDistributeFee() { // refundAcc balance after escrow refundAccBal := suite.chainA.GetSimApp().BankKeeper.GetBalance(suite.chainA.GetContext(), refundAcc, sdk.DefaultBondDenom) - suite.chainA.GetSimApp().IBCFeeKeeper.DistributePacketFees(suite.chainA.GetContext(), refundAcc.String(), forwardRelayer, reverseRelayer, identifiedPacketFee) + suite.chainA.GetSimApp().IBCFeeKeeper.DistributePacketFees(suite.chainA.GetContext(), forwardRelayer, reverseRelayer, []types.IdentifiedPacketFee{identifiedPacketFee}) if tc.expPass { // there should no longer be a fee in escrow for this packet diff --git a/modules/apps/29-fee/keeper/genesis_test.go b/modules/apps/29-fee/keeper/genesis_test.go index 568335daed1..3d4f42a3c28 100644 --- a/modules/apps/29-fee/keeper/genesis_test.go +++ b/modules/apps/29-fee/keeper/genesis_test.go @@ -18,9 +18,9 @@ func (suite *KeeperTestSuite) TestInitGenesis() { uint64(1), ) fee := types.Fee{ - defaultReceiveFee, - defaultAckFee, - defaultTimeoutFee, + RecvFee: defaultReceiveFee, + AckFee: defaultAckFee, + TimeoutFee: defaultTimeoutFee, } // relayer addresses @@ -74,19 +74,15 @@ func (suite *KeeperTestSuite) TestExportGenesis() { // setup & escrow the packet fee refundAcc := suite.chainA.SenderAccount.GetAddress() - packetId := channeltypes.NewPacketId( - ibctesting.FirstChannelID, - transfertypes.PortID, - uint64(1), - ) + packetID := channeltypes.NewPacketId(ibctesting.FirstChannelID, transfertypes.PortID, 1) fee := types.Fee{ - defaultReceiveFee, - defaultAckFee, - defaultTimeoutFee, + RecvFee: defaultReceiveFee, + AckFee: defaultAckFee, + TimeoutFee: defaultTimeoutFee, } - identifiedPacketFee := types.NewIdentifiedPacketFee(packetId, fee, refundAcc.String(), []string{}) - err := suite.chainA.GetSimApp().IBCFeeKeeper.EscrowPacketFee(suite.chainA.GetContext(), identifiedPacketFee) - suite.Require().NoError(err) + + identifiedPacketFee := types.NewIdentifiedPacketFee(packetID, fee, refundAcc.String(), []string{}) + suite.chainA.GetSimApp().IBCFeeKeeper.SetFeeInEscrow(suite.chainA.GetContext(), identifiedPacketFee) // relayer addresses sender := suite.chainA.SenderAccount.GetAddress().String() @@ -95,7 +91,7 @@ func (suite *KeeperTestSuite) TestExportGenesis() { suite.chainA.GetSimApp().IBCFeeKeeper.SetCounterpartyAddress(suite.chainA.GetContext(), sender, counterparty) // set forward relayer address - suite.chainA.GetSimApp().IBCFeeKeeper.SetForwardRelayerAddress(suite.chainA.GetContext(), packetId, sender) + suite.chainA.GetSimApp().IBCFeeKeeper.SetForwardRelayerAddress(suite.chainA.GetContext(), packetID, sender) // export genesis genesisState := suite.chainA.GetSimApp().IBCFeeKeeper.ExportGenesis(suite.chainA.GetContext()) @@ -105,7 +101,7 @@ func (suite *KeeperTestSuite) TestExportGenesis() { suite.Require().Equal(transfertypes.PortID, genesisState.FeeEnabledChannels[0].PortId) // check fee - suite.Require().Equal(packetId, genesisState.IdentifiedFees[0].PacketId) + suite.Require().Equal(packetID, genesisState.IdentifiedFees[0].PacketId) suite.Require().Equal(fee, genesisState.IdentifiedFees[0].Fee) suite.Require().Equal(refundAcc.String(), genesisState.IdentifiedFees[0].RefundAddress) suite.Require().Equal([]string(nil), genesisState.IdentifiedFees[0].Relayers) @@ -116,5 +112,5 @@ func (suite *KeeperTestSuite) TestExportGenesis() { // check registered relayer addresses suite.Require().Equal(sender, genesisState.ForwardRelayers[0].Address) - suite.Require().Equal(packetId, genesisState.ForwardRelayers[0].PacketId) + suite.Require().Equal(packetID, genesisState.ForwardRelayers[0].PacketId) } diff --git a/modules/apps/29-fee/keeper/grpc_query_test.go b/modules/apps/29-fee/keeper/grpc_query_test.go index 2c41da93d1e..9074002473a 100644 --- a/modules/apps/29-fee/keeper/grpc_query_test.go +++ b/modules/apps/29-fee/keeper/grpc_query_test.go @@ -70,8 +70,8 @@ func (suite *KeeperTestSuite) TestQueryIncentivizedPacketI() { tc.malleate() suite.chainA.GetSimApp().IBCFeeKeeper.SetFeeEnabled(suite.chainA.GetContext(), transfertypes.PortID, ibctesting.FirstChannelID) - err := suite.chainA.GetSimApp().IBCFeeKeeper.EscrowPacketFee(suite.chainA.GetContext(), identifiedPacketFee) - suite.Require().NoError(err) + suite.chainA.GetSimApp().IBCFeeKeeper.SetFeeInEscrow(suite.chainA.GetContext(), identifiedPacketFee) + ctx := sdk.WrapSDKContext(suite.chainA.GetContext()) res, err := suite.queryClient.IncentivizedPacket(ctx, req) @@ -123,8 +123,8 @@ func (suite *KeeperTestSuite) TestQueryIncentivizedPackets() { expPackets = append(expPackets, &fee1, &fee2, &fee3) suite.chainA.GetSimApp().IBCFeeKeeper.SetFeeEnabled(suite.chainA.GetContext(), transfertypes.PortID, ibctesting.FirstChannelID) - for _, p := range expPackets { - suite.chainA.GetSimApp().IBCFeeKeeper.EscrowPacketFee(suite.chainA.GetContext(), *p) + for _, packetFee := range expPackets { + suite.chainA.GetSimApp().IBCFeeKeeper.SetFeeInEscrow(suite.chainA.GetContext(), *packetFee) } req = &types.QueryIncentivizedPacketsRequest{ diff --git a/modules/apps/29-fee/keeper/keeper.go b/modules/apps/29-fee/keeper/keeper.go index 441aaf57a2c..de2e8a069ff 100644 --- a/modules/apps/29-fee/keeper/keeper.go +++ b/modules/apps/29-fee/keeper/keeper.go @@ -247,6 +247,32 @@ func (k Keeper) GetFeeInEscrow(ctx sdk.Context, packetId channeltypes.PacketId) return fee, true } +// GetFeesInEscrow returns all escrowed packet fees for a given packetID +func (k Keeper) GetFeesInEscrow(ctx sdk.Context, packetID channeltypes.PacketId) (types.IdentifiedPacketFees, bool) { + store := ctx.KVStore(k.storeKey) + key := types.KeyFeesInEscrow(packetID) + bz := store.Get(key) + if bz == nil { + return types.IdentifiedPacketFees{}, false + } + + return k.MustUnmarshalFees(bz), true +} + +// SetFeesInEscrow sets the given packet fees in escrow keyed by the packet identifier +func (k Keeper) SetFeesInEscrow(ctx sdk.Context, packetID channeltypes.PacketId, fees types.IdentifiedPacketFees) { + store := ctx.KVStore(k.storeKey) + bz := k.MustMarshalFees(fees) + store.Set(types.KeyFeesInEscrow(packetID), bz) +} + +// DeleteFeesInEscrow deletes the fee associated with the given packetID +func (k Keeper) DeleteFeesInEscrow(ctx sdk.Context, packetID channeltypes.PacketId) { + store := ctx.KVStore(k.storeKey) + key := types.KeyFeesInEscrow(packetID) + store.Delete(key) +} + // IterateChannelFeesInEscrow iterates over all the fees on the given channel currently escrowed and calls the provided callback // if the callback returns true, then iteration is stopped. func (k Keeper) IterateChannelFeesInEscrow(ctx sdk.Context, portID, channelID string, cb func(identifiedFee types.IdentifiedPacketFee) (stop bool)) { @@ -262,6 +288,21 @@ func (k Keeper) IterateChannelFeesInEscrow(ctx sdk.Context, portID, channelID st } } +// IterateIdentifiedChannelFeesInEscrow iterates over all the fees on the given channel currently escrowed and calls the provided callback +// if the callback returns true, then iteration is stopped. +func (k Keeper) IterateIdentifiedChannelFeesInEscrow(ctx sdk.Context, portID, channelID string, cb func(identifiedFees types.IdentifiedPacketFees) (stop bool)) { + store := ctx.KVStore(k.storeKey) + iterator := sdk.KVStorePrefixIterator(store, types.KeyFeesInEscrowChannelPrefix(portID, channelID)) + + defer iterator.Close() + for ; iterator.Valid(); iterator.Next() { + identifiedFees := k.MustUnmarshalFees(iterator.Value()) + if cb(identifiedFees) { + break + } + } +} + // Deletes the fee associated with the given packetId func (k Keeper) DeleteFeeInEscrow(ctx sdk.Context, packetId channeltypes.PacketId) { store := ctx.KVStore(k.storeKey) @@ -292,6 +333,21 @@ func (k Keeper) GetAllIdentifiedPacketFees(ctx sdk.Context) []types.IdentifiedPa return identifiedFees } +// // GetAllIdentifiedPacketFees returns a list of all IdentifiedPacketFees that are stored in state +// func (k Keeper) GetAllIdentifiedPacketFees(ctx sdk.Context) []types.IdentifiedPacketFees { +// store := ctx.KVStore(k.storeKey) +// iterator := sdk.KVStorePrefixIterator(store, []byte(types.FeesInEscrowPrefix)) +// defer iterator.Close() + +// var identifiedFees []types.IdentifiedPacketFees +// for ; iterator.Valid(); iterator.Next() { +// fee := k.MustUnmarshalFee(iterator.Value()) +// identifiedFees = append(identifiedFees, fee) +// } + +// return identifiedFees +// } + // MustMarshalFee attempts to encode a Fee object and returns the // raw encoded bytes. It panics on error. func (k Keeper) MustMarshalFee(fee *types.IdentifiedPacketFee) []byte { @@ -305,3 +361,17 @@ func (k Keeper) MustUnmarshalFee(bz []byte) types.IdentifiedPacketFee { k.cdc.MustUnmarshal(bz, &fee) return fee } + +// MustMarshalFees attempts to encode a Fee object and returns the +// raw encoded bytes. It panics on error. +func (k Keeper) MustMarshalFees(fees types.IdentifiedPacketFees) []byte { + return k.cdc.MustMarshal(&fees) +} + +// MustUnmarshalFees attempts to decode and return a Fee object from +// raw encoded bytes. It panics on error. +func (k Keeper) MustUnmarshalFees(bz []byte) types.IdentifiedPacketFees { + var fees types.IdentifiedPacketFees + k.cdc.MustUnmarshal(bz, &fees) + return fees +} diff --git a/modules/apps/29-fee/keeper/keeper_test.go b/modules/apps/29-fee/keeper/keeper_test.go index 3e21e268597..b510aa7b7c9 100644 --- a/modules/apps/29-fee/keeper/keeper_test.go +++ b/modules/apps/29-fee/keeper/keeper_test.go @@ -101,17 +101,20 @@ func (suite *KeeperTestSuite) TestGetAllIdentifiedPacketFees() { // escrow a fee refundAcc := suite.chainA.SenderAccount.GetAddress() - packetId := channeltypes.NewPacketId(suite.path.EndpointA.ChannelID, transfertypes.PortID, uint64(1)) - fee := types.Fee{defaultAckFee, defaultReceiveFee, defaultTimeoutFee} - identifiedPacketFee := types.NewIdentifiedPacketFee(packetId, fee, refundAcc.String(), []string{}) + packetID := channeltypes.NewPacketId(suite.path.EndpointA.ChannelID, transfertypes.PortID, 1) + fee := types.Fee{ + AckFee: defaultAckFee, + RecvFee: defaultReceiveFee, + TimeoutFee: defaultTimeoutFee, + } // escrow the packet fee - err := suite.chainA.GetSimApp().IBCFeeKeeper.EscrowPacketFee(suite.chainA.GetContext(), identifiedPacketFee) - suite.Require().NoError(err) + identifiedPacketFee := types.NewIdentifiedPacketFee(packetID, fee, refundAcc.String(), []string{}) + suite.chainA.GetSimApp().IBCFeeKeeper.SetFeeInEscrow(suite.chainA.GetContext(), identifiedPacketFee) expectedFees := []types.IdentifiedPacketFee{ { - PacketId: packetId, + PacketId: packetID, Fee: fee, RefundAddress: refundAcc.String(), Relayers: nil, diff --git a/modules/apps/29-fee/types/fee.go b/modules/apps/29-fee/types/fee.go index 841a1a3a0c1..19a5196d4a1 100644 --- a/modules/apps/29-fee/types/fee.go +++ b/modules/apps/29-fee/types/fee.go @@ -3,9 +3,15 @@ package types import ( "strings" + sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) +// Total returns the total escrowable amount for a given Fee +func (f Fee) Total() sdk.Coins { + return f.RecvFee.Add(f.AckFee...).Add(f.TimeoutFee...) +} + // Validate asserts that each Fee is valid and all three Fees are not empty or zero func (fee Fee) Validate() error { var errFees []string diff --git a/modules/apps/29-fee/types/fee.pb.go b/modules/apps/29-fee/types/fee.pb.go index 4fa6f240abb..8be56939659 100644 --- a/modules/apps/29-fee/types/fee.pb.go +++ b/modules/apps/29-fee/types/fee.pb.go @@ -161,46 +161,94 @@ func (m *IdentifiedPacketFee) GetRelayers() []string { return nil } +// IdentifiedPacketFees contains a list of packet fees +type IdentifiedPacketFees struct { + PacketFees []IdentifiedPacketFee `protobuf:"bytes,1,rep,name=packet_fees,json=packetFees,proto3" json:"packet_fees" yaml:"packet_fees"` +} + +func (m *IdentifiedPacketFees) Reset() { *m = IdentifiedPacketFees{} } +func (m *IdentifiedPacketFees) String() string { return proto.CompactTextString(m) } +func (*IdentifiedPacketFees) ProtoMessage() {} +func (*IdentifiedPacketFees) Descriptor() ([]byte, []int) { + return fileDescriptor_cb3319f1af2a53e5, []int{2} +} +func (m *IdentifiedPacketFees) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *IdentifiedPacketFees) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_IdentifiedPacketFees.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *IdentifiedPacketFees) XXX_Merge(src proto.Message) { + xxx_messageInfo_IdentifiedPacketFees.Merge(m, src) +} +func (m *IdentifiedPacketFees) XXX_Size() int { + return m.Size() +} +func (m *IdentifiedPacketFees) XXX_DiscardUnknown() { + xxx_messageInfo_IdentifiedPacketFees.DiscardUnknown(m) +} + +var xxx_messageInfo_IdentifiedPacketFees proto.InternalMessageInfo + +func (m *IdentifiedPacketFees) GetPacketFees() []IdentifiedPacketFee { + if m != nil { + return m.PacketFees + } + return nil +} + func init() { proto.RegisterType((*Fee)(nil), "ibc.applications.fee.v1.Fee") proto.RegisterType((*IdentifiedPacketFee)(nil), "ibc.applications.fee.v1.IdentifiedPacketFee") + proto.RegisterType((*IdentifiedPacketFees)(nil), "ibc.applications.fee.v1.IdentifiedPacketFees") } func init() { proto.RegisterFile("ibc/applications/fee/v1/fee.proto", fileDescriptor_cb3319f1af2a53e5) } var fileDescriptor_cb3319f1af2a53e5 = []byte{ - // 488 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x93, 0xbd, 0x8e, 0xd3, 0x40, - 0x10, 0xc7, 0x63, 0x7c, 0xba, 0x8f, 0x8d, 0x38, 0x21, 0x03, 0xc2, 0x17, 0x81, 0x13, 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d8, 0x3d, 0xf7, 0xa3, 0xfb, 0x26, - 0xf0, 0xd6, 0xf8, 0x4e, 0x0f, 0x17, 0x2c, 0xe5, 0x2b, 0x28, 0xb5, 0x7b, 0x30, 0xb0, 0x83, 0x93, - 0xe8, 0xce, 0x0f, 0xdf, 0x5f, 0x6d, 0x3c, 0xeb, 0x7a, 0xe3, 0x59, 0xbf, 0x36, 0x9e, 0xf5, 0x6d, - 0xeb, 0x75, 0xae, 0xb7, 0x5e, 0xe7, 0xc7, 0xd6, 0xeb, 0x7c, 0x7a, 0xf9, 0xf7, 0x44, 0x65, 0x2c, - 0x86, 0x73, 0xc5, 0xea, 0x73, 0x96, 0xa9, 0x64, 0x99, 0x82, 0xc6, 0x37, 0xa5, 0xd9, 0xf8, 0xd5, - 0x10, 0x9f, 0x53, 0x33, 0xe4, 0xf8, 0xb0, 0x59, 0xee, 0xf3, 0x3f, 0x01, 0x00, 0x00, 0xff, 0xff, - 0x4e, 0x82, 0x74, 0x21, 0x73, 0x03, 0x00, 0x00, + // 523 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x94, 0x93, 0x41, 0x6f, 0xd3, 0x30, + 0x14, 0xc7, 0x6b, 0x32, 0x6d, 0xab, 0x2b, 0x26, 0x14, 0x86, 0xe8, 0x2a, 0x48, 0x4b, 0x4e, 0x3d, + 0x50, 0x5b, 0xed, 0xe0, 0x00, 0x27, 0x08, 0x52, 0xa5, 0x9d, 0x40, 0x11, 0x27, 0x2e, 0x95, 0xe3, + 0xbc, 0x76, 0x56, 0x9b, 0x38, 0x8a, 0xd3, 0x48, 0x95, 0x38, 0x00, 0x9f, 0x80, 0xcf, 0xc1, 0x27, + 0xd9, 0x71, 0x47, 0x4e, 0x05, 0xb5, 0xdf, 0x60, 0x5f, 0x00, 0xe4, 0xd8, 0xab, 0x8a, 0xd8, 0x34, + 0xf5, 0x64, 0xfb, 0xf9, 0xfd, 0x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43, 0x9c, 0x6d, 0x50, 0xc1, 0xfb, 0x8b, 0x95, 0x87, 0x2e, 0x57, 0x1e, 0xfa, 0xbd, + 0xf2, 0xd0, 0xf7, 0xb5, 0x57, 0xbb, 0x5c, 0x7b, 0xb5, 0x9f, 0x6b, 0xaf, 0xf6, 0xe9, 0xe5, 0xff, + 0x55, 0x15, 0x11, 0xef, 0x4d, 0x24, 0x2d, 0x4f, 0x69, 0x22, 0xe3, 0xf9, 0x0c, 0x94, 0x9e, 0x6b, + 0x45, 0x07, 0xaf, 0x7a, 0x7a, 0xa4, 0xab, 0x42, 0x47, 0xfb, 0xd5, 0x80, 0x9d, 0xfe, 0x0d, 0x00, + 0x00, 0xff, 0xff, 0x0e, 0xeb, 0xd3, 0x29, 0xf7, 0x03, 0x00, 0x00, } func (m *Fee) Marshal() (dAtA []byte, err error) { @@ -327,6 +375,43 @@ func (m *IdentifiedPacketFee) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } +func (m *IdentifiedPacketFees) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *IdentifiedPacketFees) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *IdentifiedPacketFees) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.PacketFees) > 0 { + for iNdEx := len(m.PacketFees) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.PacketFees[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintFee(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0xa + } + } + return len(dAtA) - i, nil +} + func encodeVarintFee(dAtA []byte, offset int, v uint64) int { offset -= sovFee(v) base := offset @@ -388,6 +473,21 @@ func (m *IdentifiedPacketFee) Size() (n int) { return n } +func (m *IdentifiedPacketFees) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if len(m.PacketFees) > 0 { + for _, e := range m.PacketFees { + l = e.Size() + n += 1 + l + sovFee(uint64(l)) + } + } + return n +} + func sovFee(x uint64) (n int) { return (math_bits.Len64(x|1) + 6) / 7 } @@ -726,6 +826,90 @@ func (m *IdentifiedPacketFee) Unmarshal(dAtA []byte) error { } return nil } +func (m *IdentifiedPacketFees) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowFee + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: IdentifiedPacketFees: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: IdentifiedPacketFees: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field PacketFees", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowFee + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthFee + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthFee + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.PacketFees = append(m.PacketFees, IdentifiedPacketFee{}) + if err := m.PacketFees[len(m.PacketFees)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipFee(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthFee + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func skipFee(dAtA []byte) (n int, err error) { l := len(dAtA) iNdEx := 0 diff --git a/modules/apps/29-fee/types/fee_test.go b/modules/apps/29-fee/types/fee_test.go index 0dbd9f29c4e..349fe8dcf07 100644 --- a/modules/apps/29-fee/types/fee_test.go +++ b/modules/apps/29-fee/types/fee_test.go @@ -7,6 +7,17 @@ import ( "github.com/stretchr/testify/require" ) +func TestFeeTotal(t *testing.T) { + fee := Fee{ + AckFee: sdk.NewCoins(sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(100)}), + RecvFee: sdk.NewCoins(sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(100)}), + TimeoutFee: sdk.NewCoins(sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(100)}), + } + + total := fee.Total() + require.Equal(t, sdk.NewInt(300), total.AmountOf(sdk.DefaultBondDenom)) +} + // TestFeeValidation tests Validate func TestFeeValidation(t *testing.T) { var ( diff --git a/modules/apps/29-fee/types/keys.go b/modules/apps/29-fee/types/keys.go index 2f26b859afc..b13aa2ba4b8 100644 --- a/modules/apps/29-fee/types/keys.go +++ b/modules/apps/29-fee/types/keys.go @@ -30,6 +30,9 @@ const ( // FeeInEscrowPrefix is the key prefix for fee in escrow mapping FeeInEscrowPrefix = "feeInEscrow" + // FeesInEscrowPrefix is the key prefix for fee in escrow mapping + FeesInEscrowPrefix = "feesInEscrow" + // ForwardRelayerPrefix is the key prefix for forward relayer addresses stored in state for async acknowledgements ForwardRelayerPrefix = "forwardRelayer" ) @@ -55,7 +58,17 @@ func KeyFeeInEscrow(packetID channeltypes.PacketId) []byte { return []byte(fmt.Sprintf("%s/%d", KeyFeeInEscrowChannelPrefix(packetID.PortId, packetID.ChannelId), packetID.Sequence)) } +// KeyFeesInEscrow returns the key for escrowed fees +func KeyFeesInEscrow(packetID channeltypes.PacketId) []byte { + return []byte(fmt.Sprintf("%s/%d", KeyFeesInEscrowChannelPrefix(packetID.PortId, packetID.ChannelId), packetID.Sequence)) +} + // KeyFeeInEscrowChannelPrefix returns the key prefix for escrowed fees on the given channel func KeyFeeInEscrowChannelPrefix(portID, channelID string) []byte { return []byte(fmt.Sprintf("%s/%s/%s/packet", FeeInEscrowPrefix, portID, channelID)) } + +// KeyFeesInEscrowChannelPrefix returns the key prefix for escrowed fees on the given channel +func KeyFeesInEscrowChannelPrefix(portID, channelID string) []byte { + return []byte(fmt.Sprintf("%s/%s/%s", FeesInEscrowPrefix, portID, channelID)) +} diff --git a/modules/apps/29-fee/types/tx.pb.go b/modules/apps/29-fee/types/tx.pb.go index 46f20f29783..624c6d8279a 100644 --- a/modules/apps/29-fee/types/tx.pb.go +++ b/modules/apps/29-fee/types/tx.pb.go @@ -6,7 +6,6 @@ package types import ( context "context" fmt "fmt" - _ "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types" _ "github.com/gogo/protobuf/gogoproto" grpc1 "github.com/gogo/protobuf/grpc" proto "github.com/gogo/protobuf/proto" @@ -281,44 +280,44 @@ func init() { func init() { proto.RegisterFile("ibc/applications/fee/v1/tx.proto", fileDescriptor_05c93128649f1b96) } var fileDescriptor_05c93128649f1b96 = []byte{ - // 587 bytes of a gzipped FileDescriptorProto + // 577 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x94, 0x4f, 0x4f, 0xdb, 0x4c, - 0x10, 0xc6, 0x6d, 0xc2, 0xcb, 0x0b, 0x5b, 0x54, 0x84, 0x81, 0x12, 0x4c, 0x64, 0x53, 0xab, 0xaa, - 0x72, 0x68, 0xec, 0x12, 0x8a, 0xaa, 0x72, 0x41, 0x04, 0x09, 0x35, 0x87, 0xa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xb2, 0xfa, 0xf5, 0x46, 0x97, 0x7e, 0xdf, 0xe8, 0x92, 0x91, 0x47, 0x2f, - 0x17, 0x57, 0x66, 0x03, 0x0f, 0xa8, 0xcf, 0xc1, 0xb8, 0x5e, 0x42, 0x1b, 0x15, 0xee, 0x55, 0x9d, - 0xb0, 0xea, 0xe0, 0xcf, 0x20, 0x2e, 0x00, 0x94, 0x37, 0x28, 0xd3, 0x00, 0x88, 0x2b, 0x7e, 0x52, - 0xcc, 0x99, 0x73, 0xbc, 0x35, 0x2f, 0x00, 0x4a, 0xcb, 0xb7, 0x5d, 0x5d, 0xb2, 0x23, 0x5c, 0x39, - 0x45, 0x4f, 0x39, 0x6d, 0x33, 0x0c, 0xb5, 0x80, 0x32, 0x51, 0x23, 0xee, 0xb0, 0x97, 0xbd, 0x41, - 0x57, 0xdf, 0x49, 0x7a, 0x99, 0x8c, 0x1b, 0xf6, 0x7a, 0x72, 0x50, 0xa5, 0x4c, 0x94, 0x5d, 0xe5, - 0x3d, 0xda, 0x1c, 0x02, 0x43, 0x9f, 0x23, 0x8d, 0x4c, 0xac, 0x91, 0x1b, 0x74, 0xf5, 0xec, 0x84, - 0xc6, 0x3d, 0x62, 0xd8, 0x1b, 0xc9, 0xd9, 0x79, 0x72, 0x54, 0x76, 0x95, 0x67, 0x68, 0x85, 0x13, - 0xcf, 0x07, 0x96, 0x5d, 0x8e, 0x5d, 0x1f, 0xee, 0x14, 0x15, 0xad, 0x32, 0x68, 0x3a, 0x21, 0x30, - 0x9e, 0xfd, 0xef, 0x20, 0x93, 0x5f, 0xb3, 0xc7, 0xfb, 0x94, 0x79, 0x7b, 0x68, 0xf7, 0x81, 0x23, - 0x63, 0xb7, 0x7e, 0xc8, 0x68, 0xfb, 0x41, 0xec, 0x8c, 0x87, 0x3e, 0x56, 0xae, 0x64, 0xb4, 0x43, - 0x5c, 0xf0, 0x05, 0x69, 0x10, 0x70, 0x6b, 0x41, 0x1c, 0xad, 0xdd, 0xbb, 0xf8, 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0x8a, 0x9a, 0x15, 0x05, 0x00, + 0x00, } // Reference imports to suppress errors if they are not otherwise used. diff --git a/proto/ibc/applications/fee/v1/fee.proto b/proto/ibc/applications/fee/v1/fee.proto index bbbd82663e1..3a58f093e06 100644 --- a/proto/ibc/applications/fee/v1/fee.proto +++ b/proto/ibc/applications/fee/v1/fee.proto @@ -40,3 +40,9 @@ message IdentifiedPacketFee { string refund_address = 3 [(gogoproto.moretags) = "yaml:\"refund_address\""]; repeated string relayers = 4; } + +// IdentifiedPacketFees contains a list of packet fees +message IdentifiedPacketFees { + repeated IdentifiedPacketFee packet_fees = 1 + [(gogoproto.nullable) = false, (gogoproto.moretags) = "yaml:\"packet_fees\""]; +} \ No newline at end of file diff --git a/proto/ibc/applications/fee/v1/tx.proto b/proto/ibc/applications/fee/v1/tx.proto index 4540c28967e..900764f9343 100644 --- a/proto/ibc/applications/fee/v1/tx.proto +++ b/proto/ibc/applications/fee/v1/tx.proto @@ -6,7 +6,6 @@ option go_package = "github.com/cosmos/ibc-go/v3/modules/apps/29-fee/types"; import "gogoproto/gogo.proto"; import "ibc/applications/fee/v1/fee.proto"; -import "ibc/core/channel/v1/channel.proto"; // Msg defines the ibc/fee Msg service. service Msg { From 012001951d625409380fae44202175e9b89730b2 Mon Sep 17 00:00:00 2001 From: Damian Nolan Date: Fri, 11 Feb 2022 11:55:39 +0100 Subject: [PATCH 02/10] distribute multiple fees, fix broken tests --- modules/apps/29-fee/ibc_module.go | 10 ++++--- modules/apps/29-fee/ibc_module_test.go | 13 +++++---- modules/apps/29-fee/keeper/escrow.go | 34 ++++++++++------------- modules/apps/29-fee/keeper/escrow_test.go | 6 +--- modules/apps/29-fee/keeper/keeper.go | 8 ++++++ modules/apps/29-fee/types/fee.go | 7 +++++ 6 files changed, 44 insertions(+), 34 deletions(-) diff --git a/modules/apps/29-fee/ibc_module.go b/modules/apps/29-fee/ibc_module.go index d1663ee2226..159d72334c1 100644 --- a/modules/apps/29-fee/ibc_module.go +++ b/modules/apps/29-fee/ibc_module.go @@ -250,15 +250,17 @@ func (im IBCModule) OnTimeoutPacket( return im.app.OnTimeoutPacket(ctx, packet, relayer) } - packetId := channeltypes.NewPacketId(packet.SourceChannel, packet.SourcePort, packet.Sequence) - - identifiedPacketFee, found := im.keeper.GetFeeInEscrow(ctx, packetId) + packetID := channeltypes.NewPacketId(packet.SourceChannel, packet.SourcePort, packet.Sequence) + identifiedPacketFee, found := im.keeper.GetFeesInEscrow(ctx, packetID) if !found { // return underlying callback if fee not found for given packetID return im.app.OnTimeoutPacket(ctx, packet, relayer) } - im.keeper.DistributePacketFeesOnTimeout(ctx, identifiedPacketFee.RefundAddress, relayer, identifiedPacketFee) + im.keeper.DistributePacketFeesOnTimeout(ctx, relayer, identifiedPacketFee.PacketFees) + + // removes the fee from the store as fee is now paid + im.keeper.DeleteFeesInEscrow(ctx, packetID) // call underlying callback return im.app.OnTimeoutPacket(ctx, packet, relayer) diff --git a/modules/apps/29-fee/ibc_module_test.go b/modules/apps/29-fee/ibc_module_test.go index f59b6074182..deff2185013 100644 --- a/modules/apps/29-fee/ibc_module_test.go +++ b/modules/apps/29-fee/ibc_module_test.go @@ -526,7 +526,7 @@ func (suite *FeeTestSuite) TestOnAcknowledgementPacket() { "no op success without a packet fee", func() { packetId := channeltypes.NewPacketId(suite.path.EndpointA.ChannelID, suite.path.EndpointA.ChannelConfig.PortID, suite.chainA.SenderAccount.GetSequence()) - suite.chainA.GetSimApp().IBCFeeKeeper.DeleteFeeInEscrow(suite.chainA.GetContext(), packetId) + suite.chainA.GetSimApp().IBCFeeKeeper.DeleteFeesInEscrow(suite.chainA.GetContext(), packetId) ack = types.IncentivizedAcknowledgement{ Result: channeltypes.NewResultAcknowledgement([]byte{1}).Acknowledgement(), @@ -679,7 +679,7 @@ func (suite *FeeTestSuite) TestOnTimeoutPacket() { func() { // delete packet fee packetId := channeltypes.NewPacketId(suite.path.EndpointA.ChannelID, suite.path.EndpointA.ChannelConfig.PortID, suite.chainA.SenderAccount.GetSequence()) - suite.chainA.GetSimApp().IBCFeeKeeper.DeleteFeeInEscrow(suite.chainA.GetContext(), packetId) + suite.chainA.GetSimApp().IBCFeeKeeper.DeleteFeesInEscrow(suite.chainA.GetContext(), packetId) expectedBalance = originalBalance.Add(ibctesting.TestCoin) // timeout refund for ics20 transfer }, @@ -764,10 +764,11 @@ func (suite *FeeTestSuite) TestOnTimeoutPacket() { relayerBalance := sdk.NewCoins(suite.chainA.GetSimApp().BankKeeper.GetBalance(suite.chainA.GetContext(), relayerAddr, ibctesting.TestCoin.Denom)) if tc.expFeeDistributed { - suite.Require().Equal( - identifiedFee.Fee.TimeoutFee, - relayerBalance, - ) + // there should no longer be a fee in escrow for this packet + found := suite.chainA.GetSimApp().IBCFeeKeeper.HasFeesInEscrow(suite.chainA.GetContext(), packetId) + suite.Require().False(found) + + suite.Require().Equal(identifiedFee.Fee.TimeoutFee, relayerBalance) } else { suite.Require().Empty(relayerBalance) } diff --git a/modules/apps/29-fee/keeper/escrow.go b/modules/apps/29-fee/keeper/escrow.go index 47378ab3163..b96a69b5c48 100644 --- a/modules/apps/29-fee/keeper/escrow.go +++ b/modules/apps/29-fee/keeper/escrow.go @@ -26,10 +26,7 @@ func (k Keeper) EscrowPacketFee(ctx sdk.Context, identifiedFee types.IdentifiedP return sdkerrors.Wrapf(types.ErrRefundAccNotFound, "account with address: %s not found", refundAcc) } - coins := identifiedFee.Fee.RecvFee - coins = coins.Add(identifiedFee.Fee.AckFee...) - coins = coins.Add(identifiedFee.Fee.TimeoutFee...) - + coins := identifiedFee.Fee.Total() if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, refundAcc, types.ModuleName, coins); err != nil { return err } @@ -72,24 +69,23 @@ func (k Keeper) DistributePacketFees(ctx sdk.Context, forwardRelayer string, rev } // DistributePacketsFeesTimeout pays the timeout fee for a given packetId while refunding the acknowledgement fee & receive fee to the refund account associated with the Fee -func (k Keeper) DistributePacketFeesOnTimeout(ctx sdk.Context, refundAcc string, timeoutRelayer sdk.AccAddress, feeInEscrow types.IdentifiedPacketFee) { - // check if refundAcc address works - refundAddr, err := sdk.AccAddressFromBech32(refundAcc) - if err != nil { - panic(fmt.Sprintf("could not parse refundAcc %s to sdk.AccAddress", refundAcc)) - } - - // refund receive fee for unused forward relaying - k.distributeFee(ctx, refundAddr, feeInEscrow.Fee.RecvFee) +func (k Keeper) DistributePacketFeesOnTimeout(ctx sdk.Context, timeoutRelayer sdk.AccAddress, feesInEscrow []types.IdentifiedPacketFee) { + for _, feeInEscrow := range feesInEscrow { + // check if refundAcc address works + refundAddr, err := sdk.AccAddressFromBech32(feeInEscrow.RefundAddress) + if err != nil { + panic(fmt.Sprintf("could not parse refundAcc %s to sdk.AccAddress", feeInEscrow.RefundAddress)) + } - // refund ack fee for unused reverse relaying - k.distributeFee(ctx, refundAddr, feeInEscrow.Fee.AckFee) + // refund receive fee for unused forward relaying + k.distributeFee(ctx, refundAddr, feeInEscrow.Fee.RecvFee) - // distribute fee for timeout relaying - k.distributeFee(ctx, timeoutRelayer, feeInEscrow.Fee.TimeoutFee) + // refund ack fee for unused reverse relaying + k.distributeFee(ctx, refundAddr, feeInEscrow.Fee.AckFee) - // removes the fee from the store as fee is now paid - k.DeleteFeeInEscrow(ctx, feeInEscrow.PacketId) + // distribute fee for timeout relaying + k.distributeFee(ctx, timeoutRelayer, feeInEscrow.Fee.TimeoutFee) + } } // distributeFee will attempt to distribute the escrowed fee to the receiver address. diff --git a/modules/apps/29-fee/keeper/escrow_test.go b/modules/apps/29-fee/keeper/escrow_test.go index 20f94ade5a2..ee32b681833 100644 --- a/modules/apps/29-fee/keeper/escrow_test.go +++ b/modules/apps/29-fee/keeper/escrow_test.go @@ -225,11 +225,7 @@ func (suite *KeeperTestSuite) TestDistributeTimeoutFee() { // refundAcc balance after escrow refundAccBal := suite.chainA.GetSimApp().BankKeeper.GetBalance(suite.chainA.GetContext(), refundAcc, sdk.DefaultBondDenom) - suite.chainA.GetSimApp().IBCFeeKeeper.DistributePacketFeesOnTimeout(suite.chainA.GetContext(), refundAcc.String(), timeoutRelayer, identifiedPacketFee) - - // there should no longer be a fee in escrow for this packet - found := suite.chainA.GetSimApp().IBCFeeKeeper.HasFeeInEscrow(suite.chainA.GetContext(), packetId) - suite.Require().False(found) + suite.chainA.GetSimApp().IBCFeeKeeper.DistributePacketFeesOnTimeout(suite.chainA.GetContext(), timeoutRelayer, []types.IdentifiedPacketFee{identifiedPacketFee}) // check if the timeoutRelayer has been paid hasBalance := suite.chainA.GetSimApp().BankKeeper.HasBalance(suite.chainA.GetContext(), timeoutRelayer, fee.TimeoutFee[0]) diff --git a/modules/apps/29-fee/keeper/keeper.go b/modules/apps/29-fee/keeper/keeper.go index de2e8a069ff..d412fd8f023 100644 --- a/modules/apps/29-fee/keeper/keeper.go +++ b/modules/apps/29-fee/keeper/keeper.go @@ -259,6 +259,14 @@ func (k Keeper) GetFeesInEscrow(ctx sdk.Context, packetID channeltypes.PacketId) return k.MustUnmarshalFees(bz), true } +// HasFeesInEscrow returns true if packet fees exist for the provided packetID +func (k Keeper) HasFeesInEscrow(ctx sdk.Context, packetID channeltypes.PacketId) bool { + store := ctx.KVStore(k.storeKey) + key := types.KeyFeesInEscrow(packetID) + + return store.Has(key) +} + // SetFeesInEscrow sets the given packet fees in escrow keyed by the packet identifier func (k Keeper) SetFeesInEscrow(ctx sdk.Context, packetID channeltypes.PacketId, fees types.IdentifiedPacketFees) { store := ctx.KVStore(k.storeKey) diff --git a/modules/apps/29-fee/types/fee.go b/modules/apps/29-fee/types/fee.go index 19a5196d4a1..9cee5a4e89f 100644 --- a/modules/apps/29-fee/types/fee.go +++ b/modules/apps/29-fee/types/fee.go @@ -7,6 +7,13 @@ import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) +// NewIdentifiedPacketFees creates and returns a new IdentifiedPacketFees struct +func NewIdentifiedPacketFees(packetFees ...IdentifiedPacketFee) IdentifiedPacketFees { + return IdentifiedPacketFees{ + PacketFees: packetFees, + } +} + // Total returns the total escrowable amount for a given Fee func (f Fee) Total() sdk.Coins { return f.RecvFee.Add(f.AckFee...).Add(f.TimeoutFee...) From c190888da801ba3a919cdd09dfc23459aabf2053 Mon Sep 17 00:00:00 2001 From: Damian Nolan Date: Fri, 11 Feb 2022 12:03:36 +0100 Subject: [PATCH 03/10] use NewIdentifiedPacketFees in EscrowPacketFee --- modules/apps/29-fee/keeper/escrow.go | 7 ++----- modules/apps/29-fee/types/fee.go | 4 ++-- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/modules/apps/29-fee/keeper/escrow.go b/modules/apps/29-fee/keeper/escrow.go index b96a69b5c48..58cac90b34b 100644 --- a/modules/apps/29-fee/keeper/escrow.go +++ b/modules/apps/29-fee/keeper/escrow.go @@ -31,15 +31,12 @@ func (k Keeper) EscrowPacketFee(ctx sdk.Context, identifiedFee types.IdentifiedP return err } - var packetFees []types.IdentifiedPacketFee + packetFees := []types.IdentifiedPacketFee{identifiedFee} if feesInEscrow, found := k.GetFeesInEscrow(ctx, identifiedFee.PacketId); found { packetFees = append(packetFees, feesInEscrow.PacketFees...) } - identifiedFees := types.IdentifiedPacketFees{ - PacketFees: append(packetFees, identifiedFee), // append the new fee - } - + identifiedFees := types.NewIdentifiedPacketFees(packetFees) k.SetFeesInEscrow(ctx, identifiedFee.PacketId, identifiedFees) return nil diff --git a/modules/apps/29-fee/types/fee.go b/modules/apps/29-fee/types/fee.go index 9cee5a4e89f..7453c88e50e 100644 --- a/modules/apps/29-fee/types/fee.go +++ b/modules/apps/29-fee/types/fee.go @@ -8,13 +8,13 @@ import ( ) // NewIdentifiedPacketFees creates and returns a new IdentifiedPacketFees struct -func NewIdentifiedPacketFees(packetFees ...IdentifiedPacketFee) IdentifiedPacketFees { +func NewIdentifiedPacketFees(packetFees []IdentifiedPacketFee) IdentifiedPacketFees { return IdentifiedPacketFees{ PacketFees: packetFees, } } -// Total returns the total escrowable amount for a given Fee +// Total returns the total amount for a given Fee func (f Fee) Total() sdk.Coins { return f.RecvFee.Add(f.AckFee...).Add(f.TimeoutFee...) } From 313d12576e9fe2197e43ac75ea7f93b3a9fed946 Mon Sep 17 00:00:00 2001 From: Damian Nolan Date: Fri, 11 Feb 2022 12:06:17 +0100 Subject: [PATCH 04/10] cleanup var naming --- modules/apps/29-fee/ibc_module.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/apps/29-fee/ibc_module.go b/modules/apps/29-fee/ibc_module.go index 159d72334c1..876b3b8ee6b 100644 --- a/modules/apps/29-fee/ibc_module.go +++ b/modules/apps/29-fee/ibc_module.go @@ -251,13 +251,13 @@ func (im IBCModule) OnTimeoutPacket( } packetID := channeltypes.NewPacketId(packet.SourceChannel, packet.SourcePort, packet.Sequence) - identifiedPacketFee, found := im.keeper.GetFeesInEscrow(ctx, packetID) + identifiedPacketFees, found := im.keeper.GetFeesInEscrow(ctx, packetID) if !found { // return underlying callback if fee not found for given packetID return im.app.OnTimeoutPacket(ctx, packet, relayer) } - im.keeper.DistributePacketFeesOnTimeout(ctx, relayer, identifiedPacketFee.PacketFees) + im.keeper.DistributePacketFeesOnTimeout(ctx, relayer, identifiedPacketFees.PacketFees) // removes the fee from the store as fee is now paid im.keeper.DeleteFeesInEscrow(ctx, packetID) From e640784252017fee268c3ba6fc295cd1e3680e0f Mon Sep 17 00:00:00 2001 From: Damian Nolan Date: Sat, 12 Feb 2022 10:22:40 +0000 Subject: [PATCH 05/10] removing commented out code and adding test case --- modules/apps/29-fee/keeper/escrow_test.go | 20 +++++++++++++++++++- modules/apps/29-fee/keeper/keeper.go | 15 --------------- 2 files changed, 19 insertions(+), 16 deletions(-) diff --git a/modules/apps/29-fee/keeper/escrow_test.go b/modules/apps/29-fee/keeper/escrow_test.go index ee32b681833..eec2a257bd8 100644 --- a/modules/apps/29-fee/keeper/escrow_test.go +++ b/modules/apps/29-fee/keeper/escrow_test.go @@ -27,6 +27,23 @@ func (suite *KeeperTestSuite) TestEscrowPacketFee() { { "success", func() {}, true, }, + { + "success with existing packet fee", func() { + fee := types.Fee{ + RecvFee: receiveFee, + AckFee: ackFee, + TimeoutFee: timeoutFee, + } + + identifiedPacketFee := types.NewIdentifiedPacketFee(packetId, fee, refundAcc.String(), []string{}) + + feesInEscrow := types.IdentifiedPacketFees{ + PacketFees: []types.IdentifiedPacketFee{identifiedPacketFee}, + } + + suite.chainA.GetSimApp().IBCFeeKeeper.SetFeesInEscrow(suite.chainA.GetContext(), packetId, feesInEscrow) + }, true, + }, { "fee not enabled on this channel", func() { packetId.ChannelId = "disabled_channel" @@ -84,7 +101,8 @@ func (suite *KeeperTestSuite) TestEscrowPacketFee() { err = suite.chainA.GetSimApp().IBCFeeKeeper.EscrowPacketFee(suite.chainA.GetContext(), identifiedPacketFee) if tc.expPass { - feesInEscrow, _ := suite.chainA.GetSimApp().IBCFeeKeeper.GetFeesInEscrow(suite.chainA.GetContext(), packetId) + feesInEscrow, found := suite.chainA.GetSimApp().IBCFeeKeeper.GetFeesInEscrow(suite.chainA.GetContext(), packetId) + suite.Require().True(found) // check if the escrowed fee is set in state suite.Require().True(feesInEscrow.PacketFees[0].Fee.AckFee.IsEqual(fee.AckFee)) suite.Require().True(feesInEscrow.PacketFees[0].Fee.RecvFee.IsEqual(fee.RecvFee)) diff --git a/modules/apps/29-fee/keeper/keeper.go b/modules/apps/29-fee/keeper/keeper.go index d412fd8f023..d82e17b69a4 100644 --- a/modules/apps/29-fee/keeper/keeper.go +++ b/modules/apps/29-fee/keeper/keeper.go @@ -341,21 +341,6 @@ func (k Keeper) GetAllIdentifiedPacketFees(ctx sdk.Context) []types.IdentifiedPa return identifiedFees } -// // GetAllIdentifiedPacketFees returns a list of all IdentifiedPacketFees that are stored in state -// func (k Keeper) GetAllIdentifiedPacketFees(ctx sdk.Context) []types.IdentifiedPacketFees { -// store := ctx.KVStore(k.storeKey) -// iterator := sdk.KVStorePrefixIterator(store, []byte(types.FeesInEscrowPrefix)) -// defer iterator.Close() - -// var identifiedFees []types.IdentifiedPacketFees -// for ; iterator.Valid(); iterator.Next() { -// fee := k.MustUnmarshalFee(iterator.Value()) -// identifiedFees = append(identifiedFees, fee) -// } - -// return identifiedFees -// } - // MustMarshalFee attempts to encode a Fee object and returns the // raw encoded bytes. It panics on error. func (k Keeper) MustMarshalFee(fee *types.IdentifiedPacketFee) []byte { From 77f31f828e391b607dfdf4e615ceb51b99e3fd65 Mon Sep 17 00:00:00 2001 From: Damian Nolan Date: Tue, 15 Feb 2022 08:53:04 +0000 Subject: [PATCH 06/10] Update modules/apps/29-fee/ibc_module.go Co-authored-by: Aditya --- modules/apps/29-fee/ibc_module.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/apps/29-fee/ibc_module.go b/modules/apps/29-fee/ibc_module.go index 876b3b8ee6b..53127683a87 100644 --- a/modules/apps/29-fee/ibc_module.go +++ b/modules/apps/29-fee/ibc_module.go @@ -232,7 +232,7 @@ func (im IBCModule) OnAcknowledgementPacket( im.keeper.DistributePacketFees(ctx, ack.ForwardRelayerAddress, relayer, identifiedPacketFees.PacketFees) - // removes the fee from the store as fee is now paid + // removes the fees from the store as fees are now paid im.keeper.DeleteFeesInEscrow(ctx, packetID) // call underlying callback From d4db040b3e948d907718f4edf7026a944ecc3dd3 Mon Sep 17 00:00:00 2001 From: Sean King Date: Tue, 15 Feb 2022 15:43:05 +0100 Subject: [PATCH 07/10] fix: refund RecvFee if ForwardAddr is invalid --- modules/apps/29-fee/keeper/escrow.go | 14 ++++++++++---- modules/apps/29-fee/keeper/escrow_test.go | 5 +++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/modules/apps/29-fee/keeper/escrow.go b/modules/apps/29-fee/keeper/escrow.go index 58cac90b34b..3bf77fe81c5 100644 --- a/modules/apps/29-fee/keeper/escrow.go +++ b/modules/apps/29-fee/keeper/escrow.go @@ -44,11 +44,12 @@ func (k Keeper) EscrowPacketFee(ctx sdk.Context, identifiedFee types.IdentifiedP // DistributePacketFees pays the acknowledgement fee & receive fee for a given packetId while refunding the timeout fee to the refund account associated with the Fee. func (k Keeper) DistributePacketFees(ctx sdk.Context, forwardRelayer string, reverseRelayer sdk.AccAddress, feesInEscrow []types.IdentifiedPacketFee) { + // distribute fee for forward relaying + forwardAddr, fErr := sdk.AccAddressFromBech32(forwardRelayer) + for _, packetFee := range feesInEscrow { - // distribute fee for forward relaying - forward, err := sdk.AccAddressFromBech32(forwardRelayer) - if err == nil { - k.distributeFee(ctx, forward, packetFee.Fee.RecvFee) + if fErr == nil { + k.distributeFee(ctx, forwardAddr, packetFee.Fee.RecvFee) } // distribute fee for reverse relaying @@ -60,6 +61,11 @@ func (k Keeper) DistributePacketFees(ctx sdk.Context, forwardRelayer string, rev panic(fmt.Sprintf("could not parse refundAcc %s to sdk.AccAddress", packetFee.RefundAddress)) } + if fErr != nil { + // refund onRecv fee as forward relayer is not valid address + k.distributeFee(ctx, refundAddr, packetFee.Fee.RecvFee) + } + // refund timeout fee for unused timeout k.distributeFee(ctx, refundAddr, packetFee.Fee.TimeoutFee) } diff --git a/modules/apps/29-fee/keeper/escrow_test.go b/modules/apps/29-fee/keeper/escrow_test.go index eec2a257bd8..c003a9f3674 100644 --- a/modules/apps/29-fee/keeper/escrow_test.go +++ b/modules/apps/29-fee/keeper/escrow_test.go @@ -202,8 +202,9 @@ func (suite *KeeperTestSuite) TestDistributeFee() { hasBalance = suite.chainA.GetSimApp().BankKeeper.HasBalance(suite.chainA.GetContext(), suite.chainA.GetSimApp().IBCFeeKeeper.GetFeeModuleAddress(), sdk.Coin{Denom: sdk.DefaultBondDenom, Amount: sdk.NewInt(0)}) suite.Require().True(hasBalance) } else { - // check the module acc wallet still has forward relaying balance - hasBalance := suite.chainA.GetSimApp().BankKeeper.HasBalance(suite.chainA.GetContext(), suite.chainA.GetSimApp().IBCFeeKeeper.GetFeeModuleAddress(), fee.RecvFee[0]) + // check if the refund acc has been refunded the timeoutFee & onRecvFee + expectedRefundAccBal := refundAccBal.Add(fee.TimeoutFee[0]).Add(fee.RecvFee[0]) + hasBalance := suite.chainA.GetSimApp().BankKeeper.HasBalance(suite.chainA.GetContext(), refundAcc, expectedRefundAccBal) suite.Require().True(hasBalance) } }) From b234d48a2627b84903640dd8061f6610749934ef Mon Sep 17 00:00:00 2001 From: Sean King Date: Tue, 15 Feb 2022 15:52:53 +0100 Subject: [PATCH 08/10] test: update tests to distribute multiple identified fees --- modules/apps/29-fee/keeper/escrow_test.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/modules/apps/29-fee/keeper/escrow_test.go b/modules/apps/29-fee/keeper/escrow_test.go index c003a9f3674..0be30c8c22f 100644 --- a/modules/apps/29-fee/keeper/escrow_test.go +++ b/modules/apps/29-fee/keeper/escrow_test.go @@ -170,13 +170,16 @@ func (suite *KeeperTestSuite) TestDistributeFee() { err := suite.chainA.GetSimApp().IBCFeeKeeper.EscrowPacketFee(suite.chainA.GetContext(), identifiedPacketFee) suite.Require().NoError(err) + // escrow a second packet fee to test with multiple fees distributed + err = suite.chainA.GetSimApp().IBCFeeKeeper.EscrowPacketFee(suite.chainA.GetContext(), identifiedPacketFee) + suite.Require().NoError(err) tc.malleate() // refundAcc balance after escrow refundAccBal := suite.chainA.GetSimApp().BankKeeper.GetBalance(suite.chainA.GetContext(), refundAcc, sdk.DefaultBondDenom) - suite.chainA.GetSimApp().IBCFeeKeeper.DistributePacketFees(suite.chainA.GetContext(), forwardRelayer, reverseRelayer, []types.IdentifiedPacketFee{identifiedPacketFee}) + suite.chainA.GetSimApp().IBCFeeKeeper.DistributePacketFees(suite.chainA.GetContext(), forwardRelayer, reverseRelayer, []types.IdentifiedPacketFee{identifiedPacketFee, identifiedPacketFee}) if tc.expPass { // there should no longer be a fee in escrow for this packet @@ -184,17 +187,17 @@ func (suite *KeeperTestSuite) TestDistributeFee() { suite.Require().False(found) // check if the reverse relayer is paid - hasBalance := suite.chainA.GetSimApp().BankKeeper.HasBalance(suite.chainA.GetContext(), reverseRelayer, fee.AckFee[0]) + hasBalance := suite.chainA.GetSimApp().BankKeeper.HasBalance(suite.chainA.GetContext(), reverseRelayer, fee.AckFee[0].Add(fee.AckFee[0])) suite.Require().True(hasBalance) // check if the forward relayer is paid forward, err := sdk.AccAddressFromBech32(forwardRelayer) suite.Require().NoError(err) - hasBalance = suite.chainA.GetSimApp().BankKeeper.HasBalance(suite.chainA.GetContext(), forward, fee.RecvFee[0]) + hasBalance = suite.chainA.GetSimApp().BankKeeper.HasBalance(suite.chainA.GetContext(), forward, fee.RecvFee[0].Add(fee.RecvFee[0])) suite.Require().True(hasBalance) // check if the refund acc has been refunded the timeoutFee - expectedRefundAccBal := refundAccBal.Add(fee.TimeoutFee[0]) + expectedRefundAccBal := refundAccBal.Add(fee.TimeoutFee[0].Add(fee.TimeoutFee[0])) hasBalance = suite.chainA.GetSimApp().BankKeeper.HasBalance(suite.chainA.GetContext(), refundAcc, expectedRefundAccBal) suite.Require().True(hasBalance) @@ -203,7 +206,7 @@ func (suite *KeeperTestSuite) TestDistributeFee() { suite.Require().True(hasBalance) } else { // check if the refund acc has been refunded the timeoutFee & onRecvFee - expectedRefundAccBal := refundAccBal.Add(fee.TimeoutFee[0]).Add(fee.RecvFee[0]) + expectedRefundAccBal := refundAccBal.Add(fee.TimeoutFee[0]).Add(fee.RecvFee[0]).Add(fee.TimeoutFee[0]).Add(fee.RecvFee[0]) hasBalance := suite.chainA.GetSimApp().BankKeeper.HasBalance(suite.chainA.GetContext(), refundAcc, expectedRefundAccBal) suite.Require().True(hasBalance) } @@ -240,19 +243,22 @@ func (suite *KeeperTestSuite) TestDistributeTimeoutFee() { err := suite.chainA.GetSimApp().IBCFeeKeeper.EscrowPacketFee(suite.chainA.GetContext(), identifiedPacketFee) suite.Require().NoError(err) + // escrow a second packet fee to test with multiple fees distributed + err = suite.chainA.GetSimApp().IBCFeeKeeper.EscrowPacketFee(suite.chainA.GetContext(), identifiedPacketFee) + suite.Require().NoError(err) // refundAcc balance after escrow refundAccBal := suite.chainA.GetSimApp().BankKeeper.GetBalance(suite.chainA.GetContext(), refundAcc, sdk.DefaultBondDenom) - suite.chainA.GetSimApp().IBCFeeKeeper.DistributePacketFeesOnTimeout(suite.chainA.GetContext(), timeoutRelayer, []types.IdentifiedPacketFee{identifiedPacketFee}) + suite.chainA.GetSimApp().IBCFeeKeeper.DistributePacketFeesOnTimeout(suite.chainA.GetContext(), timeoutRelayer, []types.IdentifiedPacketFee{identifiedPacketFee, identifiedPacketFee}) // check if the timeoutRelayer has been paid hasBalance := suite.chainA.GetSimApp().BankKeeper.HasBalance(suite.chainA.GetContext(), timeoutRelayer, fee.TimeoutFee[0]) suite.Require().True(hasBalance) // check if the refund acc has been refunded the recv & ack fees - expectedRefundAccBal := refundAccBal.Add(fee.AckFee[0]) - expectedRefundAccBal = refundAccBal.Add(fee.RecvFee[0]) + expectedRefundAccBal := refundAccBal.Add(fee.AckFee[0]).Add(fee.AckFee[0]) + expectedRefundAccBal = refundAccBal.Add(fee.RecvFee[0]).Add(fee.RecvFee[0]) hasBalance = suite.chainA.GetSimApp().BankKeeper.HasBalance(suite.chainA.GetContext(), refundAcc, expectedRefundAccBal) suite.Require().True(hasBalance) From 9ca8eeef2dca1c4985b1282158b909d323686769 Mon Sep 17 00:00:00 2001 From: Sean King Date: Tue, 15 Feb 2022 17:25:25 +0100 Subject: [PATCH 09/10] refactor: clean up DistrPacketFees --- modules/apps/29-fee/keeper/escrow.go | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/modules/apps/29-fee/keeper/escrow.go b/modules/apps/29-fee/keeper/escrow.go index 36f9da3aa50..13885015673 100644 --- a/modules/apps/29-fee/keeper/escrow.go +++ b/modules/apps/29-fee/keeper/escrow.go @@ -44,28 +44,25 @@ func (k Keeper) EscrowPacketFee(ctx sdk.Context, identifiedFee types.IdentifiedP // DistributePacketFees pays the acknowledgement fee & receive fee for a given packetId while refunding the timeout fee to the refund account associated with the Fee. func (k Keeper) DistributePacketFees(ctx sdk.Context, forwardRelayer string, reverseRelayer sdk.AccAddress, feesInEscrow []types.IdentifiedPacketFee) { - // distribute fee for forward relaying forwardAddr, fErr := sdk.AccAddressFromBech32(forwardRelayer) for _, packetFee := range feesInEscrow { - if fErr == nil { - k.distributeFee(ctx, forwardAddr, packetFee.Fee.RecvFee) - } - - // distribute fee for reverse relaying - k.distributeFee(ctx, reverseRelayer, packetFee.Fee.AckFee) - - // refund timeout fee refund, refundAddr, err := sdk.AccAddressFromBech32(packetFee.RefundAddress) if err != nil { panic(fmt.Sprintf("could not parse refundAcc %s to sdk.AccAddress", packetFee.RefundAddress)) } - if fErr != nil { + if fErr == nil { + // distribute fee for forward relaying + k.distributeFee(ctx, forwardAddr, packetFee.Fee.RecvFee) + } else { // refund onRecv fee as forward relayer is not valid address k.distributeFee(ctx, refundAddr, packetFee.Fee.RecvFee) } + // distribute fee for reverse relaying + k.distributeFee(ctx, reverseRelayer, packetFee.Fee.AckFee) + // refund timeout fee for unused timeout k.distributeFee(ctx, refundAddr, packetFee.Fee.TimeoutFee) } From 9d8f139dd08d27b766217a7c9ae4a10c25a3739d Mon Sep 17 00:00:00 2001 From: Sean King Date: Wed, 16 Feb 2022 15:25:54 +0100 Subject: [PATCH 10/10] refactor: using .Empty() helper func for code hygiene --- modules/apps/29-fee/keeper/escrow.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/modules/apps/29-fee/keeper/escrow.go b/modules/apps/29-fee/keeper/escrow.go index 13885015673..e97096271b7 100644 --- a/modules/apps/29-fee/keeper/escrow.go +++ b/modules/apps/29-fee/keeper/escrow.go @@ -44,7 +44,7 @@ func (k Keeper) EscrowPacketFee(ctx sdk.Context, identifiedFee types.IdentifiedP // DistributePacketFees pays the acknowledgement fee & receive fee for a given packetId while refunding the timeout fee to the refund account associated with the Fee. func (k Keeper) DistributePacketFees(ctx sdk.Context, forwardRelayer string, reverseRelayer sdk.AccAddress, feesInEscrow []types.IdentifiedPacketFee) { - forwardAddr, fErr := sdk.AccAddressFromBech32(forwardRelayer) + forwardAddr, _ := sdk.AccAddressFromBech32(forwardRelayer) for _, packetFee := range feesInEscrow { refundAddr, err := sdk.AccAddressFromBech32(packetFee.RefundAddress) @@ -52,7 +52,8 @@ func (k Keeper) DistributePacketFees(ctx sdk.Context, forwardRelayer string, rev panic(fmt.Sprintf("could not parse refundAcc %s to sdk.AccAddress", packetFee.RefundAddress)) } - if fErr == nil { + // distribute fee to valid forward relayer address otherwise refund the fee + if !forwardAddr.Empty() { // distribute fee for forward relaying k.distributeFee(ctx, forwardAddr, packetFee.Fee.RecvFee) } else {