-
Notifications
You must be signed in to change notification settings - Fork 134
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
Changes from all commits
Commits
Show all changes
33 commits
Select commit
Hold shift + click to select a range
39e5060
Refactor validator set storage
p-offtermatt 7441fe0
Add comment for getTotalPower
p-offtermatt d9a51fb
Add provider consensus validator set storage
p-offtermatt 96a47d7
Add new MaxProviderConsensusValidators param
p-offtermatt 884a356
Add validation for MaxProviderConsensusValidators
p-offtermatt 356e6e8
Add no_valupdates_staking module
p-offtermatt b0d3ce0
Add function to get MaxProviderConsensusValidators param
p-offtermatt 5f53dc3
Start returning validators in EndBlock
p-offtermatt 463e370
Fix tests
p-offtermatt 23b4c46
Revert cosmetic change
p-offtermatt 8b57495
Revert cosmetic changes
p-offtermatt b4ed21a
Revert formatting
p-offtermatt bc69f20
Add genutil replacer module
p-offtermatt ca84368
Revert formatting
p-offtermatt eb6f00f
Revert formatting in tests/integration
p-offtermatt 8f8ff96
Revert minor formatting
p-offtermatt 4e8b917
Fix type
p-offtermatt af1b7d4
Change wrapped staking to conform to EndBlocker interface
p-offtermatt 62dfd1e
Fix typo
p-offtermatt 49a6e04
Revert "Fix typo"
p-offtermatt cc1185d
Add e2e test for inactive vals
p-offtermatt 732e35d
Start fixing e2e test
p-offtermatt c07a765
Revert formatting changes
p-offtermatt 1cf874d
Remove more formatting
p-offtermatt 9d0c9b7
Revert extra formatting
p-offtermatt 4a5b80c
Re-wire provider/app.go to use wrapped modules
p-offtermatt fda55c4
Remove consumer rewards check
p-offtermatt f576f84
Add simulator test
p-offtermatt 11b1585
Add randomly generated parameters for provider in sim
p-offtermatt beac286
Add invariant
p-offtermatt 10866bd
Add simulation to Makefile and github workflow
p-offtermatt 3af7e99
Use simcli instead of just passing true
p-offtermatt 22de291
Merge branch 'feat/inactive-vals-v50' into ph/simulator
p-offtermatt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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()
andos.RemoveAll(dir)
to ensure proper resource cleanup.Committable suggestion