<!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description Closes: #8961 SDK allows InitGenesis to return with an empty validator set. In practice, the error for an empty validator set gets thrown in tendermint. To fix this, * Add non-empty validator set check to the `mm.InitGenesis` function. This will break `simapp.Setup` because it relies on an empty validator set [#comment](https://github.com/cosmos/cosmos-sdk/pull/8909#issuecomment-804850834). * Update `simapp.Setup` to use a single validator. * Fix failing tests (Most of them are keeper tests). <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [x] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [x] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
198 lines
8.7 KiB
Go
198 lines
8.7 KiB
Go
package keeper_test
|
||
|
||
import (
|
||
"testing"
|
||
|
||
"github.com/stretchr/testify/require"
|
||
abci "github.com/tendermint/tendermint/abci/types"
|
||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
||
|
||
"github.com/cosmos/cosmos-sdk/simapp"
|
||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
||
"github.com/cosmos/cosmos-sdk/x/bank/testutil"
|
||
disttypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
|
||
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
|
||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
||
)
|
||
|
||
func TestAllocateTokensToValidatorWithCommission(t *testing.T) {
|
||
app := simapp.Setup(t, false)
|
||
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
||
|
||
addrs := simapp.AddTestAddrs(app, ctx, 3, sdk.NewInt(1234))
|
||
valAddrs := simapp.ConvertAddrsToValAddrs(addrs)
|
||
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
|
||
|
||
// create validator with 50% commission
|
||
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
|
||
tstaking.CreateValidator(sdk.ValAddress(addrs[0]), valConsPk1, sdk.NewInt(100), true)
|
||
val := app.StakingKeeper.Validator(ctx, valAddrs[0])
|
||
|
||
// allocate tokens
|
||
tokens := sdk.DecCoins{
|
||
{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDec(10)},
|
||
}
|
||
app.DistrKeeper.AllocateTokensToValidator(ctx, val, tokens)
|
||
|
||
// check commission
|
||
expected := sdk.DecCoins{
|
||
{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDec(5)},
|
||
}
|
||
require.Equal(t, expected, app.DistrKeeper.GetValidatorAccumulatedCommission(ctx, val.GetOperator()).Commission)
|
||
|
||
// check current rewards
|
||
require.Equal(t, expected, app.DistrKeeper.GetValidatorCurrentRewards(ctx, val.GetOperator()).Rewards)
|
||
}
|
||
|
||
func TestAllocateTokensToManyValidators(t *testing.T) {
|
||
app := simapp.Setup(t, false)
|
||
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
||
|
||
// reset fee pool
|
||
app.DistrKeeper.SetFeePool(ctx, disttypes.InitialFeePool())
|
||
|
||
addrs := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(1234))
|
||
valAddrs := simapp.ConvertAddrsToValAddrs(addrs)
|
||
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
|
||
|
||
// create validator with 50% commission
|
||
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), sdk.NewDec(0))
|
||
tstaking.CreateValidator(valAddrs[0], valConsPk1, sdk.NewInt(100), true)
|
||
|
||
// create second validator with 0% commission
|
||
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDec(0), sdk.NewDec(0), sdk.NewDec(0))
|
||
tstaking.CreateValidator(valAddrs[1], valConsPk2, sdk.NewInt(100), true)
|
||
|
||
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
|
||
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())
|
||
|
||
// allocate tokens as if both had voted and second was proposer
|
||
fees := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(100)))
|
||
feeCollector := app.AccountKeeper.GetModuleAccount(ctx, types.FeeCollectorName)
|
||
require.NotNil(t, feeCollector)
|
||
|
||
// fund fee collector
|
||
require.NoError(t, testutil.FundModuleAccount(app.BankKeeper, ctx, feeCollector.GetName(), fees))
|
||
|
||
app.AccountKeeper.SetAccount(ctx, feeCollector)
|
||
|
||
votes := []abci.VoteInfo{
|
||
{
|
||
Validator: abciValA,
|
||
SignedLastBlock: true,
|
||
},
|
||
{
|
||
Validator: abciValB,
|
||
SignedLastBlock: true,
|
||
},
|
||
}
|
||
app.DistrKeeper.AllocateTokens(ctx, 200, 200, valConsAddr2, votes)
|
||
|
||
// 98 outstanding rewards (100 less 2 to community pool)
|
||
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)
|
||
// 2 community pool coins
|
||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDec(2)}}, app.DistrKeeper.GetFeePool(ctx).CommunityPool)
|
||
// 50% commission for first proposer, (0.5 * 93%) * 100 / 2 = 23.25
|
||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecWithPrec(2325, 2)}}, app.DistrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[0]).Commission)
|
||
// zero commission for second proposer
|
||
require.True(t, app.DistrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[1]).Commission.IsZero())
|
||
// just staking.proportional for first proposer less commission = (0.5 * 93%) * 100 / 2 = 23.25
|
||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecWithPrec(2325, 2)}}, app.DistrKeeper.GetValidatorCurrentRewards(ctx, valAddrs[0]).Rewards)
|
||
// proposer reward + staking.proportional for second proposer = (5 % + 0.5 * (93%)) * 100 = 51.5
|
||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecWithPrec(515, 1)}}, app.DistrKeeper.GetValidatorCurrentRewards(ctx, valAddrs[1]).Rewards)
|
||
}
|
||
|
||
func TestAllocateTokensTruncation(t *testing.T) {
|
||
app := simapp.Setup(t, false)
|
||
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
||
|
||
// reset fee pool
|
||
app.DistrKeeper.SetFeePool(ctx, disttypes.InitialFeePool())
|
||
|
||
addrs := simapp.AddTestAddrs(app, ctx, 3, sdk.NewInt(1234))
|
||
valAddrs := simapp.ConvertAddrsToValAddrs(addrs)
|
||
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
|
||
|
||
// create validator with 10% commission
|
||
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(1, 1), sdk.NewDecWithPrec(1, 1), sdk.NewDec(0))
|
||
tstaking.CreateValidator(valAddrs[0], valConsPk1, sdk.NewInt(110), true)
|
||
|
||
// create second validator with 10% commission
|
||
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(1, 1), sdk.NewDecWithPrec(1, 1), sdk.NewDec(0))
|
||
tstaking.CreateValidator(valAddrs[1], valConsPk2, sdk.NewInt(100), true)
|
||
|
||
// create third validator with 10% commission
|
||
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(1, 1), sdk.NewDecWithPrec(1, 1), sdk.NewDec(0))
|
||
tstaking.CreateValidator(valAddrs[2], valConsPk3, sdk.NewInt(100), true)
|
||
|
||
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
|
||
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())
|
||
|
||
// allocate tokens as if both had voted and second was proposer
|
||
fees := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(634195840)))
|
||
|
||
feeCollector := app.AccountKeeper.GetModuleAccount(ctx, types.FeeCollectorName)
|
||
require.NotNil(t, feeCollector)
|
||
|
||
require.NoError(t, testutil.FundModuleAccount(app.BankKeeper, ctx, feeCollector.GetName(), fees))
|
||
|
||
app.AccountKeeper.SetAccount(ctx, feeCollector)
|
||
|
||
votes := []abci.VoteInfo{
|
||
{
|
||
Validator: abciValA,
|
||
SignedLastBlock: true,
|
||
},
|
||
{
|
||
Validator: abciValB,
|
||
SignedLastBlock: true,
|
||
},
|
||
{
|
||
Validator: abciValС,
|
||
SignedLastBlock: true,
|
||
},
|
||
}
|
||
app.DistrKeeper.AllocateTokens(ctx, 31, 31, sdk.ConsAddress(valConsPk2.Address()), votes)
|
||
|
||
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())
|
||
}
|