github.com/cosmos/cosmos-sdk@v0.50.10/x/simulation/params.go (about) 1 package simulation 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "math/rand" 7 8 cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" 9 "github.com/cometbft/cometbft/types" 10 11 "github.com/cosmos/cosmos-sdk/codec" 12 "github.com/cosmos/cosmos-sdk/types/simulation" 13 stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" 14 ) 15 16 const ( 17 // Minimum time per block 18 minTimePerBlock int64 = 10000 / 2 19 20 // Maximum time per block 21 maxTimePerBlock int64 = 10000 22 ) 23 24 // TODO: explain transitional matrix usage 25 var ( 26 // Currently there are 3 different liveness types, 27 // fully online, spotty connection, offline. 28 defaultLivenessTransitionMatrix, _ = CreateTransitionMatrix([][]int{ 29 {90, 20, 1}, 30 {10, 50, 5}, 31 {0, 10, 1000}, 32 }) 33 34 // 3 states: rand in range [0, 4*provided blocksize], 35 // rand in range [0, 2 * provided blocksize], 0 36 defaultBlockSizeTransitionMatrix, _ = CreateTransitionMatrix([][]int{ 37 {85, 5, 0}, 38 {15, 92, 1}, 39 {0, 3, 99}, 40 }) 41 ) 42 43 // Params define the parameters necessary for running the simulations 44 type Params struct { 45 pastEvidenceFraction float64 46 numKeys int 47 evidenceFraction float64 48 initialLivenessWeightings []int 49 livenessTransitionMatrix simulation.TransitionMatrix 50 blockSizeTransitionMatrix simulation.TransitionMatrix 51 } 52 53 func (p Params) PastEvidenceFraction() float64 { 54 return p.pastEvidenceFraction 55 } 56 57 func (p Params) NumKeys() int { 58 return p.numKeys 59 } 60 61 func (p Params) EvidenceFraction() float64 { 62 return p.evidenceFraction 63 } 64 65 func (p Params) InitialLivenessWeightings() []int { 66 return p.initialLivenessWeightings 67 } 68 69 func (p Params) LivenessTransitionMatrix() simulation.TransitionMatrix { 70 return p.livenessTransitionMatrix 71 } 72 73 func (p Params) BlockSizeTransitionMatrix() simulation.TransitionMatrix { 74 return p.blockSizeTransitionMatrix 75 } 76 77 // RandomParams returns random simulation parameters 78 func RandomParams(r *rand.Rand) Params { 79 return Params{ 80 pastEvidenceFraction: r.Float64(), 81 numKeys: simulation.RandIntBetween(r, 2, 2500), // number of accounts created for the simulation 82 evidenceFraction: r.Float64(), 83 initialLivenessWeightings: []int{simulation.RandIntBetween(r, 1, 80), r.Intn(10), r.Intn(10)}, 84 livenessTransitionMatrix: defaultLivenessTransitionMatrix, 85 blockSizeTransitionMatrix: defaultBlockSizeTransitionMatrix, 86 } 87 } 88 89 // Legacy param change proposals 90 91 // LegacyParamChange defines the object used for simulating parameter change proposals 92 type LegacyParamChange struct { 93 subspace string 94 key string 95 simValue simulation.SimValFn 96 } 97 98 func (spc LegacyParamChange) Subspace() string { 99 return spc.subspace 100 } 101 102 func (spc LegacyParamChange) Key() string { 103 return spc.key 104 } 105 106 func (spc LegacyParamChange) SimValue() simulation.SimValFn { 107 return spc.simValue 108 } 109 110 // ComposedKey creates a new composed key for the legacy param change proposal 111 func (spc LegacyParamChange) ComposedKey() string { 112 return spc.Subspace() + "/" + spc.Key() 113 } 114 115 // NewSimLegacyParamChange creates a new LegacyParamChange instance 116 func NewSimLegacyParamChange(subspace, key string, simVal simulation.SimValFn) simulation.LegacyParamChange { 117 return LegacyParamChange{ 118 subspace: subspace, 119 key: key, 120 simValue: simVal, 121 } 122 } 123 124 // Proposal Msgs 125 126 // WeightedProposalMsg defines a common struct for proposal msgs defined by external modules (i.e outside gov) 127 type WeightedProposalMsg struct { 128 appParamsKey string // key used to retrieve the value of the weight from the simulation application params 129 defaultWeight int // default weight 130 msgSimulatorFn simulation.MsgSimulatorFn // msg simulator function 131 } 132 133 func NewWeightedProposalMsg(appParamsKey string, defaultWeight int, msgSimulatorFn simulation.MsgSimulatorFn) simulation.WeightedProposalMsg { 134 return &WeightedProposalMsg{appParamsKey: appParamsKey, defaultWeight: defaultWeight, msgSimulatorFn: msgSimulatorFn} 135 } 136 137 func (w WeightedProposalMsg) AppParamsKey() string { 138 return w.appParamsKey 139 } 140 141 func (w WeightedProposalMsg) DefaultWeight() int { 142 return w.defaultWeight 143 } 144 145 func (w WeightedProposalMsg) MsgSimulatorFn() simulation.MsgSimulatorFn { 146 return w.msgSimulatorFn 147 } 148 149 // Legacy Proposal Content 150 151 // WeightedProposalContent defines a common struct for proposal content defined by external modules (i.e outside gov) 152 // 153 //nolint:staticcheck // used for legacy testing 154 type WeightedProposalContent struct { 155 appParamsKey string // key used to retrieve the value of the weight from the simulation application params 156 defaultWeight int // default weight 157 contentSimulatorFn simulation.ContentSimulatorFn // content simulator function 158 } 159 160 func NewWeightedProposalContent(appParamsKey string, defaultWeight int, contentSimulatorFn simulation.ContentSimulatorFn) simulation.WeightedProposalContent { //nolint:staticcheck // used for legacy testing 161 return &WeightedProposalContent{appParamsKey: appParamsKey, defaultWeight: defaultWeight, contentSimulatorFn: contentSimulatorFn} 162 } 163 164 func (w WeightedProposalContent) AppParamsKey() string { 165 return w.appParamsKey 166 } 167 168 func (w WeightedProposalContent) DefaultWeight() int { 169 return w.defaultWeight 170 } 171 172 func (w WeightedProposalContent) ContentSimulatorFn() simulation.ContentSimulatorFn { //nolint:staticcheck // used for legacy testing 173 return w.contentSimulatorFn 174 } 175 176 // Consensus Params 177 178 // randomConsensusParams returns random simulation consensus parameters, it extracts the Evidence from the Staking genesis state. 179 func randomConsensusParams(r *rand.Rand, appState json.RawMessage, cdc codec.JSONCodec, maxGas int64) *cmtproto.ConsensusParams { 180 var genesisState map[string]json.RawMessage 181 err := json.Unmarshal(appState, &genesisState) 182 if err != nil { 183 panic(err) 184 } 185 186 stakingGenesisState := stakingtypes.GetGenesisStateFromAppState(cdc, genesisState) 187 consensusParams := &cmtproto.ConsensusParams{ 188 Block: &cmtproto.BlockParams{ 189 MaxBytes: int64(simulation.RandIntBetween(r, 20000000, 30000000)), 190 MaxGas: maxGas, 191 }, 192 Validator: &cmtproto.ValidatorParams{ 193 PubKeyTypes: []string{types.ABCIPubKeyTypeEd25519}, 194 }, 195 Evidence: &cmtproto.EvidenceParams{ 196 MaxAgeNumBlocks: int64(stakingGenesisState.Params.UnbondingTime / AverageBlockTime), 197 MaxAgeDuration: stakingGenesisState.Params.UnbondingTime, 198 }, 199 } 200 201 bz, err := json.MarshalIndent(&consensusParams, "", " ") 202 if err != nil { 203 panic(err) 204 } 205 fmt.Printf("Selected randomly generated consensus parameters:\n%s\n", bz) 206 207 return consensusParams 208 }