lcd tests to include proposer

This commit is contained in:
rigelrozanski 2018-09-28 04:02:07 -04:00
parent 60a3541380
commit 7ab02aed76
8 changed files with 37 additions and 22 deletions

View File

@ -139,7 +139,17 @@ func InitializeTestLCD(t *testing.T, nValidators int, initAddrs []sdk.AccAddress
panic("InitializeTestLCD must use at least one validator")
}
for i := 1; i < nValidators; i++ {
// add the priv validator (actual node) to genesis validators
genDoc.Validators = append(genDoc.Validators,
tmtypes.GenesisValidator{
PubKey: privVal.PubKey,
Power: 1,
Name: "val",
},
)
// then add some randoms
for i := 2; i < nValidators; i++ {
genDoc.Validators = append(genDoc.Validators,
tmtypes.GenesisValidator{
PubKey: ed25519.GenPrivKey().PubKey(),

View File

@ -98,7 +98,7 @@ func NewGaiaApp(logger log.Logger, db dbm.DB, traceStore io.Writer, baseAppOptio
app.paramsKeeper = params.NewKeeper(app.cdc, app.keyParams)
app.stakeKeeper = stake.NewKeeper(app.cdc, app.keyStake, app.tkeyStake,
app.bankKeeper, app.RegisterCodespace(stake.DefaultCodespace))
app.distrKeeper = distr.NewKeeper(app.cdc, app.keyDistr, app.tkeyStake,
app.distrKeeper = distr.NewKeeper(app.cdc, app.keyDistr, app.tkeyDistr,
app.paramsKeeper.Setter(), app.bankKeeper, app.stakeKeeper,
app.feeCollectionKeeper, app.RegisterCodespace(stake.DefaultCodespace))
app.slashingKeeper = slashing.NewKeeper(app.cdc, app.keySlashing, app.stakeKeeper,
@ -168,7 +168,7 @@ func (app *GaiaApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.R
validatorUpdates := stake.EndBlocker(ctx, app.stakeKeeper)
// distribute rewards
//distr.EndBlocker(ctx, app.distrKeeper)
distr.EndBlocker(ctx, app.distrKeeper)
// Add these new validators to the addr -> pubkey map.
app.slashingKeeper.AddValidators(ctx, validatorUpdates)

View File

@ -15,5 +15,8 @@ func BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock, k keeper.Keeper)
// allocate fees
func EndBlocker(ctx sdk.Context, k keeper.Keeper) {
if ctx.BlockHeight() < 2 {
return
}
k.AllocateFees(ctx)
}

View File

@ -9,19 +9,17 @@ import (
// Allocate fees handles distribution of the collected fees
func (k Keeper) AllocateFees(ctx sdk.Context) {
fmt.Println("wackydebugoutput AllocateFees 0")
ctx.Logger().With("module", "x/distribution").Error(fmt.Sprintf("allocation height: %v", ctx.BlockHeight()))
// get the proposer of this block
proposerConsAddr := k.GetProposerConsAddr(ctx)
proposerValidator := k.stakeKeeper.ValidatorByConsAddr(ctx, proposerConsAddr)
proposerDist := k.GetValidatorDistInfo(ctx, proposerValidator.GetOperator())
fmt.Println("wackydebugoutput AllocateFees 1")
// get the fees which have been getting collected through all the
// transactions in the block
feesCollected := k.feeCollectionKeeper.GetCollectedFees(ctx)
feesCollectedDec := types.NewDecCoins(feesCollected)
fmt.Println("wackydebugoutput AllocateFees 2")
// allocated rewards to proposer
bondedTokens := k.stakeKeeper.TotalPower(ctx)
@ -29,30 +27,25 @@ func (k Keeper) AllocateFees(ctx sdk.Context) {
proposerMultiplier := sdk.NewDecWithPrec(1, 2).Add(sdk.NewDecWithPrec(4, 2).Mul(
sumPowerPrecommitValidators).Quo(bondedTokens))
proposerReward := feesCollectedDec.Mul(proposerMultiplier)
fmt.Println("wackydebugoutput AllocateFees 3")
// apply commission
commission := proposerReward.Mul(proposerValidator.GetCommission())
remaining := proposerReward.Mul(sdk.OneDec().Sub(proposerValidator.GetCommission()))
proposerDist.PoolCommission = proposerDist.PoolCommission.Plus(commission)
proposerDist.Pool = proposerDist.Pool.Plus(remaining)
fmt.Println("wackydebugoutput AllocateFees 4")
// allocate community funding
communityTax := k.GetCommunityTax(ctx)
communityFunding := feesCollectedDec.Mul(communityTax)
feePool := k.GetFeePool(ctx)
feePool.CommunityPool = feePool.CommunityPool.Plus(communityFunding)
fmt.Println("wackydebugoutput AllocateFees 5")
// set the global pool within the distribution module
poolReceived := feesCollectedDec.Mul(sdk.OneDec().Sub(proposerMultiplier).Sub(communityTax))
feePool.Pool = feePool.Pool.Plus(poolReceived)
fmt.Println("wackydebugoutput AllocateFees 0")
k.SetValidatorDistInfo(ctx, proposerDist)
k.SetFeePool(ctx, feePool)
fmt.Println("wackydebugoutput AllocateFees 6")
// clear the now distributed fees
k.feeCollectionKeeper.ClearCollectedFees(ctx)

View File

@ -79,7 +79,10 @@ func (k Keeper) WithdrawDelegationReward(ctx sdk.Context, delegatorAddr sdk.AccA
k.SetFeePool(ctx, feePool)
withdrawAddr := k.GetDelegatorWithdrawAddr(ctx, delegatorAddr)
k.bankKeeper.AddCoins(ctx, withdrawAddr, withdraw.TruncateDecimal())
_, _, err := k.bankKeeper.AddCoins(ctx, withdrawAddr, withdraw.TruncateDecimal())
if err != nil {
panic(err)
}
}
//___________________________________________________________________________________________
@ -89,7 +92,10 @@ func (k Keeper) WithdrawDelegationRewardsAll(ctx sdk.Context, delegatorAddr sdk.
height := ctx.BlockHeight()
withdraw := k.GetDelegatorRewardsAll(ctx, delegatorAddr, height)
withdrawAddr := k.GetDelegatorWithdrawAddr(ctx, delegatorAddr)
k.bankKeeper.AddCoins(ctx, withdrawAddr, withdraw.TruncateDecimal())
_, _, err := k.bankKeeper.AddCoins(ctx, withdrawAddr, withdraw.TruncateDecimal())
if err != nil {
panic(err)
}
}
// return all rewards for all delegations of a delegator

View File

@ -65,9 +65,9 @@ func (k Keeper) SetFeePool(ctx sdk.Context, feePool types.FeePool) {
// set the proposer public key for this block
func (k Keeper) GetProposerConsAddr(ctx sdk.Context) (consAddr sdk.ConsAddress) {
store := ctx.KVStore(k.storeKey)
tstore := ctx.KVStore(k.storeTKey)
b := store.Get(ProposerKey)
b := tstore.Get(ProposerKey)
if b == nil {
panic("Stored fee pool should not have been nil")
}
@ -78,9 +78,9 @@ func (k Keeper) GetProposerConsAddr(ctx sdk.Context) (consAddr sdk.ConsAddress)
// get the proposer public key for this block
func (k Keeper) SetProposerConsAddr(ctx sdk.Context, consAddr sdk.ConsAddress) {
store := ctx.KVStore(k.storeKey)
tstore := ctx.KVStore(k.storeTKey)
b := k.cdc.MustMarshalBinary(consAddr)
store.Set(ProposerKey, b)
tstore.Set(ProposerKey, b)
}
//______________________________________________________________________

View File

@ -6,10 +6,10 @@ import (
// keys/key-prefixes
var (
FeePoolKey = []byte{0x01} // key for global distribution state
ValidatorDistInfoKey = []byte{0x02} // prefix for each key to a validator distribution
DelegatorDistInfoKey = []byte{0x03} // prefix for each key to a delegation distribution
DelegatorWithdrawInfoKey = []byte{0x04} // prefix for each key to a delegator withdraw info
FeePoolKey = []byte{0x00} // key for global distribution state
ValidatorDistInfoKey = []byte{0x01} // prefix for each key to a validator distribution
DelegatorDistInfoKey = []byte{0x02} // prefix for each key to a delegation distribution
DelegatorWithdrawInfoKey = []byte{0x03} // prefix for each key to a delegator withdraw info
// transient
ProposerKey = []byte{0x00} // key for storing the proposer operator address

View File

@ -53,5 +53,8 @@ func (k Keeper) WithdrawValidatorRewardsAll(ctx sdk.Context, operatorAddr sdk.Va
k.SetFeePool(ctx, feePool)
withdrawAddr := k.GetDelegatorWithdrawAddr(ctx, accAddr)
k.bankKeeper.AddCoins(ctx, withdrawAddr, withdraw.TruncateDecimal())
_, _, err := k.bankKeeper.AddCoins(ctx, withdrawAddr, withdraw.TruncateDecimal())
if err != nil {
panic(err)
}
}