From 076011a0f6e8fc92d3d739d7d79f8a2d889fa4c8 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Mon, 22 Oct 2018 22:17:52 -0400 Subject: [PATCH 1/7] add GetAccum for validator and fee pool --- x/distribution/keeper/keeper.go | 11 +++++++++++ x/distribution/keeper/validator.go | 16 ++++++++++++++++ x/distribution/types/fee_pool.go | 17 ++++++++++++++++- x/distribution/types/validator_info.go | 15 ++++++++++----- 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/x/distribution/keeper/keeper.go b/x/distribution/keeper/keeper.go index 0ccf76ca63db..3919a18aedf7 100644 --- a/x/distribution/keeper/keeper.go +++ b/x/distribution/keeper/keeper.go @@ -55,6 +55,17 @@ func (k Keeper) SetFeePool(ctx sdk.Context, feePool types.FeePool) { store.Set(FeePoolKey, b) } +// get the total validator accum for the ctx height +// in the fee pool +func (k Keeper) GetFeePoolValAccum(ctx sdk.Context) sdk.Dec { + + // withdraw self-delegation + height := ctx.BlockHeight() + totalPower := k.stakeKeeper.GetLastTotalPower(ctx) + fp := k.GetFeePool(ctx) + return fp.GetTotalValAccum(height, totalPower) +} + //______________________________________________________________________ // set the proposer public key for this block diff --git a/x/distribution/keeper/validator.go b/x/distribution/keeper/validator.go index a71249d6b4ce..db3d1a9f0e36 100644 --- a/x/distribution/keeper/validator.go +++ b/x/distribution/keeper/validator.go @@ -40,6 +40,22 @@ func (k Keeper) RemoveValidatorDistInfo(ctx sdk.Context, valAddr sdk.ValAddress) store.Delete(GetValidatorDistInfoKey(valAddr)) } +// Get the calculated accum of a validator at the current block +// without affecting the state. +func (k Keeper) GetValidatorAccum(ctx sdk.Context, operatorAddr sdk.ValAddress) (sdk.Dec, sdk.Error) { + if !k.HasValidatorDistInfo(ctx, operatorAddr) { + return sdk.Dec{}, types.ErrNoValidatorDistInfo(k.codespace) + } + + // withdraw self-delegation + height := ctx.BlockHeight() + lastValPower := k.stakeKeeper.GetLastValidatorPower(ctx, operatorAddr) + valInfo := k.GetValidatorDistInfo(ctx, operatorAddr) + accum := valInfo.GetAccum(height, lastValPower) + + return accum, nil +} + // withdrawal all the validator rewards including the commission func (k Keeper) WithdrawValidatorRewardsAll(ctx sdk.Context, operatorAddr sdk.ValAddress) sdk.Error { diff --git a/x/distribution/types/fee_pool.go b/x/distribution/types/fee_pool.go index ae1d72cc0117..def6c6e75f2f 100644 --- a/x/distribution/types/fee_pool.go +++ b/x/distribution/types/fee_pool.go @@ -17,7 +17,7 @@ func NewTotalAccum(height int64) TotalAccum { } } -// update total validator accumulation factor for the new height +// update total accumulation factor for the new height // CONTRACT: height should be greater than the old height func (ta TotalAccum) UpdateForNewHeight(height int64, accumCreatedPerBlock sdk.Dec) TotalAccum { blocks := height - ta.UpdateHeight @@ -29,6 +29,16 @@ func (ta TotalAccum) UpdateForNewHeight(height int64, accumCreatedPerBlock sdk.D return ta } +// get total accumulation factor for the given height +// CONTRACT: height should be greater than the old height +func (ta TotalAccum) GetAccum(height int64, accumCreatedPerBlock sdk.Dec) sdk.Dec { + blocks := height - ta.UpdateHeight + if blocks < 0 { + panic("reverse updated for new height") + } + return ta.Accum.Add(accumCreatedPerBlock.MulInt(sdk.NewInt(blocks))) +} + //___________________________________________________________________________________________ // global fee pool for distribution @@ -45,6 +55,11 @@ func (f FeePool) UpdateTotalValAccum(height int64, totalBondedTokens sdk.Dec) Fe return f } +// get the total validator accum for the fee pool without modifying the state +func (f FeePool) GetTotalValAccum(height int64, totalBondedTokens sdk.Dec) sdk.Dec { + return f.TotalValAccum.GetAccum(height, totalBondedTokens) +} + // zero fee pool func InitialFeePool() FeePool { return FeePool{ diff --git a/x/distribution/types/validator_info.go b/x/distribution/types/validator_info.go index 5fc86efcd5ea..3caea5d8469b 100644 --- a/x/distribution/types/validator_info.go +++ b/x/distribution/types/validator_info.go @@ -19,9 +19,9 @@ func NewValidatorDistInfo(operatorAddr sdk.ValAddress, currentHeight int64) Vali return ValidatorDistInfo{ OperatorAddr: operatorAddr, FeePoolWithdrawalHeight: currentHeight, - Pool: DecCoins{}, - PoolCommission: DecCoins{}, - DelAccum: NewTotalAccum(currentHeight), + Pool: DecCoins{}, + PoolCommission: DecCoins{}, + DelAccum: NewTotalAccum(currentHeight), } } @@ -31,6 +31,12 @@ func (vi ValidatorDistInfo) UpdateTotalDelAccum(height int64, totalDelShares sdk return vi } +// Get the calculated accum of this validator at the provided height +func (vi ValidatorDistInfo) GetAccum(height int64, vdTokens sdk.Dec) sdk.Dec { + blocks := height - vi.FeePoolWithdrawalHeight + return vdTokens.MulInt(sdk.NewInt(blocks)) +} + // Move any available accumulated fees in the FeePool to the validator's pool // - updates validator info's FeePoolWithdrawalHeight, thus setting accum to 0 // - updates fee pool to latest height and total val accum w/ given totalBonded @@ -50,9 +56,8 @@ func (vi ValidatorDistInfo) TakeFeePoolRewards(fp FeePool, height int64, totalBo } // update the validators pool - blocks := height - vi.FeePoolWithdrawalHeight + accum := vi.GetAccum(height, vdTokens) vi.FeePoolWithdrawalHeight = height - accum := vdTokens.MulInt(sdk.NewInt(blocks)) if accum.GT(fp.TotalValAccum.Accum) { panic("individual accum should never be greater than the total") From 6755ec73d2f9bbd10461011e82117a10ccf82ede Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Wed, 24 Oct 2018 19:20:52 -0400 Subject: [PATCH 2/7] refactor accum --- x/distribution/keeper/delegation.go | 73 ++++++++++++------------ x/distribution/types/delegator_info.go | 27 ++++++++- x/distribution/types/fee_pool.go | 37 ------------ x/distribution/types/fee_pool_test.go | 11 ---- x/distribution/types/total_accum.go | 40 +++++++++++++ x/distribution/types/total_accum_test.go | 19 ++++++ x/distribution/types/validator_info.go | 46 ++++++++++++++- 7 files changed, 162 insertions(+), 91 deletions(-) create mode 100644 x/distribution/types/total_accum.go create mode 100644 x/distribution/types/total_accum_test.go diff --git a/x/distribution/keeper/delegation.go b/x/distribution/keeper/delegation.go index c3a6cfa54e68..3c71e8784084 100644 --- a/x/distribution/keeper/delegation.go +++ b/x/distribution/keeper/delegation.go @@ -79,21 +79,12 @@ func (k Keeper) WithdrawDelegationReward(ctx sdk.Context, delegatorAddr sdk.AccA return types.ErrNoDelegationDistInfo(k.codespace) } - // TODO: Reconcile with duplicate code in getDelegatorRewardsAll. - height := ctx.BlockHeight() - lastTotalPower := sdk.NewDecFromInt(k.stakeKeeper.GetLastTotalPower(ctx)) - lastValPower := k.stakeKeeper.GetLastValidatorPower(ctx, valAddr) - feePool := k.GetFeePool(ctx) - delInfo := k.GetDelegationDistInfo(ctx, delegatorAddr, valAddr) - valInfo := k.GetValidatorDistInfo(ctx, valAddr) - validator := k.stakeKeeper.Validator(ctx, valAddr) - delegation := k.stakeKeeper.Delegation(ctx, delegatorAddr, valAddr) - - delInfo, valInfo, feePool, withdraw := delInfo.WithdrawRewards(feePool, valInfo, height, lastTotalPower, - lastValPower, validator.GetDelegatorShares(), delegation.GetShares(), validator.GetCommission()) + feePool, valInfo, delInfo, withdraw := + withdrawDelegatorReward(ctx, delAddr, height, lastTotalPower) k.SetValidatorDistInfo(ctx, valInfo) k.SetDelegationDistInfo(ctx, delInfo) + withdrawAddr := k.GetDelegatorWithdrawAddr(ctx, delegatorAddr) coinsToAdd, change := withdraw.TruncateDecimal() feePool.CommunityPool = feePool.CommunityPool.Plus(change) @@ -110,10 +101,26 @@ func (k Keeper) WithdrawDelegationReward(ctx sdk.Context, delegatorAddr sdk.AccA // return all rewards for all delegations of a delegator func (k Keeper) WithdrawDelegationRewardsAll(ctx sdk.Context, delegatorAddr sdk.AccAddress) { height := ctx.BlockHeight() - withdraw := k.getDelegatorRewardsAll(ctx, delegatorAddr, height) - feePool := k.GetFeePool(ctx) + + // iterate over all the delegations + // TODO: Reconcile with duplicate code in WithdrawDelegationReward. + withdraw := types.DecCoins{} + lastTotalPower := sdk.NewDecFromInt(k.stakeKeeper.GetLastTotalPower(ctx)) + operationAtDelegation := func(_ int64, del sdk.Delegation) (stop bool) { + feePool, valInfo, delInfo, diWithdraw := + withdrawDelegatorReward(ctx, delAddr, height, lastTotalPower) + + withdraw = withdraw.Plus(diWithdraw) + k.SetFeePool(ctx, feePool) + k.SetValidatorDistInfo(ctx, valInfo) + k.SetDelegationDistInfo(ctx, delInfo) + return false + } + k.stakeKeeper.IterateDelegations(ctx, delAddr, operationAtDelegation) + withdrawAddr := k.GetDelegatorWithdrawAddr(ctx, delegatorAddr) coinsToAdd, change := withdraw.TruncateDecimal() + feePool := k.GetFeePool(ctx) feePool.CommunityPool = feePool.CommunityPool.Plus(change) k.SetFeePool(ctx, feePool) _, _, err := k.bankKeeper.AddCoins(ctx, withdrawAddr, coinsToAdd) @@ -123,30 +130,20 @@ func (k Keeper) WithdrawDelegationRewardsAll(ctx sdk.Context, delegatorAddr sdk. } // return all rewards for all delegations of a delegator -func (k Keeper) getDelegatorRewardsAll(ctx sdk.Context, delAddr sdk.AccAddress, height int64) types.DecCoins { +func (k Keeper) withdrawDelegatorReward(ctx sdk.Context, delAddr sdk.AccAddress, + height int64, lastTotalPower sdk.Dec) ( + feePool, ValidatorDistInfo, DelegatorDistInfo, types.DecCoins) { - withdraw := types.DecCoins{} - lastTotalPower := sdk.NewDecFromInt(k.stakeKeeper.GetLastTotalPower(ctx)) + feePool := k.GetFeePool(ctx) + valAddr := del.GetValidatorAddr() + lastValPower := k.stakeKeeper.GetLastValidatorPower(ctx, valAddr) + delInfo := k.GetDelegationDistInfo(ctx, delAddr, valAddr) + valInfo := k.GetValidatorDistInfo(ctx, valAddr) + validator := k.stakeKeeper.Validator(ctx, valAddr) + delegation := k.stakeKeeper.Delegation(ctx, delAddr, valAddr) - // iterate over all the delegations - // TODO: Reconcile with duplicate code in WithdrawDelegationReward. - operationAtDelegation := func(_ int64, del sdk.Delegation) (stop bool) { - feePool := k.GetFeePool(ctx) - valAddr := del.GetValidatorAddr() - lastValPower := k.stakeKeeper.GetLastValidatorPower(ctx, valAddr) - delInfo := k.GetDelegationDistInfo(ctx, delAddr, valAddr) - valInfo := k.GetValidatorDistInfo(ctx, valAddr) - validator := k.stakeKeeper.Validator(ctx, valAddr) - delegation := k.stakeKeeper.Delegation(ctx, delAddr, valAddr) - - delInfo, valInfo, feePool, diWithdraw := delInfo.WithdrawRewards(feePool, valInfo, height, lastTotalPower, - lastValPower, validator.GetDelegatorShares(), delegation.GetShares(), validator.GetCommission()) - withdraw = withdraw.Plus(diWithdraw) - k.SetFeePool(ctx, feePool) - k.SetValidatorDistInfo(ctx, valInfo) - k.SetDelegationDistInfo(ctx, delInfo) - return false - } - k.stakeKeeper.IterateDelegations(ctx, delAddr, operationAtDelegation) - return withdraw + delInfo, valInfo, feePool, withdraw := delInfo.WithdrawRewards(feePool, valInfo, height, lastTotalPower, + lastValPower, validator.GetDelegatorShares(), delegation.GetShares(), validator.GetCommission()) + + return feePool, valInfo, delInfo, withdraw } diff --git a/x/distribution/types/delegator_info.go b/x/distribution/types/delegator_info.go index 7c2eaef76221..14e1a788c8f6 100644 --- a/x/distribution/types/delegator_info.go +++ b/x/distribution/types/delegator_info.go @@ -21,6 +21,12 @@ func NewDelegationDistInfo(delegatorAddr sdk.AccAddress, valOperatorAddr sdk.Val } } +// Get the calculated accum of this delegator at the provided height +func (di DelegationDistInfo) GetDelAccum(height int64, delegatorShares sdk.Dec) sdk.Dec { + blocks := height - di.WithdrawalHeight + return delegatorShares.MulInt(sdk.NewInt(blocks)) +} + // Withdraw rewards from delegator. // Among many things, it does: // * updates validator info's total del accum @@ -40,9 +46,8 @@ func (di DelegationDistInfo) WithdrawRewards(fp FeePool, vi ValidatorDistInfo, vi, fp = vi.TakeFeePoolRewards(fp, height, totalBonded, vdTokens, commissionRate) - blocks := height - di.WithdrawalHeight + accum := di.GetDelAccum(height, delegatorShares) di.WithdrawalHeight = height - accum := delegatorShares.MulInt(sdk.NewInt(blocks)) withdrawalTokens := vi.Pool.MulDec(accum).QuoDec(vi.DelAccum.Accum) remainingTokens := vi.Pool.Minus(withdrawalTokens) @@ -51,3 +56,21 @@ func (di DelegationDistInfo) WithdrawRewards(fp FeePool, vi ValidatorDistInfo, return di, vi, fp, withdrawalTokens } + +// Estimate the delegators rewards at this current state, +// the estimated rewards are subject to fluctuation +func (di DelegationDistInfo) EstimateRewards(fp FeePool, vi ValidatorDistInfo, + height int64, totalBonded, vdTokens, totalDelShares, delegatorShares, + commissionRate sdk.Dec) DecCoins { + + totalDelAccum = GetTotalDelAccum(height, totalDelShares) + + if vi.DelAccum.Accum.IsZero() { + return DecCoins{} + } + + rewards = vi.EstimatePoolRewards(fp, height, totalBonded, vdTokens, commissionRate) + accum := di.GetDelAccum(height, delegatorShares) + estimatedTokens := rewards.MulDec(accum).QuoDec(totalDelAccum) + return estimatedTokens +} diff --git a/x/distribution/types/fee_pool.go b/x/distribution/types/fee_pool.go index def6c6e75f2f..1f109b6278d1 100644 --- a/x/distribution/types/fee_pool.go +++ b/x/distribution/types/fee_pool.go @@ -4,43 +4,6 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) -// total accumulation tracker -type TotalAccum struct { - UpdateHeight int64 `json:"update_height"` - Accum sdk.Dec `json:"accum"` -} - -func NewTotalAccum(height int64) TotalAccum { - return TotalAccum{ - UpdateHeight: height, - Accum: sdk.ZeroDec(), - } -} - -// update total accumulation factor for the new height -// CONTRACT: height should be greater than the old height -func (ta TotalAccum) UpdateForNewHeight(height int64, accumCreatedPerBlock sdk.Dec) TotalAccum { - blocks := height - ta.UpdateHeight - if blocks < 0 { - panic("reverse updated for new height") - } - ta.Accum = ta.Accum.Add(accumCreatedPerBlock.MulInt(sdk.NewInt(blocks))) - ta.UpdateHeight = height - return ta -} - -// get total accumulation factor for the given height -// CONTRACT: height should be greater than the old height -func (ta TotalAccum) GetAccum(height int64, accumCreatedPerBlock sdk.Dec) sdk.Dec { - blocks := height - ta.UpdateHeight - if blocks < 0 { - panic("reverse updated for new height") - } - return ta.Accum.Add(accumCreatedPerBlock.MulInt(sdk.NewInt(blocks))) -} - -//___________________________________________________________________________________________ - // global fee pool for distribution type FeePool struct { TotalValAccum TotalAccum `json:"val_accum"` // total valdator accum held by validators diff --git a/x/distribution/types/fee_pool_test.go b/x/distribution/types/fee_pool_test.go index 478ec7539baa..73bda52fabf0 100644 --- a/x/distribution/types/fee_pool_test.go +++ b/x/distribution/types/fee_pool_test.go @@ -7,17 +7,6 @@ import ( "github.com/stretchr/testify/require" ) -func TestTotalAccumUpdateForNewHeight(t *testing.T) { - - ta := NewTotalAccum(0) - - ta = ta.UpdateForNewHeight(5, sdk.NewDec(3)) - require.True(sdk.DecEq(t, sdk.NewDec(15), ta.Accum)) - - ta = ta.UpdateForNewHeight(8, sdk.NewDec(2)) - require.True(sdk.DecEq(t, sdk.NewDec(21), ta.Accum)) -} - func TestUpdateTotalValAccum(t *testing.T) { fp := InitialFeePool() diff --git a/x/distribution/types/total_accum.go b/x/distribution/types/total_accum.go new file mode 100644 index 000000000000..0915847a5ef6 --- /dev/null +++ b/x/distribution/types/total_accum.go @@ -0,0 +1,40 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// total accumulation tracker +type TotalAccum struct { + UpdateHeight int64 `json:"update_height"` + Accum sdk.Dec `json:"accum"` +} + +func NewTotalAccum(height int64) TotalAccum { + return TotalAccum{ + UpdateHeight: height, + Accum: sdk.ZeroDec(), + } +} + +// update total accumulation factor for the new height +// CONTRACT: height should be greater than the old height +func (ta TotalAccum) UpdateForNewHeight(height int64, accumCreatedPerBlock sdk.Dec) TotalAccum { + blocks := height - ta.UpdateHeight + if blocks < 0 { + panic("reverse updated for new height") + } + ta.Accum = ta.Accum.Add(accumCreatedPerBlock.MulInt(sdk.NewInt(blocks))) + ta.UpdateHeight = height + return ta +} + +// get total accumulation factor for the given height +// CONTRACT: height should be greater than the old height +func (ta TotalAccum) GetAccum(height int64, accumCreatedPerBlock sdk.Dec) sdk.Dec { + blocks := height - ta.UpdateHeight + if blocks < 0 { + panic("reverse updated for new height") + } + return ta.Accum.Add(accumCreatedPerBlock.MulInt(sdk.NewInt(blocks))) +} diff --git a/x/distribution/types/total_accum_test.go b/x/distribution/types/total_accum_test.go new file mode 100644 index 000000000000..81f80a154a0e --- /dev/null +++ b/x/distribution/types/total_accum_test.go @@ -0,0 +1,19 @@ +package types + +import ( + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/require" +) + +func TestTotalAccumUpdateForNewHeight(t *testing.T) { + + ta := NewTotalAccum(0) + + ta = ta.UpdateForNewHeight(5, sdk.NewDec(3)) + require.True(sdk.DecEq(t, sdk.NewDec(15), ta.Accum)) + + ta = ta.UpdateForNewHeight(8, sdk.NewDec(2)) + require.True(sdk.DecEq(t, sdk.NewDec(21), ta.Accum)) +} diff --git a/x/distribution/types/validator_info.go b/x/distribution/types/validator_info.go index 3caea5d8469b..b45238bee4f7 100644 --- a/x/distribution/types/validator_info.go +++ b/x/distribution/types/validator_info.go @@ -31,8 +31,13 @@ func (vi ValidatorDistInfo) UpdateTotalDelAccum(height int64, totalDelShares sdk return vi } -// Get the calculated accum of this validator at the provided height -func (vi ValidatorDistInfo) GetAccum(height int64, vdTokens sdk.Dec) sdk.Dec { +// Get the total delegator accum within this validator at the provided height +func (vi ValidatorDistInfo) GetTotalDelAccum(height int64, totalDelShares sdk.Dec) sdk.Dec { + return vi.DelAccum.GetAccum(height, totalDelShares) +} + +// Get the validator accum at the provided height +func (vi ValidatorDistInfo) GetValAccum(height int64, vdTokens sdk.Dec) sdk.Dec { blocks := height - vi.FeePoolWithdrawalHeight return vdTokens.MulInt(sdk.NewInt(blocks)) } @@ -56,7 +61,7 @@ func (vi ValidatorDistInfo) TakeFeePoolRewards(fp FeePool, height int64, totalBo } // update the validators pool - accum := vi.GetAccum(height, vdTokens) + accum := vi.GetValAccum(height, vdTokens) vi.FeePoolWithdrawalHeight = height if accum.GT(fp.TotalValAccum.Accum) { @@ -87,3 +92,38 @@ func (vi ValidatorDistInfo) WithdrawCommission(fp FeePool, height int64, return vi, fp, withdrawalTokens } + +// Estimate the validator's pool rewards at this current state, +// the estimated rewards are subject to fluctuation +func (vi ValidatorDistInfo) EstimatePoolRewards(fp FeePool, height int64, + totalBonded, vdTokens, commissionRate sdk.Dec) sdk.Coins { + + totalValAccum = fp.GetTotalValAccum(height, totalBonded) + valAccum := vi.GetValAccum(height, vdTokens) + + if accum.GT(fp.TotalValAccum.Accum) { + panic("individual accum should never be greater than the total") + } + withdrawalTokens := fp.Pool.MulDec(valAccum).QuoDec(totalValAccum) + commission := withdrawalTokens.MulDec(commissionRate) + afterCommission := withdrawalTokens.Minus(commission) + pool := vi.Pool.Plus(afterCommission) + return pool +} + +// Estimate the validator's commission pool rewards at this current state, +// the estimated rewards are subject to fluctuation +func (vi ValidatorDistInfo) EstimateCommissionRewards(fp FeePool, height int64, + totalBonded, vdTokens, commissionRate sdk.Dec) sdk.Coins { + + totalValAccum = fp.GetTotalValAccum(height, totalBonded) + valAccum := vi.GetValAccum(height, vdTokens) + + if accum.GT(fp.TotalValAccum.Accum) { + panic("individual accum should never be greater than the total") + } + withdrawalTokens := fp.Pool.MulDec(valAccum).QuoDec(totalValAccum) + commission := withdrawalTokens.MulDec(commissionRate) + commissionPool := vi.PoolCommission.Plus(commission) + return commissionPool +} From 25cbbd503c771d7fe9f0a85d098774cf401f4f12 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 25 Oct 2018 04:51:37 -0400 Subject: [PATCH 3/7] refactoring working --- x/distribution/keeper/delegation.go | 87 ++++++++++++++++++++------ x/distribution/keeper/keeper.go | 17 +++++ x/distribution/keeper/test_common.go | 21 +++++++ x/distribution/keeper/validator.go | 43 ++++++------- x/distribution/types/delegator_info.go | 28 ++++----- x/distribution/types/validator_info.go | 63 +++++++++++++------ 6 files changed, 185 insertions(+), 74 deletions(-) diff --git a/x/distribution/keeper/delegation.go b/x/distribution/keeper/delegation.go index 3c71e8784084..fb37dd06f1a5 100644 --- a/x/distribution/keeper/delegation.go +++ b/x/distribution/keeper/delegation.go @@ -69,7 +69,44 @@ func (k Keeper) RemoveDelegatorWithdrawAddr(ctx sdk.Context, delAddr, withdrawAd //___________________________________________________________________________________________ -// Withdraw all the rewards for a single delegation +// return all rewards for a delegation +func (k Keeper) withdrawDelegationReward(ctx sdk.Context, delAddr sdk.AccAddress, + height int64, lastTotalPower sdk.Dec) ( + feePool, ValidatorDistInfo, DelegatorDistInfo, types.DecCoins) { + + valAddr := del.GetValidatorAddr() + wc := k.GetWithdrawContext(ctx, valAddr) + + delInfo := k.GetDelegationDistInfo(ctx, delAddr, valAddr) + validator := k.stakeKeeper.Validator(ctx, valAddr) + delegation := k.stakeKeeper.Delegation(ctx, delAddr, valAddr) + + delInfo, valInfo, feePool, withdraw := delInfo.WithdrawRewards(wc, + validator.GetDelegatorShares(), delegation.GetShares()) + + return feePool, valInfo, delInfo, withdraw +} + +// estimate all rewards for all delegations of a delegator +func (k Keeper) estimateDelegationReward(ctx sdk.Context, delAddr sdk.AccAddress, + height int64, lastTotalPower sdk.Dec) types.DecCoins { + + valAddr := del.GetValidatorAddr() + wc := k.GetWithdrawContext(ctx, valAddr) + + delInfo := k.GetDelegationDistInfo(ctx, delAddr, valAddr) + validator := k.stakeKeeper.Validator(ctx, valAddr) + delegation := k.stakeKeeper.Delegation(ctx, delAddr, valAddr) + + estimation := delInfo.EstimateRewards(wc, + validator.GetDelegatorShares(), delegation.GetShares()) + + return estimation +} + +//___________________________________________________________________________________________ + +// withdraw all rewards for a single delegation // NOTE: This gets called "onDelegationSharesModified", // meaning any changes to bonded coins func (k Keeper) WithdrawDelegationReward(ctx sdk.Context, delegatorAddr sdk.AccAddress, @@ -80,7 +117,7 @@ func (k Keeper) WithdrawDelegationReward(ctx sdk.Context, delegatorAddr sdk.AccA } feePool, valInfo, delInfo, withdraw := - withdrawDelegatorReward(ctx, delAddr, height, lastTotalPower) + withdrawDelegationReward(ctx, delAddr, height, lastTotalPower) k.SetValidatorDistInfo(ctx, valInfo) k.SetDelegationDistInfo(ctx, delInfo) @@ -96,6 +133,17 @@ func (k Keeper) WithdrawDelegationReward(ctx sdk.Context, delegatorAddr sdk.AccA return nil } +// estimate rewards for a single delegation +func (k Keeper) EstimateDelegationReward(ctx sdk.Context, delegatorAddr sdk.AccAddress, + valAddr sdk.ValAddress) (sdk.Coins, sdk.Error) { + + if !k.HasDelegationDistInfo(ctx, delegatorAddr, valAddr) { + return types.ErrNoDelegationDistInfo(k.codespace) + } + estCoins := estimateDelegationReward(ctx, delAddr, height, lastTotalPower) + return estCoins.TruncateDecimal() +} + //___________________________________________________________________________________________ // return all rewards for all delegations of a delegator @@ -103,13 +151,12 @@ func (k Keeper) WithdrawDelegationRewardsAll(ctx sdk.Context, delegatorAddr sdk. height := ctx.BlockHeight() // iterate over all the delegations - // TODO: Reconcile with duplicate code in WithdrawDelegationReward. withdraw := types.DecCoins{} lastTotalPower := sdk.NewDecFromInt(k.stakeKeeper.GetLastTotalPower(ctx)) operationAtDelegation := func(_ int64, del sdk.Delegation) (stop bool) { - feePool, valInfo, delInfo, diWithdraw := - withdrawDelegatorReward(ctx, delAddr, height, lastTotalPower) + feePool, valInfo, delInfo, diWithdraw := + withdrawDelegationReward(ctx, delAddr, height, lastTotalPower) withdraw = withdraw.Plus(diWithdraw) k.SetFeePool(ctx, feePool) k.SetValidatorDistInfo(ctx, valInfo) @@ -129,21 +176,21 @@ func (k Keeper) WithdrawDelegationRewardsAll(ctx sdk.Context, delegatorAddr sdk. } } -// return all rewards for all delegations of a delegator -func (k Keeper) withdrawDelegatorReward(ctx sdk.Context, delAddr sdk.AccAddress, - height int64, lastTotalPower sdk.Dec) ( - feePool, ValidatorDistInfo, DelegatorDistInfo, types.DecCoins) { +// estimate all rewards for all delegations of a delegator +func (k Keeper) EstimateDelegationRewardsAll(ctx sdk.Context, + delegatorAddr sdk.AccAddress) sdk.Coins { - feePool := k.GetFeePool(ctx) - valAddr := del.GetValidatorAddr() - lastValPower := k.stakeKeeper.GetLastValidatorPower(ctx, valAddr) - delInfo := k.GetDelegationDistInfo(ctx, delAddr, valAddr) - valInfo := k.GetValidatorDistInfo(ctx, valAddr) - validator := k.stakeKeeper.Validator(ctx, valAddr) - delegation := k.stakeKeeper.Delegation(ctx, delAddr, valAddr) - - delInfo, valInfo, feePool, withdraw := delInfo.WithdrawRewards(feePool, valInfo, height, lastTotalPower, - lastValPower, validator.GetDelegatorShares(), delegation.GetShares(), validator.GetCommission()) + height := ctx.BlockHeight() - return feePool, valInfo, delInfo, withdraw + // iterate over all the delegations + total := types.DecCoins{} + lastTotalPower := sdk.NewDecFromInt(k.stakeKeeper.GetLastTotalPower(ctx)) + operationAtDelegation := func(_ int64, del sdk.Delegation) (stop bool) { + est := estimateDelegationReward(ctx, delAddr, height, lastTotalPower) + total = total.Plus(est) + return false + } + k.stakeKeeper.IterateDelegations(ctx, delAddr, operationAtDelegation) + estCoins, _ := total.TruncateDecimal() + return estCoins } diff --git a/x/distribution/keeper/keeper.go b/x/distribution/keeper/keeper.go index 3919a18aedf7..4480b9e70f8b 100644 --- a/x/distribution/keeper/keeper.go +++ b/x/distribution/keeper/keeper.go @@ -88,6 +88,23 @@ func (k Keeper) SetPreviousProposerConsAddr(ctx sdk.Context, consAddr sdk.ConsAd store.Set(ProposerKey, b) } +//______________________________________________________________________ + +// get context required for withdraw operations +func (k Keeper) GetWithdrawContext(ctx sdk.Context, + operatorAddr sdk.ValAddr) types.WithdrawContext { + + feePool := k.GetFeePool(ctx) + height := ctx.BlockHeight() + validator := k.stakeKeeper.Validator(ctx, operatorAddr) + lastValPower := k.stakeKeeper.GetLastValidatorPower(ctx, operatorAddr) + lastTotalPower := sdk.NewDecFromInt(k.stakeKeeper.GetLastTotalPower(ctx)) + + return types.NewWithdrawContext( + feePool, height, lastTotalPower, lastValPower, + validator.GetCommission()) +} + //______________________________________________________________________ // PARAM STORE diff --git a/x/distribution/keeper/test_common.go b/x/distribution/keeper/test_common.go index 58658ab8e0ac..b1bd4ae4e05c 100644 --- a/x/distribution/keeper/test_common.go +++ b/x/distribution/keeper/test_common.go @@ -158,3 +158,24 @@ func (fck DummyFeeCollectionKeeper) SetCollectedFees(in sdk.Coins) { func (fck DummyFeeCollectionKeeper) ClearCollectedFees(_ sdk.Context) { heldFees = sdk.Coins{} } + +//__________________________________________________________________________________ +// used in simulation + +// iterate over all the validator distribution infos (inefficient, just used to check invariants) +func (k Keeper) IterateValidatorDistInfos(ctx sdk.Context, + fn func(index int64, distInfo types.ValidatorDistInfo) (stop bool)) { + + store := ctx.KVStore(k.storeKey) + iter := sdk.KVStorePrefixIterator(store, ValidatorDistInfoKey) + defer iter.Close() + index := int64(0) + for ; iter.Valid(); iter.Next() { + var vdi types.ValidatorDistInfo + k.cdc.MustUnmarshalBinary(iter.Value(), &vdi) + if fn(index, vdi) { + return + } + index++ + } +} diff --git a/x/distribution/keeper/validator.go b/x/distribution/keeper/validator.go index db3d1a9f0e36..4e40cf117ee6 100644 --- a/x/distribution/keeper/validator.go +++ b/x/distribution/keeper/validator.go @@ -62,20 +62,15 @@ func (k Keeper) WithdrawValidatorRewardsAll(ctx sdk.Context, operatorAddr sdk.Va if !k.HasValidatorDistInfo(ctx, operatorAddr) { return types.ErrNoValidatorDistInfo(k.codespace) } + wc := k.GetWithdrawContext(ctx, operatorAddr) // withdraw self-delegation - height := ctx.BlockHeight() - validator := k.stakeKeeper.Validator(ctx, operatorAddr) - lastValPower := k.stakeKeeper.GetLastValidatorPower(ctx, operatorAddr) accAddr := sdk.AccAddress(operatorAddr.Bytes()) - withdraw := k.getDelegatorRewardsAll(ctx, accAddr, height) + withdraw := k.withdrawDelegatorRewardsAll(ctx, accAddr, wc.Height) // withdrawal validator commission rewards - lastTotalPower := sdk.NewDecFromInt(k.stakeKeeper.GetLastTotalPower(ctx)) valInfo := k.GetValidatorDistInfo(ctx, operatorAddr) - feePool := k.GetFeePool(ctx) - valInfo, feePool, commission := valInfo.WithdrawCommission(feePool, height, lastTotalPower, - lastValPower, validator.GetCommission()) + valInfo, feePool, commission := valInfo.WithdrawCommission(wb) withdraw = withdraw.Plus(commission) k.SetValidatorDistInfo(ctx, valInfo) @@ -91,18 +86,24 @@ func (k Keeper) WithdrawValidatorRewardsAll(ctx sdk.Context, operatorAddr sdk.Va return nil } -// iterate over all the validator distribution infos (inefficient, just used to check invariants) -func (k Keeper) IterateValidatorDistInfos(ctx sdk.Context, fn func(index int64, distInfo types.ValidatorDistInfo) (stop bool)) { - store := ctx.KVStore(k.storeKey) - iter := sdk.KVStorePrefixIterator(store, ValidatorDistInfoKey) - defer iter.Close() - index := int64(0) - for ; iter.Valid(); iter.Next() { - var vdi types.ValidatorDistInfo - k.cdc.MustUnmarshalBinary(iter.Value(), &vdi) - if fn(index, vdi) { - return - } - index++ +// estimate all the validator rewards including the commission +// note: all estimations are subject to flucuation +func (k Keeper) EstimateValidatorRewardsAll(ctx sdk.Context, operatorAddr sdk.ValAddress) sdk.Error { + + if !k.HasValidatorDistInfo(ctx, operatorAddr) { + return types.ErrNoValidatorDistInfo(k.codespace) } + wc := k.GetWithdrawContext(ctx, operatorAddr) + + // withdraw self-delegation + accAddr := sdk.AccAddress(operatorAddr.Bytes()) + withdraw := k.EstimateDelegatorRewardsAll(ctx, accAddr, wc.Height) + + // withdrawal validator commission rewards + valInfo := k.GetValidatorDistInfo(ctx, operatorAddr) + + commission := valInfo.EstimateCommission(wc) + withdraw = withdraw.Plus(commission) + truncated, _ := withdraw.TruncateDecimal() + return truncated } diff --git a/x/distribution/types/delegator_info.go b/x/distribution/types/delegator_info.go index 14e1a788c8f6..615e611a7371 100644 --- a/x/distribution/types/delegator_info.go +++ b/x/distribution/types/delegator_info.go @@ -34,20 +34,21 @@ func (di DelegationDistInfo) GetDelAccum(height int64, delegatorShares sdk.Dec) // * updates validator info's FeePoolWithdrawalHeight, thus setting accum to 0 // * updates fee pool to latest height and total val accum w/ given totalBonded // (see comment on TakeFeePoolRewards for more info) -func (di DelegationDistInfo) WithdrawRewards(fp FeePool, vi ValidatorDistInfo, - height int64, totalBonded, vdTokens, totalDelShares, delegatorShares, - commissionRate sdk.Dec) (DelegationDistInfo, ValidatorDistInfo, FeePool, DecCoins) { +func (di DelegationDistInfo) WithdrawRewards(wc WithdrawContext, vi ValidatorDistInfo, + totalDelShares, delegatorShares sdk.Dec) ( + DelegationDistInfo, ValidatorDistInfo, FeePool, DecCoins) { - vi = vi.UpdateTotalDelAccum(height, totalDelShares) + fp := wc.FeePool + vi = vi.UpdateTotalDelAccum(wc.Height, totalDelShares) if vi.DelAccum.Accum.IsZero() { return di, vi, fp, DecCoins{} } - vi, fp = vi.TakeFeePoolRewards(fp, height, totalBonded, vdTokens, commissionRate) + vi, fp = vi.TakeFeePoolRewards(wc) - accum := di.GetDelAccum(height, delegatorShares) - di.WithdrawalHeight = height + accum := di.GetDelAccum(wc.Height, delegatorShares) + di.WithdrawalHeight = wc.Height withdrawalTokens := vi.Pool.MulDec(accum).QuoDec(vi.DelAccum.Accum) remainingTokens := vi.Pool.Minus(withdrawalTokens) @@ -58,19 +59,18 @@ func (di DelegationDistInfo) WithdrawRewards(fp FeePool, vi ValidatorDistInfo, } // Estimate the delegators rewards at this current state, -// the estimated rewards are subject to fluctuation -func (di DelegationDistInfo) EstimateRewards(fp FeePool, vi ValidatorDistInfo, - height int64, totalBonded, vdTokens, totalDelShares, delegatorShares, - commissionRate sdk.Dec) DecCoins { +// note: all estimations are subject to flucuation +func (di DelegationDistInfo) EstimateRewards(wc WithdrawContext, vi ValidatorDistInfo, + totalDelShares, delegatorShares sdk.Dec) DecCoins { - totalDelAccum = GetTotalDelAccum(height, totalDelShares) + totalDelAccum := vi.GetTotalDelAccum(wc.Height, totalDelShares) if vi.DelAccum.Accum.IsZero() { return DecCoins{} } - rewards = vi.EstimatePoolRewards(fp, height, totalBonded, vdTokens, commissionRate) - accum := di.GetDelAccum(height, delegatorShares) + rewards := vi.EstimatePoolRewards(wc) + accum := di.GetDelAccum(wc.Height, delegatorShares) estimatedTokens := rewards.MulDec(accum).QuoDec(totalDelAccum) return estimatedTokens } diff --git a/x/distribution/types/validator_info.go b/x/distribution/types/validator_info.go index b45238bee4f7..166fda6f20f9 100644 --- a/x/distribution/types/validator_info.go +++ b/x/distribution/types/validator_info.go @@ -4,6 +4,29 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" ) +// common parameters used in withdraws from validators +type WithdrawContext struct { + FeePool FeePool + Height int64 // block height + TotalBonded sdk.Dec // total bonded tokens in the network + ValTokens sdk.Dec // validator's bonded tokens + CommissionRate sdk.Dec // validator commission rate +} + +func NewWithdrawContext(feePool FeePool, height int64, totalBonded, + valTokens, commissionRate sdk.Dec) WithdrawContext { + + return WithdrawContext{ + fp: feePool, + height: height, + totalBonded: totalBonded, + vdTokens: vdTokens, + commissionRate: commissionRate, + } +} + +//_____________________________________________________________________________ + // distribution info for a particular validator type ValidatorDistInfo struct { OperatorAddr sdk.ValAddress `json:"operator_addr"` @@ -51,10 +74,10 @@ func (vi ValidatorDistInfo) GetValAccum(height int64, vdTokens sdk.Dec) sdk.Dec // - called in DelegationDistInfo.WithdrawRewards // NOTE: When a delegator unbonds, say, onDelegationSharesModified -> // WithdrawDelegationReward -> WithdrawRewards -func (vi ValidatorDistInfo) TakeFeePoolRewards(fp FeePool, height int64, totalBonded, vdTokens, - commissionRate sdk.Dec) (ValidatorDistInfo, FeePool) { +func (vi ValidatorDistInfo) TakeFeePoolRewards(wc WithdrawContext) ( + ValidatorDistInfo, FeePool) { - fp = fp.UpdateTotalValAccum(height, totalBonded) + fp := wc.FeePool.UpdateTotalValAccum(height, totalBonded) if fp.TotalValAccum.Accum.IsZero() { return vi, fp @@ -64,13 +87,13 @@ func (vi ValidatorDistInfo) TakeFeePoolRewards(fp FeePool, height int64, totalBo accum := vi.GetValAccum(height, vdTokens) vi.FeePoolWithdrawalHeight = height - if accum.GT(fp.TotalValAccum.Accum) { + if accum.GT(wc.fp.TotalValAccum.Accum) { panic("individual accum should never be greater than the total") } withdrawalTokens := fp.Pool.MulDec(accum).QuoDec(fp.TotalValAccum.Accum) remainingTokens := fp.Pool.Minus(withdrawalTokens) - commission := withdrawalTokens.MulDec(commissionRate) + commission := withdrawalTokens.MulDec(wc.CommissionRate) afterCommission := withdrawalTokens.Minus(commission) fp.TotalValAccum.Accum = fp.TotalValAccum.Accum.Sub(accum) @@ -82,10 +105,10 @@ func (vi ValidatorDistInfo) TakeFeePoolRewards(fp FeePool, height int64, totalBo } // withdraw commission rewards -func (vi ValidatorDistInfo) WithdrawCommission(fp FeePool, height int64, - totalBonded, vdTokens, commissionRate sdk.Dec) (vio ValidatorDistInfo, fpo FeePool, withdrawn DecCoins) { +func (vi ValidatorDistInfo) WithdrawCommission(wc WithdrawContext) ( + vio ValidatorDistInfo, fpo FeePool, withdrawn DecCoins) { - vi, fp = vi.TakeFeePoolRewards(fp, height, totalBonded, vdTokens, commissionRate) + vi, fp = vi.TakeFeePoolRewards(wc) withdrawalTokens := vi.PoolCommission vi.PoolCommission = DecCoins{} // zero @@ -95,17 +118,18 @@ func (vi ValidatorDistInfo) WithdrawCommission(fp FeePool, height int64, // Estimate the validator's pool rewards at this current state, // the estimated rewards are subject to fluctuation -func (vi ValidatorDistInfo) EstimatePoolRewards(fp FeePool, height int64, - totalBonded, vdTokens, commissionRate sdk.Dec) sdk.Coins { +func (vi ValidatorDistInfo) EstimatePoolRewards( + wc WithdrawContext) sdk.Coins { - totalValAccum = fp.GetTotalValAccum(height, totalBonded) - valAccum := vi.GetValAccum(height, vdTokens) + fp := wc.FeePool + totalValAccum = fp.GetTotalValAccum(wc.height, wc.TotalBonded) + valAccum := vi.GetValAccum(wc.height, wc.ValTokens) if accum.GT(fp.TotalValAccum.Accum) { panic("individual accum should never be greater than the total") } withdrawalTokens := fp.Pool.MulDec(valAccum).QuoDec(totalValAccum) - commission := withdrawalTokens.MulDec(commissionRate) + commission := withdrawalTokens.MulDec(wc.CommissionRate) afterCommission := withdrawalTokens.Minus(commission) pool := vi.Pool.Plus(afterCommission) return pool @@ -113,17 +137,18 @@ func (vi ValidatorDistInfo) EstimatePoolRewards(fp FeePool, height int64, // Estimate the validator's commission pool rewards at this current state, // the estimated rewards are subject to fluctuation -func (vi ValidatorDistInfo) EstimateCommissionRewards(fp FeePool, height int64, - totalBonded, vdTokens, commissionRate sdk.Dec) sdk.Coins { +func (vi ValidatorDistInfo) EstimateCommissionRewards( + wc WithdrawContext) sdk.Coins { - totalValAccum = fp.GetTotalValAccum(height, totalBonded) - valAccum := vi.GetValAccum(height, vdTokens) + fp := wc.FeePool + totalValAccum = fp.GetTotalValAccum(wc.Height, wc.TotalBonded) + valAccum := vi.GetValAccum(wc.Height, wc.ValTokens) if accum.GT(fp.TotalValAccum.Accum) { panic("individual accum should never be greater than the total") } - withdrawalTokens := fp.Pool.MulDec(valAccum).QuoDec(totalValAccum) - commission := withdrawalTokens.MulDec(commissionRate) + withdrawalTokens := fp.Pool.MulDec(valAccum).QuoDec(wc.TotalValAccum) + commission := withdrawalTokens.MulDec(wc.CommissionRate) commissionPool := vi.PoolCommission.Plus(commission) return commissionPool } From d93a44f90abb2eefe9b2566253432b3758ac5f62 Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 25 Oct 2018 13:07:30 -0400 Subject: [PATCH 4/7] refactor WIP --- x/distribution/keeper/delegation.go | 91 ++++++++++++++------------ x/distribution/keeper/keeper.go | 6 +- x/distribution/keeper/validator.go | 16 ++--- x/distribution/types/validator_info.go | 46 ++++++------- 4 files changed, 80 insertions(+), 79 deletions(-) diff --git a/x/distribution/keeper/delegation.go b/x/distribution/keeper/delegation.go index fb37dd06f1a5..1ba82c9c6c65 100644 --- a/x/distribution/keeper/delegation.go +++ b/x/distribution/keeper/delegation.go @@ -70,18 +70,18 @@ func (k Keeper) RemoveDelegatorWithdrawAddr(ctx sdk.Context, delAddr, withdrawAd //___________________________________________________________________________________________ // return all rewards for a delegation -func (k Keeper) withdrawDelegationReward(ctx sdk.Context, delAddr sdk.AccAddress, - height int64, lastTotalPower sdk.Dec) ( - feePool, ValidatorDistInfo, DelegatorDistInfo, types.DecCoins) { +func (k Keeper) withdrawDelegationReward(ctx sdk.Context, + delAddr sdk.AccAddress, valAddr sdk.ValAddress) (types.FeePool, + types.ValidatorDistInfo, types.DelegationDistInfo, types.DecCoins) { - valAddr := del.GetValidatorAddr() wc := k.GetWithdrawContext(ctx, valAddr) + valInfo := k.GetValidatorDistInfo(ctx, valAddr) delInfo := k.GetDelegationDistInfo(ctx, delAddr, valAddr) validator := k.stakeKeeper.Validator(ctx, valAddr) delegation := k.stakeKeeper.Delegation(ctx, delAddr, valAddr) - delInfo, valInfo, feePool, withdraw := delInfo.WithdrawRewards(wc, + delInfo, valInfo, feePool, withdraw := delInfo.WithdrawRewards(wc, valInfo, validator.GetDelegatorShares(), delegation.GetShares()) return feePool, valInfo, delInfo, withdraw @@ -89,16 +89,16 @@ func (k Keeper) withdrawDelegationReward(ctx sdk.Context, delAddr sdk.AccAddress // estimate all rewards for all delegations of a delegator func (k Keeper) estimateDelegationReward(ctx sdk.Context, delAddr sdk.AccAddress, - height int64, lastTotalPower sdk.Dec) types.DecCoins { + valAddr sdk.ValAddress) types.DecCoins { - valAddr := del.GetValidatorAddr() wc := k.GetWithdrawContext(ctx, valAddr) + valInfo := k.GetValidatorDistInfo(ctx, valAddr) delInfo := k.GetDelegationDistInfo(ctx, delAddr, valAddr) validator := k.stakeKeeper.Validator(ctx, valAddr) delegation := k.stakeKeeper.Delegation(ctx, delAddr, valAddr) - estimation := delInfo.EstimateRewards(wc, + estimation := delInfo.EstimateRewards(wc, valInfo, validator.GetDelegatorShares(), delegation.GetShares()) return estimation @@ -109,54 +109,71 @@ func (k Keeper) estimateDelegationReward(ctx sdk.Context, delAddr sdk.AccAddress // withdraw all rewards for a single delegation // NOTE: This gets called "onDelegationSharesModified", // meaning any changes to bonded coins -func (k Keeper) WithdrawDelegationReward(ctx sdk.Context, delegatorAddr sdk.AccAddress, +func (k Keeper) CompleteWithdrawal(ctx sdk.Context, feePool types.FeePool, + delAddr sdk.AccAddress, amount types.DecCoins) { + + withdrawAddr := k.GetDelegatorWithdrawAddr(ctx, delAddr) + coinsToAdd, change := amount.TruncateDecimal() + feePool.CommunityPool = feePool.CommunityPool.Plus(change) + k.SetFeePool(ctx, feePool) + _, _, err := k.bankKeeper.AddCoins(ctx, withdrawAddr, coinsToAdd) + if err != nil { + panic(err) + } +} + +//___________________________________________________________________________________________ + +// withdraw all rewards for a single delegation +// NOTE: This gets called "onDelegationSharesModified", +// meaning any changes to bonded coins +func (k Keeper) WithdrawDelegationReward(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) sdk.Error { - if !k.HasDelegationDistInfo(ctx, delegatorAddr, valAddr) { + if !k.HasDelegationDistInfo(ctx, delAddr, valAddr) { return types.ErrNoDelegationDistInfo(k.codespace) } feePool, valInfo, delInfo, withdraw := - withdrawDelegationReward(ctx, delAddr, height, lastTotalPower) + k.withdrawDelegationReward(ctx, delAddr, valAddr) k.SetValidatorDistInfo(ctx, valInfo) k.SetDelegationDistInfo(ctx, delInfo) - - withdrawAddr := k.GetDelegatorWithdrawAddr(ctx, delegatorAddr) - coinsToAdd, change := withdraw.TruncateDecimal() - feePool.CommunityPool = feePool.CommunityPool.Plus(change) - k.SetFeePool(ctx, feePool) - _, _, err := k.bankKeeper.AddCoins(ctx, withdrawAddr, coinsToAdd) - if err != nil { - panic(err) - } + k.CompleteWithdrawal(ctx, feePool, delAddr, withdraw) return nil } // estimate rewards for a single delegation -func (k Keeper) EstimateDelegationReward(ctx sdk.Context, delegatorAddr sdk.AccAddress, +func (k Keeper) EstimateDelegationReward(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) (sdk.Coins, sdk.Error) { - if !k.HasDelegationDistInfo(ctx, delegatorAddr, valAddr) { - return types.ErrNoDelegationDistInfo(k.codespace) + if !k.HasDelegationDistInfo(ctx, delAddr, valAddr) { + return sdk.Coins{}, types.ErrNoDelegationDistInfo(k.codespace) } - estCoins := estimateDelegationReward(ctx, delAddr, height, lastTotalPower) - return estCoins.TruncateDecimal() + estCoins := k.estimateDelegationReward(ctx, delAddr, valAddr) + trucate, _ := estCoins.TruncateDecimal() + return trucate, nil } //___________________________________________________________________________________________ // return all rewards for all delegations of a delegator -func (k Keeper) WithdrawDelegationRewardsAll(ctx sdk.Context, delegatorAddr sdk.AccAddress) { - height := ctx.BlockHeight() +func (k Keeper) WithdrawDelegationRewardsAll(ctx sdk.Context, delAddr sdk.AccAddress) { + withdraw := k.withdrawDelegationRewardsAll(ctx, delAddr) + feePool := k.GetFeePool(ctx) + k.CompleteWithdrawal(ctx, feePool, delAddr, withdraw) +} + +func (k Keeper) withdrawDelegationRewardsAll(ctx sdk.Context, + delAddr sdk.AccAddress) types.DecCoins { // iterate over all the delegations withdraw := types.DecCoins{} - lastTotalPower := sdk.NewDecFromInt(k.stakeKeeper.GetLastTotalPower(ctx)) operationAtDelegation := func(_ int64, del sdk.Delegation) (stop bool) { + valAddr := del.GetValidatorAddr() feePool, valInfo, delInfo, diWithdraw := - withdrawDelegationReward(ctx, delAddr, height, lastTotalPower) + k.withdrawDelegationReward(ctx, delAddr, valAddr) withdraw = withdraw.Plus(diWithdraw) k.SetFeePool(ctx, feePool) k.SetValidatorDistInfo(ctx, valInfo) @@ -164,21 +181,12 @@ func (k Keeper) WithdrawDelegationRewardsAll(ctx sdk.Context, delegatorAddr sdk. return false } k.stakeKeeper.IterateDelegations(ctx, delAddr, operationAtDelegation) - - withdrawAddr := k.GetDelegatorWithdrawAddr(ctx, delegatorAddr) - coinsToAdd, change := withdraw.TruncateDecimal() - feePool := k.GetFeePool(ctx) - feePool.CommunityPool = feePool.CommunityPool.Plus(change) - k.SetFeePool(ctx, feePool) - _, _, err := k.bankKeeper.AddCoins(ctx, withdrawAddr, coinsToAdd) - if err != nil { - panic(err) - } + return withdraw } // estimate all rewards for all delegations of a delegator func (k Keeper) EstimateDelegationRewardsAll(ctx sdk.Context, - delegatorAddr sdk.AccAddress) sdk.Coins { + delAddr sdk.AccAddress) sdk.Coins { height := ctx.BlockHeight() @@ -186,7 +194,8 @@ func (k Keeper) EstimateDelegationRewardsAll(ctx sdk.Context, total := types.DecCoins{} lastTotalPower := sdk.NewDecFromInt(k.stakeKeeper.GetLastTotalPower(ctx)) operationAtDelegation := func(_ int64, del sdk.Delegation) (stop bool) { - est := estimateDelegationReward(ctx, delAddr, height, lastTotalPower) + valAddr := del.GetValidatorAddr() + est := k.estimateDelegationReward(ctx, delAddr, valAddr) total = total.Plus(est) return false } diff --git a/x/distribution/keeper/keeper.go b/x/distribution/keeper/keeper.go index 4480b9e70f8b..73477c474e3b 100644 --- a/x/distribution/keeper/keeper.go +++ b/x/distribution/keeper/keeper.go @@ -92,12 +92,12 @@ func (k Keeper) SetPreviousProposerConsAddr(ctx sdk.Context, consAddr sdk.ConsAd // get context required for withdraw operations func (k Keeper) GetWithdrawContext(ctx sdk.Context, - operatorAddr sdk.ValAddr) types.WithdrawContext { + valOperatorAddr sdk.ValAddress) types.WithdrawContext { feePool := k.GetFeePool(ctx) height := ctx.BlockHeight() - validator := k.stakeKeeper.Validator(ctx, operatorAddr) - lastValPower := k.stakeKeeper.GetLastValidatorPower(ctx, operatorAddr) + validator := k.stakeKeeper.Validator(ctx, valOperatorAddr) + lastValPower := k.stakeKeeper.GetLastValidatorPower(ctx, valOperatorAddr) lastTotalPower := sdk.NewDecFromInt(k.stakeKeeper.GetLastTotalPower(ctx)) return types.NewWithdrawContext( diff --git a/x/distribution/keeper/validator.go b/x/distribution/keeper/validator.go index 4e40cf117ee6..f27301794d91 100644 --- a/x/distribution/keeper/validator.go +++ b/x/distribution/keeper/validator.go @@ -62,27 +62,19 @@ func (k Keeper) WithdrawValidatorRewardsAll(ctx sdk.Context, operatorAddr sdk.Va if !k.HasValidatorDistInfo(ctx, operatorAddr) { return types.ErrNoValidatorDistInfo(k.codespace) } - wc := k.GetWithdrawContext(ctx, operatorAddr) // withdraw self-delegation accAddr := sdk.AccAddress(operatorAddr.Bytes()) - withdraw := k.withdrawDelegatorRewardsAll(ctx, accAddr, wc.Height) + withdraw := k.withdrawDelegationRewardsAll(ctx, accAddr) // withdrawal validator commission rewards valInfo := k.GetValidatorDistInfo(ctx, operatorAddr) - valInfo, feePool, commission := valInfo.WithdrawCommission(wb) + wc := k.GetWithdrawContext(ctx, operatorAddr) + valInfo, feePool, commission := valInfo.WithdrawCommission(wc) withdraw = withdraw.Plus(commission) k.SetValidatorDistInfo(ctx, valInfo) - withdrawAddr := k.GetDelegatorWithdrawAddr(ctx, accAddr) - truncated, change := withdraw.TruncateDecimal() - feePool.CommunityPool = feePool.CommunityPool.Plus(change) - k.SetFeePool(ctx, feePool) - _, _, err := k.bankKeeper.AddCoins(ctx, withdrawAddr, truncated) - if err != nil { - panic(err) - } - + k.CompleteWithdrawal(ctx, feePool, accAddr, withdraw) return nil } diff --git a/x/distribution/types/validator_info.go b/x/distribution/types/validator_info.go index 166fda6f20f9..705f44102523 100644 --- a/x/distribution/types/validator_info.go +++ b/x/distribution/types/validator_info.go @@ -8,20 +8,20 @@ import ( type WithdrawContext struct { FeePool FeePool Height int64 // block height - TotalBonded sdk.Dec // total bonded tokens in the network - ValTokens sdk.Dec // validator's bonded tokens + TotalPower sdk.Dec // total bonded tokens in the network + ValPower sdk.Dec // validator's bonded tokens CommissionRate sdk.Dec // validator commission rate } -func NewWithdrawContext(feePool FeePool, height int64, totalBonded, - valTokens, commissionRate sdk.Dec) WithdrawContext { +func NewWithdrawContext(feePool FeePool, height int64, totalPower, + valPower, commissionRate sdk.Dec) WithdrawContext { return WithdrawContext{ - fp: feePool, - height: height, - totalBonded: totalBonded, - vdTokens: vdTokens, - commissionRate: commissionRate, + FeePool: feePool, + Height: height, + TotalPower: totalPower, + ValPower: valPower, + CommissionRate: commissionRate, } } @@ -77,17 +77,17 @@ func (vi ValidatorDistInfo) GetValAccum(height int64, vdTokens sdk.Dec) sdk.Dec func (vi ValidatorDistInfo) TakeFeePoolRewards(wc WithdrawContext) ( ValidatorDistInfo, FeePool) { - fp := wc.FeePool.UpdateTotalValAccum(height, totalBonded) + fp := wc.FeePool.UpdateTotalValAccum(wc.Height, wc.TotalPower) if fp.TotalValAccum.Accum.IsZero() { return vi, fp } // update the validators pool - accum := vi.GetValAccum(height, vdTokens) - vi.FeePoolWithdrawalHeight = height + accum := vi.GetValAccum(wc.Height, wc.ValPower) + vi.FeePoolWithdrawalHeight = wc.Height - if accum.GT(wc.fp.TotalValAccum.Accum) { + if accum.GT(fp.TotalValAccum.Accum) { panic("individual accum should never be greater than the total") } withdrawalTokens := fp.Pool.MulDec(accum).QuoDec(fp.TotalValAccum.Accum) @@ -108,7 +108,7 @@ func (vi ValidatorDistInfo) TakeFeePoolRewards(wc WithdrawContext) ( func (vi ValidatorDistInfo) WithdrawCommission(wc WithdrawContext) ( vio ValidatorDistInfo, fpo FeePool, withdrawn DecCoins) { - vi, fp = vi.TakeFeePoolRewards(wc) + vi, fp := vi.TakeFeePoolRewards(wc) withdrawalTokens := vi.PoolCommission vi.PoolCommission = DecCoins{} // zero @@ -119,13 +119,13 @@ func (vi ValidatorDistInfo) WithdrawCommission(wc WithdrawContext) ( // Estimate the validator's pool rewards at this current state, // the estimated rewards are subject to fluctuation func (vi ValidatorDistInfo) EstimatePoolRewards( - wc WithdrawContext) sdk.Coins { + wc WithdrawContext) DecCoins { fp := wc.FeePool - totalValAccum = fp.GetTotalValAccum(wc.height, wc.TotalBonded) - valAccum := vi.GetValAccum(wc.height, wc.ValTokens) + totalValAccum := fp.GetTotalValAccum(wc.Height, wc.TotalPower) + valAccum := vi.GetValAccum(wc.Height, wc.ValPower) - if accum.GT(fp.TotalValAccum.Accum) { + if valAccum.GT(totalValAccum) { panic("individual accum should never be greater than the total") } withdrawalTokens := fp.Pool.MulDec(valAccum).QuoDec(totalValAccum) @@ -138,16 +138,16 @@ func (vi ValidatorDistInfo) EstimatePoolRewards( // Estimate the validator's commission pool rewards at this current state, // the estimated rewards are subject to fluctuation func (vi ValidatorDistInfo) EstimateCommissionRewards( - wc WithdrawContext) sdk.Coins { + wc WithdrawContext) DecCoins { fp := wc.FeePool - totalValAccum = fp.GetTotalValAccum(wc.Height, wc.TotalBonded) - valAccum := vi.GetValAccum(wc.Height, wc.ValTokens) + totalValAccum := fp.GetTotalValAccum(wc.Height, wc.TotalPower) + valAccum := vi.GetValAccum(wc.Height, wc.ValPower) - if accum.GT(fp.TotalValAccum.Accum) { + if valAccum.GT(totalValAccum) { panic("individual accum should never be greater than the total") } - withdrawalTokens := fp.Pool.MulDec(valAccum).QuoDec(wc.TotalValAccum) + withdrawalTokens := fp.Pool.MulDec(valAccum).QuoDec(totalValAccum) commission := withdrawalTokens.MulDec(wc.CommissionRate) commissionPool := vi.PoolCommission.Plus(commission) return commissionPool From 631215a767c5c441a5832df184bdb2ae701693fb Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 25 Oct 2018 13:25:52 -0400 Subject: [PATCH 5/7] Estimate -> Current, wip refactor --- x/distribution/keeper/delegation.go | 18 +++++++++--------- x/distribution/keeper/validator.go | 13 ++++++------- x/distribution/types/delegator_info.go | 11 +++++------ x/distribution/types/validator_info.go | 10 ++++------ 4 files changed, 24 insertions(+), 28 deletions(-) diff --git a/x/distribution/keeper/delegation.go b/x/distribution/keeper/delegation.go index 1ba82c9c6c65..ba9f9015a0ea 100644 --- a/x/distribution/keeper/delegation.go +++ b/x/distribution/keeper/delegation.go @@ -87,8 +87,8 @@ func (k Keeper) withdrawDelegationReward(ctx sdk.Context, return feePool, valInfo, delInfo, withdraw } -// estimate all rewards for all delegations of a delegator -func (k Keeper) estimateDelegationReward(ctx sdk.Context, delAddr sdk.AccAddress, +// get all rewards for all delegations of a delegator +func (k Keeper) currentDelegationReward(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) types.DecCoins { wc := k.GetWithdrawContext(ctx, valAddr) @@ -98,7 +98,7 @@ func (k Keeper) estimateDelegationReward(ctx sdk.Context, delAddr sdk.AccAddress validator := k.stakeKeeper.Validator(ctx, valAddr) delegation := k.stakeKeeper.Delegation(ctx, delAddr, valAddr) - estimation := delInfo.EstimateRewards(wc, valInfo, + estimation := delInfo.CurrentRewards(wc, valInfo, validator.GetDelegatorShares(), delegation.GetShares()) return estimation @@ -143,14 +143,14 @@ func (k Keeper) WithdrawDelegationReward(ctx sdk.Context, delAddr sdk.AccAddress return nil } -// estimate rewards for a single delegation -func (k Keeper) EstimateDelegationReward(ctx sdk.Context, delAddr sdk.AccAddress, +// current rewards for a single delegation +func (k Keeper) CurrentDelegationReward(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) (sdk.Coins, sdk.Error) { if !k.HasDelegationDistInfo(ctx, delAddr, valAddr) { return sdk.Coins{}, types.ErrNoDelegationDistInfo(k.codespace) } - estCoins := k.estimateDelegationReward(ctx, delAddr, valAddr) + estCoins := k.currentDelegationReward(ctx, delAddr, valAddr) trucate, _ := estCoins.TruncateDecimal() return trucate, nil } @@ -184,8 +184,8 @@ func (k Keeper) withdrawDelegationRewardsAll(ctx sdk.Context, return withdraw } -// estimate all rewards for all delegations of a delegator -func (k Keeper) EstimateDelegationRewardsAll(ctx sdk.Context, +// get all rewards for all delegations of a delegator +func (k Keeper) CurrentDelegationRewardsAll(ctx sdk.Context, delAddr sdk.AccAddress) sdk.Coins { height := ctx.BlockHeight() @@ -195,7 +195,7 @@ func (k Keeper) EstimateDelegationRewardsAll(ctx sdk.Context, lastTotalPower := sdk.NewDecFromInt(k.stakeKeeper.GetLastTotalPower(ctx)) operationAtDelegation := func(_ int64, del sdk.Delegation) (stop bool) { valAddr := del.GetValidatorAddr() - est := k.estimateDelegationReward(ctx, delAddr, valAddr) + est := k.currentDelegationReward(ctx, delAddr, valAddr) total = total.Plus(est) return false } diff --git a/x/distribution/keeper/validator.go b/x/distribution/keeper/validator.go index f27301794d91..150cc3bd10a8 100644 --- a/x/distribution/keeper/validator.go +++ b/x/distribution/keeper/validator.go @@ -51,7 +51,7 @@ func (k Keeper) GetValidatorAccum(ctx sdk.Context, operatorAddr sdk.ValAddress) height := ctx.BlockHeight() lastValPower := k.stakeKeeper.GetLastValidatorPower(ctx, operatorAddr) valInfo := k.GetValidatorDistInfo(ctx, operatorAddr) - accum := valInfo.GetAccum(height, lastValPower) + accum := valInfo.GetValAccum(height, lastValPower) return accum, nil } @@ -78,23 +78,22 @@ func (k Keeper) WithdrawValidatorRewardsAll(ctx sdk.Context, operatorAddr sdk.Va return nil } -// estimate all the validator rewards including the commission -// note: all estimations are subject to flucuation -func (k Keeper) EstimateValidatorRewardsAll(ctx sdk.Context, operatorAddr sdk.ValAddress) sdk.Error { +// get all the validator rewards including the commission +func (k Keeper) CurrentValidatorRewardsAll(ctx sdk.Context, operatorAddr sdk.ValAddress) sdk.Error { if !k.HasValidatorDistInfo(ctx, operatorAddr) { return types.ErrNoValidatorDistInfo(k.codespace) } - wc := k.GetWithdrawContext(ctx, operatorAddr) // withdraw self-delegation accAddr := sdk.AccAddress(operatorAddr.Bytes()) - withdraw := k.EstimateDelegatorRewardsAll(ctx, accAddr, wc.Height) + withdraw := k.CurrentDelegationRewardsAll(ctx, accAddr) // withdrawal validator commission rewards valInfo := k.GetValidatorDistInfo(ctx, operatorAddr) - commission := valInfo.EstimateCommission(wc) + wc := k.GetWithdrawContext(ctx, operatorAddr) + commission := valInfo.CurrentCommissionRewards(wc) withdraw = withdraw.Plus(commission) truncated, _ := withdraw.TruncateDecimal() return truncated diff --git a/x/distribution/types/delegator_info.go b/x/distribution/types/delegator_info.go index 615e611a7371..66f386a94521 100644 --- a/x/distribution/types/delegator_info.go +++ b/x/distribution/types/delegator_info.go @@ -58,9 +58,8 @@ func (di DelegationDistInfo) WithdrawRewards(wc WithdrawContext, vi ValidatorDis return di, vi, fp, withdrawalTokens } -// Estimate the delegators rewards at this current state, -// note: all estimations are subject to flucuation -func (di DelegationDistInfo) EstimateRewards(wc WithdrawContext, vi ValidatorDistInfo, +// get the delegators rewards at this current state, +func (di DelegationDistInfo) CurrentRewards(wc WithdrawContext, vi ValidatorDistInfo, totalDelShares, delegatorShares sdk.Dec) DecCoins { totalDelAccum := vi.GetTotalDelAccum(wc.Height, totalDelShares) @@ -69,8 +68,8 @@ func (di DelegationDistInfo) EstimateRewards(wc WithdrawContext, vi ValidatorDis return DecCoins{} } - rewards := vi.EstimatePoolRewards(wc) + rewards := vi.CurrentPoolRewards(wc) accum := di.GetDelAccum(wc.Height, delegatorShares) - estimatedTokens := rewards.MulDec(accum).QuoDec(totalDelAccum) - return estimatedTokens + tokens := rewards.MulDec(accum).QuoDec(totalDelAccum) + return tokens } diff --git a/x/distribution/types/validator_info.go b/x/distribution/types/validator_info.go index 705f44102523..933ea98b1fa1 100644 --- a/x/distribution/types/validator_info.go +++ b/x/distribution/types/validator_info.go @@ -116,9 +116,8 @@ func (vi ValidatorDistInfo) WithdrawCommission(wc WithdrawContext) ( return vi, fp, withdrawalTokens } -// Estimate the validator's pool rewards at this current state, -// the estimated rewards are subject to fluctuation -func (vi ValidatorDistInfo) EstimatePoolRewards( +// get the validator's pool rewards at this current state, +func (vi ValidatorDistInfo) CurrentPoolRewards( wc WithdrawContext) DecCoins { fp := wc.FeePool @@ -135,9 +134,8 @@ func (vi ValidatorDistInfo) EstimatePoolRewards( return pool } -// Estimate the validator's commission pool rewards at this current state, -// the estimated rewards are subject to fluctuation -func (vi ValidatorDistInfo) EstimateCommissionRewards( +// get the validator's commission pool rewards at this current state, +func (vi ValidatorDistInfo) CurrentCommissionRewards( wc WithdrawContext) DecCoins { fp := wc.FeePool From eda3463cf2cdddc5771e57ab134fefbf6d95d1cd Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 25 Oct 2018 13:50:00 -0400 Subject: [PATCH 6/7] core code compiling --- x/distribution/keeper/delegation.go | 14 +++++--------- x/distribution/keeper/keeper.go | 2 +- x/distribution/keeper/validator.go | 8 ++++---- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/x/distribution/keeper/delegation.go b/x/distribution/keeper/delegation.go index ba9f9015a0ea..d133d4e9112f 100644 --- a/x/distribution/keeper/delegation.go +++ b/x/distribution/keeper/delegation.go @@ -109,7 +109,7 @@ func (k Keeper) currentDelegationReward(ctx sdk.Context, delAddr sdk.AccAddress, // withdraw all rewards for a single delegation // NOTE: This gets called "onDelegationSharesModified", // meaning any changes to bonded coins -func (k Keeper) CompleteWithdrawal(ctx sdk.Context, feePool types.FeePool, +func (k Keeper) WithdrawToDelegator(ctx sdk.Context, feePool types.FeePool, delAddr sdk.AccAddress, amount types.DecCoins) { withdrawAddr := k.GetDelegatorWithdrawAddr(ctx, delAddr) @@ -139,7 +139,7 @@ func (k Keeper) WithdrawDelegationReward(ctx sdk.Context, delAddr sdk.AccAddress k.SetValidatorDistInfo(ctx, valInfo) k.SetDelegationDistInfo(ctx, delInfo) - k.CompleteWithdrawal(ctx, feePool, delAddr, withdraw) + k.WithdrawToDelegator(ctx, feePool, delAddr, withdraw) return nil } @@ -161,7 +161,7 @@ func (k Keeper) CurrentDelegationReward(ctx sdk.Context, delAddr sdk.AccAddress, func (k Keeper) WithdrawDelegationRewardsAll(ctx sdk.Context, delAddr sdk.AccAddress) { withdraw := k.withdrawDelegationRewardsAll(ctx, delAddr) feePool := k.GetFeePool(ctx) - k.CompleteWithdrawal(ctx, feePool, delAddr, withdraw) + k.WithdrawToDelegator(ctx, feePool, delAddr, withdraw) } func (k Keeper) withdrawDelegationRewardsAll(ctx sdk.Context, @@ -186,13 +186,10 @@ func (k Keeper) withdrawDelegationRewardsAll(ctx sdk.Context, // get all rewards for all delegations of a delegator func (k Keeper) CurrentDelegationRewardsAll(ctx sdk.Context, - delAddr sdk.AccAddress) sdk.Coins { - - height := ctx.BlockHeight() + delAddr sdk.AccAddress) types.DecCoins { // iterate over all the delegations total := types.DecCoins{} - lastTotalPower := sdk.NewDecFromInt(k.stakeKeeper.GetLastTotalPower(ctx)) operationAtDelegation := func(_ int64, del sdk.Delegation) (stop bool) { valAddr := del.GetValidatorAddr() est := k.currentDelegationReward(ctx, delAddr, valAddr) @@ -200,6 +197,5 @@ func (k Keeper) CurrentDelegationRewardsAll(ctx sdk.Context, return false } k.stakeKeeper.IterateDelegations(ctx, delAddr, operationAtDelegation) - estCoins, _ := total.TruncateDecimal() - return estCoins + return total } diff --git a/x/distribution/keeper/keeper.go b/x/distribution/keeper/keeper.go index 73477c474e3b..ab299a9b8d43 100644 --- a/x/distribution/keeper/keeper.go +++ b/x/distribution/keeper/keeper.go @@ -61,7 +61,7 @@ func (k Keeper) GetFeePoolValAccum(ctx sdk.Context) sdk.Dec { // withdraw self-delegation height := ctx.BlockHeight() - totalPower := k.stakeKeeper.GetLastTotalPower(ctx) + totalPower := sdk.NewDecFromInt(k.stakeKeeper.GetLastTotalPower(ctx)) fp := k.GetFeePool(ctx) return fp.GetTotalValAccum(height, totalPower) } diff --git a/x/distribution/keeper/validator.go b/x/distribution/keeper/validator.go index 150cc3bd10a8..cf6c1c11d9dc 100644 --- a/x/distribution/keeper/validator.go +++ b/x/distribution/keeper/validator.go @@ -74,15 +74,15 @@ func (k Keeper) WithdrawValidatorRewardsAll(ctx sdk.Context, operatorAddr sdk.Va withdraw = withdraw.Plus(commission) k.SetValidatorDistInfo(ctx, valInfo) - k.CompleteWithdrawal(ctx, feePool, accAddr, withdraw) + k.WithdrawToDelegator(ctx, feePool, accAddr, withdraw) return nil } // get all the validator rewards including the commission -func (k Keeper) CurrentValidatorRewardsAll(ctx sdk.Context, operatorAddr sdk.ValAddress) sdk.Error { +func (k Keeper) CurrentValidatorRewardsAll(ctx sdk.Context, operatorAddr sdk.ValAddress) (sdk.Coins, sdk.Error) { if !k.HasValidatorDistInfo(ctx, operatorAddr) { - return types.ErrNoValidatorDistInfo(k.codespace) + return sdk.Coins{}, types.ErrNoValidatorDistInfo(k.codespace) } // withdraw self-delegation @@ -96,5 +96,5 @@ func (k Keeper) CurrentValidatorRewardsAll(ctx sdk.Context, operatorAddr sdk.Val commission := valInfo.CurrentCommissionRewards(wc) withdraw = withdraw.Plus(commission) truncated, _ := withdraw.TruncateDecimal() - return truncated + return truncated, nil } From dc2e43016a7ab885376688988b6b52a2044cb4dc Mon Sep 17 00:00:00 2001 From: rigelrozanski Date: Thu, 25 Oct 2018 13:57:59 -0400 Subject: [PATCH 7/7] tests compiling/passing --- x/distribution/types/delegator_info_test.go | 12 ++++++++---- x/distribution/types/validator_info_test.go | 15 ++++++++++----- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/x/distribution/types/delegator_info_test.go b/x/distribution/types/delegator_info_test.go index 4af7f0a8f122..1c1511d7314a 100644 --- a/x/distribution/types/delegator_info_test.go +++ b/x/distribution/types/delegator_info_test.go @@ -29,8 +29,10 @@ func TestWithdrawRewards(t *testing.T) { fp.Pool = DecCoins{NewDecCoin("stake", 1000)} // withdraw rewards - di1, vi, fp, rewardRecv1 := di1.WithdrawRewards(fp, vi, height, totalBondedTokens, - validatorTokens, validatorDelShares, di1Shares, commissionRate) + wc := NewWithdrawContext(fp, height, + totalBondedTokens, validatorTokens, commissionRate) + di1, vi, fp, rewardRecv1 := di1.WithdrawRewards(wc, vi, + validatorDelShares, di1Shares) assert.Equal(t, height, di1.WithdrawalHeight) assert.True(sdk.DecEq(t, sdk.NewDec(900), fp.TotalValAccum.Accum)) @@ -44,8 +46,10 @@ func TestWithdrawRewards(t *testing.T) { fp.Pool[0].Amount = fp.Pool[0].Amount.Add(sdk.NewDec(1000)) // withdraw rewards - di2, vi, fp, rewardRecv2 := di2.WithdrawRewards(fp, vi, height, totalBondedTokens, - validatorTokens, validatorDelShares, di2Shares, commissionRate) + wc = NewWithdrawContext(fp, height, + totalBondedTokens, validatorTokens, commissionRate) + di2, vi, fp, rewardRecv2 := di2.WithdrawRewards(wc, vi, + validatorDelShares, di2Shares) assert.Equal(t, height, di2.WithdrawalHeight) assert.True(sdk.DecEq(t, sdk.NewDec(1800), fp.TotalValAccum.Accum)) diff --git a/x/distribution/types/validator_info_test.go b/x/distribution/types/validator_info_test.go index 9d1e39fa6a06..95feaba06063 100644 --- a/x/distribution/types/validator_info_test.go +++ b/x/distribution/types/validator_info_test.go @@ -28,13 +28,15 @@ func TestTakeFeePoolRewards(t *testing.T) { height = 10 fp.Pool = DecCoins{NewDecCoin("stake", 1000)} - vi1, fp = vi1.TakeFeePoolRewards(fp, height, totalBondedTokens, validatorTokens1, commissionRate1) + vi1, fp = vi1.TakeFeePoolRewards(NewWithdrawContext( + fp, height, totalBondedTokens, validatorTokens1, commissionRate1)) require.True(sdk.DecEq(t, sdk.NewDec(900), fp.TotalValAccum.Accum)) assert.True(sdk.DecEq(t, sdk.NewDec(900), fp.Pool[0].Amount)) assert.True(sdk.DecEq(t, sdk.NewDec(100-2), vi1.Pool[0].Amount)) assert.True(sdk.DecEq(t, sdk.NewDec(2), vi1.PoolCommission[0].Amount)) - vi2, fp = vi2.TakeFeePoolRewards(fp, height, totalBondedTokens, validatorTokens2, commissionRate2) + vi2, fp = vi2.TakeFeePoolRewards(NewWithdrawContext( + fp, height, totalBondedTokens, validatorTokens2, commissionRate2)) require.True(sdk.DecEq(t, sdk.NewDec(500), fp.TotalValAccum.Accum)) assert.True(sdk.DecEq(t, sdk.NewDec(500), fp.Pool[0].Amount)) assert.True(sdk.DecEq(t, sdk.NewDec(400-12), vi2.Pool[0].Amount)) @@ -44,7 +46,8 @@ func TestTakeFeePoolRewards(t *testing.T) { height = 20 fp.Pool[0].Amount = fp.Pool[0].Amount.Add(sdk.NewDec(1000)) - vi3, fp = vi3.TakeFeePoolRewards(fp, height, totalBondedTokens, validatorTokens3, commissionRate3) + vi3, fp = vi3.TakeFeePoolRewards(NewWithdrawContext( + fp, height, totalBondedTokens, validatorTokens3, commissionRate3)) require.True(sdk.DecEq(t, sdk.NewDec(500), fp.TotalValAccum.Accum)) assert.True(sdk.DecEq(t, sdk.NewDec(500), fp.Pool[0].Amount)) assert.True(sdk.DecEq(t, sdk.NewDec(1000-40), vi3.Pool[0].Amount)) @@ -66,7 +69,8 @@ func TestWithdrawCommission(t *testing.T) { fp.Pool = DecCoins{NewDecCoin("stake", 1000)} // for a more fun staring condition, have an non-withdraw update - vi, fp = vi.TakeFeePoolRewards(fp, height, totalBondedTokens, validatorTokens, commissionRate) + vi, fp = vi.TakeFeePoolRewards(NewWithdrawContext( + fp, height, totalBondedTokens, validatorTokens, commissionRate)) require.True(sdk.DecEq(t, sdk.NewDec(900), fp.TotalValAccum.Accum)) assert.True(sdk.DecEq(t, sdk.NewDec(900), fp.Pool[0].Amount)) assert.True(sdk.DecEq(t, sdk.NewDec(100-2), vi.Pool[0].Amount)) @@ -76,7 +80,8 @@ func TestWithdrawCommission(t *testing.T) { height = 20 fp.Pool[0].Amount = fp.Pool[0].Amount.Add(sdk.NewDec(1000)) - vi, fp, commissionRecv := vi.WithdrawCommission(fp, height, totalBondedTokens, validatorTokens, commissionRate) + vi, fp, commissionRecv := vi.WithdrawCommission(NewWithdrawContext( + fp, height, totalBondedTokens, validatorTokens, commissionRate)) require.True(sdk.DecEq(t, sdk.NewDec(1800), fp.TotalValAccum.Accum)) assert.True(sdk.DecEq(t, sdk.NewDec(1800), fp.Pool[0].Amount)) assert.True(sdk.DecEq(t, sdk.NewDec(200-4), vi.Pool[0].Amount))