Skip to content

Commit

Permalink
feat: simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
emidev98 committed May 22, 2023
1 parent 0df1b7a commit 455f247
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 60 deletions.
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,10 @@ test-e2e:
test-benchmark:
@VERSION=$(VERSION) go test -v -mod=readonly -tags='ledger test_ledger_mock' github.com/terra-money/alliance/x/alliance/tests/benchmark

.PHONY: test test-unit test-e2e test-benchmark
test-simulate:
@VERSION=$(VERSION) go test -v -run=TestFullAppSimulation ./app -NumBlocks 200 -BlockSize 10 -Commit -Enabled -Period 1

.PHONY: test test-unit test-e2e test-benchmark test-simulate
###############################################################################
### Linting ###
###############################################################################
Expand Down
102 changes: 74 additions & 28 deletions app/simulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,85 @@ import (
"os"
"testing"

abci "github.com/cometbft/cometbft/abci/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
simulationtypes "github.com/cosmos/cosmos-sdk/types/simulation"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
"github.com/cosmos/cosmos-sdk/x/simulation"
simcli "github.com/cosmos/cosmos-sdk/x/simulation/client/cli"
"github.com/stretchr/testify/require"

"github.com/stretchr/testify/require"
"github.com/terra-money/alliance/app"
)

// SimAppChainID hardcoded chainID for simulation
const SimAppChainID = "simulation-app"
// Hardcoded chainID for simulation.
const (
simulationAppChainID = "simulation-app"
simulationDirPrefix = "leveldb-app-sim"
simulationDBName = "Simulation"
)

func init() {
simcli.GetSimulatorFlags()
}

type SimApp interface {
app.App
GetBaseApp() *baseapp.BaseApp
AppCodec() codec.Codec
SimulationManager() *module.SimulationManager
ModuleAccountAddrs() map[string]bool
Name() string
LegacyAmino() *codec.LegacyAmino
BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock
EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock
InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain
// Running as a go test:
//
// go test -v -run=TestFullAppSimulation ./app -NumBlocks 200 -BlockSize 10 -Commit -Enabled -Period 1
func TestFullAppSimulation(t *testing.T) {
config := simcli.NewConfigFromFlags()
config.ChainID = simulationAppChainID

if !simcli.FlagEnabledValue {
t.Skip("skipping application simulation")
}

db, dir, logger, _, err := simtestutil.SetupSimulation(
config,
simulationDirPrefix,
simulationDBName,
simcli.FlagVerboseValue,
true, // Don't use this as it is confusing
)
require.NoError(t, err, "simulation setup failed")

defer func() {
require.NoError(t, db.Close())
require.NoError(t, os.RemoveAll(dir))
}()

app := app.New(logger,
db,
nil,
true,
map[int64]bool{},
app.DefaultNodeHome,
simcli.FlagPeriodValue,
app.MakeTestEncodingConfig(),
simtestutil.EmptyAppOptions{},
baseapp.SetChainID(simulationAppChainID),
)

// run randomized simulation
_, simParams, simErr := simulation.SimulateFromSeed(
t,
os.Stdout,
app.BaseApp,
simtestutil.AppStateFn(app.AppCodec(), app.SimulationManager(), app.DefaultGenesis()),
simtypes.RandomAccounts,
simtestutil.SimulationOperations(app, app.AppCodec(), config),
app.BankKeeper.GetBlockedAddresses(),
config,
app.AppCodec(),
)

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

if config.Commit {
simtestutil.PrintStats(db)
}
}

// BenchmarkSimulation run the chain simulation
Expand All @@ -61,8 +108,7 @@ func BenchmarkSimulation(b *testing.B) {

encoding := app.MakeTestEncodingConfig()

simApp := app.New(
logger,
app := app.New(logger,
db,
nil,
true,
Expand All @@ -77,17 +123,17 @@ func BenchmarkSimulation(b *testing.B) {
_, simParams, simErr := simulation.SimulateFromSeed(
b,
os.Stdout,
simApp.GetBaseApp(),
simtestutil.AppStateFn(simApp.AppCodec(), simApp.SimulationManager(), simApp.DefaultGenesis()),
simulationtypes.RandomAccounts,
simtestutil.SimulationOperations(simApp, simApp.AppCodec(), config),
simApp.ModuleAccountAddrs(),
app.GetBaseApp(),
simtestutil.AppStateFn(app.AppCodec(), app.SimulationManager(), app.DefaultGenesis()),
simtypes.RandomAccounts,
simtestutil.SimulationOperations(app, app.AppCodec(), config),
app.ModuleAccountAddrs(),
config,
simApp.AppCodec(),
app.AppCodec(),
)

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

Expand Down
22 changes: 11 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/cometbft/cometbft v0.37.1
github.com/cometbft/cometbft-db v0.8.0
github.com/cosmos/cosmos-proto v1.0.0-beta.2
github.com/cosmos/cosmos-sdk v0.47.2
github.com/cosmos/cosmos-sdk v0.47.3-0.20230513170018-83d600596f5d
github.com/cosmos/ibc-go/v7 v7.0.0
github.com/golang/mock v1.6.0
github.com/golang/protobuf v1.5.3
Expand All @@ -19,12 +19,12 @@ require (
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.2
golang.org/x/exp v0.0.0-20230321023759-10a507213a29
google.golang.org/genproto v0.0.0-20230216225411-c8e22ba71e44
google.golang.org/grpc v1.54.0
google.golang.org/genproto v0.0.0-20230306155012-7f2fa6fef1f4
google.golang.org/grpc v1.55.0
google.golang.org/protobuf v1.30.0
)

require github.com/cosmos/gogoproto v1.4.8
require github.com/cosmos/gogoproto v1.4.10

require (
cloud.google.com/go v0.110.0 // indirect
Expand All @@ -35,6 +35,7 @@ require (
cosmossdk.io/api v0.3.1 // indirect
cosmossdk.io/core v0.5.1 // indirect
cosmossdk.io/depinject v1.0.0-alpha.3 // indirect
cosmossdk.io/log v1.1.0 // indirect
filippo.io/edwards25519 v1.0.0 // indirect
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 // indirect
github.com/99designs/keyring v1.2.1 // indirect
Expand Down Expand Up @@ -107,35 +108,34 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.16.3 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lib/pq v1.10.7 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/linxGnu/grocksdb v1.7.16 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/manifoldco/promptui v0.9.0 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mimoo/StrobeGo v0.0.0-20210601165009-122bf33a46e0 // indirect
github.com/minio/highwayhash v1.0.2 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mtibben/percent v0.2.1 // indirect
github.com/pelletier/go-toml/v2 v2.0.6 // indirect
github.com/petermattis/goid v0.0.0-20221215004737-a150e88a970d // indirect
github.com/pelletier/go-toml/v2 v2.0.7 // indirect
github.com/petermattis/goid v0.0.0-20230317030725-371a4b8eda08 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.40.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/rakyll/statik v0.1.7 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rs/cors v1.8.3 // indirect
github.com/rs/zerolog v1.29.1 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
Expand Down
Loading

0 comments on commit 455f247

Please sign in to comment.