From 5afb552560ca83ceb65a4c2af55ed7e04285f44c Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Mon, 22 May 2023 17:45:18 +0200 Subject: [PATCH 01/12] chore: remove legacy_ibc_testing --- app/consumer-democracy/app.go | 7 +- .../proposals_whitelisting_test.go | 8 +- app/consumer/app.go | 6 +- app/provider/app.go | 6 +- legacy_ibc_testing/README.md | 13 - legacy_ibc_testing/core/events.go | 48 -- legacy_ibc_testing/core/expected_keepers.go | 20 - .../simapp/helpers/test_helpers.go | 85 --- legacy_ibc_testing/simapp/test_helpers.go | 158 ----- legacy_ibc_testing/testing/app.go | 175 ----- legacy_ibc_testing/testing/chain.go | 655 ------------------ legacy_ibc_testing/testing/config.go | 72 -- legacy_ibc_testing/testing/coordinator.go | 253 ------- legacy_ibc_testing/testing/endpoint.go | 581 ---------------- legacy_ibc_testing/testing/events.go | 79 --- legacy_ibc_testing/testing/path.go | 99 --- legacy_ibc_testing/testing/utils.go | 32 - legacy_ibc_testing/testing/values.go | 53 -- tests/difference/core/driver/core_test.go | 17 +- tests/difference/core/driver/setup.go | 29 +- tests/integration/common.go | 67 +- tests/integration/democracy.go | 21 +- tests/integration/expired_client.go | 2 +- tests/integration/setup.go | 80 ++- tests/integration/slashing.go | 25 +- tests/integration/throttle.go | 225 ++++-- testutil/ibc_testing/generic_setup.go | 48 +- testutil/ibc_testing/specific_setup.go | 61 +- testutil/integration/interfaces.go | 6 +- testutil/integration/validators.go | 46 ++ testutil/simibc/chain_util.go | 42 +- testutil/simibc/relay_util.go | 5 +- testutil/simibc/relayed_path.go | 3 +- x/ccv/provider/keeper/keeper_test.go | 4 +- x/ccv/provider/keeper/relay_test.go | 4 +- x/ccv/types/utils_test.go | 4 +- 36 files changed, 496 insertions(+), 2543 deletions(-) delete mode 100644 legacy_ibc_testing/README.md delete mode 100644 legacy_ibc_testing/core/events.go delete mode 100644 legacy_ibc_testing/core/expected_keepers.go delete mode 100644 legacy_ibc_testing/simapp/helpers/test_helpers.go delete mode 100644 legacy_ibc_testing/simapp/test_helpers.go delete mode 100644 legacy_ibc_testing/testing/app.go delete mode 100644 legacy_ibc_testing/testing/chain.go delete mode 100644 legacy_ibc_testing/testing/config.go delete mode 100644 legacy_ibc_testing/testing/coordinator.go delete mode 100644 legacy_ibc_testing/testing/endpoint.go delete mode 100644 legacy_ibc_testing/testing/events.go delete mode 100644 legacy_ibc_testing/testing/path.go delete mode 100644 legacy_ibc_testing/testing/utils.go delete mode 100644 legacy_ibc_testing/testing/values.go create mode 100644 testutil/integration/validators.go diff --git a/app/consumer-democracy/app.go b/app/consumer-democracy/app.go index fee730d752..94d7372521 100644 --- a/app/consumer-democracy/app.go +++ b/app/consumer-democracy/app.go @@ -16,6 +16,8 @@ import ( ibchost "github.com/cosmos/ibc-go/v7/modules/core/exported" ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper" tendermint "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint" + ibctesting "github.com/cosmos/ibc-go/v7/testing" + ibctestingtypes "github.com/cosmos/ibc-go/v7/testing/types" "github.com/spf13/cast" autocliv1 "cosmossdk.io/api/cosmos/autocli/v1" @@ -77,6 +79,7 @@ import ( govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" + // add mint mint "github.com/cosmos/cosmos-sdk/x/mint" mintkeeper "github.com/cosmos/cosmos-sdk/x/mint/keeper" @@ -103,8 +106,6 @@ import ( tmos "github.com/cometbft/cometbft/libs/os" appparams "github.com/cosmos/interchain-security/v3/app/params" - ibctestingcore "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/core" - ibctesting "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/testing" testutil "github.com/cosmos/interchain-security/v3/testutil/integration" consumer "github.com/cosmos/interchain-security/v3/x/ccv/consumer" consumerkeeper "github.com/cosmos/interchain-security/v3/x/ccv/consumer/keeper" @@ -916,7 +917,7 @@ func (app *App) GetBaseApp() *baseapp.BaseApp { } // GetStakingKeeper implements the TestingApp interface. -func (app *App) GetStakingKeeper() ibctestingcore.StakingKeeper { +func (app *App) GetStakingKeeper() ibctestingtypes.StakingKeeper { return app.ConsumerKeeper } diff --git a/app/consumer-democracy/proposals_whitelisting_test.go b/app/consumer-democracy/proposals_whitelisting_test.go index 127ec82ec5..d0e5a7742d 100644 --- a/app/consumer-democracy/proposals_whitelisting_test.go +++ b/app/consumer-democracy/proposals_whitelisting_test.go @@ -3,16 +3,18 @@ package app_test import ( "testing" + ibctesting "github.com/cosmos/ibc-go/v7/testing" "github.com/stretchr/testify/require" appConsumer "github.com/cosmos/interchain-security/v3/app/consumer-democracy" - ibctesting "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/testing" icstestingutils "github.com/cosmos/interchain-security/v3/testutil/ibc_testing" + testutil "github.com/cosmos/interchain-security/v3/testutil/integration" ) func TestDemocracyGovernanceWhitelistingKeys(t *testing.T) { - chain := ibctesting.NewTestChain(t, ibctesting.NewCoordinator(t, 0), - icstestingutils.DemocracyConsumerAppIniter, "test") + _, valUpdates, _ := testutil.CreateValidators(t, 4) + ibctesting.DefaultTestingAppInit = icstestingutils.DemocracyConsumerAppIniter(valUpdates) + chain := ibctesting.NewTestChain(t, ibctesting.NewCoordinator(t, 0), "test") paramKeeper := chain.App.(*appConsumer.App).ParamsKeeper for paramKey := range appConsumer.LegacyWhitelistedParams { ss, ok := paramKeeper.GetSubspace(paramKey.Subspace) diff --git a/app/consumer/app.go b/app/consumer/app.go index c65447e244..0c94ed8d75 100644 --- a/app/consumer/app.go +++ b/app/consumer/app.go @@ -16,6 +16,8 @@ import ( ibchost "github.com/cosmos/ibc-go/v7/modules/core/exported" ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper" tendermint "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint" + ibctesting "github.com/cosmos/ibc-go/v7/testing" + ibctestingtypes "github.com/cosmos/ibc-go/v7/testing/types" "github.com/spf13/cast" autocliv1 "cosmossdk.io/api/cosmos/autocli/v1" @@ -86,8 +88,6 @@ import ( tmos "github.com/cometbft/cometbft/libs/os" appparams "github.com/cosmos/interchain-security/v3/app/params" - ibctestingcore "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/core" - ibctesting "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/testing" testutil "github.com/cosmos/interchain-security/v3/testutil/integration" ibcconsumer "github.com/cosmos/interchain-security/v3/x/ccv/consumer" ibcconsumerkeeper "github.com/cosmos/interchain-security/v3/x/ccv/consumer/keeper" @@ -732,7 +732,7 @@ func (app *App) GetBaseApp() *baseapp.BaseApp { } // GetStakingKeeper implements the TestingApp interface. -func (app *App) GetStakingKeeper() ibctestingcore.StakingKeeper { +func (app *App) GetStakingKeeper() ibctestingtypes.StakingKeeper { return app.ConsumerKeeper } diff --git a/app/provider/app.go b/app/provider/app.go index c054f4a00b..ef3f6508f7 100644 --- a/app/provider/app.go +++ b/app/provider/app.go @@ -19,6 +19,8 @@ import ( ibchost "github.com/cosmos/ibc-go/v7/modules/core/exported" ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper" tendermint "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint" + ibctesting "github.com/cosmos/ibc-go/v7/testing" + ibctestingtypes "github.com/cosmos/ibc-go/v7/testing/types" "github.com/spf13/cast" autocliv1 "cosmossdk.io/api/cosmos/autocli/v1" @@ -99,8 +101,6 @@ import ( tmos "github.com/cometbft/cometbft/libs/os" appparams "github.com/cosmos/interchain-security/v3/app/params" - ibctestingcore "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/core" - ibctesting "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/testing" testutil "github.com/cosmos/interchain-security/v3/testutil/integration" ibcprovider "github.com/cosmos/interchain-security/v3/x/ccv/provider" ibcproviderclient "github.com/cosmos/interchain-security/v3/x/ccv/provider/client" @@ -817,7 +817,7 @@ func (app *App) GetBaseApp() *baseapp.BaseApp { } // GetStakingKeeper implements the TestingApp interface. -func (app *App) GetStakingKeeper() ibctestingcore.StakingKeeper { +func (app *App) GetStakingKeeper() ibctestingtypes.StakingKeeper { return app.StakingKeeper } diff --git a/legacy_ibc_testing/README.md b/legacy_ibc_testing/README.md deleted file mode 100644 index 1337ec1412..0000000000 --- a/legacy_ibc_testing/README.md +++ /dev/null @@ -1,13 +0,0 @@ -# Legacy IBC Testing - -### `legacy_ibc_testing` is imported from [Informal Systems fork of ibc-go](https://github.com/informalsystems/ibc-go). It contains modifications to canonical ibc-go `v3.4.0` for testing purposes only. - -Crucially, Informal's fork contained changes to the [StakingKeeper interface](https://github.com/informalsystems/ibc-go/blob/interchain-security-v3.4.0/modules/core/02-client/types/expected_keepers.go#L12) that both consumer and providers would be expected to return from the testing method `GetStakingKeeper`. For consumer apps, this method would return and IBCKeeper type. This change could not be back-ported to `v3.4.0` of ibc-go as it would be api breaking. Instead, the relevant changes made to ibc-go were consolidated and copied directly into `interchain-security`. Once ICS upgrades ibc-go to a version that supports this change, `v5` at the earliest, this test helper directory can be removed. - -**Directory** -- Core - - Contains changes made in ibc-go `core/`, but do not contain any logic requiring they live in that directory. Includes an interface definition and a testing helper method. -- Simapp - - Includes test helper substitutions for ibc-go's `simapp/` used in the Diff tests. -- Testing - - Replaces ibc-go's `testing/` directory to facilitate ibc's `TestApp`'s implementation of `GetStakingKeeper` returning the relevant `StakingKeeper` interface enhanced in Informal's ibc-go fork. \ No newline at end of file diff --git a/legacy_ibc_testing/core/events.go b/legacy_ibc_testing/core/events.go deleted file mode 100644 index a01c14c685..0000000000 --- a/legacy_ibc_testing/core/events.go +++ /dev/null @@ -1,48 +0,0 @@ -package core - -import ( - "strconv" - - clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types" - "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" - - abci "github.com/cometbft/cometbft/abci/types" -) - -/* -TODO: Remove after upgrading to ibc-go v5 -legacy_ibc_testing is temporarily copied into the interchain-security repository for the purpose of testing only. -The integration test suites rely on modifications to ibc-go's test framework that cannot be back-ported to the canonical version that ics will rely on. -These files will be deprecated once ICS is able to upgrade to ibc-go v5. -*/ - -// ReconstructPacketFromEvent recreates a packet from an appropriate provided event -func ReconstructPacketFromEvent(event abci.Event) (packet types.Packet, err error) { - attrMap := make(map[string][]byte) - for _, attr := range event.Attributes { - attrMap[attr.Key] = []byte(attr.Value) - } - - sequence, err := strconv.Atoi(string(attrMap[string(types.AttributeKeySequence)])) - if err != nil { - return packet, err - } - timeoutTimestamp, err := strconv.Atoi(string(attrMap[string(types.AttributeKeyTimeoutTimestamp)])) - if err != nil { - return packet, err - } - timeoutHeight, err := clienttypes.ParseHeight(string(attrMap[string(types.AttributeKeyTimeoutHeight)])) - if err != nil { - return packet, err - } - return types.NewPacket( - attrMap[string(types.AttributeKeyData)], //nolint:staticcheck // data - uint64(sequence), - string(attrMap[string(types.AttributeKeySrcPort)]), // sourcePort, - string(attrMap[string(types.AttributeKeySrcChannel)]), // sourceChannel, - string(attrMap[string(types.AttributeKeyDstPort)]), // destinationPort, - string(attrMap[string(types.AttributeKeyDstChannel)]), // destinationChannel string, - timeoutHeight, - uint64(timeoutTimestamp), - ), nil -} diff --git a/legacy_ibc_testing/core/expected_keepers.go b/legacy_ibc_testing/core/expected_keepers.go deleted file mode 100644 index 90a9d40f3c..0000000000 --- a/legacy_ibc_testing/core/expected_keepers.go +++ /dev/null @@ -1,20 +0,0 @@ -package core - -import ( - "time" - - sdk "github.com/cosmos/cosmos-sdk/types" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" -) - -/* -TODO: Remove after upgrading to ibc-go v5 -legacy_ibc_testing is temporarily copied into the interchain-security repository for the purpose of testing only. -The integration test suites rely on modifications to ibc-go's test framework that cannot be back-ported to the canonical version that ics will rely on. -These files will be deprecated once ICS is able to upgrade to ibc-go v5. -*/ - -type StakingKeeper interface { - GetHistoricalInfo(ctx sdk.Context, height int64) (stakingtypes.HistoricalInfo, bool) - UnbondingTime(ctx sdk.Context) time.Duration -} diff --git a/legacy_ibc_testing/simapp/helpers/test_helpers.go b/legacy_ibc_testing/simapp/helpers/test_helpers.go deleted file mode 100644 index b3e5c80d5e..0000000000 --- a/legacy_ibc_testing/simapp/helpers/test_helpers.go +++ /dev/null @@ -1,85 +0,0 @@ -package helpers - -import ( - "math/rand" - - "github.com/cosmos/cosmos-sdk/client" - cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/simulation" - "github.com/cosmos/cosmos-sdk/types/tx/signing" - authsign "github.com/cosmos/cosmos-sdk/x/auth/signing" -) - -/* -TODO: Remove after upgrading to ibc-go v5 -legacy_ibc_testing is temporarily copied into the interchain-security repository for the purpose of testing only. -The integration test suites rely on modifications to ibc-go's test framework that cannot be back-ported to the canonical version that ics will rely on. -These files will be deprecated once ICS is able to upgrade to ibc-go v5. -*/ - -// SimAppChainID hardcoded chainID for simulation -const ( - DefaultGenTxGas = 1000000 -) - -// GenTx generates a signed mock transaction. -func GenTx(gen client.TxConfig, msgs []sdk.Msg, feeAmt sdk.Coins, gas uint64, chainID string, accNums, accSeqs []uint64, priv ...cryptotypes.PrivKey) (sdk.Tx, error) { - sigs := make([]signing.SignatureV2, len(priv)) - - // create a random length memo - r := rand.New(rand.NewSource(rand.Int63())) - - memo := simulation.RandStringOfLength(r, simulation.RandIntBetween(r, 0, 100)) - - signMode := gen.SignModeHandler().DefaultMode() - - // 1st round: set SignatureV2 with empty signatures, to set correct - // signer infos. - for i, p := range priv { - sigs[i] = signing.SignatureV2{ - PubKey: p.PubKey(), - Data: &signing.SingleSignatureData{ - SignMode: signMode, - }, - Sequence: accSeqs[i], - } - } - - tx := gen.NewTxBuilder() - err := tx.SetMsgs(msgs...) - if err != nil { - return nil, err - } - err = tx.SetSignatures(sigs...) - if err != nil { - return nil, err - } - tx.SetMemo(memo) - tx.SetFeeAmount(feeAmt) - tx.SetGasLimit(gas) - - // 2nd round: once all signer infos are set, every signer can sign. - for i, p := range priv { - signerData := authsign.SignerData{ - ChainID: chainID, - AccountNumber: accNums[i], - Sequence: accSeqs[i], - } - signBytes, err := gen.SignModeHandler().GetSignBytes(signMode, signerData, tx.GetTx()) - if err != nil { - panic(err) - } - sig, err := p.Sign(signBytes) - if err != nil { - panic(err) - } - sigs[i].Data.(*signing.SingleSignatureData).Signature = sig - err = tx.SetSignatures(sigs...) - if err != nil { - panic(err) - } - } - - return tx.GetTx(), nil -} diff --git a/legacy_ibc_testing/simapp/test_helpers.go b/legacy_ibc_testing/simapp/test_helpers.go deleted file mode 100644 index efdc359499..0000000000 --- a/legacy_ibc_testing/simapp/test_helpers.go +++ /dev/null @@ -1,158 +0,0 @@ -package simapp - -import ( - "bytes" - "encoding/hex" - "fmt" - "strconv" - "testing" - "time" - - "github.com/stretchr/testify/require" - - errorsmod "cosmossdk.io/errors" - - bam "github.com/cosmos/cosmos-sdk/baseapp" - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" - cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/types/errors" - - tmproto "github.com/cometbft/cometbft/proto/tendermint/types" - tmtypes "github.com/cometbft/cometbft/types" - - "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/simapp/helpers" -) - -/* -TODO: Remove after upgrading to ibc-go v5 -legacy_ibc_testing is temporarily copied into the interchain-security repository for the purpose of testing only. -The integration test suites rely on modifications to ibc-go's test framework that cannot be back-ported to the canonical version that ics will rely on. -These files will be deprecated once ICS is able to upgrade to ibc-go v5. -*/ - -// DefaultConsensusParams defines the default Tendermint consensus params used in -// SimApp testing. -var DefaultConsensusParams = &tmproto.ConsensusParams{ - Block: &tmproto.BlockParams{ - MaxBytes: 200000, - MaxGas: 2000000, - }, - Evidence: &tmproto.EvidenceParams{ - MaxAgeNumBlocks: 302400, - MaxAgeDuration: 504 * time.Hour, // 3 weeks is the max duration - MaxBytes: 10000, - }, - Validator: &tmproto.ValidatorParams{ - PubKeyTypes: []string{ - tmtypes.ABCIPubKeyTypeEd25519, - }, - }, -} - -type GenerateAccountStrategy func(int) []sdk.AccAddress - -// ConvertAddrsToValAddrs converts the provided addresses to ValAddress. -func ConvertAddrsToValAddrs(addrs []sdk.AccAddress) []sdk.ValAddress { - valAddrs := make([]sdk.ValAddress, len(addrs)) - - for i, addr := range addrs { - valAddrs[i] = sdk.ValAddress(addr) - } - - return valAddrs -} - -func TestAddr(addr, bech string) (sdk.AccAddress, error) { - res, err := sdk.AccAddressFromHexUnsafe(addr) - if err != nil { - return nil, err - } - bechexpected := res.String() - if bech != bechexpected { - return nil, fmt.Errorf("bech encoding doesn't match reference") - } - - bechres, err := sdk.AccAddressFromBech32(bech) - if err != nil { - return nil, err - } - if !bytes.Equal(bechres, res) { - return nil, err - } - - return res, nil -} - -// SignAndDeliver signs and delivers a transaction. No simulation occurs as the -// ibc testing package causes checkState and deliverState to diverge in block time. -// -// CONTRACT: BeginBlock must be called before this function. -func SignAndDeliver( - t *testing.T, txCfg client.TxConfig, app *bam.BaseApp, header tmproto.Header, msgs []sdk.Msg, - chainID string, accNums, accSeqs []uint64, expSimPass, expPass bool, priv ...cryptotypes.PrivKey, -) (sdk.GasInfo, *sdk.Result, error) { - t.Helper() - tx, err := helpers.GenTx( - txCfg, - msgs, - sdk.Coins{sdk.NewInt64Coin(sdk.DefaultBondDenom, 0)}, - helpers.DefaultGenTxGas, - chainID, - accNums, - accSeqs, - priv..., - ) - require.NoError(t, err) - - // Simulate a sending a transaction - gInfo, res, err := app.SimDeliver(txCfg.TxEncoder(), tx) - - if expPass { - require.NoError(t, err) - require.NotNil(t, res) - } else { - require.Error(t, err) - require.Nil(t, res) - } - - return gInfo, res, err -} - -// CreateTestPubKeys returns a total of numPubKeys public keys in ascending order. -func CreateTestPubKeys(numPubKeys int) []cryptotypes.PubKey { - var publicKeys []cryptotypes.PubKey - var buffer bytes.Buffer - - // start at 10 to avoid changing 1 to 01, 2 to 02, etc - for i := 100; i < (numPubKeys + 100); i++ { - numString := strconv.Itoa(i) - buffer.WriteString("0B485CFC0EECC619440448436F8FC9DF40566F2369E72400281454CB552AF") // base pubkey string - buffer.WriteString(numString) // adding on final two digits to make pubkeys unique - publicKeys = append(publicKeys, NewPubKeyFromHex(buffer.String())) - buffer.Reset() - } - - return publicKeys -} - -// NewPubKeyFromHex returns a PubKey from a hex string. -func NewPubKeyFromHex(pk string) (res cryptotypes.PubKey) { - pkBytes, err := hex.DecodeString(pk) - if err != nil { - panic(err) - } - if len(pkBytes) != ed25519.PubKeySize { - panic(errorsmod.Wrap(errors.ErrInvalidPubKey, "invalid pubkey size")) - } - return &ed25519.PubKey{Key: pkBytes} -} - -// EmptyAppOptions is a stub implementing AppOptions -type EmptyAppOptions struct{} - -// Get implements AppOptions -func (ao EmptyAppOptions) Get(o string) interface{} { - return nil -} diff --git a/legacy_ibc_testing/testing/app.go b/legacy_ibc_testing/testing/app.go deleted file mode 100644 index ff3c146a4a..0000000000 --- a/legacy_ibc_testing/testing/app.go +++ /dev/null @@ -1,175 +0,0 @@ -package testing - -import ( - "encoding/json" - "testing" - "time" - - "github.com/cosmos/ibc-go/v7/modules/core/keeper" - "github.com/stretchr/testify/require" - - "cosmossdk.io/math" - - "github.com/cosmos/cosmos-sdk/baseapp" - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/codec" - codectypes "github.com/cosmos/cosmos-sdk/codec/types" - cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" - storetypes "github.com/cosmos/cosmos-sdk/store/types" - sdk "github.com/cosmos/cosmos-sdk/types" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" - capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - - abci "github.com/cometbft/cometbft/abci/types" - tmproto "github.com/cometbft/cometbft/proto/tendermint/types" - tmtypes "github.com/cometbft/cometbft/types" - - "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/core" - "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/simapp" - consumertypes "github.com/cosmos/interchain-security/v3/x/ccv/consumer/types" - ccvtypes "github.com/cosmos/interchain-security/v3/x/ccv/types" -) - -/* -TODO: Remove after upgrading to ibc-go v5 -legacy_ibc_testing is temporarily copied into the interchain-security repository for the purpose of testing only. -The integration test suites rely on modifications to ibc-go's test framework that cannot be back-ported to the canonical version that ics will rely on. -These files will be deprecated once ICS is able to upgrade to ibc-go v5. -*/ - -type AppIniter func() (TestingApp, map[string]json.RawMessage) - -var DefaultTestingAppInit AppIniter - -type TestingApp interface { - abci.Application - - // ibc-go additions - GetBaseApp() *baseapp.BaseApp - GetStakingKeeper() core.StakingKeeper - GetIBCKeeper() *keeper.Keeper - GetScopedIBCKeeper() capabilitykeeper.ScopedKeeper - GetTxConfig() client.TxConfig - - // Implemented by SimApp - AppCodec() codec.Codec - - // Implemented by BaseApp - LastCommitID() storetypes.CommitID - LastBlockHeight() int64 -} - -// SetupWithGenesisValSet initializes a new SimApp with a validator set and genesis accounts -// that also act as delegators. For simplicity, each validator is bonded with a delegation -// of one consensus engine unit (10^6) in the default token of the simapp from first genesis -// account. A Nop logger is set in SimApp. -func SetupWithGenesisValSet(t *testing.T, appIniter AppIniter, valSet *tmtypes.ValidatorSet, genAccs []authtypes.GenesisAccount, chainID string, powerReduction math.Int, balances ...banktypes.Balance) TestingApp { - t.Helper() - app, genesisState := appIniter() - baseapp.SetChainID(chainID)(app.GetBaseApp()) - - // set genesis accounts - authGenesis := authtypes.NewGenesisState(authtypes.DefaultParams(), genAccs) - genesisState[authtypes.ModuleName] = app.AppCodec().MustMarshalJSON(authGenesis) - - validators := make([]stakingtypes.Validator, 0, len(valSet.Validators)) - delegations := make([]stakingtypes.Delegation, 0, len(valSet.Validators)) - - bondAmt := sdk.TokensFromConsensusPower(1, powerReduction) - - initValPowers := []abci.ValidatorUpdate{} - for _, val := range valSet.Validators { - pk, err := cryptocodec.FromTmPubKeyInterface(val.PubKey) - require.NoError(t, err) - pkAny, err := codectypes.NewAnyWithValue(pk) - require.NoError(t, err) - validator := stakingtypes.Validator{ - OperatorAddress: sdk.ValAddress(val.Address).String(), - ConsensusPubkey: pkAny, - Jailed: false, - Status: stakingtypes.Bonded, - Tokens: bondAmt, - DelegatorShares: sdk.OneDec(), - Description: stakingtypes.Description{}, - UnbondingHeight: int64(0), - UnbondingTime: time.Unix(0, 0).UTC(), - Commission: stakingtypes.NewCommission(sdk.ZeroDec(), sdk.ZeroDec(), sdk.ZeroDec()), - MinSelfDelegation: sdk.ZeroInt(), - } - - validators = append(validators, validator) - delegations = append(delegations, stakingtypes.NewDelegation(genAccs[0].GetAddress(), val.Address.Bytes(), sdk.OneDec())) - - pub, _ := val.ToProto() - initValPowers = append(initValPowers, abci.ValidatorUpdate{ - Power: val.VotingPower, - PubKey: pub.PubKey, - }) - } - - // set validators and delegations - var ( - stakingGenesis stakingtypes.GenesisState - consumerGenesis ccvtypes.GenesisState - bondDenom string - ) - if genesisState[stakingtypes.ModuleName] != nil { - app.AppCodec().MustUnmarshalJSON(genesisState[stakingtypes.ModuleName], &stakingGenesis) - bondDenom = stakingGenesis.Params.BondDenom - } else { - bondDenom = sdk.DefaultBondDenom - } - - // add bonded amount to bonded pool module account - balances = append(balances, banktypes.Balance{ - Address: authtypes.NewModuleAddress(stakingtypes.BondedPoolName).String(), - Coins: sdk.Coins{sdk.NewCoin(bondDenom, bondAmt.Mul(sdk.NewInt(int64(len(valSet.Validators)))))}, - }) - - // set validators and delegations - stakingGenesis = *stakingtypes.NewGenesisState(stakingGenesis.Params, validators, delegations) - genesisState[stakingtypes.ModuleName] = app.AppCodec().MustMarshalJSON(&stakingGenesis) - - // update total supply - bankGenesis := banktypes.NewGenesisState(banktypes.DefaultGenesisState().Params, balances, sdk.NewCoins(), []banktypes.Metadata{}, []banktypes.SendEnabled{}) - genesisState[banktypes.ModuleName] = app.AppCodec().MustMarshalJSON(bankGenesis) - - if genesisState[consumertypes.ModuleName] != nil { - app.AppCodec().MustUnmarshalJSON(genesisState[consumertypes.ModuleName], &consumerGenesis) - consumerGenesis.InitialValSet = initValPowers - consumerGenesis.Params.Enabled = true - genesisState[consumertypes.ModuleName] = app.AppCodec().MustMarshalJSON(&consumerGenesis) - } - - stateBytes, err := json.MarshalIndent(genesisState, "", " ") - require.NoError(t, err) - - // init chain will set the validator set and initialize the genesis accounts - app.InitChain( - abci.RequestInitChain{ - ChainId: chainID, - Validators: []abci.ValidatorUpdate{}, - ConsensusParams: simapp.DefaultConsensusParams, - AppStateBytes: stateBytes, - }, - ) - - // commit genesis changes - app.Commit() - - app.BeginBlock( - abci.RequestBeginBlock{ - Header: tmproto.Header{ - ChainID: chainID, - Height: app.LastBlockHeight() + 1, - AppHash: app.LastCommitID().Hash, - ValidatorsHash: valSet.Hash(), - NextValidatorsHash: valSet.Hash(), - }, - }, - ) - - return app -} diff --git a/legacy_ibc_testing/testing/chain.go b/legacy_ibc_testing/testing/chain.go deleted file mode 100644 index 14177679b7..0000000000 --- a/legacy_ibc_testing/testing/chain.go +++ /dev/null @@ -1,655 +0,0 @@ -package testing - -import ( - "bytes" - "fmt" - "testing" - "time" - - clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types" - channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" - commitmenttypes "github.com/cosmos/ibc-go/v7/modules/core/23-commitment/types" - host "github.com/cosmos/ibc-go/v7/modules/core/24-host" - "github.com/cosmos/ibc-go/v7/modules/core/exported" - "github.com/cosmos/ibc-go/v7/modules/core/types" - ibctmtypes "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint" - "github.com/cosmos/ibc-go/v7/testing/mock" - "github.com/stretchr/testify/require" - - errorsmod "cosmossdk.io/errors" - - "github.com/cosmos/cosmos-sdk/client" - "github.com/cosmos/cosmos-sdk/codec" - "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1" - cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - sdk "github.com/cosmos/cosmos-sdk/types" - authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" - banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" - capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper" - capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types" - teststaking "github.com/cosmos/cosmos-sdk/x/staking/testutil" - stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - - abci "github.com/cometbft/cometbft/abci/types" - "github.com/cometbft/cometbft/crypto/tmhash" - tmproto "github.com/cometbft/cometbft/proto/tendermint/types" - tmprotoversion "github.com/cometbft/cometbft/proto/tendermint/version" - tmtypes "github.com/cometbft/cometbft/types" - tmversion "github.com/cometbft/cometbft/version" - - ibctestingcore "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/core" - "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/simapp" -) - -/* -TODO: Remove after upgrading to ibc-go v5 -legacy_ibc_testing is temporarily copied into the interchain-security repository for the purpose of testing only. -The integration test suites rely on modifications to ibc-go's test framework that cannot be back-ported to the canonical version that ics will rely on. -These files will be deprecated once ICS is able to upgrade to ibc-go v5. -*/ - -var MaxAccounts = 10 - -type SenderAccount struct { - SenderPrivKey cryptotypes.PrivKey - SenderAccount authtypes.AccountI -} - -// TestChain is a testing struct that wraps a simapp with the last TM Header, the current ABCI -// header and the validators of the TestChain. It also contains a field called ChainID. This -// is the clientID that *other* chains use to refer to this TestChain. The SenderAccount -// is used for delivering transactions through the application state. -// NOTE: the actual application uses an empty chain-id for ease of testing. -type TestChain struct { - *testing.T - - Coordinator *Coordinator - App TestingApp - ChainID string - LastHeader *ibctmtypes.Header // header for last block height committed - CurrentHeader tmproto.Header // header for current block height - QueryServer types.QueryServer - TxConfig client.TxConfig - Codec codec.BinaryCodec - - Vals *tmtypes.ValidatorSet - NextVals *tmtypes.ValidatorSet - - // Signers is a map from validator address to the PrivValidator - // The map is converted into an array that is the same order as the validators right before signing commit - // This ensures that signers will always be in correct order even as validator powers change. - // If a test adds a new validator after chain creation, then the signer map must be updated to include - // the new PrivValidator entry. - Signers map[string]tmtypes.PrivValidator - - // SentPackets is a map from packet sequences to sent packets, - // reconstructed from emitted events of type SendPacketEvent - SentPackets map[string]channeltypes.Packet - - // autogenerated sender private key - SenderPrivKey cryptotypes.PrivKey - SenderAccount authtypes.AccountI - - SenderAccounts []SenderAccount -} - -// NewTestChainWithValSet initializes a new TestChain instance with the given validator set -// and signer array. It also initializes 10 Sender accounts with a balance of 10000000000000000000 coins of -// bond denom to use for tests. -// -// The first block height is committed to state in order to allow for client creations on -// counterparty chains. The TestChain will return with a block height starting at 2. -// -// Time management is handled by the Coordinator in order to ensure synchrony between chains. -// Each update of any chain increments the block header time for all chains by 5 seconds. -// -// NOTE: to use a custom sender privkey and account for testing purposes, replace and modify this -// constructor function. -// -// CONTRACT: Validator array must be provided in the order expected by Tendermint. -// i.e. sorted first by power and then lexicographically by address. -func NewTestChainWithValSet(t *testing.T, coord *Coordinator, appIniter AppIniter, chainID string, valSet *tmtypes.ValidatorSet, signers map[string]tmtypes.PrivValidator) *TestChain { - t.Helper() - genAccs := []authtypes.GenesisAccount{} - genBals := []banktypes.Balance{} - senderAccs := []SenderAccount{} - - // generate genesis accounts - for i := 0; i < MaxAccounts; i++ { - senderPrivKey := secp256k1.GenPrivKey() - acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), uint64(i), 0) - amount, ok := sdk.NewIntFromString("10000000000000000000") - require.True(t, ok) - - balance := banktypes.Balance{ - Address: acc.GetAddress().String(), - Coins: sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, amount)), - } - - genAccs = append(genAccs, acc) - genBals = append(genBals, balance) - - senderAcc := SenderAccount{ - SenderAccount: acc, - SenderPrivKey: senderPrivKey, - } - - senderAccs = append(senderAccs, senderAcc) - } - - app := SetupWithGenesisValSet(t, appIniter, valSet, genAccs, chainID, sdk.DefaultPowerReduction, genBals...) - - // create current header and call begin block - header := tmproto.Header{ - ChainID: chainID, - Height: 1, - Time: coord.CurrentTime.UTC(), - } - - txConfig := app.GetTxConfig() - - // create an account to send transactions from - chain := &TestChain{ - T: t, - Coordinator: coord, - ChainID: chainID, - App: app, - CurrentHeader: header, - QueryServer: app.GetIBCKeeper(), - TxConfig: txConfig, - Codec: app.AppCodec(), - Vals: valSet, - NextVals: valSet, - Signers: signers, - SentPackets: make(map[string]channeltypes.Packet), - SenderPrivKey: senderAccs[0].SenderPrivKey, - SenderAccount: senderAccs[0].SenderAccount, - SenderAccounts: senderAccs, - } - - coord.CommitBlock(chain) - - return chain -} - -// NewTestChain initializes a new test chain with a default of 4 validators -// Use this function if the tests do not need custom control over the validator set -func NewTestChain(t *testing.T, coord *Coordinator, appIniter AppIniter, chainID string) *TestChain { - t.Helper() - // generate validators private/public key - var ( - validatorsPerChain = 4 - validators []*tmtypes.Validator - signersByAddress = make(map[string]tmtypes.PrivValidator, validatorsPerChain) - ) - - for i := 0; i < validatorsPerChain; i++ { - privVal := mock.NewPV() - pubKey, err := privVal.GetPubKey() - require.NoError(t, err) - validators = append(validators, tmtypes.NewValidator(pubKey, 1)) - signersByAddress[pubKey.Address().String()] = privVal - } - - // construct validator set; - // Note that the validators are sorted by voting power - // or, if equal, by address lexical order - valSet := tmtypes.NewValidatorSet(validators) - - return NewTestChainWithValSet(t, coord, appIniter, chainID, valSet, signersByAddress) -} - -// GetContext returns the current context for the application. -func (chain *TestChain) GetContext() sdk.Context { - return chain.App.GetBaseApp().NewContext(false, chain.CurrentHeader) -} - -// QueryProof performs an abci query with the given key and returns the proto encoded merkle proof -// for the query and the height at which the proof will succeed on a tendermint verifier. -func (chain *TestChain) QueryProof(key []byte) ([]byte, clienttypes.Height) { - return chain.QueryProofAtHeight(key, chain.App.LastBlockHeight()) -} - -// QueryProof performs an abci query with the given key and returns the proto encoded merkle proof -// for the query and the height at which the proof will succeed on a tendermint verifier. -func (chain *TestChain) QueryProofAtHeight(key []byte, height int64) ([]byte, clienttypes.Height) { - res := chain.App.Query(abci.RequestQuery{ - Path: fmt.Sprintf("store/%s/key", exported.StoreKey), - Height: height - 1, - Data: key, - Prove: true, - }) - - merkleProof, err := commitmenttypes.ConvertProofs(res.ProofOps) - require.NoError(chain.T, err) - - proof, err := chain.App.AppCodec().Marshal(&merkleProof) - require.NoError(chain.T, err) - - revision := clienttypes.ParseChainID(chain.ChainID) - - // proof height + 1 is returned as the proof created corresponds to the height the proof - // was created in the IAVL tree. Tendermint and subsequently the clients that rely on it - // have heights 1 above the IAVL tree. Thus we return proof height + 1 - return proof, clienttypes.NewHeight(revision, uint64(res.Height)+1) -} - -// QueryUpgradeProof performs an abci query with the given key and returns the proto encoded merkle proof -// for the query and the height at which the proof will succeed on a tendermint verifier. -func (chain *TestChain) QueryUpgradeProof(key []byte, height uint64) ([]byte, clienttypes.Height) { - res := chain.App.Query(abci.RequestQuery{ - Path: "store/upgrade/key", - Height: int64(height - 1), - Data: key, - Prove: true, - }) - - merkleProof, err := commitmenttypes.ConvertProofs(res.ProofOps) - require.NoError(chain.T, err) - - proof, err := chain.App.AppCodec().Marshal(&merkleProof) - require.NoError(chain.T, err) - - revision := clienttypes.ParseChainID(chain.ChainID) - - // proof height + 1 is returned as the proof created corresponds to the height the proof - // was created in the IAVL tree. Tendermint and subsequently the clients that rely on it - // have heights 1 above the IAVL tree. Thus we return proof height + 1 - return proof, clienttypes.NewHeight(revision, uint64(res.Height+1)) -} - -// QueryConsensusStateProof performs an abci query for a consensus state -// stored on the given clientID. The proof and consensusHeight are returned. -func (chain *TestChain) QueryConsensusStateProof(clientID string) ([]byte, clienttypes.Height) { - clientState := chain.GetClientState(clientID) - - consensusHeight := clientState.GetLatestHeight().(clienttypes.Height) - consensusKey := host.FullConsensusStateKey(clientID, consensusHeight) - proofConsensus, _ := chain.QueryProof(consensusKey) - - return proofConsensus, consensusHeight -} - -// GetSentPacketKey returns a key for accessing a sent packet, -// given an ibc sequence number and the channel ID for the source endpoint. -func GetSentPacketKey(sequence uint64, channelID string) string { - return fmt.Sprintf("%s-%d", channelID, sequence) -} - -// GetSentPacket returns the sent packet with `sequence` (if any), -// reconstructed from emitted events of type SendPacketEvent -func (chain *TestChain) GetSentPacket(sequence uint64, channelID string) (packet channeltypes.Packet, found bool) { - sentPacketKey := GetSentPacketKey(sequence, channelID) - packet, found = chain.SentPackets[sentPacketKey] - return -} - -// setSentPacketsFromEvents stores the sent packet reconstructed -// from emitted events of type SendPacketEvent -func (chain *TestChain) setSentPacketsFromEvents(events []abci.Event) { - for _, event := range events { - if event.Type == channeltypes.EventTypeSendPacket { - packet, err := ibctestingcore.ReconstructPacketFromEvent(event) - require.NoError(chain.T, err) - sentPacketKey := GetSentPacketKey(packet.GetSequence(), packet.GetSourceChannel()) - chain.SentPackets[sentPacketKey] = packet - } - } -} - -// NextBlock sets the last header to the current header and increments the current header to be -// at the next block height. It does not update the time as that is handled by the Coordinator. -// It will call Endblock and Commit and apply the validator set changes to the next validators -// of the next block being created. This follows the Tendermint protocol of applying valset changes -// returned on block `n` to the validators of block `n+2`. -// It calls BeginBlock with the new block created before returning. -func (chain *TestChain) NextBlock() (abci.ResponseEndBlock, abci.ResponseCommit, abci.ResponseBeginBlock) { - ebRes := chain.App.EndBlock(abci.RequestEndBlock{Height: chain.CurrentHeader.Height}) - // store packets sent during EndBlock - chain.setSentPacketsFromEvents(ebRes.Events) - - cRes := chain.App.Commit() - - // val set changes returned from previous block get applied to the next validators - // of this block. See tendermint spec for details. - chain.Vals = chain.NextVals - chain.NextVals = ApplyValSetChanges(chain.T, chain.Vals, ebRes.ValidatorUpdates) - - // set the last header to the current header - // use nil trusted fields - chain.LastHeader = chain.CurrentTMClientHeader() - - // increment the current header - chain.CurrentHeader = tmproto.Header{ - ChainID: chain.ChainID, - Height: chain.App.LastBlockHeight() + 1, - AppHash: chain.App.LastCommitID().Hash, - // NOTE: the time is increased by the coordinator to maintain time synchrony amongst - // chains. - Time: chain.CurrentHeader.Time, - ValidatorsHash: chain.Vals.Hash(), - NextValidatorsHash: chain.NextVals.Hash(), - } - - bbRes := chain.App.BeginBlock(abci.RequestBeginBlock{Header: chain.CurrentHeader}) - // store packets sent during BeginBlock - chain.setSentPacketsFromEvents(bbRes.Events) - - return ebRes, cRes, bbRes -} - -// sendMsgs delivers a transaction through the application without returning the result. -func (chain *TestChain) sendMsgs(msgs ...sdk.Msg) error { - _, err := chain.SendMsgs(msgs...) - return err -} - -// SendMsgs delivers a transaction through the application. It updates the senders sequence -// number and updates the TestChain's headers. It returns the result and error if one -// occurred. -func (chain *TestChain) SendMsgs(msgs ...sdk.Msg) (*sdk.Result, error) { - // ensure the chain has the latest time - chain.Coordinator.UpdateTimeForChain(chain) - - _, r, err := simapp.SignAndDeliver( - chain.T, - chain.TxConfig, - chain.App.GetBaseApp(), - chain.GetContext().BlockHeader(), - msgs, - chain.ChainID, - []uint64{chain.SenderAccount.GetAccountNumber()}, - []uint64{chain.SenderAccount.GetSequence()}, - true, true, chain.SenderPrivKey, - ) - if err != nil { - return nil, err - } - // store packets sent during the execution of this transaction - chain.setSentPacketsFromEvents(r.Events) - - // NextBlock calls app.Commit() - chain.NextBlock() - - // increment sequence for successful transaction execution - err = chain.SenderAccount.SetSequence(chain.SenderAccount.GetSequence() + 1) - if err != nil { - return nil, err - } - - chain.Coordinator.IncrementTime() - - return r, nil -} - -// GetClientState retrieves the client state for the provided clientID. The client is -// expected to exist otherwise testing will fail. -func (chain *TestChain) GetClientState(clientID string) exported.ClientState { - clientState, found := chain.App.GetIBCKeeper().ClientKeeper.GetClientState(chain.GetContext(), clientID) - require.True(chain.T, found) - - return clientState -} - -// GetConsensusState retrieves the consensus state for the provided clientID and height. -// It will return a success boolean depending on if consensus state exists or not. -func (chain *TestChain) GetConsensusState(clientID string, height exported.Height) (exported.ConsensusState, bool) { - return chain.App.GetIBCKeeper().ClientKeeper.GetClientConsensusState(chain.GetContext(), clientID, height) -} - -// GetValsAtHeight will return the validator set of the chain at a given height. It will return -// a success boolean depending on if the validator set exists or not at that height. -func (chain *TestChain) GetValsAtHeight(height int64) (*tmtypes.ValidatorSet, bool) { - histInfo, ok := chain.App.GetStakingKeeper().GetHistoricalInfo(chain.GetContext(), height) - if !ok { - return nil, false - } - - valSet := stakingtypes.Validators(histInfo.Valset) - - tmValidators, err := teststaking.ToTmValidators(valSet, sdk.DefaultPowerReduction) - if err != nil { - panic(err) - } - return tmtypes.NewValidatorSet(tmValidators), true -} - -// GetAcknowledgement retrieves an acknowledgement for the provided packet. If the -// acknowledgement does not exist then testing will fail. -func (chain *TestChain) GetAcknowledgement(packet exported.PacketI) []byte { - ack, found := chain.App.GetIBCKeeper().ChannelKeeper.GetPacketAcknowledgement(chain.GetContext(), packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence()) - require.True(chain.T, found) - - return ack -} - -// GetPrefix returns the prefix for used by a chain in connection creation -func (chain *TestChain) GetPrefix() commitmenttypes.MerklePrefix { - return commitmenttypes.NewMerklePrefix(chain.App.GetIBCKeeper().ConnectionKeeper.GetCommitmentPrefix().Bytes()) -} - -// ConstructUpdateTMClientHeader will construct a valid 07-tendermint Header to update the -// light client on the source chain. -func (chain *TestChain) ConstructUpdateTMClientHeader(counterparty *TestChain, clientID string) (*ibctmtypes.Header, error) { - return chain.ConstructUpdateTMClientHeaderWithTrustedHeight(counterparty, clientID, clienttypes.ZeroHeight()) -} - -// ConstructUpdateTMClientHeader will construct a valid 07-tendermint Header to update the -// light client on the source chain. -func (chain *TestChain) ConstructUpdateTMClientHeaderWithTrustedHeight(counterparty *TestChain, clientID string, trustedHeight clienttypes.Height) (*ibctmtypes.Header, error) { - header := counterparty.LastHeader - // Relayer must query for LatestHeight on client to get TrustedHeight if the trusted height is not set - if trustedHeight.IsZero() { - trustedHeight = chain.GetClientState(clientID).GetLatestHeight().(clienttypes.Height) - } - var ( - tmTrustedVals *tmtypes.ValidatorSet - ok bool - ) - // Once we get TrustedHeight from client, we must query the validators from the counterparty chain - // If the LatestHeight == LastHeader.Height, then TrustedValidators are current validators - // If LatestHeight < LastHeader.Height, we can query the historical validator set from HistoricalInfo - if trustedHeight == counterparty.LastHeader.GetHeight() { - tmTrustedVals = counterparty.Vals - } else { - // NOTE: We need to get validators from counterparty at height: trustedHeight+1 - // since the last trusted validators for a header at height h - // is the NextValidators at h+1 committed to in header h by - // NextValidatorsHash - tmTrustedVals, ok = counterparty.GetValsAtHeight(int64(trustedHeight.RevisionHeight + 1)) - if !ok { - return nil, errorsmod.Wrapf(ibctmtypes.ErrInvalidHeaderHeight, "could not retrieve trusted validators at trustedHeight: %d", trustedHeight) - } - } - // inject trusted fields into last header - // for now assume revision number is 0 - header.TrustedHeight = trustedHeight - - trustedVals, err := tmTrustedVals.ToProto() - if err != nil { - return nil, err - } - header.TrustedValidators = trustedVals - - return header, nil -} - -// ExpireClient fast forwards the chain's block time by the provided amount of time which will -// expire any clients with a trusting period less than or equal to this amount of time. -func (chain *TestChain) ExpireClient(amount time.Duration) { - chain.Coordinator.IncrementTimeBy(amount) -} - -// CurrentTMClientHeader creates a TM header using the current header parameters -// on the chain. The trusted fields in the header are set to nil. -func (chain *TestChain) CurrentTMClientHeader() *ibctmtypes.Header { - return chain.CreateTMClientHeader(chain.ChainID, chain.CurrentHeader.Height, clienttypes.Height{}, chain.CurrentHeader.Time, chain.Vals, chain.NextVals, nil, chain.Signers) -} - -// CreateTMClientHeader creates a TM header to update the TM client. Args are passed in to allow -// caller flexibility to use params that differ from the chain. -func (chain *TestChain) CreateTMClientHeader(chainID string, blockHeight int64, trustedHeight clienttypes.Height, timestamp time.Time, tmValSet, nextVals, tmTrustedVals *tmtypes.ValidatorSet, signers map[string]tmtypes.PrivValidator) *ibctmtypes.Header { - var ( - valSet *tmproto.ValidatorSet - trustedVals *tmproto.ValidatorSet - ) - - if tmValSet == nil { - panic("tmValSet cannot be nil") - } - - vsetHash := tmValSet.Hash() - nextValHash := nextVals.Hash() - - tmHeader := tmtypes.Header{ - Version: tmprotoversion.Consensus{Block: tmversion.BlockProtocol, App: 2}, - ChainID: chainID, - Height: blockHeight, - Time: timestamp, - LastBlockID: MakeBlockID(make([]byte, tmhash.Size), 10000, make([]byte, tmhash.Size)), - LastCommitHash: chain.App.LastCommitID().Hash, - DataHash: tmhash.Sum([]byte("data_hash")), - ValidatorsHash: vsetHash, - NextValidatorsHash: nextValHash, - ConsensusHash: tmhash.Sum([]byte("consensus_hash")), - AppHash: chain.CurrentHeader.AppHash, - LastResultsHash: tmhash.Sum([]byte("last_results_hash")), - EvidenceHash: tmhash.Sum([]byte("evidence_hash")), - ProposerAddress: tmValSet.Proposer.Address, - } - - hhash := tmHeader.Hash() - blockID := MakeBlockID(hhash, 3, tmhash.Sum([]byte("part_set"))) - voteSet := tmtypes.NewVoteSet(chainID, blockHeight, 1, tmproto.PrecommitType, tmValSet) - - // MakeCommit expects a signer array in the same order as the validator array. - // Thus we iterate over the ordered validator set and construct a signer array - // from the signer map in the same order. - var signerArr []tmtypes.PrivValidator - - for _, v := range tmValSet.Validators { - if v == nil { - panic("validator in tmValSet cannot be nil") - } - signerArr = append(signerArr, signers[v.Address.String()]) - } - - commit, err := tmtypes.MakeCommit(blockID, blockHeight, 1, voteSet, signerArr, timestamp) - require.NoError(chain.T, err) - - signedHeader := &tmproto.SignedHeader{ - Header: tmHeader.ToProto(), - Commit: commit.ToProto(), - } - - if tmValSet != nil { - valSet, err = tmValSet.ToProto() - require.NoError(chain.T, err) - } - - if tmTrustedVals != nil { - trustedVals, err = tmTrustedVals.ToProto() - require.NoError(chain.T, err) - } - - // The trusted fields may be nil. They may be filled before relaying messages to a client. - // The relayer is responsible for querying client and injecting appropriate trusted fields. - return &ibctmtypes.Header{ - SignedHeader: signedHeader, - ValidatorSet: valSet, - TrustedHeight: trustedHeight, - TrustedValidators: trustedVals, - } -} - -// MakeBlockID copied unimported test functions from tmtypes to use them here -func MakeBlockID(hash []byte, partSetSize uint32, partSetHash []byte) tmtypes.BlockID { - return tmtypes.BlockID{ - Hash: hash, - PartSetHeader: tmtypes.PartSetHeader{ - Total: partSetSize, - Hash: partSetHash, - }, - } -} - -// CreateSortedSignerArray takes two PrivValidators, and the corresponding Validator structs -// (including voting power). It returns a signer array of PrivValidators that matches the -// sorting of ValidatorSet. -// The sorting is first by .VotingPower (descending), with secondary index of .Address (ascending). -func CreateSortedSignerArray(altPrivVal, suitePrivVal tmtypes.PrivValidator, - altVal, suiteVal *tmtypes.Validator, -) []tmtypes.PrivValidator { - switch { - case altVal.VotingPower > suiteVal.VotingPower: - return []tmtypes.PrivValidator{altPrivVal, suitePrivVal} - case altVal.VotingPower < suiteVal.VotingPower: - return []tmtypes.PrivValidator{suitePrivVal, altPrivVal} - default: - if bytes.Compare(altVal.Address, suiteVal.Address) == -1 { - return []tmtypes.PrivValidator{altPrivVal, suitePrivVal} - } - return []tmtypes.PrivValidator{suitePrivVal, altPrivVal} - } -} - -// CreatePortCapability binds and claims a capability for the given portID if it does not -// already exist. This function will fail testing on any resulting error. -// NOTE: only creation of a capability for a transfer or mock port is supported -// Other applications must bind to the port in InitGenesis or modify this code. -func (chain *TestChain) CreatePortCapability(scopedKeeper capabilitykeeper.ScopedKeeper, portID string) { - // check if the portId is already binded, if not bind it - _, ok := chain.App.GetScopedIBCKeeper().GetCapability(chain.GetContext(), host.PortPath(portID)) - if !ok { - // create capability using the IBC capability keeper - cap, err := chain.App.GetScopedIBCKeeper().NewCapability(chain.GetContext(), host.PortPath(portID)) - require.NoError(chain.T, err) - - // claim capability using the scopedKeeper - err = scopedKeeper.ClaimCapability(chain.GetContext(), cap, host.PortPath(portID)) - require.NoError(chain.T, err) - } - - chain.NextBlock() -} - -// GetPortCapability returns the port capability for the given portID. The capability must -// exist, otherwise testing will fail. -func (chain *TestChain) GetPortCapability(portID string) *capabilitytypes.Capability { - cap, ok := chain.App.GetScopedIBCKeeper().GetCapability(chain.GetContext(), host.PortPath(portID)) - require.True(chain.T, ok) - - return cap -} - -// CreateChannelCapability binds and claims a capability for the given portID and channelID -// if it does not already exist. This function will fail testing on any resulting error. The -// scoped keeper passed in will claim the new capability. -func (chain *TestChain) CreateChannelCapability(scopedKeeper capabilitykeeper.ScopedKeeper, portID, channelID string) { - capName := host.ChannelCapabilityPath(portID, channelID) - // check if the portId is already binded, if not bind it - _, ok := chain.App.GetScopedIBCKeeper().GetCapability(chain.GetContext(), capName) - if !ok { - cap, err := chain.App.GetScopedIBCKeeper().NewCapability(chain.GetContext(), capName) - require.NoError(chain.T, err) - err = scopedKeeper.ClaimCapability(chain.GetContext(), cap, capName) - require.NoError(chain.T, err) - } - - chain.NextBlock() -} - -// GetChannelCapability returns the channel capability for the given portID and channelID. -// The capability must exist, otherwise testing will fail. -func (chain *TestChain) GetChannelCapability(portID, channelID string) *capabilitytypes.Capability { - cap, ok := chain.App.GetScopedIBCKeeper().GetCapability(chain.GetContext(), host.ChannelCapabilityPath(portID, channelID)) - require.True(chain.T, ok) - - return cap -} - -// GetTimeoutHeight is a convenience function which returns a IBC packet timeout height -// to be used for testing. It returns the current IBC height + 100 blocks -func (chain *TestChain) GetTimeoutHeight() clienttypes.Height { - return clienttypes.NewHeight(clienttypes.ParseChainID(chain.ChainID), uint64(chain.GetContext().BlockHeight())+100) -} diff --git a/legacy_ibc_testing/testing/config.go b/legacy_ibc_testing/testing/config.go deleted file mode 100644 index dffe01053f..0000000000 --- a/legacy_ibc_testing/testing/config.go +++ /dev/null @@ -1,72 +0,0 @@ -package testing - -import ( - "time" - - connectiontypes "github.com/cosmos/ibc-go/v7/modules/core/03-connection/types" - channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" - "github.com/cosmos/ibc-go/v7/modules/core/exported" - ibctmtypes "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint" - "github.com/cosmos/ibc-go/v7/testing/mock" -) - -/* -TODO: Remove after upgrading to ibc-go v5 -legacy_ibc_testing is temporarily copied into the interchain-security repository for the purpose of testing only. -The integration test suites rely on modifications to ibc-go's test framework that cannot be back-ported to the canonical version that ics will rely on. -These files will be deprecated once ICS is able to upgrade to ibc-go v5. -*/ - -type ClientConfig interface { - GetClientType() string -} - -type TendermintConfig struct { - TrustLevel ibctmtypes.Fraction - TrustingPeriod time.Duration - UnbondingPeriod time.Duration - MaxClockDrift time.Duration - AllowUpdateAfterExpiry bool - AllowUpdateAfterMisbehaviour bool -} - -func NewTendermintConfig() *TendermintConfig { - return &TendermintConfig{ - TrustLevel: DefaultTrustLevel, - TrustingPeriod: TrustingPeriod, - UnbondingPeriod: UnbondingPeriod, - MaxClockDrift: MaxClockDrift, - AllowUpdateAfterExpiry: false, - AllowUpdateAfterMisbehaviour: false, - } -} - -func (tmcfg *TendermintConfig) GetClientType() string { - return exported.Tendermint -} - -type ConnectionConfig struct { - DelayPeriod uint64 - Version *connectiontypes.Version -} - -func NewConnectionConfig() *ConnectionConfig { - return &ConnectionConfig{ - DelayPeriod: DefaultDelayPeriod, - Version: ConnectionVersion, - } -} - -type ChannelConfig struct { - PortID string - Version string - Order channeltypes.Order -} - -func NewChannelConfig() *ChannelConfig { - return &ChannelConfig{ - PortID: mock.PortID, - Version: DefaultChannelVersion, - Order: channeltypes.UNORDERED, - } -} diff --git a/legacy_ibc_testing/testing/coordinator.go b/legacy_ibc_testing/testing/coordinator.go deleted file mode 100644 index 13f5ac1bec..0000000000 --- a/legacy_ibc_testing/testing/coordinator.go +++ /dev/null @@ -1,253 +0,0 @@ -package testing - -import ( - "fmt" - "strconv" - "testing" - "time" - - "github.com/stretchr/testify/require" - - abci "github.com/cometbft/cometbft/abci/types" -) - -/* -TODO: Remove after upgrading to ibc-go v5 -legacy_ibc_testing is temporarily copied into the interchain-security repository for the purpose of testing only. -The integration test suites rely on modifications to ibc-go's test framework that cannot be back-ported to the canonical version that ics will rely on. -These files will be deprecated once ICS is able to upgrade to ibc-go v5. -*/ - -var ( - ChainIDPrefix = "testchain" - GlobalStartTime = time.Date(2020, 1, 2, 0, 0, 0, 0, time.UTC) - TimeIncrement = time.Second * 5 -) - -// Coordinator is a testing struct which contains N TestChain's. It handles keeping all chains -// in sync with regards to time. -type Coordinator struct { - *testing.T - - CurrentTime time.Time - Chains map[string]*TestChain -} - -// NewCoordinator initializes Coordinator with N TestChain's -func NewCoordinator(t *testing.T, n int) *Coordinator { - t.Helper() - chains := make(map[string]*TestChain) - coord := &Coordinator{ - T: t, - CurrentTime: GlobalStartTime, - } - - for i := 1; i <= n; i++ { - chainID := GetChainID(i) - chains[chainID] = NewTestChain(t, coord, DefaultTestingAppInit, chainID) - } - coord.Chains = chains - - return coord -} - -// IncrementTime iterates through all the TestChain's and increments their current header time -// by 5 seconds. -// -// CONTRACT: this function must be called after every Commit on any TestChain. -func (coord *Coordinator) IncrementTime() { - coord.IncrementTimeBy(TimeIncrement) -} - -// IncrementTimeBy iterates through all the TestChain's and increments their current header time -// by specified time. -func (coord *Coordinator) IncrementTimeBy(increment time.Duration) { - coord.CurrentTime = coord.CurrentTime.Add(increment).UTC() - coord.UpdateTime() -} - -// UpdateTime updates all clocks for the TestChains to the current global time. -func (coord *Coordinator) UpdateTime() { - for _, chain := range coord.Chains { - coord.UpdateTimeForChain(chain) - } -} - -// UpdateTimeForChain updates the clock for a specific chain. -func (coord *Coordinator) UpdateTimeForChain(chain *TestChain) { - chain.CurrentHeader.Time = coord.CurrentTime.UTC() - chain.App.BeginBlock(abci.RequestBeginBlock{Header: chain.CurrentHeader}) -} - -// Setup constructs a TM client, connection, and channel on both chains provided. It will -// fail if any error occurs. The clientID's, TestConnections, and TestChannels are returned -// for both chains. The channels created are connected to the ibc-transfer application. -func (coord *Coordinator) Setup(path *Path) { - coord.SetupConnections(path) - - // channels can also be referenced through the returned connections - coord.CreateChannels(path) -} - -// SetupClients is a helper function to create clients on both chains. It assumes the -// caller does not anticipate any errors. -func (coord *Coordinator) SetupClients(path *Path) { - err := path.EndpointA.CreateClient() - require.NoError(coord.T, err) - - err = path.EndpointB.CreateClient() - require.NoError(coord.T, err) -} - -// SetupClientConnections is a helper function to create clients and the appropriate -// connections on both the source and counterparty chain. It assumes the caller does not -// anticipate any errors. -func (coord *Coordinator) SetupConnections(path *Path) { - coord.SetupClients(path) - - coord.CreateConnections(path) -} - -// CreateConnection constructs and executes connection handshake messages in order to create -// OPEN channels on chainA and chainB. The connection information of for chainA and chainB -// are returned within a TestConnection struct. The function expects the connections to be -// successfully opened otherwise testing will fail. -func (coord *Coordinator) CreateConnections(path *Path) { - err := path.EndpointA.ConnOpenInit() - require.NoError(coord.T, err) - - err = path.EndpointB.ConnOpenTry() - require.NoError(coord.T, err) - - err = path.EndpointA.ConnOpenAck() - require.NoError(coord.T, err) - - err = path.EndpointB.ConnOpenConfirm() - require.NoError(coord.T, err) - - // ensure counterparty is up to date - err = path.EndpointA.UpdateClient() - require.NoError(coord.T, err) -} - -// CreateMockChannels constructs and executes channel handshake messages to create OPEN -// channels that use a mock application module that returns nil on all callbacks. This -// function is expects the channels to be successfully opened otherwise testing will -// fail. -func (coord *Coordinator) CreateMockChannels(path *Path) { - path.EndpointA.ChannelConfig.PortID = MockPort - path.EndpointB.ChannelConfig.PortID = MockPort - - coord.CreateChannels(path) -} - -// CreateTransferChannels constructs and executes channel handshake messages to create OPEN -// ibc-transfer channels on chainA and chainB. The function expects the channels to be -// successfully opened otherwise testing will fail. -func (coord *Coordinator) CreateTransferChannels(path *Path) { - path.EndpointA.ChannelConfig.PortID = TransferPort - path.EndpointB.ChannelConfig.PortID = TransferPort - - coord.CreateChannels(path) -} - -// CreateChannel constructs and executes channel handshake messages in order to create -// OPEN channels on chainA and chainB. The function expects the channels to be successfully -// opened otherwise testing will fail. -func (coord *Coordinator) CreateChannels(path *Path) { - err := path.EndpointA.ChanOpenInit() - require.NoError(coord.T, err) - - err = path.EndpointB.ChanOpenTry() - require.NoError(coord.T, err) - - err = path.EndpointA.ChanOpenAck() - require.NoError(coord.T, err) - - err = path.EndpointB.ChanOpenConfirm() - require.NoError(coord.T, err) - - // ensure counterparty is up to date - err = path.EndpointA.UpdateClient() - require.NoError(coord.T, err) -} - -// GetChain returns the TestChain using the given chainID and returns an error if it does -// not exist. -func (coord *Coordinator) GetChain(chainID string) *TestChain { - chain, found := coord.Chains[chainID] - require.True(coord.T, found, fmt.Sprintf("%s chain does not exist", chainID)) - return chain -} - -// GetChainID returns the chainID used for the provided index. -func GetChainID(index int) string { - return ChainIDPrefix + strconv.Itoa(index) -} - -// CommitBlock commits a block on the provided indexes and then increments the global time. -// -// CONTRACT: the passed in list of indexes must not contain duplicates -func (coord *Coordinator) CommitBlock(chains ...*TestChain) { - for _, chain := range chains { - // NextBlock calls app.Commit() - chain.NextBlock() - } - coord.IncrementTime() -} - -// CommitNBlocks commits n blocks to state and updates the block height by 1 for each commit. -func (coord *Coordinator) CommitNBlocks(chain *TestChain, n uint64) { - for i := uint64(0); i < n; i++ { - chain.NextBlock() - coord.IncrementTime() - } -} - -// CommitBlockGetResponses commits a block and provides abci responses -func (coord *Coordinator) CommitBlockGetResponses(chain *TestChain) ( - abci.ResponseEndBlock, abci.ResponseCommit, abci.ResponseBeginBlock, -) { - ebRes, cRes, bbResp := chain.NextBlock() - coord.IncrementTime() - return ebRes, cRes, bbResp -} - -// ConnOpenInitOnBothChains initializes a connection on both endpoints with the state INIT -// using the OpenInit handshake call. -func (coord *Coordinator) ConnOpenInitOnBothChains(path *Path) error { - if err := path.EndpointA.ConnOpenInit(); err != nil { - return err - } - - if err := path.EndpointB.ConnOpenInit(); err != nil { - return err - } - - if err := path.EndpointA.UpdateClient(); err != nil { - return err - } - - return path.EndpointB.UpdateClient() -} - -// ChanOpenInitOnBothChains initializes a channel on the source chain and counterparty chain -// with the state INIT using the OpenInit handshake call. -func (coord *Coordinator) ChanOpenInitOnBothChains(path *Path) error { - // NOTE: only creation of a capability for a transfer or mock port is supported - // Other applications must bind to the port in InitGenesis or modify this code. - - if err := path.EndpointA.ChanOpenInit(); err != nil { - return err - } - - if err := path.EndpointB.ChanOpenInit(); err != nil { - return err - } - - if err := path.EndpointA.UpdateClient(); err != nil { - return err - } - - return path.EndpointB.UpdateClient() -} diff --git a/legacy_ibc_testing/testing/endpoint.go b/legacy_ibc_testing/testing/endpoint.go deleted file mode 100644 index ef9213d222..0000000000 --- a/legacy_ibc_testing/testing/endpoint.go +++ /dev/null @@ -1,581 +0,0 @@ -package testing - -import ( - "fmt" - - clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types" - connectiontypes "github.com/cosmos/ibc-go/v7/modules/core/03-connection/types" - channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" - commitmenttypes "github.com/cosmos/ibc-go/v7/modules/core/23-commitment/types" - host "github.com/cosmos/ibc-go/v7/modules/core/24-host" - "github.com/cosmos/ibc-go/v7/modules/core/exported" - ibctmtypes "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint" - "github.com/stretchr/testify/require" - - sdk "github.com/cosmos/cosmos-sdk/types" -) - -/* -TODO: Remove after upgrading to ibc-go v5 -legacy_ibc_testing is temporarily copied into the interchain-security repository for the purpose of testing only. -The integration test suites rely on modifications to ibc-go's test framework that cannot be back-ported to the canonical version that ics will rely on. -These files will be deprecated once ICS is able to upgrade to ibc-go v5. -*/ - -// Endpoint is a which represents a channel endpoint and its associated -// client and connections. It contains client, connection, and channel -// configuration parameters. Endpoint functions will utilize the parameters -// set in the configuration structs when executing IBC messages. -type Endpoint struct { - Chain *TestChain - Counterparty *Endpoint - ClientID string - ConnectionID string - ChannelID string - - ClientConfig ClientConfig - ConnectionConfig *ConnectionConfig - ChannelConfig *ChannelConfig -} - -// NewDefaultEndpoint constructs a new endpoint using default values. -// CONTRACT: the counterparty endpoitn must be set by the caller. -func NewDefaultEndpoint(chain *TestChain) *Endpoint { - return &Endpoint{ - Chain: chain, - ClientConfig: NewTendermintConfig(), - ConnectionConfig: NewConnectionConfig(), - ChannelConfig: NewChannelConfig(), - } -} - -// QueryProof queries proof associated with this endpoint using the lastest client state -// height on the counterparty chain. -func (endpoint *Endpoint) QueryProof(key []byte) ([]byte, clienttypes.Height) { - // obtain the counterparty client representing the chain associated with the endpoint - clientState := endpoint.Counterparty.Chain.GetClientState(endpoint.Counterparty.ClientID) - - // query proof on the counterparty using the latest height of the IBC client - return endpoint.QueryProofAtHeight(key, clientState.GetLatestHeight().GetRevisionHeight()) -} - -// QueryProofAtHeight queries proof associated with this endpoint using the proof height -// provided -func (endpoint *Endpoint) QueryProofAtHeight(key []byte, height uint64) ([]byte, clienttypes.Height) { - // query proof on the counterparty using the latest height of the IBC client - return endpoint.Chain.QueryProofAtHeight(key, int64(height)) -} - -// CreateClient creates an IBC client on the endpoint. It will update the -// clientID for the endpoint if the message is successfully executed. -// NOTE: a solo machine client will be created with an empty diversifier. -func (endpoint *Endpoint) CreateClient() (err error) { - // ensure counterparty has committed state - endpoint.Chain.Coordinator.CommitBlock(endpoint.Counterparty.Chain) - - var ( - clientState exported.ClientState - consensusState exported.ConsensusState - ) - - switch endpoint.ClientConfig.GetClientType() { - case exported.Tendermint: - tmConfig, ok := endpoint.ClientConfig.(*TendermintConfig) - require.True(endpoint.Chain.T, ok) - - height := endpoint.Counterparty.Chain.LastHeader.GetHeight().(clienttypes.Height) - clientState = ibctmtypes.NewClientState( - endpoint.Counterparty.Chain.ChainID, tmConfig.TrustLevel, tmConfig.TrustingPeriod, tmConfig.UnbondingPeriod, tmConfig.MaxClockDrift, - height, commitmenttypes.GetSDKSpecs(), UpgradePath, - ) - consensusState = endpoint.Counterparty.Chain.LastHeader.ConsensusState() - case exported.Solomachine: - // TODO - // solo := NewSolomachine(endpoint.Chain.T, endpoint.Chain.Codec, clientID, "", 1) - // clientState = solo.ClientState() - // consensusState = solo.ConsensusState() - - default: - err = fmt.Errorf("client type %s is not supported", endpoint.ClientConfig.GetClientType()) - } - - if err != nil { - return err - } - - msg, err := clienttypes.NewMsgCreateClient( - clientState, consensusState, endpoint.Chain.SenderAccount.GetAddress().String(), - ) - require.NoError(endpoint.Chain.T, err) - - res, err := endpoint.Chain.SendMsgs(msg) - if err != nil { - return err - } - - endpoint.ClientID, err = ParseClientIDFromEvents(res.GetEvents()) - require.NoError(endpoint.Chain.T, err) - - return nil -} - -// UpdateClient updates the IBC client associated with the endpoint. -func (endpoint *Endpoint) UpdateClient() (err error) { - // ensure counterparty has committed state - endpoint.Chain.Coordinator.CommitBlock(endpoint.Counterparty.Chain) - - var header *ibctmtypes.Header - - switch endpoint.ClientConfig.GetClientType() { - case exported.Tendermint: - header, err = endpoint.Chain.ConstructUpdateTMClientHeader(endpoint.Counterparty.Chain, endpoint.ClientID) - - default: - err = fmt.Errorf("client type %s is not supported", endpoint.ClientConfig.GetClientType()) - } - - if err != nil { - return err - } - - msg, err := clienttypes.NewMsgUpdateClient( - endpoint.ClientID, header, - endpoint.Chain.SenderAccount.GetAddress().String(), - ) - require.NoError(endpoint.Chain.T, err) - - return endpoint.Chain.sendMsgs(msg) -} - -// ConnOpenInit will construct and execute a MsgConnectionOpenInit on the associated endpoint. -func (endpoint *Endpoint) ConnOpenInit() error { - msg := connectiontypes.NewMsgConnectionOpenInit( - endpoint.ClientID, - endpoint.Counterparty.ClientID, - endpoint.Counterparty.Chain.GetPrefix(), DefaultOpenInitVersion, endpoint.ConnectionConfig.DelayPeriod, - endpoint.Chain.SenderAccount.GetAddress().String(), - ) - res, err := endpoint.Chain.SendMsgs(msg) - if err != nil { - return err - } - - endpoint.ConnectionID, err = ParseConnectionIDFromEvents(res.GetEvents()) - require.NoError(endpoint.Chain.T, err) - - return nil -} - -// ConnOpenTry will construct and execute a MsgConnectionOpenTry on the associated endpoint. -func (endpoint *Endpoint) ConnOpenTry() error { - err := endpoint.UpdateClient() - require.NoError(endpoint.Chain.T, err) - - counterpartyClient, proofClient, proofConsensus, consensusHeight, proofInit, proofHeight := endpoint.QueryConnectionHandshakeProof() - - msg := connectiontypes.NewMsgConnectionOpenTry( - endpoint.ClientID, // does not support handshake continuation - endpoint.Counterparty.ConnectionID, - endpoint.Counterparty.ClientID, - counterpartyClient, - endpoint.Counterparty.Chain.GetPrefix(), - []*connectiontypes.Version{ConnectionVersion}, - endpoint.ConnectionConfig.DelayPeriod, - proofInit, proofClient, proofConsensus, - proofHeight, consensusHeight, - endpoint.Chain.SenderAccount.GetAddress().String(), - ) - res, err := endpoint.Chain.SendMsgs(msg) - if err != nil { - return err - } - - if endpoint.ConnectionID == "" { - endpoint.ConnectionID, err = ParseConnectionIDFromEvents(res.GetEvents()) - require.NoError(endpoint.Chain.T, err) - } - - return nil -} - -// ConnOpenAck will construct and execute a MsgConnectionOpenAck on the associated endpoint. -func (endpoint *Endpoint) ConnOpenAck() error { - err := endpoint.UpdateClient() - require.NoError(endpoint.Chain.T, err) - - counterpartyClient, proofClient, proofConsensus, consensusHeight, proofTry, proofHeight := endpoint.QueryConnectionHandshakeProof() - - msg := connectiontypes.NewMsgConnectionOpenAck( - endpoint.ConnectionID, endpoint.Counterparty.ConnectionID, counterpartyClient, // testing doesn't use flexible selection - proofTry, proofClient, proofConsensus, - proofHeight, consensusHeight, - ConnectionVersion, - endpoint.Chain.SenderAccount.GetAddress().String(), - ) - return endpoint.Chain.sendMsgs(msg) -} - -// ConnOpenConfirm will construct and execute a MsgConnectionOpenConfirm on the associated endpoint. -func (endpoint *Endpoint) ConnOpenConfirm() error { - err := endpoint.UpdateClient() - require.NoError(endpoint.Chain.T, err) - - connectionKey := host.ConnectionKey(endpoint.Counterparty.ConnectionID) - proof, height := endpoint.Counterparty.Chain.QueryProof(connectionKey) - - msg := connectiontypes.NewMsgConnectionOpenConfirm( - endpoint.ConnectionID, - proof, height, - endpoint.Chain.SenderAccount.GetAddress().String(), - ) - return endpoint.Chain.sendMsgs(msg) -} - -// QueryConnectionHandshakeProof returns all the proofs necessary to execute OpenTry or Open Ack of -// the connection handshakes. It returns the counterparty client state, proof of the counterparty -// client state, proof of the counterparty consensus state, the consensus state height, proof of -// the counterparty connection, and the proof height for all the proofs returned. -func (endpoint *Endpoint) QueryConnectionHandshakeProof() ( - clientState exported.ClientState, proofClient, - proofConsensus []byte, consensusHeight clienttypes.Height, - proofConnection []byte, proofHeight clienttypes.Height, -) { - // obtain the client state on the counterparty chain - clientState = endpoint.Counterparty.Chain.GetClientState(endpoint.Counterparty.ClientID) - - // query proof for the client state on the counterparty - clientKey := host.FullClientStateKey(endpoint.Counterparty.ClientID) - proofClient, proofHeight = endpoint.Counterparty.QueryProof(clientKey) - - consensusHeight = clientState.GetLatestHeight().(clienttypes.Height) - - // query proof for the consensus state on the counterparty - consensusKey := host.FullConsensusStateKey(endpoint.Counterparty.ClientID, consensusHeight) - proofConsensus, _ = endpoint.Counterparty.QueryProofAtHeight(consensusKey, proofHeight.GetRevisionHeight()) - - // query proof for the connection on the counterparty - connectionKey := host.ConnectionKey(endpoint.Counterparty.ConnectionID) - proofConnection, _ = endpoint.Counterparty.QueryProofAtHeight(connectionKey, proofHeight.GetRevisionHeight()) - - return -} - -// ChanOpenInit will construct and execute a MsgChannelOpenInit on the associated endpoint. -func (endpoint *Endpoint) ChanOpenInit() error { - msg := channeltypes.NewMsgChannelOpenInit( - endpoint.ChannelConfig.PortID, - endpoint.ChannelConfig.Version, endpoint.ChannelConfig.Order, []string{endpoint.ConnectionID}, - endpoint.Counterparty.ChannelConfig.PortID, - endpoint.Chain.SenderAccount.GetAddress().String(), - ) - res, err := endpoint.Chain.SendMsgs(msg) - if err != nil { - return err - } - - endpoint.ChannelID, err = ParseChannelIDFromEvents(res.GetEvents()) - require.NoError(endpoint.Chain.T, err) - - return nil -} - -// ChanOpenTry will construct and execute a MsgChannelOpenTry on the associated endpoint. -func (endpoint *Endpoint) ChanOpenTry() error { - err := endpoint.UpdateClient() - require.NoError(endpoint.Chain.T, err) - - channelKey := host.ChannelKey(endpoint.Counterparty.ChannelConfig.PortID, endpoint.Counterparty.ChannelID) - proof, height := endpoint.Counterparty.Chain.QueryProof(channelKey) - - msg := channeltypes.NewMsgChannelOpenTry( - endpoint.ChannelConfig.PortID, // does not support handshake continuation - endpoint.ChannelConfig.Version, - endpoint.ChannelConfig.Order, - []string{endpoint.ConnectionID}, - endpoint.Counterparty.ChannelConfig.PortID, - endpoint.Counterparty.ChannelID, - endpoint.Counterparty.ChannelConfig.Version, - proof, height, - endpoint.Chain.SenderAccount.GetAddress().String(), - ) - res, err := endpoint.Chain.SendMsgs(msg) - if err != nil { - return err - } - - if endpoint.ChannelID == "" { - endpoint.ChannelID, err = ParseChannelIDFromEvents(res.GetEvents()) - require.NoError(endpoint.Chain.T, err) - } - - // update version to selected app version - // NOTE: this update must be performed after the endpoint channelID is set - endpoint.ChannelConfig.Version = endpoint.GetChannel().Version - - return nil -} - -// ChanOpenAck will construct and execute a MsgChannelOpenAck on the associated endpoint. -func (endpoint *Endpoint) ChanOpenAck() error { - err := endpoint.UpdateClient() - require.NoError(endpoint.Chain.T, err) - - channelKey := host.ChannelKey(endpoint.Counterparty.ChannelConfig.PortID, endpoint.Counterparty.ChannelID) - proof, height := endpoint.Counterparty.Chain.QueryProof(channelKey) - - msg := channeltypes.NewMsgChannelOpenAck( - endpoint.ChannelConfig.PortID, endpoint.ChannelID, - endpoint.Counterparty.ChannelID, endpoint.Counterparty.ChannelConfig.Version, // testing doesn't use flexible selection - proof, height, - endpoint.Chain.SenderAccount.GetAddress().String(), - ) - return endpoint.Chain.sendMsgs(msg) -} - -// ChanOpenConfirm will construct and execute a MsgChannelOpenConfirm on the associated endpoint. -func (endpoint *Endpoint) ChanOpenConfirm() error { - err := endpoint.UpdateClient() - require.NoError(endpoint.Chain.T, err) - - channelKey := host.ChannelKey(endpoint.Counterparty.ChannelConfig.PortID, endpoint.Counterparty.ChannelID) - proof, height := endpoint.Counterparty.Chain.QueryProof(channelKey) - - msg := channeltypes.NewMsgChannelOpenConfirm( - endpoint.ChannelConfig.PortID, endpoint.ChannelID, - proof, height, - endpoint.Chain.SenderAccount.GetAddress().String(), - ) - return endpoint.Chain.sendMsgs(msg) -} - -// ChanCloseInit will construct and execute a MsgChannelCloseInit on the associated endpoint. -// -// NOTE: does not work with ibc-transfer module -func (endpoint *Endpoint) ChanCloseInit() error { - msg := channeltypes.NewMsgChannelCloseInit( - endpoint.ChannelConfig.PortID, endpoint.ChannelID, - endpoint.Chain.SenderAccount.GetAddress().String(), - ) - return endpoint.Chain.sendMsgs(msg) -} - -// SendPacket sends a packet through the channel keeper using the associated endpoint -// The counterparty client is updated so proofs can be sent to the counterparty chain. -func (endpoint *Endpoint) SendPacket(packet exported.PacketI) error { - channelCap := endpoint.Chain.GetChannelCapability(packet.GetSourcePort(), packet.GetSourceChannel()) - - timeoutHeight := clienttypes.Height{ - RevisionNumber: packet.GetTimeoutHeight().GetRevisionNumber(), - RevisionHeight: packet.GetTimeoutHeight().GetRevisionHeight(), - } - - // no need to send message, acting as a module - _, err := endpoint.Chain.App.GetIBCKeeper().ChannelKeeper.SendPacket(endpoint.Chain.GetContext(), - channelCap, packet.GetSourcePort(), - packet.GetSourceChannel(), - timeoutHeight, - packet.GetTimeoutTimestamp(), - packet.GetData()) - if err != nil { - return err - } - - // commit changes since no message was sent - endpoint.Chain.Coordinator.CommitBlock(endpoint.Chain) - - return endpoint.Counterparty.UpdateClient() -} - -// RecvPacket receives a packet on the associated endpoint. -// The counterparty client is updated. -func (endpoint *Endpoint) RecvPacket(packet channeltypes.Packet) error { - _, err := endpoint.RecvPacketWithResult(packet) - if err != nil { - return err - } - - return nil -} - -// RecvPacketWithResult receives a packet on the associated endpoint and the result -// of the transaction is returned. The counterparty client is updated. -func (endpoint *Endpoint) RecvPacketWithResult(packet channeltypes.Packet) (*sdk.Result, error) { - // get proof of packet commitment on source - packetKey := host.PacketCommitmentKey(packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) - proof, proofHeight := endpoint.Counterparty.Chain.QueryProof(packetKey) - - recvMsg := channeltypes.NewMsgRecvPacket(packet, proof, proofHeight, endpoint.Chain.SenderAccount.GetAddress().String()) - - // receive on counterparty and update source client - res, err := endpoint.Chain.SendMsgs(recvMsg) - if err != nil { - return nil, err - } - - if err := endpoint.Counterparty.UpdateClient(); err != nil { - return nil, err - } - - return res, nil -} - -// WriteAcknowledgement writes an acknowledgement on the channel associated with the endpoint. -// The counterparty client is updated. -func (endpoint *Endpoint) WriteAcknowledgement(ack exported.Acknowledgement, packet exported.PacketI) error { - channelCap := endpoint.Chain.GetChannelCapability(packet.GetDestPort(), packet.GetDestChannel()) - - // no need to send message, acting as a handler - err := endpoint.Chain.App.GetIBCKeeper().ChannelKeeper.WriteAcknowledgement(endpoint.Chain.GetContext(), channelCap, packet, ack) - if err != nil { - return err - } - - // commit changes since no message was sent - endpoint.Chain.Coordinator.CommitBlock(endpoint.Chain) - - return endpoint.Counterparty.UpdateClient() -} - -// AcknowledgePacket sends a MsgAcknowledgement to the channel associated with the endpoint. -func (endpoint *Endpoint) AcknowledgePacket(packet channeltypes.Packet, ack []byte) error { - // get proof of acknowledgement on counterparty - packetKey := host.PacketAcknowledgementKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence()) - proof, proofHeight := endpoint.Counterparty.QueryProof(packetKey) - - ackMsg := channeltypes.NewMsgAcknowledgement(packet, ack, proof, proofHeight, endpoint.Chain.SenderAccount.GetAddress().String()) - - return endpoint.Chain.sendMsgs(ackMsg) -} - -// TimeoutPacket sends a MsgTimeout to the channel associated with the endpoint. -func (endpoint *Endpoint) TimeoutPacket(packet channeltypes.Packet) error { - // get proof for timeout based on channel order - var packetKey []byte - - switch endpoint.ChannelConfig.Order { - case channeltypes.ORDERED: - packetKey = host.NextSequenceRecvKey(packet.GetDestPort(), packet.GetDestChannel()) - case channeltypes.UNORDERED: - packetKey = host.PacketReceiptKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence()) - default: - return fmt.Errorf("unsupported order type %s", endpoint.ChannelConfig.Order) - } - - proof, proofHeight := endpoint.Counterparty.QueryProof(packetKey) - nextSeqRecv, found := endpoint.Counterparty.Chain.App.GetIBCKeeper().ChannelKeeper.GetNextSequenceRecv(endpoint.Counterparty.Chain.GetContext(), endpoint.ChannelConfig.PortID, endpoint.ChannelID) - require.True(endpoint.Chain.T, found) - - timeoutMsg := channeltypes.NewMsgTimeout( - packet, nextSeqRecv, - proof, proofHeight, endpoint.Chain.SenderAccount.GetAddress().String(), - ) - - return endpoint.Chain.sendMsgs(timeoutMsg) -} - -// TimeoutOnClose sends a MsgTimeoutOnClose to the channel associated with the endpoint. -func (endpoint *Endpoint) TimeoutOnClose(packet channeltypes.Packet) error { - // get proof for timeout based on channel order - var packetKey []byte - - switch endpoint.ChannelConfig.Order { - case channeltypes.ORDERED: - packetKey = host.NextSequenceRecvKey(packet.GetDestPort(), packet.GetDestChannel()) - case channeltypes.UNORDERED: - packetKey = host.PacketReceiptKey(packet.GetDestPort(), packet.GetDestChannel(), packet.GetSequence()) - default: - return fmt.Errorf("unsupported order type %s", endpoint.ChannelConfig.Order) - } - - proof, proofHeight := endpoint.Counterparty.QueryProof(packetKey) - - channelKey := host.ChannelKey(packet.GetDestPort(), packet.GetDestChannel()) - proofClosed, _ := endpoint.Counterparty.QueryProof(channelKey) - - nextSeqRecv, found := endpoint.Counterparty.Chain.App.GetIBCKeeper().ChannelKeeper.GetNextSequenceRecv(endpoint.Counterparty.Chain.GetContext(), endpoint.ChannelConfig.PortID, endpoint.ChannelID) - require.True(endpoint.Chain.T, found) - - timeoutOnCloseMsg := channeltypes.NewMsgTimeoutOnClose( - packet, nextSeqRecv, - proof, proofClosed, proofHeight, endpoint.Chain.SenderAccount.GetAddress().String(), - ) - - return endpoint.Chain.sendMsgs(timeoutOnCloseMsg) -} - -// SetChannelClosed sets a channel state to CLOSED. -func (endpoint *Endpoint) SetChannelClosed() error { - channel := endpoint.GetChannel() - - channel.State = channeltypes.CLOSED - endpoint.Chain.App.GetIBCKeeper().ChannelKeeper.SetChannel(endpoint.Chain.GetContext(), endpoint.ChannelConfig.PortID, endpoint.ChannelID, channel) - - endpoint.Chain.Coordinator.CommitBlock(endpoint.Chain) - - return endpoint.Counterparty.UpdateClient() -} - -// GetClientState retrieves the Client State for this endpoint. The -// client state is expected to exist otherwise testing will fail. -func (endpoint *Endpoint) GetClientState() exported.ClientState { - return endpoint.Chain.GetClientState(endpoint.ClientID) -} - -// SetClientState sets the client state for this endpoint. -func (endpoint *Endpoint) SetClientState(clientState exported.ClientState) { - endpoint.Chain.App.GetIBCKeeper().ClientKeeper.SetClientState(endpoint.Chain.GetContext(), endpoint.ClientID, clientState) -} - -// GetConsensusState retrieves the Consensus State for this endpoint at the provided height. -// The consensus state is expected to exist otherwise testing will fail. -func (endpoint *Endpoint) GetConsensusState(height exported.Height) exported.ConsensusState { - consensusState, found := endpoint.Chain.GetConsensusState(endpoint.ClientID, height) - require.True(endpoint.Chain.T, found) - - return consensusState -} - -// SetConsensusState sets the consensus state for this endpoint. -func (endpoint *Endpoint) SetConsensusState(consensusState exported.ConsensusState, height exported.Height) { - endpoint.Chain.App.GetIBCKeeper().ClientKeeper.SetClientConsensusState(endpoint.Chain.GetContext(), endpoint.ClientID, height, consensusState) -} - -// GetConnection retrieves an IBC Connection for the endpoint. The -// connection is expected to exist otherwise testing will fail. -func (endpoint *Endpoint) GetConnection() connectiontypes.ConnectionEnd { - connection, found := endpoint.Chain.App.GetIBCKeeper().ConnectionKeeper.GetConnection(endpoint.Chain.GetContext(), endpoint.ConnectionID) - require.True(endpoint.Chain.T, found) - - return connection -} - -// SetConnection sets the connection for this endpoint. -func (endpoint *Endpoint) SetConnection(connection connectiontypes.ConnectionEnd) { - endpoint.Chain.App.GetIBCKeeper().ConnectionKeeper.SetConnection(endpoint.Chain.GetContext(), endpoint.ConnectionID, connection) -} - -// GetChannel retrieves an IBC Channel for the endpoint. The channel -// is expected to exist otherwise testing will fail. -func (endpoint *Endpoint) GetChannel() channeltypes.Channel { - channel, found := endpoint.Chain.App.GetIBCKeeper().ChannelKeeper.GetChannel(endpoint.Chain.GetContext(), endpoint.ChannelConfig.PortID, endpoint.ChannelID) - require.True(endpoint.Chain.T, found) - - return channel -} - -// SetChannel sets the channel for this endpoint. -func (endpoint *Endpoint) SetChannel(channel channeltypes.Channel) { - endpoint.Chain.App.GetIBCKeeper().ChannelKeeper.SetChannel(endpoint.Chain.GetContext(), endpoint.ChannelConfig.PortID, endpoint.ChannelID, channel) -} - -// QueryClientStateProof performs and abci query for a client stat associated -// with this endpoint and returns the ClientState along with the proof. -func (endpoint *Endpoint) QueryClientStateProof() (exported.ClientState, []byte) { - // retrieve client state to provide proof for - clientState := endpoint.GetClientState() - - clientKey := host.FullClientStateKey(endpoint.ClientID) - proofClient, _ := endpoint.QueryProof(clientKey) - - return clientState, proofClient -} diff --git a/legacy_ibc_testing/testing/events.go b/legacy_ibc_testing/testing/events.go deleted file mode 100644 index 4bab444cbb..0000000000 --- a/legacy_ibc_testing/testing/events.go +++ /dev/null @@ -1,79 +0,0 @@ -package testing - -import ( - "fmt" - - clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types" - connectiontypes "github.com/cosmos/ibc-go/v7/modules/core/03-connection/types" - channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" - - sdk "github.com/cosmos/cosmos-sdk/types" -) - -/* -TODO: Remove after upgrading to ibc-go v5 -legacy_ibc_testing is temporarily copied into the interchain-security repository for the purpose of testing only. -The integration test suites rely on modifications to ibc-go's test framework that cannot be back-ported to the canonical version that ics will rely on. -These files will be deprecated once ICS is able to upgrade to ibc-go v5. -*/ - -// ParseClientIDFromEvents parses events emitted from a MsgCreateClient and returns the -// client identifier. -func ParseClientIDFromEvents(events sdk.Events) (string, error) { - for _, ev := range events { - if ev.Type == clienttypes.EventTypeCreateClient { - for _, attr := range ev.Attributes { - if attr.Key == clienttypes.AttributeKeyClientID { - return attr.Value, nil - } - } - } - } - return "", fmt.Errorf("client identifier event attribute not found") -} - -// ParseConnectionIDFromEvents parses events emitted from a MsgConnectionOpenInit or -// MsgConnectionOpenTry and returns the connection identifier. -func ParseConnectionIDFromEvents(events sdk.Events) (string, error) { - for _, ev := range events { - if ev.Type == connectiontypes.EventTypeConnectionOpenInit || - ev.Type == connectiontypes.EventTypeConnectionOpenTry { - for _, attr := range ev.Attributes { - if attr.Key == connectiontypes.AttributeKeyConnectionID { - return attr.Value, nil - } - } - } - } - return "", fmt.Errorf("connection identifier event attribute not found") -} - -// ParseChannelIDFromEvents parses events emitted from a MsgChannelOpenInit or -// MsgChannelOpenTry and returns the channel identifier. -func ParseChannelIDFromEvents(events sdk.Events) (string, error) { - for _, ev := range events { - if ev.Type == channeltypes.EventTypeChannelOpenInit || ev.Type == channeltypes.EventTypeChannelOpenTry { - for _, attr := range ev.Attributes { - if attr.Key == channeltypes.AttributeKeyChannelID { - return attr.Value, nil - } - } - } - } - return "", fmt.Errorf("channel identifier event attribute not found") -} - -// ParseAckFromEvents parses events emitted from a MsgRecvPacket and returns the -// acknowledgement. -func ParseAckFromEvents(events sdk.Events) ([]byte, error) { - for _, ev := range events { - if ev.Type == channeltypes.EventTypeWriteAck { - for _, attr := range ev.Attributes { - if attr.Key == channeltypes.AttributeKeyAck { //nolint:staticcheck // data - return []byte(attr.Value), nil - } - } - } - } - return nil, fmt.Errorf("acknowledgement event attribute not found") -} diff --git a/legacy_ibc_testing/testing/path.go b/legacy_ibc_testing/testing/path.go deleted file mode 100644 index 31efe8215d..0000000000 --- a/legacy_ibc_testing/testing/path.go +++ /dev/null @@ -1,99 +0,0 @@ -package testing - -import ( - "bytes" - "fmt" - - channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" -) - -/* -TODO: Remove after upgrading to ibc-go v5 -legacy_ibc_testing is temporarily copied into the interchain-security repository for the purpose of testing only. -The integration test suites rely on modifications to ibc-go's test framework that cannot be back-ported to the canonical version that ics will rely on. -These files will be deprecated once ICS is able to upgrade to ibc-go v5. -*/ - -// Path contains two endpoints representing two chains connected over IBC -type Path struct { - EndpointA *Endpoint - EndpointB *Endpoint -} - -// NewPath constructs an endpoint for each chain using the default values -// for the endpoints. Each endpoint is updated to have a pointer to the -// counterparty endpoint. -func NewPath(chainA, chainB *TestChain) *Path { - endpointA := NewDefaultEndpoint(chainA) - endpointB := NewDefaultEndpoint(chainB) - - endpointA.Counterparty = endpointB - endpointB.Counterparty = endpointA - - return &Path{ - EndpointA: endpointA, - EndpointB: endpointB, - } -} - -// SetChannelOrdered sets the channel order for both endpoints to ORDERED. -func (path *Path) SetChannelOrdered() { - path.EndpointA.ChannelConfig.Order = channeltypes.ORDERED - path.EndpointB.ChannelConfig.Order = channeltypes.ORDERED -} - -// RelayPacket attempts to relay the packet first on EndpointA and then on EndpointB -// if EndpointA does not contain a packet commitment for that packet. An error is returned -// if a relay step fails or the packet commitment does not exist on either endpoint. -func (path *Path) RelayPacket(packet channeltypes.Packet) error { - pc := path.EndpointA.Chain.App.GetIBCKeeper().ChannelKeeper.GetPacketCommitment(path.EndpointA.Chain.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) - if bytes.Equal(pc, channeltypes.CommitPacket(path.EndpointA.Chain.App.AppCodec(), packet)) { - - // packet found, relay from A to B - if err := path.EndpointB.UpdateClient(); err != nil { - return err - } - - res, err := path.EndpointB.RecvPacketWithResult(packet) - if err != nil { - return err - } - - ack, err := ParseAckFromEvents(res.GetEvents()) - if err != nil { - return err - } - - if err := path.EndpointA.AcknowledgePacket(packet, ack); err != nil { - return err - } - - return nil - } - - pc = path.EndpointB.Chain.App.GetIBCKeeper().ChannelKeeper.GetPacketCommitment(path.EndpointB.Chain.GetContext(), packet.GetSourcePort(), packet.GetSourceChannel(), packet.GetSequence()) - if bytes.Equal(pc, channeltypes.CommitPacket(path.EndpointB.Chain.App.AppCodec(), packet)) { - - // packet found, relay B to A - if err := path.EndpointA.UpdateClient(); err != nil { - return err - } - - res, err := path.EndpointA.RecvPacketWithResult(packet) - if err != nil { - return err - } - - ack, err := ParseAckFromEvents(res.GetEvents()) - if err != nil { - return err - } - - if err := path.EndpointB.AcknowledgePacket(packet, ack); err != nil { - return err - } - return nil - } - - return fmt.Errorf("packet commitment does not exist on either endpoint for provided packet") -} diff --git a/legacy_ibc_testing/testing/utils.go b/legacy_ibc_testing/testing/utils.go deleted file mode 100644 index 047793f455..0000000000 --- a/legacy_ibc_testing/testing/utils.go +++ /dev/null @@ -1,32 +0,0 @@ -package testing - -import ( - "testing" - - "github.com/stretchr/testify/require" - - abci "github.com/cometbft/cometbft/abci/types" - tmtypes "github.com/cometbft/cometbft/types" -) - -/* -TODO: Remove after upgrading to ibc-go v5 -legacy_ibc_testing is temporarily copied into the interchain-security repository for the purpose of testing only. -The integration test suites rely on modifications to ibc-go's test framework that cannot be back-ported to the canonical version that ics will rely on. -These files will be deprecated once ICS is able to upgrade to ibc-go v5. -*/ - -// ApplyValSetChanges takes in tmtypes.ValidatorSet and []abci.ValidatorUpdate and will return a new tmtypes.ValidatorSet which has the -// provided validator updates applied to the provided validator set. -func ApplyValSetChanges(t *testing.T, valSet *tmtypes.ValidatorSet, valUpdates []abci.ValidatorUpdate) *tmtypes.ValidatorSet { - t.Helper() - updates, err := tmtypes.PB2TM.ValidatorUpdates(valUpdates) - require.NoError(t, err) - - // must copy since validator set will mutate with UpdateWithChangeSet - newVals := valSet.Copy() - err = newVals.UpdateWithChangeSet(updates) - require.NoError(t, err) - - return newVals -} diff --git a/legacy_ibc_testing/testing/values.go b/legacy_ibc_testing/testing/values.go deleted file mode 100644 index 579d00cea4..0000000000 --- a/legacy_ibc_testing/testing/values.go +++ /dev/null @@ -1,53 +0,0 @@ -package testing - -import ( - "time" - - ibctransfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types" - connectiontypes "github.com/cosmos/ibc-go/v7/modules/core/03-connection/types" - ibctmtypes "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint" - "github.com/cosmos/ibc-go/v7/testing/mock" -) - -/* -TODO: Remove after upgrading to ibc-go v5 -legacy_ibc_testing is temporarily copied into the interchain-security repository for the purpose of testing only. -The integration test suites rely on modifications to ibc-go's test framework that cannot be back-ported to the canonical version that ics will rely on. -These files will be deprecated once ICS is able to upgrade to ibc-go v5. -*/ - -const ( - FirstChannelID = "channel-0" - FirstConnectionID = "connection-0" - - // Default params constants used to create a TM client - TrustingPeriod time.Duration = time.Hour * 24 * 7 * 2 - UnbondingPeriod time.Duration = time.Hour * 24 * 7 * 3 - MaxClockDrift time.Duration = time.Second * 10 - DefaultDelayPeriod uint64 = 0 - - DefaultChannelVersion = mock.Version - InvalidID = "IDisInvalid" - - // Application Ports - TransferPort = ibctransfertypes.ModuleName - MockPort = mock.ModuleName - - // used for testing proposals - Title = "title" - Description = "description" -) - -var ( - DefaultOpenInitVersion *connectiontypes.Version - - // Default params variables used to create a TM client - DefaultTrustLevel ibctmtypes.Fraction = ibctmtypes.DefaultTrustLevel - - UpgradePath = []string{"upgrade", "upgradedIBCState"} - - ConnectionVersion = connectiontypes.ExportedVersionsToProto(connectiontypes.GetCompatibleVersions())[0] - - MockAcknowledgement = mock.MockAcknowledgement.Acknowledgement() - MockPacketData = mock.MockPacketData -) diff --git a/tests/difference/core/driver/core_test.go b/tests/difference/core/driver/core_test.go index ec97fbf9c1..b9eaa52b9c 100644 --- a/tests/difference/core/driver/core_test.go +++ b/tests/difference/core/driver/core_test.go @@ -4,18 +4,16 @@ import ( "fmt" "time" - channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" + sdk "github.com/cosmos/cosmos-sdk/types" + ibctesting "github.com/cosmos/ibc-go/v7/testing" "github.com/stretchr/testify/suite" - sdk "github.com/cosmos/cosmos-sdk/types" slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper" stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" appConsumer "github.com/cosmos/interchain-security/v3/app/consumer" appProvider "github.com/cosmos/interchain-security/v3/app/provider" - ibctestingcore "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/core" - ibctesting "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/testing" simibc "github.com/cosmos/interchain-security/v3/testutil/simibc" consumerkeeper "github.com/cosmos/interchain-security/v3/x/ccv/consumer/keeper" ) @@ -184,13 +182,10 @@ func (s *CoreSuite) consumerSlash(val sdk.ConsAddress, h int64, isDowntime bool) before := len(ctx.EventManager().Events()) s.consumerKeeper().SlashWithInfractionReason(ctx, val, h, 0, sdk.Dec{}, kind) // consumer module emits packets on slash, so these must be collected. - evts := ctx.EventManager().ABCIEvents() - for _, e := range evts[before:] { - if e.Type == channeltypes.EventTypeSendPacket { - packet, err := ibctestingcore.ReconstructPacketFromEvent(e) - s.Require().NoError(err) - s.simibc.Outboxes.AddPacket(s.chainID(C), packet) - } + evts := ctx.EventManager().Events() + packets := simibc.ParsePacketsFromEvents(evts[before:]) + if len(packets) > 0 { + s.simibc.Outboxes.AddPacket(s.chainID(C), packets[0]) } } diff --git a/tests/difference/core/driver/setup.go b/tests/difference/core/driver/setup.go index 9ca6d00b9f..832e1d3e44 100644 --- a/tests/difference/core/driver/setup.go +++ b/tests/difference/core/driver/setup.go @@ -6,14 +6,9 @@ import ( "encoding/json" "time" - clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types" - channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" - commitmenttypes "github.com/cosmos/ibc-go/v7/modules/core/23-commitment/types" - ibctmtypes "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint" - "github.com/cosmos/ibc-go/v7/testing/mock" - "github.com/stretchr/testify/require" - "github.com/stretchr/testify/suite" - + abci "github.com/cometbft/cometbft/abci/types" + tmproto "github.com/cometbft/cometbft/proto/tendermint/types" + tmtypes "github.com/cometbft/cometbft/types" "github.com/cosmos/cosmos-sdk/baseapp" codectypes "github.com/cosmos/cosmos-sdk/codec/types" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" @@ -26,15 +21,20 @@ import ( slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" - abci "github.com/cometbft/cometbft/abci/types" - tmproto "github.com/cometbft/cometbft/proto/tendermint/types" - tmtypes "github.com/cometbft/cometbft/types" + clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types" + channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" + commitmenttypes "github.com/cosmos/ibc-go/v7/modules/core/23-commitment/types" + ibctmtypes "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint" + ibctesting "github.com/cosmos/ibc-go/v7/testing" + "github.com/cosmos/ibc-go/v7/testing/mock" appConsumer "github.com/cosmos/interchain-security/v3/app/consumer" appProvider "github.com/cosmos/interchain-security/v3/app/provider" - ibctesting "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/testing" icstestingutils "github.com/cosmos/interchain-security/v3/testutil/ibc_testing" + testutil "github.com/cosmos/interchain-security/v3/testutil/integration" simibc "github.com/cosmos/interchain-security/v3/testutil/simibc" consumerkeeper "github.com/cosmos/interchain-security/v3/x/ccv/consumer/keeper" consumertypes "github.com/cosmos/interchain-security/v3/x/ccv/consumer/types" @@ -252,7 +252,7 @@ func (b *Builder) getAppBytesAndSenders( func (b *Builder) newChain( coord *ibctesting.Coordinator, - appInit ibctesting.AppIniter, + appInit icstestingutils.AppIniter, chainID string, validators *tmtypes.ValidatorSet, signers map[string]tmtypes.PrivValidator, @@ -349,7 +349,8 @@ func (b *Builder) createProviderAndConsumer() { // Create provider coordinator.Chains[ibctesting.GetChainID(0)] = b.newChain(coordinator, icstestingutils.ProviderAppIniter, ibctesting.GetChainID(0), validators, signers) // Create consumer, using the same validators. - coordinator.Chains[ibctesting.GetChainID(1)] = b.newChain(coordinator, icstestingutils.ConsumerAppIniter, ibctesting.GetChainID(1), validators, signers) + valUpdates := testutil.ToValidatorUpdates(b.suite.T(), validators) + coordinator.Chains[ibctesting.GetChainID(1)] = b.newChain(coordinator, icstestingutils.ConsumerAppIniter(valUpdates), ibctesting.GetChainID(1), validators, signers) b.coordinator = coordinator b.valAddresses = addresses diff --git a/tests/integration/common.go b/tests/integration/common.go index 7d5175d5d7..78a8ba334d 100644 --- a/tests/integration/common.go +++ b/tests/integration/common.go @@ -9,6 +9,7 @@ import ( commitmenttypes "github.com/cosmos/ibc-go/v7/modules/core/23-commitment/types" "github.com/cosmos/ibc-go/v7/modules/core/exported" ibctm "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint" + ibctesting "github.com/cosmos/ibc-go/v7/testing" "github.com/stretchr/testify/require" "cosmossdk.io/math" @@ -21,7 +22,6 @@ import ( abci "github.com/cometbft/cometbft/abci/types" tmtypes "github.com/cometbft/cometbft/types" - ibctesting "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/testing" icstestingutils "github.com/cosmos/interchain-security/v3/testutil/ibc_testing" testutil "github.com/cosmos/interchain-security/v3/testutil/integration" providertypes "github.com/cosmos/interchain-security/v3/x/ccv/provider/types" @@ -216,19 +216,33 @@ func redelegate(s *CCVTestSuite, delAddr sdk.AccAddress, valSrcAddr sdk.ValAddre } // sendOnProviderRecvOnConsumer sends a packet from the provider chain and receives it on the consumer chain -func sendOnProviderRecvOnConsumer(s *CCVTestSuite, path *ibctesting.Path, packet channeltypes.Packet) { - err := path.EndpointB.SendPacket(packet) +func sendOnProviderRecvOnConsumer(s *CCVTestSuite, path *ibctesting.Path, timeoutHeight clienttypes.Height, timeoutTimestamp uint64, data []byte) channeltypes.Packet { + sequence, err := path.EndpointB.SendPacket(timeoutHeight, timeoutTimestamp, data) s.Require().NoError(err) + + packet := channeltypes.NewPacket(data, sequence, + path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, + path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, + timeoutHeight, timeoutTimestamp) + err = path.EndpointA.RecvPacket(packet) s.Require().NoError(err) + return packet } // sendOnConsumerRecvOnProvider sends a packet from the consumer chain and receives it on the provider chain -func sendOnConsumerRecvOnProvider(s *CCVTestSuite, path *ibctesting.Path, packet channeltypes.Packet) { - err := path.EndpointA.SendPacket(packet) +func sendOnConsumerRecvOnProvider(s *CCVTestSuite, path *ibctesting.Path, timeoutHeight clienttypes.Height, timeoutTimestamp uint64, data []byte) channeltypes.Packet { + sequence, err := path.EndpointA.SendPacket(timeoutHeight, timeoutTimestamp, data) s.Require().NoError(err) + + packet := channeltypes.NewPacket(data, sequence, + path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, + path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, + timeoutHeight, timeoutTimestamp) + err = path.EndpointB.RecvPacket(packet) s.Require().NoError(err) + return packet } // relayAllCommittedPackets relays all committed packets from `srcChain` on `path` @@ -256,7 +270,7 @@ func relayAllCommittedPackets( // relay all packets from srcChain to counterparty for _, commitment := range commitments { // - get packets - packet, found := srcChain.GetSentPacket(commitment.Sequence, srcChannelID) + packet, found := s.getSentPacket(srcChain, commitment.Sequence, srcChannelID) s.Require().True( found, fmt.Sprintf("did not find sent packet; %s", msgAndArgs...), @@ -278,7 +292,7 @@ func relayAllCommittedPackets( // to be one day larger than the consumer unbonding period. func incrementTimeByUnbondingPeriod(s *CCVTestSuite, chainType ChainType) { // Get unboding periods - providerUnbondingPeriod := s.providerApp.GetStakingKeeper().UnbondingTime(s.providerCtx()) + providerUnbondingPeriod := s.providerApp.GetTestStakingKeeper().UnbondingTime(s.providerCtx()) consumerUnbondingPeriod := s.consumerApp.GetConsumerKeeper().GetUnbondingPeriod(s.consumerCtx()) var jumpPeriod time.Duration if chainType == Provider { @@ -374,14 +388,7 @@ func (suite *CCVTestSuite) SendEmptyVSCPacket() { nil, ) - seq, ok := suite.providerApp.GetIBCKeeper().ChannelKeeper.GetNextSequenceSend( - suite.providerChain.GetContext(), ccv.ProviderPortID, suite.path.EndpointB.ChannelID) - suite.Require().True(ok) - - packet := channeltypes.NewPacket(pd.GetBytes(), seq, ccv.ProviderPortID, suite.path.EndpointB.ChannelID, - ccv.ConsumerPortID, suite.path.EndpointA.ChannelID, clienttypes.Height{}, timeout) - - sendOnProviderRecvOnConsumer(suite, suite.getFirstBundle().Path, packet) + sendOnProviderRecvOnConsumer(suite, suite.getFirstBundle().Path, clienttypes.Height{}, timeout, pd.GetBytes()) } // commitSlashPacket returns a commit hash for the given slash packet data @@ -412,8 +419,8 @@ func (suite *CCVTestSuite) commitConsumerPacket(ctx sdk.Context, packetData ccv. // constructSlashPacketFromConsumer constructs an IBC packet embedding // slash packet data to be sent from consumer to provider func (s *CCVTestSuite) constructSlashPacketFromConsumer(bundle icstestingutils.ConsumerBundle, - tmVal tmtypes.Validator, infractionType stakingtypes.Infraction, ibcSeqNum uint64, -) channeltypes.Packet { + tmVal tmtypes.Validator, infractionType stakingtypes.Infraction, +) []byte { valsetUpdateId := bundle.GetKeeper().GetHeightValsetUpdateID( bundle.GetCtx(), uint64(bundle.GetCtx().BlockHeight())) @@ -430,23 +437,12 @@ func (s *CCVTestSuite) constructSlashPacketFromConsumer(bundle icstestingutils.C }, }, } - - return channeltypes.NewPacket(data.GetBytes(), - ibcSeqNum, - ccv.ConsumerPortID, // Src port - bundle.Path.EndpointA.ChannelID, // Src channel - ccv.ProviderPortID, // Dst port - bundle.Path.EndpointB.ChannelID, // Dst channel - clienttypes.Height{}, - uint64(bundle.GetCtx().BlockTime().Add(ccv.DefaultCCVTimeoutPeriod).UnixNano()), - ) + return data.GetBytes() } // constructVSCMaturedPacketFromConsumer constructs an IBC packet embedding // VSC Matured packet data to be sent from consumer to provider -func (s *CCVTestSuite) constructVSCMaturedPacketFromConsumer(bundle icstestingutils.ConsumerBundle, - ibcSeqNum uint64, -) channeltypes.Packet { +func (s *CCVTestSuite) constructVSCMaturedPacketFromConsumer(bundle icstestingutils.ConsumerBundle) []byte { valsetUpdateId := bundle.GetKeeper().GetHeightValsetUpdateID( bundle.GetCtx(), uint64(bundle.GetCtx().BlockHeight())) @@ -456,16 +452,7 @@ func (s *CCVTestSuite) constructVSCMaturedPacketFromConsumer(bundle icstestingut VscMaturedPacketData: &ccv.VSCMaturedPacketData{ValsetUpdateId: valsetUpdateId}, }, } - - return channeltypes.NewPacket(data.GetBytes(), - ibcSeqNum, - ccv.ConsumerPortID, // Src port - bundle.Path.EndpointA.ChannelID, // Src channel - ccv.ProviderPortID, // Dst port - bundle.Path.EndpointB.ChannelID, // Dst channel - clienttypes.Height{}, - uint64(bundle.GetCtx().BlockTime().Add(ccv.DefaultCCVTimeoutPeriod).UnixNano()), - ) + return data.GetBytes() } // incrementTime increments the overall time by jumpPeriod diff --git a/tests/integration/democracy.go b/tests/integration/democracy.go index 461cfec3b3..f90f40bcbd 100644 --- a/tests/integration/democracy.go +++ b/tests/integration/democracy.go @@ -1,20 +1,17 @@ package integration import ( - "testing" "time" - "github.com/stretchr/testify/suite" - "cosmossdk.io/math" - sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" + ibctesting "github.com/cosmos/ibc-go/v7/testing" + "github.com/stretchr/testify/suite" - ibctesting "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/testing" icstestingutils "github.com/cosmos/interchain-security/v3/testutil/ibc_testing" testutil "github.com/cosmos/interchain-security/v3/testutil/integration" consumertypes "github.com/cosmos/interchain-security/v3/x/ccv/consumer/types" @@ -31,22 +28,22 @@ type ConsumerDemocracyTestSuite struct { // NewCCVTestSuite returns a new instance of ConsumerDemocracyTestSuite, // ready to be tested against using suite.Run(). func NewConsumerDemocracyTestSuite[T testutil.DemocConsumerApp]( - democConsumerAppIniter ibctesting.AppIniter, + democConsumerAppIniter icstestingutils.ValSetAppIniter, ) *ConsumerDemocracyTestSuite { democSuite := new(ConsumerDemocracyTestSuite) - democSuite.setupCallback = func(t *testing.T) ( + democSuite.setupCallback = func(s *suite.Suite) ( *ibctesting.Coordinator, *ibctesting.TestChain, testutil.DemocConsumerApp, ) { - t.Helper() + s.T().Helper() // Instantiate the test coordinator - coordinator := ibctesting.NewCoordinator(t, 0) + coordinator := ibctesting.NewCoordinator(s.T(), 0) // Add single democracy consumer to coordinator, store returned test chain and app. democConsumer, democConsumerApp := icstestingutils.AddDemocracyConsumer[T]( - t, coordinator, democConsumerAppIniter) + coordinator, s, democConsumerAppIniter) // Pass variables to suite. return coordinator, democConsumer, democConsumerApp @@ -56,7 +53,7 @@ func NewConsumerDemocracyTestSuite[T testutil.DemocConsumerApp]( // Callback for instantiating a new coordinator, consumer test chain, and consumer app // before every test defined on the suite. -type DemocSetupCallback func(t *testing.T) ( +type DemocSetupCallback func(s *suite.Suite) ( coord *ibctesting.Coordinator, consumerChain *ibctesting.TestChain, consumerApp testutil.DemocConsumerApp, @@ -66,7 +63,7 @@ type DemocSetupCallback func(t *testing.T) ( func (suite *ConsumerDemocracyTestSuite) SetupTest() { // Instantiate new test utils using callback suite.coordinator, suite.consumerChain, - suite.consumerApp = suite.setupCallback(suite.T()) + suite.consumerApp = suite.setupCallback(&suite.Suite) } func (s *ConsumerDemocracyTestSuite) TestDemocracyRewardsDistribution() { diff --git a/tests/integration/expired_client.go b/tests/integration/expired_client.go index 2a8babacfa..f196f16ce1 100644 --- a/tests/integration/expired_client.go +++ b/tests/integration/expired_client.go @@ -6,6 +6,7 @@ import ( clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types" ibcexported "github.com/cosmos/ibc-go/v7/modules/core/exported" ibctm "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint" + ibctesting "github.com/cosmos/ibc-go/v7/testing" "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519" sdk "github.com/cosmos/cosmos-sdk/types" @@ -13,7 +14,6 @@ import ( abci "github.com/cometbft/cometbft/abci/types" - ibctesting "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/testing" ccv "github.com/cosmos/interchain-security/v3/x/ccv/types" ) diff --git a/tests/integration/setup.go b/tests/integration/setup.go index 61cb8a96c9..76f6311dab 100644 --- a/tests/integration/setup.go +++ b/tests/integration/setup.go @@ -1,21 +1,28 @@ package integration import ( + "context" + "fmt" + "sync" "testing" transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types" channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" ibctmtypes "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint" + ibctesting "github.com/cosmos/ibc-go/v7/testing" "github.com/cosmos/ibc-go/v7/testing/mock" "github.com/stretchr/testify/suite" + "github.com/cosmos/cosmos-sdk/baseapp" + store "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" + abci "github.com/cometbft/cometbft/abci/types" tmencoding "github.com/cometbft/cometbft/crypto/encoding" - ibctesting "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/testing" icstestingutils "github.com/cosmos/interchain-security/v3/testutil/ibc_testing" testutil "github.com/cosmos/interchain-security/v3/testutil/integration" + "github.com/cosmos/interchain-security/v3/testutil/simibc" ccv "github.com/cosmos/interchain-security/v3/x/ccv/types" ) @@ -58,11 +65,16 @@ type CCVTestSuite struct { // The preferred way to access chains, apps, and paths when designing tests around multiple consumers. consumerBundles map[string]*icstestingutils.ConsumerBundle skippedTests map[string]bool + + // packetSniffers maps a chain and a packetSniffer + packetSniffers map[*ibctesting.TestChain]*packetSniffer } // NewCCVTestSuite returns a new instance of CCVTestSuite, ready to be tested against using suite.Run(). func NewCCVTestSuite[Tp testutil.ProviderApp, Tc testutil.ConsumerApp]( - providerAppIniter, consumerAppIniter ibctesting.AppIniter, skippedTests []string, + providerAppIniter icstestingutils.AppIniter, + consumerAppIniter icstestingutils.ValSetAppIniter, + skippedTests []string, ) *CCVTestSuite { ccvSuite := new(CCVTestSuite) @@ -107,9 +119,12 @@ func (suite *CCVTestSuite) BeforeTest(suiteName, testName string) { // SetupTest sets up in-mem state before every test func (suite *CCVTestSuite) SetupTest() { + suite.packetSniffers = make(map[*ibctesting.TestChain]*packetSniffer) + // Instantiate new coordinator and provider chain using callback suite.coordinator, suite.providerChain, suite.providerApp = suite.setupProviderCallback(suite.T()) + suite.registerPacketSniffer(suite.providerChain) providerKeeper := suite.providerApp.GetProviderKeeper() // re-assign all validator keys for the first consumer chain @@ -121,6 +136,7 @@ func (suite *CCVTestSuite) SetupTest() { for i := 0; i < numConsumers; i++ { bundle := suite.setupConsumerCallback(&suite.Suite, suite.coordinator, i) suite.consumerBundles[bundle.Chain.ChainID] = bundle + suite.registerPacketSniffer(bundle.Chain) } // initialize each consumer chain with it's corresponding genesis state @@ -147,6 +163,21 @@ func (suite *CCVTestSuite) SetupTest() { } } +func (s *CCVTestSuite) registerPacketSniffer(chain *ibctesting.TestChain) { + if s.packetSniffers == nil { + s.packetSniffers = make(map[*ibctesting.TestChain]*packetSniffer) + } + p := newPacketSniffer() + chain.App.GetBaseApp().SetStreamingService(p) + s.packetSniffers[chain] = p +} + +func (s *CCVTestSuite) getSentPacket(chain *ibctesting.TestChain, sequence uint64, channelID string) (packet channeltypes.Packet, found bool) { + key := getSentPacketKey(sequence, channelID) + packet, found = s.packetSniffers[chain].packets[key] + return +} + // initConsumerChain initializes a consumer chain given a genesis state func initConsumerChain( s *CCVTestSuite, @@ -292,7 +323,7 @@ func (suite *CCVTestSuite) SetupTransferChannel() { func (s CCVTestSuite) validateEndpointsClientConfig(consumerBundle icstestingutils.ConsumerBundle) { //nolint:govet // this is a test so we can copy locks consumerKeeper := consumerBundle.GetKeeper() - providerStakingKeeper := s.providerApp.GetStakingKeeper() + providerStakingKeeper := s.providerApp.GetTestStakingKeeper() consumerUnbondingPeriod := consumerKeeper.GetUnbondingPeriod(consumerBundle.GetCtx()) cs, ok := s.providerApp.GetIBCKeeper().ClientKeeper.GetClientState(s.providerCtx(), @@ -342,3 +373,46 @@ func preProposalKeyAssignment(s *CCVTestSuite, chainID string) { s.Require().NoError(err) } } + +// packetSniffer implements the StreamingService interface. +// Implements ListenEndBlock to record packets from events. +type packetSniffer struct { + packets map[string]channeltypes.Packet +} + +var _ baseapp.StreamingService = &packetSniffer{} + +func newPacketSniffer() *packetSniffer { + return &packetSniffer{ + packets: make(map[string]channeltypes.Packet), + } +} + +func (ps *packetSniffer) ListenEndBlock(ctx context.Context, req abci.RequestEndBlock, res abci.ResponseEndBlock) error { + packets := simibc.ParsePacketsFromEvents(simibc.ABCIToSDKEvents(res.GetEvents())) + for _, packet := range packets { + ps.packets[getSentPacketKey(packet.Sequence, packet.SourceChannel)] = packet + } + return nil +} + +// getSentPacketKey returns a key for accessing a sent packet, +// given an ibc sequence number and the channel ID for the source endpoint. +func getSentPacketKey(sequence uint64, channelID string) string { + return fmt.Sprintf("%s-%d", channelID, sequence) +} + +func (*packetSniffer) ListenBeginBlock(ctx context.Context, req abci.RequestBeginBlock, res abci.ResponseBeginBlock) error { + return nil +} + +func (*packetSniffer) ListenCommit(ctx context.Context, res abci.ResponseCommit) error { + return nil +} + +func (*packetSniffer) ListenDeliverTx(ctx context.Context, req abci.RequestDeliverTx, res abci.ResponseDeliverTx) error { + return nil +} +func (*packetSniffer) Close() error { return nil } +func (*packetSniffer) Listeners() map[store.StoreKey][]store.WriteListener { return nil } +func (*packetSniffer) Stream(wg *sync.WaitGroup) error { return nil } diff --git a/tests/integration/slashing.go b/tests/integration/slashing.go index 1ce7d11db8..c3fdfdc0c1 100644 --- a/tests/integration/slashing.go +++ b/tests/integration/slashing.go @@ -68,8 +68,12 @@ func (s *CCVTestSuite) TestRelayAndApplyDowntimePacket() { s.setDefaultValSigningInfo(*tmtypes.NewValidator(tmPk, stakingVal.ConsensusPower(sdk.DefaultPowerReduction))) // Send slash packet from the first consumer chain - packet := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmVal, stakingtypes.Infraction_INFRACTION_DOWNTIME, 1) - err = s.getFirstBundle().Path.EndpointA.SendPacket(packet) + var ( + timeoutHeight = clienttypes.Height{} + timeoutTimestamp = uint64(s.getFirstBundle().GetCtx().BlockTime().Add(ccv.DefaultCCVTimeoutPeriod).UnixNano()) + ) + data := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmVal, stakingtypes.Infraction_INFRACTION_DOWNTIME) + sequence, err := s.getFirstBundle().Path.EndpointA.SendPacket(timeoutHeight, timeoutTimestamp, data) s.Require().NoError(err) // Set outstanding slashing flag for first consumer, it's important to use the consumer's cons addr here @@ -86,6 +90,10 @@ func (s *CCVTestSuite) TestRelayAndApplyDowntimePacket() { valsetUpdateIdN := providerKeeper.GetValidatorSetUpdateId(s.providerCtx()) // receive the slash packet on the provider chain. RecvPacket() calls the provider endblocker twice + packet := channeltypes.NewPacket(data, sequence, + s.getFirstBundle().Path.EndpointA.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointA.ChannelID, + s.getFirstBundle().Path.EndpointB.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointB.ChannelID, + timeoutHeight, timeoutTimestamp) err = s.path.EndpointB.RecvPacket(packet) s.Require().NoError(err) @@ -196,13 +204,12 @@ func (s *CCVTestSuite) TestRelayAndApplyDoubleSignPacket() { s.setDefaultValSigningInfo(*tmtypes.NewValidator(tmPk, stakingVal.ConsensusPower(sdk.DefaultPowerReduction))) // Send slash packet from the first consumer chain - packet := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmVal, stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN, 1) - err = s.getFirstBundle().Path.EndpointA.SendPacket(packet) - s.Require().NoError(err) - - // receive the slash packet on the provider chain. RecvPacket() advances two blocks - err = s.path.EndpointB.RecvPacket(packet) - s.Require().NoError(err) + var ( + timeoutHeight = clienttypes.Height{} + timeoutTimestamp = uint64(s.getFirstBundle().GetCtx().BlockTime().Add(ccv.DefaultCCVTimeoutPeriod).UnixNano()) + ) + data := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmVal, stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN) + packet := sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, data) // Advance a few more blocks to make sure any voting power changes would be reflected s.providerChain.NextBlock() diff --git a/tests/integration/throttle.go b/tests/integration/throttle.go index 0505d6257a..2963683c87 100644 --- a/tests/integration/throttle.go +++ b/tests/integration/throttle.go @@ -3,6 +3,7 @@ package integration import ( "time" + clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types" channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -59,12 +60,16 @@ func (s *CCVTestSuite) TestBasicSlashPacketThrottling() { for _, val := range vals { s.Require().False(val.IsJailed()) } + var ( + timeoutHeight = clienttypes.Height{} + timeoutTimestamp = uint64(s.getFirstBundle().GetCtx().BlockTime().Add(ccvtypes.DefaultCCVTimeoutPeriod).UnixNano()) + ) // Send a slash packet from consumer to provider s.setDefaultValSigningInfo(*s.providerChain.Vals.Validators[0]) tmVal := s.providerChain.Vals.Validators[0] - packet := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmVal, stakingtypes.Infraction_INFRACTION_DOWNTIME, 1) - sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, packet) + data := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmVal, stakingtypes.Infraction_INFRACTION_DOWNTIME) + sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, data) // Assert validator 0 is jailed and has no power vals = providerStakingKeeper.GetAllValidators(s.providerCtx()) @@ -82,8 +87,8 @@ func (s *CCVTestSuite) TestBasicSlashPacketThrottling() { // Now send a second slash packet from consumer to provider for a different validator. s.setDefaultValSigningInfo(*s.providerChain.Vals.Validators[2]) tmVal = s.providerChain.Vals.Validators[2] - packet = s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmVal, stakingtypes.Infraction_INFRACTION_DOWNTIME, 2) - sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, packet) + data = s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmVal, stakingtypes.Infraction_INFRACTION_DOWNTIME) + sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, data) // Require that slash packet has not been handled vals = providerStakingKeeper.GetAllValidators(s.providerCtx()) @@ -163,11 +168,15 @@ func (s *CCVTestSuite) TestMultiConsumerSlashPacketThrottling() { // when no slash packets are sent. // Send 2 VSC matured packets from every consumer to provider + var ( + timeoutHeight = clienttypes.Height{} + timeoutTimestamp = uint64(s.getFirstBundle().GetCtx().BlockTime().Add(ccvtypes.DefaultCCVTimeoutPeriod).UnixNano()) + ) for consumerChainID, bundle := range s.consumerBundles { - packet := s.constructVSCMaturedPacketFromConsumer(*bundle, 1) // use sequence 1 - sendOnConsumerRecvOnProvider(s, bundle.Path, packet) - packet = s.constructVSCMaturedPacketFromConsumer(*bundle, 2) // use sequence 2 - sendOnConsumerRecvOnProvider(s, bundle.Path, packet) + data := s.constructVSCMaturedPacketFromConsumer(*bundle) + sendOnConsumerRecvOnProvider(s, bundle.Path, timeoutHeight, timeoutTimestamp, data) + data = s.constructVSCMaturedPacketFromConsumer(*bundle) + sendOnConsumerRecvOnProvider(s, bundle.Path, timeoutHeight, timeoutTimestamp, data) // Confirm packets were not queued on provider for this consumer s.Require().Equal(uint64(0), providerKeeper.GetThrottledPacketDataSize(s.providerCtx(), consumerChainID)) @@ -196,19 +205,18 @@ func (s *CCVTestSuite) TestMultiConsumerSlashPacketThrottling() { // Send downtime slash packet from consumer to provider tmVal := s.providerChain.Vals.Validators[idx] valsToSlash = append(valsToSlash, *tmVal) - packet := s.constructSlashPacketFromConsumer( + data := s.constructSlashPacketFromConsumer( *bundle, *tmVal, stakingtypes.Infraction_INFRACTION_DOWNTIME, - 3, // use sequence 3, 1 and 2 are used above. ) - sendOnConsumerRecvOnProvider(s, bundle.Path, packet) + sendOnConsumerRecvOnProvider(s, bundle.Path, timeoutHeight, timeoutTimestamp, data) // Send two trailing VSC matured packets from consumer to provider - packet = s.constructVSCMaturedPacketFromConsumer(*bundle, 4) // use sequence 4 - sendOnConsumerRecvOnProvider(s, bundle.Path, packet) - packet = s.constructVSCMaturedPacketFromConsumer(*bundle, 5) // use sequence 5 - sendOnConsumerRecvOnProvider(s, bundle.Path, packet) + data = s.constructVSCMaturedPacketFromConsumer(*bundle) + sendOnConsumerRecvOnProvider(s, bundle.Path, timeoutHeight, timeoutTimestamp, data) + data = s.constructVSCMaturedPacketFromConsumer(*bundle) + sendOnConsumerRecvOnProvider(s, bundle.Path, timeoutHeight, timeoutTimestamp, data) idx++ } @@ -287,8 +295,8 @@ func (s *CCVTestSuite) TestPacketSpam() { providerKeeper.SetParams(s.providerCtx(), params) providerKeeper.InitializeSlashMeter(s.providerCtx()) - // The packets to be recv in a single block, ordered as they will be recv. - packets := []channeltypes.Packet{} + // The packets data to be recv in a single block, ordered as they will be recv. + var packetsData [][]byte firstBundle := s.getFirstBundle() @@ -312,13 +320,21 @@ func (s *CCVTestSuite) TestPacketSpam() { infractionType = stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN } valToJail := s.providerChain.Vals.Validators[ibcSeqNum%3] - packets = append(packets, s.constructSlashPacketFromConsumer(firstBundle, *valToJail, infractionType, ibcSeqNum)) + packetsData = append(packetsData, s.constructSlashPacketFromConsumer(firstBundle, *valToJail, infractionType)) } // Recv 500 packets from consumer to provider in same block - for _, packet := range packets { - consumerPacketData, err := provider.UnmarshalConsumerPacket(packet) // Same func used by provider's OnRecvPacket + var ( + timeoutHeight = clienttypes.Height{} + timeoutTimestamp = uint64(firstBundle.GetCtx().BlockTime().Add(ccvtypes.DefaultCCVTimeoutPeriod).UnixNano()) + ) + for sequence, data := range packetsData { + consumerPacketData, err := provider.UnmarshalConsumerPacket(channeltypes.Packet{Data: data}) // Same func used by provider's OnRecvPacket s.Require().NoError(err) + packet := channeltypes.NewPacket(data, uint64(sequence), + firstBundle.Path.EndpointA.ChannelConfig.PortID, firstBundle.Path.EndpointA.ChannelID, + firstBundle.Path.EndpointB.ChannelConfig.PortID, firstBundle.Path.EndpointB.ChannelID, + timeoutHeight, timeoutTimestamp) providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, *consumerPacketData.GetSlashPacketData()) } @@ -349,8 +365,8 @@ func (s *CCVTestSuite) TestDoubleSignDoesNotAffectThrottling() { providerKeeper.SetParams(s.providerCtx(), params) providerKeeper.InitializeSlashMeter(s.providerCtx()) - // The packets to be recv in a single block, ordered as they will be recv. - packets := []channeltypes.Packet{} + // The packetsData to be recv in a single block, ordered as they will be recv. + var packetsData [][]byte firstBundle := s.getFirstBundle() @@ -366,13 +382,21 @@ func (s *CCVTestSuite) TestDoubleSignDoesNotAffectThrottling() { // Increment ibc seq num for each packet (starting at 1) ibcSeqNum++ valToJail := s.providerChain.Vals.Validators[ibcSeqNum%3] - packets = append(packets, s.constructSlashPacketFromConsumer(firstBundle, *valToJail, stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN, ibcSeqNum)) + packetsData = append(packetsData, s.constructSlashPacketFromConsumer(firstBundle, *valToJail, stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN)) } // Recv 500 packets from consumer to provider in same block - for _, packet := range packets { - consumerPacketData, err := provider.UnmarshalConsumerPacket(packet) // Same func used by provider's OnRecvPacket + var ( + timeoutHeight = clienttypes.Height{} + timeoutTimestamp = uint64(firstBundle.GetCtx().BlockTime().Add(ccvtypes.DefaultCCVTimeoutPeriod).UnixNano()) + ) + for sequence, data := range packetsData { + consumerPacketData, err := provider.UnmarshalConsumerPacket(channeltypes.Packet{Data: data}) // Same func used by provider's OnRecvPacket s.Require().NoError(err) + packet := channeltypes.NewPacket(data, uint64(sequence), + firstBundle.Path.EndpointA.ChannelConfig.PortID, firstBundle.Path.EndpointA.ChannelID, + firstBundle.Path.EndpointB.ChannelConfig.PortID, firstBundle.Path.EndpointB.ChannelID, + timeoutHeight, timeoutTimestamp) providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, *consumerPacketData.GetSlashPacketData()) } @@ -438,7 +462,7 @@ func (s *CCVTestSuite) TestQueueOrdering() { providerKeeper.InitializeSlashMeter(s.providerCtx()) // The packets to be recv in a single block, ordered as they will be recv. - packets := []channeltypes.Packet{} + var packetsData [][]byte firstBundle := s.getFirstBundle() @@ -448,29 +472,39 @@ func (s *CCVTestSuite) TestQueueOrdering() { s.setDefaultValSigningInfo(*s.providerChain.Vals.Validators[2]) // Track and increment ibc seq num for each packet, since these need to be unique. - ibcSeqNum := uint64(4) + var ( + ibcSeqNum = uint64(4) + ibcSeqs []uint64 + ) for ibcSeqNum < 504 { // Increment ibc seq num for each packet (starting at 5) ibcSeqNum++ + ibcSeqs = append(ibcSeqs, ibcSeqNum) // Instantiate a vsc matured packet every 10th packet if ibcSeqNum%10 == 0 { - packets = append(packets, s.constructVSCMaturedPacketFromConsumer(firstBundle, ibcSeqNum)) + packetsData = append(packetsData, s.constructVSCMaturedPacketFromConsumer(firstBundle)) continue } // Else instantiate a slash packet valToJail := s.providerChain.Vals.Validators[ibcSeqNum%3] - packets = append(packets, s.constructSlashPacketFromConsumer(firstBundle, *valToJail, stakingtypes.Infraction_INFRACTION_DOWNTIME, ibcSeqNum)) + packetsData = append(packetsData, s.constructSlashPacketFromConsumer(firstBundle, *valToJail, stakingtypes.Infraction_INFRACTION_DOWNTIME)) } // Recv 500 packets from consumer to provider in same block - for i, packet := range packets { - - consumerPacketData, err := provider.UnmarshalConsumerPacket(packet) // Same func used by provider's OnRecvPacket + var ( + timeoutHeight = clienttypes.Height{} + timeoutTimestamp = uint64(firstBundle.GetCtx().BlockTime().Add(ccvtypes.DefaultCCVTimeoutPeriod).UnixNano()) + ) + for i, data := range packetsData { + consumerPacketData, err := provider.UnmarshalConsumerPacket(channeltypes.Packet{Data: data}) // Same func used by provider's OnRecvPacket s.Require().NoError(err) - + packet := channeltypes.NewPacket(data, ibcSeqs[i], + firstBundle.Path.EndpointA.ChannelConfig.PortID, firstBundle.Path.EndpointA.ChannelID, + firstBundle.Path.EndpointB.ChannelConfig.PortID, firstBundle.Path.EndpointB.ChannelID, + timeoutHeight, timeoutTimestamp) // Type depends on index packets were appended from above if (i+5)%10 == 0 { vscMaturedPacketData := consumerPacketData.GetVscMaturedPacketData() @@ -595,15 +629,19 @@ func (s *CCVTestSuite) TestSlashingSmallValidators() { s.setDefaultValSigningInfo(*s.providerChain.Vals.Validators[3]) // Send slash packets from consumer to provider for small validators. + var ( + timeoutHeight = clienttypes.Height{} + timeoutTimestamp = uint64(s.getFirstBundle().GetCtx().BlockTime().Add(ccvtypes.DefaultCCVTimeoutPeriod).UnixNano()) + ) tmval1 := s.providerChain.Vals.Validators[1] tmval2 := s.providerChain.Vals.Validators[2] tmval3 := s.providerChain.Vals.Validators[3] - packet1 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval1, stakingtypes.Infraction_INFRACTION_DOWNTIME, 1) - packet2 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval2, stakingtypes.Infraction_INFRACTION_DOWNTIME, 2) - packet3 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval3, stakingtypes.Infraction_INFRACTION_DOWNTIME, 3) - sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, packet1) - sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, packet2) - sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, packet3) + data1 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval1, stakingtypes.Infraction_INFRACTION_DOWNTIME) + data2 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval2, stakingtypes.Infraction_INFRACTION_DOWNTIME) + data3 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval3, stakingtypes.Infraction_INFRACTION_DOWNTIME) + sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, data1) + sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, data2) + sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, data3) // Default slash meter replenish fraction is 0.05, so all sent packets should be handled immediately. vals = providerStakingKeeper.GetAllValidators(s.providerCtx()) @@ -672,19 +710,27 @@ func (s *CCVTestSuite) TestSlashSameValidator() { s.setDefaultValSigningInfo(*tmval2) s.setDefaultValSigningInfo(*tmval3) - packets := []channeltypes.Packet{ - s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval1, stakingtypes.Infraction_INFRACTION_DOWNTIME, 1), - s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval2, stakingtypes.Infraction_INFRACTION_DOWNTIME, 2), - s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval3, stakingtypes.Infraction_INFRACTION_DOWNTIME, 3), - s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval1, stakingtypes.Infraction_INFRACTION_DOWNTIME, 4), - s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval2, stakingtypes.Infraction_INFRACTION_DOWNTIME, 5), - s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval3, stakingtypes.Infraction_INFRACTION_DOWNTIME, 6), + packetsData := [][]byte{ + s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval1, stakingtypes.Infraction_INFRACTION_DOWNTIME), + s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval2, stakingtypes.Infraction_INFRACTION_DOWNTIME), + s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval3, stakingtypes.Infraction_INFRACTION_DOWNTIME), + s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval1, stakingtypes.Infraction_INFRACTION_DOWNTIME), + s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval2, stakingtypes.Infraction_INFRACTION_DOWNTIME), + s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval3, stakingtypes.Infraction_INFRACTION_DOWNTIME), } // Recv and queue all slash packets. - for _, packet := range packets { - consumerPacketData, err := provider.UnmarshalConsumerPacket(packet) // Same func used by provider's OnRecvPacket + var ( + timeoutHeight = clienttypes.Height{} + timeoutTimestamp = uint64(s.getFirstBundle().GetCtx().BlockTime().Add(ccvtypes.DefaultCCVTimeoutPeriod).UnixNano()) + ) + for i, data := range packetsData { + consumerPacketData, err := provider.UnmarshalConsumerPacket(channeltypes.Packet{Data: data}) // Same func used by provider's OnRecvPacket s.Require().NoError(err) + packet := channeltypes.NewPacket(data, uint64(i), + s.getFirstBundle().Path.EndpointA.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointA.ChannelID, + s.getFirstBundle().Path.EndpointB.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointB.ChannelID, + timeoutHeight, timeoutTimestamp) providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, *consumerPacketData.GetSlashPacketData()) } @@ -719,7 +765,7 @@ func (s CCVTestSuite) TestSlashAllValidators() { //nolint:govet // this is a tes providerKeeper.InitializeSlashMeter(s.providerCtx()) // The packets to be recv in a single block, ordered as they will be recv. - packets := []channeltypes.Packet{} + var packetsData [][]byte // Track and increment ibc seq num for each packet, since these need to be unique. ibcSeqNum := uint64(1) @@ -728,24 +774,32 @@ func (s CCVTestSuite) TestSlashAllValidators() { //nolint:govet // this is a tes // these first 4 packets should jail 100% of the total power. for _, val := range s.providerChain.Vals.Validators { s.setDefaultValSigningInfo(*val) - packets = append(packets, s.constructSlashPacketFromConsumer( - s.getFirstBundle(), *val, stakingtypes.Infraction_INFRACTION_DOWNTIME, ibcSeqNum)) + packetsData = append(packetsData, s.constructSlashPacketFromConsumer( + s.getFirstBundle(), *val, stakingtypes.Infraction_INFRACTION_DOWNTIME)) ibcSeqNum++ } // add 5 more slash packets for each validator, that will be handled in the same block. for _, val := range s.providerChain.Vals.Validators { for i := 0; i < 5; i++ { - packets = append(packets, s.constructSlashPacketFromConsumer( - s.getFirstBundle(), *val, stakingtypes.Infraction_INFRACTION_DOWNTIME, ibcSeqNum)) + packetsData = append(packetsData, s.constructSlashPacketFromConsumer( + s.getFirstBundle(), *val, stakingtypes.Infraction_INFRACTION_DOWNTIME)) ibcSeqNum++ } } // Recv and queue all slash packets. - for _, packet := range packets { - consumerPacketData, err := provider.UnmarshalConsumerPacket(packet) // Same func used by provider's OnRecvPacket + var ( + timeoutHeight = clienttypes.Height{} + timeoutTimestamp = uint64(s.getFirstBundle().GetCtx().BlockTime().Add(ccvtypes.DefaultCCVTimeoutPeriod).UnixNano()) + ) + for i, data := range packetsData { + consumerPacketData, err := provider.UnmarshalConsumerPacket(channeltypes.Packet{Data: data}) // Same func used by provider's OnRecvPacket s.Require().NoError(err) + packet := channeltypes.NewPacket(data, uint64(i), + s.getFirstBundle().Path.EndpointA.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointA.ChannelID, + s.getFirstBundle().Path.EndpointB.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointB.ChannelID, + timeoutHeight, timeoutTimestamp) providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, *consumerPacketData.GetSlashPacketData()) } @@ -774,12 +828,20 @@ func (s *CCVTestSuite) TestLeadingVSCMaturedAreDequeued() { providerKeeper := s.providerApp.GetProviderKeeper() // Queue up 50 vsc matured packets for each consumer + var ( + timeoutHeight = clienttypes.Height{} + timeoutTimestamp = uint64(s.getFirstBundle().GetCtx().BlockTime().Add(ccvtypes.DefaultCCVTimeoutPeriod).UnixNano()) + ) for _, bundle := range s.consumerBundles { for i := 0; i < 50; i++ { ibcSeqNum := uint64(i) - packet := s.constructVSCMaturedPacketFromConsumer(*bundle, ibcSeqNum) - packetData := ccvtypes.ConsumerPacketData{} - ccvtypes.ModuleCdc.MustUnmarshalJSON(packet.GetData(), &packetData) + data := s.constructVSCMaturedPacketFromConsumer(*bundle) + packetData, err := provider.UnmarshalConsumerPacket(channeltypes.Packet{Data: data}) // Same func used by provider's OnRecvPacket + s.Require().NoError(err) + packet := channeltypes.NewPacket(data, ibcSeqNum, + ccvtypes.ConsumerPortID, bundle.Path.EndpointA.ChannelID, + ccvtypes.ProviderPortID, bundle.Path.EndpointB.ChannelID, + timeoutHeight, timeoutTimestamp) providerKeeper.OnRecvVSCMaturedPacket(s.providerCtx(), packet, *packetData.GetVscMaturedPacketData()) } @@ -789,11 +851,16 @@ func (s *CCVTestSuite) TestLeadingVSCMaturedAreDequeued() { for _, bundle := range s.consumerBundles { for i := 50; i < 100; i++ { ibcSeqNum := uint64(i) - packet := s.constructSlashPacketFromConsumer(*bundle, - *s.providerChain.Vals.Validators[0], stakingtypes.Infraction_INFRACTION_DOWNTIME, ibcSeqNum) - consumerPacketData, err := provider.UnmarshalConsumerPacket(packet) // Same func used by provider's OnRecvPacket + data := s.constructSlashPacketFromConsumer(*bundle, + *s.providerChain.Vals.Validators[0], stakingtypes.Infraction_INFRACTION_DOWNTIME) + packetData, err := provider.UnmarshalConsumerPacket(channeltypes.Packet{Data: data}) // Same func used by provider's OnRecvPacket s.Require().NoError(err) - providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, *consumerPacketData.GetSlashPacketData()) + packet := channeltypes.NewPacket(data, ibcSeqNum, + ccvtypes.ConsumerPortID, bundle.Path.EndpointA.ChannelID, + ccvtypes.ProviderPortID, bundle.Path.EndpointB.ChannelID, + timeoutHeight, timeoutTimestamp) + providerKeeper.OnRecvSlashPacket(s.providerCtx(), + packet, *packetData.GetSlashPacketData()) } } @@ -801,9 +868,13 @@ func (s *CCVTestSuite) TestLeadingVSCMaturedAreDequeued() { for _, bundle := range s.consumerBundles { for i := 100; i < 150; i++ { ibcSeqNum := uint64(i) - packet := s.constructVSCMaturedPacketFromConsumer(*bundle, ibcSeqNum) + data := s.constructVSCMaturedPacketFromConsumer(*bundle) packetData := ccvtypes.ConsumerPacketData{} - ccvtypes.ModuleCdc.MustUnmarshalJSON(packet.GetData(), &packetData) + ccvtypes.ModuleCdc.MustUnmarshalJSON(data, &packetData) + packet := channeltypes.NewPacket(data, ibcSeqNum, + ccvtypes.ConsumerPortID, bundle.Path.EndpointA.ChannelID, + ccvtypes.ProviderPortID, bundle.Path.EndpointB.ChannelID, + timeoutHeight, timeoutTimestamp) providerKeeper.OnRecvVSCMaturedPacket(s.providerCtx(), packet, *packetData.GetVscMaturedPacketData()) } @@ -864,12 +935,20 @@ func (s *CCVTestSuite) TestVscMaturedHandledPerBlockLimit() { providerKeeper.InitializeSlashMeter(s.providerCtx()) // Queue up 100 vsc matured packets for each consumer + var ( + timeoutHeight = clienttypes.Height{} + timeoutTimestamp = uint64(s.getFirstBundle().GetCtx().BlockTime().Add(ccvtypes.DefaultCCVTimeoutPeriod).UnixNano()) + ) for _, bundle := range s.consumerBundles { for i := 0; i < 100; i++ { ibcSeqNum := uint64(i) - packet := s.constructVSCMaturedPacketFromConsumer(*bundle, ibcSeqNum) + data := s.constructVSCMaturedPacketFromConsumer(*bundle) packetData := ccvtypes.ConsumerPacketData{} - ccvtypes.ModuleCdc.MustUnmarshalJSON(packet.GetData(), &packetData) + ccvtypes.ModuleCdc.MustUnmarshalJSON(data, &packetData) + packet := channeltypes.NewPacket(data, ibcSeqNum, + ccvtypes.ConsumerPortID, bundle.Path.EndpointA.ChannelID, + ccvtypes.ProviderPortID, bundle.Path.EndpointB.ChannelID, + timeoutHeight, timeoutTimestamp) providerKeeper.OnRecvVSCMaturedPacket(s.providerCtx(), packet, *packetData.GetVscMaturedPacketData()) } @@ -879,11 +958,15 @@ func (s *CCVTestSuite) TestVscMaturedHandledPerBlockLimit() { for _, bundle := range s.consumerBundles { for i := 100; i < 150; i++ { ibcSeqNum := uint64(i) - packet := s.constructSlashPacketFromConsumer(*bundle, - *s.providerChain.Vals.Validators[0], stakingtypes.Infraction_INFRACTION_DOWNTIME, ibcSeqNum) - consumerPacketData, err := provider.UnmarshalConsumerPacket(packet) // Same func used by provider's OnRecvPacket - s.Require().NoError(err) - providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, *consumerPacketData.GetSlashPacketData()) + data := s.constructSlashPacketFromConsumer(*bundle, + *s.providerChain.Vals.Validators[0], stakingtypes.Infraction_INFRACTION_DOWNTIME) + packetData := ccvtypes.ConsumerPacketData{} + ccvtypes.ModuleCdc.MustUnmarshalJSON(data, &packetData) + packet := channeltypes.NewPacket(data, ibcSeqNum, + ccvtypes.ConsumerPortID, bundle.Path.EndpointA.ChannelID, + ccvtypes.ProviderPortID, bundle.Path.EndpointB.ChannelID, + timeoutHeight, timeoutTimestamp) + providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, *packetData.GetSlashPacketData()) } } diff --git a/testutil/ibc_testing/generic_setup.go b/testutil/ibc_testing/generic_setup.go index 25c483fca5..e9d959b665 100644 --- a/testutil/ibc_testing/generic_setup.go +++ b/testutil/ibc_testing/generic_setup.go @@ -1,32 +1,47 @@ package ibc_testing import ( + "encoding/json" "fmt" "testing" clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types" + ibctesting "github.com/cosmos/ibc-go/v7/testing" "github.com/stretchr/testify/suite" sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cometbft/cometbft/abci/types" tmencoding "github.com/cometbft/cometbft/crypto/encoding" tmtypes "github.com/cometbft/cometbft/types" - ibctesting "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/testing" testutil "github.com/cosmos/interchain-security/v3/testutil/integration" testkeeper "github.com/cosmos/interchain-security/v3/testutil/keeper" consumerkeeper "github.com/cosmos/interchain-security/v3/x/ccv/consumer/keeper" ) +type ( + AppIniter func() (ibctesting.TestingApp, map[string]json.RawMessage) + ValSetAppIniter func([]types.ValidatorUpdate) AppIniter +) + // Contains generic setup code for running integration tests against a provider, consumer, // and/or democracy consumer app.go implementation. You should not need to modify or replicate this file // to run integration tests against your app.go implementations! var ( + FirstConsumerChainID string + provChainID string + democConsumerChainID string +) + +func init() { + // Disable revision format + ibctesting.ChainIDSuffix = "" FirstConsumerChainID = ibctesting.GetChainID(2) - provChainID = ibctesting.GetChainID(1) + provChainID = ibctesting.GetChainID(1) democConsumerChainID = ibctesting.GetChainID(5000) -) +} // ConsumerBundle serves as a way to store useful in-mem consumer app chain state // and relevant IBC paths in the context of CCV integration testing. @@ -48,11 +63,12 @@ func (cb ConsumerBundle) GetKeeper() consumerkeeper.Keeper { } // AddProvider adds a new provider chain to the coordinator and returns the test chain and app type -func AddProvider[T testutil.ProviderApp](t *testing.T, coordinator *ibctesting.Coordinator, appIniter ibctesting.AppIniter) ( +func AddProvider[T testutil.ProviderApp](t *testing.T, coordinator *ibctesting.Coordinator, appIniter AppIniter) ( *ibctesting.TestChain, T, ) { t.Helper() - provider := ibctesting.NewTestChain(t, coordinator, appIniter, provChainID) + ibctesting.DefaultTestingAppInit = appIniter + provider := ibctesting.NewTestChain(t, coordinator, provChainID) coordinator.Chains[provChainID] = provider providerToReturn, ok := provider.App.(T) @@ -64,11 +80,18 @@ func AddProvider[T testutil.ProviderApp](t *testing.T, coordinator *ibctesting.C } // AddDemocracyConsumer adds a new democ consumer chain to the coordinator and returns the test chain and app type -func AddDemocracyConsumer[T testutil.DemocConsumerApp](t *testing.T, coordinator *ibctesting.Coordinator, - appIniter ibctesting.AppIniter, +func AddDemocracyConsumer[T testutil.DemocConsumerApp]( + coordinator *ibctesting.Coordinator, + s *suite.Suite, + appIniter ValSetAppIniter, ) (*ibctesting.TestChain, T) { - t.Helper() - democConsumer := ibctesting.NewTestChain(t, coordinator, appIniter, democConsumerChainID) + s.T().Helper() + + // generate validators private/public key + valSet, valUpdates, signers := testutil.CreateValidators(s.T(), 4) + + ibctesting.DefaultTestingAppInit = appIniter(valUpdates) + democConsumer := ibctesting.NewTestChainWithValSet(s.T(), coordinator, democConsumerChainID, valSet, signers) coordinator.Chains[democConsumerChainID] = democConsumer democConsumerToReturn, ok := democConsumer.App.(T) @@ -89,7 +112,7 @@ func AddConsumer[Tp testutil.ProviderApp, Tc testutil.ConsumerApp]( coordinator *ibctesting.Coordinator, s *suite.Suite, index int, - appIniter ibctesting.AppIniter, + appIniter ValSetAppIniter, ) *ConsumerBundle { // consumer chain ID chainID := ibctesting.GetChainID(index + 2) @@ -135,8 +158,9 @@ func AddConsumer[Tp testutil.ProviderApp, Tc testutil.ConsumerApp]( } // create and instantiate consumer chain - testChain := ibctesting.NewTestChainWithValSet(s.T(), coordinator, - appIniter, chainID, tmtypes.NewValidatorSet(valz), providerChain.Signers) + ibctesting.DefaultTestingAppInit = appIniter(consumerGenesisState.InitialValSet) + testChain := ibctesting.NewTestChainWithValSet(s.T(), coordinator, chainID, + tmtypes.NewValidatorSet(valz), providerChain.Signers) coordinator.Chains[chainID] = testChain consumerToReturn, ok := testChain.App.(Tc) diff --git a/testutil/ibc_testing/specific_setup.go b/testutil/ibc_testing/specific_setup.go index 948007d212..577b878791 100644 --- a/testutil/ibc_testing/specific_setup.go +++ b/testutil/ibc_testing/specific_setup.go @@ -7,15 +7,26 @@ package ibc_testing import ( "encoding/json" + ibctesting "github.com/cosmos/ibc-go/v7/testing" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" + sdk "github.com/cosmos/cosmos-sdk/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" tmdb "github.com/cometbft/cometbft-db" + "github.com/cometbft/cometbft/abci/types" "github.com/cometbft/cometbft/libs/log" appConsumer "github.com/cosmos/interchain-security/v3/app/consumer" appConsumerDemocracy "github.com/cosmos/interchain-security/v3/app/consumer-democracy" appProvider "github.com/cosmos/interchain-security/v3/app/provider" - ibctesting "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/testing" + consumertypes "github.com/cosmos/interchain-security/v3/x/ccv/consumer/types" +) + +var ( + _ AppIniter = ProviderAppIniter + _ ValSetAppIniter = ConsumerAppIniter + _ ValSetAppIniter = DemocracyConsumerAppIniter ) // ProviderAppIniter implements ibctesting.AppIniter for a provider app @@ -25,16 +36,44 @@ func ProviderAppIniter() (ibctesting.TestingApp, map[string]json.RawMessage) { return testApp, appProvider.NewDefaultGenesisState(encoding.Codec) } -// ConsumerAppIniter implements ibctesting.AppIniter for a consumer app -func ConsumerAppIniter() (ibctesting.TestingApp, map[string]json.RawMessage) { - encoding := appConsumer.MakeTestEncodingConfig() - testApp := appConsumer.New(log.NewNopLogger(), tmdb.NewMemDB(), nil, true, simtestutil.EmptyAppOptions{}) - return testApp, appConsumer.NewDefaultGenesisState(encoding.Codec) +// ConsumerAppIniter returns a ibctesting.ValSetAppIniter for a consumer app +func ConsumerAppIniter(initValPowers []types.ValidatorUpdate) AppIniter { + return func() (ibctesting.TestingApp, map[string]json.RawMessage) { + encoding := appConsumer.MakeTestEncodingConfig() + testApp := appConsumer.New(log.NewNopLogger(), tmdb.NewMemDB(), nil, true, simtestutil.EmptyAppOptions{}) + genesisState := appConsumer.NewDefaultGenesisState(encoding.Codec) + // NOTE ibc-go/v7/testing.SetupWithGenesisValSet requires a staking module + // genesisState or it panics. Feed a minimum one. + genesisState[stakingtypes.ModuleName] = encoding.Codec.MustMarshalJSON( + &stakingtypes.GenesisState{ + Params: stakingtypes.Params{BondDenom: sdk.DefaultBondDenom}, + }, + ) + // Feed consumer genesis with provider validators + var consumerGenesis consumertypes.GenesisState + encoding.Codec.MustUnmarshalJSON(genesisState[consumertypes.ModuleName], &consumerGenesis) + consumerGenesis.InitialValSet = initValPowers + consumerGenesis.Params.Enabled = true + genesisState[consumertypes.ModuleName] = encoding.Codec.MustMarshalJSON(&consumerGenesis) + + return testApp, genesisState + } } -// DemocracyConsumerAppIniter implements ibctesting.AppIniter for a democracy consumer app -func DemocracyConsumerAppIniter() (ibctesting.TestingApp, map[string]json.RawMessage) { - encoding := appConsumerDemocracy.MakeTestEncodingConfig() - testApp := appConsumerDemocracy.New(log.NewNopLogger(), tmdb.NewMemDB(), nil, true, simtestutil.EmptyAppOptions{}) - return testApp, appConsumerDemocracy.NewDefaultGenesisState(encoding.Codec) +// DemocracyConsumerAppIniter implements ibctesting.ValSetAppIniter for a democracy consumer app +func DemocracyConsumerAppIniter(initValPowers []types.ValidatorUpdate) AppIniter { + return func() (ibctesting.TestingApp, map[string]json.RawMessage) { + encoding := appConsumerDemocracy.MakeTestEncodingConfig() + testApp := appConsumerDemocracy.New(log.NewNopLogger(), tmdb.NewMemDB(), nil, true, simtestutil.EmptyAppOptions{}) + genesisState := appConsumerDemocracy.NewDefaultGenesisState(encoding.Codec) + // Feed consumer genesis with provider validators + // TODO See if useful for democracy + var consumerGenesis consumertypes.GenesisState + encoding.Codec.MustUnmarshalJSON(genesisState[consumertypes.ModuleName], &consumerGenesis) + consumerGenesis.InitialValSet = initValPowers + consumerGenesis.Params.Enabled = true + genesisState[consumertypes.ModuleName] = encoding.Codec.MustMarshalJSON(&consumerGenesis) + + return testApp, genesisState + } } diff --git a/testutil/integration/interfaces.go b/testutil/integration/interfaces.go index b677d46789..cedd840726 100644 --- a/testutil/integration/interfaces.go +++ b/testutil/integration/interfaces.go @@ -3,6 +3,9 @@ package integration import ( "time" + ibctesting "github.com/cosmos/ibc-go/v7/testing" + + "cosmossdk.io/api/tendermint/abci" "cosmossdk.io/math" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" @@ -16,9 +19,6 @@ import ( slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" "github.com/cosmos/cosmos-sdk/x/staking/types" - abci "github.com/cometbft/cometbft/abci/types" - - ibctesting "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/testing" consumerkeeper "github.com/cosmos/interchain-security/v3/x/ccv/consumer/keeper" providerkeeper "github.com/cosmos/interchain-security/v3/x/ccv/provider/keeper" ccvtypes "github.com/cosmos/interchain-security/v3/x/ccv/types" diff --git a/testutil/integration/validators.go b/testutil/integration/validators.go new file mode 100644 index 0000000000..9859bb55f1 --- /dev/null +++ b/testutil/integration/validators.go @@ -0,0 +1,46 @@ +package integration + +import ( + "testing" + + "github.com/cometbft/cometbft/abci/types" + tmencoding "github.com/cometbft/cometbft/crypto/encoding" + tmtypes "github.com/cometbft/cometbft/types" + "github.com/cosmos/cosmos-sdk/testutil/mock" + "github.com/stretchr/testify/require" +) + +func CreateValidators(t *testing.T, n int) ( + *tmtypes.ValidatorSet, []types.ValidatorUpdate, map[string]tmtypes.PrivValidator, +) { + // generate validators private/public key + var ( + validators []*tmtypes.Validator + signersByAddress = make(map[string]tmtypes.PrivValidator, n) + ) + for i := 0; i < n; i++ { + privVal := mock.NewPV() + pubKey, err := privVal.GetPubKey() + require.NoError(t, err) + val := tmtypes.NewValidator(pubKey, 1) + validators = append(validators, val) + signersByAddress[pubKey.Address().String()] = privVal + } + // construct validator set; + // Note that the validators are sorted by voting power + // or, if equal, by address lexical order + valSet := tmtypes.NewValidatorSet(validators) + return valSet, ToValidatorUpdates(t, valSet), signersByAddress +} + +func ToValidatorUpdates(t *testing.T, valSet *tmtypes.ValidatorSet) (valUpdates []types.ValidatorUpdate) { + for _, val := range valSet.Validators { + protoPubKey, err := tmencoding.PubKeyToProto(val.PubKey) + require.NoError(t, err) + valUpdates = append(valUpdates, types.ValidatorUpdate{ + PubKey: protoPubKey, + Power: val.VotingPower, + }) + } + return +} diff --git a/testutil/simibc/chain_util.go b/testutil/simibc/chain_util.go index 98622695d3..7f01d6e694 100644 --- a/testutil/simibc/chain_util.go +++ b/testutil/simibc/chain_util.go @@ -3,14 +3,13 @@ package simibc import ( "time" - channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" - ibctmtypes "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint" - abci "github.com/cometbft/cometbft/abci/types" tmproto "github.com/cometbft/cometbft/proto/tendermint/types" + sdk "github.com/cosmos/cosmos-sdk/types" - ibctestingcore "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/core" - ibctesting "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/testing" + channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" + ibctmtypes "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint" + ibctesting "github.com/cosmos/ibc-go/v7/testing" ) // BeginBlock updates the current header and calls the app.BeginBlock method. @@ -56,14 +55,37 @@ func EndBlock(c *ibctesting.TestChain, preCommitCallback func()) (*ibctmtypes.He c.LastHeader = c.CurrentTMClientHeader() - packets := []channeltypes.Packet{} + sdkEvts := ABCIToSDKEvents(ebRes.Events) + packets := ParsePacketsFromEvents(sdkEvts) + + return c.LastHeader, packets +} - for _, e := range ebRes.Events { - if e.Type == channeltypes.EventTypeSendPacket { - packet, _ := ibctestingcore.ReconstructPacketFromEvent(e) +// ParsePacketsFromEvents returns all packets found in events. +func ParsePacketsFromEvents(events []sdk.Event) (packets []channeltypes.Packet) { + for i, ev := range events { + if ev.Type == channeltypes.EventTypeSendPacket { + packet, err := ibctesting.ParsePacketFromEvents(events[i:]) + if err != nil { + panic(err) + } packets = append(packets, packet) } } + return +} - return c.LastHeader, packets +// ABCIToSDKEvents converts a list of ABCI events to Cosmos SDK events. +func ABCIToSDKEvents(abciEvents []abci.Event) sdk.Events { + var events sdk.Events + for _, evt := range abciEvents { + var attributes []sdk.Attribute + for _, attr := range evt.GetAttributes() { + attributes = append(attributes, sdk.NewAttribute(attr.Key, attr.Value)) + } + + events = events.AppendEvent(sdk.NewEvent(evt.GetType(), attributes...)) + } + + return events } diff --git a/testutil/simibc/relay_util.go b/testutil/simibc/relay_util.go index 97bcabf57c..9fd5213d5b 100644 --- a/testutil/simibc/relay_util.go +++ b/testutil/simibc/relay_util.go @@ -5,6 +5,8 @@ import ( channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" host "github.com/cosmos/ibc-go/v7/modules/core/24-host" ibctmtypes "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint" + ibctesting "github.com/cosmos/ibc-go/v7/testing" + simapp "github.com/cosmos/ibc-go/v7/testing/simapp" "github.com/stretchr/testify/require" errorsmod "cosmossdk.io/errors" @@ -12,9 +14,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" tmtypes "github.com/cometbft/cometbft/types" - - simapp "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/simapp" - ibctesting "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/testing" ) // UpdateReceiverClient DELIVERs a header to the receiving endpoint diff --git a/testutil/simibc/relayed_path.go b/testutil/simibc/relayed_path.go index 7ef97c4bee..daf32f0c84 100644 --- a/testutil/simibc/relayed_path.go +++ b/testutil/simibc/relayed_path.go @@ -5,8 +5,7 @@ import ( "time" ibctmtypes "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint" - - ibctesting "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/testing" + ibctesting "github.com/cosmos/ibc-go/v7/testing" ) // RelayedPath is a wrapper around ibctesting.Path gives fine-grained diff --git a/x/ccv/provider/keeper/keeper_test.go b/x/ccv/provider/keeper/keeper_test.go index 364aac5669..1caa90b0c8 100644 --- a/x/ccv/provider/keeper/keeper_test.go +++ b/x/ccv/provider/keeper/keeper_test.go @@ -9,11 +9,11 @@ import ( "github.com/stretchr/testify/require" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" + ibctesting "github.com/cosmos/ibc-go/v7/testing" abci "github.com/cometbft/cometbft/abci/types" tmprotocrypto "github.com/cometbft/cometbft/proto/tendermint/crypto" - ibcsimapp "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/simapp" cryptotestutil "github.com/cosmos/interchain-security/v3/testutil/crypto" testkeeper "github.com/cosmos/interchain-security/v3/testutil/keeper" "github.com/cosmos/interchain-security/v3/x/ccv/provider/types" @@ -156,7 +156,7 @@ func TestPendingVSCs(t *testing.T) { pending := providerKeeper.GetPendingVSCPackets(ctx, chainID) require.Len(t, pending, 0) - pks := ibcsimapp.CreateTestPubKeys(4) + _, pks, _ := ibctesting.GenerateKeys(t, 4) var ppks [4]tmprotocrypto.PublicKey for i, pk := range pks { ppks[i], _ = cryptocodec.ToTmProtoPublicKey(pk) diff --git a/x/ccv/provider/keeper/relay_test.go b/x/ccv/provider/keeper/relay_test.go index 590286261c..d3fdcaa21e 100644 --- a/x/ccv/provider/keeper/relay_test.go +++ b/x/ccv/provider/keeper/relay_test.go @@ -8,6 +8,7 @@ import ( clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types" channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" exported "github.com/cosmos/ibc-go/v7/modules/core/exported" + ibctesting "github.com/cosmos/ibc-go/v7/testing" "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" @@ -17,7 +18,6 @@ import ( abci "github.com/cometbft/cometbft/abci/types" - ibcsimapp "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/simapp" cryptotestutil "github.com/cosmos/interchain-security/v3/testutil/crypto" testkeeper "github.com/cosmos/interchain-security/v3/testutil/keeper" "github.com/cosmos/interchain-security/v3/x/ccv/provider/keeper" @@ -27,7 +27,7 @@ import ( // TestQueueVSCPackets tests queueing validator set updates. func TestQueueVSCPackets(t *testing.T) { - key := ibcsimapp.CreateTestPubKeys(1)[0] + _, _, key := ibctesting.GenerateKeys(t, 1) tmPubKey, _ := cryptocodec.ToTmProtoPublicKey(key) testCases := []struct { diff --git a/x/ccv/types/utils_test.go b/x/ccv/types/utils_test.go index f43e5449f1..88c1b96335 100644 --- a/x/ccv/types/utils_test.go +++ b/x/ccv/types/utils_test.go @@ -3,18 +3,18 @@ package types_test import ( "testing" + ibctesting "github.com/cosmos/ibc-go/v7/testing" "github.com/stretchr/testify/require" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" abci "github.com/cometbft/cometbft/abci/types" - ibcsimapp "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/simapp" "github.com/cosmos/interchain-security/v3/x/ccv/types" ) func TestAccumulateChanges(t *testing.T) { - testKeys := ibcsimapp.CreateTestPubKeys(2) + _, testKeys, _ := ibctesting.GenerateKeys(t, 2) tmPubKey, _ := cryptocodec.ToTmProtoPublicKey(testKeys[0]) tmPubKey2, _ := cryptocodec.ToTmProtoPublicKey(testKeys[1]) From 52743c8ff88c70b664c70111fa7f4b8d9e03b40f Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Thu, 3 Aug 2023 19:47:25 +0200 Subject: [PATCH 02/12] fix import --- app/sovereign/app.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/sovereign/app.go b/app/sovereign/app.go index 1693cb1b60..c6ffb0e853 100644 --- a/app/sovereign/app.go +++ b/app/sovereign/app.go @@ -16,6 +16,8 @@ import ( ibchost "github.com/cosmos/ibc-go/v7/modules/core/exported" ibckeeper "github.com/cosmos/ibc-go/v7/modules/core/keeper" tendermint "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint" + ibctesting "github.com/cosmos/ibc-go/v7/testing" + ibctestingtypes "github.com/cosmos/ibc-go/v7/testing/types" "github.com/spf13/cast" autocliv1 "cosmossdk.io/api/cosmos/autocli/v1" @@ -77,6 +79,7 @@ import ( govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" + // add mint mint "github.com/cosmos/cosmos-sdk/x/mint" mintkeeper "github.com/cosmos/cosmos-sdk/x/mint/keeper" @@ -104,8 +107,6 @@ import ( tmos "github.com/cometbft/cometbft/libs/os" appparams "github.com/cosmos/interchain-security/v3/app/params" - ibctestingcore "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/core" - ibctesting "github.com/cosmos/interchain-security/v3/legacy_ibc_testing/testing" testutil "github.com/cosmos/interchain-security/v3/testutil/integration" ) @@ -770,7 +771,7 @@ func (app *App) GetBaseApp() *baseapp.BaseApp { } // GetStakingKeeper implements the TestingApp interface. -func (app *App) GetStakingKeeper() ibctestingcore.StakingKeeper { +func (app *App) GetStakingKeeper() ibctestingtypes.StakingKeeper { return app.StakingKeeper } From 4dc237ba37afaef6f801eab0abd6490bb65cbf3e Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Fri, 4 Aug 2023 10:55:32 +0200 Subject: [PATCH 03/12] introduce provider.UnmarshalConsumerPacketData --- tests/integration/throttle.go | 28 ++++++++++++---------------- x/ccv/provider/ibc_module.go | 8 ++++++-- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/integration/throttle.go b/tests/integration/throttle.go index 2963683c87..48b26cec55 100644 --- a/tests/integration/throttle.go +++ b/tests/integration/throttle.go @@ -329,7 +329,7 @@ func (s *CCVTestSuite) TestPacketSpam() { timeoutTimestamp = uint64(firstBundle.GetCtx().BlockTime().Add(ccvtypes.DefaultCCVTimeoutPeriod).UnixNano()) ) for sequence, data := range packetsData { - consumerPacketData, err := provider.UnmarshalConsumerPacket(channeltypes.Packet{Data: data}) // Same func used by provider's OnRecvPacket + consumerPacketData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket s.Require().NoError(err) packet := channeltypes.NewPacket(data, uint64(sequence), firstBundle.Path.EndpointA.ChannelConfig.PortID, firstBundle.Path.EndpointA.ChannelID, @@ -391,7 +391,7 @@ func (s *CCVTestSuite) TestDoubleSignDoesNotAffectThrottling() { timeoutTimestamp = uint64(firstBundle.GetCtx().BlockTime().Add(ccvtypes.DefaultCCVTimeoutPeriod).UnixNano()) ) for sequence, data := range packetsData { - consumerPacketData, err := provider.UnmarshalConsumerPacket(channeltypes.Packet{Data: data}) // Same func used by provider's OnRecvPacket + consumerPacketData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket s.Require().NoError(err) packet := channeltypes.NewPacket(data, uint64(sequence), firstBundle.Path.EndpointA.ChannelConfig.PortID, firstBundle.Path.EndpointA.ChannelID, @@ -499,7 +499,7 @@ func (s *CCVTestSuite) TestQueueOrdering() { timeoutTimestamp = uint64(firstBundle.GetCtx().BlockTime().Add(ccvtypes.DefaultCCVTimeoutPeriod).UnixNano()) ) for i, data := range packetsData { - consumerPacketData, err := provider.UnmarshalConsumerPacket(channeltypes.Packet{Data: data}) // Same func used by provider's OnRecvPacket + consumerPacketData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket s.Require().NoError(err) packet := channeltypes.NewPacket(data, ibcSeqs[i], firstBundle.Path.EndpointA.ChannelConfig.PortID, firstBundle.Path.EndpointA.ChannelID, @@ -725,7 +725,7 @@ func (s *CCVTestSuite) TestSlashSameValidator() { timeoutTimestamp = uint64(s.getFirstBundle().GetCtx().BlockTime().Add(ccvtypes.DefaultCCVTimeoutPeriod).UnixNano()) ) for i, data := range packetsData { - consumerPacketData, err := provider.UnmarshalConsumerPacket(channeltypes.Packet{Data: data}) // Same func used by provider's OnRecvPacket + consumerPacketData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket s.Require().NoError(err) packet := channeltypes.NewPacket(data, uint64(i), s.getFirstBundle().Path.EndpointA.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointA.ChannelID, @@ -767,16 +767,12 @@ func (s CCVTestSuite) TestSlashAllValidators() { //nolint:govet // this is a tes // The packets to be recv in a single block, ordered as they will be recv. var packetsData [][]byte - // Track and increment ibc seq num for each packet, since these need to be unique. - ibcSeqNum := uint64(1) - // Instantiate a slash packet for each validator, // these first 4 packets should jail 100% of the total power. for _, val := range s.providerChain.Vals.Validators { s.setDefaultValSigningInfo(*val) packetsData = append(packetsData, s.constructSlashPacketFromConsumer( s.getFirstBundle(), *val, stakingtypes.Infraction_INFRACTION_DOWNTIME)) - ibcSeqNum++ } // add 5 more slash packets for each validator, that will be handled in the same block. @@ -784,7 +780,6 @@ func (s CCVTestSuite) TestSlashAllValidators() { //nolint:govet // this is a tes for i := 0; i < 5; i++ { packetsData = append(packetsData, s.constructSlashPacketFromConsumer( s.getFirstBundle(), *val, stakingtypes.Infraction_INFRACTION_DOWNTIME)) - ibcSeqNum++ } } @@ -794,9 +789,10 @@ func (s CCVTestSuite) TestSlashAllValidators() { //nolint:govet // this is a tes timeoutTimestamp = uint64(s.getFirstBundle().GetCtx().BlockTime().Add(ccvtypes.DefaultCCVTimeoutPeriod).UnixNano()) ) for i, data := range packetsData { - consumerPacketData, err := provider.UnmarshalConsumerPacket(channeltypes.Packet{Data: data}) // Same func used by provider's OnRecvPacket + ibcSeqNum := uint64(i) + consumerPacketData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket s.Require().NoError(err) - packet := channeltypes.NewPacket(data, uint64(i), + packet := channeltypes.NewPacket(data, ibcSeqNum, s.getFirstBundle().Path.EndpointA.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointA.ChannelID, s.getFirstBundle().Path.EndpointB.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointB.ChannelID, timeoutHeight, timeoutTimestamp) @@ -836,7 +832,7 @@ func (s *CCVTestSuite) TestLeadingVSCMaturedAreDequeued() { for i := 0; i < 50; i++ { ibcSeqNum := uint64(i) data := s.constructVSCMaturedPacketFromConsumer(*bundle) - packetData, err := provider.UnmarshalConsumerPacket(channeltypes.Packet{Data: data}) // Same func used by provider's OnRecvPacket + packetData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket s.Require().NoError(err) packet := channeltypes.NewPacket(data, ibcSeqNum, ccvtypes.ConsumerPortID, bundle.Path.EndpointA.ChannelID, @@ -853,7 +849,7 @@ func (s *CCVTestSuite) TestLeadingVSCMaturedAreDequeued() { ibcSeqNum := uint64(i) data := s.constructSlashPacketFromConsumer(*bundle, *s.providerChain.Vals.Validators[0], stakingtypes.Infraction_INFRACTION_DOWNTIME) - packetData, err := provider.UnmarshalConsumerPacket(channeltypes.Packet{Data: data}) // Same func used by provider's OnRecvPacket + packetData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket s.Require().NoError(err) packet := channeltypes.NewPacket(data, ibcSeqNum, ccvtypes.ConsumerPortID, bundle.Path.EndpointA.ChannelID, @@ -960,13 +956,13 @@ func (s *CCVTestSuite) TestVscMaturedHandledPerBlockLimit() { ibcSeqNum := uint64(i) data := s.constructSlashPacketFromConsumer(*bundle, *s.providerChain.Vals.Validators[0], stakingtypes.Infraction_INFRACTION_DOWNTIME) - packetData := ccvtypes.ConsumerPacketData{} - ccvtypes.ModuleCdc.MustUnmarshalJSON(data, &packetData) + consumderPacketData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket + s.Require().NoError(err) packet := channeltypes.NewPacket(data, ibcSeqNum, ccvtypes.ConsumerPortID, bundle.Path.EndpointA.ChannelID, ccvtypes.ProviderPortID, bundle.Path.EndpointB.ChannelID, timeoutHeight, timeoutTimestamp) - providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, *packetData.GetSlashPacketData()) + providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, *consumderPacketData.GetSlashPacketData()) } } diff --git a/x/ccv/provider/ibc_module.go b/x/ccv/provider/ibc_module.go index a19fd7e5bb..75da9588d6 100644 --- a/x/ccv/provider/ibc_module.go +++ b/x/ccv/provider/ibc_module.go @@ -209,11 +209,15 @@ func (am AppModule) OnRecvPacket( } func UnmarshalConsumerPacket(packet channeltypes.Packet) (consumerPacket ccv.ConsumerPacketData, err error) { + return UnmarshalConsumerPacketData(packet.GetData()) +} + +func UnmarshalConsumerPacketData(packetData []byte) (consumerPacket ccv.ConsumerPacketData, err error) { // First try unmarshaling into ccv.ConsumerPacketData type - if err := ccv.ModuleCdc.UnmarshalJSON(packet.GetData(), &consumerPacket); err != nil { + if err := ccv.ModuleCdc.UnmarshalJSON(packetData, &consumerPacket); err != nil { // If failed, packet should be a v1 slash packet, retry for ConsumerPacketDataV1 packet type var v1Packet ccv.ConsumerPacketDataV1 - errV1 := ccv.ModuleCdc.UnmarshalJSON(packet.GetData(), &v1Packet) + errV1 := ccv.ModuleCdc.UnmarshalJSON(packetData, &v1Packet) if errV1 != nil { // If neither worked, return error return ccv.ConsumerPacketData{}, errV1 From 8f9896618fb39dd37ce14786a266841e2a5d66a7 Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Fri, 4 Aug 2023 12:36:54 +0200 Subject: [PATCH 04/12] use patched ibc-go --- go.mod | 2 ++ go.sum | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index 6cc988a794..9bb0b8193a 100644 --- a/go.mod +++ b/go.mod @@ -188,3 +188,5 @@ require ( // following versions might cause unexpected behavior replace github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 + +replace github.com/cosmos/ibc-go/v7 => github.com/allinbits/ibc-go/v7 v7.0.0-20230804095822-27dc704b7dec diff --git a/go.sum b/go.sum index 03e76f9812..019bd1a958 100644 --- a/go.sum +++ b/go.sum @@ -248,6 +248,8 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM= +github.com/allinbits/ibc-go/v7 v7.0.0-20230804095822-27dc704b7dec h1:/xskaNv9DTdO0TrJJFcEENKP6uX3F0c+edM8OwM5eqM= +github.com/allinbits/ibc-go/v7 v7.0.0-20230804095822-27dc704b7dec/go.mod h1:7MptlWeIyqmDiuJeRAFqBvXKY8Hybd+rF8vMSmGd2zg= github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/apache/arrow/go/arrow v0.0.0-20191024131854-af6fa24be0db/go.mod h1:VTxUBvSJ3s3eHAg65PNgrsn5BtqCRPdmyXh6rAfdxN0= @@ -396,8 +398,6 @@ github.com/cosmos/gogoproto v1.4.10 h1:QH/yT8X+c0F4ZDacDv3z+xE3WU1P1Z3wQoLMBRJoK github.com/cosmos/gogoproto v1.4.10/go.mod h1:3aAZzeRWpAwr+SS/LLkICX2/kDFyaYVzckBDzygIxek= github.com/cosmos/iavl v0.20.0 h1:fTVznVlepH0KK8NyKq8w+U7c2L6jofa27aFX6YGlm38= github.com/cosmos/iavl v0.20.0/go.mod h1:WO7FyvaZJoH65+HFOsDir7xU9FWk2w9cHXNW1XHcl7A= -github.com/cosmos/ibc-go/v7 v7.3.0 h1:QtGeVMi/3JeLWuvEuC60sBHpAF40Oenx/y+bP8+wRRw= -github.com/cosmos/ibc-go/v7 v7.3.0/go.mod h1:mUmaHFXpXrEdcxfdXyau+utZf14pGKVUiXwYftRZZfQ= github.com/cosmos/ics23/go v0.10.0 h1:iXqLLgp2Lp+EdpIuwXTYIQU+AiHj9mOC2X9ab++bZDM= github.com/cosmos/ics23/go v0.10.0/go.mod h1:ZfJSmng/TBNTBkFemHHHj5YY7VAU/MBU980F4VU1NG0= github.com/cosmos/ledger-cosmos-go v0.12.2 h1:/XYaBlE2BJxtvpkHiBm97gFGSGmYGKunKyF3nNqAXZA= From bad42ac4de1bae9a4ce97143683d34395692f8a9 Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Fri, 4 Aug 2023 15:09:15 +0200 Subject: [PATCH 05/12] fix annoying import order --- app/consumer-democracy/app.go | 1 - app/sovereign/app.go | 1 - tests/difference/core/driver/core_test.go | 2 +- tests/difference/core/driver/setup.go | 23 ++++++++++++----------- tests/integration/democracy.go | 6 ++++-- x/ccv/provider/keeper/keeper_test.go | 2 +- 6 files changed, 18 insertions(+), 17 deletions(-) diff --git a/app/consumer-democracy/app.go b/app/consumer-democracy/app.go index 94d7372521..1a35a89a8a 100644 --- a/app/consumer-democracy/app.go +++ b/app/consumer-democracy/app.go @@ -79,7 +79,6 @@ import ( govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" - // add mint mint "github.com/cosmos/cosmos-sdk/x/mint" mintkeeper "github.com/cosmos/cosmos-sdk/x/mint/keeper" diff --git a/app/sovereign/app.go b/app/sovereign/app.go index c6ffb0e853..b5629bfc22 100644 --- a/app/sovereign/app.go +++ b/app/sovereign/app.go @@ -79,7 +79,6 @@ import ( govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1" - // add mint mint "github.com/cosmos/cosmos-sdk/x/mint" mintkeeper "github.com/cosmos/cosmos-sdk/x/mint/keeper" diff --git a/tests/difference/core/driver/core_test.go b/tests/difference/core/driver/core_test.go index b9eaa52b9c..2d42e65dba 100644 --- a/tests/difference/core/driver/core_test.go +++ b/tests/difference/core/driver/core_test.go @@ -4,10 +4,10 @@ import ( "fmt" "time" - sdk "github.com/cosmos/cosmos-sdk/types" ibctesting "github.com/cosmos/ibc-go/v7/testing" "github.com/stretchr/testify/suite" + sdk "github.com/cosmos/cosmos-sdk/types" slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper" stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" diff --git a/tests/difference/core/driver/setup.go b/tests/difference/core/driver/setup.go index 832e1d3e44..2a4b17eddf 100644 --- a/tests/difference/core/driver/setup.go +++ b/tests/difference/core/driver/setup.go @@ -6,9 +6,15 @@ import ( "encoding/json" "time" - abci "github.com/cometbft/cometbft/abci/types" - tmproto "github.com/cometbft/cometbft/proto/tendermint/types" - tmtypes "github.com/cometbft/cometbft/types" + clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types" + channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" + commitmenttypes "github.com/cosmos/ibc-go/v7/modules/core/23-commitment/types" + ibctmtypes "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint" + ibctesting "github.com/cosmos/ibc-go/v7/testing" + "github.com/cosmos/ibc-go/v7/testing/mock" + "github.com/stretchr/testify/require" + "github.com/stretchr/testify/suite" + "github.com/cosmos/cosmos-sdk/baseapp" codectypes "github.com/cosmos/cosmos-sdk/codec/types" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" @@ -21,15 +27,10 @@ import ( slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" - "github.com/stretchr/testify/require" - "github.com/stretchr/testify/suite" - clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types" - channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" - commitmenttypes "github.com/cosmos/ibc-go/v7/modules/core/23-commitment/types" - ibctmtypes "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint" - ibctesting "github.com/cosmos/ibc-go/v7/testing" - "github.com/cosmos/ibc-go/v7/testing/mock" + abci "github.com/cometbft/cometbft/abci/types" + tmproto "github.com/cometbft/cometbft/proto/tendermint/types" + tmtypes "github.com/cometbft/cometbft/types" appConsumer "github.com/cosmos/interchain-security/v3/app/consumer" appProvider "github.com/cosmos/interchain-security/v3/app/provider" diff --git a/tests/integration/democracy.go b/tests/integration/democracy.go index f90f40bcbd..56cfcea02c 100644 --- a/tests/integration/democracy.go +++ b/tests/integration/democracy.go @@ -3,14 +3,16 @@ package integration import ( "time" + ibctesting "github.com/cosmos/ibc-go/v7/testing" + "github.com/stretchr/testify/suite" + "cosmossdk.io/math" + sdk "github.com/cosmos/cosmos-sdk/types" authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" govtypes "github.com/cosmos/cosmos-sdk/x/gov/types" govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1" minttypes "github.com/cosmos/cosmos-sdk/x/mint/types" - ibctesting "github.com/cosmos/ibc-go/v7/testing" - "github.com/stretchr/testify/suite" icstestingutils "github.com/cosmos/interchain-security/v3/testutil/ibc_testing" testutil "github.com/cosmos/interchain-security/v3/testutil/integration" diff --git a/x/ccv/provider/keeper/keeper_test.go b/x/ccv/provider/keeper/keeper_test.go index 1caa90b0c8..be3ef4001c 100644 --- a/x/ccv/provider/keeper/keeper_test.go +++ b/x/ccv/provider/keeper/keeper_test.go @@ -6,10 +6,10 @@ import ( "testing" "time" + ibctesting "github.com/cosmos/ibc-go/v7/testing" "github.com/stretchr/testify/require" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" - ibctesting "github.com/cosmos/ibc-go/v7/testing" abci "github.com/cometbft/cometbft/abci/types" tmprotocrypto "github.com/cometbft/cometbft/proto/tendermint/crypto" From 68749ac230602cb8d19f090f8097db4aa9d5e120 Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Fri, 4 Aug 2023 15:37:17 +0200 Subject: [PATCH 06/12] test: ignore key ordering Seems like ibctesting.GenerateKeys can returns keys in different orders when test cover is enabled. --- x/ccv/types/utils_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/ccv/types/utils_test.go b/x/ccv/types/utils_test.go index 88c1b96335..18ed01f7dc 100644 --- a/x/ccv/types/utils_test.go +++ b/x/ccv/types/utils_test.go @@ -84,7 +84,7 @@ func TestAccumulateChanges(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { changes := types.AccumulateChanges(tc.changes1, tc.changes2) - require.Equal(t, tc.expected, changes) + require.ElementsMatch(t, tc.expected, changes) }) } } From 5352ca82943c90b90e66d99e34eb07b40fcd2a5e Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Wed, 30 Aug 2023 14:13:03 +0200 Subject: [PATCH 07/12] fix after rebase --- tests/integration/throttle_retry.go | 18 +++++++++++++++--- testutil/ibc_testing/specific_setup.go | 5 +++-- testutil/integration/interfaces.go | 3 ++- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/tests/integration/throttle_retry.go b/tests/integration/throttle_retry.go index ae15aac977..b907800bb5 100644 --- a/tests/integration/throttle_retry.go +++ b/tests/integration/throttle_retry.go @@ -1,6 +1,7 @@ package integration import ( + clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types" channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" @@ -52,8 +53,15 @@ func (s *CCVTestSuite) TestSlashRetries() { // Construct a mock slash packet from consumer tmval1 := s.providerChain.Vals.Validators[1] - packet1 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval1, stakingtypes.Infraction_INFRACTION_DOWNTIME, 1) - + packetData1 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval1, stakingtypes.Infraction_INFRACTION_DOWNTIME) + var ( + timeoutHeight = clienttypes.Height{} + timeoutTimestamp = uint64(s.getFirstBundle().GetCtx().BlockTime().Add(ccvtypes.DefaultCCVTimeoutPeriod).UnixNano()) + ) + packet1 := channeltypes.NewPacket(packetData1, 1, + s.getFirstBundle().Path.EndpointA.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointA.ChannelID, + s.getFirstBundle().Path.EndpointB.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointB.ChannelID, + timeoutHeight, timeoutTimestamp) // Mock the sending of the packet on consumer consumerKeeper.AppendPendingPacket(s.consumerCtx(), ccvtypes.SlashPacket, &ccvtypes.ConsumerPacketData_SlashPacketData{ @@ -105,7 +113,11 @@ func (s *CCVTestSuite) TestSlashRetries() { // Construct and mock the sending of a second packet on consumer tmval2 := s.providerChain.Vals.Validators[2] - packet2 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval2, stakingtypes.Infraction_INFRACTION_DOWNTIME, 1) + packetData2 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval2, stakingtypes.Infraction_INFRACTION_DOWNTIME) + packet2 := channeltypes.NewPacket(packetData2, 1, + s.getFirstBundle().Path.EndpointA.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointA.ChannelID, + s.getFirstBundle().Path.EndpointB.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointB.ChannelID, + timeoutHeight, timeoutTimestamp) consumerKeeper.AppendPendingPacket(s.consumerCtx(), ccvtypes.SlashPacket, &ccvtypes.ConsumerPacketData_SlashPacketData{ diff --git a/testutil/ibc_testing/specific_setup.go b/testutil/ibc_testing/specific_setup.go index 577b878791..8ac559d740 100644 --- a/testutil/ibc_testing/specific_setup.go +++ b/testutil/ibc_testing/specific_setup.go @@ -21,6 +21,7 @@ import ( appConsumerDemocracy "github.com/cosmos/interchain-security/v3/app/consumer-democracy" appProvider "github.com/cosmos/interchain-security/v3/app/provider" consumertypes "github.com/cosmos/interchain-security/v3/x/ccv/consumer/types" + ccvtypes "github.com/cosmos/interchain-security/v3/x/ccv/types" ) var ( @@ -50,7 +51,7 @@ func ConsumerAppIniter(initValPowers []types.ValidatorUpdate) AppIniter { }, ) // Feed consumer genesis with provider validators - var consumerGenesis consumertypes.GenesisState + var consumerGenesis ccvtypes.GenesisState encoding.Codec.MustUnmarshalJSON(genesisState[consumertypes.ModuleName], &consumerGenesis) consumerGenesis.InitialValSet = initValPowers consumerGenesis.Params.Enabled = true @@ -68,7 +69,7 @@ func DemocracyConsumerAppIniter(initValPowers []types.ValidatorUpdate) AppIniter genesisState := appConsumerDemocracy.NewDefaultGenesisState(encoding.Codec) // Feed consumer genesis with provider validators // TODO See if useful for democracy - var consumerGenesis consumertypes.GenesisState + var consumerGenesis ccvtypes.GenesisState encoding.Codec.MustUnmarshalJSON(genesisState[consumertypes.ModuleName], &consumerGenesis) consumerGenesis.InitialValSet = initValPowers consumerGenesis.Params.Enabled = true diff --git a/testutil/integration/interfaces.go b/testutil/integration/interfaces.go index cedd840726..f084129ec0 100644 --- a/testutil/integration/interfaces.go +++ b/testutil/integration/interfaces.go @@ -5,7 +5,6 @@ import ( ibctesting "github.com/cosmos/ibc-go/v7/testing" - "cosmossdk.io/api/tendermint/abci" "cosmossdk.io/math" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" @@ -19,6 +18,8 @@ import ( slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types" "github.com/cosmos/cosmos-sdk/x/staking/types" + abci "github.com/cometbft/cometbft/abci/types" + consumerkeeper "github.com/cosmos/interchain-security/v3/x/ccv/consumer/keeper" providerkeeper "github.com/cosmos/interchain-security/v3/x/ccv/provider/keeper" ccvtypes "github.com/cosmos/interchain-security/v3/x/ccv/types" From 0aef204a3d5ce3719352bc2fd53298e15fd5eec0 Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Wed, 30 Aug 2023 15:07:51 +0200 Subject: [PATCH 08/12] replace allinbits/ibc-go with patched cosmos/ibc-go Also fix panic in TestRedelegationNoConsumer --- go.mod | 2 +- go.sum | 4 ++-- tests/integration/unbonding.go | 4 ++++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 9bb0b8193a..e5e862b782 100644 --- a/go.mod +++ b/go.mod @@ -189,4 +189,4 @@ require ( // following versions might cause unexpected behavior replace github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 -replace github.com/cosmos/ibc-go/v7 => github.com/allinbits/ibc-go/v7 v7.0.0-20230804095822-27dc704b7dec +replace github.com/cosmos/ibc-go/v7 => github.com/cosmos/ibc-go/v7 v7.2.1-0.20230830084256-668fa1d67ef8 diff --git a/go.sum b/go.sum index 019bd1a958..2a67d8cd4d 100644 --- a/go.sum +++ b/go.sum @@ -248,8 +248,6 @@ github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRF github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= github.com/allegro/bigcache v1.2.1-0.20190218064605-e24eb225f156/go.mod h1:Cb/ax3seSYIx7SuZdm2G2xzfwmv3TPSk2ucNfQESPXM= -github.com/allinbits/ibc-go/v7 v7.0.0-20230804095822-27dc704b7dec h1:/xskaNv9DTdO0TrJJFcEENKP6uX3F0c+edM8OwM5eqM= -github.com/allinbits/ibc-go/v7 v7.0.0-20230804095822-27dc704b7dec/go.mod h1:7MptlWeIyqmDiuJeRAFqBvXKY8Hybd+rF8vMSmGd2zg= github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/apache/arrow/go/arrow v0.0.0-20191024131854-af6fa24be0db/go.mod h1:VTxUBvSJ3s3eHAg65PNgrsn5BtqCRPdmyXh6rAfdxN0= @@ -398,6 +396,8 @@ github.com/cosmos/gogoproto v1.4.10 h1:QH/yT8X+c0F4ZDacDv3z+xE3WU1P1Z3wQoLMBRJoK github.com/cosmos/gogoproto v1.4.10/go.mod h1:3aAZzeRWpAwr+SS/LLkICX2/kDFyaYVzckBDzygIxek= github.com/cosmos/iavl v0.20.0 h1:fTVznVlepH0KK8NyKq8w+U7c2L6jofa27aFX6YGlm38= github.com/cosmos/iavl v0.20.0/go.mod h1:WO7FyvaZJoH65+HFOsDir7xU9FWk2w9cHXNW1XHcl7A= +github.com/cosmos/ibc-go/v7 v7.2.1-0.20230830084256-668fa1d67ef8 h1:bynn9Fdu/PCQwqWkxAnJi392b2EU9/OPl37kM58nNn8= +github.com/cosmos/ibc-go/v7 v7.2.1-0.20230830084256-668fa1d67ef8/go.mod h1:OOcjKIRku/j1Xs1RgKK0yvKRrJ5iFuZYMetR1n3yMlc= github.com/cosmos/ics23/go v0.10.0 h1:iXqLLgp2Lp+EdpIuwXTYIQU+AiHj9mOC2X9ab++bZDM= github.com/cosmos/ics23/go v0.10.0/go.mod h1:ZfJSmng/TBNTBkFemHHHj5YY7VAU/MBU980F4VU1NG0= github.com/cosmos/ledger-cosmos-go v0.12.2 h1:/XYaBlE2BJxtvpkHiBm97gFGSGmYGKunKyF3nNqAXZA= diff --git a/tests/integration/unbonding.go b/tests/integration/unbonding.go index 7693c782e5..35d1cfbae1 100644 --- a/tests/integration/unbonding.go +++ b/tests/integration/unbonding.go @@ -370,6 +370,10 @@ func (s *CCVTestSuite) TestRedelegationNoConsumer() { s.providerCtx().BlockTime().Add(stakingKeeper.UnbondingTime(s.providerCtx())), ) + // required before call to incrementTimeByUnbondingPeriod or else a panic + // occurs in ibc-go because trusted validators don't match last trusted. + s.providerChain.NextBlock() + // Increment time so that the unbonding period passes on the provider incrementTimeByUnbondingPeriod(s, Provider) From 5c5e4e4a2bc78d3f080411168a89dc2a52c4b8c1 Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Wed, 30 Aug 2023 15:27:49 +0200 Subject: [PATCH 09/12] fix lint --- testutil/integration/validators.go | 8 ++++++-- testutil/simibc/chain_util.go | 9 +++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/testutil/integration/validators.go b/testutil/integration/validators.go index 9859bb55f1..c8d2decadc 100644 --- a/testutil/integration/validators.go +++ b/testutil/integration/validators.go @@ -3,16 +3,19 @@ package integration import ( "testing" + "github.com/stretchr/testify/require" + + "github.com/cosmos/cosmos-sdk/testutil/mock" + "github.com/cometbft/cometbft/abci/types" tmencoding "github.com/cometbft/cometbft/crypto/encoding" tmtypes "github.com/cometbft/cometbft/types" - "github.com/cosmos/cosmos-sdk/testutil/mock" - "github.com/stretchr/testify/require" ) func CreateValidators(t *testing.T, n int) ( *tmtypes.ValidatorSet, []types.ValidatorUpdate, map[string]tmtypes.PrivValidator, ) { + t.Helper() // generate validators private/public key var ( validators []*tmtypes.Validator @@ -34,6 +37,7 @@ func CreateValidators(t *testing.T, n int) ( } func ToValidatorUpdates(t *testing.T, valSet *tmtypes.ValidatorSet) (valUpdates []types.ValidatorUpdate) { + t.Helper() for _, val := range valSet.Validators { protoPubKey, err := tmencoding.PubKeyToProto(val.PubKey) require.NoError(t, err) diff --git a/testutil/simibc/chain_util.go b/testutil/simibc/chain_util.go index 7f01d6e694..1c00c928c5 100644 --- a/testutil/simibc/chain_util.go +++ b/testutil/simibc/chain_util.go @@ -3,13 +3,14 @@ package simibc import ( "time" - abci "github.com/cometbft/cometbft/abci/types" - tmproto "github.com/cometbft/cometbft/proto/tendermint/types" - sdk "github.com/cosmos/cosmos-sdk/types" - channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" ibctmtypes "github.com/cosmos/ibc-go/v7/modules/light-clients/07-tendermint" ibctesting "github.com/cosmos/ibc-go/v7/testing" + + sdk "github.com/cosmos/cosmos-sdk/types" + + abci "github.com/cometbft/cometbft/abci/types" + tmproto "github.com/cometbft/cometbft/proto/tendermint/types" ) // BeginBlock updates the current header and calls the app.BeginBlock method. From cdb43f9b7c669355e113518cf8f14d2d7d325fab Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Fri, 1 Sep 2023 10:46:39 +0200 Subject: [PATCH 10/12] use released ibc-go 7.3.0 --- go.mod | 2 -- go.sum | 4 ++-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/go.mod b/go.mod index e5e862b782..6cc988a794 100644 --- a/go.mod +++ b/go.mod @@ -188,5 +188,3 @@ require ( // following versions might cause unexpected behavior replace github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 - -replace github.com/cosmos/ibc-go/v7 => github.com/cosmos/ibc-go/v7 v7.2.1-0.20230830084256-668fa1d67ef8 diff --git a/go.sum b/go.sum index 2a67d8cd4d..03e76f9812 100644 --- a/go.sum +++ b/go.sum @@ -396,8 +396,8 @@ github.com/cosmos/gogoproto v1.4.10 h1:QH/yT8X+c0F4ZDacDv3z+xE3WU1P1Z3wQoLMBRJoK github.com/cosmos/gogoproto v1.4.10/go.mod h1:3aAZzeRWpAwr+SS/LLkICX2/kDFyaYVzckBDzygIxek= github.com/cosmos/iavl v0.20.0 h1:fTVznVlepH0KK8NyKq8w+U7c2L6jofa27aFX6YGlm38= github.com/cosmos/iavl v0.20.0/go.mod h1:WO7FyvaZJoH65+HFOsDir7xU9FWk2w9cHXNW1XHcl7A= -github.com/cosmos/ibc-go/v7 v7.2.1-0.20230830084256-668fa1d67ef8 h1:bynn9Fdu/PCQwqWkxAnJi392b2EU9/OPl37kM58nNn8= -github.com/cosmos/ibc-go/v7 v7.2.1-0.20230830084256-668fa1d67ef8/go.mod h1:OOcjKIRku/j1Xs1RgKK0yvKRrJ5iFuZYMetR1n3yMlc= +github.com/cosmos/ibc-go/v7 v7.3.0 h1:QtGeVMi/3JeLWuvEuC60sBHpAF40Oenx/y+bP8+wRRw= +github.com/cosmos/ibc-go/v7 v7.3.0/go.mod h1:mUmaHFXpXrEdcxfdXyau+utZf14pGKVUiXwYftRZZfQ= github.com/cosmos/ics23/go v0.10.0 h1:iXqLLgp2Lp+EdpIuwXTYIQU+AiHj9mOC2X9ab++bZDM= github.com/cosmos/ics23/go v0.10.0/go.mod h1:ZfJSmng/TBNTBkFemHHHj5YY7VAU/MBU980F4VU1NG0= github.com/cosmos/ledger-cosmos-go v0.12.2 h1:/XYaBlE2BJxtvpkHiBm97gFGSGmYGKunKyF3nNqAXZA= From 717b3f87d30f36251250ae7bb6f44103d1644577 Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Wed, 6 Sep 2023 14:32:56 +0200 Subject: [PATCH 11/12] add newPacketFromConsumer/Provider helper funcs --- tests/integration/common.go | 27 +++++++++------ tests/integration/slashing.go | 10 ++---- tests/integration/throttle.go | 51 ++++++----------------------- tests/integration/throttle_retry.go | 10 ++---- tests/integration/valset_update.go | 4 +-- 5 files changed, 33 insertions(+), 69 deletions(-) diff --git a/tests/integration/common.go b/tests/integration/common.go index 78a8ba334d..18e5e6022a 100644 --- a/tests/integration/common.go +++ b/tests/integration/common.go @@ -215,15 +215,26 @@ func redelegate(s *CCVTestSuite, delAddr sdk.AccAddress, valSrcAddr sdk.ValAddre } } +func (s *CCVTestSuite) newPacketFromProvider(data []byte, sequence uint64, path *ibctesting.Path, timeoutHeight clienttypes.Height, timeoutTimestamp uint64) channeltypes.Packet { + return channeltypes.NewPacket(data, sequence, + path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, + path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, + timeoutHeight, timeoutTimestamp) +} + +func (s *CCVTestSuite) newPacketFromConsumer(data []byte, sequence uint64, path *ibctesting.Path, timeoutHeight clienttypes.Height, timeoutTimestamp uint64) channeltypes.Packet { + return channeltypes.NewPacket(data, sequence, + path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, + path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, + timeoutHeight, timeoutTimestamp) +} + // sendOnProviderRecvOnConsumer sends a packet from the provider chain and receives it on the consumer chain func sendOnProviderRecvOnConsumer(s *CCVTestSuite, path *ibctesting.Path, timeoutHeight clienttypes.Height, timeoutTimestamp uint64, data []byte) channeltypes.Packet { sequence, err := path.EndpointB.SendPacket(timeoutHeight, timeoutTimestamp, data) s.Require().NoError(err) - packet := channeltypes.NewPacket(data, sequence, - path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, - path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, - timeoutHeight, timeoutTimestamp) + packet := s.newPacketFromProvider(data, sequence, path, timeoutHeight, timeoutTimestamp) err = path.EndpointA.RecvPacket(packet) s.Require().NoError(err) @@ -235,10 +246,7 @@ func sendOnConsumerRecvOnProvider(s *CCVTestSuite, path *ibctesting.Path, timeou sequence, err := path.EndpointA.SendPacket(timeoutHeight, timeoutTimestamp, data) s.Require().NoError(err) - packet := channeltypes.NewPacket(data, sequence, - path.EndpointA.ChannelConfig.PortID, path.EndpointA.ChannelID, - path.EndpointB.ChannelConfig.PortID, path.EndpointB.ChannelID, - timeoutHeight, timeoutTimestamp) + packet := s.newPacketFromConsumer(data, sequence, path, timeoutHeight, timeoutTimestamp) err = path.EndpointB.RecvPacket(packet) s.Require().NoError(err) @@ -410,8 +418,7 @@ func (suite *CCVTestSuite) commitConsumerPacket(ctx sdk.Context, packetData ccv. oldBlockTime := ctx.BlockTime() timeout := uint64(oldBlockTime.Add(ccv.DefaultCCVTimeoutPeriod).UnixNano()) - packet := channeltypes.NewPacket(packetData.GetBytes(), 1, ccv.ConsumerPortID, suite.path.EndpointA.ChannelID, - ccv.ProviderPortID, suite.path.EndpointB.ChannelID, clienttypes.Height{}, timeout) + packet := suite.newPacketFromConsumer(packetData.GetBytes(), 1, suite.path, clienttypes.Height{}, timeout) return channeltypes.CommitPacket(suite.consumerChain.App.AppCodec(), packet) } diff --git a/tests/integration/slashing.go b/tests/integration/slashing.go index c3fdfdc0c1..3b825fb612 100644 --- a/tests/integration/slashing.go +++ b/tests/integration/slashing.go @@ -90,10 +90,7 @@ func (s *CCVTestSuite) TestRelayAndApplyDowntimePacket() { valsetUpdateIdN := providerKeeper.GetValidatorSetUpdateId(s.providerCtx()) // receive the slash packet on the provider chain. RecvPacket() calls the provider endblocker twice - packet := channeltypes.NewPacket(data, sequence, - s.getFirstBundle().Path.EndpointA.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointA.ChannelID, - s.getFirstBundle().Path.EndpointB.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointB.ChannelID, - timeoutHeight, timeoutTimestamp) + packet := s.newPacketFromConsumer(data, sequence, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp) err = s.path.EndpointB.RecvPacket(packet) s.Require().NoError(err) @@ -266,9 +263,8 @@ func (s *CCVTestSuite) TestSlashPacketAcknowledgement() { SlashPacketData: &spd, }, ) - packet := channeltypes.NewPacket(cpd.GetBytes(), // Consumer always sends v1 packet data - 1, ccv.ConsumerPortID, s.path.EndpointA.ChannelID, - ccv.ProviderPortID, s.path.EndpointB.ChannelID, clienttypes.Height{}, 0) + packet := s.newPacketFromConsumer(cpd.GetBytes(), // Consumer always sends v1 packet data + 1, s.path, clienttypes.Height{}, 0) // Map infraction height on provider so validation passes and provider returns valid ack result providerKeeper.SetValsetUpdateBlockHeight(s.providerCtx(), spd.ValsetUpdateId, 47923) diff --git a/tests/integration/throttle.go b/tests/integration/throttle.go index 48b26cec55..dc32ae7799 100644 --- a/tests/integration/throttle.go +++ b/tests/integration/throttle.go @@ -4,7 +4,6 @@ import ( "time" clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types" - channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" sdk "github.com/cosmos/cosmos-sdk/types" stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" @@ -331,10 +330,7 @@ func (s *CCVTestSuite) TestPacketSpam() { for sequence, data := range packetsData { consumerPacketData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket s.Require().NoError(err) - packet := channeltypes.NewPacket(data, uint64(sequence), - firstBundle.Path.EndpointA.ChannelConfig.PortID, firstBundle.Path.EndpointA.ChannelID, - firstBundle.Path.EndpointB.ChannelConfig.PortID, firstBundle.Path.EndpointB.ChannelID, - timeoutHeight, timeoutTimestamp) + packet := s.newPacketFromConsumer(data, uint64(sequence), firstBundle.Path, timeoutHeight, timeoutTimestamp) providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, *consumerPacketData.GetSlashPacketData()) } @@ -393,10 +389,7 @@ func (s *CCVTestSuite) TestDoubleSignDoesNotAffectThrottling() { for sequence, data := range packetsData { consumerPacketData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket s.Require().NoError(err) - packet := channeltypes.NewPacket(data, uint64(sequence), - firstBundle.Path.EndpointA.ChannelConfig.PortID, firstBundle.Path.EndpointA.ChannelID, - firstBundle.Path.EndpointB.ChannelConfig.PortID, firstBundle.Path.EndpointB.ChannelID, - timeoutHeight, timeoutTimestamp) + packet := s.newPacketFromConsumer(data, uint64(sequence), firstBundle.Path, timeoutHeight, timeoutTimestamp) providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, *consumerPacketData.GetSlashPacketData()) } @@ -501,10 +494,7 @@ func (s *CCVTestSuite) TestQueueOrdering() { for i, data := range packetsData { consumerPacketData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket s.Require().NoError(err) - packet := channeltypes.NewPacket(data, ibcSeqs[i], - firstBundle.Path.EndpointA.ChannelConfig.PortID, firstBundle.Path.EndpointA.ChannelID, - firstBundle.Path.EndpointB.ChannelConfig.PortID, firstBundle.Path.EndpointB.ChannelID, - timeoutHeight, timeoutTimestamp) + packet := s.newPacketFromConsumer(data, ibcSeqs[i], firstBundle.Path, timeoutHeight, timeoutTimestamp) // Type depends on index packets were appended from above if (i+5)%10 == 0 { vscMaturedPacketData := consumerPacketData.GetVscMaturedPacketData() @@ -727,10 +717,7 @@ func (s *CCVTestSuite) TestSlashSameValidator() { for i, data := range packetsData { consumerPacketData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket s.Require().NoError(err) - packet := channeltypes.NewPacket(data, uint64(i), - s.getFirstBundle().Path.EndpointA.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointA.ChannelID, - s.getFirstBundle().Path.EndpointB.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointB.ChannelID, - timeoutHeight, timeoutTimestamp) + packet := s.newPacketFromConsumer(data, uint64(i), s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp) providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, *consumerPacketData.GetSlashPacketData()) } @@ -792,10 +779,7 @@ func (s CCVTestSuite) TestSlashAllValidators() { //nolint:govet // this is a tes ibcSeqNum := uint64(i) consumerPacketData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket s.Require().NoError(err) - packet := channeltypes.NewPacket(data, ibcSeqNum, - s.getFirstBundle().Path.EndpointA.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointA.ChannelID, - s.getFirstBundle().Path.EndpointB.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointB.ChannelID, - timeoutHeight, timeoutTimestamp) + packet := s.newPacketFromConsumer(data, ibcSeqNum, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp) providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, *consumerPacketData.GetSlashPacketData()) } @@ -834,10 +818,7 @@ func (s *CCVTestSuite) TestLeadingVSCMaturedAreDequeued() { data := s.constructVSCMaturedPacketFromConsumer(*bundle) packetData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket s.Require().NoError(err) - packet := channeltypes.NewPacket(data, ibcSeqNum, - ccvtypes.ConsumerPortID, bundle.Path.EndpointA.ChannelID, - ccvtypes.ProviderPortID, bundle.Path.EndpointB.ChannelID, - timeoutHeight, timeoutTimestamp) + packet := s.newPacketFromConsumer(data, ibcSeqNum, bundle.Path, timeoutHeight, timeoutTimestamp) providerKeeper.OnRecvVSCMaturedPacket(s.providerCtx(), packet, *packetData.GetVscMaturedPacketData()) } @@ -851,10 +832,7 @@ func (s *CCVTestSuite) TestLeadingVSCMaturedAreDequeued() { *s.providerChain.Vals.Validators[0], stakingtypes.Infraction_INFRACTION_DOWNTIME) packetData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket s.Require().NoError(err) - packet := channeltypes.NewPacket(data, ibcSeqNum, - ccvtypes.ConsumerPortID, bundle.Path.EndpointA.ChannelID, - ccvtypes.ProviderPortID, bundle.Path.EndpointB.ChannelID, - timeoutHeight, timeoutTimestamp) + packet := s.newPacketFromConsumer(data, ibcSeqNum, bundle.Path, timeoutHeight, timeoutTimestamp) providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, *packetData.GetSlashPacketData()) } @@ -867,10 +845,7 @@ func (s *CCVTestSuite) TestLeadingVSCMaturedAreDequeued() { data := s.constructVSCMaturedPacketFromConsumer(*bundle) packetData := ccvtypes.ConsumerPacketData{} ccvtypes.ModuleCdc.MustUnmarshalJSON(data, &packetData) - packet := channeltypes.NewPacket(data, ibcSeqNum, - ccvtypes.ConsumerPortID, bundle.Path.EndpointA.ChannelID, - ccvtypes.ProviderPortID, bundle.Path.EndpointB.ChannelID, - timeoutHeight, timeoutTimestamp) + packet := s.newPacketFromConsumer(data, ibcSeqNum, bundle.Path, timeoutHeight, timeoutTimestamp) providerKeeper.OnRecvVSCMaturedPacket(s.providerCtx(), packet, *packetData.GetVscMaturedPacketData()) } @@ -941,10 +916,7 @@ func (s *CCVTestSuite) TestVscMaturedHandledPerBlockLimit() { data := s.constructVSCMaturedPacketFromConsumer(*bundle) packetData := ccvtypes.ConsumerPacketData{} ccvtypes.ModuleCdc.MustUnmarshalJSON(data, &packetData) - packet := channeltypes.NewPacket(data, ibcSeqNum, - ccvtypes.ConsumerPortID, bundle.Path.EndpointA.ChannelID, - ccvtypes.ProviderPortID, bundle.Path.EndpointB.ChannelID, - timeoutHeight, timeoutTimestamp) + packet := s.newPacketFromConsumer(data, ibcSeqNum, bundle.Path, timeoutHeight, timeoutTimestamp) providerKeeper.OnRecvVSCMaturedPacket(s.providerCtx(), packet, *packetData.GetVscMaturedPacketData()) } @@ -958,10 +930,7 @@ func (s *CCVTestSuite) TestVscMaturedHandledPerBlockLimit() { *s.providerChain.Vals.Validators[0], stakingtypes.Infraction_INFRACTION_DOWNTIME) consumderPacketData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket s.Require().NoError(err) - packet := channeltypes.NewPacket(data, ibcSeqNum, - ccvtypes.ConsumerPortID, bundle.Path.EndpointA.ChannelID, - ccvtypes.ProviderPortID, bundle.Path.EndpointB.ChannelID, - timeoutHeight, timeoutTimestamp) + packet := s.newPacketFromConsumer(data, ibcSeqNum, bundle.Path, timeoutHeight, timeoutTimestamp) providerKeeper.OnRecvSlashPacket(s.providerCtx(), packet, *consumderPacketData.GetSlashPacketData()) } } diff --git a/tests/integration/throttle_retry.go b/tests/integration/throttle_retry.go index b907800bb5..366a83f8cf 100644 --- a/tests/integration/throttle_retry.go +++ b/tests/integration/throttle_retry.go @@ -58,10 +58,7 @@ func (s *CCVTestSuite) TestSlashRetries() { timeoutHeight = clienttypes.Height{} timeoutTimestamp = uint64(s.getFirstBundle().GetCtx().BlockTime().Add(ccvtypes.DefaultCCVTimeoutPeriod).UnixNano()) ) - packet1 := channeltypes.NewPacket(packetData1, 1, - s.getFirstBundle().Path.EndpointA.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointA.ChannelID, - s.getFirstBundle().Path.EndpointB.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointB.ChannelID, - timeoutHeight, timeoutTimestamp) + packet1 := s.newPacketFromConsumer(packetData1, 1, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp) // Mock the sending of the packet on consumer consumerKeeper.AppendPendingPacket(s.consumerCtx(), ccvtypes.SlashPacket, &ccvtypes.ConsumerPacketData_SlashPacketData{ @@ -114,10 +111,7 @@ func (s *CCVTestSuite) TestSlashRetries() { // Construct and mock the sending of a second packet on consumer tmval2 := s.providerChain.Vals.Validators[2] packetData2 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval2, stakingtypes.Infraction_INFRACTION_DOWNTIME) - packet2 := channeltypes.NewPacket(packetData2, 1, - s.getFirstBundle().Path.EndpointA.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointA.ChannelID, - s.getFirstBundle().Path.EndpointB.ChannelConfig.PortID, s.getFirstBundle().Path.EndpointB.ChannelID, - timeoutHeight, timeoutTimestamp) + packet2 := s.newPacketFromConsumer(packetData2, 1, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp) consumerKeeper.AppendPendingPacket(s.consumerCtx(), ccvtypes.SlashPacket, &ccvtypes.ConsumerPacketData_SlashPacketData{ diff --git a/tests/integration/valset_update.go b/tests/integration/valset_update.go index 955261aa79..b12066afa8 100644 --- a/tests/integration/valset_update.go +++ b/tests/integration/valset_update.go @@ -4,7 +4,6 @@ import ( "time" clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types" - channeltypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types" cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" sdk "github.com/cosmos/cosmos-sdk/types" @@ -71,8 +70,7 @@ func (suite *CCVTestSuite) TestQueueAndSendVSCMaturedPackets() { ) // send first packet - packet := channeltypes.NewPacket(pd.GetBytes(), 1, ccv.ProviderPortID, suite.path.EndpointB.ChannelID, ccv.ConsumerPortID, suite.path.EndpointA.ChannelID, - clienttypes.NewHeight(1, 0), 0) + packet := suite.newPacketFromProvider(pd.GetBytes(), 1, suite.path, clienttypes.NewHeight(1, 0), 0) ack := consumerKeeper.OnRecvVSCPacket(suite.consumerChain.GetContext(), packet, pd) suite.Require().NotNil(ack, "OnRecvVSCPacket did not return ack") suite.Require().True(ack.Success(), "OnRecvVSCPacket did not return a Success Acknowledgment") From 660eb8a2ba6734688e71e7008032e676c1cdeb51 Mon Sep 17 00:00:00 2001 From: Thomas Bruyelle Date: Wed, 6 Sep 2023 14:49:52 +0200 Subject: [PATCH 12/12] constructPacket method return ccv type instead of []byte --- tests/integration/common.go | 10 ++-- tests/integration/slashing.go | 10 ++-- tests/integration/throttle.go | 76 +++++++++++++++-------------- tests/integration/throttle_retry.go | 4 +- 4 files changed, 50 insertions(+), 50 deletions(-) diff --git a/tests/integration/common.go b/tests/integration/common.go index 18e5e6022a..a638c73fc2 100644 --- a/tests/integration/common.go +++ b/tests/integration/common.go @@ -427,11 +427,11 @@ func (suite *CCVTestSuite) commitConsumerPacket(ctx sdk.Context, packetData ccv. // slash packet data to be sent from consumer to provider func (s *CCVTestSuite) constructSlashPacketFromConsumer(bundle icstestingutils.ConsumerBundle, tmVal tmtypes.Validator, infractionType stakingtypes.Infraction, -) []byte { +) ccv.ConsumerPacketData { valsetUpdateId := bundle.GetKeeper().GetHeightValsetUpdateID( bundle.GetCtx(), uint64(bundle.GetCtx().BlockHeight())) - data := ccv.ConsumerPacketData{ + return ccv.ConsumerPacketData{ Type: ccv.SlashPacket, Data: &ccv.ConsumerPacketData_SlashPacketData{ SlashPacketData: &ccv.SlashPacketData{ @@ -444,22 +444,20 @@ func (s *CCVTestSuite) constructSlashPacketFromConsumer(bundle icstestingutils.C }, }, } - return data.GetBytes() } // constructVSCMaturedPacketFromConsumer constructs an IBC packet embedding // VSC Matured packet data to be sent from consumer to provider -func (s *CCVTestSuite) constructVSCMaturedPacketFromConsumer(bundle icstestingutils.ConsumerBundle) []byte { +func (s *CCVTestSuite) constructVSCMaturedPacketFromConsumer(bundle icstestingutils.ConsumerBundle) ccv.ConsumerPacketData { valsetUpdateId := bundle.GetKeeper().GetHeightValsetUpdateID( bundle.GetCtx(), uint64(bundle.GetCtx().BlockHeight())) - data := ccv.ConsumerPacketData{ + return ccv.ConsumerPacketData{ Type: ccv.VscMaturedPacket, Data: &ccv.ConsumerPacketData_VscMaturedPacketData{ VscMaturedPacketData: &ccv.VSCMaturedPacketData{ValsetUpdateId: valsetUpdateId}, }, } - return data.GetBytes() } // incrementTime increments the overall time by jumpPeriod diff --git a/tests/integration/slashing.go b/tests/integration/slashing.go index 3b825fb612..f1be51d33c 100644 --- a/tests/integration/slashing.go +++ b/tests/integration/slashing.go @@ -72,8 +72,8 @@ func (s *CCVTestSuite) TestRelayAndApplyDowntimePacket() { timeoutHeight = clienttypes.Height{} timeoutTimestamp = uint64(s.getFirstBundle().GetCtx().BlockTime().Add(ccv.DefaultCCVTimeoutPeriod).UnixNano()) ) - data := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmVal, stakingtypes.Infraction_INFRACTION_DOWNTIME) - sequence, err := s.getFirstBundle().Path.EndpointA.SendPacket(timeoutHeight, timeoutTimestamp, data) + slashPacket := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmVal, stakingtypes.Infraction_INFRACTION_DOWNTIME) + sequence, err := s.getFirstBundle().Path.EndpointA.SendPacket(timeoutHeight, timeoutTimestamp, slashPacket.GetBytes()) s.Require().NoError(err) // Set outstanding slashing flag for first consumer, it's important to use the consumer's cons addr here @@ -90,7 +90,7 @@ func (s *CCVTestSuite) TestRelayAndApplyDowntimePacket() { valsetUpdateIdN := providerKeeper.GetValidatorSetUpdateId(s.providerCtx()) // receive the slash packet on the provider chain. RecvPacket() calls the provider endblocker twice - packet := s.newPacketFromConsumer(data, sequence, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp) + packet := s.newPacketFromConsumer(slashPacket.GetBytes(), sequence, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp) err = s.path.EndpointB.RecvPacket(packet) s.Require().NoError(err) @@ -205,8 +205,8 @@ func (s *CCVTestSuite) TestRelayAndApplyDoubleSignPacket() { timeoutHeight = clienttypes.Height{} timeoutTimestamp = uint64(s.getFirstBundle().GetCtx().BlockTime().Add(ccv.DefaultCCVTimeoutPeriod).UnixNano()) ) - data := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmVal, stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN) - packet := sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, data) + slashPacket := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmVal, stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN) + packet := sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, slashPacket.GetBytes()) // Advance a few more blocks to make sure any voting power changes would be reflected s.providerChain.NextBlock() diff --git a/tests/integration/throttle.go b/tests/integration/throttle.go index dc32ae7799..7c83529262 100644 --- a/tests/integration/throttle.go +++ b/tests/integration/throttle.go @@ -67,8 +67,8 @@ func (s *CCVTestSuite) TestBasicSlashPacketThrottling() { // Send a slash packet from consumer to provider s.setDefaultValSigningInfo(*s.providerChain.Vals.Validators[0]) tmVal := s.providerChain.Vals.Validators[0] - data := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmVal, stakingtypes.Infraction_INFRACTION_DOWNTIME) - sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, data) + slashPacket := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmVal, stakingtypes.Infraction_INFRACTION_DOWNTIME) + sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, slashPacket.GetBytes()) // Assert validator 0 is jailed and has no power vals = providerStakingKeeper.GetAllValidators(s.providerCtx()) @@ -86,8 +86,8 @@ func (s *CCVTestSuite) TestBasicSlashPacketThrottling() { // Now send a second slash packet from consumer to provider for a different validator. s.setDefaultValSigningInfo(*s.providerChain.Vals.Validators[2]) tmVal = s.providerChain.Vals.Validators[2] - data = s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmVal, stakingtypes.Infraction_INFRACTION_DOWNTIME) - sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, data) + slashPacket = s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmVal, stakingtypes.Infraction_INFRACTION_DOWNTIME) + sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, slashPacket.GetBytes()) // Require that slash packet has not been handled vals = providerStakingKeeper.GetAllValidators(s.providerCtx()) @@ -172,10 +172,10 @@ func (s *CCVTestSuite) TestMultiConsumerSlashPacketThrottling() { timeoutTimestamp = uint64(s.getFirstBundle().GetCtx().BlockTime().Add(ccvtypes.DefaultCCVTimeoutPeriod).UnixNano()) ) for consumerChainID, bundle := range s.consumerBundles { - data := s.constructVSCMaturedPacketFromConsumer(*bundle) - sendOnConsumerRecvOnProvider(s, bundle.Path, timeoutHeight, timeoutTimestamp, data) - data = s.constructVSCMaturedPacketFromConsumer(*bundle) - sendOnConsumerRecvOnProvider(s, bundle.Path, timeoutHeight, timeoutTimestamp, data) + slashPacket := s.constructVSCMaturedPacketFromConsumer(*bundle) + sendOnConsumerRecvOnProvider(s, bundle.Path, timeoutHeight, timeoutTimestamp, slashPacket.GetBytes()) + slashPacket = s.constructVSCMaturedPacketFromConsumer(*bundle) + sendOnConsumerRecvOnProvider(s, bundle.Path, timeoutHeight, timeoutTimestamp, slashPacket.GetBytes()) // Confirm packets were not queued on provider for this consumer s.Require().Equal(uint64(0), providerKeeper.GetThrottledPacketDataSize(s.providerCtx(), consumerChainID)) @@ -204,18 +204,18 @@ func (s *CCVTestSuite) TestMultiConsumerSlashPacketThrottling() { // Send downtime slash packet from consumer to provider tmVal := s.providerChain.Vals.Validators[idx] valsToSlash = append(valsToSlash, *tmVal) - data := s.constructSlashPacketFromConsumer( + slashPacket := s.constructSlashPacketFromConsumer( *bundle, *tmVal, stakingtypes.Infraction_INFRACTION_DOWNTIME, ) - sendOnConsumerRecvOnProvider(s, bundle.Path, timeoutHeight, timeoutTimestamp, data) + sendOnConsumerRecvOnProvider(s, bundle.Path, timeoutHeight, timeoutTimestamp, slashPacket.GetBytes()) // Send two trailing VSC matured packets from consumer to provider - data = s.constructVSCMaturedPacketFromConsumer(*bundle) - sendOnConsumerRecvOnProvider(s, bundle.Path, timeoutHeight, timeoutTimestamp, data) - data = s.constructVSCMaturedPacketFromConsumer(*bundle) - sendOnConsumerRecvOnProvider(s, bundle.Path, timeoutHeight, timeoutTimestamp, data) + slashPacket = s.constructVSCMaturedPacketFromConsumer(*bundle) + sendOnConsumerRecvOnProvider(s, bundle.Path, timeoutHeight, timeoutTimestamp, slashPacket.GetBytes()) + slashPacket = s.constructVSCMaturedPacketFromConsumer(*bundle) + sendOnConsumerRecvOnProvider(s, bundle.Path, timeoutHeight, timeoutTimestamp, slashPacket.GetBytes()) idx++ } @@ -319,7 +319,8 @@ func (s *CCVTestSuite) TestPacketSpam() { infractionType = stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN } valToJail := s.providerChain.Vals.Validators[ibcSeqNum%3] - packetsData = append(packetsData, s.constructSlashPacketFromConsumer(firstBundle, *valToJail, infractionType)) + slashPacket := s.constructSlashPacketFromConsumer(firstBundle, *valToJail, infractionType) + packetsData = append(packetsData, slashPacket.GetBytes()) } // Recv 500 packets from consumer to provider in same block @@ -378,7 +379,8 @@ func (s *CCVTestSuite) TestDoubleSignDoesNotAffectThrottling() { // Increment ibc seq num for each packet (starting at 1) ibcSeqNum++ valToJail := s.providerChain.Vals.Validators[ibcSeqNum%3] - packetsData = append(packetsData, s.constructSlashPacketFromConsumer(firstBundle, *valToJail, stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN)) + slashPacket := s.constructSlashPacketFromConsumer(firstBundle, *valToJail, stakingtypes.Infraction_INFRACTION_DOUBLE_SIGN) + packetsData = append(packetsData, slashPacket.GetBytes()) } // Recv 500 packets from consumer to provider in same block @@ -477,13 +479,13 @@ func (s *CCVTestSuite) TestQueueOrdering() { // Instantiate a vsc matured packet every 10th packet if ibcSeqNum%10 == 0 { - packetsData = append(packetsData, s.constructVSCMaturedPacketFromConsumer(firstBundle)) + packetsData = append(packetsData, s.constructVSCMaturedPacketFromConsumer(firstBundle).GetBytes()) continue } // Else instantiate a slash packet valToJail := s.providerChain.Vals.Validators[ibcSeqNum%3] - packetsData = append(packetsData, s.constructSlashPacketFromConsumer(firstBundle, *valToJail, stakingtypes.Infraction_INFRACTION_DOWNTIME)) + packetsData = append(packetsData, s.constructSlashPacketFromConsumer(firstBundle, *valToJail, stakingtypes.Infraction_INFRACTION_DOWNTIME).GetBytes()) } // Recv 500 packets from consumer to provider in same block @@ -626,12 +628,12 @@ func (s *CCVTestSuite) TestSlashingSmallValidators() { tmval1 := s.providerChain.Vals.Validators[1] tmval2 := s.providerChain.Vals.Validators[2] tmval3 := s.providerChain.Vals.Validators[3] - data1 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval1, stakingtypes.Infraction_INFRACTION_DOWNTIME) - data2 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval2, stakingtypes.Infraction_INFRACTION_DOWNTIME) - data3 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval3, stakingtypes.Infraction_INFRACTION_DOWNTIME) - sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, data1) - sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, data2) - sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, data3) + slashPacket1 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval1, stakingtypes.Infraction_INFRACTION_DOWNTIME) + slashPacket2 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval2, stakingtypes.Infraction_INFRACTION_DOWNTIME) + slashPacket3 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval3, stakingtypes.Infraction_INFRACTION_DOWNTIME) + sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, slashPacket1.GetBytes()) + sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, slashPacket2.GetBytes()) + sendOnConsumerRecvOnProvider(s, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp, slashPacket3.GetBytes()) // Default slash meter replenish fraction is 0.05, so all sent packets should be handled immediately. vals = providerStakingKeeper.GetAllValidators(s.providerCtx()) @@ -701,12 +703,12 @@ func (s *CCVTestSuite) TestSlashSameValidator() { s.setDefaultValSigningInfo(*tmval3) packetsData := [][]byte{ - s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval1, stakingtypes.Infraction_INFRACTION_DOWNTIME), - s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval2, stakingtypes.Infraction_INFRACTION_DOWNTIME), - s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval3, stakingtypes.Infraction_INFRACTION_DOWNTIME), - s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval1, stakingtypes.Infraction_INFRACTION_DOWNTIME), - s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval2, stakingtypes.Infraction_INFRACTION_DOWNTIME), - s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval3, stakingtypes.Infraction_INFRACTION_DOWNTIME), + s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval1, stakingtypes.Infraction_INFRACTION_DOWNTIME).GetBytes(), + s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval2, stakingtypes.Infraction_INFRACTION_DOWNTIME).GetBytes(), + s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval3, stakingtypes.Infraction_INFRACTION_DOWNTIME).GetBytes(), + s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval1, stakingtypes.Infraction_INFRACTION_DOWNTIME).GetBytes(), + s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval2, stakingtypes.Infraction_INFRACTION_DOWNTIME).GetBytes(), + s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval3, stakingtypes.Infraction_INFRACTION_DOWNTIME).GetBytes(), } // Recv and queue all slash packets. @@ -759,14 +761,14 @@ func (s CCVTestSuite) TestSlashAllValidators() { //nolint:govet // this is a tes for _, val := range s.providerChain.Vals.Validators { s.setDefaultValSigningInfo(*val) packetsData = append(packetsData, s.constructSlashPacketFromConsumer( - s.getFirstBundle(), *val, stakingtypes.Infraction_INFRACTION_DOWNTIME)) + s.getFirstBundle(), *val, stakingtypes.Infraction_INFRACTION_DOWNTIME).GetBytes()) } // add 5 more slash packets for each validator, that will be handled in the same block. for _, val := range s.providerChain.Vals.Validators { for i := 0; i < 5; i++ { packetsData = append(packetsData, s.constructSlashPacketFromConsumer( - s.getFirstBundle(), *val, stakingtypes.Infraction_INFRACTION_DOWNTIME)) + s.getFirstBundle(), *val, stakingtypes.Infraction_INFRACTION_DOWNTIME).GetBytes()) } } @@ -815,7 +817,7 @@ func (s *CCVTestSuite) TestLeadingVSCMaturedAreDequeued() { for _, bundle := range s.consumerBundles { for i := 0; i < 50; i++ { ibcSeqNum := uint64(i) - data := s.constructVSCMaturedPacketFromConsumer(*bundle) + data := s.constructVSCMaturedPacketFromConsumer(*bundle).GetBytes() packetData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket s.Require().NoError(err) packet := s.newPacketFromConsumer(data, ibcSeqNum, bundle.Path, timeoutHeight, timeoutTimestamp) @@ -829,7 +831,7 @@ func (s *CCVTestSuite) TestLeadingVSCMaturedAreDequeued() { for i := 50; i < 100; i++ { ibcSeqNum := uint64(i) data := s.constructSlashPacketFromConsumer(*bundle, - *s.providerChain.Vals.Validators[0], stakingtypes.Infraction_INFRACTION_DOWNTIME) + *s.providerChain.Vals.Validators[0], stakingtypes.Infraction_INFRACTION_DOWNTIME).GetBytes() packetData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket s.Require().NoError(err) packet := s.newPacketFromConsumer(data, ibcSeqNum, bundle.Path, timeoutHeight, timeoutTimestamp) @@ -842,7 +844,7 @@ func (s *CCVTestSuite) TestLeadingVSCMaturedAreDequeued() { for _, bundle := range s.consumerBundles { for i := 100; i < 150; i++ { ibcSeqNum := uint64(i) - data := s.constructVSCMaturedPacketFromConsumer(*bundle) + data := s.constructVSCMaturedPacketFromConsumer(*bundle).GetBytes() packetData := ccvtypes.ConsumerPacketData{} ccvtypes.ModuleCdc.MustUnmarshalJSON(data, &packetData) packet := s.newPacketFromConsumer(data, ibcSeqNum, bundle.Path, timeoutHeight, timeoutTimestamp) @@ -913,7 +915,7 @@ func (s *CCVTestSuite) TestVscMaturedHandledPerBlockLimit() { for _, bundle := range s.consumerBundles { for i := 0; i < 100; i++ { ibcSeqNum := uint64(i) - data := s.constructVSCMaturedPacketFromConsumer(*bundle) + data := s.constructVSCMaturedPacketFromConsumer(*bundle).GetBytes() packetData := ccvtypes.ConsumerPacketData{} ccvtypes.ModuleCdc.MustUnmarshalJSON(data, &packetData) packet := s.newPacketFromConsumer(data, ibcSeqNum, bundle.Path, timeoutHeight, timeoutTimestamp) @@ -927,7 +929,7 @@ func (s *CCVTestSuite) TestVscMaturedHandledPerBlockLimit() { for i := 100; i < 150; i++ { ibcSeqNum := uint64(i) data := s.constructSlashPacketFromConsumer(*bundle, - *s.providerChain.Vals.Validators[0], stakingtypes.Infraction_INFRACTION_DOWNTIME) + *s.providerChain.Vals.Validators[0], stakingtypes.Infraction_INFRACTION_DOWNTIME).GetBytes() consumderPacketData, err := provider.UnmarshalConsumerPacketData(data) // Same func used by provider's OnRecvPacket s.Require().NoError(err) packet := s.newPacketFromConsumer(data, ibcSeqNum, bundle.Path, timeoutHeight, timeoutTimestamp) diff --git a/tests/integration/throttle_retry.go b/tests/integration/throttle_retry.go index 366a83f8cf..4d46b310cf 100644 --- a/tests/integration/throttle_retry.go +++ b/tests/integration/throttle_retry.go @@ -53,7 +53,7 @@ func (s *CCVTestSuite) TestSlashRetries() { // Construct a mock slash packet from consumer tmval1 := s.providerChain.Vals.Validators[1] - packetData1 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval1, stakingtypes.Infraction_INFRACTION_DOWNTIME) + packetData1 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval1, stakingtypes.Infraction_INFRACTION_DOWNTIME).GetBytes() var ( timeoutHeight = clienttypes.Height{} timeoutTimestamp = uint64(s.getFirstBundle().GetCtx().BlockTime().Add(ccvtypes.DefaultCCVTimeoutPeriod).UnixNano()) @@ -110,7 +110,7 @@ func (s *CCVTestSuite) TestSlashRetries() { // Construct and mock the sending of a second packet on consumer tmval2 := s.providerChain.Vals.Validators[2] - packetData2 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval2, stakingtypes.Infraction_INFRACTION_DOWNTIME) + packetData2 := s.constructSlashPacketFromConsumer(s.getFirstBundle(), *tmval2, stakingtypes.Infraction_INFRACTION_DOWNTIME).GetBytes() packet2 := s.newPacketFromConsumer(packetData2, 1, s.getFirstBundle().Path, timeoutHeight, timeoutTimestamp) consumerKeeper.AppendPendingPacket(s.consumerCtx(), ccvtypes.SlashPacket,