finished off validator tests
This commit is contained in:
parent
94731a502a
commit
7b1f4a87ea
|
@ -1,8 +1,6 @@
|
|||
package keeper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/distribution/types"
|
||||
)
|
||||
|
@ -50,7 +48,6 @@ func (k Keeper) WithdrawValidatorRewardsAll(ctx sdk.Context, operatorAddr sdk.Va
|
|||
feePool := k.GetFeePool(ctx)
|
||||
valInfo, feePool, commission := valInfo.WithdrawCommission(feePool, height, bondedTokens,
|
||||
validator.GetTokens(), validator.GetCommission())
|
||||
fmt.Printf("debug within wvra commission: %v\n", commission[0].Amount.String())
|
||||
withdraw = withdraw.Plus(commission)
|
||||
k.SetValidatorDistInfo(ctx, valInfo)
|
||||
k.SetFeePool(ctx, feePool)
|
||||
|
|
|
@ -81,9 +81,9 @@ func TestWithdrawValidatorRewardsAllDelegatorWithCommission(t *testing.T) {
|
|||
denom := sk.GetParams(ctx).BondDenom
|
||||
|
||||
//first make a validator
|
||||
commission := sdk.NewDecWithPrec(1, 1)
|
||||
commissionRate := sdk.NewDecWithPrec(1, 1)
|
||||
msgCreateValidator := stake.NewTestMsgCreateValidatorWithCommission(
|
||||
valOpAddr1, valConsPk1, 10, commission)
|
||||
valOpAddr1, valConsPk1, 10, commissionRate)
|
||||
got := stakeHandler(ctx, msgCreateValidator)
|
||||
require.True(t, got.IsOK(), "expected msg to be ok, got %v", got)
|
||||
_ = sk.ApplyAndReturnValidatorSetUpdates(ctx)
|
||||
|
@ -106,11 +106,11 @@ func TestWithdrawValidatorRewardsAllDelegatorWithCommission(t *testing.T) {
|
|||
keeper.SetSumPrecommitPower(ctx, totalPowerDec)
|
||||
keeper.AllocateFees(ctx)
|
||||
|
||||
// withdraw self-delegation reward
|
||||
// withdraw validator reward
|
||||
ctx = ctx.WithBlockHeight(1)
|
||||
keeper.WithdrawValidatorRewardsAll(ctx, valOpAddr1)
|
||||
amt = accMapper.GetAccount(ctx, valAccAddr1).GetCoins().AmountOf(denom)
|
||||
commissionTaken := sdk.NewDec(100).Mul(commission)
|
||||
commissionTaken := sdk.NewDec(100).Mul(commissionRate)
|
||||
afterCommission := sdk.NewDec(100).Sub(commissionTaken)
|
||||
selfDelegationReward := afterCommission.Quo(sdk.NewDec(2))
|
||||
expRes := sdk.NewDec(90).Add(commissionTaken).Add(selfDelegationReward).TruncateInt() // 90 + 100 tokens * 10/20
|
||||
|
@ -118,9 +118,100 @@ func TestWithdrawValidatorRewardsAllDelegatorWithCommission(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestWithdrawValidatorRewardsAllMultipleValidator(t *testing.T) {
|
||||
ctx, accMapper, keeper, sk, fck := CreateTestInputAdvanced(t, false, 100, sdk.ZeroDec())
|
||||
stakeHandler := stake.NewHandler(sk)
|
||||
denom := sk.GetParams(ctx).BondDenom
|
||||
|
||||
//make some validators with different commissions
|
||||
msgCreateValidator := stake.NewTestMsgCreateValidatorWithCommission(
|
||||
valOpAddr1, valConsPk1, 10, sdk.NewDecWithPrec(1, 1))
|
||||
got := stakeHandler(ctx, msgCreateValidator)
|
||||
require.True(t, got.IsOK(), "expected msg to be ok, got %v", got)
|
||||
|
||||
msgCreateValidator = stake.NewTestMsgCreateValidatorWithCommission(
|
||||
valOpAddr2, valConsPk2, 50, sdk.NewDecWithPrec(2, 1))
|
||||
got = stakeHandler(ctx, msgCreateValidator)
|
||||
require.True(t, got.IsOK(), "expected msg to be ok, got %v", got)
|
||||
|
||||
msgCreateValidator = stake.NewTestMsgCreateValidatorWithCommission(
|
||||
valOpAddr3, valConsPk3, 40, sdk.NewDecWithPrec(3, 1))
|
||||
got = stakeHandler(ctx, msgCreateValidator)
|
||||
require.True(t, got.IsOK(), "expected msg to be ok, got %v", got)
|
||||
|
||||
_ = sk.ApplyAndReturnValidatorSetUpdates(ctx)
|
||||
|
||||
totalPower := int64(100)
|
||||
totalPowerDec := sdk.NewDec(totalPower)
|
||||
|
||||
// allocate 100 denom of fees
|
||||
feeInputs := sdk.NewInt(1000)
|
||||
fck.SetCollectedFees(sdk.Coins{sdk.NewCoin(denom, feeInputs)})
|
||||
require.Equal(t, feeInputs, fck.GetCollectedFees(ctx).AmountOf(denom))
|
||||
keeper.SetProposerConsAddr(ctx, valConsAddr1)
|
||||
keeper.SetSumPrecommitPower(ctx, totalPowerDec)
|
||||
keeper.AllocateFees(ctx)
|
||||
|
||||
// withdraw validator reward
|
||||
ctx = ctx.WithBlockHeight(1)
|
||||
keeper.WithdrawValidatorRewardsAll(ctx, valOpAddr1)
|
||||
amt := accMapper.GetAccount(ctx, valAccAddr1).GetCoins().AmountOf(denom)
|
||||
|
||||
feesInNonProposer := sdk.NewDecFromInt(feeInputs).Mul(sdk.NewDecWithPrec(95, 2))
|
||||
feesInProposer := sdk.NewDecFromInt(feeInputs).Mul(sdk.NewDecWithPrec(5, 2))
|
||||
expRes := sdk.NewDec(90). // orig tokens (100 - 10)
|
||||
Add(feesInNonProposer.Quo(sdk.NewDec(10))). // validator 1 has 1/10 total power
|
||||
Add(feesInProposer).
|
||||
TruncateInt()
|
||||
require.True(sdk.IntEq(t, expRes, amt))
|
||||
}
|
||||
|
||||
func TestWithdrawValidatorRewardsAllMultipleDelegator(t *testing.T) {
|
||||
ctx, accMapper, keeper, sk, fck := CreateTestInputAdvanced(t, false, 100, sdk.ZeroDec())
|
||||
stakeHandler := stake.NewHandler(sk)
|
||||
denom := sk.GetParams(ctx).BondDenom
|
||||
|
||||
//first make a validator with 10% commission
|
||||
commissionRate := sdk.NewDecWithPrec(1, 1)
|
||||
msgCreateValidator := stake.NewTestMsgCreateValidatorWithCommission(
|
||||
valOpAddr1, valConsPk1, 10, sdk.NewDecWithPrec(1, 1))
|
||||
got := stakeHandler(ctx, msgCreateValidator)
|
||||
require.True(t, got.IsOK(), "expected msg to be ok, got %v", got)
|
||||
_ = sk.ApplyAndReturnValidatorSetUpdates(ctx)
|
||||
|
||||
// delegate
|
||||
msgDelegate := stake.NewTestMsgDelegate(delAddr1, valOpAddr1, 10)
|
||||
got = stakeHandler(ctx, msgDelegate)
|
||||
require.True(t, got.IsOK())
|
||||
amt := accMapper.GetAccount(ctx, delAddr1).GetCoins().AmountOf(denom)
|
||||
require.Equal(t, int64(90), amt.Int64())
|
||||
|
||||
msgDelegate = stake.NewTestMsgDelegate(delAddr2, valOpAddr1, 20)
|
||||
got = stakeHandler(ctx, msgDelegate)
|
||||
require.True(t, got.IsOK())
|
||||
amt = accMapper.GetAccount(ctx, delAddr2).GetCoins().AmountOf(denom)
|
||||
require.Equal(t, int64(80), amt.Int64())
|
||||
|
||||
totalPower := int64(40)
|
||||
totalPowerDec := sdk.NewDec(totalPower)
|
||||
|
||||
// allocate 100 denom of fees
|
||||
feeInputs := sdk.NewInt(100)
|
||||
fck.SetCollectedFees(sdk.Coins{sdk.NewCoin(denom, feeInputs)})
|
||||
require.Equal(t, feeInputs, fck.GetCollectedFees(ctx).AmountOf(denom))
|
||||
keeper.SetProposerConsAddr(ctx, valConsAddr1)
|
||||
keeper.SetSumPrecommitPower(ctx, totalPowerDec)
|
||||
keeper.AllocateFees(ctx)
|
||||
|
||||
// withdraw validator reward
|
||||
ctx = ctx.WithBlockHeight(1)
|
||||
keeper.WithdrawValidatorRewardsAll(ctx, valOpAddr1)
|
||||
amt = accMapper.GetAccount(ctx, valAccAddr1).GetCoins().AmountOf(denom)
|
||||
|
||||
commissionTaken := sdk.NewDec(100).Mul(commissionRate)
|
||||
afterCommission := sdk.NewDec(100).Sub(commissionTaken)
|
||||
expRes := sdk.NewDec(90).
|
||||
Add(afterCommission.Quo(sdk.NewDec(4))).
|
||||
Add(commissionTaken).
|
||||
TruncateInt() // 90 + 100*90% tokens * 10/40
|
||||
require.True(sdk.IntEq(t, expRes, amt))
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue