github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/x/staking/simulation/operations.go (about)

     1  package simulation
     2  
     3  import (
     4  	"math/rand"
     5  
     6  	sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types"
     7  	govtypes "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/gov/types"
     8  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/simulation"
     9  	"github.com/fibonacci-chain/fbc/x/params/types"
    10  )
    11  
    12  // SimulateParamChangeProposalContent returns random parameter change content.
    13  // It will generate a ParameterChangeProposal object with anywhere between 1 and
    14  // the total amount of defined parameters changes, all of which have random valid values.
    15  func SimulateParamChangeProposalContent(paramChangePool []simulation.ParamChange) simulation.ContentSimulatorFn {
    16  	return func(r *rand.Rand, _ sdk.Context, _ []simulation.Account) govtypes.Content {
    17  
    18  		lenParamChange := len(paramChangePool)
    19  		if lenParamChange == 0 {
    20  			panic("param changes array is empty")
    21  		}
    22  
    23  		numChanges := simulation.RandIntBetween(r, 1, lenParamChange)
    24  		paramChanges := make([]types.ParamChange, numChanges)
    25  
    26  		// map from key to empty struct; used only for look-up of the keys of the
    27  		// parameters that are already in the random set of changes.
    28  		paramChangesKeys := make(map[string]struct{})
    29  
    30  		for i := 0; i < numChanges; i++ {
    31  			spc := paramChangePool[r.Intn(len(paramChangePool))]
    32  
    33  			// do not include duplicate parameter changes for a given subspace/key
    34  			_, ok := paramChangesKeys[spc.ComposedKey()]
    35  			for ok {
    36  				spc = paramChangePool[r.Intn(len(paramChangePool))]
    37  				_, ok = paramChangesKeys[spc.ComposedKey()]
    38  			}
    39  
    40  			// add a new distinct parameter to the set of changes and register the key
    41  			// to avoid further duplicates
    42  			paramChangesKeys[spc.ComposedKey()] = struct{}{}
    43  			paramChanges[i] = types.NewParamChange(spc.Subspace, spc.Key, spc.SimValue(r))
    44  		}
    45  
    46  		return types.NewParameterChangeProposal(
    47  			simulation.RandStringOfLength(r, 140),  // title
    48  			simulation.RandStringOfLength(r, 5000), // description
    49  			paramChanges,                           // set of changes
    50  			100,
    51  		)
    52  	}
    53  }