2018-10-04 18:37:40 -07:00
|
|
|
package keeper
|
|
|
|
|
2018-10-10 14:43:47 -07:00
|
|
|
import (
|
|
|
|
"testing"
|
2018-10-04 18:37:40 -07:00
|
|
|
|
2018-10-10 14:43:47 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-12-10 06:27:25 -08:00
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-01-11 12:08:01 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking"
|
2018-10-10 14:43:47 -07:00
|
|
|
)
|
2018-10-04 18:37:40 -07:00
|
|
|
|
2018-10-19 11:36:00 -07:00
|
|
|
func TestAllocateTokensBasic(t *testing.T) {
|
2018-10-10 14:43:47 -07:00
|
|
|
|
|
|
|
// no community tax on inputs
|
|
|
|
ctx, _, keeper, sk, fck := CreateTestInputAdvanced(t, false, 100, sdk.ZeroDec())
|
2019-01-11 12:08:01 -08:00
|
|
|
stakingHandler := staking.NewHandler(sk)
|
2018-10-10 14:43:47 -07:00
|
|
|
denom := sk.GetParams(ctx).BondDenom
|
|
|
|
|
|
|
|
//first make a validator
|
2018-10-10 20:16:49 -07:00
|
|
|
totalPower := int64(10)
|
2019-01-02 12:29:47 -08:00
|
|
|
totalPowerInt := sdk.NewInt(totalPower)
|
2019-01-11 12:08:01 -08:00
|
|
|
msgCreateValidator := staking.NewTestMsgCreateValidator(valOpAddr1, valConsPk1, totalPower)
|
|
|
|
got := stakingHandler(ctx, msgCreateValidator)
|
2018-10-10 14:43:47 -07:00
|
|
|
require.True(t, got.IsOK(), "expected msg to be ok, got %v", got)
|
|
|
|
_ = sk.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
|
|
|
|
|
|
// verify everything has been set in staking correctly
|
|
|
|
validator, found := sk.GetValidator(ctx, valOpAddr1)
|
|
|
|
require.True(t, found)
|
|
|
|
require.Equal(t, sdk.Bonded, validator.Status)
|
2019-01-02 12:29:47 -08:00
|
|
|
assert.True(sdk.IntEq(t, totalPowerInt, validator.Tokens))
|
|
|
|
assert.True(sdk.DecEq(t, sdk.NewDec(totalPower), validator.DelegatorShares))
|
2018-10-10 14:43:47 -07:00
|
|
|
bondedTokens := sk.TotalPower(ctx)
|
2019-01-02 12:29:47 -08:00
|
|
|
assert.True(sdk.IntEq(t, totalPowerInt, bondedTokens))
|
2018-10-10 14:43:47 -07:00
|
|
|
|
|
|
|
// initial fee pool should be empty
|
|
|
|
feePool := keeper.GetFeePool(ctx)
|
2018-10-26 04:27:55 -07:00
|
|
|
require.Nil(t, feePool.ValPool)
|
2018-10-10 14:43:47 -07:00
|
|
|
|
2018-10-10 20:16:49 -07:00
|
|
|
// allocate 100 denom of fees
|
2018-10-10 14:43:47 -07:00
|
|
|
feeInputs := sdk.NewInt(100)
|
|
|
|
fck.SetCollectedFees(sdk.Coins{sdk.NewCoin(denom, feeInputs)})
|
|
|
|
require.Equal(t, feeInputs, fck.GetCollectedFees(ctx).AmountOf(denom))
|
2018-10-19 11:36:00 -07:00
|
|
|
keeper.AllocateTokens(ctx, sdk.OneDec(), valConsAddr1)
|
2018-10-10 14:43:47 -07:00
|
|
|
|
|
|
|
// verify that these fees have been received by the feePool
|
2018-10-15 12:51:51 -07:00
|
|
|
percentProposer := sdk.NewDecWithPrec(5, 2)
|
|
|
|
percentRemaining := sdk.OneDec().Sub(percentProposer)
|
2018-10-10 14:43:47 -07:00
|
|
|
feePool = keeper.GetFeePool(ctx)
|
2018-10-15 12:51:51 -07:00
|
|
|
expRes := sdk.NewDecFromInt(feeInputs).Mul(percentRemaining)
|
2018-10-26 04:27:55 -07:00
|
|
|
require.Equal(t, 1, len(feePool.ValPool))
|
|
|
|
require.True(sdk.DecEq(t, expRes, feePool.ValPool[0].Amount))
|
2018-10-04 18:37:40 -07:00
|
|
|
}
|
2018-10-10 20:16:49 -07:00
|
|
|
|
2018-10-19 11:36:00 -07:00
|
|
|
func TestAllocateTokensWithCommunityTax(t *testing.T) {
|
2018-10-15 12:51:51 -07:00
|
|
|
communityTax := sdk.NewDecWithPrec(1, 2) //1%
|
|
|
|
ctx, _, keeper, sk, fck := CreateTestInputAdvanced(t, false, 100, communityTax)
|
2019-01-11 12:08:01 -08:00
|
|
|
stakingHandler := staking.NewHandler(sk)
|
2018-10-10 20:16:49 -07:00
|
|
|
denom := sk.GetParams(ctx).BondDenom
|
|
|
|
|
|
|
|
//first make a validator
|
|
|
|
totalPower := int64(10)
|
2019-01-11 12:08:01 -08:00
|
|
|
msgCreateValidator := staking.NewTestMsgCreateValidator(valOpAddr1, valConsPk1, totalPower)
|
|
|
|
got := stakingHandler(ctx, msgCreateValidator)
|
2018-10-10 20:16:49 -07:00
|
|
|
require.True(t, got.IsOK(), "expected msg to be ok, got %v", got)
|
|
|
|
_ = sk.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
|
|
|
|
|
|
// allocate 100 denom of fees
|
|
|
|
feeInputs := sdk.NewInt(100)
|
|
|
|
fck.SetCollectedFees(sdk.Coins{sdk.NewCoin(denom, feeInputs)})
|
2018-10-19 11:36:00 -07:00
|
|
|
keeper.AllocateTokens(ctx, sdk.OneDec(), valConsAddr1)
|
2018-10-10 20:16:49 -07:00
|
|
|
|
|
|
|
// verify that these fees have been received by the feePool
|
|
|
|
feePool := keeper.GetFeePool(ctx)
|
|
|
|
// 5% goes to proposer, 1% community tax
|
2018-10-15 12:51:51 -07:00
|
|
|
percentProposer := sdk.NewDecWithPrec(5, 2)
|
|
|
|
percentRemaining := sdk.OneDec().Sub(communityTax.Add(percentProposer))
|
|
|
|
expRes := sdk.NewDecFromInt(feeInputs).Mul(percentRemaining)
|
2018-10-26 04:27:55 -07:00
|
|
|
require.Equal(t, 1, len(feePool.ValPool))
|
|
|
|
require.True(sdk.DecEq(t, expRes, feePool.ValPool[0].Amount))
|
2018-10-10 20:16:49 -07:00
|
|
|
}
|
|
|
|
|
2018-10-19 11:36:00 -07:00
|
|
|
func TestAllocateTokensWithPartialPrecommitPower(t *testing.T) {
|
2018-10-15 12:51:51 -07:00
|
|
|
communityTax := sdk.NewDecWithPrec(1, 2)
|
|
|
|
ctx, _, keeper, sk, fck := CreateTestInputAdvanced(t, false, 100, communityTax)
|
2019-01-11 12:08:01 -08:00
|
|
|
stakingHandler := staking.NewHandler(sk)
|
2018-10-10 20:16:49 -07:00
|
|
|
denom := sk.GetParams(ctx).BondDenom
|
|
|
|
|
|
|
|
//first make a validator
|
2018-10-12 04:15:13 -07:00
|
|
|
totalPower := int64(100)
|
2019-01-11 12:08:01 -08:00
|
|
|
msgCreateValidator := staking.NewTestMsgCreateValidator(valOpAddr1, valConsPk1, totalPower)
|
|
|
|
got := stakingHandler(ctx, msgCreateValidator)
|
2018-10-10 20:16:49 -07:00
|
|
|
require.True(t, got.IsOK(), "expected msg to be ok, got %v", got)
|
|
|
|
_ = sk.ApplyAndReturnValidatorSetUpdates(ctx)
|
|
|
|
|
|
|
|
// allocate 100 denom of fees
|
|
|
|
feeInputs := sdk.NewInt(100)
|
|
|
|
fck.SetCollectedFees(sdk.Coins{sdk.NewCoin(denom, feeInputs)})
|
2018-10-15 12:51:51 -07:00
|
|
|
percentPrecommitVotes := sdk.NewDecWithPrec(25, 2)
|
2018-10-19 11:36:00 -07:00
|
|
|
keeper.AllocateTokens(ctx, percentPrecommitVotes, valConsAddr1)
|
2018-10-10 20:16:49 -07:00
|
|
|
|
|
|
|
// verify that these fees have been received by the feePool
|
|
|
|
feePool := keeper.GetFeePool(ctx)
|
2018-10-15 12:51:51 -07:00
|
|
|
// 1% + 4%*0.25 to proposer + 1% community tax = 97%
|
|
|
|
percentProposer := sdk.NewDecWithPrec(1, 2).Add(sdk.NewDecWithPrec(4, 2).Mul(percentPrecommitVotes))
|
|
|
|
percentRemaining := sdk.OneDec().Sub(communityTax.Add(percentProposer))
|
|
|
|
expRes := sdk.NewDecFromInt(feeInputs).Mul(percentRemaining)
|
2018-10-26 04:27:55 -07:00
|
|
|
require.Equal(t, 1, len(feePool.ValPool))
|
|
|
|
require.True(sdk.DecEq(t, expRes, feePool.ValPool[0].Amount))
|
2018-10-10 20:16:49 -07:00
|
|
|
}
|