Skip to content

Commit

Permalink
modify simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Jul 23, 2024
1 parent 5d3244d commit 93578c6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 102 deletions.
43 changes: 3 additions & 40 deletions simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,21 @@ package simulation
import (
"encoding/json"
"fmt"
"math/rand"
"sort"

"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/types/simulation"

"github.com/strangelove-ventures/poa"
)

const (
admins = "admins"
allowValSelfExit = "allow_validator_self_exit"
)

func RandomizedGenState(simState *module.SimulationState) {
var (
adm []string
allowSelfExit bool
)

simState.AppParams.GetOrGenerate(admins, &adm, simState.Rand, func(r *rand.Rand) {
// Select a random number of admins from the simState accounts
adminSet := make(map[string]bool)
numAdmins := simulation.RandIntBetween(r, 1, len(simState.Accounts))
for i := 0; i < numAdmins; i++ {
acc, _ := simulation.RandomAcc(r, simState.Accounts)
adminSet[acc.Address.String()] = true
}

// Iteration order on map is non-deterministic
for k := range adminSet {
adm = append(adm, k)
}

// Sort the admin list for determinism
sort.Strings(adm)
})

// Allow validator self exit is enabled 50% of the time
simState.AppParams.GetOrGenerate(allowValSelfExit, &allowSelfExit, simState.Rand, func(r *rand.Rand) { allowSelfExit = r.Intn(2) == 1 })

poaGenesis := poa.GenesisState{
Params: poa.Params{
Admins: adm,
AllowValidatorSelfExit: allowSelfExit,
},
Vals: []poa.Validator{},
}

bz, err := json.MarshalIndent(&poaGenesis.Params, "", " ")
bz, err := json.MarshalIndent(&poaGenesis, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("Selected randomly generated poa parameters:\n%s\n", bz) // nolint: forbidigo
fmt.Printf("Selected randomly generated poa genesis :\n%s\n", bz) // nolint: forbidigo
simState.GenState[poa.ModuleName] = simState.Cdc.MustMarshalJSON(&poaGenesis)
}
67 changes: 5 additions & 62 deletions simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const (
OpWeightMsgPOASetPower = "op_weight_msg_poa_set_power" // nolint: gosec
OpWeightMsgPOARemoveValidator = "op_weight_msg_poa_remove_validator" // nolint: gosec
OpWeightMsgPOARemovePendingValidator = "op_weight_msg_poa_remove_pending_validator" // nolint: gosec
OpWeightMsgPOAUpdateParams = "op_weight_msg_poa_update_params" // nolint: gosec
OpWeightMsgPOACreateValidator = "op_weight_msg_poa_create_validator" // nolint: gosec

)
Expand All @@ -35,7 +34,6 @@ var (
OpWeightMsgPOASetPower: 100,
OpWeightMsgPOARemoveValidator: 20,
OpWeightMsgPOARemovePendingValidator: 100,
OpWeightMsgPOAUpdateParams: 85,
OpWeightMsgPOACreateValidator: 100,
}
)
Expand Down Expand Up @@ -72,8 +70,6 @@ func getWeightedOperation(op string, weight int, txGen client.TxConfig, k keeper
return simulation.NewWeightedOperation(weight, SimulateMsgRemoveValidator(txGen, k))
case OpWeightMsgPOARemovePendingValidator:
return simulation.NewWeightedOperation(weight, SimulateMsgRemovePendingValidator(txGen, k))
case OpWeightMsgPOAUpdateParams:
return simulation.NewWeightedOperation(weight, SimulateMsgUpdateParams(txGen, k))
case OpWeightMsgPOACreateValidator:
return simulation.NewWeightedOperation(weight, SimulateMsgCreateValidator(txGen, k))
default:
Expand Down Expand Up @@ -129,43 +125,6 @@ func SimulateMsgCreateValidator(txGen client.TxConfig, k keeper.Keeper) simtypes
}
}

func SimulateMsgUpdateParams(txGen client.TxConfig, k keeper.Keeper) simtypes.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
msgType := sdk.MsgTypeURL(&poatypes.MsgUpdateParams{})

params, err := k.GetParams(ctx)
if err != nil {
return simtypes.NoOpMsg(poatypes.ModuleName, msgType, "unable to get params"), nil, err
}

admins := params.GetAdmins()
if len(admins) < 2 {
return simtypes.NoOpMsg(poatypes.ModuleName, msgType, "not enough admins found"), nil, nil
}

// Remove a random admin from the list
idx := r.Intn(len(admins))
admins[idx] = admins[len(admins)-1]
admins = admins[:len(admins)-1]

adminAcc, err := selectRandomPOAAccount(r, admins, accs)
if err != nil {
return simtypes.NoOpMsg(poatypes.ModuleName, msgType, err.Error()), nil, err
}

msg := poatypes.MsgUpdateParams{
Sender: adminAcc.Address.String(),
Params: poatypes.Params{
Admins: admins,
AllowValidatorSelfExit: r.Intn(2) == 1,
},
}

return genAndDeliverTxWithRandFees(r, app, ctx, txGen, adminAcc, &msg, k)
}
}

func SimulateMsgRemovePendingValidator(txGen client.TxConfig, k keeper.Keeper) simtypes.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
Expand All @@ -182,7 +141,7 @@ func SimulateMsgRemovePendingValidator(txGen client.TxConfig, k keeper.Keeper) s
// Pick a random pending validator address
valAddr := pending.Validators[r.Intn(len(pending.Validators))].OperatorAddress

adminAcc, err := selectRandomPOAAccount(r, k.GetAdmins(ctx), accs)
adminAcc, err := getPOAAdmin(accs, k.GetAdmin(ctx))
if err != nil {
return simtypes.NoOpMsg(poatypes.ModuleName, msgType, err.Error()), nil, err
}
Expand Down Expand Up @@ -215,7 +174,7 @@ func SimulateMsgRemoveValidator(txGen client.TxConfig, k keeper.Keeper) simtypes
// Select a random bonded validator to remove
validator := validators[r.Intn(len(validators))]

adminAcc, err := selectRandomPOAAccount(r, k.GetAdmins(ctx), accs)
adminAcc, err := getPOAAdmin(accs, k.GetAdmin(ctx))
if err != nil {
return simtypes.NoOpMsg(poatypes.ModuleName, msgType, err.Error()), nil, err
}
Expand Down Expand Up @@ -259,7 +218,7 @@ func SimulateMsgSetPower(txGen client.TxConfig, k keeper.Keeper) simtypes.Operat
return simtypes.NoOpMsg(poatypes.ModuleName, msgType, errStr), nil, nil
}

adminAcc, err := selectRandomPOAAccount(r, k.GetAdmins(ctx), accs)
adminAcc, err := getPOAAdmin(accs, k.GetAdmin(ctx))
if err != nil {
return simtypes.NoOpMsg(poatypes.ModuleName, msgType, err.Error()), nil, err
}
Expand Down Expand Up @@ -338,31 +297,15 @@ func getNewPower(r *rand.Rand, k keeper.Keeper, ctx sdk.Context, validator staki
return newPowerTokens.Uint64(), "", false
}

func selectRandomPOAAccount(r *rand.Rand, admins []string, accs []simtypes.Account) (simtypes.Account, error) {
randomAdminAddr, err := getRandomPOAAdmin(r, admins)
if err != nil {
return simtypes.Account{}, err
}

acc, found := simtypes.FindAccount(accs, sdk.MustAccAddressFromBech32(randomAdminAddr))
func getPOAAdmin(accs []simtypes.Account, admin string) (simtypes.Account, error) {
acc, found := simtypes.FindAccount(accs, sdk.MustAccAddressFromBech32(admin))
if !found {
return simtypes.Account{}, errors.New("admin not found in simulator accounts")
}

return acc, nil
}

func getRandomPOAAdmin(r *rand.Rand, admins []string) (string, error) {
if len(admins) == 0 {
return "", errors.New("no admins found")
}

idx := r.Intn(len(admins))
admin := admins[idx]

return admin, nil
}

func generateRandomDescription(r *rand.Rand) poatypes.Description {
return poatypes.Description{
Moniker: simtypes.RandStringOfLength(r, 10),
Expand Down

0 comments on commit 93578c6

Please sign in to comment.