Skip to content

Commit

Permalink
testcase improve (#216)
Browse files Browse the repository at this point in the history
* testcase improve
  • Loading branch information
fletcher142 authored May 14, 2020
1 parent c58cc35 commit ba1e91c
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
6 changes: 3 additions & 3 deletions x/slashing/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ func checkValidatorSigningInfo(t *testing.T, mapp *mock.App, keeper Keeper,
func TestSlashingMsgs(t *testing.T) {
mapp, stakeKeeper, keeper := getMockApp(t)

genCoin := sdk.NewCoin("steak", sdk.NewDecWithoutFra(42).RawInt())
bondCoin := sdk.NewCoin("steak", sdk.NewDecWithoutFra(10).RawInt())
genCoin := sdk.NewCoin("steak", sdk.NewDecWithoutFra(42000).RawInt())
bondCoin := sdk.NewCoin("steak", sdk.NewDecWithoutFra(10000).RawInt())

acc1 := &auth.BaseAccount{
Address: addr1,
Expand All @@ -124,7 +124,7 @@ func TestSlashingMsgs(t *testing.T) {
validator := checkValidator(t, mapp, stakeKeeper, addr1, true)
require.Equal(t, sdk.ValAddress(addr1), validator.OperatorAddr)
require.Equal(t, sdk.Bonded, validator.Status)
require.True(sdk.DecEq(t, sdk.NewDecWithoutFra(10), validator.BondedTokens()))
require.True(sdk.DecEq(t, sdk.NewDecWithoutFra(10000), validator.BondedTokens()))
unjailMsg := MsgUnjail{ValidatorAddr: sdk.ValAddress(validator.ConsPubKey.Address())}

// no signing info yet
Expand Down
15 changes: 7 additions & 8 deletions x/slashing/claim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ func TestSideChainSlashDowntime(t *testing.T) {
// create a validator
bondAmount := int64(10000e8)
realSlashAmt := sdk.MinInt64(slashingParams.DowntimeSlashAmount, bondAmount)
realPoolFeeAdd := sdk.MinInt64(realSlashAmt, slashingParams.DowntimeSlashFee)
valAddr := addrs[0]
sideConsAddr, sideFeeAddr := createSideAddr(20), createSideAddr(20)
msgCreateVal := newTestMsgCreateSideValidator(valAddr, sideConsAddr, sideFeeAddr, bondAmount)
Expand All @@ -49,7 +48,6 @@ func TestSideChainSlashDowntime(t *testing.T) {

_, sdkErr = hooks.ExecuteClaim(ctx, string(jsonClaim))
require.Nil(t, sdkErr, "Expected nil, but got : %v", sdkErr)
require.EqualValues(t, realPoolFeeAdd, fees.Pool.BlockFees().Tokens.AmountOf("steak"))

info, found := keeper.getValidatorSigningInfo(sideCtx, sideConsAddr)
require.True(t, found)
Expand Down Expand Up @@ -98,9 +96,9 @@ func TestSideChainSlashDowntime(t *testing.T) {
require.Nil(t, err)
sdkErr = hooks.CheckClaim(ctx, string(jsonClaim))
require.Nil(t, sdkErr)
_, sdkErr = hooks.ExecuteClaim(ctx, string(jsonClaim))
require.NotNil(t, sdkErr, "Exepcted get err, but got nil")
require.EqualValues(t, CodeExpiredEvidence, sdkErr.Code(), "Expected got 201 err code, but got err: %v", sdkErr)
claimResult, sdkErr := hooks.ExecuteClaim(ctx, string(jsonClaim))
require.Nil(t, sdkErr)
require.EqualValues(t, CodeExpiredEvidence, claimResult.Code, "Expected got 201 err code, but got err: %v", sdkErr)

claim.SideTimestamp = ctx.BlockHeader().Time.Add(-6 * 60 * 60 * time.Second).Unix()
claim.SideConsAddr = sideConsAddr
Expand Down Expand Up @@ -171,6 +169,7 @@ func TestSlashDowntimeBalanceVerify(t *testing.T) {
sdkErr := hooks.CheckClaim(ctx, string(jsonClaim))
require.Nil(t, sdkErr)

feesInPoolBefore := fees.Pool.BlockFees().Tokens.AmountOf("steak")
_, sdkErr = hooks.ExecuteClaim(ctx, string(jsonClaim))
require.Nil(t, sdkErr)

Expand All @@ -184,7 +183,7 @@ func TestSlashDowntimeBalanceVerify(t *testing.T) {
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
require.EqualValues(t, 5000e8, fees.Pool.BlockFees().Tokens.AmountOf("steak")-feesInPoolBefore) // add 5000e8 as DowntimeSlashFee to fee pool

coins := bk.GetCoins(ctx, distributionAddr)
require.EqualValues(t, 3000e8, coins.AmountOf("steak")) // remaining amount(3000e8) allocated to
Expand Down Expand Up @@ -215,13 +214,13 @@ func TestSlashDowntimeBalanceVerify(t *testing.T) {
require.False(t, found)

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

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)
require.False(t, found)
}
6 changes: 4 additions & 2 deletions x/slashing/handler_sidechain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func TestSideChainSlashDoubleSign(t *testing.T) {
err = json.Unmarshal([]byte(headersJson), &headers)
require.Nil(t, err)

feesInPoolBefore := fees.Pool.BlockFees().Tokens.AmountOf("steak")
msgSubmitEvidence := NewMsgBscSubmitEvidence(submitter, headers)
got = NewHandler(keeper)(ctx, msgSubmitEvidence)
require.True(t, got.IsOK(), "expected submit evidence msg to be ok, got: %v", got)
Expand All @@ -67,7 +68,7 @@ func TestSideChainSlashDoubleSign(t *testing.T) {
submitterBalance := bankKeeper.GetCoins(ctx, submitter).AmountOf("steak")
require.EqualValues(t, initCoins+slashParams.SubmitterReward, submitterBalance)

require.EqualValues(t, slashParams.DoubleSignSlashAmount-slashParams.SubmitterReward, fees.Pool.BlockFees().Tokens.AmountOf("steak"))
require.EqualValues(t, slashParams.DoubleSignSlashAmount-slashParams.SubmitterReward, fees.Pool.BlockFees().Tokens.AmountOf("steak")-feesInPoolBefore)

slashRecord, found := keeper.getSlashRecord(sideCtx, mSideConsAddr, DoubleSign, 1)
require.True(t, found)
Expand Down Expand Up @@ -159,6 +160,7 @@ func TestSideChainSlashDoubleSignUBD(t *testing.T) {
err = json.Unmarshal([]byte(headersJson), &headers)
require.Nil(t, err)

feesInPoolBefore := fees.Pool.BlockFees().Tokens.AmountOf("steak")
msgSubmitEvidence := NewMsgBscSubmitEvidence(submitter, headers)
got = NewHandler(keeper)(ctx, msgSubmitEvidence)
require.True(t, got.IsOK(), "expected submit evidence msg to be ok, got: %v", got)
Expand All @@ -176,7 +178,7 @@ func TestSideChainSlashDoubleSignUBD(t *testing.T) {
submitterBalance := bankKeeper.GetCoins(ctx, submitter).AmountOf("steak")
require.EqualValues(t, initCoins+slashParams.SubmitterReward, submitterBalance)

require.EqualValues(t, slashParams.DoubleSignSlashAmount-slashParams.SubmitterReward, fees.Pool.BlockFees().Tokens.AmountOf("steak"))
require.EqualValues(t, slashParams.DoubleSignSlashAmount-slashParams.SubmitterReward, fees.Pool.BlockFees().Tokens.AmountOf("steak")-feesInPoolBefore)

stakingPoolBalance := bankKeeper.GetCoins(ctx, stake.DelegationAccAddr).AmountOf("steak")
require.EqualValues(t, 4000e8, stakingPoolBalance)
Expand Down
2 changes: 1 addition & 1 deletion x/slashing/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func TestCannotUnjailUnlessJailed(t *testing.T) {
// initial setup
ctx, ck, sk, _, keeper := createTestInput(t, DefaultParams())
slh := NewSlashingHandler(keeper)
amtInt := sdk.NewDecWithoutFra(100).RawInt()
amtInt := sdk.NewDecWithoutFra(10000).RawInt()
addr, val, amt := addrs[0], pks[0], amtInt
msg := NewTestMsgCreateValidator(addr, val, amt)
got := stake.NewStakeHandler(sk)(ctx, msg)
Expand Down
3 changes: 3 additions & 0 deletions x/slashing/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ func TestHandleAbsentValidator(t *testing.T) {

// initial setup
ctx, ck, sk, _, keeper := createTestInput(t, keeperTestParams())
stakeParam := stake.DefaultParams()
stakeParam.MinSelfDelegation = 10e8
sk.SetParams(ctx, stakeParam)
amtInt := sdk.NewDecWithoutFra(100).RawInt()
addr, val, amt := addrs[0], pks[0], amtInt
sh := stake.NewStakeHandler(sk)
Expand Down

0 comments on commit ba1e91c

Please sign in to comment.