202 lines
7.1 KiB
Go
202 lines
7.1 KiB
Go
package keeper_test
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"cosmossdk.io/math"
|
|
"github.com/golang/mock/gomock"
|
|
"github.com/stretchr/testify/require"
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/testutil"
|
|
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
"github.com/cosmos/cosmos-sdk/x/distribution"
|
|
"github.com/cosmos/cosmos-sdk/x/distribution/keeper"
|
|
distrtestutil "github.com/cosmos/cosmos-sdk/x/distribution/testutil"
|
|
"github.com/cosmos/cosmos-sdk/x/distribution/types"
|
|
)
|
|
|
|
func TestSetWithdrawAddr(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
key := sdk.NewKVStoreKey(types.StoreKey)
|
|
testCtx := testutil.DefaultContextWithDB(t, key, sdk.NewTransientStoreKey("transient_test"))
|
|
encCfg := moduletestutil.MakeTestEncodingConfig(distribution.AppModuleBasic{})
|
|
ctx := testCtx.Ctx.WithBlockHeader(tmproto.Header{Time: time.Now()})
|
|
addrs := simtestutil.CreateIncrementalAccounts(2)
|
|
|
|
delegatorAddr := addrs[0]
|
|
withdrawAddr := addrs[1]
|
|
|
|
bankKeeper := distrtestutil.NewMockBankKeeper(ctrl)
|
|
stakingKeeper := distrtestutil.NewMockStakingKeeper(ctrl)
|
|
accountKeeper := distrtestutil.NewMockAccountKeeper(ctrl)
|
|
|
|
accountKeeper.EXPECT().GetModuleAddress("distribution").Return(distrAcc.GetAddress())
|
|
|
|
bankKeeper.EXPECT().BlockedAddr(withdrawAddr).Return(false).AnyTimes()
|
|
bankKeeper.EXPECT().BlockedAddr(distrAcc.GetAddress()).Return(true).AnyTimes()
|
|
|
|
distrKeeper := keeper.NewKeeper(
|
|
encCfg.Codec,
|
|
key,
|
|
accountKeeper,
|
|
bankKeeper,
|
|
stakingKeeper,
|
|
"fee_collector",
|
|
authtypes.NewModuleAddress("gov").String(),
|
|
)
|
|
|
|
params := types.DefaultParams()
|
|
params.WithdrawAddrEnabled = false
|
|
require.NoError(t, distrKeeper.SetParams(ctx, params))
|
|
|
|
err := distrKeeper.SetWithdrawAddr(ctx, delegatorAddr, withdrawAddr)
|
|
require.NotNil(t, err)
|
|
|
|
params.WithdrawAddrEnabled = true
|
|
require.NoError(t, distrKeeper.SetParams(ctx, params))
|
|
|
|
err = distrKeeper.SetWithdrawAddr(ctx, delegatorAddr, withdrawAddr)
|
|
require.Nil(t, err)
|
|
|
|
require.Error(t, distrKeeper.SetWithdrawAddr(ctx, delegatorAddr, distrAcc.GetAddress()))
|
|
}
|
|
|
|
func TestWithdrawValidatorCommission(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
key := sdk.NewKVStoreKey(types.StoreKey)
|
|
testCtx := testutil.DefaultContextWithDB(t, key, sdk.NewTransientStoreKey("transient_test"))
|
|
encCfg := moduletestutil.MakeTestEncodingConfig(distribution.AppModuleBasic{})
|
|
ctx := testCtx.Ctx.WithBlockHeader(tmproto.Header{Time: time.Now()})
|
|
addrs := simtestutil.CreateIncrementalAccounts(1)
|
|
|
|
valAddr := sdk.ValAddress(addrs[0])
|
|
|
|
bankKeeper := distrtestutil.NewMockBankKeeper(ctrl)
|
|
stakingKeeper := distrtestutil.NewMockStakingKeeper(ctrl)
|
|
accountKeeper := distrtestutil.NewMockAccountKeeper(ctrl)
|
|
|
|
accountKeeper.EXPECT().GetModuleAddress("distribution").Return(distrAcc.GetAddress())
|
|
|
|
valCommission := sdk.DecCoins{
|
|
sdk.NewDecCoinFromDec("mytoken", math.LegacyNewDec(5).Quo(math.LegacyNewDec(4))),
|
|
sdk.NewDecCoinFromDec("stake", math.LegacyNewDec(3).Quo(math.LegacyNewDec(2))),
|
|
}
|
|
|
|
distrKeeper := keeper.NewKeeper(
|
|
encCfg.Codec,
|
|
key,
|
|
accountKeeper,
|
|
bankKeeper,
|
|
stakingKeeper,
|
|
"fee_collector",
|
|
authtypes.NewModuleAddress("gov").String(),
|
|
)
|
|
|
|
// set outstanding rewards
|
|
distrKeeper.SetValidatorOutstandingRewards(ctx, valAddr, types.ValidatorOutstandingRewards{Rewards: valCommission})
|
|
|
|
// set commission
|
|
distrKeeper.SetValidatorAccumulatedCommission(ctx, valAddr, types.ValidatorAccumulatedCommission{Commission: valCommission})
|
|
|
|
// withdraw commission
|
|
coins := sdk.NewCoins(sdk.NewCoin("mytoken", sdk.NewInt(1)), sdk.NewCoin("stake", sdk.NewInt(1)))
|
|
// if SendCoinsFromModuleToAccount is called, we know that the withdraw was successful
|
|
bankKeeper.EXPECT().SendCoinsFromModuleToAccount(gomock.Any(), "distribution", addrs[0], coins).Return(nil)
|
|
|
|
_, err := distrKeeper.WithdrawValidatorCommission(ctx, valAddr)
|
|
require.NoError(t, err)
|
|
|
|
// check remainder
|
|
remainder := distrKeeper.GetValidatorAccumulatedCommission(ctx, valAddr).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) {
|
|
ctrl := gomock.NewController(t)
|
|
key := sdk.NewKVStoreKey(types.StoreKey)
|
|
testCtx := testutil.DefaultContextWithDB(t, key, sdk.NewTransientStoreKey("transient_test"))
|
|
encCfg := moduletestutil.MakeTestEncodingConfig(distribution.AppModuleBasic{})
|
|
ctx := testCtx.Ctx.WithBlockHeader(tmproto.Header{Time: time.Now()})
|
|
addrs := simtestutil.CreateIncrementalAccounts(2)
|
|
|
|
valAddr0 := sdk.ValAddress(addrs[0])
|
|
valAddr1 := sdk.ValAddress(addrs[1])
|
|
|
|
bankKeeper := distrtestutil.NewMockBankKeeper(ctrl)
|
|
stakingKeeper := distrtestutil.NewMockStakingKeeper(ctrl)
|
|
accountKeeper := distrtestutil.NewMockAccountKeeper(ctrl)
|
|
|
|
accountKeeper.EXPECT().GetModuleAddress("distribution").Return(distrAcc.GetAddress())
|
|
|
|
distrKeeper := keeper.NewKeeper(
|
|
encCfg.Codec,
|
|
key,
|
|
accountKeeper,
|
|
bankKeeper,
|
|
stakingKeeper,
|
|
"fee_collector",
|
|
authtypes.NewModuleAddress("gov").String(),
|
|
)
|
|
|
|
valCommission := sdk.DecCoins{
|
|
sdk.NewDecCoinFromDec("mytoken", math.LegacyNewDec(5).Quo(math.LegacyNewDec(4))),
|
|
sdk.NewDecCoinFromDec("stake", math.LegacyNewDec(3).Quo(math.LegacyNewDec(2))),
|
|
}
|
|
|
|
distrKeeper.SetValidatorOutstandingRewards(ctx, valAddr0, types.ValidatorOutstandingRewards{Rewards: valCommission})
|
|
distrKeeper.SetValidatorOutstandingRewards(ctx, valAddr1, types.ValidatorOutstandingRewards{Rewards: valCommission})
|
|
|
|
expectedRewards := valCommission.MulDec(math.LegacyNewDec(2))
|
|
totalRewards := distrKeeper.GetTotalRewards(ctx)
|
|
|
|
require.Equal(t, expectedRewards, totalRewards)
|
|
}
|
|
|
|
func TestFundCommunityPool(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
key := sdk.NewKVStoreKey(types.StoreKey)
|
|
testCtx := testutil.DefaultContextWithDB(t, key, sdk.NewTransientStoreKey("transient_test"))
|
|
encCfg := moduletestutil.MakeTestEncodingConfig(distribution.AppModuleBasic{})
|
|
ctx := testCtx.Ctx.WithBlockHeader(tmproto.Header{Time: time.Now()})
|
|
addrs := simtestutil.CreateIncrementalAccounts(1)
|
|
|
|
bankKeeper := distrtestutil.NewMockBankKeeper(ctrl)
|
|
stakingKeeper := distrtestutil.NewMockStakingKeeper(ctrl)
|
|
accountKeeper := distrtestutil.NewMockAccountKeeper(ctrl)
|
|
|
|
accountKeeper.EXPECT().GetModuleAddress("distribution").Return(distrAcc.GetAddress())
|
|
|
|
distrKeeper := keeper.NewKeeper(
|
|
encCfg.Codec,
|
|
key,
|
|
accountKeeper,
|
|
bankKeeper,
|
|
stakingKeeper,
|
|
"fee_collector",
|
|
authtypes.NewModuleAddress("gov").String(),
|
|
)
|
|
|
|
// reset fee pool
|
|
distrKeeper.SetFeePool(ctx, types.InitialFeePool())
|
|
|
|
initPool := distrKeeper.GetFeePool(ctx)
|
|
require.Empty(t, initPool.CommunityPool)
|
|
|
|
amount := sdk.NewCoins(sdk.NewInt64Coin("stake", 100))
|
|
bankKeeper.EXPECT().SendCoinsFromAccountToModule(gomock.Any(), addrs[0], "distribution", amount).Return(nil)
|
|
err := distrKeeper.FundCommunityPool(ctx, amount, addrs[0])
|
|
require.Nil(t, err)
|
|
|
|
require.Equal(t, initPool.CommunityPool.Add(sdk.NewDecCoinsFromCoins(amount...)...), distrKeeper.GetFeePool(ctx).CommunityPool)
|
|
}
|