From cb3b9ad09e0fd6aed43806abeba502f7e4287aee Mon Sep 17 00:00:00 2001 From: DauTT Date: Sun, 12 Jul 2020 15:37:53 +0200 Subject: [PATCH 1/2] x/slashing/simulation/genesis.go: add unit tests --- x/slashing/simulation/genesis_test.go | 80 +++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 x/slashing/simulation/genesis_test.go diff --git a/x/slashing/simulation/genesis_test.go b/x/slashing/simulation/genesis_test.go new file mode 100644 index 000000000000..e384f9a043f0 --- /dev/null +++ b/x/slashing/simulation/genesis_test.go @@ -0,0 +1,80 @@ +package simulation_test + +import ( + "encoding/json" + "math/rand" + "testing" + "time" + + "github.com/stretchr/testify/require" + + "github.com/cosmos/cosmos-sdk/codec" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/types/module" + simtypes "github.com/cosmos/cosmos-sdk/types/simulation" + "github.com/cosmos/cosmos-sdk/x/slashing/simulation" + "github.com/cosmos/cosmos-sdk/x/slashing/types" +) + +// TestRandomizedGenState tests the normal scenario of applying RandomizedGenState. +// Abonormal scenarios are not tested here. +func TestRandomizedGenState(t *testing.T) { + cdc := codec.New() + s := rand.NewSource(1) + r := rand.New(s) + + simState := module.SimulationState{ + AppParams: make(simtypes.AppParams), + Cdc: cdc, + Rand: r, + NumBonded: 3, + Accounts: simtypes.RandomAccounts(r, 3), + InitialStake: 1000, + GenState: make(map[string]json.RawMessage), + } + + simulation.RandomizedGenState(&simState) + + var stakingGenesis types.GenesisState + simState.Cdc.MustUnmarshalJSON(simState.GenState[types.ModuleName], &stakingGenesis) + + dec1, _ := sdk.NewDecFromStr("0.600000000000000000") + dec2, _ := sdk.NewDecFromStr("0.022222222222222222") + dec3, _ := sdk.NewDecFromStr("0.008928571428571429") + + require.Equal(t, dec1, stakingGenesis.Params.MinSignedPerWindow) + require.Equal(t, dec2, stakingGenesis.Params.SlashFractionDoubleSign) + require.Equal(t, dec3, stakingGenesis.Params.SlashFractionDowntime) + require.Equal(t, int64(720), stakingGenesis.Params.SignedBlocksWindow) + require.Equal(t, time.Duration(34800000000000), stakingGenesis.Params.DowntimeJailDuration) + require.Len(t, stakingGenesis.MissedBlocks, 0) + require.Len(t, stakingGenesis.SigningInfos, 0) + +} + +// TestRandomizedGenState tests abnormal scenarios of applying RandomizedGenState. +func TestRandomizedGenState1(t *testing.T) { + cdc := codec.New() + + s := rand.NewSource(1) + r := rand.New(s) + + // all these tests will panic + tests := []struct { + simState module.SimulationState + panicMsg string + }{ + { // panic => reason: incomplete initialization of the simState + module.SimulationState{}, "invalid memory address or nil pointer dereference"}, + { // panic => reason: incomplete initialization of the simState + module.SimulationState{ + AppParams: make(simtypes.AppParams), + Cdc: cdc, + Rand: r, + }, "assignment to entry in nil map"}, + } + + for _, tt := range tests { + require.Panicsf(t, func() { simulation.RandomizedGenState(&tt.simState) }, tt.panicMsg) + } +} From 7ad508d95095c3870cc7d2c976fe437b894911ae Mon Sep 17 00:00:00 2001 From: DauTT Date: Sun, 12 Jul 2020 23:31:23 +0200 Subject: [PATCH 2/2] rename variable stakingGenesis to slashingGenesis --- x/slashing/simulation/genesis_test.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/x/slashing/simulation/genesis_test.go b/x/slashing/simulation/genesis_test.go index e384f9a043f0..e9604dad7805 100644 --- a/x/slashing/simulation/genesis_test.go +++ b/x/slashing/simulation/genesis_test.go @@ -35,20 +35,20 @@ func TestRandomizedGenState(t *testing.T) { simulation.RandomizedGenState(&simState) - var stakingGenesis types.GenesisState - simState.Cdc.MustUnmarshalJSON(simState.GenState[types.ModuleName], &stakingGenesis) + var slashingGenesis types.GenesisState + simState.Cdc.MustUnmarshalJSON(simState.GenState[types.ModuleName], &slashingGenesis) dec1, _ := sdk.NewDecFromStr("0.600000000000000000") dec2, _ := sdk.NewDecFromStr("0.022222222222222222") dec3, _ := sdk.NewDecFromStr("0.008928571428571429") - require.Equal(t, dec1, stakingGenesis.Params.MinSignedPerWindow) - require.Equal(t, dec2, stakingGenesis.Params.SlashFractionDoubleSign) - require.Equal(t, dec3, stakingGenesis.Params.SlashFractionDowntime) - require.Equal(t, int64(720), stakingGenesis.Params.SignedBlocksWindow) - require.Equal(t, time.Duration(34800000000000), stakingGenesis.Params.DowntimeJailDuration) - require.Len(t, stakingGenesis.MissedBlocks, 0) - require.Len(t, stakingGenesis.SigningInfos, 0) + require.Equal(t, dec1, slashingGenesis.Params.MinSignedPerWindow) + require.Equal(t, dec2, slashingGenesis.Params.SlashFractionDoubleSign) + require.Equal(t, dec3, slashingGenesis.Params.SlashFractionDowntime) + require.Equal(t, int64(720), slashingGenesis.Params.SignedBlocksWindow) + require.Equal(t, time.Duration(34800000000000), slashingGenesis.Params.DowntimeJailDuration) + require.Len(t, slashingGenesis.MissedBlocks, 0) + require.Len(t, slashingGenesis.SigningInfos, 0) }