refactor: use mocks for x/distribution (#12889)
* initial commit * add mocks * TestAllocateTokensToManyValidators * one file more in, like a hundred left * progress * progress * move tests to integration * finally got TestCalculateRewardsMultiDelegator right lol * small fixes * progress * progress * progress * progress * revert test panic * progress * progress * more progress * fix go.mod * merge * merge * cache issue? * fix go.sum * fix tests * make mocks * move back simulations Co-authored-by: Marko <marbar3778@yahoo.com>
This commit is contained in:
@@ -0,0 +1,248 @@
|
||||
package keeper_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"cosmossdk.io/math"
|
||||
"github.com/stretchr/testify/require"
|
||||
abci "github.com/tendermint/tendermint/abci/types"
|
||||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
||||
|
||||
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
|
||||
"github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
|
||||
banktestutil "github.com/cosmos/cosmos-sdk/x/bank/testutil"
|
||||
"github.com/cosmos/cosmos-sdk/x/distribution/keeper"
|
||||
"github.com/cosmos/cosmos-sdk/x/distribution/testutil"
|
||||
disttypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
|
||||
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
||||
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
|
||||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||
)
|
||||
|
||||
func TestAllocateTokensToValidatorWithCommission(t *testing.T) {
|
||||
var (
|
||||
bankKeeper bankkeeper.Keeper
|
||||
distrKeeper keeper.Keeper
|
||||
stakingKeeper *stakingkeeper.Keeper
|
||||
)
|
||||
|
||||
app, err := simtestutil.Setup(testutil.AppConfig,
|
||||
&bankKeeper,
|
||||
&distrKeeper,
|
||||
&stakingKeeper,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
||||
|
||||
addrs := simtestutil.AddTestAddrs(bankKeeper, stakingKeeper, ctx, 3, sdk.NewInt(1234))
|
||||
valAddrs := simtestutil.ConvertAddrsToValAddrs(addrs)
|
||||
tstaking := teststaking.NewHelper(t, ctx, stakingKeeper)
|
||||
|
||||
// create validator with 50% commission
|
||||
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), math.LegacyNewDec(0))
|
||||
tstaking.CreateValidator(sdk.ValAddress(addrs[0]), valConsPk0, sdk.NewInt(100), true)
|
||||
val := stakingKeeper.Validator(ctx, valAddrs[0])
|
||||
|
||||
// allocate tokens
|
||||
tokens := sdk.DecCoins{
|
||||
{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(10)},
|
||||
}
|
||||
distrKeeper.AllocateTokensToValidator(ctx, val, tokens)
|
||||
|
||||
// check commission
|
||||
expected := sdk.DecCoins{
|
||||
{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(5)},
|
||||
}
|
||||
require.Equal(t, expected, distrKeeper.GetValidatorAccumulatedCommission(ctx, val.GetOperator()).Commission)
|
||||
|
||||
// check current rewards
|
||||
require.Equal(t, expected, distrKeeper.GetValidatorCurrentRewards(ctx, val.GetOperator()).Rewards)
|
||||
}
|
||||
|
||||
func TestAllocateTokensToManyValidators(t *testing.T) {
|
||||
var (
|
||||
accountKeeper authkeeper.AccountKeeper
|
||||
bankKeeper bankkeeper.Keeper
|
||||
distrKeeper keeper.Keeper
|
||||
stakingKeeper *stakingkeeper.Keeper
|
||||
)
|
||||
|
||||
app, err := simtestutil.Setup(testutil.AppConfig,
|
||||
&accountKeeper,
|
||||
&bankKeeper,
|
||||
&distrKeeper,
|
||||
&stakingKeeper,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
||||
|
||||
// reset fee pool
|
||||
distrKeeper.SetFeePool(ctx, disttypes.InitialFeePool())
|
||||
|
||||
addrs := simtestutil.AddTestAddrs(bankKeeper, stakingKeeper, ctx, 2, sdk.NewInt(1234))
|
||||
valAddrs := simtestutil.ConvertAddrsToValAddrs(addrs)
|
||||
tstaking := teststaking.NewHelper(t, ctx, stakingKeeper)
|
||||
|
||||
// create validator with 50% commission
|
||||
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), math.LegacyNewDec(0))
|
||||
tstaking.CreateValidator(valAddrs[0], valConsPk0, sdk.NewInt(100), true)
|
||||
|
||||
// create second validator with 0% commission
|
||||
tstaking.Commission = stakingtypes.NewCommissionRates(math.LegacyNewDec(0), math.LegacyNewDec(0), math.LegacyNewDec(0))
|
||||
tstaking.CreateValidator(valAddrs[1], valConsPk1, sdk.NewInt(100), true)
|
||||
|
||||
abciValA := abci.Validator{
|
||||
Address: valConsPk0.Address(),
|
||||
Power: 100,
|
||||
}
|
||||
abciValB := abci.Validator{
|
||||
Address: valConsPk1.Address(),
|
||||
Power: 100,
|
||||
}
|
||||
|
||||
// assert initial state: zero outstanding rewards, zero community pool, zero commission, zero current rewards
|
||||
require.True(t, distrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[0]).Rewards.IsZero())
|
||||
require.True(t, distrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[1]).Rewards.IsZero())
|
||||
require.True(t, distrKeeper.GetFeePool(ctx).CommunityPool.IsZero())
|
||||
require.True(t, distrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[0]).Commission.IsZero())
|
||||
require.True(t, distrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[1]).Commission.IsZero())
|
||||
require.True(t, distrKeeper.GetValidatorCurrentRewards(ctx, valAddrs[0]).Rewards.IsZero())
|
||||
require.True(t, 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 := accountKeeper.GetModuleAccount(ctx, types.FeeCollectorName)
|
||||
require.NotNil(t, feeCollector)
|
||||
|
||||
// fund fee collector
|
||||
require.NoError(t, banktestutil.FundModuleAccount(bankKeeper, ctx, feeCollector.GetName(), fees))
|
||||
|
||||
accountKeeper.SetAccount(ctx, feeCollector)
|
||||
|
||||
votes := []abci.VoteInfo{
|
||||
{
|
||||
Validator: abciValA,
|
||||
SignedLastBlock: true,
|
||||
},
|
||||
{
|
||||
Validator: abciValB,
|
||||
SignedLastBlock: true,
|
||||
},
|
||||
}
|
||||
distrKeeper.AllocateTokens(ctx, 200, votes)
|
||||
|
||||
// 98 outstanding rewards (100 less 2 to community pool)
|
||||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecWithPrec(490, 1)}}, distrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[0]).Rewards)
|
||||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecWithPrec(490, 1)}}, distrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[1]).Rewards)
|
||||
|
||||
// 2 community pool coins
|
||||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(2)}}, distrKeeper.GetFeePool(ctx).CommunityPool)
|
||||
|
||||
// 50% commission for first proposer, (0.5 * 98%) * 100 / 2 = 23.25
|
||||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecWithPrec(2450, 2)}}, distrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[0]).Commission)
|
||||
|
||||
// zero commission for second proposer
|
||||
require.True(t, distrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[1]).Commission.IsZero())
|
||||
|
||||
// just staking.proportional for first proposer less commission = (0.5 * 98%) * 100 / 2 = 24.50
|
||||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecWithPrec(2450, 2)}}, distrKeeper.GetValidatorCurrentRewards(ctx, valAddrs[0]).Rewards)
|
||||
|
||||
// proposer reward + staking.proportional for second proposer = (0.5 * (98%)) * 100 = 49
|
||||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecWithPrec(490, 1)}}, distrKeeper.GetValidatorCurrentRewards(ctx, valAddrs[1]).Rewards)
|
||||
}
|
||||
|
||||
func TestAllocateTokensTruncation(t *testing.T) {
|
||||
var (
|
||||
accountKeeper authkeeper.AccountKeeper
|
||||
bankKeeper bankkeeper.Keeper
|
||||
distrKeeper keeper.Keeper
|
||||
stakingKeeper *stakingkeeper.Keeper
|
||||
)
|
||||
|
||||
app, err := simtestutil.Setup(testutil.AppConfig,
|
||||
&accountKeeper,
|
||||
&bankKeeper,
|
||||
&distrKeeper,
|
||||
&stakingKeeper,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
||||
|
||||
// reset fee pool
|
||||
distrKeeper.SetFeePool(ctx, disttypes.InitialFeePool())
|
||||
|
||||
addrs := simtestutil.AddTestAddrs(bankKeeper, stakingKeeper, ctx, 3, sdk.NewInt(1234))
|
||||
valAddrs := simtestutil.ConvertAddrsToValAddrs(addrs)
|
||||
tstaking := teststaking.NewHelper(t, ctx, stakingKeeper)
|
||||
|
||||
// create validator with 10% commission
|
||||
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(1, 1), sdk.NewDecWithPrec(1, 1), math.LegacyNewDec(0))
|
||||
tstaking.CreateValidator(valAddrs[0], valConsPk0, sdk.NewInt(110), true)
|
||||
|
||||
// create second validator with 10% commission
|
||||
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(1, 1), sdk.NewDecWithPrec(1, 1), math.LegacyNewDec(0))
|
||||
tstaking.CreateValidator(valAddrs[1], valConsPk1, sdk.NewInt(100), true)
|
||||
|
||||
// create third validator with 10% commission
|
||||
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(1, 1), sdk.NewDecWithPrec(1, 1), math.LegacyNewDec(0))
|
||||
tstaking.CreateValidator(valAddrs[2], valConsPk2, sdk.NewInt(100), true)
|
||||
|
||||
abciValA := abci.Validator{
|
||||
Address: valConsPk0.Address(),
|
||||
Power: 11,
|
||||
}
|
||||
abciValB := abci.Validator{
|
||||
Address: valConsPk1.Address(),
|
||||
Power: 10,
|
||||
}
|
||||
abciValС := abci.Validator{
|
||||
Address: valConsPk2.Address(),
|
||||
Power: 10,
|
||||
}
|
||||
|
||||
// assert initial state: zero outstanding rewards, zero community pool, zero commission, zero current rewards
|
||||
require.True(t, distrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[0]).Rewards.IsZero())
|
||||
require.True(t, distrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[1]).Rewards.IsZero())
|
||||
require.True(t, distrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[1]).Rewards.IsZero())
|
||||
require.True(t, distrKeeper.GetFeePool(ctx).CommunityPool.IsZero())
|
||||
require.True(t, distrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[0]).Commission.IsZero())
|
||||
require.True(t, distrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[1]).Commission.IsZero())
|
||||
require.True(t, distrKeeper.GetValidatorCurrentRewards(ctx, valAddrs[0]).Rewards.IsZero())
|
||||
require.True(t, 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 := accountKeeper.GetModuleAccount(ctx, types.FeeCollectorName)
|
||||
require.NotNil(t, feeCollector)
|
||||
|
||||
require.NoError(t, banktestutil.FundModuleAccount(bankKeeper, ctx, feeCollector.GetName(), fees))
|
||||
|
||||
accountKeeper.SetAccount(ctx, feeCollector)
|
||||
|
||||
votes := []abci.VoteInfo{
|
||||
{
|
||||
Validator: abciValA,
|
||||
SignedLastBlock: true,
|
||||
},
|
||||
{
|
||||
Validator: abciValB,
|
||||
SignedLastBlock: true,
|
||||
},
|
||||
{
|
||||
Validator: abciValС,
|
||||
SignedLastBlock: true,
|
||||
},
|
||||
}
|
||||
distrKeeper.AllocateTokens(ctx, 31, votes)
|
||||
|
||||
require.True(t, distrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[0]).Rewards.IsValid())
|
||||
require.True(t, distrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[1]).Rewards.IsValid())
|
||||
require.True(t, distrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[2]).Rewards.IsValid())
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package keeper_test
|
||||
|
||||
import (
|
||||
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/distribution/types"
|
||||
)
|
||||
|
||||
var (
|
||||
PKS = simtestutil.CreateTestPubKeys(5)
|
||||
|
||||
valConsPk0 = PKS[0]
|
||||
valConsPk1 = PKS[1]
|
||||
valConsPk2 = PKS[2]
|
||||
|
||||
valConsAddr0 = sdk.ConsAddress(valConsPk0.Address())
|
||||
valConsAddr1 = sdk.ConsAddress(valConsPk1.Address())
|
||||
|
||||
distrAcc = authtypes.NewEmptyModuleAccount(types.ModuleName)
|
||||
)
|
||||
@@ -0,0 +1,824 @@
|
||||
package keeper_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"cosmossdk.io/math"
|
||||
"github.com/stretchr/testify/require"
|
||||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
||||
|
||||
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
|
||||
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
|
||||
banktestutil "github.com/cosmos/cosmos-sdk/x/bank/testutil"
|
||||
"github.com/cosmos/cosmos-sdk/x/distribution/keeper"
|
||||
"github.com/cosmos/cosmos-sdk/x/distribution/testutil"
|
||||
"github.com/cosmos/cosmos-sdk/x/staking"
|
||||
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
||||
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
|
||||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||
)
|
||||
|
||||
func TestCalculateRewardsBasic(t *testing.T) {
|
||||
var (
|
||||
bankKeeper bankkeeper.Keeper
|
||||
distrKeeper keeper.Keeper
|
||||
stakingKeeper *stakingkeeper.Keeper
|
||||
)
|
||||
|
||||
app, err := simtestutil.Setup(testutil.AppConfig,
|
||||
&bankKeeper,
|
||||
&distrKeeper,
|
||||
&stakingKeeper,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
||||
|
||||
distrKeeper.DeleteAllValidatorHistoricalRewards(ctx)
|
||||
|
||||
tstaking := teststaking.NewHelper(t, ctx, stakingKeeper)
|
||||
|
||||
addr := simtestutil.AddTestAddrs(bankKeeper, stakingKeeper, ctx, 2, sdk.NewInt(1000))
|
||||
valAddrs := simtestutil.ConvertAddrsToValAddrs(addr)
|
||||
|
||||
// create validator with 50% commission
|
||||
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), math.LegacyNewDec(0))
|
||||
tstaking.CreateValidator(valAddrs[0], valConsPk0, sdk.NewInt(100), true)
|
||||
|
||||
// end block to bond validator and start new block
|
||||
staking.EndBlocker(ctx, stakingKeeper)
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
|
||||
tstaking.Ctx = ctx
|
||||
|
||||
// fetch validator and delegation
|
||||
val := stakingKeeper.Validator(ctx, valAddrs[0])
|
||||
del := stakingKeeper.Delegation(ctx, sdk.AccAddress(valAddrs[0]), valAddrs[0])
|
||||
|
||||
// historical count should be 2 (once for validator init, once for delegation init)
|
||||
require.Equal(t, uint64(2), distrKeeper.GetValidatorHistoricalReferenceCount(ctx))
|
||||
|
||||
// end period
|
||||
endingPeriod := distrKeeper.IncrementValidatorPeriod(ctx, val)
|
||||
|
||||
// historical count should be 2 still
|
||||
require.Equal(t, uint64(2), distrKeeper.GetValidatorHistoricalReferenceCount(ctx))
|
||||
|
||||
// calculate delegation rewards
|
||||
rewards := distrKeeper.CalculateDelegationRewards(ctx, val, del, endingPeriod)
|
||||
|
||||
// rewards should be zero
|
||||
require.True(t, rewards.IsZero())
|
||||
|
||||
// allocate some rewards
|
||||
initial := int64(10)
|
||||
tokens := sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(initial)}}
|
||||
distrKeeper.AllocateTokensToValidator(ctx, val, tokens)
|
||||
|
||||
// end period
|
||||
endingPeriod = distrKeeper.IncrementValidatorPeriod(ctx, val)
|
||||
|
||||
// calculate delegation rewards
|
||||
rewards = distrKeeper.CalculateDelegationRewards(ctx, val, del, endingPeriod)
|
||||
|
||||
// rewards should be half the tokens
|
||||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(initial / 2)}}, rewards)
|
||||
|
||||
// commission should be the other half
|
||||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(initial / 2)}}, distrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[0]).Commission)
|
||||
}
|
||||
|
||||
func TestCalculateRewardsAfterSlash(t *testing.T) {
|
||||
var (
|
||||
bankKeeper bankkeeper.Keeper
|
||||
distrKeeper keeper.Keeper
|
||||
stakingKeeper *stakingkeeper.Keeper
|
||||
)
|
||||
|
||||
app, err := simtestutil.Setup(testutil.AppConfig,
|
||||
&bankKeeper,
|
||||
&distrKeeper,
|
||||
&stakingKeeper,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
||||
|
||||
addr := simtestutil.AddTestAddrs(bankKeeper, stakingKeeper, ctx, 2, sdk.NewInt(100000000))
|
||||
valAddrs := simtestutil.ConvertAddrsToValAddrs(addr)
|
||||
tstaking := teststaking.NewHelper(t, ctx, stakingKeeper)
|
||||
|
||||
// create validator with 50% commission
|
||||
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), math.LegacyNewDec(0))
|
||||
valPower := int64(100)
|
||||
tstaking.CreateValidatorWithValPower(valAddrs[0], valConsPk0, valPower, true)
|
||||
|
||||
// end block to bond validator
|
||||
staking.EndBlocker(ctx, stakingKeeper)
|
||||
|
||||
// next block
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
|
||||
|
||||
// fetch validator and delegation
|
||||
val := stakingKeeper.Validator(ctx, valAddrs[0])
|
||||
del := stakingKeeper.Delegation(ctx, sdk.AccAddress(valAddrs[0]), valAddrs[0])
|
||||
|
||||
// end period
|
||||
endingPeriod := distrKeeper.IncrementValidatorPeriod(ctx, val)
|
||||
|
||||
// calculate delegation rewards
|
||||
rewards := distrKeeper.CalculateDelegationRewards(ctx, val, del, endingPeriod)
|
||||
|
||||
// rewards should be zero
|
||||
require.True(t, rewards.IsZero())
|
||||
|
||||
// start out block height
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 3)
|
||||
|
||||
// slash the validator by 50%
|
||||
stakingKeeper.Slash(ctx, valConsAddr0, ctx.BlockHeight(), valPower, sdk.NewDecWithPrec(5, 1))
|
||||
|
||||
// retrieve validator
|
||||
val = stakingKeeper.Validator(ctx, valAddrs[0])
|
||||
|
||||
// increase block height
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 3)
|
||||
|
||||
// allocate some rewards
|
||||
initial := stakingKeeper.TokensFromConsensusPower(ctx, 10)
|
||||
tokens := sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecFromInt(initial)}}
|
||||
distrKeeper.AllocateTokensToValidator(ctx, val, tokens)
|
||||
|
||||
// end period
|
||||
endingPeriod = distrKeeper.IncrementValidatorPeriod(ctx, val)
|
||||
|
||||
// calculate delegation rewards
|
||||
rewards = distrKeeper.CalculateDelegationRewards(ctx, val, del, endingPeriod)
|
||||
|
||||
// rewards should be half the tokens
|
||||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecFromInt(initial.QuoRaw(2))}}, rewards)
|
||||
|
||||
// commission should be the other half
|
||||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecFromInt(initial.QuoRaw(2))}},
|
||||
distrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[0]).Commission)
|
||||
}
|
||||
|
||||
func TestCalculateRewardsAfterManySlashes(t *testing.T) {
|
||||
var (
|
||||
bankKeeper bankkeeper.Keeper
|
||||
distrKeeper keeper.Keeper
|
||||
stakingKeeper *stakingkeeper.Keeper
|
||||
)
|
||||
|
||||
app, err := simtestutil.Setup(testutil.AppConfig,
|
||||
&bankKeeper,
|
||||
&distrKeeper,
|
||||
&stakingKeeper,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
||||
|
||||
tstaking := teststaking.NewHelper(t, ctx, stakingKeeper)
|
||||
addr := simtestutil.AddTestAddrs(bankKeeper, stakingKeeper, ctx, 2, sdk.NewInt(100000000))
|
||||
valAddrs := simtestutil.ConvertAddrsToValAddrs(addr)
|
||||
|
||||
// create validator with 50% commission
|
||||
valPower := int64(100)
|
||||
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), math.LegacyNewDec(0))
|
||||
tstaking.CreateValidatorWithValPower(valAddrs[0], valConsPk0, valPower, true)
|
||||
|
||||
// end block to bond validator
|
||||
staking.EndBlocker(ctx, stakingKeeper)
|
||||
|
||||
// next block
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
|
||||
|
||||
// fetch validator and delegation
|
||||
val := stakingKeeper.Validator(ctx, valAddrs[0])
|
||||
del := stakingKeeper.Delegation(ctx, sdk.AccAddress(valAddrs[0]), valAddrs[0])
|
||||
|
||||
// end period
|
||||
endingPeriod := distrKeeper.IncrementValidatorPeriod(ctx, val)
|
||||
|
||||
// calculate delegation rewards
|
||||
rewards := distrKeeper.CalculateDelegationRewards(ctx, val, del, endingPeriod)
|
||||
|
||||
// rewards should be zero
|
||||
require.True(t, rewards.IsZero())
|
||||
|
||||
// start out block height
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 3)
|
||||
|
||||
// slash the validator by 50%
|
||||
stakingKeeper.Slash(ctx, valConsAddr0, ctx.BlockHeight(), valPower, sdk.NewDecWithPrec(5, 1))
|
||||
|
||||
// fetch the validator again
|
||||
val = stakingKeeper.Validator(ctx, valAddrs[0])
|
||||
|
||||
// increase block height
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 3)
|
||||
|
||||
// allocate some rewards
|
||||
initial := stakingKeeper.TokensFromConsensusPower(ctx, 10)
|
||||
tokens := sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecFromInt(initial)}}
|
||||
distrKeeper.AllocateTokensToValidator(ctx, val, tokens)
|
||||
|
||||
// slash the validator by 50% again
|
||||
stakingKeeper.Slash(ctx, valConsAddr0, ctx.BlockHeight(), valPower/2, sdk.NewDecWithPrec(5, 1))
|
||||
|
||||
// fetch the validator again
|
||||
val = stakingKeeper.Validator(ctx, valAddrs[0])
|
||||
|
||||
// increase block height
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 3)
|
||||
|
||||
// allocate some more rewards
|
||||
distrKeeper.AllocateTokensToValidator(ctx, val, tokens)
|
||||
|
||||
// end period
|
||||
endingPeriod = distrKeeper.IncrementValidatorPeriod(ctx, val)
|
||||
|
||||
// calculate delegation rewards
|
||||
rewards = distrKeeper.CalculateDelegationRewards(ctx, val, del, endingPeriod)
|
||||
|
||||
// rewards should be half the tokens
|
||||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecFromInt(initial)}}, rewards)
|
||||
|
||||
// commission should be the other half
|
||||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: sdk.NewDecFromInt(initial)}},
|
||||
distrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[0]).Commission)
|
||||
}
|
||||
|
||||
func TestCalculateRewardsMultiDelegator(t *testing.T) {
|
||||
var (
|
||||
bankKeeper bankkeeper.Keeper
|
||||
distrKeeper keeper.Keeper
|
||||
stakingKeeper *stakingkeeper.Keeper
|
||||
)
|
||||
|
||||
app, err := simtestutil.Setup(testutil.AppConfig,
|
||||
&bankKeeper,
|
||||
&distrKeeper,
|
||||
&stakingKeeper,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
||||
|
||||
tstaking := teststaking.NewHelper(t, ctx, stakingKeeper)
|
||||
addr := simtestutil.AddTestAddrs(bankKeeper, stakingKeeper, ctx, 2, sdk.NewInt(100000000))
|
||||
valAddrs := simtestutil.ConvertAddrsToValAddrs(addr)
|
||||
|
||||
// create validator with 50% commission
|
||||
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), math.LegacyNewDec(0))
|
||||
tstaking.CreateValidator(valAddrs[0], valConsPk0, sdk.NewInt(100), true)
|
||||
|
||||
// end block to bond validator
|
||||
staking.EndBlocker(ctx, stakingKeeper)
|
||||
|
||||
// next block
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
|
||||
|
||||
// fetch validator and delegation
|
||||
val := stakingKeeper.Validator(ctx, valAddrs[0])
|
||||
del1 := stakingKeeper.Delegation(ctx, sdk.AccAddress(valAddrs[0]), valAddrs[0])
|
||||
|
||||
// allocate some rewards
|
||||
initial := int64(20)
|
||||
tokens := sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(initial)}}
|
||||
distrKeeper.AllocateTokensToValidator(ctx, val, tokens)
|
||||
|
||||
// second delegation
|
||||
tstaking.Ctx = ctx
|
||||
tstaking.Delegate(sdk.AccAddress(valAddrs[1]), valAddrs[0], sdk.NewInt(100))
|
||||
del2 := stakingKeeper.Delegation(ctx, sdk.AccAddress(valAddrs[1]), valAddrs[0])
|
||||
|
||||
// fetch updated validator
|
||||
val = stakingKeeper.Validator(ctx, valAddrs[0])
|
||||
|
||||
// end block
|
||||
staking.EndBlocker(ctx, stakingKeeper)
|
||||
|
||||
// next block
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
|
||||
|
||||
// allocate some more rewards
|
||||
distrKeeper.AllocateTokensToValidator(ctx, val, tokens)
|
||||
|
||||
// end period
|
||||
endingPeriod := distrKeeper.IncrementValidatorPeriod(ctx, val)
|
||||
|
||||
// calculate delegation rewards for del1
|
||||
rewards := distrKeeper.CalculateDelegationRewards(ctx, val, del1, endingPeriod)
|
||||
|
||||
// rewards for del1 should be 3/4 initial
|
||||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(initial * 3 / 4)}}, rewards)
|
||||
|
||||
// calculate delegation rewards for del2
|
||||
rewards = distrKeeper.CalculateDelegationRewards(ctx, val, del2, endingPeriod)
|
||||
|
||||
// rewards for del2 should be 1/4 initial
|
||||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(initial * 1 / 4)}}, rewards)
|
||||
|
||||
// commission should be equal to initial (50% twice)
|
||||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(initial)}}, distrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[0]).Commission)
|
||||
}
|
||||
|
||||
func TestWithdrawDelegationRewardsBasic(t *testing.T) {
|
||||
var (
|
||||
accountKeeper authkeeper.AccountKeeper
|
||||
bankKeeper bankkeeper.Keeper
|
||||
distrKeeper keeper.Keeper
|
||||
stakingKeeper *stakingkeeper.Keeper
|
||||
)
|
||||
|
||||
app, err := simtestutil.Setup(testutil.AppConfig,
|
||||
&accountKeeper,
|
||||
&bankKeeper,
|
||||
&distrKeeper,
|
||||
&stakingKeeper,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
||||
|
||||
distrKeeper.DeleteAllValidatorHistoricalRewards(ctx)
|
||||
|
||||
balancePower := int64(1000)
|
||||
balanceTokens := stakingKeeper.TokensFromConsensusPower(ctx, balancePower)
|
||||
addr := simtestutil.AddTestAddrs(bankKeeper, stakingKeeper, ctx, 1, sdk.NewInt(1000000000))
|
||||
valAddrs := simtestutil.ConvertAddrsToValAddrs(addr)
|
||||
tstaking := teststaking.NewHelper(t, ctx, stakingKeeper)
|
||||
|
||||
// set module account coins
|
||||
distrAcc := distrKeeper.GetDistributionAccount(ctx)
|
||||
require.NoError(t, banktestutil.FundModuleAccount(bankKeeper, ctx, distrAcc.GetName(), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, balanceTokens))))
|
||||
accountKeeper.SetModuleAccount(ctx, distrAcc)
|
||||
|
||||
// create validator with 50% commission
|
||||
power := int64(100)
|
||||
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), math.LegacyNewDec(0))
|
||||
valTokens := tstaking.CreateValidatorWithValPower(valAddrs[0], valConsPk0, power, true)
|
||||
|
||||
// assert correct initial balance
|
||||
expTokens := balanceTokens.Sub(valTokens)
|
||||
require.Equal(t,
|
||||
sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, expTokens)},
|
||||
bankKeeper.GetAllBalances(ctx, sdk.AccAddress(valAddrs[0])),
|
||||
)
|
||||
|
||||
// end block to bond validator
|
||||
staking.EndBlocker(ctx, stakingKeeper)
|
||||
|
||||
// next block
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
|
||||
|
||||
// fetch validator and delegation
|
||||
val := stakingKeeper.Validator(ctx, valAddrs[0])
|
||||
|
||||
// allocate some rewards
|
||||
initial := stakingKeeper.TokensFromConsensusPower(ctx, 10)
|
||||
tokens := sdk.DecCoins{sdk.NewDecCoin(sdk.DefaultBondDenom, initial)}
|
||||
|
||||
distrKeeper.AllocateTokensToValidator(ctx, val, tokens)
|
||||
|
||||
// historical count should be 2 (initial + latest for delegation)
|
||||
require.Equal(t, uint64(2), distrKeeper.GetValidatorHistoricalReferenceCount(ctx))
|
||||
|
||||
// withdraw rewards
|
||||
_, err = distrKeeper.WithdrawDelegationRewards(ctx, sdk.AccAddress(valAddrs[0]), valAddrs[0])
|
||||
require.Nil(t, err)
|
||||
|
||||
// historical count should still be 2 (added one record, cleared one)
|
||||
require.Equal(t, uint64(2), distrKeeper.GetValidatorHistoricalReferenceCount(ctx))
|
||||
|
||||
// assert correct balance
|
||||
exp := balanceTokens.Sub(valTokens).Add(initial.QuoRaw(2))
|
||||
require.Equal(t,
|
||||
sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, exp)},
|
||||
bankKeeper.GetAllBalances(ctx, sdk.AccAddress(valAddrs[0])),
|
||||
)
|
||||
|
||||
// withdraw commission
|
||||
_, err = distrKeeper.WithdrawValidatorCommission(ctx, valAddrs[0])
|
||||
require.Nil(t, err)
|
||||
|
||||
// assert correct balance
|
||||
exp = balanceTokens.Sub(valTokens).Add(initial)
|
||||
require.Equal(t,
|
||||
sdk.Coins{sdk.NewCoin(sdk.DefaultBondDenom, exp)},
|
||||
bankKeeper.GetAllBalances(ctx, sdk.AccAddress(valAddrs[0])),
|
||||
)
|
||||
}
|
||||
|
||||
func TestCalculateRewardsAfterManySlashesInSameBlock(t *testing.T) {
|
||||
var (
|
||||
bankKeeper bankkeeper.Keeper
|
||||
distrKeeper keeper.Keeper
|
||||
stakingKeeper *stakingkeeper.Keeper
|
||||
)
|
||||
|
||||
app, err := simtestutil.Setup(testutil.AppConfig,
|
||||
&bankKeeper,
|
||||
&distrKeeper,
|
||||
&stakingKeeper,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
||||
|
||||
addr := simtestutil.AddTestAddrs(bankKeeper, stakingKeeper, ctx, 1, sdk.NewInt(1000000000))
|
||||
valAddrs := simtestutil.ConvertAddrsToValAddrs(addr)
|
||||
tstaking := teststaking.NewHelper(t, ctx, stakingKeeper)
|
||||
|
||||
// create validator with 50% commission
|
||||
valPower := int64(100)
|
||||
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), math.LegacyNewDec(0))
|
||||
tstaking.CreateValidatorWithValPower(valAddrs[0], valConsPk0, valPower, true)
|
||||
|
||||
// end block to bond validator
|
||||
staking.EndBlocker(ctx, stakingKeeper)
|
||||
|
||||
// next block
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
|
||||
|
||||
// fetch validator and delegation
|
||||
val := stakingKeeper.Validator(ctx, valAddrs[0])
|
||||
del := stakingKeeper.Delegation(ctx, sdk.AccAddress(valAddrs[0]), valAddrs[0])
|
||||
|
||||
// end period
|
||||
endingPeriod := distrKeeper.IncrementValidatorPeriod(ctx, val)
|
||||
|
||||
// calculate delegation rewards
|
||||
rewards := distrKeeper.CalculateDelegationRewards(ctx, val, del, endingPeriod)
|
||||
|
||||
// rewards should be zero
|
||||
require.True(t, rewards.IsZero())
|
||||
|
||||
// start out block height
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 3)
|
||||
|
||||
// allocate some rewards
|
||||
initial := sdk.NewDecFromInt(stakingKeeper.TokensFromConsensusPower(ctx, 10))
|
||||
tokens := sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: initial}}
|
||||
distrKeeper.AllocateTokensToValidator(ctx, val, tokens)
|
||||
|
||||
// slash the validator by 50%
|
||||
stakingKeeper.Slash(ctx, valConsAddr0, ctx.BlockHeight(), valPower, sdk.NewDecWithPrec(5, 1))
|
||||
|
||||
// slash the validator by 50% again
|
||||
stakingKeeper.Slash(ctx, valConsAddr0, ctx.BlockHeight(), valPower/2, sdk.NewDecWithPrec(5, 1))
|
||||
|
||||
// fetch the validator again
|
||||
val = stakingKeeper.Validator(ctx, valAddrs[0])
|
||||
|
||||
// increase block height
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 3)
|
||||
|
||||
// allocate some more rewards
|
||||
distrKeeper.AllocateTokensToValidator(ctx, val, tokens)
|
||||
|
||||
// end period
|
||||
endingPeriod = distrKeeper.IncrementValidatorPeriod(ctx, val)
|
||||
|
||||
// calculate delegation rewards
|
||||
rewards = distrKeeper.CalculateDelegationRewards(ctx, val, del, endingPeriod)
|
||||
|
||||
// rewards should be half the tokens
|
||||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: initial}}, rewards)
|
||||
|
||||
// commission should be the other half
|
||||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: initial}}, distrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[0]).Commission)
|
||||
}
|
||||
|
||||
func TestCalculateRewardsMultiDelegatorMultiSlash(t *testing.T) {
|
||||
var (
|
||||
bankKeeper bankkeeper.Keeper
|
||||
distrKeeper keeper.Keeper
|
||||
stakingKeeper *stakingkeeper.Keeper
|
||||
)
|
||||
|
||||
app, err := simtestutil.Setup(testutil.AppConfig,
|
||||
&bankKeeper,
|
||||
&distrKeeper,
|
||||
&stakingKeeper,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
||||
|
||||
tstaking := teststaking.NewHelper(t, ctx, stakingKeeper)
|
||||
addr := simtestutil.AddTestAddrs(bankKeeper, stakingKeeper, ctx, 2, sdk.NewInt(1000000000))
|
||||
valAddrs := simtestutil.ConvertAddrsToValAddrs(addr)
|
||||
|
||||
// create validator with 50% commission
|
||||
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), math.LegacyNewDec(0))
|
||||
valPower := int64(100)
|
||||
tstaking.CreateValidatorWithValPower(valAddrs[0], valConsPk0, valPower, true)
|
||||
|
||||
// end block to bond validator
|
||||
staking.EndBlocker(ctx, stakingKeeper)
|
||||
|
||||
// next block
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
|
||||
|
||||
// fetch validator and delegation
|
||||
val := stakingKeeper.Validator(ctx, valAddrs[0])
|
||||
del1 := stakingKeeper.Delegation(ctx, sdk.AccAddress(valAddrs[0]), valAddrs[0])
|
||||
|
||||
// allocate some rewards
|
||||
initial := sdk.NewDecFromInt(stakingKeeper.TokensFromConsensusPower(ctx, 30))
|
||||
tokens := sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: initial}}
|
||||
distrKeeper.AllocateTokensToValidator(ctx, val, tokens)
|
||||
|
||||
// slash the validator
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 3)
|
||||
stakingKeeper.Slash(ctx, valConsAddr0, ctx.BlockHeight(), valPower, sdk.NewDecWithPrec(5, 1))
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 3)
|
||||
|
||||
// second delegation
|
||||
tstaking.DelegateWithPower(sdk.AccAddress(valAddrs[1]), valAddrs[0], 100)
|
||||
|
||||
del2 := stakingKeeper.Delegation(ctx, sdk.AccAddress(valAddrs[1]), valAddrs[0])
|
||||
|
||||
// end block
|
||||
staking.EndBlocker(ctx, stakingKeeper)
|
||||
|
||||
// next block
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
|
||||
|
||||
// allocate some more rewards
|
||||
distrKeeper.AllocateTokensToValidator(ctx, val, tokens)
|
||||
|
||||
// slash the validator again
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 3)
|
||||
stakingKeeper.Slash(ctx, valConsAddr0, ctx.BlockHeight(), valPower, sdk.NewDecWithPrec(5, 1))
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 3)
|
||||
|
||||
// fetch updated validator
|
||||
val = stakingKeeper.Validator(ctx, valAddrs[0])
|
||||
|
||||
// end period
|
||||
endingPeriod := distrKeeper.IncrementValidatorPeriod(ctx, val)
|
||||
|
||||
// calculate delegation rewards for del1
|
||||
rewards := distrKeeper.CalculateDelegationRewards(ctx, val, del1, endingPeriod)
|
||||
|
||||
// rewards for del1 should be 2/3 initial (half initial first period, 1/6 initial second period)
|
||||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: initial.QuoInt64(2).Add(initial.QuoInt64(6))}}, rewards)
|
||||
|
||||
// calculate delegation rewards for del2
|
||||
rewards = distrKeeper.CalculateDelegationRewards(ctx, val, del2, endingPeriod)
|
||||
|
||||
// rewards for del2 should be initial / 3
|
||||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: initial.QuoInt64(3)}}, rewards)
|
||||
|
||||
// commission should be equal to initial (twice 50% commission, unaffected by slashing)
|
||||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: initial}}, distrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[0]).Commission)
|
||||
}
|
||||
|
||||
func TestCalculateRewardsMultiDelegatorMultWithdraw(t *testing.T) {
|
||||
var (
|
||||
accountKeeper authkeeper.AccountKeeper
|
||||
bankKeeper bankkeeper.Keeper
|
||||
distrKeeper keeper.Keeper
|
||||
stakingKeeper *stakingkeeper.Keeper
|
||||
)
|
||||
|
||||
app, err := simtestutil.Setup(testutil.AppConfig,
|
||||
&accountKeeper,
|
||||
&bankKeeper,
|
||||
&distrKeeper,
|
||||
&stakingKeeper,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
||||
|
||||
distrKeeper.DeleteAllValidatorHistoricalRewards(ctx)
|
||||
|
||||
tstaking := teststaking.NewHelper(t, ctx, stakingKeeper)
|
||||
addr := simtestutil.AddTestAddrs(bankKeeper, stakingKeeper, ctx, 2, sdk.NewInt(1000000000))
|
||||
valAddrs := simtestutil.ConvertAddrsToValAddrs(addr)
|
||||
initial := int64(20)
|
||||
|
||||
// set module account coins
|
||||
distrAcc := distrKeeper.GetDistributionAccount(ctx)
|
||||
require.NoError(t, banktestutil.FundModuleAccount(bankKeeper, ctx, distrAcc.GetName(), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1000)))))
|
||||
accountKeeper.SetModuleAccount(ctx, distrAcc)
|
||||
|
||||
tokens := sdk.DecCoins{sdk.NewDecCoinFromDec(sdk.DefaultBondDenom, math.LegacyNewDec(initial))}
|
||||
|
||||
// create validator with 50% commission
|
||||
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), math.LegacyNewDec(0))
|
||||
tstaking.CreateValidator(valAddrs[0], valConsPk0, sdk.NewInt(100), true)
|
||||
|
||||
// end block to bond validator
|
||||
staking.EndBlocker(ctx, stakingKeeper)
|
||||
|
||||
// next block
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
|
||||
|
||||
// fetch validator and delegation
|
||||
val := stakingKeeper.Validator(ctx, valAddrs[0])
|
||||
del1 := stakingKeeper.Delegation(ctx, sdk.AccAddress(valAddrs[0]), valAddrs[0])
|
||||
|
||||
// allocate some rewards
|
||||
distrKeeper.AllocateTokensToValidator(ctx, val, tokens)
|
||||
|
||||
// historical count should be 2 (validator init, delegation init)
|
||||
require.Equal(t, uint64(2), distrKeeper.GetValidatorHistoricalReferenceCount(ctx))
|
||||
|
||||
// second delegation
|
||||
tstaking.Delegate(sdk.AccAddress(valAddrs[1]), valAddrs[0], sdk.NewInt(100))
|
||||
|
||||
// historical count should be 3 (second delegation init)
|
||||
require.Equal(t, uint64(3), distrKeeper.GetValidatorHistoricalReferenceCount(ctx))
|
||||
|
||||
// fetch updated validator
|
||||
val = stakingKeeper.Validator(ctx, valAddrs[0])
|
||||
del2 := stakingKeeper.Delegation(ctx, sdk.AccAddress(valAddrs[1]), valAddrs[0])
|
||||
|
||||
// end block
|
||||
staking.EndBlocker(ctx, stakingKeeper)
|
||||
|
||||
// next block
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
|
||||
|
||||
// allocate some more rewards
|
||||
distrKeeper.AllocateTokensToValidator(ctx, val, tokens)
|
||||
|
||||
// first delegator withdraws
|
||||
_, err = distrKeeper.WithdrawDelegationRewards(ctx, sdk.AccAddress(valAddrs[0]), valAddrs[0])
|
||||
require.NoError(t, err)
|
||||
|
||||
// second delegator withdraws
|
||||
_, err = distrKeeper.WithdrawDelegationRewards(ctx, sdk.AccAddress(valAddrs[1]), valAddrs[0])
|
||||
require.NoError(t, err)
|
||||
|
||||
// historical count should be 3 (validator init + two delegations)
|
||||
require.Equal(t, uint64(3), distrKeeper.GetValidatorHistoricalReferenceCount(ctx))
|
||||
|
||||
// validator withdraws commission
|
||||
_, err = distrKeeper.WithdrawValidatorCommission(ctx, valAddrs[0])
|
||||
require.NoError(t, err)
|
||||
|
||||
// end period
|
||||
endingPeriod := distrKeeper.IncrementValidatorPeriod(ctx, val)
|
||||
|
||||
// calculate delegation rewards for del1
|
||||
rewards := distrKeeper.CalculateDelegationRewards(ctx, val, del1, endingPeriod)
|
||||
|
||||
// rewards for del1 should be zero
|
||||
require.True(t, rewards.IsZero())
|
||||
|
||||
// calculate delegation rewards for del2
|
||||
rewards = distrKeeper.CalculateDelegationRewards(ctx, val, del2, endingPeriod)
|
||||
|
||||
// rewards for del2 should be zero
|
||||
require.True(t, rewards.IsZero())
|
||||
|
||||
// commission should be zero
|
||||
require.True(t, distrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[0]).Commission.IsZero())
|
||||
|
||||
// next block
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
|
||||
|
||||
// allocate some more rewards
|
||||
distrKeeper.AllocateTokensToValidator(ctx, val, tokens)
|
||||
|
||||
// first delegator withdraws again
|
||||
_, err = distrKeeper.WithdrawDelegationRewards(ctx, sdk.AccAddress(valAddrs[0]), valAddrs[0])
|
||||
require.NoError(t, err)
|
||||
|
||||
// end period
|
||||
endingPeriod = distrKeeper.IncrementValidatorPeriod(ctx, val)
|
||||
|
||||
// calculate delegation rewards for del1
|
||||
rewards = distrKeeper.CalculateDelegationRewards(ctx, val, del1, endingPeriod)
|
||||
|
||||
// rewards for del1 should be zero
|
||||
require.True(t, rewards.IsZero())
|
||||
|
||||
// calculate delegation rewards for del2
|
||||
rewards = distrKeeper.CalculateDelegationRewards(ctx, val, del2, endingPeriod)
|
||||
|
||||
// rewards for del2 should be 1/4 initial
|
||||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(initial / 4)}}, rewards)
|
||||
|
||||
// commission should be half initial
|
||||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(initial / 2)}}, distrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[0]).Commission)
|
||||
|
||||
// next block
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
|
||||
|
||||
// allocate some more rewards
|
||||
distrKeeper.AllocateTokensToValidator(ctx, val, tokens)
|
||||
|
||||
// withdraw commission
|
||||
_, err = distrKeeper.WithdrawValidatorCommission(ctx, valAddrs[0])
|
||||
require.NoError(t, err)
|
||||
|
||||
// end period
|
||||
endingPeriod = distrKeeper.IncrementValidatorPeriod(ctx, val)
|
||||
|
||||
// calculate delegation rewards for del1
|
||||
rewards = distrKeeper.CalculateDelegationRewards(ctx, val, del1, endingPeriod)
|
||||
|
||||
// rewards for del1 should be 1/4 initial
|
||||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(initial / 4)}}, rewards)
|
||||
|
||||
// calculate delegation rewards for del2
|
||||
rewards = distrKeeper.CalculateDelegationRewards(ctx, val, del2, endingPeriod)
|
||||
|
||||
// rewards for del2 should be 1/2 initial
|
||||
require.Equal(t, sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(initial / 2)}}, rewards)
|
||||
|
||||
// commission should be zero
|
||||
require.True(t, distrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[0]).Commission.IsZero())
|
||||
}
|
||||
|
||||
func Test100PercentCommissionReward(t *testing.T) {
|
||||
var (
|
||||
accountKeeper authkeeper.AccountKeeper
|
||||
bankKeeper bankkeeper.Keeper
|
||||
distrKeeper keeper.Keeper
|
||||
stakingKeeper *stakingkeeper.Keeper
|
||||
)
|
||||
|
||||
app, err := simtestutil.Setup(testutil.AppConfig,
|
||||
&accountKeeper,
|
||||
&bankKeeper,
|
||||
&distrKeeper,
|
||||
&stakingKeeper,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
||||
|
||||
tstaking := teststaking.NewHelper(t, ctx, stakingKeeper)
|
||||
addr := simtestutil.AddTestAddrs(bankKeeper, stakingKeeper, ctx, 2, sdk.NewInt(1000000000))
|
||||
valAddrs := simtestutil.ConvertAddrsToValAddrs(addr)
|
||||
initial := int64(20)
|
||||
|
||||
// set module account coins
|
||||
distrAcc := distrKeeper.GetDistributionAccount(ctx)
|
||||
require.NoError(t, banktestutil.FundModuleAccount(bankKeeper, ctx, distrAcc.GetName(), sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1000)))))
|
||||
accountKeeper.SetModuleAccount(ctx, distrAcc)
|
||||
|
||||
tokens := sdk.DecCoins{sdk.NewDecCoinFromDec(sdk.DefaultBondDenom, math.LegacyNewDec(initial))}
|
||||
|
||||
// create validator with 100% commission
|
||||
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(10, 1), sdk.NewDecWithPrec(10, 1), math.LegacyNewDec(0))
|
||||
tstaking.CreateValidator(valAddrs[0], valConsPk0, sdk.NewInt(100), true)
|
||||
stakingKeeper.Delegation(ctx, sdk.AccAddress(valAddrs[0]), valAddrs[0])
|
||||
|
||||
// end block to bond validator
|
||||
staking.EndBlocker(ctx, stakingKeeper)
|
||||
// next block
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
|
||||
|
||||
// fetch validator
|
||||
val := stakingKeeper.Validator(ctx, valAddrs[0])
|
||||
|
||||
// allocate some rewards
|
||||
distrKeeper.AllocateTokensToValidator(ctx, val, tokens)
|
||||
|
||||
// end block
|
||||
staking.EndBlocker(ctx, stakingKeeper)
|
||||
|
||||
// next block
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
|
||||
|
||||
// allocate some more rewards
|
||||
distrKeeper.AllocateTokensToValidator(ctx, val, tokens)
|
||||
|
||||
// next block
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
|
||||
|
||||
// allocate some more rewards
|
||||
distrKeeper.AllocateTokensToValidator(ctx, val, tokens)
|
||||
|
||||
rewards, err := distrKeeper.WithdrawDelegationRewards(ctx, sdk.AccAddress(valAddrs[0]), valAddrs[0])
|
||||
require.NoError(t, err)
|
||||
|
||||
denom, _ := sdk.GetBaseDenom()
|
||||
zeroRewards := sdk.Coins{
|
||||
sdk.Coin{
|
||||
Denom: denom,
|
||||
Amount: math.ZeroInt(),
|
||||
},
|
||||
}
|
||||
require.True(t, rewards.IsEqual(zeroRewards))
|
||||
events := ctx.EventManager().Events()
|
||||
lastEvent := events[len(events)-1]
|
||||
hasValue := false
|
||||
for _, attr := range lastEvent.Attributes {
|
||||
if string(attr.Key) == "amount" && string(attr.Value) == "0" {
|
||||
hasValue = true
|
||||
}
|
||||
}
|
||||
require.True(t, hasValue)
|
||||
}
|
||||
@@ -0,0 +1,680 @@
|
||||
package keeper_test
|
||||
|
||||
import (
|
||||
gocontext "context"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"cosmossdk.io/math"
|
||||
"github.com/stretchr/testify/suite"
|
||||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/baseapp"
|
||||
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
||||
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/types/query"
|
||||
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
|
||||
banktestutil "github.com/cosmos/cosmos-sdk/x/bank/testutil"
|
||||
"github.com/cosmos/cosmos-sdk/x/distribution/keeper"
|
||||
"github.com/cosmos/cosmos-sdk/x/distribution/testutil"
|
||||
"github.com/cosmos/cosmos-sdk/x/distribution/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/staking"
|
||||
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
||||
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
|
||||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
||||
)
|
||||
|
||||
type KeeperTestSuite struct {
|
||||
suite.Suite
|
||||
|
||||
ctx sdk.Context
|
||||
queryClient types.QueryClient
|
||||
addrs []sdk.AccAddress
|
||||
valAddrs []sdk.ValAddress
|
||||
|
||||
interfaceRegistry codectypes.InterfaceRegistry
|
||||
bankKeeper bankkeeper.Keeper
|
||||
distrKeeper keeper.Keeper
|
||||
stakingKeeper *stakingkeeper.Keeper
|
||||
msgServer types.MsgServer
|
||||
}
|
||||
|
||||
func (suite *KeeperTestSuite) SetupTest() {
|
||||
app, err := simtestutil.Setup(testutil.AppConfig,
|
||||
&suite.interfaceRegistry,
|
||||
&suite.bankKeeper,
|
||||
&suite.distrKeeper,
|
||||
&suite.stakingKeeper,
|
||||
)
|
||||
suite.NoError(err)
|
||||
|
||||
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
||||
|
||||
queryHelper := baseapp.NewQueryServerTestHelper(ctx, suite.interfaceRegistry)
|
||||
types.RegisterQueryServer(queryHelper, keeper.NewQuerier(suite.distrKeeper))
|
||||
queryClient := types.NewQueryClient(queryHelper)
|
||||
|
||||
suite.ctx = ctx
|
||||
suite.queryClient = queryClient
|
||||
|
||||
suite.addrs = simtestutil.AddTestAddrs(suite.bankKeeper, suite.stakingKeeper, ctx, 2, sdk.NewInt(1000000000))
|
||||
suite.valAddrs = simtestutil.ConvertAddrsToValAddrs(suite.addrs)
|
||||
|
||||
suite.NoError(suite.distrKeeper.SetParams(suite.ctx, types.DefaultParams()))
|
||||
suite.msgServer = keeper.NewMsgServerImpl(suite.distrKeeper)
|
||||
}
|
||||
|
||||
func (suite *KeeperTestSuite) TestGRPCParams() {
|
||||
ctx, queryClient := suite.ctx, suite.queryClient
|
||||
|
||||
var (
|
||||
params types.Params
|
||||
req *types.QueryParamsRequest
|
||||
expParams types.Params
|
||||
)
|
||||
|
||||
testCases := []struct {
|
||||
msg string
|
||||
malleate func()
|
||||
expPass bool
|
||||
}{
|
||||
{
|
||||
"empty params request",
|
||||
func() {
|
||||
req = &types.QueryParamsRequest{}
|
||||
expParams = types.DefaultParams()
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"valid request",
|
||||
func() {
|
||||
params = types.Params{
|
||||
CommunityTax: sdk.NewDecWithPrec(3, 1),
|
||||
BaseProposerReward: sdk.NewDecWithPrec(2, 1),
|
||||
BonusProposerReward: sdk.NewDecWithPrec(1, 1),
|
||||
WithdrawAddrEnabled: true,
|
||||
}
|
||||
|
||||
suite.NoError(suite.distrKeeper.SetParams(ctx, params))
|
||||
req = &types.QueryParamsRequest{}
|
||||
expParams = params
|
||||
},
|
||||
true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
suite.Run(fmt.Sprintf("Case %s", testCase.msg), func() {
|
||||
testCase.malleate()
|
||||
|
||||
paramsRes, err := queryClient.Params(gocontext.Background(), req)
|
||||
|
||||
if testCase.expPass {
|
||||
suite.Require().NoError(err)
|
||||
suite.Require().NotNil(paramsRes)
|
||||
suite.Require().Equal(paramsRes.Params, expParams)
|
||||
} else {
|
||||
suite.Require().Error(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (suite *KeeperTestSuite) TestGRPCValidatorOutstandingRewards() {
|
||||
ctx, queryClient, valAddrs := suite.ctx, suite.queryClient, suite.valAddrs
|
||||
|
||||
valCommission := sdk.DecCoins{
|
||||
sdk.NewDecCoinFromDec("mytoken", math.LegacyNewDec(5000)),
|
||||
sdk.NewDecCoinFromDec("stake", math.LegacyNewDec(300)),
|
||||
}
|
||||
|
||||
// set outstanding rewards
|
||||
suite.distrKeeper.SetValidatorOutstandingRewards(ctx, valAddrs[0], types.ValidatorOutstandingRewards{Rewards: valCommission})
|
||||
rewards := suite.distrKeeper.GetValidatorOutstandingRewards(ctx, valAddrs[0])
|
||||
|
||||
var req *types.QueryValidatorOutstandingRewardsRequest
|
||||
|
||||
testCases := []struct {
|
||||
msg string
|
||||
malleate func()
|
||||
expPass bool
|
||||
}{
|
||||
{
|
||||
"empty request",
|
||||
func() {
|
||||
req = &types.QueryValidatorOutstandingRewardsRequest{}
|
||||
},
|
||||
false,
|
||||
}, {
|
||||
"valid request",
|
||||
func() {
|
||||
req = &types.QueryValidatorOutstandingRewardsRequest{ValidatorAddress: valAddrs[0].String()}
|
||||
},
|
||||
true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
suite.Run(fmt.Sprintf("Case %s", testCase.msg), func() {
|
||||
testCase.malleate()
|
||||
|
||||
validatorOutstandingRewards, err := queryClient.ValidatorOutstandingRewards(gocontext.Background(), req)
|
||||
|
||||
if testCase.expPass {
|
||||
suite.Require().NoError(err)
|
||||
suite.Require().Equal(rewards, validatorOutstandingRewards.Rewards)
|
||||
suite.Require().Equal(valCommission, validatorOutstandingRewards.Rewards.Rewards)
|
||||
} else {
|
||||
suite.Require().Error(err)
|
||||
suite.Require().Nil(validatorOutstandingRewards)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (suite *KeeperTestSuite) TestGRPCValidatorCommission() {
|
||||
ctx, queryClient, valAddrs := suite.ctx, suite.queryClient, suite.valAddrs
|
||||
|
||||
commission := sdk.DecCoins{{Denom: "token1", Amount: math.LegacyNewDec(4)}, {Denom: "token2", Amount: math.LegacyNewDec(2)}}
|
||||
suite.distrKeeper.SetValidatorAccumulatedCommission(ctx, valAddrs[0], types.ValidatorAccumulatedCommission{Commission: commission})
|
||||
|
||||
var req *types.QueryValidatorCommissionRequest
|
||||
|
||||
testCases := []struct {
|
||||
msg string
|
||||
malleate func()
|
||||
expPass bool
|
||||
}{
|
||||
{
|
||||
"empty request",
|
||||
func() {
|
||||
req = &types.QueryValidatorCommissionRequest{}
|
||||
},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"valid request",
|
||||
func() {
|
||||
req = &types.QueryValidatorCommissionRequest{ValidatorAddress: valAddrs[0].String()}
|
||||
},
|
||||
true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
suite.Run(fmt.Sprintf("Case %s", testCase.msg), func() {
|
||||
testCase.malleate()
|
||||
|
||||
commissionRes, err := queryClient.ValidatorCommission(gocontext.Background(), req)
|
||||
|
||||
if testCase.expPass {
|
||||
suite.Require().NoError(err)
|
||||
suite.Require().NotNil(commissionRes)
|
||||
suite.Require().Equal(commissionRes.Commission.Commission, commission)
|
||||
} else {
|
||||
suite.Require().Error(err)
|
||||
suite.Require().Nil(commissionRes)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (suite *KeeperTestSuite) TestGRPCValidatorSlashes() {
|
||||
ctx, queryClient, valAddrs := suite.ctx, suite.queryClient, suite.valAddrs
|
||||
|
||||
slashes := []types.ValidatorSlashEvent{
|
||||
types.NewValidatorSlashEvent(3, sdk.NewDecWithPrec(5, 1)),
|
||||
types.NewValidatorSlashEvent(5, sdk.NewDecWithPrec(5, 1)),
|
||||
types.NewValidatorSlashEvent(7, sdk.NewDecWithPrec(5, 1)),
|
||||
types.NewValidatorSlashEvent(9, sdk.NewDecWithPrec(5, 1)),
|
||||
}
|
||||
|
||||
for i, slash := range slashes {
|
||||
suite.distrKeeper.SetValidatorSlashEvent(ctx, valAddrs[0], uint64(i+2), 0, slash)
|
||||
}
|
||||
|
||||
var (
|
||||
req *types.QueryValidatorSlashesRequest
|
||||
expRes *types.QueryValidatorSlashesResponse
|
||||
)
|
||||
|
||||
testCases := []struct {
|
||||
msg string
|
||||
malleate func()
|
||||
expPass bool
|
||||
}{
|
||||
{
|
||||
"empty request",
|
||||
func() {
|
||||
req = &types.QueryValidatorSlashesRequest{}
|
||||
expRes = &types.QueryValidatorSlashesResponse{}
|
||||
},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"Ending height lesser than start height request",
|
||||
func() {
|
||||
req = &types.QueryValidatorSlashesRequest{
|
||||
ValidatorAddress: valAddrs[1].String(),
|
||||
StartingHeight: 10,
|
||||
EndingHeight: 1,
|
||||
}
|
||||
expRes = &types.QueryValidatorSlashesResponse{Pagination: &query.PageResponse{}}
|
||||
},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"no slash event validator request",
|
||||
func() {
|
||||
req = &types.QueryValidatorSlashesRequest{
|
||||
ValidatorAddress: valAddrs[1].String(),
|
||||
StartingHeight: 1,
|
||||
EndingHeight: 10,
|
||||
}
|
||||
expRes = &types.QueryValidatorSlashesResponse{Pagination: &query.PageResponse{}}
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"request slashes with offset 2 and limit 2",
|
||||
func() {
|
||||
pageReq := &query.PageRequest{
|
||||
Offset: 2,
|
||||
Limit: 2,
|
||||
}
|
||||
|
||||
req = &types.QueryValidatorSlashesRequest{
|
||||
ValidatorAddress: valAddrs[0].String(),
|
||||
StartingHeight: 1,
|
||||
EndingHeight: 10,
|
||||
Pagination: pageReq,
|
||||
}
|
||||
|
||||
expRes = &types.QueryValidatorSlashesResponse{
|
||||
Slashes: slashes[2:],
|
||||
}
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"request slashes with page limit 3 and count total",
|
||||
func() {
|
||||
pageReq := &query.PageRequest{
|
||||
Limit: 3,
|
||||
CountTotal: true,
|
||||
}
|
||||
|
||||
req = &types.QueryValidatorSlashesRequest{
|
||||
ValidatorAddress: valAddrs[0].String(),
|
||||
StartingHeight: 1,
|
||||
EndingHeight: 10,
|
||||
Pagination: pageReq,
|
||||
}
|
||||
|
||||
expRes = &types.QueryValidatorSlashesResponse{
|
||||
Slashes: slashes[:3],
|
||||
}
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"request slashes with page limit 4 and count total",
|
||||
func() {
|
||||
pageReq := &query.PageRequest{
|
||||
Limit: 4,
|
||||
CountTotal: true,
|
||||
}
|
||||
|
||||
req = &types.QueryValidatorSlashesRequest{
|
||||
ValidatorAddress: valAddrs[0].String(),
|
||||
StartingHeight: 1,
|
||||
EndingHeight: 10,
|
||||
Pagination: pageReq,
|
||||
}
|
||||
|
||||
expRes = &types.QueryValidatorSlashesResponse{
|
||||
Slashes: slashes[:4],
|
||||
}
|
||||
},
|
||||
true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
suite.Run(fmt.Sprintf("Case %s", testCase.msg), func() {
|
||||
testCase.malleate()
|
||||
|
||||
slashesRes, err := queryClient.ValidatorSlashes(gocontext.Background(), req)
|
||||
|
||||
if testCase.expPass {
|
||||
suite.Require().NoError(err)
|
||||
suite.Require().Equal(expRes.GetSlashes(), slashesRes.GetSlashes())
|
||||
} else {
|
||||
suite.Require().Error(err)
|
||||
suite.Require().Nil(slashesRes)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (suite *KeeperTestSuite) TestGRPCDelegationRewards() {
|
||||
ctx, addrs, valAddrs := suite.ctx, suite.addrs, suite.valAddrs
|
||||
|
||||
tstaking := teststaking.NewHelper(suite.T(), ctx, suite.stakingKeeper)
|
||||
tstaking.Commission = stakingtypes.NewCommissionRates(sdk.NewDecWithPrec(5, 1), sdk.NewDecWithPrec(5, 1), math.LegacyNewDec(0))
|
||||
tstaking.CreateValidator(valAddrs[0], valConsPk0, sdk.NewInt(100), true)
|
||||
|
||||
staking.EndBlocker(ctx, suite.stakingKeeper)
|
||||
ctx = ctx.WithBlockHeight(ctx.BlockHeight() + 1)
|
||||
|
||||
queryHelper := baseapp.NewQueryServerTestHelper(ctx, suite.interfaceRegistry)
|
||||
types.RegisterQueryServer(queryHelper, keeper.NewQuerier(suite.distrKeeper))
|
||||
queryClient := types.NewQueryClient(queryHelper)
|
||||
|
||||
val := suite.stakingKeeper.Validator(ctx, valAddrs[0])
|
||||
|
||||
initial := int64(10)
|
||||
tokens := sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(initial)}}
|
||||
suite.distrKeeper.AllocateTokensToValidator(ctx, val, tokens)
|
||||
|
||||
// test command delegation rewards grpc
|
||||
var (
|
||||
req *types.QueryDelegationRewardsRequest
|
||||
expRes *types.QueryDelegationRewardsResponse
|
||||
)
|
||||
|
||||
testCases := []struct {
|
||||
msg string
|
||||
malleate func()
|
||||
expPass bool
|
||||
}{
|
||||
{
|
||||
"empty request",
|
||||
func() {
|
||||
req = &types.QueryDelegationRewardsRequest{}
|
||||
},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"empty delegator request",
|
||||
func() {
|
||||
req = &types.QueryDelegationRewardsRequest{
|
||||
DelegatorAddress: "",
|
||||
ValidatorAddress: valAddrs[0].String(),
|
||||
}
|
||||
},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"empty validator request",
|
||||
func() {
|
||||
req = &types.QueryDelegationRewardsRequest{
|
||||
DelegatorAddress: addrs[1].String(),
|
||||
ValidatorAddress: "",
|
||||
}
|
||||
},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"request with wrong delegator and validator",
|
||||
func() {
|
||||
req = &types.QueryDelegationRewardsRequest{
|
||||
DelegatorAddress: addrs[1].String(),
|
||||
ValidatorAddress: valAddrs[1].String(),
|
||||
}
|
||||
},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"valid request",
|
||||
func() {
|
||||
req = &types.QueryDelegationRewardsRequest{
|
||||
DelegatorAddress: addrs[0].String(),
|
||||
ValidatorAddress: valAddrs[0].String(),
|
||||
}
|
||||
|
||||
expRes = &types.QueryDelegationRewardsResponse{
|
||||
Rewards: sdk.DecCoins{{Denom: sdk.DefaultBondDenom, Amount: math.LegacyNewDec(initial / 2)}},
|
||||
}
|
||||
},
|
||||
true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
suite.Run(fmt.Sprintf("Case %s", testCase.msg), func() {
|
||||
testCase.malleate()
|
||||
|
||||
rewards, err := queryClient.DelegationRewards(gocontext.Background(), req)
|
||||
|
||||
if testCase.expPass {
|
||||
suite.Require().NoError(err)
|
||||
suite.Require().Equal(expRes, rewards)
|
||||
} else {
|
||||
suite.Require().Error(err)
|
||||
suite.Require().Nil(rewards)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// test command delegator total rewards grpc
|
||||
var (
|
||||
totalRewardsReq *types.QueryDelegationTotalRewardsRequest
|
||||
expTotalRewardsRes *types.QueryDelegationTotalRewardsResponse
|
||||
)
|
||||
|
||||
testCases = []struct {
|
||||
msg string
|
||||
malleate func()
|
||||
expPass bool
|
||||
}{
|
||||
{
|
||||
"empty request",
|
||||
func() {
|
||||
totalRewardsReq = &types.QueryDelegationTotalRewardsRequest{}
|
||||
},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"valid total delegation rewards",
|
||||
func() {
|
||||
totalRewardsReq = &types.QueryDelegationTotalRewardsRequest{
|
||||
DelegatorAddress: addrs[0].String(),
|
||||
}
|
||||
|
||||
expectedDelReward := types.NewDelegationDelegatorReward(valAddrs[0],
|
||||
sdk.DecCoins{sdk.NewInt64DecCoin("stake", 5)})
|
||||
|
||||
expTotalRewardsRes = &types.QueryDelegationTotalRewardsResponse{
|
||||
Rewards: []types.DelegationDelegatorReward{expectedDelReward},
|
||||
Total: expectedDelReward.Reward,
|
||||
}
|
||||
},
|
||||
true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
suite.Run(fmt.Sprintf("Case %s", testCase.msg), func() {
|
||||
testCase.malleate()
|
||||
|
||||
totalRewardsRes, err := queryClient.DelegationTotalRewards(gocontext.Background(), totalRewardsReq)
|
||||
|
||||
if testCase.expPass {
|
||||
suite.Require().NoError(err)
|
||||
suite.Require().Equal(totalRewardsRes, expTotalRewardsRes)
|
||||
} else {
|
||||
|
||||
suite.Require().Error(err)
|
||||
suite.Require().Nil(totalRewardsRes)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// test command validator delegators grpc
|
||||
var (
|
||||
delegatorValidatorsReq *types.QueryDelegatorValidatorsRequest
|
||||
expDelegatorValidatorsRes *types.QueryDelegatorValidatorsResponse
|
||||
)
|
||||
|
||||
testCases = []struct {
|
||||
msg string
|
||||
malleate func()
|
||||
expPass bool
|
||||
}{
|
||||
{
|
||||
"empty request",
|
||||
func() {
|
||||
delegatorValidatorsReq = &types.QueryDelegatorValidatorsRequest{}
|
||||
},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"request no delegations address",
|
||||
func() {
|
||||
delegatorValidatorsReq = &types.QueryDelegatorValidatorsRequest{
|
||||
DelegatorAddress: addrs[1].String(),
|
||||
}
|
||||
|
||||
expDelegatorValidatorsRes = &types.QueryDelegatorValidatorsResponse{}
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"valid request",
|
||||
func() {
|
||||
delegatorValidatorsReq = &types.QueryDelegatorValidatorsRequest{
|
||||
DelegatorAddress: addrs[0].String(),
|
||||
}
|
||||
expDelegatorValidatorsRes = &types.QueryDelegatorValidatorsResponse{
|
||||
Validators: []string{valAddrs[0].String()},
|
||||
}
|
||||
},
|
||||
true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
suite.Run(fmt.Sprintf("Case %s", testCase.msg), func() {
|
||||
testCase.malleate()
|
||||
|
||||
validators, err := queryClient.DelegatorValidators(gocontext.Background(), delegatorValidatorsReq)
|
||||
|
||||
if testCase.expPass {
|
||||
suite.Require().NoError(err)
|
||||
suite.Require().Equal(expDelegatorValidatorsRes, validators)
|
||||
} else {
|
||||
suite.Require().Error(err)
|
||||
suite.Require().Nil(validators)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (suite *KeeperTestSuite) TestGRPCDelegatorWithdrawAddress() {
|
||||
ctx, queryClient, addrs := suite.ctx, suite.queryClient, suite.addrs
|
||||
|
||||
err := suite.distrKeeper.SetWithdrawAddr(ctx, addrs[0], addrs[1])
|
||||
suite.Require().Nil(err)
|
||||
|
||||
var req *types.QueryDelegatorWithdrawAddressRequest
|
||||
|
||||
testCases := []struct {
|
||||
msg string
|
||||
malleate func()
|
||||
expPass bool
|
||||
}{
|
||||
{
|
||||
"empty request",
|
||||
func() {
|
||||
req = &types.QueryDelegatorWithdrawAddressRequest{}
|
||||
},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"valid request",
|
||||
func() {
|
||||
req = &types.QueryDelegatorWithdrawAddressRequest{DelegatorAddress: addrs[0].String()}
|
||||
},
|
||||
true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
suite.Run(fmt.Sprintf("Case %s", testCase.msg), func() {
|
||||
testCase.malleate()
|
||||
|
||||
withdrawAddress, err := queryClient.DelegatorWithdrawAddress(gocontext.Background(), req)
|
||||
|
||||
if testCase.expPass {
|
||||
suite.Require().NoError(err)
|
||||
suite.Require().Equal(withdrawAddress.WithdrawAddress, addrs[1].String())
|
||||
} else {
|
||||
suite.Require().Error(err)
|
||||
suite.Require().Nil(withdrawAddress)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (suite *KeeperTestSuite) TestGRPCCommunityPool() {
|
||||
ctx, queryClient, addrs := suite.ctx, suite.queryClient, suite.addrs
|
||||
// reset fee pool
|
||||
suite.distrKeeper.SetFeePool(ctx, types.InitialFeePool())
|
||||
|
||||
var (
|
||||
req *types.QueryCommunityPoolRequest
|
||||
expPool *types.QueryCommunityPoolResponse
|
||||
)
|
||||
|
||||
testCases := []struct {
|
||||
msg string
|
||||
malleate func()
|
||||
expPass bool
|
||||
}{
|
||||
{
|
||||
"valid request empty community pool",
|
||||
func() {
|
||||
req = &types.QueryCommunityPoolRequest{}
|
||||
expPool = &types.QueryCommunityPoolResponse{}
|
||||
},
|
||||
true,
|
||||
},
|
||||
{
|
||||
"valid request",
|
||||
func() {
|
||||
amount := sdk.NewCoins(sdk.NewInt64Coin("stake", 100))
|
||||
suite.Require().NoError(banktestutil.FundAccount(suite.bankKeeper, ctx, addrs[0], amount))
|
||||
|
||||
err := suite.distrKeeper.FundCommunityPool(ctx, amount, addrs[0])
|
||||
suite.Require().Nil(err)
|
||||
req = &types.QueryCommunityPoolRequest{}
|
||||
|
||||
expPool = &types.QueryCommunityPoolResponse{Pool: sdk.NewDecCoinsFromCoins(amount...)}
|
||||
},
|
||||
true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
suite.Run(fmt.Sprintf("Case %s", testCase.msg), func() {
|
||||
testCase.malleate()
|
||||
|
||||
pool, err := queryClient.CommunityPool(gocontext.Background(), req)
|
||||
|
||||
if testCase.expPass {
|
||||
suite.Require().NoError(err)
|
||||
suite.Require().Equal(expPool, pool)
|
||||
} else {
|
||||
suite.Require().Error(err)
|
||||
suite.Require().Nil(pool)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDistributionTestSuite(t *testing.T) {
|
||||
suite.Run(t, new(KeeperTestSuite))
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
package keeper_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"cosmossdk.io/math"
|
||||
"github.com/stretchr/testify/require"
|
||||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
||||
|
||||
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
|
||||
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
|
||||
banktestutil "github.com/cosmos/cosmos-sdk/x/bank/testutil"
|
||||
"github.com/cosmos/cosmos-sdk/x/distribution/keeper"
|
||||
"github.com/cosmos/cosmos-sdk/x/distribution/testutil"
|
||||
"github.com/cosmos/cosmos-sdk/x/distribution/types"
|
||||
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
||||
)
|
||||
|
||||
func TestSetWithdrawAddr(t *testing.T) {
|
||||
var (
|
||||
bankKeeper bankkeeper.Keeper
|
||||
distrKeeper keeper.Keeper
|
||||
stakingKeeper *stakingkeeper.Keeper
|
||||
)
|
||||
|
||||
app, err := simtestutil.Setup(testutil.AppConfig,
|
||||
&bankKeeper,
|
||||
&distrKeeper,
|
||||
&stakingKeeper,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
||||
|
||||
addr := simtestutil.AddTestAddrs(bankKeeper, stakingKeeper, ctx, 2, sdk.NewInt(1000000000))
|
||||
|
||||
params := distrKeeper.GetParams(ctx)
|
||||
params.WithdrawAddrEnabled = false
|
||||
require.NoError(t, distrKeeper.SetParams(ctx, params))
|
||||
|
||||
err = distrKeeper.SetWithdrawAddr(ctx, addr[0], addr[1])
|
||||
require.NotNil(t, err)
|
||||
|
||||
params.WithdrawAddrEnabled = true
|
||||
require.NoError(t, distrKeeper.SetParams(ctx, params))
|
||||
|
||||
err = distrKeeper.SetWithdrawAddr(ctx, addr[0], addr[1])
|
||||
require.Nil(t, err)
|
||||
|
||||
require.Error(t, distrKeeper.SetWithdrawAddr(ctx, addr[0], distrAcc.GetAddress()))
|
||||
}
|
||||
|
||||
func TestWithdrawValidatorCommission(t *testing.T) {
|
||||
var (
|
||||
accountKeeper authkeeper.AccountKeeper
|
||||
bankKeeper bankkeeper.Keeper
|
||||
distrKeeper keeper.Keeper
|
||||
stakingKeeper *stakingkeeper.Keeper
|
||||
)
|
||||
|
||||
app, err := simtestutil.Setup(testutil.AppConfig,
|
||||
&accountKeeper,
|
||||
&bankKeeper,
|
||||
&distrKeeper,
|
||||
&stakingKeeper,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
||||
|
||||
valCommission := sdk.DecCoins{
|
||||
sdk.NewDecCoinFromDec("mytoken", math.LegacyNewDec(5).Quo(math.LegacyNewDec(4))),
|
||||
sdk.NewDecCoinFromDec("stake", math.LegacyNewDec(3).Quo(math.LegacyNewDec(2))),
|
||||
}
|
||||
|
||||
addr := simtestutil.AddTestAddrs(bankKeeper, stakingKeeper, ctx, 1, sdk.NewInt(1000000000))
|
||||
valAddrs := simtestutil.ConvertAddrsToValAddrs(addr)
|
||||
|
||||
// set module account coins
|
||||
distrAcc := distrKeeper.GetDistributionAccount(ctx)
|
||||
coins := sdk.NewCoins(sdk.NewCoin("mytoken", sdk.NewInt(2)), sdk.NewCoin("stake", sdk.NewInt(2)))
|
||||
require.NoError(t, banktestutil.FundModuleAccount(bankKeeper, ctx, distrAcc.GetName(), coins))
|
||||
|
||||
accountKeeper.SetModuleAccount(ctx, distrAcc)
|
||||
|
||||
// check initial balance
|
||||
balance := bankKeeper.GetAllBalances(ctx, sdk.AccAddress(valAddrs[0]))
|
||||
expTokens := stakingKeeper.TokensFromConsensusPower(ctx, 1000)
|
||||
expCoins := sdk.NewCoins(sdk.NewCoin("stake", expTokens))
|
||||
require.Equal(t, expCoins, balance)
|
||||
|
||||
// set outstanding rewards
|
||||
distrKeeper.SetValidatorOutstandingRewards(ctx, valAddrs[0], types.ValidatorOutstandingRewards{Rewards: valCommission})
|
||||
|
||||
// set commission
|
||||
distrKeeper.SetValidatorAccumulatedCommission(ctx, valAddrs[0], types.ValidatorAccumulatedCommission{Commission: valCommission})
|
||||
|
||||
// withdraw commission
|
||||
_, err = distrKeeper.WithdrawValidatorCommission(ctx, valAddrs[0])
|
||||
require.NoError(t, err)
|
||||
|
||||
// check balance increase
|
||||
balance = bankKeeper.GetAllBalances(ctx, sdk.AccAddress(valAddrs[0]))
|
||||
require.Equal(t, sdk.NewCoins(
|
||||
sdk.NewCoin("mytoken", sdk.NewInt(1)),
|
||||
sdk.NewCoin("stake", expTokens.AddRaw(1)),
|
||||
), balance)
|
||||
|
||||
// check remainder
|
||||
remainder := distrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[0]).Commission
|
||||
require.Equal(t, sdk.DecCoins{
|
||||
sdk.NewDecCoinFromDec("mytoken", math.LegacyNewDec(1).Quo(math.LegacyNewDec(4))),
|
||||
sdk.NewDecCoinFromDec("stake", math.LegacyNewDec(1).Quo(math.LegacyNewDec(2))),
|
||||
}, remainder)
|
||||
|
||||
require.True(t, true)
|
||||
}
|
||||
|
||||
func TestGetTotalRewards(t *testing.T) {
|
||||
var (
|
||||
bankKeeper bankkeeper.Keeper
|
||||
distrKeeper keeper.Keeper
|
||||
stakingKeeper *stakingkeeper.Keeper
|
||||
)
|
||||
|
||||
app, err := simtestutil.Setup(testutil.AppConfig,
|
||||
&bankKeeper,
|
||||
&distrKeeper,
|
||||
&stakingKeeper,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
||||
|
||||
valCommission := sdk.DecCoins{
|
||||
sdk.NewDecCoinFromDec("mytoken", math.LegacyNewDec(5).Quo(math.LegacyNewDec(4))),
|
||||
sdk.NewDecCoinFromDec("stake", math.LegacyNewDec(3).Quo(math.LegacyNewDec(2))),
|
||||
}
|
||||
|
||||
addr := simtestutil.AddTestAddrs(bankKeeper, stakingKeeper, ctx, 2, sdk.NewInt(1000000000))
|
||||
valAddrs := simtestutil.ConvertAddrsToValAddrs(addr)
|
||||
|
||||
distrKeeper.SetValidatorOutstandingRewards(ctx, valAddrs[0], types.ValidatorOutstandingRewards{Rewards: valCommission})
|
||||
distrKeeper.SetValidatorOutstandingRewards(ctx, valAddrs[1], types.ValidatorOutstandingRewards{Rewards: valCommission})
|
||||
|
||||
expectedRewards := valCommission.MulDec(math.LegacyNewDec(2))
|
||||
totalRewards := distrKeeper.GetTotalRewards(ctx)
|
||||
|
||||
require.Equal(t, expectedRewards, totalRewards)
|
||||
}
|
||||
|
||||
func TestFundCommunityPool(t *testing.T) {
|
||||
var (
|
||||
bankKeeper bankkeeper.Keeper
|
||||
distrKeeper keeper.Keeper
|
||||
stakingKeeper *stakingkeeper.Keeper
|
||||
)
|
||||
|
||||
app, err := simtestutil.Setup(testutil.AppConfig,
|
||||
&bankKeeper,
|
||||
&distrKeeper,
|
||||
&stakingKeeper,
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
||||
|
||||
// reset fee pool
|
||||
distrKeeper.SetFeePool(ctx, types.InitialFeePool())
|
||||
|
||||
addr := simtestutil.AddTestAddrs(bankKeeper, stakingKeeper, ctx, 2, math.ZeroInt())
|
||||
|
||||
amount := sdk.NewCoins(sdk.NewInt64Coin("stake", 100))
|
||||
require.NoError(t, banktestutil.FundAccount(bankKeeper, ctx, addr[0], amount))
|
||||
|
||||
initPool := distrKeeper.GetFeePool(ctx)
|
||||
require.Empty(t, initPool.CommunityPool)
|
||||
|
||||
err = distrKeeper.FundCommunityPool(ctx, amount, addr[0])
|
||||
require.Nil(t, err)
|
||||
|
||||
require.Equal(t, initPool.CommunityPool.Add(sdk.NewDecCoinsFromCoins(amount...)...), distrKeeper.GetFeePool(ctx).CommunityPool)
|
||||
require.Empty(t, bankKeeper.GetAllBalances(ctx, addr[0]))
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
package keeper_test
|
||||
|
||||
import (
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/distribution/types"
|
||||
)
|
||||
|
||||
func (s *KeeperTestSuite) TestMsgUpdateParams() {
|
||||
// default params
|
||||
communityTax := sdk.NewDecWithPrec(2, 2) // 2%
|
||||
baseProposerReward := sdk.NewDecWithPrec(1, 2) // 1%
|
||||
bonusProposerReward := sdk.NewDecWithPrec(4, 2) // 4%
|
||||
withdrawAddrEnabled := true
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
input *types.MsgUpdateParams
|
||||
expErr bool
|
||||
expErrMsg string
|
||||
}{
|
||||
{
|
||||
name: "invalid authority",
|
||||
input: &types.MsgUpdateParams{
|
||||
Authority: "invalid",
|
||||
Params: types.Params{
|
||||
CommunityTax: sdk.NewDecWithPrec(2, 0),
|
||||
BaseProposerReward: baseProposerReward,
|
||||
BonusProposerReward: bonusProposerReward,
|
||||
WithdrawAddrEnabled: withdrawAddrEnabled,
|
||||
},
|
||||
},
|
||||
expErr: true,
|
||||
expErrMsg: "invalid authority",
|
||||
},
|
||||
{
|
||||
name: "community tax > 1",
|
||||
input: &types.MsgUpdateParams{
|
||||
Authority: s.distrKeeper.GetAuthority(),
|
||||
Params: types.Params{
|
||||
CommunityTax: sdk.NewDecWithPrec(2, 0),
|
||||
BaseProposerReward: baseProposerReward,
|
||||
BonusProposerReward: bonusProposerReward,
|
||||
WithdrawAddrEnabled: withdrawAddrEnabled,
|
||||
},
|
||||
},
|
||||
expErr: true,
|
||||
expErrMsg: "community tax should be non-negative and less than one",
|
||||
},
|
||||
{
|
||||
name: "negative community tax",
|
||||
input: &types.MsgUpdateParams{
|
||||
Authority: s.distrKeeper.GetAuthority(),
|
||||
Params: types.Params{
|
||||
CommunityTax: sdk.NewDecWithPrec(-2, 1),
|
||||
BaseProposerReward: baseProposerReward,
|
||||
BonusProposerReward: bonusProposerReward,
|
||||
WithdrawAddrEnabled: withdrawAddrEnabled,
|
||||
},
|
||||
},
|
||||
expErr: true,
|
||||
expErrMsg: "community tax should be non-negative and less than one",
|
||||
},
|
||||
{
|
||||
name: "base proposer reward > 1",
|
||||
input: &types.MsgUpdateParams{
|
||||
Authority: s.distrKeeper.GetAuthority(),
|
||||
Params: types.Params{
|
||||
CommunityTax: communityTax,
|
||||
BaseProposerReward: sdk.NewDecWithPrec(2, 0),
|
||||
BonusProposerReward: bonusProposerReward,
|
||||
WithdrawAddrEnabled: withdrawAddrEnabled,
|
||||
},
|
||||
},
|
||||
expErr: true,
|
||||
expErrMsg: "sum of base, bonus proposer rewards, and community tax cannot be greater than one",
|
||||
},
|
||||
{
|
||||
name: "negative base proposer reward",
|
||||
input: &types.MsgUpdateParams{
|
||||
Authority: s.distrKeeper.GetAuthority(),
|
||||
Params: types.Params{
|
||||
CommunityTax: communityTax,
|
||||
BaseProposerReward: sdk.NewDecWithPrec(-2, 0),
|
||||
BonusProposerReward: bonusProposerReward,
|
||||
WithdrawAddrEnabled: withdrawAddrEnabled,
|
||||
},
|
||||
},
|
||||
expErr: true,
|
||||
expErrMsg: "base proposer reward should be positive",
|
||||
},
|
||||
{
|
||||
name: "bonus proposer reward > 1",
|
||||
input: &types.MsgUpdateParams{
|
||||
Authority: s.distrKeeper.GetAuthority(),
|
||||
Params: types.Params{
|
||||
CommunityTax: communityTax,
|
||||
BaseProposerReward: baseProposerReward,
|
||||
BonusProposerReward: sdk.NewDecWithPrec(2, 0),
|
||||
WithdrawAddrEnabled: withdrawAddrEnabled,
|
||||
},
|
||||
},
|
||||
expErr: true,
|
||||
expErrMsg: "sum of base, bonus proposer rewards, and community tax cannot be greater than one",
|
||||
},
|
||||
{
|
||||
name: "negative bonus proposer reward",
|
||||
input: &types.MsgUpdateParams{
|
||||
Authority: s.distrKeeper.GetAuthority(),
|
||||
Params: types.Params{
|
||||
CommunityTax: communityTax,
|
||||
BaseProposerReward: baseProposerReward,
|
||||
BonusProposerReward: sdk.NewDecWithPrec(-2, 0),
|
||||
WithdrawAddrEnabled: withdrawAddrEnabled,
|
||||
},
|
||||
},
|
||||
expErr: true,
|
||||
expErrMsg: "bonus proposer reward should be positive",
|
||||
},
|
||||
{
|
||||
name: "all good",
|
||||
input: &types.MsgUpdateParams{
|
||||
Authority: s.distrKeeper.GetAuthority(),
|
||||
Params: types.Params{
|
||||
CommunityTax: communityTax,
|
||||
BaseProposerReward: baseProposerReward,
|
||||
BonusProposerReward: bonusProposerReward,
|
||||
WithdrawAddrEnabled: withdrawAddrEnabled,
|
||||
},
|
||||
},
|
||||
expErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
s.Run(tc.name, func() {
|
||||
_, err := s.msgServer.UpdateParams(s.ctx, tc.input)
|
||||
|
||||
if tc.expErr {
|
||||
s.Require().Error(err)
|
||||
s.Require().Contains(err.Error(), tc.expErrMsg)
|
||||
} else {
|
||||
s.Require().NoError(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (s *KeeperTestSuite) TestCommunityPoolSpend() {
|
||||
recipient := sdk.AccAddress([]byte("addr1_______________"))
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
input *types.MsgCommunityPoolSpend
|
||||
expErr bool
|
||||
expErrMsg string
|
||||
}{
|
||||
{
|
||||
name: "invalid authority",
|
||||
input: &types.MsgCommunityPoolSpend{
|
||||
Authority: "invalid",
|
||||
Recipient: recipient.String(),
|
||||
Amount: sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(100))),
|
||||
},
|
||||
expErr: true,
|
||||
expErrMsg: "invalid authority",
|
||||
},
|
||||
{
|
||||
name: "invalid recipient",
|
||||
input: &types.MsgCommunityPoolSpend{
|
||||
Authority: s.distrKeeper.GetAuthority(),
|
||||
Recipient: "invalid",
|
||||
Amount: sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(100))),
|
||||
},
|
||||
expErr: true,
|
||||
expErrMsg: "decoding bech32 failed",
|
||||
},
|
||||
{
|
||||
name: "valid message",
|
||||
input: &types.MsgCommunityPoolSpend{
|
||||
Authority: s.distrKeeper.GetAuthority(),
|
||||
Recipient: recipient.String(),
|
||||
Amount: sdk.NewCoins(sdk.NewCoin("stake", sdk.NewInt(100))),
|
||||
},
|
||||
expErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
s.Run(tc.name, func() {
|
||||
_, err := s.msgServer.CommunityPoolSpend(s.ctx, tc.input)
|
||||
|
||||
if tc.expErr {
|
||||
s.Require().Error(err)
|
||||
s.Require().Contains(err.Error(), tc.expErrMsg)
|
||||
} else {
|
||||
s.Require().NoError(err)
|
||||
|
||||
r, err := sdk.AccAddressFromBech32(tc.input.Recipient)
|
||||
s.Require().NoError(err)
|
||||
|
||||
b := s.bankKeeper.GetAllBalances(s.ctx, r)
|
||||
s.Require().False(b.IsZero())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,117 @@
|
||||
package keeper_test
|
||||
|
||||
import (
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/distribution/types"
|
||||
)
|
||||
|
||||
func (s *KeeperTestSuite) TestParams() {
|
||||
// default params
|
||||
communityTax := sdk.NewDecWithPrec(2, 2) // 2%
|
||||
baseProposerReward := sdk.NewDecWithPrec(1, 2) // 1%
|
||||
bonusProposerReward := sdk.NewDecWithPrec(4, 2) // 4%
|
||||
withdrawAddrEnabled := true
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
input types.Params
|
||||
expErr bool
|
||||
expErrMsg string
|
||||
}{
|
||||
{
|
||||
name: "community tax > 1",
|
||||
input: types.Params{
|
||||
CommunityTax: sdk.NewDecWithPrec(2, 0),
|
||||
BaseProposerReward: baseProposerReward,
|
||||
BonusProposerReward: bonusProposerReward,
|
||||
WithdrawAddrEnabled: withdrawAddrEnabled,
|
||||
},
|
||||
expErr: true,
|
||||
expErrMsg: "community tax should be non-negative and less than one",
|
||||
},
|
||||
{
|
||||
name: "negative community tax",
|
||||
input: types.Params{
|
||||
CommunityTax: sdk.NewDecWithPrec(-2, 1),
|
||||
BaseProposerReward: baseProposerReward,
|
||||
BonusProposerReward: bonusProposerReward,
|
||||
WithdrawAddrEnabled: withdrawAddrEnabled,
|
||||
},
|
||||
expErr: true,
|
||||
expErrMsg: "community tax should be non-negative and less than one",
|
||||
},
|
||||
{
|
||||
name: "base proposer reward > 1",
|
||||
input: types.Params{
|
||||
CommunityTax: communityTax,
|
||||
BaseProposerReward: sdk.NewDecWithPrec(2, 0),
|
||||
BonusProposerReward: bonusProposerReward,
|
||||
WithdrawAddrEnabled: withdrawAddrEnabled,
|
||||
},
|
||||
expErr: true,
|
||||
expErrMsg: "sum of base, bonus proposer rewards, and community tax cannot be greater than one",
|
||||
},
|
||||
{
|
||||
name: "negative base proposer reward",
|
||||
input: types.Params{
|
||||
CommunityTax: communityTax,
|
||||
BaseProposerReward: sdk.NewDecWithPrec(-2, 0),
|
||||
BonusProposerReward: bonusProposerReward,
|
||||
WithdrawAddrEnabled: withdrawAddrEnabled,
|
||||
},
|
||||
expErr: true,
|
||||
expErrMsg: "base proposer reward should be positive",
|
||||
},
|
||||
{
|
||||
name: "bonus proposer reward > 1",
|
||||
input: types.Params{
|
||||
CommunityTax: communityTax,
|
||||
BaseProposerReward: baseProposerReward,
|
||||
BonusProposerReward: sdk.NewDecWithPrec(2, 0),
|
||||
WithdrawAddrEnabled: withdrawAddrEnabled,
|
||||
},
|
||||
expErr: true,
|
||||
expErrMsg: "sum of base, bonus proposer rewards, and community tax cannot be greater than one",
|
||||
},
|
||||
{
|
||||
name: "negative bonus proposer reward",
|
||||
input: types.Params{
|
||||
CommunityTax: communityTax,
|
||||
BaseProposerReward: baseProposerReward,
|
||||
BonusProposerReward: sdk.NewDecWithPrec(-2, 0),
|
||||
WithdrawAddrEnabled: withdrawAddrEnabled,
|
||||
},
|
||||
expErr: true,
|
||||
expErrMsg: "bonus proposer reward should be positive",
|
||||
},
|
||||
{
|
||||
name: "all good",
|
||||
input: types.Params{
|
||||
CommunityTax: communityTax,
|
||||
BaseProposerReward: baseProposerReward,
|
||||
BonusProposerReward: bonusProposerReward,
|
||||
WithdrawAddrEnabled: withdrawAddrEnabled,
|
||||
},
|
||||
expErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
s.Run(tc.name, func() {
|
||||
expected := s.distrKeeper.GetParams(s.ctx)
|
||||
err := s.distrKeeper.SetParams(s.ctx, tc.input)
|
||||
|
||||
if tc.expErr {
|
||||
s.Require().Error(err)
|
||||
s.Require().Contains(err.Error(), tc.expErrMsg)
|
||||
} else {
|
||||
expected = tc.input
|
||||
s.Require().NoError(err)
|
||||
}
|
||||
|
||||
params := s.distrKeeper.GetParams(s.ctx)
|
||||
s.Require().Equal(expected, params)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package distribution_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
||||
|
||||
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
|
||||
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
|
||||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/distribution/testutil"
|
||||
"github.com/cosmos/cosmos-sdk/x/distribution/types"
|
||||
)
|
||||
|
||||
func TestItCreatesModuleAccountOnInitBlock(t *testing.T) {
|
||||
var accountKeeper authkeeper.AccountKeeper
|
||||
|
||||
app, err := simtestutil.SetupAtGenesis(testutil.AppConfig, &accountKeeper)
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
||||
acc := accountKeeper.GetAccount(ctx, authtypes.NewModuleAddress(types.ModuleName))
|
||||
require.NotNil(t, acc)
|
||||
}
|
||||
Reference in New Issue
Block a user