github.com/fibonacci-chain/fbc@v0.0.0-20231124064014-c7636198c1e9/libs/cosmos-sdk/x/slashing/abci_test.go (about) 1 package slashing 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/stretchr/testify/require" 8 9 abci "github.com/fibonacci-chain/fbc/libs/tendermint/abci/types" 10 11 sdk "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/types" 12 slashingkeeper "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/slashing/internal/keeper" 13 "github.com/fibonacci-chain/fbc/libs/cosmos-sdk/x/staking" 14 ) 15 16 func TestBeginBlocker(t *testing.T) { 17 ctx, ck, sk, _, keeper := slashingkeeper.CreateTestInput(t, DefaultParams()) 18 power := int64(100) 19 amt := sdk.TokensFromConsensusPower(power) 20 addr, pk := slashingkeeper.Addrs[2], slashingkeeper.Pks[2] 21 22 // bond the validator 23 res, err := staking.NewHandler(sk)(ctx, slashingkeeper.NewTestMsgCreateValidator(addr, pk, amt)) 24 require.NoError(t, err) 25 require.NotNil(t, res) 26 27 staking.EndBlocker(ctx, sk) 28 require.Equal( 29 t, ck.GetCoins(ctx, sdk.AccAddress(addr)), 30 sdk.NewCoins(sdk.NewCoin(sk.GetParams(ctx).BondDenom, slashingkeeper.InitTokens.Sub(amt))), 31 ) 32 require.Equal(t, amt, sk.Validator(ctx, addr).GetBondedTokens()) 33 34 val := abci.Validator{ 35 Address: pk.Address(), 36 Power: amt.ToDec().Int64(), 37 } 38 39 // mark the validator as having signed 40 req := abci.RequestBeginBlock{ 41 LastCommitInfo: abci.LastCommitInfo{ 42 Votes: []abci.VoteInfo{{ 43 Validator: val, 44 SignedLastBlock: true, 45 }}, 46 }, 47 } 48 BeginBlocker(ctx, req, keeper) 49 50 info, found := keeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(pk.Address())) 51 require.True(t, found) 52 require.Equal(t, ctx.BlockHeight(), info.StartHeight) 53 require.Equal(t, int64(1), info.IndexOffset) 54 require.Equal(t, time.Unix(0, 0).UTC(), info.JailedUntil) 55 require.Equal(t, int64(0), info.MissedBlocksCounter) 56 57 height := int64(0) 58 59 // for 1000 blocks, mark the validator as having signed 60 for ; height < keeper.SignedBlocksWindow(ctx); height++ { 61 ctx.SetBlockHeight(height) 62 req = abci.RequestBeginBlock{ 63 LastCommitInfo: abci.LastCommitInfo{ 64 Votes: []abci.VoteInfo{{ 65 Validator: val, 66 SignedLastBlock: true, 67 }}, 68 }, 69 } 70 BeginBlocker(ctx, req, keeper) 71 } 72 73 // for 500 blocks, mark the validator as having not signed 74 for ; height < ((keeper.SignedBlocksWindow(ctx) * 2) - keeper.MinSignedPerWindow(ctx) + 1); height++ { 75 ctx.SetBlockHeight(height) 76 req = abci.RequestBeginBlock{ 77 LastCommitInfo: abci.LastCommitInfo{ 78 Votes: []abci.VoteInfo{{ 79 Validator: val, 80 SignedLastBlock: false, 81 }}, 82 }, 83 } 84 BeginBlocker(ctx, req, keeper) 85 } 86 87 // end block 88 staking.EndBlocker(ctx, sk) 89 90 // validator should be jailed 91 validator, found := sk.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(pk)) 92 require.True(t, found) 93 require.Equal(t, sdk.Unbonding, validator.GetStatus()) 94 }