From 0615dd9a143c46ff20fabe8409a1dcc82bfe463a Mon Sep 17 00:00:00 2001 From: Alex Peters Date: Tue, 7 Jan 2025 16:36:21 +0100 Subject: [PATCH] Godoc and minor cleanup --- server/v2/appmanager/appmanager.go | 3 +++ server/v2/appmanager/stf.go | 1 + simapp/v2/sim_runner.go | 14 ++++++++++++++ simapp/v2/sim_test.go | 1 - 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/server/v2/appmanager/appmanager.go b/server/v2/appmanager/appmanager.go index cc76a53945db..e283c39e3b0c 100644 --- a/server/v2/appmanager/appmanager.go +++ b/server/v2/appmanager/appmanager.go @@ -55,6 +55,8 @@ type AppManager[T transaction.Tx] interface { // and uncommitted state QueryWithState(ctx context.Context, state corestore.ReaderMap, request transaction.Msg) (transaction.Msg, error) + // DeliverSims processes simulated transactions for a block and generates a response with potential state changes. + // The simsBuilder generates simulated transactions. DeliverSims( ctx context.Context, block *server.BlockRequest[T], @@ -194,6 +196,7 @@ func (a appManager[T]) DeliverBlock( return blockResponse, newState, nil } +// DeliverSims same as DeliverBlock for sims only. func (a appManager[T]) DeliverSims( ctx context.Context, block *server.BlockRequest[T], diff --git a/server/v2/appmanager/stf.go b/server/v2/appmanager/stf.go index ae1a51de3ba1..e98ac1084838 100644 --- a/server/v2/appmanager/stf.go +++ b/server/v2/appmanager/stf.go @@ -42,6 +42,7 @@ type StateTransitionFunction[T transaction.Tx] interface { req transaction.Msg, ) (transaction.Msg, error) + // DeliverSims provides an interface for state transitions by sims. DeliverSims( ctx context.Context, block *server.BlockRequest[T], diff --git a/simapp/v2/sim_runner.go b/simapp/v2/sim_runner.go index 33cf3889be34..f18989f507e9 100644 --- a/simapp/v2/sim_runner.go +++ b/simapp/v2/sim_runner.go @@ -113,6 +113,7 @@ type ( AppFactory[T Tx, V SimulationApp[T]] func(config depinject.Config, outputs ...any) (V, error) ) +// SetupTestInstance initializes and returns a TestInstance prepared with an app, keepers, and necessary configurations. func SetupTestInstance[T Tx, V SimulationApp[T]](t *testing.T, factory AppFactory[T, V], appConfig depinject.Config) TestInstance[T] { t.Helper() nodeHome := t.TempDir() @@ -155,6 +156,7 @@ func SetupTestInstance[T Tx, V SimulationApp[T]](t *testing.T, factory AppFactor } } +// RunWithSeeds runs a series of subtests using the default set of random seeds for deterministic simulation testing. func RunWithSeeds[T Tx](t *testing.T, seeds []int64) { t.Helper() cfg := cli.NewConfigFromFlags() @@ -168,6 +170,7 @@ func RunWithSeeds[T Tx](t *testing.T, seeds []int64) { } } +// RunWithSeed initializes and executes a simulation run with the given seed, generating blocks and transactions. func RunWithSeed[T Tx, V SimulationApp[T]](t *testing.T, appFactory AppFactory[T, V], appConfig depinject.Config, tCfg simtypes.Config, seed int64) { t.Helper() r := rand.New(rand.NewSource(seed)) @@ -214,6 +217,9 @@ func RunWithSeed[T Tx, V SimulationApp[T]](t *testing.T, appFactory AppFactory[T require.NoError(t, testInstance.App.Close(), "closing app") } +// prepareInitialGenesisState initializes the genesis state for simulation by generating accounts, app state, chain ID, and timestamp. +// It uses a random seed, configuration parameters, and module manager to customize the state. +// Blocked accounts are removed from the simulation accounts list based on the bank keeper's configuration. func prepareInitialGenesisState[T Tx]( app SimulationApp[T], r *rand.Rand, @@ -240,6 +246,7 @@ func prepareInitialGenesisState[T Tx]( return accounts, appState, chainID, genesisTimestamp } +// doChainInitWithGenesis initializes the blockchain state with the provided genesis data and returns the initial block response and state root. func doChainInitWithGenesis[T Tx]( t *testing.T, ctx context.Context, @@ -285,6 +292,7 @@ func doChainInitWithGenesis[T Tx]( return initRsp, stateRoot } +// chainState represents the state of a blockchain during a simulation run. type chainState[T Tx] struct { chainID string blockTime time.Time @@ -296,6 +304,10 @@ type chainState[T Tx] struct { txConfig client.TxConfig } +// doMainLoop executes the main simulation loop after chain setup with genesis block. +// Based on the initial seed and configurations, a deterministic set of messages is generated +// and executed. Events like validators missing votes or double signing are included in this +// process. The runtime tracks the validator's state and history. func doMainLoop[T Tx]( t *testing.T, rootCtx context.Context, @@ -421,6 +433,8 @@ func doMainLoop[T Tx]( fmt.Printf("Tx total: %d skipped: %d\n", txTotalCounter, txSkippedCounter) } +// prepareSimsMsgFactories constructs and returns a function to retrieve simulation message factories for all modules. +// It initializes proposal and factory registries, registers proposals and weighted operations, and sorts deterministically. func prepareSimsMsgFactories( r *rand.Rand, modules map[string]appmodulev2.AppModule, diff --git a/simapp/v2/sim_test.go b/simapp/v2/sim_test.go index f72eec17b549..c92aeaedefc3 100644 --- a/simapp/v2/sim_test.go +++ b/simapp/v2/sim_test.go @@ -4,5 +4,4 @@ import "testing" func TestSimsAppV2(t *testing.T) { RunWithSeeds[Tx](t, defaultSeeds) - // RunWithSeed(t, cli.NewConfigFromFlags(), 99) }