2018-10-10 19:53:25 -07:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
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"
|
|
|
|
|
2018-10-10 19:53:25 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
2019-01-16 14:17:56 -08:00
|
|
|
func TestSetWithdrawAddr(t *testing.T) {
|
|
|
|
ctx, _, keeper, _, _ := CreateTestInputDefault(t, false, 1000)
|
|
|
|
|
|
|
|
keeper.SetWithdrawAddrEnabled(ctx, false)
|
|
|
|
|
|
|
|
err := keeper.SetWithdrawAddr(ctx, delAddr1, delAddr2)
|
|
|
|
require.NotNil(t, err)
|
|
|
|
|
|
|
|
keeper.SetWithdrawAddrEnabled(ctx, true)
|
|
|
|
|
|
|
|
err = keeper.SetWithdrawAddr(ctx, delAddr1, delAddr2)
|
|
|
|
require.Nil(t, err)
|
2019-07-31 08:39:02 -07:00
|
|
|
|
|
|
|
keeper.blacklistedAddrs[distrAcc.GetAddress().String()] = true
|
|
|
|
require.Error(t, keeper.SetWithdrawAddr(ctx, delAddr1, distrAcc.GetAddress()))
|
2019-01-16 14:17:56 -08:00
|
|
|
}
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
func TestWithdrawValidatorCommission(t *testing.T) {
|
|
|
|
ctx, ak, keeper, _, _ := CreateTestInputDefault(t, false, 1000)
|
|
|
|
|
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))),
|
|
|
|
}
|
|
|
|
|
2019-06-28 13:11:27 -07:00
|
|
|
// set module account coins
|
|
|
|
distrAcc := keeper.GetDistributionAccount(ctx)
|
|
|
|
distrAcc.SetCoins(sdk.NewCoins(
|
|
|
|
sdk.NewCoin("mytoken", sdk.NewInt(2)),
|
|
|
|
sdk.NewCoin("stake", sdk.NewInt(2)),
|
|
|
|
))
|
|
|
|
keeper.supplyKeeper.SetModuleAccount(ctx, distrAcc)
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// check initial balance
|
|
|
|
balance := ak.GetAccount(ctx, sdk.AccAddress(valOpAddr3)).GetCoins()
|
2019-06-12 08:57:47 -07:00
|
|
|
expTokens := sdk.TokensFromConsensusPower(1000)
|
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
|
|
|
|
keeper.SetValidatorOutstandingRewards(ctx, valOpAddr3, valCommission)
|
|
|
|
|
2019-01-16 13:38:05 -08:00
|
|
|
// set commission
|
2019-02-15 07:33:23 -08:00
|
|
|
keeper.SetValidatorAccumulatedCommission(ctx, valOpAddr3, valCommission)
|
2019-01-16 13:38:05 -08:00
|
|
|
|
|
|
|
// withdraw commission
|
|
|
|
keeper.WithdrawValidatorCommission(ctx, valOpAddr3)
|
|
|
|
|
|
|
|
// check balance increase
|
|
|
|
balance = ak.GetAccount(ctx, sdk.AccAddress(valOpAddr3)).GetCoins()
|
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
|
|
|
|
remainder := keeper.GetValidatorAccumulatedCommission(ctx, valOpAddr3)
|
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) {
|
|
|
|
ctx, _, keeper, _, _ := CreateTestInputDefault(t, false, 1000)
|
|
|
|
|
|
|
|
valCommission := sdk.DecCoins{
|
|
|
|
sdk.NewDecCoinFromDec("mytoken", sdk.NewDec(5).Quo(sdk.NewDec(4))),
|
|
|
|
sdk.NewDecCoinFromDec("stake", sdk.NewDec(3).Quo(sdk.NewDec(2))),
|
|
|
|
}
|
|
|
|
|
|
|
|
keeper.SetValidatorOutstandingRewards(ctx, valOpAddr1, valCommission)
|
|
|
|
keeper.SetValidatorOutstandingRewards(ctx, valOpAddr2, valCommission)
|
|
|
|
|
|
|
|
expectedRewards := valCommission.MulDec(sdk.NewDec(2))
|
|
|
|
totalRewards := keeper.GetTotalRewards(ctx)
|
|
|
|
|
|
|
|
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) {
|
2019-12-10 11:09:22 -08:00
|
|
|
// nolint dogsled
|
|
|
|
ctx, _, bk, keeper, _, _, _ := CreateTestInputAdvanced(t, false, 1000, sdk.NewDecWithPrec(2, 2))
|
|
|
|
|
|
|
|
amount := sdk.NewCoins(sdk.NewInt64Coin("stake", 100))
|
|
|
|
_ = bk.SetCoins(ctx, delAddr1, amount)
|
|
|
|
|
|
|
|
initPool := keeper.GetFeePool(ctx)
|
|
|
|
assert.Empty(t, initPool.CommunityPool)
|
|
|
|
|
2019-12-10 20:02:15 -08:00
|
|
|
err := keeper.FundCommunityPool(ctx, amount, delAddr1)
|
2019-12-10 11:09:22 -08:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2020-01-03 12:44:53 -08:00
|
|
|
assert.Equal(t, initPool.CommunityPool.Add(sdk.NewDecCoinsFromCoins(amount...)...), keeper.GetFeePool(ctx).CommunityPool)
|
2019-12-10 11:09:22 -08:00
|
|
|
assert.Empty(t, bk.GetCoins(ctx, delAddr1))
|
|
|
|
}
|