github.com/Finschia/finschia-sdk@v0.48.1/x/simulation/params.go (about)

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