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

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