Skip to content

Commit

Permalink
Merge pull request #212 from binance-chain/bsc_base_slashcases
Browse files Browse the repository at this point in the history
add testcases for slashing
  • Loading branch information
fletcher142 authored May 14, 2020
2 parents 4959dab + aeb4274 commit c58cc35
Show file tree
Hide file tree
Showing 4 changed files with 250 additions and 32 deletions.
101 changes: 101 additions & 0 deletions x/slashing/claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,104 @@ func TestSideChainSlashDowntime(t *testing.T) {
require.NotNil(t, sdkErr, "Expected got err of no signing info found, but got nil")

}

func TestSlashDowntimeBalanceVerify(t *testing.T) {

slashingParams := DefaultParams()
slashingParams.MaxEvidenceAge = 12 * 60 * 60 * time.Second
slashingParams.DowntimeSlashAmount = 8000e8
slashingParams.DowntimeSlashFee = 5000e8
ctx, sideCtx, bk, stakeKeeper, _, keeper := createSideTestInput(t, slashingParams)
hooks := keeper.ClaimHooks()

bondAmount := int64(10000e8)
// create validator to be allocated slashed amount further
valAddr1 := addrs[0]
sideConsAddr1, sideFeeAddr1 := createSideAddr(20), createSideAddr(20)
msgCreateVal := newTestMsgCreateSideValidator(valAddr1, sideConsAddr1, sideFeeAddr1, bondAmount)
ctx = ctx.WithBlockHeight(100)
got := stake.NewHandler(stakeKeeper, gov.Keeper{})(ctx, msgCreateVal)
require.True(t, got.IsOK(), "expected create validator msg to be ok, got: %v", got)
validator1, found := stakeKeeper.GetValidator(sideCtx, valAddr1)
require.True(t, found)
distributionAddr := validator1.DistributionAddr
stake.EndBreatheBlock(ctx, stakeKeeper)

// create a validator will be slashed amount
ctx = ctx.WithBlockHeight(200)
valAddr2 := addrs[1]
sideConsAddr2, sideFeeAddr2 := createSideAddr(20), createSideAddr(20)
msgCreateVal = newTestMsgCreateSideValidator(valAddr2, sideConsAddr2, sideFeeAddr2, bondAmount)
got = stake.NewHandler(stakeKeeper, gov.Keeper{})(ctx, msgCreateVal)
require.True(t, got.IsOK(), "expected create validator msg to be ok, got: %v", got)
// end block
stake.EndBreatheBlock(ctx, stakeKeeper)

sideHeight := int64(50)
sideChainId := "bsc"
sideTimestamp := ctx.BlockHeader().Time.Add(-6 * 60 * 60 * time.Second)
claim := SideDowntimeSlashClaim{
SideConsAddr: sideConsAddr2,
SideHeight: sideHeight,
SideChainId: sideChainId,
SideTimestamp: sideTimestamp.Unix(),
}
jsonClaim, err := json.Marshal(claim)
require.Nil(t, err)
sdkErr := hooks.CheckClaim(ctx, string(jsonClaim))
require.Nil(t, sdkErr)

_, sdkErr = hooks.ExecuteClaim(ctx, string(jsonClaim))
require.Nil(t, sdkErr)

validator2, found := stakeKeeper.GetValidator(sideCtx, valAddr2)
require.True(t, found)
require.True(t, validator2.Jailed)
require.EqualValues(t, 2000e8, validator2.Tokens.RawInt())
require.EqualValues(t, 2000e8, validator2.DelegatorShares.RawInt())

delegation, found := stakeKeeper.GetDelegation(sideCtx, sdk.AccAddress(valAddr2), valAddr2)
require.True(t, found)
require.EqualValues(t, 2000e8, delegation.Shares.RawInt()) // slashed 8000e8 from validator2 delegation

require.EqualValues(t,5000e8, fees.Pool.BlockFees().Tokens.AmountOf("steak")) // add 5000e8 as DowntimeSlashFee to fee pool

coins := bk.GetCoins(ctx, distributionAddr)
require.EqualValues(t, 3000e8, coins.AmountOf("steak")) // remaining amount(3000e8) allocated to

sideHeight = int64(80)
sideChainId = "bsc"
sideTimestamp = ctx.BlockHeader().Time.Add(-3 * 60 * 60 * time.Second)
claim = SideDowntimeSlashClaim{
SideConsAddr: sideConsAddr2,
SideHeight: sideHeight,
SideChainId: sideChainId,
SideTimestamp: sideTimestamp.Unix(),
}
jsonClaim, err = json.Marshal(claim)
require.Nil(t, err)
sdkErr = hooks.CheckClaim(ctx, string(jsonClaim))
require.Nil(t, sdkErr)
_, sdkErr = hooks.ExecuteClaim(ctx, string(jsonClaim))
require.Nil(t, sdkErr)

validator2, found = stakeKeeper.GetValidator(sideCtx, valAddr2)
require.True(t, found)
require.True(t, validator2.Jailed)
require.EqualValues(t, 0, validator2.Tokens.RawInt())
require.EqualValues(t, 0, validator2.DelegatorShares.RawInt())

_, found = stakeKeeper.GetDelegation(sideCtx, sdk.AccAddress(valAddr2), valAddr2)
require.False(t, found)

realSlashedAmount := int64(2000e8)
require.EqualValues(t,slashingParams.DowntimeSlashFee + realSlashedAmount, fees.Pool.BlockFees().Tokens.AmountOf("steak"))

coins = bk.GetCoins(ctx, distributionAddr)
require.EqualValues(t, 3000e8, coins.AmountOf("steak"))

// end block
stake.EndBreatheBlock(ctx, stakeKeeper)
_, found = stakeKeeper.GetValidator(sideCtx, valAddr2)
require.False(t,found)
}
Loading

0 comments on commit c58cc35

Please sign in to comment.