github.com/Finschia/finschia-sdk@v0.48.1/x/slashing/keeper/signing_info_test.go (about) 1 package keeper_test 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/stretchr/testify/require" 8 tmproto "github.com/tendermint/tendermint/proto/tendermint/types" 9 10 "github.com/Finschia/finschia-sdk/simapp" 11 sdk "github.com/Finschia/finschia-sdk/types" 12 "github.com/Finschia/finschia-sdk/x/slashing/types" 13 ) 14 15 func TestGetSetValidatorSigningInfo(t *testing.T) { 16 app := simapp.Setup(false) 17 ctx := app.BaseApp.NewContext(false, tmproto.Header{}) 18 addrDels := simapp.AddTestAddrsIncremental(app, ctx, 1, app.StakingKeeper.TokensFromConsensusPower(ctx, 200)) 19 20 info, found := app.SlashingKeeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[0])) 21 require.False(t, found) 22 newInfo := types.NewValidatorSigningInfo( 23 sdk.ConsAddress(addrDels[0]), 24 int64(4), 25 int64(3), 26 time.Unix(2, 0), 27 false, 28 int64(10), 29 ) 30 app.SlashingKeeper.SetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[0]), newInfo) 31 info, found = app.SlashingKeeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[0])) 32 require.True(t, found) 33 require.Equal(t, info.StartHeight, int64(4)) 34 require.Equal(t, info.IndexOffset, int64(3)) 35 require.Equal(t, info.JailedUntil, time.Unix(2, 0).UTC()) 36 require.Equal(t, info.MissedBlocksCounter, int64(10)) 37 } 38 39 func TestGetSetValidatorMissedBlockBitArray(t *testing.T) { 40 app := simapp.Setup(false) 41 ctx := app.BaseApp.NewContext(false, tmproto.Header{}) 42 addrDels := simapp.AddTestAddrsIncremental(app, ctx, 1, app.StakingKeeper.TokensFromConsensusPower(ctx, 200)) 43 44 missed := app.SlashingKeeper.GetValidatorMissedBlockBitArray(ctx, sdk.ConsAddress(addrDels[0]), 0) 45 require.False(t, missed) // treat empty key as not missed 46 app.SlashingKeeper.SetValidatorMissedBlockBitArray(ctx, sdk.ConsAddress(addrDels[0]), 0, true) 47 missed = app.SlashingKeeper.GetValidatorMissedBlockBitArray(ctx, sdk.ConsAddress(addrDels[0]), 0) 48 require.True(t, missed) // now should be missed 49 } 50 51 func TestTombstoned(t *testing.T) { 52 app := simapp.Setup(false) 53 ctx := app.BaseApp.NewContext(false, tmproto.Header{}) 54 addrDels := simapp.AddTestAddrsIncremental(app, ctx, 1, app.StakingKeeper.TokensFromConsensusPower(ctx, 200)) 55 56 require.Panics(t, func() { app.SlashingKeeper.Tombstone(ctx, sdk.ConsAddress(addrDels[0])) }) 57 require.False(t, app.SlashingKeeper.IsTombstoned(ctx, sdk.ConsAddress(addrDels[0]))) 58 59 newInfo := types.NewValidatorSigningInfo( 60 sdk.ConsAddress(addrDels[0]), 61 int64(4), 62 int64(3), 63 time.Unix(2, 0), 64 false, 65 int64(10), 66 ) 67 app.SlashingKeeper.SetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[0]), newInfo) 68 69 require.False(t, app.SlashingKeeper.IsTombstoned(ctx, sdk.ConsAddress(addrDels[0]))) 70 app.SlashingKeeper.Tombstone(ctx, sdk.ConsAddress(addrDels[0])) 71 require.True(t, app.SlashingKeeper.IsTombstoned(ctx, sdk.ConsAddress(addrDels[0]))) 72 require.Panics(t, func() { app.SlashingKeeper.Tombstone(ctx, sdk.ConsAddress(addrDels[0])) }) 73 } 74 75 func TestJailUntil(t *testing.T) { 76 app := simapp.Setup(false) 77 ctx := app.BaseApp.NewContext(false, tmproto.Header{}) 78 addrDels := simapp.AddTestAddrsIncremental(app, ctx, 1, app.StakingKeeper.TokensFromConsensusPower(ctx, 200)) 79 80 require.Panics(t, func() { app.SlashingKeeper.JailUntil(ctx, sdk.ConsAddress(addrDels[0]), time.Now()) }) 81 82 newInfo := types.NewValidatorSigningInfo( 83 sdk.ConsAddress(addrDels[0]), 84 int64(4), 85 int64(3), 86 time.Unix(2, 0), 87 false, 88 int64(10), 89 ) 90 app.SlashingKeeper.SetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[0]), newInfo) 91 app.SlashingKeeper.JailUntil(ctx, sdk.ConsAddress(addrDels[0]), time.Unix(253402300799, 0).UTC()) 92 93 info, ok := app.SlashingKeeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[0])) 94 require.True(t, ok) 95 require.Equal(t, time.Unix(253402300799, 0).UTC(), info.JailedUntil) 96 }