Skip to content

Commit

Permalink
[BP: release/v6 <- #202] refactor: remove the ability to take a fee…
Browse files Browse the repository at this point in the history
… for each forwarded packet (#206)

* refactor: remove the ability to take a fee for each forwarded packet (#202)

* refactor: remove the ability to take a fee that is sent to the community pool

* fix: mocks

* fix: remove WithFee unit test (no longer used due to fee refactor)

* fix(e2e): upgrade test

---------

Co-authored-by: Reece Williams <[email protected]>
(cherry picked from commit 26d8080)

# Conflicts:
#	middleware/packet-forward-middleware/e2e/upgrade_test.go
#	middleware/packet-forward-middleware/go.mod
#	middleware/packet-forward-middleware/packetforward/client/cli/cli.go
#	middleware/packet-forward-middleware/packetforward/keeper/genesis.go
#	middleware/packet-forward-middleware/packetforward/keeper/grpc_query.go
#	middleware/packet-forward-middleware/packetforward/keeper/keeper.go
#	middleware/packet-forward-middleware/packetforward/keeper/params.go
#	middleware/packet-forward-middleware/packetforward/module.go
#	middleware/packet-forward-middleware/packetforward/module_test.go
#	middleware/packet-forward-middleware/packetforward/types/codec.go
#	middleware/packet-forward-middleware/packetforward/types/expected_keepers.go
#	middleware/packet-forward-middleware/packetforward/types/genesis.pb.go
#	middleware/packet-forward-middleware/packetforward/types/params.go
#	middleware/packet-forward-middleware/packetforward/types/query.pb.go
#	middleware/packet-forward-middleware/packetforward/types/query.pb.gw.go
#	middleware/packet-forward-middleware/proto/packetforward/v1/genesis.proto
#	middleware/packet-forward-middleware/proto/packetforward/v1/query.proto
#	middleware/packet-forward-middleware/test/mock/bank_keeper.go
#	middleware/packet-forward-middleware/test/mock/channel_keeper.go
#	middleware/packet-forward-middleware/test/mock/distribution_keeper.go
#	middleware/packet-forward-middleware/test/mock/ibc_module.go
#	middleware/packet-forward-middleware/test/mock/ics4_wrapper.go
#	middleware/packet-forward-middleware/test/mock/transfer_keeper.go
#	middleware/packet-forward-middleware/test/setup.go
#	middleware/packet-forward-middleware/testing/simapp/app.go
#	middleware/packet-forward-middleware/testing/simapp/upgrades/upgrades.go

* refactor: backport changes to remove the ability to take a fee that is sent to the community pool

* chore: fix linter errors

---------

Co-authored-by: Justin Tieri <[email protected]>
  • Loading branch information
mergify[bot] and jtieri authored Aug 6, 2024
1 parent 8e503d4 commit 6e1d3ea
Show file tree
Hide file tree
Showing 23 changed files with 3,676 additions and 1,378 deletions.
1 change: 0 additions & 1 deletion middleware/packet-forward-middleware/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ cosmovisor:
mocks: $(MOCKS_DIR)
go install go.uber.org/mock/[email protected]
mockgen -package=mock -destination=./test/mock/transfer_keeper.go $(GOMOD)/packetforward/types TransferKeeper
mockgen -package=mock -destination=./test/mock/distribution_keeper.go $(GOMOD)/packetforward/types DistributionKeeper
mockgen -package=mock -destination=./test/mock/bank_keeper.go $(GOMOD)/packetforward/types BankKeeper
mockgen -package=mock -destination=./test/mock/channel_keeper.go $(GOMOD)/packetforward/types ChannelKeeper
mockgen -package=mock -destination=./test/mock/ics4_wrapper.go github.com/cosmos/ibc-go/v6/modules/core/05-port/types ICS4Wrapper
Expand Down
141 changes: 140 additions & 1 deletion middleware/packet-forward-middleware/e2e/forward_timeout_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ func TestTimeoutOnForward(t *testing.T) {
_, err = testutil.PollForAck(ctx, chainA, chainAHeight, chainAHeight+30, transferTx.Packet)
require.NoError(t, err)

err = testutil.WaitForBlocks(ctx, 5, chainA)
err = testutil.WaitForBlocks(ctx, 10, chainA)
require.NoError(t, err)

// Assert balances are updated to reflect tokens now being on ChainD
Expand Down Expand Up @@ -394,4 +394,143 @@ func TestTimeoutOnForward(t *testing.T) {
require.True(t, firstHopEscrowBalance.Equal(transferAmount))
require.True(t, secondHopEscrowBalance.Equal(transferAmount))
require.True(t, thirdHopEscrowBalance.Equal(transferAmount))

// ---

// Compose IBC tx that will go from ChainD -> ChainC -> ChainB -> ChainA and succeed.
transfer = ibc.WalletAmount{
Address: userC.FormattedAddress(),
Denom: thirdHopDenom,
Amount: transferAmount,
}

secondHopMetadata = &PacketMetadata{
Forward: &ForwardMetadata{
Receiver: userA.FormattedAddress(),
Channel: baChan.ChannelID,
Port: baChan.PortID,
},
}
nextBz, err = json.Marshal(secondHopMetadata)
require.NoError(t, err)
next = string(nextBz)

firstHopMetadata = &PacketMetadata{
Forward: &ForwardMetadata{
Receiver: userB.FormattedAddress(),
Channel: cbChan.ChannelID,
Port: cbChan.PortID,
Next: &next,
},
}

memo, err = json.Marshal(firstHopMetadata)
require.NoError(t, err)

chainDHeight, err = chainD.Height(ctx)
require.NoError(t, err)

transferTx, err = chainD.SendIBCTransfer(ctx, dcChan.ChannelID, userD.KeyName(), transfer, ibc.TransferOptions{Memo: string(memo)})
require.NoError(t, err)

_, err = testutil.PollForAck(ctx, chainD, chainDHeight, chainDHeight+25, transferTx.Packet)
require.NoError(t, err)

err = testutil.WaitForBlocks(ctx, 5, chainD)
require.NoError(t, err)

// Assert balances to ensure timeout happened and user funds are still present on ChainD
chainABalance, err = chainA.GetBalance(ctx, userA.FormattedAddress(), chainA.Config().Denom)
require.NoError(t, err)

chainBBalance, err = chainB.GetBalance(ctx, userB.FormattedAddress(), firstHopIBCDenom)
require.NoError(t, err)

chainCBalance, err = chainC.GetBalance(ctx, userC.FormattedAddress(), secondHopIBCDenom)
require.NoError(t, err)

chainDBalance, err = chainD.GetBalance(ctx, userD.FormattedAddress(), thirdHopIBCDenom)
require.NoError(t, err)

require.True(t, chainABalance.Equal(initBal))
require.True(t, chainBBalance.Equal(zeroBal))
require.True(t, chainCBalance.Equal(zeroBal))
require.True(t, chainDBalance.Equal(zeroBal))

firstHopEscrowBalance, err = chainA.GetBalance(ctx, firstHopEscrowAccount, chainA.Config().Denom)
require.NoError(t, err)

secondHopEscrowBalance, err = chainB.GetBalance(ctx, secondHopEscrowAccount, firstHopIBCDenom)
require.NoError(t, err)

thirdHopEscrowBalance, err = chainC.GetBalance(ctx, thirdHopEscrowAccount, secondHopIBCDenom)
require.NoError(t, err)

require.True(t, firstHopEscrowBalance.Equal(zeroBal))
require.True(t, secondHopEscrowBalance.Equal(zeroBal))
require.True(t, thirdHopEscrowBalance.Equal(zeroBal))

// ----- 2

// Compose IBC tx that will go from ChainD -> ChainC -> ChainB -> ChainA and succeed.
transfer = ibc.WalletAmount{
Address: userB.FormattedAddress(),
Denom: chainA.Config().Denom,
Amount: transferAmount,
}

firstHopMetadata = &PacketMetadata{
Forward: &ForwardMetadata{
Receiver: userA.FormattedAddress(),
Channel: baChan.ChannelID,
Port: baChan.PortID,
Timeout: 1 * time.Second,
},
}

memo, err = json.Marshal(firstHopMetadata)
require.NoError(t, err)

chainAHeight, err = chainA.Height(ctx)
require.NoError(t, err)

transferTx, err = chainA.SendIBCTransfer(ctx, abChan.ChannelID, userA.KeyName(), transfer, ibc.TransferOptions{Memo: string(memo)})
require.NoError(t, err)

_, err = testutil.PollForAck(ctx, chainA, chainAHeight, chainAHeight+25, transferTx.Packet)
require.NoError(t, err)

err = testutil.WaitForBlocks(ctx, 5, chainA)
require.NoError(t, err)

// Assert balances to ensure timeout happened and user funds are still present on ChainD
chainABalance, err = chainA.GetBalance(ctx, userA.FormattedAddress(), chainA.Config().Denom)
require.NoError(t, err)

chainBBalance, err = chainB.GetBalance(ctx, userB.FormattedAddress(), firstHopIBCDenom)
require.NoError(t, err)

chainCBalance, err = chainC.GetBalance(ctx, userC.FormattedAddress(), secondHopIBCDenom)
require.NoError(t, err)

chainDBalance, err = chainD.GetBalance(ctx, userD.FormattedAddress(), thirdHopIBCDenom)
require.NoError(t, err)

require.True(t, chainABalance.Equal(initBal))
require.True(t, chainBBalance.Equal(zeroBal))
require.True(t, chainCBalance.Equal(zeroBal))
require.True(t, chainDBalance.Equal(zeroBal))

firstHopEscrowBalance, err = chainA.GetBalance(ctx, firstHopEscrowAccount, chainA.Config().Denom)
require.NoError(t, err)

secondHopEscrowBalance, err = chainB.GetBalance(ctx, secondHopEscrowAccount, firstHopIBCDenom)
require.NoError(t, err)

thirdHopEscrowBalance, err = chainC.GetBalance(ctx, thirdHopEscrowAccount, secondHopIBCDenom)
require.NoError(t, err)

require.True(t, firstHopEscrowBalance.Equal(zeroBal))
require.True(t, secondHopEscrowBalance.Equal(zeroBal))
require.True(t, thirdHopEscrowBalance.Equal(zeroBal))
}
174 changes: 174 additions & 0 deletions middleware/packet-forward-middleware/e2e/upgrade_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package e2e

import (
"context"
"fmt"
"testing"
"time"

sdkmath "cosmossdk.io/math"
upgradetypes "cosmossdk.io/x/upgrade/types"
cosmosproto "github.com/cosmos/gogoproto/proto"
"github.com/docker/docker/client"
"github.com/strangelove-ventures/interchaintest/v8"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
"github.com/strangelove-ventures/interchaintest/v8/testutil"
"github.com/stretchr/testify/require"
)

const (
chainName = "simapp"
upgradeName = "v2" // x/params migration

haltHeightDelta = uint64(9) // will propose upgrade this many blocks in the future
blocksAfterUpgrade = uint64(7)

VotingPeriod = "15s"
MaxDepositPeriod = "10s"
)

var (
// baseChain is the current version of the chain that will be upgraded from
// docker image load -i ../prev_builds/pfm_7_0_1.tar
baseChain = ibc.DockerImage{
Repository: "pfm",
Version: "v7.0.1",
UidGid: "1025:1025",
}

// make local-image
upgradeTo = ibc.DockerImage{
Repository: "pfm",
Version: "local",
}
)

func TestPFMUpgrade(t *testing.T) {
CosmosChainUpgradeTest(t, chainName, upgradeTo.Repository, upgradeTo.Version, upgradeName)
}

func CosmosChainUpgradeTest(t *testing.T, chainName, upgradeRepo, upgradeDockerTag, upgradeName string) {
if testing.Short() {
t.Skip("skipping in short mode")
}

t.Parallel()

previousVersionGenesis := []cosmos.GenesisKV{
{
Key: "app_state.gov.params.voting_period",
Value: VotingPeriod,
},
{
Key: "app_state.gov.params.max_deposit_period",
Value: MaxDepositPeriod,
},
{
Key: "app_state.gov.params.min_deposit.0.denom",
Value: Denom,
},
}

// Upgrade default to use the base chain image
cfg := DefaultConfig
cfg.ModifyGenesis = cosmos.ModifyGenesis(previousVersionGenesis)
cfg.Images = []ibc.DockerImage{baseChain}

numVals, numNodes := 2, 0
chains := interchaintest.CreateChainWithConfig(t, numVals, numNodes, chainName, upgradeDockerTag, cfg)
chain := chains[0].(*cosmos.CosmosChain)

ctx, ic, client, _ := interchaintest.BuildInitialChain(t, chains, false)
t.Cleanup(func() {
ic.Close()
})

var userFunds = sdkmath.NewInt(10_000_000_000)
users := interchaintest.GetAndFundTestUsers(t, ctx, t.Name(), userFunds, chain)
chainUser := users[0]

// upgrade
height, err := chain.Height(ctx)
require.NoError(t, err, "error fetching height before submit upgrade proposal")

haltHeight := height + haltHeightDelta
proposalID := SubmitUpgradeProposal(t, ctx, chain, chainUser, upgradeName, haltHeight)

ValidatorVoting(t, ctx, chain, proposalID, height, haltHeight)
UpgradeNodes(t, ctx, chain, client, haltHeight, upgradeRepo, upgradeDockerTag)
}

func SubmitUpgradeProposal(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, user ibc.Wallet, upgradeName string, haltHeight uint64) string {
upgradeMsg := []cosmosproto.Message{
&upgradetypes.MsgSoftwareUpgrade{
// Gov Module account
Authority: "cosmos10d07y265gmmuvt4z0w9aw880jnsr700j6zn9kn",
Plan: upgradetypes.Plan{
Name: upgradeName,
Height: int64(haltHeight),
},
},
}

proposal, err := chain.BuildProposal(upgradeMsg, "Chain Upgrade "+upgradeName, "Summary desc", "ipfs://CID", fmt.Sprintf(`500000000%s`, chain.Config().Denom), user.KeyName(), false)
require.NoError(t, err, "error building proposal")

txProp, err := chain.SubmitProposal(ctx, user.KeyName(), proposal)
require.NoError(t, err, "error submitting proposal")

t.Log("txProp", txProp)
return txProp.ProposalID
}

func UpgradeNodes(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, client *client.Client, haltHeight uint64, upgradeRepo, upgradeBranchVersion string) {
// bring down nodes to prepare for upgrade
t.Log("stopping node(s)")
err := chain.StopAllNodes(ctx)
require.NoError(t, err, "error stopping node(s)")

// upgrade version on all nodes
t.Log("upgrading node(s)")
chain.UpgradeVersion(ctx, client, upgradeRepo, upgradeBranchVersion)

// start all nodes back up.
// validators reach consensus on first block after upgrade height
// and chain block production resumes.
t.Log("starting node(s)")
err = chain.StartAllNodes(ctx)
require.NoError(t, err, "error starting upgraded node(s)")

timeoutCtx, timeoutCtxCancel := context.WithTimeout(ctx, time.Second*60)
defer timeoutCtxCancel()

err = testutil.WaitForBlocks(timeoutCtx, int(blocksAfterUpgrade), chain)
require.NoError(t, err, "chain did not produce blocks after upgrade")

height, err := chain.Height(ctx)
require.NoError(t, err, "error fetching height after upgrade")

require.GreaterOrEqual(t, height, haltHeight+blocksAfterUpgrade, "height did not increment enough after upgrade")
}

func ValidatorVoting(t *testing.T, ctx context.Context, chain *cosmos.CosmosChain, proposalID string, height uint64, haltHeight uint64) {
err := chain.VoteOnProposalAllValidators(ctx, proposalID, cosmos.ProposalVoteYes)
require.NoError(t, err, "failed to submit votes")

_, err = cosmos.PollForProposalStatusV8(ctx, chain, height, height+haltHeightDelta, proposalID, cosmos.ProposalStatusPassedV8)
require.NoError(t, err, "proposal status did not change to passed in expected number of blocks")

timeoutCtx, timeoutCtxCancel := context.WithTimeout(ctx, time.Second*45)
defer timeoutCtxCancel()

height, err = chain.Height(ctx)
require.NoError(t, err, "error fetching height before upgrade")

// this should timeout due to chain halt at upgrade height.
_ = testutil.WaitForBlocks(timeoutCtx, int(haltHeight-height), chain)

height, err = chain.Height(ctx)
require.NoError(t, err, "error fetching height after chain should have halted")

// make sure that chain is halted
require.Equal(t, haltHeight, height, "height is not equal to halt height")
}
6 changes: 3 additions & 3 deletions middleware/packet-forward-middleware/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ require (
github.com/cosmos/cosmos-sdk v0.46.15
github.com/cosmos/ibc-go/v6 v6.2.0
github.com/gogo/protobuf v1.3.3
github.com/golang/protobuf v1.5.3
github.com/gorilla/mux v1.8.0
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/iancoleman/orderedmap v0.2.0
Expand All @@ -22,8 +21,6 @@ require (
github.com/tendermint/tendermint v0.34.29
github.com/tendermint/tm-db v0.6.7
go.uber.org/mock v0.2.0
google.golang.org/genproto/googleapis/api v0.0.0-20230629202037-9506855d4529
google.golang.org/grpc v1.56.2
)

require (
Expand Down Expand Up @@ -59,6 +56,7 @@ require (
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
github.com/golang/glog v1.1.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/gofuzz v1.2.0 // indirect
Expand Down Expand Up @@ -113,7 +111,9 @@ require (
golang.org/x/term v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
google.golang.org/genproto v0.0.0-20230525234025-438c736192d0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230629202037-9506855d4529 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230525234030-28d5490b6b19 // indirect
google.golang.org/grpc v1.56.2 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
Loading

0 comments on commit 6e1d3ea

Please sign in to comment.