github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/simulation/params.go (about) 1 package simulation 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "math/rand" 7 8 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec" 9 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 10 govtypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/gov/types" 11 ) 12 13 const ( 14 // Minimum time per block 15 minTimePerBlock int64 = 10000 / 2 16 17 // Maximum time per block 18 maxTimePerBlock int64 = 10000 19 ) 20 21 // TODO: explain transitional matrix usage 22 var ( 23 // Currently there are 3 different liveness types, 24 // fully online, spotty connection, offline. 25 defaultLivenessTransitionMatrix, _ = CreateTransitionMatrix([][]int{ 26 {90, 20, 1}, 27 {10, 50, 5}, 28 {0, 10, 1000}, 29 }) 30 31 // 3 states: rand in range [0, 4*provided blocksize], 32 // rand in range [0, 2 * provided blocksize], 0 33 defaultBlockSizeTransitionMatrix, _ = CreateTransitionMatrix([][]int{ 34 {85, 5, 0}, 35 {15, 92, 1}, 36 {0, 3, 99}, 37 }) 38 ) 39 40 // AppParams defines a flat JSON of key/values for all possible configurable 41 // simulation parameters. It might contain: operation weights, simulation parameters 42 // and flattened module state parameters (i.e not stored under it's respective module name). 43 type AppParams map[string]json.RawMessage 44 45 // ParamSimulator creates a parameter value from a source of random number 46 type ParamSimulator func(r *rand.Rand) 47 48 // GetOrGenerate attempts to get a given parameter by key from the AppParams 49 // object. If it exists, it'll be decoded and returned. Otherwise, the provided 50 // ParamSimulator is used to generate a random value or default value (eg: in the 51 // case of operation weights where Rand is not used). 52 func (sp AppParams) GetOrGenerate(cdc *codec.Codec, key string, ptr interface{}, r *rand.Rand, ps ParamSimulator) { 53 if v, ok := sp[key]; ok && v != nil { 54 cdc.MustUnmarshalJSON(v, ptr) 55 return 56 } 57 58 ps(r) 59 } 60 61 // ContentSimulatorFn defines a function type alias for generating random proposal 62 // content. 63 type ContentSimulatorFn func(r *rand.Rand, ctx sdk.Context, accs []Account) govtypes.Content 64 65 // Params define the parameters necessary for running the simulations 66 type Params struct { 67 PastEvidenceFraction float64 68 NumKeys int 69 EvidenceFraction float64 70 InitialLivenessWeightings []int 71 LivenessTransitionMatrix TransitionMatrix 72 BlockSizeTransitionMatrix TransitionMatrix 73 } 74 75 // RandomParams returns random simulation parameters 76 func RandomParams(r *rand.Rand) Params { 77 return Params{ 78 PastEvidenceFraction: r.Float64(), 79 NumKeys: RandIntBetween(r, 2, 2500), // number of accounts created for the simulation 80 EvidenceFraction: r.Float64(), 81 InitialLivenessWeightings: []int{RandIntBetween(r, 1, 80), r.Intn(10), r.Intn(10)}, 82 LivenessTransitionMatrix: defaultLivenessTransitionMatrix, 83 BlockSizeTransitionMatrix: defaultBlockSizeTransitionMatrix, 84 } 85 } 86 87 //----------------------------------------------------------------------------- 88 // Param change proposals 89 90 // SimValFn function to generate the randomized parameter change value 91 type SimValFn func(r *rand.Rand) string 92 93 // ParamChange defines the object used for simulating parameter change proposals 94 type ParamChange struct { 95 Subspace string 96 Key string 97 SimValue SimValFn 98 } 99 100 // NewSimParamChange creates a new ParamChange instance 101 func NewSimParamChange(subspace, key string, simVal SimValFn) ParamChange { 102 return ParamChange{ 103 Subspace: subspace, 104 Key: key, 105 SimValue: simVal, 106 } 107 } 108 109 // ComposedKey creates a new composed key for the param change proposal 110 func (spc ParamChange) ComposedKey() string { 111 return fmt.Sprintf("%s/%s", spc.Subspace, spc.Key) 112 } 113 114 //----------------------------------------------------------------------------- 115 // Proposal Contents 116 117 // WeightedProposalContent defines a common struct for proposal contents defined by 118 // external modules (i.e outside gov) 119 type WeightedProposalContent struct { 120 AppParamsKey string // key used to retrieve the value of the weight from the simulation application params 121 DefaultWeight int // default weight 122 ContentSimulatorFn ContentSimulatorFn // content simulator function 123 }