github.com/Finschia/finschia-sdk@v0.48.1/x/staking/simulation/genesis_test.go (about) 1 package simulation_test 2 3 import ( 4 "encoding/json" 5 "math/rand" 6 "testing" 7 8 "github.com/stretchr/testify/require" 9 10 "github.com/Finschia/finschia-sdk/codec" 11 codectypes "github.com/Finschia/finschia-sdk/codec/types" 12 cryptocodec "github.com/Finschia/finschia-sdk/crypto/codec" 13 "github.com/Finschia/finschia-sdk/types/module" 14 simtypes "github.com/Finschia/finschia-sdk/types/simulation" 15 "github.com/Finschia/finschia-sdk/x/staking/simulation" 16 "github.com/Finschia/finschia-sdk/x/staking/types" 17 ) 18 19 // TestRandomizedGenState tests the normal scenario of applying RandomizedGenState. 20 // Abonormal scenarios are not tested here. 21 func TestRandomizedGenState(t *testing.T) { 22 interfaceRegistry := codectypes.NewInterfaceRegistry() 23 cryptocodec.RegisterInterfaces(interfaceRegistry) 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 stakingGenesis types.GenesisState 42 simState.Cdc.MustUnmarshalJSON(simState.GenState[types.ModuleName], &stakingGenesis) 43 44 require.Equal(t, uint32(207), stakingGenesis.Params.MaxValidators) 45 require.Equal(t, uint32(7), stakingGenesis.Params.MaxEntries) 46 require.Equal(t, uint32(8687), stakingGenesis.Params.HistoricalEntries) 47 require.Equal(t, "stake", stakingGenesis.Params.BondDenom) 48 require.Equal(t, float64(238280), stakingGenesis.Params.UnbondingTime.Seconds()) 49 // check numbers of Delegations and Validators 50 require.Len(t, stakingGenesis.Delegations, 3) 51 require.Len(t, stakingGenesis.Validators, 3) 52 // check Delegations 53 require.Equal(t, "link1tnh2q55v8wyygtt9srz5safamzdengsn4hy6fe", stakingGenesis.Delegations[0].DelegatorAddress) 54 require.Equal(t, "linkvaloper1tnh2q55v8wyygtt9srz5safamzdengsn8rx882", stakingGenesis.Delegations[0].ValidatorAddress) 55 require.Equal(t, "1000.000000000000000000", stakingGenesis.Delegations[0].Shares.String()) 56 // check validators 57 require.Equal(t, "linkvaloper1ghekyjucln7y67ntx7cf27m9dpuxxemnjz9hqc", stakingGenesis.Validators[2].GetOperator().String()) 58 require.Equal(t, []byte{0xa, 0x20, 0x51, 0xde, 0xbd, 0xe8, 0xfa, 0xdf, 0x4e, 0xfc, 0x33, 0xa5, 0x16, 0x94, 0xf6, 0xee, 0xd3, 0x69, 0x7a, 0x7a, 0x1c, 0x2d, 0x50, 0xb6, 0x2, 0xf7, 0x16, 0x4e, 0x66, 0x9f, 0xff, 0x38, 0x91, 0x9b}, stakingGenesis.Validators[2].ConsensusPubkey.Value) 59 require.Equal(t, false, stakingGenesis.Validators[2].Jailed) 60 require.Equal(t, "BOND_STATUS_UNBONDED", stakingGenesis.Validators[2].Status.String()) 61 require.Equal(t, "1000", stakingGenesis.Validators[2].Tokens.String()) 62 require.Equal(t, "1000.000000000000000000", stakingGenesis.Validators[2].DelegatorShares.String()) 63 require.Equal(t, "0.292059246265731326", stakingGenesis.Validators[2].Commission.CommissionRates.Rate.String()) 64 require.Equal(t, "0.330000000000000000", stakingGenesis.Validators[2].Commission.CommissionRates.MaxRate.String()) 65 require.Equal(t, "0.038337453731274481", stakingGenesis.Validators[2].Commission.CommissionRates.MaxChangeRate.String()) 66 require.Equal(t, "1", stakingGenesis.Validators[2].MinSelfDelegation.String()) 67 } 68 69 // TestRandomizedGenState1 tests abnormal scenarios of applying RandomizedGenState. 70 func TestRandomizedGenState1(t *testing.T) { 71 interfaceRegistry := codectypes.NewInterfaceRegistry() 72 cdc := codec.NewProtoCodec(interfaceRegistry) 73 74 s := rand.NewSource(1) 75 r := rand.New(s) 76 // all these tests will panic 77 tests := []struct { 78 simState module.SimulationState 79 panicMsg string 80 }{ 81 { // panic => reason: incomplete initialization of the simState 82 module.SimulationState{}, "invalid memory address or nil pointer dereference"}, 83 { // panic => reason: incomplete initialization of the simState 84 module.SimulationState{ 85 AppParams: make(simtypes.AppParams), 86 Cdc: cdc, 87 Rand: r, 88 }, "invalid memory address or nil pointer dereference"}, 89 { 90 // panic => reason: numBonded != len(Accnounts) 91 module.SimulationState{ 92 AppParams: make(simtypes.AppParams), 93 Cdc: cdc, 94 Rand: r, 95 NumBonded: 4, 96 Accounts: simtypes.RandomAccounts(r, 3), 97 InitialStake: 1000, 98 GenState: make(map[string]json.RawMessage), 99 }, "invalid memory address or nil pointer dereference", 100 }, 101 } 102 103 for _, tt := range tests { 104 require.Panicsf(t, func() { simulation.RandomizedGenState(&tt.simState) }, tt.panicMsg) 105 } 106 }