github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/types/module/simulation.go (about)

     1  package module
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"math/rand"
     7  	"time"
     8  
     9  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
    10  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
    11  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/simulation"
    12  )
    13  
    14  // AppModuleSimulation defines the standard functions that every module should expose
    15  // for the SDK blockchain simulator
    16  type AppModuleSimulation interface {
    17  	// randomized genesis states
    18  	GenerateGenesisState(input *SimulationState)
    19  
    20  	// content functions used to simulate governance proposals
    21  	ProposalContents(simState SimulationState) []simulation.WeightedProposalContent
    22  
    23  	// randomized module parameters for param change proposals
    24  	RandomizedParams(r *rand.Rand) []simulation.ParamChange
    25  
    26  	// register a func to decode the each module's defined types from their corresponding store key
    27  	RegisterStoreDecoder(sdk.StoreDecoderRegistry)
    28  
    29  	// simulation operations (i.e msgs) with their respective weight
    30  	WeightedOperations(simState SimulationState) []simulation.WeightedOperation
    31  }
    32  
    33  // SimulationManager defines a simulation manager that provides the high level utility
    34  // for managing and executing simulation functionalities for a group of modules
    35  type SimulationManager struct {
    36  	Modules       []AppModuleSimulation    // array of app modules; we use an array for deterministic simulation tests
    37  	StoreDecoders sdk.StoreDecoderRegistry // functions to decode the key-value pairs from each module's store
    38  }
    39  
    40  // NewSimulationManager creates a new SimulationManager object
    41  //
    42  // CONTRACT: All the modules provided must be also registered on the module Manager
    43  func NewSimulationManager(modules ...AppModuleSimulation) *SimulationManager {
    44  	return &SimulationManager{
    45  		Modules:       modules,
    46  		StoreDecoders: make(sdk.StoreDecoderRegistry),
    47  	}
    48  }
    49  
    50  // GetProposalContents returns each module's proposal content generator function
    51  // with their default operation weight and key.
    52  func (sm *SimulationManager) GetProposalContents(simState SimulationState) []simulation.WeightedProposalContent {
    53  	var wContents []simulation.WeightedProposalContent //nolint:prealloc
    54  	for _, module := range sm.Modules {
    55  		wContents = append(wContents, module.ProposalContents(simState)...)
    56  	}
    57  
    58  	return wContents
    59  }
    60  
    61  // RegisterStoreDecoders registers each of the modules' store decoders into a map
    62  func (sm *SimulationManager) RegisterStoreDecoders() {
    63  	for _, module := range sm.Modules {
    64  		module.RegisterStoreDecoder(sm.StoreDecoders)
    65  	}
    66  }
    67  
    68  // GenerateGenesisStates generates a randomized GenesisState for each of the
    69  // registered modules
    70  func (sm *SimulationManager) GenerateGenesisStates(simState *SimulationState) {
    71  	for _, module := range sm.Modules {
    72  		module.GenerateGenesisState(simState)
    73  	}
    74  }
    75  
    76  // GenerateParamChanges generates randomized contents for creating params change
    77  // proposal transactions
    78  func (sm *SimulationManager) GenerateParamChanges(seed int64) (paramChanges []simulation.ParamChange) {
    79  	r := rand.New(rand.NewSource(seed))
    80  
    81  	for _, module := range sm.Modules {
    82  		paramChanges = append(paramChanges, module.RandomizedParams(r)...)
    83  	}
    84  
    85  	return
    86  }
    87  
    88  // WeightedOperations returns all the modules' weighted operations of an application
    89  func (sm *SimulationManager) WeightedOperations(simState SimulationState) []simulation.WeightedOperation {
    90  	var wOps []simulation.WeightedOperation //nolint:prealloc
    91  	for _, module := range sm.Modules {
    92  		wOps = append(wOps, module.WeightedOperations(simState)...)
    93  	}
    94  
    95  	return wOps
    96  }
    97  
    98  // SimulationState is the input parameters used on each of the module's randomized
    99  // GenesisState generator function
   100  type SimulationState struct {
   101  	AppParams    simulation.AppParams
   102  	Cdc          *codec.Codec                         // application codec
   103  	Rand         *rand.Rand                           // random number
   104  	GenState     map[string]json.RawMessage           // genesis state
   105  	Accounts     []simulation.Account                 // simulation accounts
   106  	InitialStake int64                                // initial coins per account
   107  	NumBonded    int64                                // number of initially bonded accounts
   108  	GenTimestamp time.Time                            // genesis timestamp
   109  	UnbondTime   time.Duration                        // staking unbond time stored to use it as the slashing maximum evidence duration
   110  	ParamChanges []simulation.ParamChange             // simulated parameter changes from modules
   111  	Contents     []simulation.WeightedProposalContent // proposal content generator functions with their default weight and app sim key
   112  }