github.com/cosmos/cosmos-sdk@v0.50.10/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 sdkmath "cosmossdk.io/math" 11 12 "github.com/cosmos/cosmos-sdk/codec" 13 codectypes "github.com/cosmos/cosmos-sdk/codec/types" 14 cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec" 15 sdk "github.com/cosmos/cosmos-sdk/types" 16 "github.com/cosmos/cosmos-sdk/types/module" 17 simtypes "github.com/cosmos/cosmos-sdk/types/simulation" 18 "github.com/cosmos/cosmos-sdk/x/staking/simulation" 19 "github.com/cosmos/cosmos-sdk/x/staking/types" 20 ) 21 22 // TestRandomizedGenState tests the normal scenario of applying RandomizedGenState. 23 // Abonormal scenarios are not tested here. 24 func TestRandomizedGenState(t *testing.T) { 25 interfaceRegistry := codectypes.NewInterfaceRegistry() 26 cryptocodec.RegisterInterfaces(interfaceRegistry) 27 cdc := codec.NewProtoCodec(interfaceRegistry) 28 29 s := rand.NewSource(1) 30 r := rand.New(s) 31 32 simState := module.SimulationState{ 33 AppParams: make(simtypes.AppParams), 34 Cdc: cdc, 35 Rand: r, 36 NumBonded: 3, 37 BondDenom: sdk.DefaultBondDenom, 38 Accounts: simtypes.RandomAccounts(r, 3), 39 InitialStake: sdkmath.NewInt(1000), 40 GenState: make(map[string]json.RawMessage), 41 } 42 43 simulation.RandomizedGenState(&simState) 44 45 var stakingGenesis types.GenesisState 46 simState.Cdc.MustUnmarshalJSON(simState.GenState[types.ModuleName], &stakingGenesis) 47 48 require.Equal(t, uint32(207), stakingGenesis.Params.MaxValidators) 49 require.Equal(t, uint32(7), stakingGenesis.Params.MaxEntries) 50 require.Equal(t, uint32(8687), stakingGenesis.Params.HistoricalEntries) 51 require.Equal(t, "stake", stakingGenesis.Params.BondDenom) 52 require.Equal(t, float64(238280), stakingGenesis.Params.UnbondingTime.Seconds()) 53 // check numbers of Delegations and Validators 54 require.Len(t, stakingGenesis.Delegations, 3) 55 require.Len(t, stakingGenesis.Validators, 3) 56 // check Delegations 57 require.Equal(t, "cosmos1tnh2q55v8wyygtt9srz5safamzdengsnqeycj3", stakingGenesis.Delegations[0].DelegatorAddress) 58 require.Equal(t, "cosmosvaloper1tnh2q55v8wyygtt9srz5safamzdengsn9dsd7z", stakingGenesis.Delegations[0].ValidatorAddress) 59 require.Equal(t, "1000.000000000000000000", stakingGenesis.Delegations[0].Shares.String()) 60 // check validators 61 require.Equal(t, "cosmosvaloper1ghekyjucln7y67ntx7cf27m9dpuxxemnsvnaes", stakingGenesis.Validators[2].GetOperator()) 62 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) 63 require.Equal(t, false, stakingGenesis.Validators[2].Jailed) 64 require.Equal(t, "BOND_STATUS_UNBONDED", stakingGenesis.Validators[2].Status.String()) 65 require.Equal(t, "1000", stakingGenesis.Validators[2].Tokens.String()) 66 require.Equal(t, "1000.000000000000000000", stakingGenesis.Validators[2].DelegatorShares.String()) 67 require.Equal(t, "0.292059246265731326", stakingGenesis.Validators[2].Commission.CommissionRates.Rate.String()) 68 require.Equal(t, "0.330000000000000000", stakingGenesis.Validators[2].Commission.CommissionRates.MaxRate.String()) 69 require.Equal(t, "0.038337453731274481", stakingGenesis.Validators[2].Commission.CommissionRates.MaxChangeRate.String()) 70 require.Equal(t, "1", stakingGenesis.Validators[2].MinSelfDelegation.String()) 71 } 72 73 // TestRandomizedGenState1 tests abnormal scenarios of applying RandomizedGenState. 74 func TestRandomizedGenState1(t *testing.T) { 75 interfaceRegistry := codectypes.NewInterfaceRegistry() 76 cdc := codec.NewProtoCodec(interfaceRegistry) 77 78 s := rand.NewSource(1) 79 r := rand.New(s) 80 // all these tests will panic 81 tests := []struct { 82 simState module.SimulationState 83 panicMsg string 84 }{ 85 { // panic => reason: incomplete initialization of the simState 86 module.SimulationState{}, "invalid memory address or nil pointer dereference"}, 87 { // panic => reason: incomplete initialization of the simState 88 module.SimulationState{ 89 AppParams: make(simtypes.AppParams), 90 Cdc: cdc, 91 Rand: r, 92 }, "invalid memory address or nil pointer dereference"}, 93 { 94 // panic => reason: numBonded != len(Accnounts) 95 module.SimulationState{ 96 AppParams: make(simtypes.AppParams), 97 Cdc: cdc, 98 Rand: r, 99 NumBonded: 4, 100 Accounts: simtypes.RandomAccounts(r, 3), 101 InitialStake: sdkmath.NewInt(1000), 102 GenState: make(map[string]json.RawMessage), 103 }, "invalid memory address or nil pointer dereference", 104 }, 105 } 106 107 for _, tt := range tests { 108 require.Panicsf(t, func() { simulation.RandomizedGenState(&tt.simState) }, tt.panicMsg) 109 } 110 }