Merge PR #4838: Sim refactor 2 - Move GenesisState generators to modules
This commit is contained in:
committed by
Alexander Bezobchuk
parent
a131570cdc
commit
45b25ceaae
@@ -130,17 +130,9 @@ type AppModuleGenesis interface {
|
||||
ExportGenesis(sdk.Context) json.RawMessage
|
||||
}
|
||||
|
||||
// AppModuleSimulation defines the standard functions that every module should expose
|
||||
// for the SDK blockchain simulator
|
||||
type AppModuleSimulation interface {
|
||||
// register a func to decode the each module's defined types from their corresponding store key
|
||||
RegisterStoreDecoder(sdk.StoreDecoderRegistry)
|
||||
}
|
||||
|
||||
// AppModule is the standard form for an application module
|
||||
type AppModule interface {
|
||||
AppModuleGenesis
|
||||
AppModuleSimulation
|
||||
|
||||
// registers
|
||||
RegisterInvariants(sdk.InvariantRegistry)
|
||||
@@ -173,9 +165,6 @@ func NewGenesisOnlyAppModule(amg AppModuleGenesis) AppModule {
|
||||
// RegisterInvariants is a placeholder function register no invariants
|
||||
func (GenesisOnlyAppModule) RegisterInvariants(_ sdk.InvariantRegistry) {}
|
||||
|
||||
// RegisterStoreDecoder empty store decoder registry
|
||||
func (GenesisOnlyAppModule) RegisterStoreDecoder(_ sdk.StoreDecoderRegistry) {}
|
||||
|
||||
// Route empty module message route
|
||||
func (GenesisOnlyAppModule) Route() string { return "" }
|
||||
|
||||
|
||||
@@ -1,20 +1,41 @@
|
||||
package module
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/codec"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/simulation"
|
||||
)
|
||||
|
||||
// AppModuleSimulation defines the standard functions that every module should expose
|
||||
// for the SDK blockchain simulator
|
||||
type AppModuleSimulation interface {
|
||||
// register a func to decode the each module's defined types from their corresponding store key
|
||||
RegisterStoreDecoder(sdk.StoreDecoderRegistry)
|
||||
|
||||
// randomized genesis states
|
||||
GenerateGenesisState(input *SimulationState)
|
||||
|
||||
// randomized module parameters for param change proposals
|
||||
RandomizedParams(r *rand.Rand) []simulation.ParamChange
|
||||
}
|
||||
|
||||
// SimulationManager defines a simulation manager that provides the high level utility
|
||||
// for managing and executing simulation functionalities for a group of modules
|
||||
type SimulationManager struct {
|
||||
Modules map[string]AppModule
|
||||
StoreDecoders sdk.StoreDecoderRegistry
|
||||
Modules []AppModuleSimulation // array of app modules; we use an array for deterministic simulation tests
|
||||
StoreDecoders sdk.StoreDecoderRegistry // functions to decode the key-value pairs from each module's store
|
||||
}
|
||||
|
||||
// NewSimulationManager creates a new SimulationManager object
|
||||
func NewSimulationManager(moduleMap map[string]AppModule) *SimulationManager {
|
||||
//
|
||||
// CONTRACT: All the modules provided must be also registered on the module Manager
|
||||
func NewSimulationManager(modules ...AppModuleSimulation) *SimulationManager {
|
||||
return &SimulationManager{
|
||||
Modules: moduleMap,
|
||||
Modules: modules,
|
||||
StoreDecoders: make(sdk.StoreDecoderRegistry),
|
||||
}
|
||||
}
|
||||
@@ -25,3 +46,37 @@ func (sm *SimulationManager) RegisterStoreDecoders() {
|
||||
module.RegisterStoreDecoder(sm.StoreDecoders)
|
||||
}
|
||||
}
|
||||
|
||||
// GenerateGenesisStates generates a randomized GenesisState for each of the
|
||||
// registered modules
|
||||
func (sm *SimulationManager) GenerateGenesisStates(input *SimulationState) {
|
||||
for _, module := range sm.Modules {
|
||||
module.GenerateGenesisState(input)
|
||||
}
|
||||
}
|
||||
|
||||
// GenerateParamChanges generates randomized contents for creating params change
|
||||
// proposal transactions
|
||||
func (sm *SimulationManager) GenerateParamChanges(seed int64) (paramChanges []simulation.ParamChange) {
|
||||
r := rand.New(rand.NewSource(seed))
|
||||
|
||||
for _, module := range sm.Modules {
|
||||
paramChanges = append(paramChanges, module.RandomizedParams(r)...)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// SimulationState is the input parameters used on each of the module's randomized
|
||||
// GenesisState generator function
|
||||
type SimulationState struct {
|
||||
AppParams simulation.AppParams
|
||||
Cdc *codec.Codec // application codec
|
||||
Rand *rand.Rand // random number
|
||||
GenState map[string]json.RawMessage // genesis state
|
||||
Accounts []simulation.Account // simulation accounts
|
||||
InitialStake int64 // initial coins per account
|
||||
NumBonded int64 // number of initially bonded acconts
|
||||
GenTimestamp time.Time // genesis timestamp
|
||||
UnbondTime time.Duration // staking unbond time stored to use it as the slashing maximum evidence duration
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user