-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: enable the simulator for the provider module (#2005)
* Refactor validator set storage * Add comment for getTotalPower * Add provider consensus validator set storage * Add new MaxProviderConsensusValidators param * Add validation for MaxProviderConsensusValidators * Add no_valupdates_staking module * Add function to get MaxProviderConsensusValidators param * Start returning validators in EndBlock * Fix tests * Revert cosmetic change * Revert cosmetic changes * Revert formatting * Add genutil replacer module * Revert formatting * Revert formatting in tests/integration * Revert minor formatting * Fix type * Change wrapped staking to conform to EndBlocker interface * Fix typo * Revert "Fix typo" This reverts commit 62dfd1e. * Add e2e test for inactive vals * Start fixing e2e test * Revert formatting changes * Remove more formatting * Revert extra formatting * Re-wire provider/app.go to use wrapped modules * Remove consumer rewards check * Add simulator test * Add randomly generated parameters for provider in sim * Add invariant * Add simulation to Makefile and github workflow * Use simcli instead of just passing true
- Loading branch information
1 parent
e71129e
commit 9788053
Showing
7 changed files
with
234 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, 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)) | ||
}() | ||
|
||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package keeper | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
types "github.com/cosmos/interchain-security/v5/x/ccv/provider/types" | ||
) | ||
|
||
// RegisterInvariants registers all staking invariants | ||
func RegisterInvariants(ir sdk.InvariantRegistry, k *Keeper) { | ||
ir.RegisterRoute(types.ModuleName, "max-provider-validators", | ||
MaxProviderConsensusValidatorsInvariant(k)) | ||
} | ||
|
||
// MaxProviderConsensusValidatorsInvariant checks that the number of provider consensus validators | ||
// is less than or equal to the maximum number of provider consensus validators | ||
func MaxProviderConsensusValidatorsInvariant(k *Keeper) sdk.Invariant { | ||
return func(ctx sdk.Context) (string, bool) { | ||
params := k.GetParams(ctx) | ||
maxProviderConsensusValidators := params.MaxProviderConsensusValidators | ||
|
||
consensusValidators := k.GetLastProviderConsensusValSet(ctx) | ||
if int64(len(consensusValidators)) > maxProviderConsensusValidators { | ||
return sdk.FormatInvariant(types.ModuleName, "max-provider-validators", | ||
fmt.Sprintf("number of provider consensus validators: %d, exceeds max: %d", | ||
len(consensusValidators), maxProviderConsensusValidators)), true | ||
} | ||
|
||
return "", false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package simulation | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"math/rand" | ||
|
||
"github.com/cosmos/cosmos-sdk/types/module" | ||
"github.com/cosmos/interchain-security/v5/x/ccv/provider/types" | ||
) | ||
|
||
// Simulation parameter constants | ||
const ( | ||
// only includes params that make sense even with a single | ||
maxProviderConsensusValidators = "max_provider_consensus_validators" | ||
) | ||
|
||
// genMaxProviderConsensusValidators returns randomized maxProviderConsensusValidators | ||
func genMaxProviderConsensusValidators(r *rand.Rand) int64 { | ||
return int64(r.Intn(250) + 1) | ||
} | ||
|
||
// RandomizedGenState generates a random GenesisState for staking | ||
func RandomizedGenState(simState *module.SimulationState) { | ||
// params | ||
var ( | ||
maxProviderConsensusVals int64 | ||
) | ||
|
||
simState.AppParams.GetOrGenerate(maxProviderConsensusValidators, &maxProviderConsensusVals, simState.Rand, func(r *rand.Rand) { maxProviderConsensusVals = genMaxProviderConsensusValidators(r) }) | ||
|
||
providerParams := types.DefaultParams() | ||
providerParams.MaxProviderConsensusValidators = maxProviderConsensusVals | ||
|
||
providerGenesis := types.DefaultGenesisState() | ||
providerGenesis.Params = providerParams | ||
|
||
bz, err := json.MarshalIndent(&providerGenesis.Params, "", " ") | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Printf("Selected randomly generated provider parameters:\n%s\n", bz) | ||
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(providerGenesis) | ||
} |