allocation tests
This commit is contained in:
parent
900a5f47f6
commit
f4f39ae03f
|
@ -19,10 +19,7 @@ func (k Keeper) AllocateFees(ctx sdk.Context) {
|
|||
|
||||
// get the proposer of this block
|
||||
proposerConsAddr := k.GetProposerConsAddr(ctx)
|
||||
fmt.Printf("debug proposerConsAddr: %v\n", proposerConsAddr.String())
|
||||
proposerValidator := k.stakeKeeper.ValidatorByConsAddr(ctx, proposerConsAddr)
|
||||
fmt.Printf("debug in allocate proposerValidator: %v\n", proposerValidator.GetOperator())
|
||||
|
||||
proposerDist := k.GetValidatorDistInfo(ctx, proposerValidator.GetOperator())
|
||||
|
||||
// get the fees which have been getting collected through all the
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package keeper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
|
@ -18,7 +17,9 @@ func TestAllocateFeesBasic(t *testing.T) {
|
|||
denom := sk.GetParams(ctx).BondDenom
|
||||
|
||||
//first make a validator
|
||||
msgCreateValidator := stake.NewTestMsgCreateValidator(valOpAddr1, valConsPk1, 10)
|
||||
totalPower := int64(10)
|
||||
totalPowerDec := sdk.NewDec(totalPower)
|
||||
msgCreateValidator := stake.NewTestMsgCreateValidator(valOpAddr1, valConsPk1, totalPower)
|
||||
got := stakeHandler(ctx, msgCreateValidator)
|
||||
require.True(t, got.IsOK(), "expected msg to be ok, got %v", got)
|
||||
_ = sk.ApplyAndReturnValidatorSetUpdates(ctx)
|
||||
|
@ -27,22 +28,21 @@ func TestAllocateFeesBasic(t *testing.T) {
|
|||
validator, found := sk.GetValidator(ctx, valOpAddr1)
|
||||
require.True(t, found)
|
||||
require.Equal(t, sdk.Bonded, validator.Status)
|
||||
assert.True(sdk.DecEq(t, sdk.NewDec(10), validator.Tokens))
|
||||
assert.True(sdk.DecEq(t, sdk.NewDec(10), validator.DelegatorShares))
|
||||
assert.True(sdk.DecEq(t, totalPowerDec, validator.Tokens))
|
||||
assert.True(sdk.DecEq(t, totalPowerDec, validator.DelegatorShares))
|
||||
bondedTokens := sk.TotalPower(ctx)
|
||||
assert.True(sdk.DecEq(t, sdk.NewDec(10), bondedTokens))
|
||||
assert.True(sdk.DecEq(t, totalPowerDec, bondedTokens))
|
||||
|
||||
// initial fee pool should be empty
|
||||
feePool := keeper.GetFeePool(ctx)
|
||||
require.Nil(t, feePool.Pool)
|
||||
|
||||
// allocate 10 denom of fees
|
||||
// allocate 100 denom of fees
|
||||
feeInputs := sdk.NewInt(100)
|
||||
fck.SetCollectedFees(sdk.Coins{sdk.NewCoin(denom, feeInputs)})
|
||||
require.Equal(t, feeInputs, fck.GetCollectedFees(ctx).AmountOf(denom))
|
||||
fmt.Printf("debug valConsAddr1: %v\n", valConsAddr1.String())
|
||||
keeper.SetProposerConsAddr(ctx, valConsAddr1)
|
||||
keeper.SetSumPrecommitPower(ctx, sdk.NewDec(10))
|
||||
keeper.SetSumPrecommitPower(ctx, totalPowerDec)
|
||||
keeper.AllocateFees(ctx)
|
||||
|
||||
// verify that these fees have been received by the feePool
|
||||
|
@ -51,3 +51,59 @@ func TestAllocateFeesBasic(t *testing.T) {
|
|||
require.Equal(t, 1, len(feePool.Pool))
|
||||
require.True(sdk.DecEq(t, expRes, feePool.Pool[0].Amount))
|
||||
}
|
||||
|
||||
func TestAllocateFeesWithCommunityTax(t *testing.T) {
|
||||
ctx, _, keeper, sk, fck := CreateTestInputAdvanced(t, false, 100, sdk.NewDecWithPrec(1, 2)) //1%
|
||||
stakeHandler := stake.NewHandler(sk)
|
||||
denom := sk.GetParams(ctx).BondDenom
|
||||
|
||||
//first make a validator
|
||||
totalPower := int64(10)
|
||||
totalPowerDec := sdk.NewDec(totalPower)
|
||||
msgCreateValidator := stake.NewTestMsgCreateValidator(valOpAddr1, valConsPk1, totalPower)
|
||||
got := stakeHandler(ctx, msgCreateValidator)
|
||||
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)})
|
||||
keeper.SetProposerConsAddr(ctx, valConsAddr1)
|
||||
keeper.SetSumPrecommitPower(ctx, totalPowerDec)
|
||||
keeper.AllocateFees(ctx)
|
||||
|
||||
// verify that these fees have been received by the feePool
|
||||
feePool := keeper.GetFeePool(ctx)
|
||||
// 5% goes to proposer, 1% community tax
|
||||
expRes := sdk.NewDecFromInt(feeInputs).Mul(sdk.NewDecWithPrec(94, 2))
|
||||
require.Equal(t, 1, len(feePool.Pool))
|
||||
require.True(sdk.DecEq(t, expRes, feePool.Pool[0].Amount))
|
||||
}
|
||||
|
||||
func TestAllocateFeesWithPartialPrecommitPower(t *testing.T) {
|
||||
ctx, _, keeper, sk, fck := CreateTestInputAdvanced(t, false, 100, sdk.NewDecWithPrec(1, 2)) //1%
|
||||
stakeHandler := stake.NewHandler(sk)
|
||||
denom := sk.GetParams(ctx).BondDenom
|
||||
|
||||
//first make a validator
|
||||
totalPower := int64(10)
|
||||
totalPowerDec := sdk.NewDec(totalPower)
|
||||
msgCreateValidator := stake.NewTestMsgCreateValidator(valOpAddr1, valConsPk1, totalPower)
|
||||
got := stakeHandler(ctx, msgCreateValidator)
|
||||
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)})
|
||||
keeper.SetProposerConsAddr(ctx, valConsAddr1)
|
||||
keeper.SetSumPrecommitPower(ctx, totalPowerDec.Mul(sdk.NewDecWithPrec(25, 2))) // 25% precommit power
|
||||
keeper.AllocateFees(ctx)
|
||||
|
||||
// verify that these fees have been received by the feePool
|
||||
feePool := keeper.GetFeePool(ctx)
|
||||
// 1% + 4%*0.25 goes to proposer, 1% community tax
|
||||
expRes := sdk.NewDecFromInt(feeInputs).Mul(sdk.NewDecWithPrec(97, 2))
|
||||
require.Equal(t, 1, len(feePool.Pool))
|
||||
require.True(sdk.DecEq(t, expRes, feePool.Pool[0].Amount))
|
||||
}
|
||||
|
|
|
@ -10,8 +10,6 @@ import (
|
|||
// Create a new validator distribution record
|
||||
func (k Keeper) onValidatorCreated(ctx sdk.Context, addr sdk.ValAddress) {
|
||||
|
||||
fmt.Printf("debug asdgojasklnaslkjv addr: %v\n", addr)
|
||||
|
||||
height := ctx.BlockHeight()
|
||||
vdi := types.ValidatorDistInfo{
|
||||
OperatorAddr: addr,
|
||||
|
|
|
@ -85,7 +85,7 @@ func (k Keeper) SetProposerConsAddr(ctx sdk.Context, consAddr sdk.ConsAddress) {
|
|||
func (k Keeper) GetSumPrecommitPower(ctx sdk.Context) (sumPrecommitPower sdk.Dec) {
|
||||
tstore := ctx.KVStore(k.storeTKey)
|
||||
|
||||
b := tstore.Get(ProposerKey)
|
||||
b := tstore.Get(SumPrecommitPowerKey)
|
||||
if b == nil {
|
||||
panic("Proposer cons address was likely not set in begin block")
|
||||
}
|
||||
|
@ -98,7 +98,7 @@ func (k Keeper) GetSumPrecommitPower(ctx sdk.Context) (sumPrecommitPower sdk.Dec
|
|||
func (k Keeper) SetSumPrecommitPower(ctx sdk.Context, sumPrecommitPower sdk.Dec) {
|
||||
tstore := ctx.KVStore(k.storeTKey)
|
||||
b := k.cdc.MustMarshalBinary(sumPrecommitPower)
|
||||
tstore.Set(ProposerKey, b)
|
||||
tstore.Set(SumPrecommitPowerKey, b)
|
||||
}
|
||||
|
||||
//______________________________________________________________________
|
||||
|
|
Loading…
Reference in New Issue