Skip to content

Commit

Permalink
Move sims runner to simsx
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Sep 11, 2024
1 parent e010034 commit 975e769
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 30 deletions.
6 changes: 3 additions & 3 deletions simapp/sim_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
package simapp

import (
"github.com/cosmos/cosmos-sdk/simsx"
"testing"

"github.com/cosmos/cosmos-sdk/testutils/sims"
simcli "github.com/cosmos/cosmos-sdk/x/simulation/client/cli"
)

Expand All @@ -15,7 +15,7 @@ func BenchmarkFullAppSimulation(b *testing.B) {
b.ReportAllocs()

config := simcli.NewConfigFromFlags()
config.ChainID = sims.SimAppChainID
config.ChainID = simsx.SimAppChainID

sims.RunWithSeed(b, config, NewSimApp, setupStateFactory, 1, nil)
simsx.RunWithSeed(b, config, NewSimApp, setupStateFactory, 1, nil)
}
30 changes: 15 additions & 15 deletions simapp/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/binary"
"encoding/json"
"flag"
"github.com/cosmos/cosmos-sdk/simsx"
"io"
"math/rand"
"strings"
Expand All @@ -29,7 +30,6 @@ import (
"github.com/cosmos/cosmos-sdk/baseapp"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
"github.com/cosmos/cosmos-sdk/testutils/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
Expand All @@ -53,12 +53,12 @@ func interBlockCacheOpt() func(*baseapp.BaseApp) {
}

func TestFullAppSimulation(t *testing.T) {
sims.Run(t, NewSimApp, setupStateFactory)
simsx.Run(t, NewSimApp, setupStateFactory)
}

func setupStateFactory(app *SimApp) sims.SimStateFactory {
func setupStateFactory(app *SimApp) simsx.SimStateFactory {
blockedAddre, _ := BlockedAddresses(app.interfaceRegistry.SigningContext().AddressCodec())
return sims.SimStateFactory{
return simsx.SimStateFactory{
Codec: app.AppCodec(),
AppStateFn: simtestutil.AppStateFn(app.AppCodec(), app.AuthKeeper.AddressCodec(), app.StakingKeeper.ValidatorAddressCodec(), app.SimulationManager().Modules, app.DefaultGenesis()),
BlockedAddr: blockedAddre,
Expand All @@ -73,14 +73,14 @@ var (
)

func TestAppImportExport(t *testing.T) {
sims.Run(t, NewSimApp, setupStateFactory, func(t testing.TB, ti sims.TestInstance[*SimApp]) {
simsx.Run(t, NewSimApp, setupStateFactory, func(t testing.TB, ti simsx.TestInstance[*SimApp]) {
app := ti.App
t.Log("exporting genesis...\n")
exported, err := app.ExportAppStateAndValidators(false, exportWithValidatorSet, exportAllModules)
require.NoError(t, err)

t.Log("importing genesis...\n")
newTestInstance := sims.NewSimulationAppInstance(t, ti.Cfg, NewSimApp)
newTestInstance := simsx.NewSimulationAppInstance(t, ti.Cfg, NewSimApp)
newApp := newTestInstance.App
var genesisState GenesisState
require.NoError(t, json.Unmarshal(exported.AppState, &genesisState))
Expand Down Expand Up @@ -115,20 +115,20 @@ func TestAppImportExport(t *testing.T) {
// set up a new node instance, Init chain from exported genesis
// run new instance for n blocks
func TestAppSimulationAfterImport(t *testing.T) {
sims.Run(t, NewSimApp, setupStateFactory, func(t testing.TB, ti sims.TestInstance[*SimApp]) {
simsx.Run(t, NewSimApp, setupStateFactory, func(t testing.TB, ti simsx.TestInstance[*SimApp]) {
app := ti.App
t.Log("exporting genesis...\n")
exported, err := app.ExportAppStateAndValidators(false, exportWithValidatorSet, exportAllModules)
require.NoError(t, err)

t.Log("importing genesis...\n")
importGenesisStateFactory := func(app *SimApp) sims.SimStateFactory {
return sims.SimStateFactory{
importGenesisStateFactory := func(app *SimApp) simsx.SimStateFactory {
return simsx.SimStateFactory{
Codec: app.AppCodec(),
AppStateFn: func(r *rand.Rand, accs []simtypes.Account, config simtypes.Config) (json.RawMessage, []simtypes.Account, string, time.Time) {
_, err = app.InitChain(&abci.InitChainRequest{
AppStateBytes: exported.AppState,
ChainId: sims.SimAppChainID,
ChainId: simsx.SimAppChainID,
})
if IsEmptyValidatorSetErr(err) {
t.Skip("Skipping simulation as all validators have been unbonded")
Expand All @@ -144,7 +144,7 @@ func TestAppSimulationAfterImport(t *testing.T) {
BalanceSource: app.BankKeeper,
}
}
sims.RunWithSeed(t, ti.Cfg, NewSimApp, importGenesisStateFactory, ti.Cfg.Seed, ti.Cfg.FuzzSeed)
simsx.RunWithSeed(t, ti.Cfg, NewSimApp, importGenesisStateFactory, ti.Cfg.Seed, ti.Cfg.FuzzSeed)
})
}

Expand Down Expand Up @@ -178,7 +178,7 @@ func TestAppStateDeterminism(t *testing.T) {
"streaming.abci.stop-node-on-err": true,
}
others := appOpts
appOpts = sims.AppOptionsFn(func(k string) any {
appOpts = simsx.AppOptionsFn(func(k string) any {
if v, ok := m[k]; ok {
return v
}
Expand All @@ -190,7 +190,7 @@ func TestAppStateDeterminism(t *testing.T) {
var mx sync.Mutex
appHashResults := make(map[int64][][]byte)
appSimLogger := make(map[int64][]simulation.LogWriter)
captureAndCheckHash := func(t testing.TB, ti sims.TestInstance[*SimApp]) {
captureAndCheckHash := func(t testing.TB, ti simsx.TestInstance[*SimApp]) {
seed, appHash := ti.Cfg.Seed, ti.App.LastCommitID().Hash
mx.Lock()
otherHashes, execWriters := appHashResults[seed], appSimLogger[seed]
Expand All @@ -216,7 +216,7 @@ func TestAppStateDeterminism(t *testing.T) {
}
}
// run simulations
sims.RunWithSeeds(t, interBlockCachingAppFactory, setupStateFactory, seeds, []byte{}, captureAndCheckHash)
simsx.RunWithSeeds(t, interBlockCachingAppFactory, setupStateFactory, seeds, []byte{}, captureAndCheckHash)
}

type ComparableStoreApp interface {
Expand Down Expand Up @@ -264,7 +264,7 @@ func FuzzFullAppSimulation(f *testing.F) {
t.Skip()
return
}
sims.RunWithSeeds(
simsx.RunWithSeeds(
t,
NewSimApp,
setupStateFactory,
Expand Down
23 changes: 11 additions & 12 deletions testutils/sims/runner.go → simsx/runner.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package sims
package simsx

import (
"encoding/json"
Expand All @@ -22,7 +22,6 @@ import (
"github.com/cosmos/cosmos-sdk/runtime"
"github.com/cosmos/cosmos-sdk/server"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/simsx"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
Expand All @@ -48,8 +47,8 @@ type SimStateFactory struct {
Codec codec.Codec
AppStateFn simtypes.AppStateFn
BlockedAddr map[string]bool
AccountSource simsx.AccountSourceX
BalanceSource simsx.BalanceSource
AccountSource AccountSourceX
BalanceSource BalanceSource
}

// SimulationApp abstract app that is used by sims
Expand Down Expand Up @@ -159,14 +158,14 @@ func RunWithSeed[T SimulationApp](

type (
HasWeightedOperationsX interface {
WeightedOperationsX(weight simsx.WeightSource, reg simsx.Registry)
WeightedOperationsX(weight WeightSource, reg Registry)
}
HasWeightedOperationsXWithProposals interface {
WeightedOperationsX(weights simsx.WeightSource, reg simsx.Registry, proposals iter.Seq2[uint32, simsx.SimMsgFactoryX],
WeightedOperationsX(weights WeightSource, reg Registry, proposals iter.Seq2[uint32, SimMsgFactoryX],
legacyProposals []simtypes.WeightedProposalContent) //nolint: staticcheck // used for legacy proposal types
}
HasProposalMsgsX interface {
ProposalMsgsX(weights simsx.WeightSource, reg simsx.Registry)
ProposalMsgsX(weights WeightSource, reg Registry)
}
)

Expand Down Expand Up @@ -214,7 +213,7 @@ func prepareWeightedOps(
config simtypes.Config,
txConfig client.TxConfig,
logger log.Logger,
) (simulation.WeightedOperations, *simsx.BasicSimulationReporter) {
) (simulation.WeightedOperations, *BasicSimulationReporter) {
cdc := stateFact.Codec
signingCtx := cdc.InterfaceRegistry().SigningContext()
simState := module.SimulationState{
Expand All @@ -238,10 +237,10 @@ func prepareWeightedOps(
}
}

weights := simsx.ParamWeightSource(simState.AppParams)
reporter := simsx.NewBasicSimulationReporter()
weights := ParamWeightSource(simState.AppParams)
reporter := NewBasicSimulationReporter()

pReg := make(simsx.UniqueTypeRegistry)
pReg := make(UniqueTypeRegistry)
wProps := make([]simtypes.WeightedProposalMsg, 0, len(sm.Modules))
wContent := make([]simtypes.WeightedProposalContent, 0)

Expand All @@ -263,7 +262,7 @@ func prepareWeightedOps(
panic("legacy proposal contents are not empty")
}

oReg := simsx.NewSimsMsgRegistryAdapter(reporter, stateFact.AccountSource, stateFact.BalanceSource, txConfig, logger)
oReg := NewSimsMsgRegistryAdapter(reporter, stateFact.AccountSource, stateFact.BalanceSource, txConfig, logger)
wOps := make([]simtypes.WeightedOperation, 0, len(sm.Modules))
for _, m := range sm.Modules {
// add operations
Expand Down

0 comments on commit 975e769

Please sign in to comment.