2020-03-05 06:00:45 -08:00
|
|
|
|
package keeper_test
|
2018-10-04 18:37:40 -07:00
|
|
|
|
|
2018-10-10 14:43:47 -07:00
|
|
|
|
import (
|
|
|
|
|
"testing"
|
2018-10-04 18:37:40 -07:00
|
|
|
|
|
2018-10-10 14:43:47 -07:00
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-03-05 06:00:45 -08:00
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2018-12-10 06:27:25 -08:00
|
|
|
|
|
2020-03-05 06:00:45 -08:00
|
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
2018-12-10 06:27:25 -08:00
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-03-05 06:00:45 -08:00
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
2019-01-11 12:08:01 -08:00
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking"
|
2018-10-10 14:43:47 -07:00
|
|
|
|
)
|
2018-10-04 18:37:40 -07:00
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
|
func TestAllocateTokensToValidatorWithCommission(t *testing.T) {
|
2020-03-05 06:00:45 -08:00
|
|
|
|
app := simapp.Setup(false)
|
|
|
|
|
ctx := app.BaseApp.NewContext(false, abci.Header{})
|
|
|
|
|
|
|
|
|
|
addrs := simapp.AddTestAddrs(app, ctx, 3, sdk.NewInt(1234))
|
|
|
|
|
valAddrs := simapp.ConvertAddrsToValAddrs(addrs)
|
|
|
|
|
|
|
|
|
|
sh := staking.NewHandler(app.StakingKeeper)
|
2019-01-16 13:38:05 -08:00
|
|
|
|
|
|
|
|
|
// create validator with 50% commission
|
2019-06-03 14:14:21 -07:00
|
|
|
|
commission := staking.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
|
2019-12-27 09:57:54 -08:00
|
|
|
|
msg := staking.NewMsgCreateValidator(
|
2020-03-05 06:00:45 -08:00
|
|
|
|
sdk.ValAddress(addrs[0]), valConsPk1,
|
2019-12-27 09:57:54 -08:00
|
|
|
|
sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100)), staking.Description{}, commission, sdk.OneInt(),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
res, err := sh(ctx, msg)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.NotNil(t, res)
|
|
|
|
|
|
2020-03-05 06:00:45 -08:00
|
|
|
|
val := app.StakingKeeper.Validator(ctx, valAddrs[0])
|
2019-01-16 13:38:05 -08:00
|
|
|
|
|
|
|
|
|
// allocate tokens
|
|
|
|
|
tokens := sdk.DecCoins{
|
2019-09-16 10:11:17 -07:00
|
|
|
|
{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDec(10)},
|
2019-01-16 13:38:05 -08:00
|
|
|
|
}
|
2020-03-05 06:00:45 -08:00
|
|
|
|
app.DistrKeeper.AllocateTokensToValidator(ctx, val, tokens)
|
2019-01-16 13:38:05 -08:00
|
|
|
|
|
|
|
|
|
// check commission
|
|
|
|
|
expected := sdk.DecCoins{
|
2019-09-16 10:11:17 -07:00
|
|
|
|
{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDec(5)},
|
2019-01-16 13:38:05 -08:00
|
|
|
|
}
|
2020-03-05 06:00:45 -08:00
|
|
|
|
require.Equal(t, expected, app.DistrKeeper.GetValidatorAccumulatedCommission(ctx, val.GetOperator()).Commission)
|
2019-01-16 13:38:05 -08:00
|
|
|
|
|
|
|
|
|
// check current rewards
|
2020-03-05 06:00:45 -08:00
|
|
|
|
require.Equal(t, expected, app.DistrKeeper.GetValidatorCurrentRewards(ctx, val.GetOperator()).Rewards)
|
2018-10-04 18:37:40 -07:00
|
|
|
|
}
|
2018-10-10 20:16:49 -07:00
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
|
func TestAllocateTokensToManyValidators(t *testing.T) {
|
2020-03-05 06:00:45 -08:00
|
|
|
|
app := simapp.Setup(false)
|
|
|
|
|
ctx := app.BaseApp.NewContext(false, abci.Header{})
|
|
|
|
|
|
|
|
|
|
sh := staking.NewHandler(app.StakingKeeper)
|
|
|
|
|
addrs := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(1234))
|
|
|
|
|
valAddrs := simapp.ConvertAddrsToValAddrs(addrs)
|
2019-01-16 13:38:05 -08:00
|
|
|
|
|
|
|
|
|
// create validator with 50% commission
|
2019-06-03 14:14:21 -07:00
|
|
|
|
commission := staking.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
|
2020-03-05 06:00:45 -08:00
|
|
|
|
msg := staking.NewMsgCreateValidator(valAddrs[0], valConsPk1,
|
2019-02-13 15:01:50 -08:00
|
|
|
|
sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100)), staking.Description{}, commission, sdk.OneInt())
|
2019-12-27 09:57:54 -08:00
|
|
|
|
|
|
|
|
|
res, err := sh(ctx, msg)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.NotNil(t, res)
|
2019-01-16 13:38:05 -08:00
|
|
|
|
|
|
|
|
|
// create second validator with 0% commission
|
2019-06-03 14:14:21 -07:00
|
|
|
|
commission = staking.NewCommissionRates(sdk.NewDec(0), sdk.NewDec(0), sdk.NewDec(0))
|
2020-03-05 06:00:45 -08:00
|
|
|
|
msg = staking.NewMsgCreateValidator(valAddrs[1], valConsPk2,
|
2019-02-13 15:01:50 -08:00
|
|
|
|
sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100)), staking.Description{}, commission, sdk.OneInt())
|
2019-12-27 09:57:54 -08:00
|
|
|
|
|
|
|
|
|
res, err = sh(ctx, msg)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.NotNil(t, res)
|
2019-01-16 13:38:05 -08:00
|
|
|
|
|
|
|
|
|
abciValA := abci.Validator{
|
|
|
|
|
Address: valConsPk1.Address(),
|
|
|
|
|
Power: 100,
|
|
|
|
|
}
|
|
|
|
|
abciValB := abci.Validator{
|
|
|
|
|
Address: valConsPk2.Address(),
|
|
|
|
|
Power: 100,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// assert initial state: zero outstanding rewards, zero community pool, zero commission, zero current rewards
|
2020-03-05 06:00:45 -08:00
|
|
|
|
require.True(t, app.DistrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[0]).Rewards.IsZero())
|
|
|
|
|
require.True(t, app.DistrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[1]).Rewards.IsZero())
|
|
|
|
|
require.True(t, app.DistrKeeper.GetFeePool(ctx).CommunityPool.IsZero())
|
|
|
|
|
require.True(t, app.DistrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[0]).Commission.IsZero())
|
|
|
|
|
require.True(t, app.DistrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[1]).Commission.IsZero())
|
|
|
|
|
require.True(t, app.DistrKeeper.GetValidatorCurrentRewards(ctx, valAddrs[0]).Rewards.IsZero())
|
|
|
|
|
require.True(t, app.DistrKeeper.GetValidatorCurrentRewards(ctx, valAddrs[1]).Rewards.IsZero())
|
2019-01-16 13:38:05 -08:00
|
|
|
|
|
|
|
|
|
// allocate tokens as if both had voted and second was proposer
|
2019-06-28 13:11:27 -07:00
|
|
|
|
fees := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100)))
|
2020-04-20 12:32:10 -07:00
|
|
|
|
feeCollector := app.AccountKeeper.GetModuleAccount(ctx, types.FeeCollectorName)
|
2019-06-28 13:11:27 -07:00
|
|
|
|
require.NotNil(t, feeCollector)
|
|
|
|
|
|
2020-03-05 06:00:45 -08:00
|
|
|
|
err = app.BankKeeper.SetBalances(ctx, feeCollector.GetAddress(), fees)
|
2019-06-28 13:11:27 -07:00
|
|
|
|
require.NoError(t, err)
|
2020-03-05 06:00:45 -08:00
|
|
|
|
app.AccountKeeper.SetAccount(ctx, feeCollector)
|
2019-06-28 13:11:27 -07:00
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
|
votes := []abci.VoteInfo{
|
|
|
|
|
{
|
|
|
|
|
Validator: abciValA,
|
|
|
|
|
SignedLastBlock: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Validator: abciValB,
|
|
|
|
|
SignedLastBlock: true,
|
|
|
|
|
},
|
|
|
|
|
}
|
2020-03-05 06:00:45 -08:00
|
|
|
|
app.DistrKeeper.AllocateTokens(ctx, 200, 200, valConsAddr2, votes)
|
2019-01-16 13:38:05 -08:00
|
|
|
|
|
|
|
|
|
// 98 outstanding rewards (100 less 2 to community pool)
|
2020-03-05 06:00:45 -08:00
|
|
|
|
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecWithPrec(465, 1)}}, app.DistrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[0]).Rewards)
|
|
|
|
|
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecWithPrec(515, 1)}}, app.DistrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[1]).Rewards)
|
2019-01-16 13:38:05 -08:00
|
|
|
|
// 2 community pool coins
|
2020-03-05 06:00:45 -08:00
|
|
|
|
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDec(2)}}, app.DistrKeeper.GetFeePool(ctx).CommunityPool)
|
2019-01-16 13:38:05 -08:00
|
|
|
|
// 50% commission for first proposer, (0.5 * 93%) * 100 / 2 = 23.25
|
2020-03-05 06:00:45 -08:00
|
|
|
|
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecWithPrec(2325, 2)}}, app.DistrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[0]).Commission)
|
2019-01-16 13:38:05 -08:00
|
|
|
|
// zero commission for second proposer
|
2020-03-05 06:00:45 -08:00
|
|
|
|
require.True(t, app.DistrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[1]).Commission.IsZero())
|
2019-01-16 13:38:05 -08:00
|
|
|
|
// just staking.proportional for first proposer less commission = (0.5 * 93%) * 100 / 2 = 23.25
|
2020-03-05 06:00:45 -08:00
|
|
|
|
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecWithPrec(2325, 2)}}, app.DistrKeeper.GetValidatorCurrentRewards(ctx, valAddrs[0]).Rewards)
|
2019-01-16 13:38:05 -08:00
|
|
|
|
// proposer reward + staking.proportional for second proposer = (5 % + 0.5 * (93%)) * 100 = 51.5
|
2020-03-05 06:00:45 -08:00
|
|
|
|
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecWithPrec(515, 1)}}, app.DistrKeeper.GetValidatorCurrentRewards(ctx, valAddrs[1]).Rewards)
|
2018-10-10 20:16:49 -07:00
|
|
|
|
}
|
2019-02-26 10:23:11 -08:00
|
|
|
|
|
|
|
|
|
func TestAllocateTokensTruncation(t *testing.T) {
|
2020-03-05 06:00:45 -08:00
|
|
|
|
app := simapp.Setup(false)
|
|
|
|
|
ctx := app.BaseApp.NewContext(false, abci.Header{})
|
|
|
|
|
|
|
|
|
|
addrs := simapp.AddTestAddrs(app, ctx, 3, sdk.NewInt(1234))
|
|
|
|
|
valAddrs := simapp.ConvertAddrsToValAddrs(addrs)
|
|
|
|
|
sh := staking.NewHandler(app.StakingKeeper)
|
2019-02-26 10:23:11 -08:00
|
|
|
|
|
|
|
|
|
// create validator with 10% commission
|
2019-06-03 14:14:21 -07:00
|
|
|
|
commission := staking.NewCommissionRates(sdk.NewDecWithPrec(1, 1), sdk.NewDecWithPrec(1, 1), sdk.NewDec(0))
|
2020-03-05 06:00:45 -08:00
|
|
|
|
msg := staking.NewMsgCreateValidator(valAddrs[0], valConsPk1,
|
2019-02-26 10:23:11 -08:00
|
|
|
|
sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(110)), staking.Description{}, commission, sdk.OneInt())
|
2019-12-27 09:57:54 -08:00
|
|
|
|
res, err := sh(ctx, msg)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.NotNil(t, res)
|
2019-02-26 10:23:11 -08:00
|
|
|
|
|
|
|
|
|
// create second validator with 10% commission
|
2019-06-03 14:14:21 -07:00
|
|
|
|
commission = staking.NewCommissionRates(sdk.NewDecWithPrec(1, 1), sdk.NewDecWithPrec(1, 1), sdk.NewDec(0))
|
2020-03-05 06:00:45 -08:00
|
|
|
|
msg = staking.NewMsgCreateValidator(valAddrs[1], valConsPk2,
|
2019-02-26 10:23:11 -08:00
|
|
|
|
sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100)), staking.Description{}, commission, sdk.OneInt())
|
2019-12-27 09:57:54 -08:00
|
|
|
|
res, err = sh(ctx, msg)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.NotNil(t, res)
|
2019-02-26 10:23:11 -08:00
|
|
|
|
|
|
|
|
|
// create third validator with 10% commission
|
2019-06-03 14:14:21 -07:00
|
|
|
|
commission = staking.NewCommissionRates(sdk.NewDecWithPrec(1, 1), sdk.NewDecWithPrec(1, 1), sdk.NewDec(0))
|
2020-03-05 06:00:45 -08:00
|
|
|
|
msg = staking.NewMsgCreateValidator(valAddrs[2], valConsPk3,
|
2019-02-26 10:23:11 -08:00
|
|
|
|
sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100)), staking.Description{}, commission, sdk.OneInt())
|
2019-12-27 09:57:54 -08:00
|
|
|
|
res, err = sh(ctx, msg)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.NotNil(t, res)
|
2019-02-26 10:23:11 -08:00
|
|
|
|
|
|
|
|
|
abciValA := abci.Validator{
|
|
|
|
|
Address: valConsPk1.Address(),
|
|
|
|
|
Power: 11,
|
|
|
|
|
}
|
|
|
|
|
abciValB := abci.Validator{
|
|
|
|
|
Address: valConsPk2.Address(),
|
|
|
|
|
Power: 10,
|
|
|
|
|
}
|
|
|
|
|
abciValС := abci.Validator{
|
|
|
|
|
Address: valConsPk3.Address(),
|
|
|
|
|
Power: 10,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// assert initial state: zero outstanding rewards, zero community pool, zero commission, zero current rewards
|
2020-03-05 06:00:45 -08:00
|
|
|
|
require.True(t, app.DistrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[0]).Rewards.IsZero())
|
|
|
|
|
require.True(t, app.DistrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[1]).Rewards.IsZero())
|
|
|
|
|
require.True(t, app.DistrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[1]).Rewards.IsZero())
|
|
|
|
|
require.True(t, app.DistrKeeper.GetFeePool(ctx).CommunityPool.IsZero())
|
|
|
|
|
require.True(t, app.DistrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[0]).Commission.IsZero())
|
|
|
|
|
require.True(t, app.DistrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[1]).Commission.IsZero())
|
|
|
|
|
require.True(t, app.DistrKeeper.GetValidatorCurrentRewards(ctx, valAddrs[0]).Rewards.IsZero())
|
|
|
|
|
require.True(t, app.DistrKeeper.GetValidatorCurrentRewards(ctx, valAddrs[1]).Rewards.IsZero())
|
2019-02-26 10:23:11 -08:00
|
|
|
|
|
|
|
|
|
// allocate tokens as if both had voted and second was proposer
|
2019-06-28 13:11:27 -07:00
|
|
|
|
fees := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(634195840)))
|
|
|
|
|
|
2020-04-20 12:32:10 -07:00
|
|
|
|
feeCollector := app.AccountKeeper.GetModuleAccount(ctx, types.FeeCollectorName)
|
2019-06-28 13:11:27 -07:00
|
|
|
|
require.NotNil(t, feeCollector)
|
|
|
|
|
|
2020-03-05 06:00:45 -08:00
|
|
|
|
err = app.BankKeeper.SetBalances(ctx, feeCollector.GetAddress(), fees)
|
2019-06-28 13:11:27 -07:00
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
2020-03-05 06:00:45 -08:00
|
|
|
|
app.AccountKeeper.SetAccount(ctx, feeCollector)
|
2019-06-28 13:11:27 -07:00
|
|
|
|
|
2019-02-26 10:23:11 -08:00
|
|
|
|
votes := []abci.VoteInfo{
|
|
|
|
|
{
|
|
|
|
|
Validator: abciValA,
|
|
|
|
|
SignedLastBlock: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Validator: abciValB,
|
|
|
|
|
SignedLastBlock: true,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Validator: abciValС,
|
|
|
|
|
SignedLastBlock: true,
|
|
|
|
|
},
|
|
|
|
|
}
|
2020-03-05 06:00:45 -08:00
|
|
|
|
app.DistrKeeper.AllocateTokens(ctx, 31, 31, sdk.ConsAddress(valConsPk2.Address()), votes)
|
2019-02-26 10:23:11 -08:00
|
|
|
|
|
2020-03-05 06:00:45 -08:00
|
|
|
|
require.True(t, app.DistrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[0]).Rewards.IsValid())
|
|
|
|
|
require.True(t, app.DistrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[1]).Rewards.IsValid())
|
|
|
|
|
require.True(t, app.DistrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[2]).Rewards.IsValid())
|
2019-02-26 10:23:11 -08:00
|
|
|
|
}
|