2020-03-05 06:00:45 -08:00
|
|
|
package keeper_test
|
2018-10-10 19:53:25 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-12-10 11:09:22 -08:00
|
|
|
"github.com/stretchr/testify/assert"
|
2018-12-10 06:27:25 -08:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-08-14 10:58:53 -07:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2020-03-05 06:00:45 -08:00
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
2018-10-10 19:53:25 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-02-11 06:58:37 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/distribution/types"
|
2018-10-10 19:53:25 -07:00
|
|
|
)
|
|
|
|
|
2019-01-16 14:17:56 -08:00
|
|
|
func TestSetWithdrawAddr(t *testing.T) {
|
2020-03-05 06:00:45 -08:00
|
|
|
app := simapp.Setup(false)
|
2020-08-14 10:58:53 -07:00
|
|
|
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
2020-03-05 06:00:45 -08:00
|
|
|
|
|
|
|
addr := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(1000000000))
|
2019-01-16 14:17:56 -08:00
|
|
|
|
2020-03-05 06:00:45 -08:00
|
|
|
params := app.DistrKeeper.GetParams(ctx)
|
2020-01-10 12:08:14 -08:00
|
|
|
params.WithdrawAddrEnabled = false
|
2020-03-05 06:00:45 -08:00
|
|
|
app.DistrKeeper.SetParams(ctx, params)
|
2019-01-16 14:17:56 -08:00
|
|
|
|
2020-03-05 06:00:45 -08:00
|
|
|
err := app.DistrKeeper.SetWithdrawAddr(ctx, addr[0], addr[1])
|
2019-01-16 14:17:56 -08:00
|
|
|
require.NotNil(t, err)
|
|
|
|
|
2020-01-10 12:08:14 -08:00
|
|
|
params.WithdrawAddrEnabled = true
|
2020-03-05 06:00:45 -08:00
|
|
|
app.DistrKeeper.SetParams(ctx, params)
|
2019-01-16 14:17:56 -08:00
|
|
|
|
2020-03-05 06:00:45 -08:00
|
|
|
err = app.DistrKeeper.SetWithdrawAddr(ctx, addr[0], addr[1])
|
2019-01-16 14:17:56 -08:00
|
|
|
require.Nil(t, err)
|
2019-07-31 08:39:02 -07:00
|
|
|
|
2020-03-05 06:00:45 -08:00
|
|
|
require.Error(t, app.DistrKeeper.SetWithdrawAddr(ctx, addr[0], distrAcc.GetAddress()))
|
2019-01-16 14:17:56 -08:00
|
|
|
}
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
func TestWithdrawValidatorCommission(t *testing.T) {
|
2020-03-05 06:00:45 -08:00
|
|
|
app := simapp.Setup(false)
|
2020-08-14 10:58:53 -07:00
|
|
|
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
2019-01-16 13:38:05 -08:00
|
|
|
|
2019-02-15 07:33:23 -08:00
|
|
|
valCommission := sdk.DecCoins{
|
|
|
|
sdk.NewDecCoinFromDec("mytoken", sdk.NewDec(5).Quo(sdk.NewDec(4))),
|
|
|
|
sdk.NewDecCoinFromDec("stake", sdk.NewDec(3).Quo(sdk.NewDec(2))),
|
|
|
|
}
|
|
|
|
|
2020-03-05 06:00:45 -08:00
|
|
|
addr := simapp.AddTestAddrs(app, ctx, 1, sdk.NewInt(1000000000))
|
|
|
|
valAddrs := simapp.ConvertAddrsToValAddrs(addr)
|
|
|
|
|
2019-06-28 13:11:27 -07:00
|
|
|
// set module account coins
|
2020-03-05 06:00:45 -08:00
|
|
|
distrAcc := app.DistrKeeper.GetDistributionAccount(ctx)
|
2020-03-20 12:24:18 -07:00
|
|
|
err := app.BankKeeper.SetBalances(ctx, distrAcc.GetAddress(), sdk.NewCoins(
|
2019-06-28 13:11:27 -07:00
|
|
|
sdk.NewCoin("mytoken", sdk.NewInt(2)),
|
|
|
|
sdk.NewCoin("stake", sdk.NewInt(2)),
|
|
|
|
))
|
2020-03-20 12:24:18 -07:00
|
|
|
require.NoError(t, err)
|
2020-04-20 12:32:10 -07:00
|
|
|
app.AccountKeeper.SetModuleAccount(ctx, distrAcc)
|
2019-06-28 13:11:27 -07:00
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// check initial balance
|
2020-03-05 06:00:45 -08:00
|
|
|
balance := app.BankKeeper.GetAllBalances(ctx, sdk.AccAddress(valAddrs[0]))
|
2021-01-27 02:24:00 -08:00
|
|
|
expTokens := sdk.TokensFromConsensusPower(1000, sdk.DefaultPowerReduction)
|
2019-06-28 13:11:27 -07:00
|
|
|
expCoins := sdk.NewCoins(sdk.NewCoin("stake", expTokens))
|
|
|
|
require.Equal(t, expCoins, balance)
|
2019-01-16 13:38:05 -08:00
|
|
|
|
2019-03-06 10:54:12 -08:00
|
|
|
// set outstanding rewards
|
2020-03-05 06:00:45 -08:00
|
|
|
app.DistrKeeper.SetValidatorOutstandingRewards(ctx, valAddrs[0], types.ValidatorOutstandingRewards{Rewards: valCommission})
|
2019-03-06 10:54:12 -08:00
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// set commission
|
2020-03-05 06:00:45 -08:00
|
|
|
app.DistrKeeper.SetValidatorAccumulatedCommission(ctx, valAddrs[0], types.ValidatorAccumulatedCommission{Commission: valCommission})
|
2019-01-16 13:38:05 -08:00
|
|
|
|
|
|
|
// withdraw commission
|
2020-03-20 12:24:18 -07:00
|
|
|
_, err = app.DistrKeeper.WithdrawValidatorCommission(ctx, valAddrs[0])
|
|
|
|
require.NoError(t, err)
|
2019-01-16 13:38:05 -08:00
|
|
|
|
|
|
|
// check balance increase
|
2020-03-05 06:00:45 -08:00
|
|
|
balance = app.BankKeeper.GetAllBalances(ctx, sdk.AccAddress(valAddrs[0]))
|
2019-06-28 13:11:27 -07:00
|
|
|
require.Equal(t, sdk.NewCoins(
|
2019-02-15 07:33:23 -08:00
|
|
|
sdk.NewCoin("mytoken", sdk.NewInt(1)),
|
|
|
|
sdk.NewCoin("stake", expTokens.AddRaw(1)),
|
2019-06-28 13:11:27 -07:00
|
|
|
), balance)
|
2019-01-16 13:38:05 -08:00
|
|
|
|
|
|
|
// check remainder
|
2020-03-05 06:00:45 -08:00
|
|
|
remainder := app.DistrKeeper.GetValidatorAccumulatedCommission(ctx, valAddrs[0]).Commission
|
2019-02-05 21:30:48 -08:00
|
|
|
require.Equal(t, sdk.DecCoins{
|
2019-02-15 07:33:23 -08:00
|
|
|
sdk.NewDecCoinFromDec("mytoken", sdk.NewDec(1).Quo(sdk.NewDec(4))),
|
|
|
|
sdk.NewDecCoinFromDec("stake", sdk.NewDec(1).Quo(sdk.NewDec(2))),
|
2019-02-05 21:30:48 -08:00
|
|
|
}, remainder)
|
2019-01-16 13:38:05 -08:00
|
|
|
|
|
|
|
require.True(t, true)
|
2018-10-10 19:53:25 -07:00
|
|
|
}
|
2019-06-28 13:11:27 -07:00
|
|
|
|
|
|
|
func TestGetTotalRewards(t *testing.T) {
|
2020-03-05 06:00:45 -08:00
|
|
|
app := simapp.Setup(false)
|
2020-08-14 10:58:53 -07:00
|
|
|
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
2019-06-28 13:11:27 -07:00
|
|
|
|
|
|
|
valCommission := sdk.DecCoins{
|
|
|
|
sdk.NewDecCoinFromDec("mytoken", sdk.NewDec(5).Quo(sdk.NewDec(4))),
|
|
|
|
sdk.NewDecCoinFromDec("stake", sdk.NewDec(3).Quo(sdk.NewDec(2))),
|
|
|
|
}
|
|
|
|
|
2020-03-05 06:00:45 -08:00
|
|
|
addr := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(1000000000))
|
|
|
|
valAddrs := simapp.ConvertAddrsToValAddrs(addr)
|
|
|
|
|
|
|
|
app.DistrKeeper.SetValidatorOutstandingRewards(ctx, valAddrs[0], types.ValidatorOutstandingRewards{Rewards: valCommission})
|
|
|
|
app.DistrKeeper.SetValidatorOutstandingRewards(ctx, valAddrs[1], types.ValidatorOutstandingRewards{Rewards: valCommission})
|
2019-06-28 13:11:27 -07:00
|
|
|
|
|
|
|
expectedRewards := valCommission.MulDec(sdk.NewDec(2))
|
2020-03-05 06:00:45 -08:00
|
|
|
totalRewards := app.DistrKeeper.GetTotalRewards(ctx)
|
2019-06-28 13:11:27 -07:00
|
|
|
|
|
|
|
require.Equal(t, expectedRewards, totalRewards)
|
|
|
|
}
|
2019-12-10 11:09:22 -08:00
|
|
|
|
2019-12-10 20:02:15 -08:00
|
|
|
func TestFundCommunityPool(t *testing.T) {
|
2020-03-05 06:00:45 -08:00
|
|
|
app := simapp.Setup(false)
|
2020-08-14 10:58:53 -07:00
|
|
|
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
2020-03-05 06:00:45 -08:00
|
|
|
|
|
|
|
addr := simapp.AddTestAddrs(app, ctx, 2, sdk.NewInt(1000000000))
|
2019-12-10 11:09:22 -08:00
|
|
|
|
|
|
|
amount := sdk.NewCoins(sdk.NewInt64Coin("stake", 100))
|
2020-03-05 06:00:45 -08:00
|
|
|
require.NoError(t, app.BankKeeper.SetBalances(ctx, addr[0], amount))
|
2019-12-10 11:09:22 -08:00
|
|
|
|
2020-03-05 06:00:45 -08:00
|
|
|
initPool := app.DistrKeeper.GetFeePool(ctx)
|
2019-12-10 11:09:22 -08:00
|
|
|
assert.Empty(t, initPool.CommunityPool)
|
|
|
|
|
2020-03-05 06:00:45 -08:00
|
|
|
err := app.DistrKeeper.FundCommunityPool(ctx, amount, addr[0])
|
2019-12-10 11:09:22 -08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2020-03-05 06:00:45 -08:00
|
|
|
assert.Equal(t, initPool.CommunityPool.Add(sdk.NewDecCoinsFromCoins(amount...)...), app.DistrKeeper.GetFeePool(ctx).CommunityPool)
|
|
|
|
assert.Empty(t, app.BankKeeper.GetAllBalances(ctx, addr[0]))
|
2019-12-10 11:09:22 -08:00
|
|
|
}
|