Skip to content

Commit

Permalink
feat(simulator): burn held balance and update params
Browse files Browse the repository at this point in the history
  • Loading branch information
fmorency committed Jun 27, 2024
1 parent d30b11b commit 4c3116c
Showing 1 changed file with 86 additions and 2 deletions.
88 changes: 86 additions & 2 deletions x/manifest/simulation/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ import (
)

const (
OpWeightMsgPayoutStakeholders = "op_weight_msg_payout_stakeholders" // nolint: gosec
OpWeightMsgPayout = "op_weight_msg_manifest_payout" // nolint: gosec
OpWeightMsgBurnHeldBalance = "op_weight_msg_manifest_burn_held_balance" // nolint: gosec
OpWeightMsgUpdateParams = "op_weight_msg_manifest_update_params" // nolint: gosec
DefaultWeightMsgPayoutStakeholders = 100
DefaultWeightMsgBurnHeldBalance = 100
DefaultWeightMsgUpdateParams = 100
)

// WeightedOperations returns the all the gov module operations with their respective weights.
Expand All @@ -30,15 +34,35 @@ func WeightedOperations(appParams simtypes.AppParams,
operations := make([]simtypes.WeightedOperation, 0)

var weightMsgPayoutStakeholders int
appParams.GetOrGenerate(OpWeightMsgPayoutStakeholders, &weightMsgPayoutStakeholders, nil, func(_ *rand.Rand) {
appParams.GetOrGenerate(OpWeightMsgPayout, &weightMsgPayoutStakeholders, nil, func(_ *rand.Rand) {
weightMsgPayoutStakeholders = DefaultWeightMsgPayoutStakeholders
})

var weightMsgBurnHeldBalance int
appParams.GetOrGenerate(OpWeightMsgBurnHeldBalance, &weightMsgBurnHeldBalance, nil, func(_ *rand.Rand) {
weightMsgBurnHeldBalance = DefaultWeightMsgBurnHeldBalance
})

var weightMsgUpdateParams int
appParams.GetOrGenerate(OpWeightMsgUpdateParams, &weightMsgUpdateParams, nil, func(_ *rand.Rand) {
weightMsgUpdateParams = DefaultWeightMsgUpdateParams
})

operations = append(operations, simulation.NewWeightedOperation(
weightMsgPayoutStakeholders,
SimulateMsgPayout(txGen, k),
))

operations = append(operations, simulation.NewWeightedOperation(
weightMsgBurnHeldBalance,
SimulateMsgBurnHeldBalance(txGen, k),
))

operations = append(operations, simulation.NewWeightedOperation(
weightMsgUpdateParams,
SimulateMsgUpdateParams(txGen, k),
))

return operations
}

Expand Down Expand Up @@ -95,6 +119,62 @@ func SimulateMsgPayout(txGen client.TxConfig, k keeper.Keeper) simtypes.Operatio
}
}

func SimulateMsgBurnHeldBalance(txGen client.TxConfig, k keeper.Keeper) simtypes.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, _ string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
msgType := sdk.MsgTypeURL(&types.MsgBurnHeldBalance{})
simAccount := accs[0]
if simAccount.Address.String() != k.GetAuthority() {
return simtypes.NoOpMsg(types.ModuleName, msgType, "invalid authority"), nil, nil
}

spendable := k.GetBankKeeper().SpendableCoins(ctx, simAccount.Address)
coinsToBurn := simtypes.RandSubsetCoins(r, spendable)
if coinsToBurn.Empty() {
return simtypes.NoOpMsg(types.ModuleName, msgType, "no spendable coin found"), nil, nil
}

if err := k.GetBankKeeper().IsSendEnabledCoins(ctx, coinsToBurn...); err != nil {
return simtypes.NoOpMsg(types.ModuleName, msgType, err.Error()), nil, nil
}

var fees sdk.Coins
var err error
coins, hasNeg := spendable.SafeSub(coinsToBurn...)
if !hasNeg {
fees, err = simtypes.RandomFees(r, ctx, coins)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msgType, "unable to generate fees"), nil, nil
}
}

msg := types.MsgBurnHeldBalance{
Authority: simAccount.Address.String(),
BurnCoins: coinsToBurn,
}

return genAndDeliverTx(r, app, ctx, txGen, simAccount, &msg, k, fees)
}
}

func SimulateMsgUpdateParams(txGen client.TxConfig, k keeper.Keeper) simtypes.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, _ string,
) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
msgType := sdk.MsgTypeURL(&types.MsgUpdateParams{})
simAccount := accs[0]
if simAccount.Address.String() != k.GetAuthority() {
return simtypes.NoOpMsg(types.ModuleName, msgType, "invalid authority"), nil, nil
}

msg := types.MsgUpdateParams{
Authority: simAccount.Address.String(),
Params: types.Params{},
}

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

func newOperationInput(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, txGen client.TxConfig, simAccount simtypes.Account, msg sdk.Msg, k keeper.Keeper) simulation.OperationInput {
return simulation.OperationInput{
R: r,
Expand All @@ -113,3 +193,7 @@ func newOperationInput(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, txGe
func genAndDeliverTxWithRandFees(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, txGen client.TxConfig, simAccount simtypes.Account, msg sdk.Msg, k keeper.Keeper) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
return simulation.GenAndDeliverTxWithRandFees(newOperationInput(r, app, ctx, txGen, simAccount, msg, k))
}

func genAndDeliverTx(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, txGen client.TxConfig, simAccount simtypes.Account, msg sdk.Msg, k keeper.Keeper, fees sdk.Coins) (simtypes.OperationMsg, []simtypes.FutureOperation, error) {
return simulation.GenAndDeliverTx(newOperationInput(r, app, ctx, txGen, simAccount, msg, k), fees)
}

0 comments on commit 4c3116c

Please sign in to comment.