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