github.com/Finschia/finschia-sdk@v0.49.1/x/slashing/abci_test.go (about)

     1  package slashing_test
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	ocabci "github.com/Finschia/ostracon/abci/types"
     8  	"github.com/stretchr/testify/require"
     9  	abci "github.com/tendermint/tendermint/abci/types"
    10  	tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
    11  
    12  	"github.com/Finschia/finschia-sdk/simapp"
    13  	sdk "github.com/Finschia/finschia-sdk/types"
    14  	"github.com/Finschia/finschia-sdk/x/slashing"
    15  	"github.com/Finschia/finschia-sdk/x/staking"
    16  	"github.com/Finschia/finschia-sdk/x/staking/teststaking"
    17  	stakingtypes "github.com/Finschia/finschia-sdk/x/staking/types"
    18  )
    19  
    20  func TestBeginBlocker(t *testing.T) {
    21  	app := simapp.Setup(false)
    22  	ctx := app.BaseApp.NewContext(false, tmproto.Header{})
    23  
    24  	pks := simapp.CreateTestPubKeys(1)
    25  	simapp.AddTestAddrsFromPubKeys(app, ctx, pks, app.StakingKeeper.TokensFromConsensusPower(ctx, 200))
    26  	addr, pk := sdk.ValAddress(pks[0].Address()), pks[0]
    27  	tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
    28  
    29  	// bond the validator
    30  	power := int64(100)
    31  	amt := tstaking.CreateValidatorWithValPower(addr, pk, power, true)
    32  	staking.EndBlocker(ctx, app.StakingKeeper)
    33  	require.Equal(
    34  		t, app.BankKeeper.GetAllBalances(ctx, sdk.AccAddress(addr)),
    35  		sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.GetParams(ctx).BondDenom, InitTokens.Sub(amt))),
    36  	)
    37  	require.Equal(t, amt, app.StakingKeeper.Validator(ctx, addr).GetBondedTokens())
    38  
    39  	val := abci.Validator{
    40  		Address: pk.Address(),
    41  		Power:   power,
    42  	}
    43  
    44  	// mark the validator as having signed
    45  	req := ocabci.RequestBeginBlock{
    46  		LastCommitInfo: abci.LastCommitInfo{
    47  			Votes: []abci.VoteInfo{{
    48  				Validator:       val,
    49  				SignedLastBlock: true,
    50  			}},
    51  		},
    52  	}
    53  
    54  	slashing.BeginBlocker(ctx, req, app.SlashingKeeper)
    55  
    56  	info, found := app.SlashingKeeper.GetValidatorSigningInfo(ctx, sdk.ConsAddress(pk.Address()))
    57  	require.True(t, found)
    58  	require.Equal(t, ctx.BlockHeight(), info.StartHeight)
    59  	require.Equal(t, int64(1), info.IndexOffset)
    60  	require.Equal(t, time.Unix(0, 0).UTC(), info.JailedUntil)
    61  	require.Equal(t, int64(0), info.MissedBlocksCounter)
    62  
    63  	height := int64(0)
    64  
    65  	// for 1000 blocks, mark the validator as having signed
    66  	for ; height < app.SlashingKeeper.SignedBlocksWindow(ctx); height++ {
    67  		ctx = ctx.WithBlockHeight(height)
    68  		req = ocabci.RequestBeginBlock{
    69  			LastCommitInfo: abci.LastCommitInfo{
    70  				Votes: []abci.VoteInfo{{
    71  					Validator:       val,
    72  					SignedLastBlock: true,
    73  				}},
    74  			},
    75  		}
    76  
    77  		slashing.BeginBlocker(ctx, req, app.SlashingKeeper)
    78  	}
    79  
    80  	// for 500 blocks, mark the validator as having not signed
    81  	for ; height < ((app.SlashingKeeper.SignedBlocksWindow(ctx) * 2) - app.SlashingKeeper.MinSignedPerWindow(ctx) + 1); height++ {
    82  		ctx = ctx.WithBlockHeight(height)
    83  		req = ocabci.RequestBeginBlock{
    84  			LastCommitInfo: abci.LastCommitInfo{
    85  				Votes: []abci.VoteInfo{{
    86  					Validator:       val,
    87  					SignedLastBlock: false,
    88  				}},
    89  			},
    90  		}
    91  
    92  		slashing.BeginBlocker(ctx, req, app.SlashingKeeper)
    93  	}
    94  
    95  	// end block
    96  	staking.EndBlocker(ctx, app.StakingKeeper)
    97  
    98  	// validator should be jailed
    99  	validator, found := app.StakingKeeper.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(pk))
   100  	require.True(t, found)
   101  	require.Equal(t, stakingtypes.Unbonding, validator.GetStatus())
   102  }