github.com/Finschia/finschia-sdk@v0.48.1/x/slashing/simulation/operations_test.go (about)

     1  package simulation_test
     2  
     3  import (
     4  	"math/rand"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/require"
     9  	tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
    10  
    11  	ocabci "github.com/Finschia/ostracon/abci/types"
    12  
    13  	"github.com/Finschia/finschia-sdk/simapp"
    14  	simappparams "github.com/Finschia/finschia-sdk/simapp/params"
    15  	sdk "github.com/Finschia/finschia-sdk/types"
    16  	simtypes "github.com/Finschia/finschia-sdk/types/simulation"
    17  	distrtypes "github.com/Finschia/finschia-sdk/x/distribution/types"
    18  	minttypes "github.com/Finschia/finschia-sdk/x/mint/types"
    19  	"github.com/Finschia/finschia-sdk/x/slashing/simulation"
    20  	"github.com/Finschia/finschia-sdk/x/slashing/types"
    21  	stakingtypes "github.com/Finschia/finschia-sdk/x/staking/types"
    22  )
    23  
    24  // TestWeightedOperations tests the weights of the operations.
    25  func TestWeightedOperations(t *testing.T) {
    26  	app, ctx := createTestApp(false)
    27  	ctx.WithChainID("test-chain")
    28  
    29  	cdc := app.AppCodec()
    30  	appParams := make(simtypes.AppParams)
    31  
    32  	s := rand.NewSource(1)
    33  	r := rand.New(s)
    34  	accs := simtypes.RandomAccounts(r, 3)
    35  
    36  	expected := []struct {
    37  		weight     int
    38  		opMsgRoute string
    39  		opMsgName  string
    40  	}{{simappparams.DefaultWeightMsgUnjail, types.ModuleName, types.TypeMsgUnjail}}
    41  
    42  	weightesOps := simulation.WeightedOperations(appParams, cdc, app.AccountKeeper, app.BankKeeper, app.SlashingKeeper, app.StakingKeeper)
    43  	for i, w := range weightesOps {
    44  		operationMsg, _, _ := w.Op()(r, app.BaseApp, ctx, accs, ctx.ChainID())
    45  		// the following checks are very much dependent from the ordering of the output given
    46  		// by WeightedOperations. if the ordering in WeightedOperations changes some tests
    47  		// will fail
    48  		require.Equal(t, expected[i].weight, w.Weight(), "weight should be the same")
    49  		require.Equal(t, expected[i].opMsgRoute, operationMsg.Route, "route should be the same")
    50  		require.Equal(t, expected[i].opMsgName, operationMsg.Name, "operation Msg name should be the same")
    51  	}
    52  }
    53  
    54  // TestSimulateMsgUnjail tests the normal scenario of a valid message of type types.MsgUnjail.
    55  // Abonormal scenarios, where the message is created by an errors, are not tested here.
    56  func TestSimulateMsgUnjail(t *testing.T) {
    57  	app, ctx := createTestApp(false)
    58  	blockTime := time.Now().UTC()
    59  	ctx = ctx.WithBlockTime(blockTime)
    60  
    61  	// setup 3 accounts
    62  	s := rand.NewSource(1)
    63  	r := rand.New(s)
    64  	accounts := getTestingAccounts(t, r, app, ctx, 3)
    65  
    66  	// setup accounts[0] as validator0
    67  	validator0 := getTestingValidator0(t, app, ctx, accounts)
    68  
    69  	// setup validator0 by consensus address
    70  	app.StakingKeeper.SetValidatorByConsAddr(ctx, validator0)
    71  	val0ConsAddress, err := validator0.GetConsAddr()
    72  	require.NoError(t, err)
    73  	info := types.NewValidatorSigningInfo(val0ConsAddress, int64(4), int64(3),
    74  		time.Unix(2, 0), false, int64(10))
    75  	app.SlashingKeeper.SetValidatorSigningInfo(ctx, val0ConsAddress, info)
    76  
    77  	// put validator0 in jail
    78  	app.StakingKeeper.Jail(ctx, val0ConsAddress)
    79  
    80  	// setup self delegation
    81  	delTokens := app.StakingKeeper.TokensFromConsensusPower(ctx, 2)
    82  	validator0, issuedShares := validator0.AddTokensFromDel(delTokens)
    83  	val0AccAddress, err := sdk.ValAddressFromBech32(validator0.OperatorAddress)
    84  	require.NoError(t, err)
    85  	selfDelegation := stakingtypes.NewDelegation(val0AccAddress.Bytes(), validator0.GetOperator(), issuedShares)
    86  	app.StakingKeeper.SetDelegation(ctx, selfDelegation)
    87  	app.DistrKeeper.SetDelegatorStartingInfo(ctx, validator0.GetOperator(), val0AccAddress.Bytes(), distrtypes.NewDelegatorStartingInfo(2, sdk.OneDec(), 200))
    88  
    89  	// begin a new block
    90  	app.BeginBlock(ocabci.RequestBeginBlock{Header: tmproto.Header{Height: app.LastBlockHeight() + 1, AppHash: app.LastCommitID().Hash, Time: blockTime}})
    91  
    92  	// execute operation
    93  	op := simulation.SimulateMsgUnjail(app.AccountKeeper, app.BankKeeper, app.SlashingKeeper, app.StakingKeeper)
    94  	operationMsg, futureOperations, err := op(r, app.BaseApp, ctx, accounts, "")
    95  	require.NoError(t, err)
    96  
    97  	var msg types.MsgUnjail
    98  	types.ModuleCdc.UnmarshalJSON(operationMsg.Msg, &msg)
    99  
   100  	require.True(t, operationMsg.OK)
   101  	require.Equal(t, types.TypeMsgUnjail, msg.Type())
   102  	require.Equal(t, "linkvaloper1tnh2q55v8wyygtt9srz5safamzdengsn8rx882", msg.ValidatorAddr)
   103  	require.Len(t, futureOperations, 0)
   104  }
   105  
   106  // returns context and an app with updated mint keeper
   107  func createTestApp(isCheckTx bool) (*simapp.SimApp, sdk.Context) {
   108  	app := simapp.Setup(isCheckTx)
   109  
   110  	ctx := app.BaseApp.NewContext(isCheckTx, tmproto.Header{})
   111  	app.MintKeeper.SetParams(ctx, minttypes.DefaultParams())
   112  	app.MintKeeper.SetMinter(ctx, minttypes.DefaultInitialMinter())
   113  
   114  	return app, ctx
   115  }
   116  
   117  func getTestingAccounts(t *testing.T, r *rand.Rand, app *simapp.SimApp, ctx sdk.Context, n int) []simtypes.Account {
   118  	accounts := simtypes.RandomAccounts(r, n)
   119  
   120  	initAmt := app.StakingKeeper.TokensFromConsensusPower(ctx, 200)
   121  	initCoins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, initAmt))
   122  
   123  	// add coins to the accounts
   124  	for _, account := range accounts {
   125  		acc := app.AccountKeeper.NewAccountWithAddress(ctx, account.Address)
   126  		app.AccountKeeper.SetAccount(ctx, acc)
   127  		require.NoError(t, simapp.FundAccount(app, ctx, account.Address, initCoins))
   128  	}
   129  
   130  	return accounts
   131  }
   132  
   133  func getTestingValidator0(t *testing.T, app *simapp.SimApp, ctx sdk.Context, accounts []simtypes.Account) stakingtypes.Validator {
   134  	commission0 := stakingtypes.NewCommission(sdk.ZeroDec(), sdk.OneDec(), sdk.OneDec())
   135  	return getTestingValidator(t, app, ctx, accounts, commission0, 0)
   136  }
   137  
   138  func getTestingValidator(t *testing.T, app *simapp.SimApp, ctx sdk.Context, accounts []simtypes.Account, commission stakingtypes.Commission, n int) stakingtypes.Validator {
   139  	account := accounts[n]
   140  	valPubKey := account.ConsKey.PubKey()
   141  	valAddr := sdk.ValAddress(account.PubKey.Address().Bytes())
   142  	validator, err := stakingtypes.NewValidator(valAddr, valPubKey, stakingtypes.Description{})
   143  	require.NoError(t, err)
   144  	validator, err = validator.SetInitialCommission(commission)
   145  	require.NoError(t, err)
   146  
   147  	validator.DelegatorShares = sdk.NewDec(100)
   148  	validator.Tokens = sdk.NewInt(1000000)
   149  
   150  	app.StakingKeeper.SetValidator(ctx, validator)
   151  
   152  	return validator
   153  }