address @ValarDragon comments, param store update

This commit is contained in:
rigelrozanski 2018-10-15 15:51:51 -04:00
parent 5de0c9a1f8
commit 2f4a01bcba
10 changed files with 53 additions and 34 deletions

View File

@ -111,8 +111,8 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio
app.distrKeeper = distr.NewKeeper(
app.cdc,
app.keyDistr, app.tkeyDistr,
app.paramsKeeper.Setter(), app.bankKeeper, app.stakeKeeper,
app.feeCollectionKeeper,
app.paramsKeeper.Subspace(distr.DefaultParamspace),
app.bankKeeper, app.stakeKeeper, app.feeCollectionKeeper,
app.RegisterCodespace(stake.DefaultCodespace),
)
app.slashingKeeper = slashing.NewKeeper(

View File

@ -244,10 +244,6 @@ func (d Dec) Quo(d2 Dec) Dec {
// quotient
func (d Dec) QuoInt(i Int) Dec {
mul := new(big.Int).Quo(d.Int, i.i)
if mul.BitLen() > 255+DecimalPrecisionBits {
panic("Int overflow")
}
return Dec{mul}
}

View File

@ -12,7 +12,7 @@ type (
Hooks = keeper.Hooks
DelegatorWithdrawInfo = types.DelegatorWithdrawInfo
DelegationDistInfo = types.DelegationDistInfo
DelegationDistInfo = types.DelegationDistInfo
ValidatorDistInfo = types.ValidatorDistInfo
TotalAccum = types.TotalAccum
FeePool = types.FeePool
@ -34,9 +34,10 @@ var (
GetDelegatorWithdrawAddrKey = keeper.GetDelegatorWithdrawAddrKey
FeePoolKey = keeper.FeePoolKey
ValidatorDistInfoKey = keeper.ValidatorDistInfoKey
DelegationDistInfoKey = keeper.DelegationDistInfoKey
DelegationDistInfoKey = keeper.DelegationDistInfoKey
DelegatorWithdrawInfoKey = keeper.DelegatorWithdrawInfoKey
ProposerKey = keeper.ProposerKey
DefaultParamspace = keeper.DefaultParamspace
InitialFeePool = types.InitialFeePool

View File

@ -26,8 +26,8 @@ func InitGenesis(ctx sdk.Context, keeper Keeper, data types.GenesisState) {
func WriteGenesis(ctx sdk.Context, keeper Keeper) types.GenesisState {
feePool := keeper.GetFeePool(ctx)
communityTax := keeper.GetCommunityTax(ctx)
vdis := keeper.GetAllVDIs(ctx)
ddis := keeper.GetAllDDIs(ctx)
dwis := keeper.GetAllDWIs(ctx)
vdis := keeper.GetAllValidatorDistInfos(ctx)
ddis := keeper.GetAllDelegationDistInfos(ctx)
dwis := keeper.GetAllDelegatorWithdrawInfos(ctx)
return NewGenesisState(feePool, communityTax, vdis, ddis, dwis)
}

View File

@ -46,14 +46,17 @@ func TestAllocateFeesBasic(t *testing.T) {
keeper.AllocateFees(ctx)
// verify that these fees have been received by the feePool
percentProposer := sdk.NewDecWithPrec(5, 2)
percentRemaining := sdk.OneDec().Sub(percentProposer)
feePool = keeper.GetFeePool(ctx)
expRes := sdk.NewDecFromInt(feeInputs).Mul(sdk.NewDecWithPrec(95, 2)) // 5% goes to proposer
expRes := sdk.NewDecFromInt(feeInputs).Mul(percentRemaining)
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%
communityTax := sdk.NewDecWithPrec(1, 2) //1%
ctx, _, keeper, sk, fck := CreateTestInputAdvanced(t, false, 100, communityTax)
stakeHandler := stake.NewHandler(sk)
denom := sk.GetParams(ctx).BondDenom
@ -74,13 +77,16 @@ func TestAllocateFeesWithCommunityTax(t *testing.T) {
// 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))
percentProposer := sdk.NewDecWithPrec(5, 2)
percentRemaining := sdk.OneDec().Sub(communityTax.Add(percentProposer))
expRes := sdk.NewDecFromInt(feeInputs).Mul(percentRemaining)
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%
communityTax := sdk.NewDecWithPrec(1, 2)
ctx, _, keeper, sk, fck := CreateTestInputAdvanced(t, false, 100, communityTax)
stakeHandler := stake.NewHandler(sk)
denom := sk.GetParams(ctx).BondDenom
@ -95,13 +101,16 @@ func TestAllocateFeesWithPartialPrecommitPower(t *testing.T) {
feeInputs := sdk.NewInt(100)
fck.SetCollectedFees(sdk.Coins{sdk.NewCoin(denom, feeInputs)})
keeper.SetProposerConsAddr(ctx, valConsAddr1)
keeper.SetPercentPrecommitVotes(ctx, sdk.NewDecWithPrec(25, 2))
percentPrecommitVotes := sdk.NewDecWithPrec(25, 2)
keeper.SetPercentPrecommitVotes(ctx, percentPrecommitVotes)
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))
// 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)
require.Equal(t, 1, len(feePool.Pool))
require.True(sdk.DecEq(t, expRes, feePool.Pool[0].Amount))
}

View File

@ -6,7 +6,7 @@ import (
)
// Get the set of all validator-distribution-info's with no limits, used during genesis dump
func (k Keeper) GetAllVDIs(ctx sdk.Context) (vdis []types.ValidatorDistInfo) {
func (k Keeper) GetAllValidatorDistInfos(ctx sdk.Context) (vdis []types.ValidatorDistInfo) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, ValidatorDistInfoKey)
defer iterator.Close()
@ -20,7 +20,7 @@ func (k Keeper) GetAllVDIs(ctx sdk.Context) (vdis []types.ValidatorDistInfo) {
}
// Get the set of all delegator-distribution-info's with no limits, used during genesis dump
func (k Keeper) GetAllDDIs(ctx sdk.Context) (ddis []types.DelegationDistInfo) {
func (k Keeper) GetAllDelegationDistInfos(ctx sdk.Context) (ddis []types.DelegationDistInfo) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, DelegationDistInfoKey)
defer iterator.Close()
@ -34,7 +34,7 @@ func (k Keeper) GetAllDDIs(ctx sdk.Context) (ddis []types.DelegationDistInfo) {
}
// Get the set of all delegator-withdraw addresses with no limits, used during genesis dump
func (k Keeper) GetAllDWIs(ctx sdk.Context) (dwis []types.DelegatorWithdrawInfo) {
func (k Keeper) GetAllDelegatorWithdrawInfos(ctx sdk.Context) (dwis []types.DelegatorWithdrawInfo) {
store := ctx.KVStore(k.storeKey)
iterator := sdk.KVStorePrefixIterator(store, DelegationDistInfoKey)
defer iterator.Close()

View File

@ -12,7 +12,7 @@ type Keeper struct {
storeKey sdk.StoreKey
storeTKey sdk.StoreKey
cdc *codec.Codec
ps params.Setter
paramSpace params.Subspace
bankKeeper types.BankKeeper
stakeKeeper types.StakeKeeper
feeCollectionKeeper types.FeeCollectionKeeper
@ -21,14 +21,14 @@ type Keeper struct {
codespace sdk.CodespaceType
}
func NewKeeper(cdc *codec.Codec, key, tkey sdk.StoreKey, ps params.Setter, ck types.BankKeeper,
func NewKeeper(cdc *codec.Codec, key, tkey sdk.StoreKey, paramSpace params.Subspace, ck types.BankKeeper,
sk types.StakeKeeper, fck types.FeeCollectionKeeper, codespace sdk.CodespaceType) Keeper {
keeper := Keeper{
storeKey: key,
storeTKey: tkey,
cdc: cdc,
ps: ps,
paramSpace: paramSpace.WithTypeTable(ParamTypeTable()),
bankKeeper: ck,
stakeKeeper: sk,
feeCollectionKeeper: fck,
@ -104,15 +104,22 @@ func (k Keeper) SetPercentPrecommitVotes(ctx sdk.Context, percentPrecommitVotes
//______________________________________________________________________
// PARAM STORE
// Type declaration for parameters
func ParamTypeTable() params.TypeTable {
return params.NewTypeTable(
ParamStoreKeyCommunityTax, sdk.Dec{},
)
}
// Returns the current CommunityTax rate from the global param store
// nolint: errcheck
func (k Keeper) GetCommunityTax(ctx sdk.Context) sdk.Dec {
var communityTax sdk.Dec
k.ps.Get(ctx, ParamStoreKeyCommunityTax, &communityTax)
k.paramSpace.Get(ctx, ParamStoreKeyCommunityTax, &communityTax)
return communityTax
}
// nolint: errcheck
func (k Keeper) SetCommunityTax(ctx sdk.Context, communityTax sdk.Dec) {
k.ps.Set(ctx, ParamStoreKeyCommunityTax, &communityTax)
k.paramSpace.Set(ctx, ParamStoreKeyCommunityTax, &communityTax)
}

View File

@ -14,11 +14,14 @@ var (
// transient
ProposerKey = []byte{0x00} // key for storing the proposer operator address
PercentPrecommitVotesKey = []byte{0x01} // key for storing the power of the precommit validators
// params store
ParamStoreKeyCommunityTax = []byte("distr/community-tax")
)
// nolint
const (
ParamStoreKeyCommunityTax = "distr/community-tax"
// default paramspace for params keeper
DefaultParamspace = "distr"
)
// gets the key for the validator distribution info from address

View File

@ -90,6 +90,7 @@ func CreateTestInputAdvanced(t *testing.T, isCheckTx bool, initCoins int64,
keyAcc := sdk.NewKVStoreKey("acc")
keyFeeCollection := sdk.NewKVStoreKey("fee")
keyParams := sdk.NewKVStoreKey("params")
tkeyParams := sdk.NewTransientStoreKey("transient_params")
db := dbm.NewMemDB()
ms := store.NewCommitMultiStore(db)
@ -101,15 +102,18 @@ func CreateTestInputAdvanced(t *testing.T, isCheckTx bool, initCoins int64,
ms.MountStoreWithDB(keyAcc, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(keyFeeCollection, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db)
err := ms.LoadLatestVersion()
require.Nil(t, err)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "foochainid"}, isCheckTx, log.NewNopLogger())
cdc := MakeTestCodec()
pk := params.NewKeeper(cdc, keyParams, tkeyParams)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "foochainid"}, isCheckTx, log.NewNopLogger())
accountMapper := auth.NewAccountMapper(cdc, keyAcc, auth.ProtoBaseAccount)
ck := bank.NewBaseKeeper(accountMapper)
sk := stake.NewKeeper(cdc, keyStake, tkeyStake, ck, stake.DefaultCodespace)
sk := stake.NewKeeper(cdc, keyStake, tkeyStake, ck, pk.Subspace(stake.DefaultParamspace), stake.DefaultCodespace)
sk.SetPool(ctx, stake.InitialPool())
sk.SetParams(ctx, stake.DefaultParams())
sk.InitIntraTxCounter(ctx)
@ -126,8 +130,7 @@ func CreateTestInputAdvanced(t *testing.T, isCheckTx bool, initCoins int64,
}
fck := DummyFeeCollectionKeeper{}
pk := params.NewKeeper(cdc, keyParams)
keeper := NewKeeper(cdc, keyDistr, tkeyDistr, pk.Setter(), ck, sk, fck, types.DefaultCodespace)
keeper := NewKeeper(cdc, keyDistr, tkeyDistr, pk.Subspace(DefaultParamspace), ck, sk, fck, types.DefaultCodespace)
// set the distribution hooks on staking
sk = sk.WithHooks(keeper.Hooks())

View File

@ -612,12 +612,12 @@ func TestValidatorQueue(t *testing.T) {
keeper.SetParams(ctx, params)
// create the validator
msgCreateValidator := newTestMsgCreateValidator(validatorAddr, keep.PKs[0], 10)
msgCreateValidator := NewTestMsgCreateValidator(validatorAddr, keep.PKs[0], 10)
got := handleMsgCreateValidator(ctx, msgCreateValidator, keeper)
require.True(t, got.IsOK(), "expected no error on runMsgCreateValidator")
// bond a delegator
msgDelegate := newTestMsgDelegate(delegatorAddr, validatorAddr, 10)
msgDelegate := NewTestMsgDelegate(delegatorAddr, validatorAddr, 10)
got = handleMsgDelegate(ctx, msgDelegate, keeper)
require.True(t, got.IsOK(), "expected ok, got %v", got)