From 2b95d5317e3a244af7ffcd57e80be190fb438edf Mon Sep 17 00:00:00 2001 From: Jonathan Gimeno Date: Tue, 3 Mar 2020 11:20:18 +0100 Subject: [PATCH] refactor signing info test --- x/slashing/keeper/old_signing_info_test.go | 61 ---------------------- x/slashing/keeper/signing_info_test.go | 59 +++++++++++++++++++++ 2 files changed, 59 insertions(+), 61 deletions(-) delete mode 100644 x/slashing/keeper/old_signing_info_test.go diff --git a/x/slashing/keeper/old_signing_info_test.go b/x/slashing/keeper/old_signing_info_test.go deleted file mode 100644 index 1e4347b22..000000000 --- a/x/slashing/keeper/old_signing_info_test.go +++ /dev/null @@ -1,61 +0,0 @@ -package keeper - -import ( - "testing" - "time" - - "github.com/stretchr/testify/require" - - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/slashing/types" -) - -func TestGetSetValidatorMissedBlockBitArray(t *testing.T) { - ctx, _, _, _, keeper := CreateTestInput(t, types.DefaultParams()) - missed := keeper.GetValidatorMissedBlockBitArray(ctx, sdk.ConsAddress(Addrs[0]), 0) - require.False(t, missed) // treat empty key as not missed - keeper.SetValidatorMissedBlockBitArray(ctx, sdk.ConsAddress(Addrs[0]), 0, true) - missed = keeper.GetValidatorMissedBlockBitArray(ctx, sdk.ConsAddress(Addrs[0]), 0) - require.True(t, missed) // now should be missed -} - -func TestTombstoned(t *testing.T) { - ctx, _, _, _, keeper := CreateTestInput(t, types.DefaultParams()) - require.Panics(t, func() { keeper.Tombstone(ctx, sdk.ConsAddress(Addrs[0])) }) - require.False(t, keeper.IsTombstoned(ctx, sdk.ConsAddress(Addrs[0]))) - - newInfo := types.NewValidatorSigningInfo( - sdk.ConsAddress(Addrs[0]), - int64(4), - int64(3), - time.Unix(2, 0), - false, - int64(10), - ) - keeper.SetValidatorSigningInfo(ctx, sdk.ConsAddress(Addrs[0]), newInfo) - - require.False(t, keeper.IsTombstoned(ctx, sdk.ConsAddress(Addrs[0]))) - keeper.Tombstone(ctx, sdk.ConsAddress(Addrs[0])) - require.True(t, keeper.IsTombstoned(ctx, sdk.ConsAddress(Addrs[0]))) - require.Panics(t, func() { keeper.Tombstone(ctx, sdk.ConsAddress(Addrs[0])) }) -} - -func TestJailUntil(t *testing.T) { - ctx, _, _, _, keeper := CreateTestInput(t, types.DefaultParams()) - require.Panics(t, func() { keeper.JailUntil(ctx, sdk.ConsAddress(Addrs[0]), time.Now()) }) - - newInfo := types.NewValidatorSigningInfo( - sdk.ConsAddress(Addrs[0]), - int64(4), - int64(3), - time.Unix(2, 0), - false, - int64(10), - ) - keeper.SetValidatorSigningInfo(ctx, sdk.ConsAddress(Addrs[0]), newInfo) - keeper.JailUntil(ctx, sdk.ConsAddress(Addrs[0]), time.Unix(253402300799, 0).UTC()) - - info, ok := keeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(Addrs[0])) - require.True(t, ok) - require.Equal(t, time.Unix(253402300799, 0).UTC(), info.JailedUntil) -} diff --git a/x/slashing/keeper/signing_info_test.go b/x/slashing/keeper/signing_info_test.go index c58f4f900..e10df926c 100644 --- a/x/slashing/keeper/signing_info_test.go +++ b/x/slashing/keeper/signing_info_test.go @@ -36,3 +36,62 @@ func TestGetSetValidatorSigningInfo(t *testing.T) { require.Equal(t, info.JailedUntil, time.Unix(2, 0).UTC()) require.Equal(t, info.MissedBlocksCounter, int64(10)) } + +func TestGetSetValidatorMissedBlockBitArray(t *testing.T) { + app := simapp.Setup(false) + ctx := app.BaseApp.NewContext(false, abci.Header{}) + addrDels := simapp.AddTestAddrsIncremental(app, ctx, 1, sdk.TokensFromConsensusPower(200)) + + missed := app.SlashingKeeper.GetValidatorMissedBlockBitArray(ctx, sdk.ConsAddress(addrDels[0]), 0) + require.False(t, missed) // treat empty key as not missed + app.SlashingKeeper.SetValidatorMissedBlockBitArray(ctx, sdk.ConsAddress(addrDels[0]), 0, true) + missed = app.SlashingKeeper.GetValidatorMissedBlockBitArray(ctx, sdk.ConsAddress(addrDels[0]), 0) + require.True(t, missed) // now should be missed +} + +func TestTombstoned(t *testing.T) { + app := simapp.Setup(false) + ctx := app.BaseApp.NewContext(false, abci.Header{}) + addrDels := simapp.AddTestAddrsIncremental(app, ctx, 1, sdk.TokensFromConsensusPower(200)) + + require.Panics(t, func() { app.SlashingKeeper.Tombstone(ctx, sdk.ConsAddress(addrDels[0])) }) + require.False(t, app.SlashingKeeper.IsTombstoned(ctx, sdk.ConsAddress(addrDels[0]))) + + newInfo := types.NewValidatorSigningInfo( + sdk.ConsAddress(addrDels[0]), + int64(4), + int64(3), + time.Unix(2, 0), + false, + int64(10), + ) + app.SlashingKeeper.SetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[0]), newInfo) + + require.False(t, app.SlashingKeeper.IsTombstoned(ctx, sdk.ConsAddress(addrDels[0]))) + app.SlashingKeeper.Tombstone(ctx, sdk.ConsAddress(addrDels[0])) + require.True(t, app.SlashingKeeper.IsTombstoned(ctx, sdk.ConsAddress(addrDels[0]))) + require.Panics(t, func() { app.SlashingKeeper.Tombstone(ctx, sdk.ConsAddress(addrDels[0])) }) +} + +func TestJailUntil(t *testing.T) { + app := simapp.Setup(false) + ctx := app.BaseApp.NewContext(false, abci.Header{}) + addrDels := simapp.AddTestAddrsIncremental(app, ctx, 1, sdk.TokensFromConsensusPower(200)) + + require.Panics(t, func() { app.SlashingKeeper.JailUntil(ctx, sdk.ConsAddress(addrDels[0]), time.Now()) }) + + newInfo := types.NewValidatorSigningInfo( + sdk.ConsAddress(addrDels[0]), + int64(4), + int64(3), + time.Unix(2, 0), + false, + int64(10), + ) + app.SlashingKeeper.SetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[0]), newInfo) + app.SlashingKeeper.JailUntil(ctx, sdk.ConsAddress(addrDels[0]), time.Unix(253402300799, 0).UTC()) + + info, ok := app.SlashingKeeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[0])) + require.True(t, ok) + require.Equal(t, time.Unix(253402300799, 0).UTC(), info.JailedUntil) +}