From ba1e91cfffba69ec01efb46f2a17e92bf6c4b6e6 Mon Sep 17 00:00:00 2001 From: fletcher142 <42368365+fletcher142@users.noreply.github.com> Date: Thu, 14 May 2020 19:43:08 +0800 Subject: [PATCH] testcase improve (#216) * testcase improve --- x/slashing/app_test.go | 6 +++--- x/slashing/claim_test.go | 15 +++++++-------- x/slashing/handler_sidechain_test.go | 6 ++++-- x/slashing/handler_test.go | 2 +- x/slashing/keeper_test.go | 3 +++ 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/x/slashing/app_test.go b/x/slashing/app_test.go index f97276b83..4df1b707b 100644 --- a/x/slashing/app_test.go +++ b/x/slashing/app_test.go @@ -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, @@ -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 diff --git a/x/slashing/claim_test.go b/x/slashing/claim_test.go index 9c81c84cd..d2eedb79e 100644 --- a/x/slashing/claim_test.go +++ b/x/slashing/claim_test.go @@ -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) @@ -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) @@ -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 @@ -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) @@ -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 @@ -215,7 +214,7 @@ 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")) @@ -223,5 +222,5 @@ func TestSlashDowntimeBalanceVerify(t *testing.T) { // end block stake.EndBreatheBlock(ctx, stakeKeeper) _, found = stakeKeeper.GetValidator(sideCtx, valAddr2) - require.False(t,found) + require.False(t, found) } diff --git a/x/slashing/handler_sidechain_test.go b/x/slashing/handler_sidechain_test.go index 759c92f1d..2add6568d 100644 --- a/x/slashing/handler_sidechain_test.go +++ b/x/slashing/handler_sidechain_test.go @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/x/slashing/handler_test.go b/x/slashing/handler_test.go index 1425b5094..2e2f4beca 100644 --- a/x/slashing/handler_test.go +++ b/x/slashing/handler_test.go @@ -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) diff --git a/x/slashing/keeper_test.go b/x/slashing/keeper_test.go index 9ce566b66..11658f574 100644 --- a/x/slashing/keeper_test.go +++ b/x/slashing/keeper_test.go @@ -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)