Skip to content

Commit

Permalink
29-Fee: Genesis (#557)
Browse files Browse the repository at this point in the history
* proto: adding genesis state

* feat: add GetAllIdentifiedPacketFees

* feat: adding genesis.go & updating proto + app.go

* fix: removing PortId from genesis

* feat: adding GetAll for relayer addr/fee enabled chan + update genesis

* test: TestExportGenesis

* feat: update type + hook up to module.go

* fix: remove PortKey

* fix: imports + remove scoped keeper

* nit: using NewPacketId helper and updating helper def to have correct params
  • Loading branch information
seantking authored Nov 24, 2021
1 parent 8ebbe18 commit d419972
Show file tree
Hide file tree
Showing 14 changed files with 1,019 additions and 64 deletions.
41 changes: 41 additions & 0 deletions docs/ibc/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
- [IdentifiedPacketFee](#ibc.applications.fee.v1.IdentifiedPacketFee)

- [ibc/applications/fee/v1/genesis.proto](#ibc/applications/fee/v1/genesis.proto)
- [FeeEnabledChannel](#ibc.applications.fee.v1.FeeEnabledChannel)
- [GenesisState](#ibc.applications.fee.v1.GenesisState)
- [RegisteredRelayerAddress](#ibc.applications.fee.v1.RegisteredRelayerAddress)

- [ibc/applications/fee/v1/query.proto](#ibc/applications/fee/v1/query.proto)
- [QueryIncentivizedPacketRequest](#ibc.applications.fee.v1.QueryIncentivizedPacketRequest)
Expand Down Expand Up @@ -670,12 +672,51 @@ and an optional list of relayers that are permitted to receive the fee.



<a name="ibc.applications.fee.v1.FeeEnabledChannel"></a>

### FeeEnabledChannel
Contains the PortID & ChannelID for a fee enabled channel


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `port_id` | [string](#string) | | |
| `channel_id` | [string](#string) | | |






<a name="ibc.applications.fee.v1.GenesisState"></a>

### GenesisState
GenesisState defines the fee middleware genesis state


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `identified_fees` | [IdentifiedPacketFee](#ibc.applications.fee.v1.IdentifiedPacketFee) | repeated | |
| `fee_enabled_channels` | [FeeEnabledChannel](#ibc.applications.fee.v1.FeeEnabledChannel) | repeated | |
| `registered_relayers` | [RegisteredRelayerAddress](#ibc.applications.fee.v1.RegisteredRelayerAddress) | repeated | |






<a name="ibc.applications.fee.v1.RegisteredRelayerAddress"></a>

### RegisteredRelayerAddress
Contains the address and counterparty address for a specific relayer (for distributing fees)


| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| `address` | [string](#string) | | |
| `counterparty_address` | [string](#string) | | |





Expand Down
1 change: 0 additions & 1 deletion modules/apps/29-fee/keeper/escrow.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

"github.com/cosmos/ibc-go/modules/apps/29-fee/types"
Expand Down
14 changes: 7 additions & 7 deletions modules/apps/29-fee/keeper/escrow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (suite *KeeperTestSuite) TestEscrowPacketFee() {
ackFee = validCoins
receiveFee = validCoins2
timeoutFee = validCoins3
packetId := &channeltypes.PacketId{ChannelId: validChannelId, PortId: types.PortKey, Sequence: uint64(1)}
packetId := &channeltypes.PacketId{ChannelId: validChannelId, PortId: types.PortID, Sequence: uint64(1)}

tc.malleate()
fee := types.Fee{ackFee, receiveFee, timeoutFee}
Expand Down Expand Up @@ -130,7 +130,7 @@ func (suite *KeeperTestSuite) TestDistributeFee() {
{
"fee not found for packet", func() {
// setting packetId with an invalid sequence of 2
packetId = &channeltypes.PacketId{ChannelId: validChannelId, PortId: types.PortKey, Sequence: uint64(2)}
packetId = &channeltypes.PacketId{ChannelId: validChannelId, PortId: types.PortID, Sequence: uint64(2)}
}, false,
},
}
Expand All @@ -149,7 +149,7 @@ func (suite *KeeperTestSuite) TestDistributeFee() {
ackFee = validCoins
receiveFee = validCoins2
timeoutFee = validCoins3
packetId = &channeltypes.PacketId{ChannelId: validChannelId, PortId: types.PortKey, Sequence: validSeq}
packetId = &channeltypes.PacketId{ChannelId: validChannelId, PortId: types.PortID, Sequence: validSeq}
fee := types.Fee{receiveFee, ackFee, timeoutFee}

// escrow the packet fee & store the fee in state
Expand Down Expand Up @@ -188,7 +188,7 @@ func (suite *KeeperTestSuite) TestDistributeFee() {

} else {
suite.Require().Error(err)
invalidPacketID := &channeltypes.PacketId{PortId: types.PortKey, ChannelId: validChannelId, Sequence: 1}
invalidPacketID := &channeltypes.PacketId{PortId: types.PortID, ChannelId: validChannelId, Sequence: 1}
hasFeeInEscrow := suite.chainA.GetSimApp().IBCFeeKeeper.HasFeeInEscrow(suite.chainA.GetContext(), invalidPacketID)
// there should still be a fee in escrow for this packet
suite.Require().True(hasFeeInEscrow)
Expand Down Expand Up @@ -222,7 +222,7 @@ func (suite *KeeperTestSuite) TestDistributeTimeoutFee() {
{
"fee not found for packet", func() {
// setting packetId with an invalid sequence of 2
packetId = &channeltypes.PacketId{ChannelId: validChannelId, PortId: types.PortKey, Sequence: uint64(2)}
packetId = &channeltypes.PacketId{ChannelId: validChannelId, PortId: types.PortID, Sequence: uint64(2)}
}, false,
},
}
Expand All @@ -240,7 +240,7 @@ func (suite *KeeperTestSuite) TestDistributeTimeoutFee() {
ackFee = validCoins
receiveFee = validCoins2
timeoutFee = validCoins3
packetId = &channeltypes.PacketId{ChannelId: validChannelId, PortId: types.PortKey, Sequence: validSeq}
packetId = &channeltypes.PacketId{ChannelId: validChannelId, PortId: types.PortID, Sequence: validSeq}
fee := types.Fee{receiveFee, ackFee, timeoutFee}

// escrow the packet fee & store the fee in state
Expand Down Expand Up @@ -274,7 +274,7 @@ func (suite *KeeperTestSuite) TestDistributeTimeoutFee() {

} else {
suite.Require().Error(err)
invalidPacketID := &channeltypes.PacketId{PortId: types.PortKey, ChannelId: validChannelId, Sequence: 1}
invalidPacketID := &channeltypes.PacketId{PortId: types.PortID, ChannelId: validChannelId, Sequence: 1}
hasFeeInEscrow := suite.chainA.GetSimApp().IBCFeeKeeper.HasFeeInEscrow(suite.chainA.GetContext(), invalidPacketID)
// there should still be a fee in escrow for this packet
suite.Require().True(hasFeeInEscrow)
Expand Down
21 changes: 18 additions & 3 deletions modules/apps/29-fee/keeper/genesis.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
package keeper

/*
import (
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/cosmos/ibc-go/modules/apps/29-fee/types"
)

// InitGenesis
func (k Keeper) InitGenesis(ctx sdk.Context, state types.GenesisState) {
for _, fee := range state.IdentifiedFees {
k.SetFeeInEscrow(ctx, fee)
}

for _, addr := range state.RegisteredRelayers {
k.SetCounterpartyAddress(ctx, addr.Address, addr.CounterpartyAddress)
}

for _, enabledChan := range state.FeeEnabledChannels {
k.SetFeeEnabled(ctx, enabledChan.PortId, enabledChan.ChannelId)
}
}

// ExportGenesis
func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
return &types.GenesisState{}
return &types.GenesisState{
IdentifiedFees: k.GetAllIdentifiedPacketFees(ctx),
FeeEnabledChannels: k.GetAllFeeEnabledChannels(ctx),
RegisteredRelayers: k.GetAllRelayerAddresses(ctx),
}
}
*/
112 changes: 112 additions & 0 deletions modules/apps/29-fee/keeper/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package keeper_test

import (
"github.com/cosmos/ibc-go/modules/apps/29-fee/types"
transfertypes "github.com/cosmos/ibc-go/modules/apps/transfer/types"
ibctesting "github.com/cosmos/ibc-go/testing"
)

func (suite *KeeperTestSuite) TestInitGenesis() {
suite.SetupTest()

// build PacketId & Fee
refundAcc := suite.chainA.SenderAccount.GetAddress()
packetId := types.NewPacketId(
ibctesting.FirstChannelID,
types.PortID,
uint64(1),
)
fee := types.Fee{
validCoins,
validCoins2,
validCoins3,
}

// relayer addresses
sender := suite.chainA.SenderAccount.GetAddress().String()
counterparty := suite.chainB.SenderAccount.GetAddress().String()

genesisState := types.GenesisState{
IdentifiedFees: []*types.IdentifiedPacketFee{
{
PacketId: packetId,
Fee: fee,
RefundAddress: refundAcc.String(),
Relayers: nil,
},
},
FeeEnabledChannels: []*types.FeeEnabledChannel{
{
PortId: transfertypes.PortID,
ChannelId: ibctesting.FirstChannelID,
},
},
RegisteredRelayers: []*types.RegisteredRelayerAddress{
{
Address: sender,
CounterpartyAddress: counterparty,
},
},
}

suite.chainA.GetSimApp().IBCFeeKeeper.InitGenesis(suite.chainA.GetContext(), genesisState)

// check fee
identifiedFee, found := suite.chainA.GetSimApp().IBCFeeKeeper.GetFeeInEscrow(suite.chainA.GetContext(), packetId)
suite.Require().True(found)
suite.Require().Equal(genesisState.IdentifiedFees[0], &identifiedFee)

// check fee is enabled
isEnabled := suite.chainA.GetSimApp().IBCFeeKeeper.IsFeeEnabled(suite.chainA.GetContext(), transfertypes.PortID, ibctesting.FirstChannelID)
suite.Require().True(isEnabled)

// check relayers
addr, found := suite.chainA.GetSimApp().IBCFeeKeeper.GetCounterpartyAddress(suite.chainA.GetContext(), sender)
suite.Require().True(found)
suite.Require().Equal(genesisState.RegisteredRelayers[0].CounterpartyAddress, addr)
}

func (suite *KeeperTestSuite) TestExportGenesis() {
suite.SetupTest()
// set fee enabled
suite.chainA.GetSimApp().IBCFeeKeeper.SetFeeEnabled(suite.chainA.GetContext(), transfertypes.PortID, ibctesting.FirstChannelID)

// setup & escrow the packet fee
refundAcc := suite.chainA.SenderAccount.GetAddress()
packetId := types.NewPacketId(
ibctesting.FirstChannelID,
types.PortID,
uint64(1),
)
fee := types.Fee{
validCoins,
validCoins2,
validCoins3,
}
identifiedPacketFee := &types.IdentifiedPacketFee{PacketId: packetId, Fee: fee, RefundAddress: refundAcc.String(), Relayers: []string{}}
err := suite.chainA.GetSimApp().IBCFeeKeeper.EscrowPacketFee(suite.chainA.GetContext(), identifiedPacketFee)
suite.Require().NoError(err)

// relayer addresses
sender := suite.chainA.SenderAccount.GetAddress().String()
counterparty := suite.chainB.SenderAccount.GetAddress().String()
// set counterparty address
suite.chainA.GetSimApp().IBCFeeKeeper.SetCounterpartyAddress(suite.chainA.GetContext(), sender, counterparty)

// export genesis
genesisState := suite.chainA.GetSimApp().IBCFeeKeeper.ExportGenesis(suite.chainA.GetContext())

// check fee enabled
suite.Require().Equal(ibctesting.FirstChannelID, genesisState.FeeEnabledChannels[0].ChannelId)
suite.Require().Equal(transfertypes.PortID, genesisState.FeeEnabledChannels[0].PortId)

// check fee
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)

// check registered relayer addresses
suite.Require().Equal(sender, genesisState.RegisteredRelayers[0].Address)
suite.Require().Equal(counterparty, genesisState.RegisteredRelayers[0].CounterpartyAddress)
}
10 changes: 5 additions & 5 deletions modules/apps/29-fee/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func (suite *KeeperTestSuite) TestQueryIncentivizedPacket() {
)

// setup
validPacketId := types.NewPacketId(ibctesting.FirstChannelID, 1)
invalidPacketId := types.NewPacketId(ibctesting.FirstChannelID, 2)
validPacketId := types.NewPacketId(ibctesting.FirstChannelID, types.PortID, 1)
invalidPacketId := types.NewPacketId(ibctesting.FirstChannelID, types.PortID, 2)
identifiedPacketFee := types.NewIdentifiedPacketFee(
validPacketId,
types.Fee{
Expand Down Expand Up @@ -110,9 +110,9 @@ func (suite *KeeperTestSuite) TestQueryIncentivizedPackets() {
func() {
refundAcc := suite.chainA.SenderAccount.GetAddress()

fee1 := types.NewIdentifiedPacketFee(types.NewPacketId(ibctesting.FirstChannelID, 1), fee, refundAcc.String(), []string(nil))
fee2 := types.NewIdentifiedPacketFee(types.NewPacketId(ibctesting.FirstChannelID, 2), fee, refundAcc.String(), []string(nil))
fee3 := types.NewIdentifiedPacketFee(types.NewPacketId(ibctesting.FirstChannelID, 3), fee, refundAcc.String(), []string(nil))
fee1 := types.NewIdentifiedPacketFee(types.NewPacketId(ibctesting.FirstChannelID, types.PortID, 1), fee, refundAcc.String(), []string(nil))
fee2 := types.NewIdentifiedPacketFee(types.NewPacketId(ibctesting.FirstChannelID, types.PortID, 2), fee, refundAcc.String(), []string(nil))
fee3 := types.NewIdentifiedPacketFee(types.NewPacketId(ibctesting.FirstChannelID, types.PortID, 3), fee, refundAcc.String(), []string(nil))

expPackets = []*types.IdentifiedPacketFee{}
expPackets = append(expPackets, fee1, fee2, fee3)
Expand Down
Loading

0 comments on commit d419972

Please sign in to comment.