mukan-sdk/x/slashing/simulation/operations.go
Mukan Erkin Törük abb1ff956e
Some checks are pending
Build SimApp / build (amd64) (push) Waiting to run
Build SimApp / build (arm64) (push) Waiting to run
CodeQL / Analyze (push) Waiting to run
Build & Push / build (push) Waiting to run
Run Gosec / Gosec (push) Waiting to run
Lint / golangci-lint (push) Waiting to run
Checks dependencies and mocks generation / Check go mod tidy (push) Waiting to run
Checks dependencies and mocks generation / Check up to date mocks (push) Waiting to run
System Tests / setup (push) Waiting to run
System Tests / test-system (push) Blocked by required conditions
System Tests / test-system-legacy (push) Blocked by required conditions
Tests / Code Coverage / split-test-files (push) Waiting to run
Tests / Code Coverage / tests (00) (push) Blocked by required conditions
Tests / Code Coverage / tests (01) (push) Blocked by required conditions
Tests / Code Coverage / tests (02) (push) Blocked by required conditions
Tests / Code Coverage / tests (03) (push) Blocked by required conditions
Tests / Code Coverage / test-integration (push) Waiting to run
Tests / Code Coverage / test-e2e (push) Waiting to run
Tests / Code Coverage / repo-analysis (push) Blocked by required conditions
Tests / Code Coverage / test-sim-nondeterminism (push) Waiting to run
Tests / Code Coverage / test-clientv2 (push) Waiting to run
Tests / Code Coverage / test-core (push) Waiting to run
Tests / Code Coverage / test-depinject (push) Waiting to run
Tests / Code Coverage / test-errors (push) Waiting to run
Tests / Code Coverage / test-math (push) Waiting to run
Tests / Code Coverage / test-schema (push) Waiting to run
Tests / Code Coverage / test-collections (push) Waiting to run
Tests / Code Coverage / test-cosmovisor (push) Waiting to run
Tests / Code Coverage / test-confix (push) Waiting to run
Tests / Code Coverage / test-store (push) Waiting to run
Tests / Code Coverage / test-log (push) Waiting to run
Tests / Code Coverage / test-x-tx (push) Waiting to run
Tests / Code Coverage / test-x-nft (push) Waiting to run
Tests / Code Coverage / test-x-circuit (push) Waiting to run
Tests / Code Coverage / test-x-feegrant (push) Waiting to run
Tests / Code Coverage / test-x-evidence (push) Waiting to run
Tests / Code Coverage / test-x-upgrade (push) Waiting to run
Tests / Code Coverage / test-tools-benchmark (push) Waiting to run
refactor: complete sovereign stack cleanup — all github.com upstream refs purged
2026-05-11 03:46:06 +03:00

169 lines
5.7 KiB
Go

package simulation
import (
"errors"
"math/rand"
"git.cw.tr/mukan-network/mukan-sdk/baseapp"
"git.cw.tr/mukan-network/mukan-sdk/client"
"git.cw.tr/mukan-network/mukan-sdk/codec"
codectypes "git.cw.tr/mukan-network/mukan-sdk/codec/types"
"git.cw.tr/mukan-network/mukan-sdk/testutil"
simtestutil "git.cw.tr/mukan-network/mukan-sdk/testutil/sims"
sdk "git.cw.tr/mukan-network/mukan-sdk/types"
simtypes "git.cw.tr/mukan-network/mukan-sdk/types/simulation"
"git.cw.tr/mukan-network/mukan-sdk/x/simulation"
"git.cw.tr/mukan-network/mukan-sdk/x/slashing/keeper"
"git.cw.tr/mukan-network/mukan-sdk/x/slashing/types"
)
// Simulation operation weights constants
// will be removed in the future
const (
OpWeightMsgUnjail = "op_weight_msg_unjail"
DefaultWeightMsgUnjail = 100
)
// 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,
cdc codec.JSONCodec,
txGen client.TxConfig,
ak types.AccountKeeper,
bk types.BankKeeper,
k keeper.Keeper,
sk types.StakingKeeper,
) simulation.WeightedOperations {
var weightMsgUnjail int
appParams.GetOrGenerate(OpWeightMsgUnjail, &weightMsgUnjail, nil, func(_ *rand.Rand) {
weightMsgUnjail = DefaultWeightMsgUnjail
})
return simulation.WeightedOperations{
simulation.NewWeightedOperation(
weightMsgUnjail,
SimulateMsgUnjail(codec.NewProtoCodec(registry), txGen, ak, bk, k, sk),
),
}
}
// 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,
ak types.AccountKeeper,
bk types.BankKeeper,
k keeper.Keeper,
sk types.StakingKeeper,
) 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(&types.MsgUnjail{})
allVals, err := sk.GetAllValidators(ctx)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msgType, "unable to get all validators"), nil, err
}
validator, ok := testutil.RandSliceElem(r, allVals)
if !ok {
return simtypes.NoOpMsg(types.ModuleName, msgType, "validator is not ok"), nil, nil // skip
}
bz, err := sk.ValidatorAddressCodec().StringToBytes(validator.GetOperator())
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msgType, "unable to convert validator address to bytes"), nil, err
}
simAccount, found := simtypes.FindAccount(accs, sdk.AccAddress(bz))
if !found {
return simtypes.NoOpMsg(types.ModuleName, msgType, "unable to find account"), nil, nil // skip
}
if !validator.IsJailed() {
// TODO: due to this condition this message is almost, if not always, skipped !
return simtypes.NoOpMsg(types.ModuleName, msgType, "validator is not jailed"), nil, nil
}
consAddr, err := validator.GetConsAddr()
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msgType, "unable to get validator consensus key"), nil, nil
}
info, err := k.GetValidatorSigningInfo(ctx, consAddr)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msgType, "unable to find validator signing info"), nil, nil
}
selfDel, err := sk.Delegation(ctx, simAccount.Address, bz)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msgType, "unable to get self delegation"), nil, nil
}
if selfDel == nil {
return simtypes.NoOpMsg(types.ModuleName, msgType, "self delegation is nil"), nil, nil
}
account := ak.GetAccount(ctx, sdk.AccAddress(bz))
spendable := bk.SpendableCoins(ctx, account.GetAddress())
fees, err := simtypes.RandomFees(r, ctx, spendable)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, msgType, "unable to generate fees"), nil, nil
}
msg := types.NewMsgUnjail(validator.GetOperator())
tx, err := simtestutil.GenSignedMockTx(
r,
txGen,
[]sdk.Msg{msg},
fees,
simtestutil.DefaultGenTxGas,
chainID,
[]uint64{account.GetAccountNumber()},
[]uint64{account.GetSequence()},
simAccount.PrivKey,
)
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "unable to generate mock tx"), nil, err
}
_, res, err := app.SimDeliver(txGen.TxEncoder(), tx)
// result should fail if:
// - validator cannot be unjailed due to tombstone
// - validator is still in jailed period
// - self delegation too low
if info.Tombstoned ||
ctx.BlockHeader().Time.Before(info.JailedUntil) ||
selfDel.GetShares().IsNil() ||
validator.TokensFromShares(selfDel.GetShares()).TruncateInt().LT(validator.GetMinSelfDelegation()) {
if res != nil && err == nil {
if info.Tombstoned {
return simtypes.NewOperationMsg(msg, true, ""), nil, errors.New("validator should not have been unjailed if validator tombstoned")
}
if ctx.BlockHeader().Time.Before(info.JailedUntil) {
return simtypes.NewOperationMsg(msg, true, ""), nil, errors.New("validator unjailed while validator still in jail period")
}
if selfDel.GetShares().IsNil() ||
validator.TokensFromShares(selfDel.GetShares()).TruncateInt().LT(validator.GetMinSelfDelegation()) {
return simtypes.NewOperationMsg(msg, true, ""), nil, errors.New("validator unjailed even though self-delegation too low")
}
}
// msg failed as expected
return simtypes.NewOperationMsg(msg, false, ""), nil, nil
}
if err != nil {
return simtypes.NoOpMsg(types.ModuleName, sdk.MsgTypeURL(msg), "unable to deliver tx"), nil, errors.New(res.Log)
}
return simtypes.NewOperationMsg(msg, true, ""), nil, nil
}
}