Skip to content

Commit

Permalink
Godoc and minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
alpe committed Jan 7, 2025
1 parent bea7e48 commit 0615dd9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions server/v2/appmanager/appmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down Expand Up @@ -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],
Expand Down
1 change: 1 addition & 0 deletions server/v2/appmanager/stf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
14 changes: 14 additions & 0 deletions simapp/v2/sim_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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))
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 0 additions & 1 deletion simapp/v2/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ import "testing"

func TestSimsAppV2(t *testing.T) {
RunWithSeeds[Tx](t, defaultSeeds)
// RunWithSeed(t, cli.NewConfigFromFlags(), 99)
}

0 comments on commit 0615dd9

Please sign in to comment.