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

     1  package simulation
     2  
     3  import (
     4  	"math/rand"
     5  
     6  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/codec"
     7  	simappparams "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/simapp/params"
     8  	"github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/simulation"
     9  	"github.com/fibonacci-chain/fbc/x/slashing/internal/keeper"
    10  	"github.com/fibonacci-chain/fbc/x/slashing/internal/types"
    11  	stakingkeeper "github.com/fibonacci-chain/fbc/x/staking/keeper"
    12  )
    13  
    14  // Simulation operation weights constants
    15  const (
    16  	OpWeightMsgUnjail = "op_weight_msg_unjail"
    17  )
    18  
    19  // WeightedOperations returns all the operations from the module with their respective weights
    20  func WeightedOperations(
    21  	appParams simulation.AppParams, cdc *codec.Codec, ak types.AccountKeeper,
    22  	k keeper.Keeper, sk stakingkeeper.Keeper,
    23  ) simulation.WeightedOperations {
    24  
    25  	var weightMsgUnjail int
    26  	appParams.GetOrGenerate(cdc, OpWeightMsgUnjail, &weightMsgUnjail, nil,
    27  		func(_ *rand.Rand) {
    28  			weightMsgUnjail = simappparams.DefaultWeightMsgUnjail
    29  		},
    30  	)
    31  
    32  	return simulation.WeightedOperations{
    33  		simulation.NewWeightedOperation(
    34  			weightMsgUnjail,
    35  			SimulateMsgUnjail(ak, k, sk),
    36  		),
    37  	}
    38  }
    39  
    40  // SimulateMsgUnjail generates a MsgUnjail with random values
    41  // nolint: funlen
    42  func SimulateMsgUnjail(ak types.AccountKeeper, k keeper.Keeper, sk stakingkeeper.Keeper) simulation.Operation { // nolint:interfacer
    43  	return nil
    44  	//todo:disable follow code just for now
    45  	//return func(
    46  	//	r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
    47  	//	accs []simulation.Account, chainID string,
    48  	//) (simulation.OperationMsg, []simulation.FutureOperation, error) {
    49  	//
    50  	//	validator, ok := stakingkeeper.RandomValidator(r, sk, ctx)
    51  	//	if !ok {
    52  	//		return simulation.NoOpMsg(types.ModuleName), nil, nil // skip
    53  	//	}
    54  	//
    55  	//	simAccount, found := simulation.FindAccount(accs, sdk.AccAddress(validator.GetOperator()))
    56  	//	if !found {
    57  	//		return simulation.NoOpMsg(types.ModuleName), nil, nil // skip
    58  	//	}
    59  	//
    60  	//	if !validator.IsJailed() {
    61  	//		// TODO: due to this condition this message is almost, if not always, skipped !
    62  	//		return simulation.NoOpMsg(types.ModuleName), nil, nil
    63  	//	}
    64  	//
    65  	//	consAddr := sdk.ConsAddress(validator.GetConsPubKey().Address())
    66  	//	info, found := k.GetValidatorSigningInfo(ctx, consAddr)
    67  	//	if !found {
    68  	//		return simulation.NoOpMsg(types.ModuleName), nil, nil // skip
    69  	//	}
    70  	//
    71  	//	selfDel := sk.Delegation(ctx, simAccount.Address, validator.GetOperator())
    72  	//	if selfDel == nil {
    73  	//		return simulation.NoOpMsg(types.ModuleName), nil, nil // skip
    74  	//	}
    75  	//
    76  	//	account := ak.GetAccount(ctx, sdk.AccAddress(validator.GetOperator()))
    77  	//	fees, err := simulation.RandomFees(r, ctx, account.SpendableCoins(ctx.BlockTime()))
    78  	//	if err != nil {
    79  	//		return simulation.NoOpMsg(types.ModuleName), nil, err
    80  	//	}
    81  	//
    82  	//	msg := types.NewMsgUnjail(validator.GetOperator())
    83  	//
    84  	//	tx := helpers.GenTx(
    85  	//		[]sdk.Msg{msg},
    86  	//		fees,
    87  	//		helpers.DefaultGenTxGas,
    88  	//		chainID,
    89  	//		[]uint64{account.GetAccountNumber()},
    90  	//		[]uint64{account.GetSequence()},
    91  	//		simAccount.PrivKey,
    92  	//	)
    93  	//
    94  	//	_, res, err := app.Deliver(tx)
    95  	//
    96  	//	// result should fail if:
    97  	//	// - validator cannot be unjailed due to tombstone
    98  	//	// - validator is still in jailed period
    99  	//	// - self delegation too low
   100  	//	if info.Tombstoned ||
   101  	//		ctx.BlockHeader().Time.Before(info.JailedUntil) ||
   102  	//		validator.TokensFromShares(selfDel.GetShares()).TruncateInt().LT(validator.GetMinSelfDelegation()) {
   103  	//		if res != nil && err == nil {
   104  	//			if info.Tombstoned {
   105  	//				return simulation.NewOperationMsg(msg, true, ""), nil, errors.New("validator should not have been unjailed if validator tombstoned")
   106  	//			}
   107  	//			if ctx.BlockHeader().Time.Before(info.JailedUntil) {
   108  	//				return simulation.NewOperationMsg(msg, true, ""), nil, errors.New("validator unjailed while validator still in jail period")
   109  	//			}
   110  	//			if validator.TokensFromShares(selfDel.GetShares()).TruncateInt().LT(validator.GetMinSelfDelegation()) {
   111  	//				return simulation.NewOperationMsg(msg, true, ""), nil, errors.New("validator unjailed even though self-delegation too low")
   112  	//			}
   113  	//		}
   114  	//		// msg failed as expected
   115  	//		return simulation.NewOperationMsg(msg, false, ""), nil, nil
   116  	//	}
   117  	//
   118  	//	if err != nil {
   119  	//		return simulation.NoOpMsg(types.ModuleName), nil, errors.New(res.Log)
   120  	//	}
   121  	//
   122  	//	return simulation.NewOperationMsg(msg, true, ""), nil, nil
   123  	//}
   124  }