github.com/cosmos/cosmos-sdk@v0.50.10/x/slashing/simulation/genesis_test.go (about)

     1  package simulation_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"math/rand"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/stretchr/testify/require"
    10  
    11  	sdkmath "cosmossdk.io/math"
    12  
    13  	"github.com/cosmos/cosmos-sdk/codec"
    14  	codectypes "github.com/cosmos/cosmos-sdk/codec/types"
    15  	"github.com/cosmos/cosmos-sdk/types/module"
    16  	simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
    17  	"github.com/cosmos/cosmos-sdk/x/slashing/simulation"
    18  	"github.com/cosmos/cosmos-sdk/x/slashing/types"
    19  )
    20  
    21  // TestRandomizedGenState tests the normal scenario of applying RandomizedGenState.
    22  // Abonormal scenarios are not tested here.
    23  func TestRandomizedGenState(t *testing.T) {
    24  	interfaceRegistry := codectypes.NewInterfaceRegistry()
    25  	cdc := codec.NewProtoCodec(interfaceRegistry)
    26  
    27  	s := rand.NewSource(1)
    28  	r := rand.New(s)
    29  
    30  	simState := module.SimulationState{
    31  		AppParams:    make(simtypes.AppParams),
    32  		Cdc:          cdc,
    33  		Rand:         r,
    34  		NumBonded:    3,
    35  		Accounts:     simtypes.RandomAccounts(r, 3),
    36  		InitialStake: sdkmath.NewInt(1000),
    37  		GenState:     make(map[string]json.RawMessage),
    38  	}
    39  
    40  	simulation.RandomizedGenState(&simState)
    41  
    42  	var slashingGenesis types.GenesisState
    43  	simState.Cdc.MustUnmarshalJSON(simState.GenState[types.ModuleName], &slashingGenesis)
    44  
    45  	dec1, _ := sdkmath.LegacyNewDecFromStr("0.600000000000000000")
    46  	dec2, _ := sdkmath.LegacyNewDecFromStr("0.022222222222222222")
    47  	dec3, _ := sdkmath.LegacyNewDecFromStr("0.008928571428571429")
    48  
    49  	require.Equal(t, dec1, slashingGenesis.Params.MinSignedPerWindow)
    50  	require.Equal(t, dec2, slashingGenesis.Params.SlashFractionDoubleSign)
    51  	require.Equal(t, dec3, slashingGenesis.Params.SlashFractionDowntime)
    52  	require.Equal(t, int64(720), slashingGenesis.Params.SignedBlocksWindow)
    53  	require.Equal(t, time.Duration(34800000000000), slashingGenesis.Params.DowntimeJailDuration)
    54  	require.Len(t, slashingGenesis.MissedBlocks, 0)
    55  	require.Len(t, slashingGenesis.SigningInfos, 0)
    56  }
    57  
    58  // TestRandomizedGenState tests abnormal scenarios of applying RandomizedGenState.
    59  func TestRandomizedGenState1(t *testing.T) {
    60  	interfaceRegistry := codectypes.NewInterfaceRegistry()
    61  	cdc := codec.NewProtoCodec(interfaceRegistry)
    62  
    63  	s := rand.NewSource(1)
    64  	r := rand.New(s)
    65  
    66  	// all these tests will panic
    67  	tests := []struct {
    68  		simState module.SimulationState
    69  		panicMsg string
    70  	}{
    71  		{ // panic => reason: incomplete initialization of the simState
    72  			module.SimulationState{}, "invalid memory address or nil pointer dereference"},
    73  		{ // panic => reason: incomplete initialization of the simState
    74  			module.SimulationState{
    75  				AppParams: make(simtypes.AppParams),
    76  				Cdc:       cdc,
    77  				Rand:      r,
    78  			}, "assignment to entry in nil map"},
    79  	}
    80  
    81  	for _, tt := range tests {
    82  		require.Panicsf(t, func() { simulation.RandomizedGenState(&tt.simState) }, tt.panicMsg)
    83  	}
    84  }