Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

test: enable the simulator for the provider module #2005

Merged
merged 33 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
39e5060
Refactor validator set storage
p-offtermatt Jun 25, 2024
7441fe0
Add comment for getTotalPower
p-offtermatt Jun 25, 2024
d9a51fb
Add provider consensus validator set storage
p-offtermatt Jun 25, 2024
96a47d7
Add new MaxProviderConsensusValidators param
p-offtermatt Jun 25, 2024
884a356
Add validation for MaxProviderConsensusValidators
p-offtermatt Jun 25, 2024
356e6e8
Add no_valupdates_staking module
p-offtermatt Jun 25, 2024
b0d3ce0
Add function to get MaxProviderConsensusValidators param
p-offtermatt Jun 26, 2024
5f53dc3
Start returning validators in EndBlock
p-offtermatt Jun 26, 2024
463e370
Fix tests
p-offtermatt Jun 26, 2024
23b4c46
Revert cosmetic change
p-offtermatt Jun 26, 2024
8b57495
Revert cosmetic changes
p-offtermatt Jun 26, 2024
b4ed21a
Revert formatting
p-offtermatt Jun 26, 2024
bc69f20
Add genutil replacer module
p-offtermatt Jun 26, 2024
ca84368
Revert formatting
p-offtermatt Jun 26, 2024
eb6f00f
Revert formatting in tests/integration
p-offtermatt Jun 26, 2024
8f8ff96
Revert minor formatting
p-offtermatt Jun 26, 2024
4e8b917
Fix type
p-offtermatt Jun 27, 2024
af1b7d4
Change wrapped staking to conform to EndBlocker interface
p-offtermatt Jun 27, 2024
62dfd1e
Fix typo
p-offtermatt Jun 27, 2024
49a6e04
Revert "Fix typo"
p-offtermatt Jun 27, 2024
cc1185d
Add e2e test for inactive vals
p-offtermatt Jun 27, 2024
732e35d
Start fixing e2e test
p-offtermatt Jun 27, 2024
c07a765
Revert formatting changes
p-offtermatt Jul 2, 2024
1cf874d
Remove more formatting
p-offtermatt Jul 2, 2024
9d0c9b7
Revert extra formatting
p-offtermatt Jul 2, 2024
4a5b80c
Re-wire provider/app.go to use wrapped modules
p-offtermatt Jul 2, 2024
fda55c4
Remove consumer rewards check
p-offtermatt Jul 2, 2024
f576f84
Add simulator test
p-offtermatt Jul 2, 2024
11b1585
Add randomly generated parameters for provider in sim
p-offtermatt Jul 3, 2024
beac286
Add invariant
p-offtermatt Jul 3, 2024
10866bd
Add simulation to Makefile and github workflow
p-offtermatt Jul 3, 2024
3af7e99
Use simcli instead of just passing true
p-offtermatt Jul 4, 2024
22de291
Merge branch 'feat/inactive-vals-v50' into ph/simulator
p-offtermatt Jul 15, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions .github/workflows/simulation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: Simulation
on:
workflow_call:
pull_request:
merge_group:
push:
branches:
- main
- release/v*
- feat/*

permissions:
contents: read

concurrency:
group: ci-${{ github.ref }}-tests
cancel-in-progress: true

jobs:
simulation:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: "1.22"
check-latest: true
cache: true
cache-dependency-path: go.sum
- uses: technote-space/[email protected]
id: git_diff
with:
PATTERNS: |
**/*.go
go.mod
go.sum
**/go.mod
**/go.sum
**/Makefile
Makefile
- name: simulation test
if: env.GIT_DIFF
run: |
make sim-full
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ verify-models:
../run_invariants.sh


###############################################################################
### Simulation tests ###

# Run a full simulation test
sim-full:
cd app/provider;\
go test -mod=readonly -run=^TestFullAppSimulation$ -Enabled=true -NumBlocks=500 -BlockSize=200 -Commit=true -timeout 24h github.com/cosmos/interchain-security/v5/app/provider -v

###############################################################################
### Linting ###
Expand Down
2 changes: 1 addition & 1 deletion app/consumer-democracy/proposals_whitelisting.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type legacyParamChangeKey struct {
// these parameters don't exist in the consumer app -- keeping them as an
var LegacyWhitelistedParams = map[legacyParamChangeKey]struct{}{
// add whitlisted legacy parameters here [cosmos-sdk <= 0.47]
// commented parameters are just an example - most params have been moved to their respecitve modules
// commented parameters are just an example - most params have been moved to their respective modules
// and they cannot be changed through legacy governance proposals
{Subspace: banktypes.ModuleName, Key: "SendEnabled"}: {},
}
Expand Down
47 changes: 30 additions & 17 deletions app/provider/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ import (
"os"
"path/filepath"

dbm "github.com/cosmos/cosmos-db"
"github.com/cosmos/gogoproto/proto"
"github.com/cosmos/ibc-go/modules/capability"
capabilitykeeper "github.com/cosmos/ibc-go/modules/capability/keeper"
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
"github.com/cosmos/ibc-go/v8/modules/apps/transfer"
ibctransferkeeper "github.com/cosmos/ibc-go/v8/modules/apps/transfer/keeper"
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
Expand All @@ -26,7 +30,7 @@ import (
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
"cosmossdk.io/client/v2/autocli"
"cosmossdk.io/core/appmodule"

"cosmossdk.io/log"
storetypes "cosmossdk.io/store/types"
"cosmossdk.io/x/evidence"
evidencekeeper "cosmossdk.io/x/evidence/keeper"
Expand All @@ -35,6 +39,7 @@ import (
"cosmossdk.io/x/upgrade"
upgradekeeper "cosmossdk.io/x/upgrade/keeper"
upgradetypes "cosmossdk.io/x/upgrade/types"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
Expand All @@ -50,16 +55,20 @@ import (
"github.com/cosmos/cosmos-sdk/server/config"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/std"
"github.com/cosmos/cosmos-sdk/testutil/testdata/testpb"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/types/msgservice"
sigtypes "github.com/cosmos/cosmos-sdk/types/tx/signing"
"github.com/cosmos/cosmos-sdk/version"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
authcodec "github.com/cosmos/cosmos-sdk/x/auth/codec"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
"github.com/cosmos/cosmos-sdk/x/auth/posthandler"
authsims "github.com/cosmos/cosmos-sdk/x/auth/simulation"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
txmodule "github.com/cosmos/cosmos-sdk/x/auth/tx/config"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/auth/vesting"
vestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
Expand Down Expand Up @@ -93,29 +102,21 @@ import (
"github.com/cosmos/cosmos-sdk/x/slashing"
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
slashingtypes "github.com/cosmos/cosmos-sdk/x/slashing/types"
"github.com/cosmos/cosmos-sdk/x/staking"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cosmos/ibc-go/modules/capability"
capabilitykeeper "github.com/cosmos/ibc-go/modules/capability/keeper"
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"

"cosmossdk.io/log"
abci "github.com/cometbft/cometbft/abci/types"
tmjson "github.com/cometbft/cometbft/libs/json"
tmos "github.com/cometbft/cometbft/libs/os"
dbm "github.com/cosmos/cosmos-db"

appencoding "github.com/cosmos/interchain-security/v5/app/encoding"
testutil "github.com/cosmos/interchain-security/v5/testutil/integration"
no_valupdates_genutil "github.com/cosmos/interchain-security/v5/x/ccv/no_valupdates_genutil"
no_valupdates_staking "github.com/cosmos/interchain-security/v5/x/ccv/no_valupdates_staking"
ibcprovider "github.com/cosmos/interchain-security/v5/x/ccv/provider"
ibcproviderclient "github.com/cosmos/interchain-security/v5/x/ccv/provider/client"
ibcproviderkeeper "github.com/cosmos/interchain-security/v5/x/ccv/provider/keeper"
providertypes "github.com/cosmos/interchain-security/v5/x/ccv/provider/types"

"github.com/cosmos/cosmos-sdk/testutil/testdata/testpb"
sigtypes "github.com/cosmos/cosmos-sdk/types/tx/signing"
txmodule "github.com/cosmos/cosmos-sdk/x/auth/tx/config"
)

const (
Expand Down Expand Up @@ -152,7 +153,7 @@ var (
mint.AppModuleBasic{},
slashing.AppModuleBasic{},
distr.AppModuleBasic{},
staking.AppModuleBasic{},
no_valupdates_staking.AppModuleBasic{},
upgrade.AppModuleBasic{},
evidence.AppModuleBasic{},

Expand Down Expand Up @@ -352,9 +353,7 @@ func New(
// Remove the ConsumerRewardsPool from the group of blocked recipient addresses in bank
// this is required for the provider chain to be able to receive tokens from
// the consumer chain
bankBlockedAddrs := app.ModuleAccountAddrs()
delete(bankBlockedAddrs, authtypes.NewModuleAddress(
providertypes.ConsumerRewardsPool).String())
bankBlockedAddrs := BankBlockedAddrs(app)

app.BankKeeper = bankkeeper.NewBaseKeeper(
appCodec,
Expand Down Expand Up @@ -536,7 +535,7 @@ func New(
// NOTE: Any module instantiated in the module manager that is later modified
// must be passed by reference here.
app.MM = module.NewManager(
genutil.NewAppModule(
no_valupdates_genutil.NewAppModule(
app.AccountKeeper,
app.StakingKeeper,
app,
Expand All @@ -552,7 +551,7 @@ func New(
mint.NewAppModule(appCodec, app.MintKeeper, app.AccountKeeper, nil, app.GetSubspace(minttypes.ModuleName)),
slashing.NewAppModule(appCodec, app.SlashingKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.GetSubspace(slashingtypes.ModuleName), app.interfaceRegistry),
distr.NewAppModule(appCodec, app.DistrKeeper, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.GetSubspace(distrtypes.ModuleName)),
staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName)),
no_valupdates_staking.NewAppModule(appCodec, app.StakingKeeper, app.AccountKeeper, app.BankKeeper, app.GetSubspace(stakingtypes.ModuleName)),
upgrade.NewAppModule(&app.UpgradeKeeper, app.AccountKeeper.AddressCodec()),
evidence.NewAppModule(app.EvidenceKeeper),

Expand Down Expand Up @@ -679,6 +678,13 @@ func New(
}

// create the simulation manager and define the order of the modules for deterministic simulations
overrideModules := map[string]module.AppModuleSimulation{
authtypes.ModuleName: auth.NewAppModule(app.appCodec, app.AccountKeeper, authsims.RandomGenesisAccounts, app.GetSubspace(authtypes.ModuleName)),
}
app.sm = module.NewSimulationManagerFromAppModules(app.MM.Modules, overrideModules)

// register the store decoders for simulation tests
app.sm.RegisterStoreDecoders()

// Note this upgrade handler is just an example and may not be exactly what you need to implement.
// See https://docs.cosmos.network/v0.45/building-modules/upgrade.html
Expand Down Expand Up @@ -780,6 +786,13 @@ func New(
return app
}

func BankBlockedAddrs(app *App) map[string]bool {
bankBlockedAddrs := app.ModuleAccountAddrs()
delete(bankBlockedAddrs, authtypes.NewModuleAddress(
providertypes.ConsumerRewardsPool).String())
return bankBlockedAddrs
}

// Name returns the name of the App
func (app *App) Name() string { return app.BaseApp.Name() }

Expand Down
85 changes: 85 additions & 0 deletions app/provider/sim_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package app_test

import (
"os"
"testing"

"cosmossdk.io/store"
"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/server"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
"github.com/cosmos/cosmos-sdk/x/simulation"
simcli "github.com/cosmos/cosmos-sdk/x/simulation/client/cli"

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

providerapp "github.com/cosmos/interchain-security/v5/app/provider"
)

func init() {
simcli.GetSimulatorFlags()
}

// interBlockCacheOpt returns a BaseApp option function that sets the persistent
// inter-block write-through cache.
func interBlockCacheOpt() func(*baseapp.BaseApp) {
return baseapp.SetInterBlockCache(store.NewCommitKVStoreCacheManager())
}

// fauxMerkleModeOpt returns a BaseApp option to use a dbStoreAdapter instead of
// an IAVLStore for faster simulation speed.
func fauxMerkleModeOpt(bapp *baseapp.BaseApp) {
bapp.SetFauxMerkleMode()
}

func TestFullAppSimulation(t *testing.T) {
config := simcli.NewConfigFromFlags()
config.ChainID = "provi"

db, dir, logger, skip, err := simtestutil.SetupSimulation(config, "leveldb-app-sim", "Simulation", simcli.FlagVerboseValue, true) // simcli.FlagEnabledValue)
if skip {
t.Skip("skipping application simulation")
}
require.NoError(t, err, "simulation setup failed")

defer func() {
require.NoError(t, db.Close())
require.NoError(t, os.RemoveAll(dir))
}()
Comment on lines +48 to +51
Copy link
Contributor

Choose a reason for hiding this comment

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

Handle potential errors in deferred functions.

The deferred functions should handle potential errors from db.Close() and os.RemoveAll(dir) to ensure proper resource cleanup.

defer func() {
  if err := db.Close(); err != nil {
    t.Errorf("failed to close database: %v", err)
  }
  if err := os.RemoveAll(dir); err != nil {
    t.Errorf("failed to remove directory: %v", err)
  }
}()
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
defer func() {
require.NoError(t, db.Close())
require.NoError(t, os.RemoveAll(dir))
}()
defer func() {
if err := db.Close(); err != nil {
t.Errorf("failed to close database: %v", err)
}
if err := os.RemoveAll(dir); err != nil {
t.Errorf("failed to remove directory: %v", err)
}
}()


appOptions := make(simtestutil.AppOptionsMap, 0)
appOptions[flags.FlagHome] = providerapp.DefaultNodeHome
appOptions[server.FlagInvCheckPeriod] = simcli.FlagPeriodValue

app := providerapp.New(logger, db, nil, true, appOptions, fauxMerkleModeOpt, interBlockCacheOpt(), baseapp.SetChainID("provi"))
require.Equal(t, "interchain-security-p", app.Name())

encoding := providerapp.MakeTestEncodingConfig()

genesisState := providerapp.NewDefaultGenesisState(encoding.Codec)

// run randomized simulation
_, simParams, simErr := simulation.SimulateFromSeed(
t,
os.Stdout,
app.BaseApp,
simtestutil.AppStateFn(encoding.Codec, app.SimulationManager(), genesisState),
simtypes.RandomAccounts, // Replace with own random account function if using keys other than secp256k1
simtestutil.SimulationOperations(app, app.AppCodec(), config),
providerapp.BankBlockedAddrs(app),
config,
app.AppCodec(),
)

// export state and simParams before the simulation error is checked
err = simtestutil.CheckExportSimulation(app, config, simParams)
require.NoError(t, err)
require.NoError(t, simErr)

if config.Commit {
simtestutil.PrintStats(db)
}
}
1 change: 0 additions & 1 deletion app/sovereign/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ func (app *App) prepForZeroHeightGenesis(ctx sdk.Context, jailAllowedAddrs []str
// withdraw all validator commission
app.StakingKeeper.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) (stop bool) {
valBz, err := app.StakingKeeper.ValidatorAddressCodec().StringToBytes(val.GetOperator())

if err != nil {
panic(err)
}
Expand Down
3 changes: 3 additions & 0 deletions proto/interchain_security/ccv/provider/v1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ message GenesisState {

repeated interchain_security.ccv.provider.v1.ExportedVscSendTimestamp exported_vsc_send_timestamps = 13
[ (gogoproto.nullable) = false ];

repeated ConsensusValidator last_provider_consensus_validators = 14
[ (gogoproto.nullable) = false ];
}

// The provider CCV module's knowledge of consumer state.
Expand Down
17 changes: 12 additions & 5 deletions proto/interchain_security/ccv/provider/v1/provider.proto
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ message Params {

// The number of blocks that comprise an epoch.
int64 blocks_per_epoch = 10;

// The maximal number of validators that will be passed
// to the consensus engine on the provider.
int64 max_provider_consensus_validators = 11;
}

// SlashAcks contains cons addresses of consumer chain validators
Expand Down Expand Up @@ -359,15 +363,18 @@ message ConsumerAddrsToPrune {
AddressList consumer_addrs = 3;
}

// ConsumerValidator is used to facilitate epoch-based transitions. It contains relevant info for
// a validator that is expected to validate on a consumer chain during an epoch.
message ConsumerValidator {
// ConsensusValidator is used to express a validator that
// should be validating on a chain.
// It contains relevant info for
// a validator that is expected to validate on
// either the provider or a consumer chain.
message ConsensusValidator {
// validator's consensus address on the provider chain
bytes provider_cons_addr = 1;
// voting power the validator has during this epoch
int64 power = 2;
// public key the validator uses on the consumer chain during this epoch
tendermint.crypto.PublicKey consumer_public_key = 3;
// public key the validator uses on the chain it is validating on
tendermint.crypto.PublicKey public_key = 3;
}
// ConsumerRewardsAllocation stores the rewards allocated by a consumer chain
// to the consumer rewards pool. It is used to allocate the tokens to the consumer
Expand Down
Loading