feat(sims): Add simsx support to modules (#24145)

Co-authored-by: Alex | Interchain Labs <alex@interchainlabs.io>
This commit is contained in:
Alexander Peters
2025-04-02 13:23:36 +00:00
committed by GitHub
co-authored by Alex | Interchain Labs
parent 99da3279be
commit ea908cef68
53 changed files with 2025 additions and 241 deletions
-8
View File
@@ -1,8 +1,6 @@
package simulation
import (
"encoding/json"
"fmt"
"math/rand"
"time"
@@ -70,11 +68,5 @@ func RandomizedGenState(simState *module.SimulationState) {
)
slashingGenesis := types.NewGenesisState(params, []types.SigningInfo{}, []types.ValidatorMissedBlocks{})
bz, err := json.MarshalIndent(&slashingGenesis, "", " ")
if err != nil {
panic(err)
}
fmt.Printf("Selected randomly generated slashing parameters:\n%s\n", bz)
simState.GenState[types.ModuleName] = simState.Cdc.MustMarshalJSON(slashingGenesis)
}
+98
View File
@@ -0,0 +1,98 @@
package simulation
import (
"context"
"errors"
"time"
sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/simsx"
"github.com/cosmos/cosmos-sdk/x/slashing/keeper"
"github.com/cosmos/cosmos-sdk/x/slashing/types"
)
func MsgUnjailFactory(k keeper.Keeper, sk types.StakingKeeper) simsx.SimMsgFactoryX {
return simsx.NewSimMsgFactoryWithDeliveryResultHandler[*types.MsgUnjail](func(ctx context.Context, testData *simsx.ChainDataSource, reporter simsx.SimulationReporter) ([]simsx.SimAccount, *types.MsgUnjail, simsx.SimDeliveryResultHandler) {
allVals, err := sk.GetAllValidators(ctx)
if err != nil {
reporter.Skip(err.Error())
return nil, nil, nil
}
validator := simsx.OneOf(testData.Rand(), allVals)
if !validator.IsJailed() {
reporter.Skip("validator not jailed")
return nil, nil, nil
}
if validator.InvalidExRate() {
reporter.Skip("validator with invalid exchange rate")
return nil, nil, nil
}
info, err := k.GetValidatorSigningInfo(ctx, must(validator.GetConsAddr()))
if err != nil {
reporter.Skip(err.Error())
return nil, nil, nil
}
valOperBz := must(sk.ValidatorAddressCodec().StringToBytes(validator.GetOperator()))
valOper := testData.GetAccountbyAccAddr(reporter, valOperBz)
if reporter.IsSkipped() {
return nil, nil, nil
}
selfDel, err := sk.Delegation(ctx, valOper.Address, valOperBz)
if selfDel == nil || err != nil {
reporter.Skip("no self delegation")
return nil, nil, nil
}
var handler simsx.SimDeliveryResultHandler
// result should fail if:
// - validator cannot be unjailed due to tombstone
// - validator is still in jailed period
// - self delegation too low
if info.Tombstoned ||
simsx.BlockTime(ctx).Before(info.JailedUntil) ||
selfDel.GetShares().IsNil() ||
validator.TokensFromShares(selfDel.GetShares()).TruncateInt().LT(validator.GetMinSelfDelegation()) {
handler = func(err error) error {
if err == nil {
switch {
case info.Tombstoned:
return errors.New("validator should not have been unjailed if validator tombstoned")
case simsx.BlockTime(ctx).Before(info.JailedUntil):
return errors.New("validator unjailed while validator still in jail period")
case selfDel.GetShares().IsNil() || validator.TokensFromShares(selfDel.GetShares()).TruncateInt().LT(validator.GetMinSelfDelegation()):
return errors.New("validator unjailed even though self-delegation too low")
}
}
return nil
}
}
return []simsx.SimAccount{valOper}, types.NewMsgUnjail(validator.GetOperator()), handler
})
}
// MsgUpdateParamsFactory creates a gov proposal for param updates
func MsgUpdateParamsFactory() simsx.SimMsgFactoryFn[*types.MsgUpdateParams] {
return func(_ context.Context, testData *simsx.ChainDataSource, reporter simsx.SimulationReporter) ([]simsx.SimAccount, *types.MsgUpdateParams) {
r := testData.Rand()
params := types.DefaultParams()
params.DowntimeJailDuration = time.Duration(r.Timestamp().UnixNano())
params.SignedBlocksWindow = int64(r.IntInRange(1, 1000))
params.MinSignedPerWindow = sdkmath.LegacyNewDecWithPrec(int64(r.IntInRange(1, 100)), 2)
params.SlashFractionDoubleSign = sdkmath.LegacyNewDecWithPrec(int64(r.IntInRange(1, 100)), 2)
params.SlashFractionDowntime = sdkmath.LegacyNewDecWithPrec(int64(r.IntInRange(1, 100)), 2)
return nil, &types.MsgUpdateParams{
Authority: testData.ModuleAccountAddress(reporter, "gov"),
Params: params,
}
}
}
func must[T any](r T, err error) T {
if err != nil {
panic(err)
}
return r
}
+3
View File
@@ -18,6 +18,7 @@ import (
)
// Simulation operation weights constants
// will be removed in the future
const (
OpWeightMsgUnjail = "op_weight_msg_unjail"
@@ -25,6 +26,7 @@ const (
)
// WeightedOperations returns all the operations from the module with their respective weights
// migrate to the msg factories instead, this method will be removed in the future
func WeightedOperations(
registry codectypes.InterfaceRegistry,
appParams simtypes.AppParams,
@@ -49,6 +51,7 @@ func WeightedOperations(
}
// SimulateMsgUnjail generates a MsgUnjail with random values
// migrate to the msg factories instead, this method will be removed in the future
func SimulateMsgUnjail(
cdc *codec.ProtoCodec,
txGen client.TxConfig,
+3
View File
@@ -14,6 +14,7 @@ import (
)
// Simulation operation weights constants
// will be removed in the future
const (
DefaultWeightMsgUpdateParams int = 100
@@ -21,6 +22,7 @@ const (
)
// ProposalMsgs defines the module weighted proposals' contents
// migrate to the msg factories instead, this method will be removed in the future
func ProposalMsgs() []simtypes.WeightedProposalMsg {
return []simtypes.WeightedProposalMsg{
simulation.NewWeightedProposalMsg(
@@ -32,6 +34,7 @@ func ProposalMsgs() []simtypes.WeightedProposalMsg {
}
// SimulateMsgUpdateParams returns a random MsgUpdateParams
// migrate to the msg factories instead, this method will be removed in the future
func SimulateMsgUpdateParams(r *rand.Rand, _ sdk.Context, _ []simtypes.Account) sdk.Msg {
// use the default gov module account address as authority
var authority sdk.AccAddress = address.Module("gov")