Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: distr simulation accum invariance #2563

Closed
wants to merge 41 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
1674625
Update PENDING.md
cwgoes Oct 16, 2018
2fff810
This PR now will do something else
cwgoes Oct 16, 2018
67258c4
Merge branch 'develop' into cwgoes/check-supply-in-simulation
cwgoes Oct 16, 2018
b700107
Add simulated distribution msgs
cwgoes Oct 16, 2018
1d4b595
Correctly set power in vote info
cwgoes Oct 16, 2018
74780c6
Merge branch 'develop' into cwgoes/check-supply-in-simulation
cwgoes Oct 18, 2018
f527163
Merge branch 'develop' into cwgoes/check-supply-in-simulation
cwgoes Oct 18, 2018
e099491
Merge PR #2526: Distribution fixes from simulation
cwgoes Oct 18, 2018
06c7e27
Merge branch 'develop' into cwgoes/check-supply-in-simulation
cwgoes Oct 18, 2018
25ce0de
Update stake module simulation and fix linter errors
cwgoes Oct 18, 2018
42ae267
Simulation fixes work-in-progress
cwgoes Oct 19, 2018
4779359
Linter, enable simulated distribution msgs again
cwgoes Oct 19, 2018
d21f93a
Simulation debugging contd.
cwgoes Oct 19, 2018
c3d3a79
Merge branch 'develop' into cwgoes/check-supply-in-simulation
cwgoes Oct 19, 2018
375e821
Merge branch 'develop' into cwgoes/check-supply-in-simulation
cwgoes Oct 19, 2018
22cb698
Merge branch 'develop' into cwgoes/check-supply-in-simulation
cwgoes Oct 19, 2018
d9916f9
bugfix (#2535)
rigelrozanski Oct 19, 2018
4f719d6
Remove the print statement
cwgoes Oct 19, 2018
6c9ad8c
Add sanity checks
cwgoes Oct 19, 2018
2e8f354
Fixup stake hooks (hopefully...)
cwgoes Oct 19, 2018
843ccaf
Remove comment
cwgoes Oct 19, 2018
1afb5bf
Simulate minting, fix bug where pool was not updated
cwgoes Oct 19, 2018
dc13a0c
Use InflateSupply instead
cwgoes Oct 19, 2018
a6ef3c4
use power instead of total tokens
rigelrozanski Oct 19, 2018
7770aec
Withdraw rewards on bonded to unbonding
cwgoes Oct 19, 2018
255a6a5
Cleanup to one hook
cwgoes Oct 19, 2018
c88fc48
Fix linter issues
cwgoes Oct 19, 2018
c9e5c32
Merge branch 'develop' into cwgoes/check-supply-in-simulation
cwgoes Oct 19, 2018
3fa5778
Update naming (BondedValidatorIndex->ValidatorsBondedIndex) and add s…
jaekwon Oct 21, 2018
015b829
GetValidatorsBonded -> LastValidators etc
jaekwon Oct 21, 2018
5416af8
LastValidatorPower -> LastValidator
jaekwon Oct 21, 2018
1cc7432
Replicate Rigel's changes but w/ modifications as discussed + some na…
jaekwon Oct 22, 2018
19225fc
Print debugging info
jaekwon Oct 22, 2018
471ddee
tweak debug output
jaekwon Oct 22, 2018
278d237
call hook on slashing
rigelrozanski Oct 22, 2018
a3447cd
debug cleanup
rigelrozanski Oct 22, 2018
c4d7747
Merge remote-tracking branch 'origin/cwgoes/check-supply-in-simulatio…
rigelrozanski Oct 22, 2018
bcc3192
correct LastValidatorPower
rigelrozanski Oct 22, 2018
e088c8b
Merge pull request #2552 from cosmos/jae/check-supply-in-simulation
rigelrozanski Oct 22, 2018
7f43860
Merge branch 'cwgoes/check-supply-in-simulation' of github.com:cosmos…
cwgoes Oct 23, 2018
4df3260
add GetAccum for validator and fee pool
rigelrozanski Oct 23, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ BREAKING CHANGES
* [simulation] \#2162 Added back correct supply invariants
* [x/slashing] \#2430 Simulate more slashes, check if validator is jailed before jailing
* [x/stake] \#2393 Removed `CompleteUnbonding` and `CompleteRedelegation` Msg types, and instead added unbonding/redelegation queues to endblocker
* [x/mock/simulation] \#2501 Simulate transactions & invariants for fee distribution
* [x/stake] \#1673 Validators are no longer deleted until they can no longer possibly be slashed
* [\#1890](https://github.com/cosmos/cosmos-sdk/issues/1890) Start chain with initial state + sequence of transactions
* [cli] Rename `gaiad init gentx` to `gaiad gentx`.
Expand Down
16 changes: 9 additions & 7 deletions cmd/gaia/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package app
import (
"encoding/json"
"fmt"
"io"
"os"
"sort"

bam "github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -19,9 +23,6 @@ import (
dbm "github.com/tendermint/tendermint/libs/db"
"github.com/tendermint/tendermint/libs/log"
tmtypes "github.com/tendermint/tendermint/types"
"io"
"os"
"sort"
)

const (
Expand Down Expand Up @@ -334,17 +335,18 @@ var _ sdk.StakingHooks = Hooks{}
func (h Hooks) OnValidatorCreated(ctx sdk.Context, addr sdk.ValAddress) {
h.dh.OnValidatorCreated(ctx, addr)
}
func (h Hooks) OnValidatorCommissionChange(ctx sdk.Context, addr sdk.ValAddress) {
h.dh.OnValidatorCommissionChange(ctx, addr)
func (h Hooks) OnValidatorModified(ctx sdk.Context, addr sdk.ValAddress) {
h.dh.OnValidatorModified(ctx, addr)
}
func (h Hooks) OnValidatorRemoved(ctx sdk.Context, addr sdk.ValAddress) {
h.dh.OnValidatorRemoved(ctx, addr)
}
func (h Hooks) OnValidatorBonded(ctx sdk.Context, addr sdk.ConsAddress) {
h.sh.OnValidatorBonded(ctx, addr)
}
func (h Hooks) OnValidatorBeginUnbonding(ctx sdk.Context, addr sdk.ConsAddress) {
h.sh.OnValidatorBeginUnbonding(ctx, addr)
func (h Hooks) OnValidatorBeginUnbonding(ctx sdk.Context, addr sdk.ConsAddress, operator sdk.ValAddress) {
h.dh.OnValidatorBeginUnbonding(ctx, addr, operator)
h.sh.OnValidatorBeginUnbonding(ctx, addr, operator)
}
func (h Hooks) OnDelegationCreated(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) {
h.dh.OnDelegationCreated(ctx, delAddr, valAddr)
Expand Down
24 changes: 14 additions & 10 deletions cmd/gaia/app/sim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import (
"github.com/tendermint/tendermint/libs/log"

sdk "github.com/cosmos/cosmos-sdk/types"
authsim "github.com/cosmos/cosmos-sdk/x/auth/simulation"
banksim "github.com/cosmos/cosmos-sdk/x/bank/simulation"
distr "github.com/cosmos/cosmos-sdk/x/distribution"
distributionsim "github.com/cosmos/cosmos-sdk/x/distribution/simulation"
"github.com/cosmos/cosmos-sdk/x/gov"
govsim "github.com/cosmos/cosmos-sdk/x/gov/simulation"
"github.com/cosmos/cosmos-sdk/x/mint"
Expand Down Expand Up @@ -49,7 +51,7 @@ func appStateFn(r *rand.Rand, accs []simulation.Account) json.RawMessage {

// Randomly generate some genesis accounts
for _, acc := range accs {
coins := sdk.Coins{sdk.Coin{"steak", sdk.NewInt(100)}}
coins := sdk.Coins{sdk.Coin{"steak", sdk.NewInt(10000)}}
genesisAccounts = append(genesisAccounts, GenesisAccount{
Address: acc.Address,
Coins: coins,
Expand All @@ -71,20 +73,16 @@ func appStateFn(r *rand.Rand, accs []simulation.Account) json.RawMessage {
valAddrs[i] = valAddr

validator := stake.NewValidator(valAddr, accs[i].PubKey, stake.Description{})
validator.Tokens = sdk.NewDec(100)
validator.DelegatorShares = sdk.NewDec(100)
delegation := stake.Delegation{accs[i].Address, valAddr, sdk.NewDec(100), 0}
validator.Tokens = sdk.NewDec(10000)
validator.DelegatorShares = sdk.NewDec(10000)
delegation := stake.Delegation{accs[i].Address, valAddr, sdk.NewDec(10000), 0}
validators = append(validators, validator)
delegations = append(delegations, delegation)
}
stakeGenesis.Pool.LooseTokens = sdk.NewDec(int64(100*250) + (numInitiallyBonded * 100))
stakeGenesis.Pool.LooseTokens = sdk.NewDec(int64(10000*250) + (numInitiallyBonded * 10000))
stakeGenesis.Validators = validators
stakeGenesis.Bonds = delegations

// No inflation, for now
mintGenesis := mint.DefaultGenesisState()
mintGenesis.Params.InflationMax = sdk.NewDec(0)
mintGenesis.Params.InflationMin = sdk.NewDec(0)

genesis := GenesisState{
Accounts: genesisAccounts,
Expand All @@ -106,7 +104,12 @@ func appStateFn(r *rand.Rand, accs []simulation.Account) json.RawMessage {

func testAndRunTxs(app *GaiaApp) []simulation.WeightedOperation {
return []simulation.WeightedOperation{
{5, authsim.SimulateDeductFee(app.accountMapper, app.feeCollectionKeeper)},
{100, banksim.SingleInputSendMsg(app.accountMapper, app.bankKeeper)},
{50, distributionsim.SimulateMsgSetWithdrawAddress(app.accountMapper, app.distrKeeper)},
{50, distributionsim.SimulateMsgWithdrawDelegatorRewardsAll(app.accountMapper, app.distrKeeper)},
{50, distributionsim.SimulateMsgWithdrawDelegatorReward(app.accountMapper, app.distrKeeper)},
{50, distributionsim.SimulateMsgWithdrawValidatorRewardsAll(app.accountMapper, app.distrKeeper)},
{5, govsim.SimulateSubmittingVotingAndSlashingForProposal(app.govKeeper, app.stakeKeeper)},
{100, govsim.SimulateMsgDeposit(app.govKeeper, app.stakeKeeper)},
{100, stakesim.SimulateMsgCreateValidator(app.accountMapper, app.stakeKeeper)},
Expand All @@ -121,8 +124,9 @@ func testAndRunTxs(app *GaiaApp) []simulation.WeightedOperation {
func invariants(app *GaiaApp) []simulation.Invariant {
return []simulation.Invariant{
banksim.NonnegativeBalanceInvariant(app.accountMapper),
distributionsim.AllInvariants(app.bankKeeper, app.distrKeeper, app.accountMapper),
govsim.AllInvariants(),
stakesim.AllInvariants(app.bankKeeper, app.stakeKeeper, app.accountMapper),
stakesim.AllInvariants(app.bankKeeper, app.stakeKeeper, app.feeCollectionKeeper, app.distrKeeper, app.accountMapper),
slashingsim.AllInvariants(),
}
}
Expand Down
8 changes: 4 additions & 4 deletions docs/spec/staking/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ The staking module allow for the following hooks to be registered with staking e
``` golang
// event hooks for staking validator object
type StakingHooks interface {
OnValidatorCreated(ctx Context, address ValAddress) // called when a validator is created
OnValidatorCommissionChange(ctx Context, address ValAddress) // called when a validator's commission is modified
OnValidatorRemoved(ctx Context, address ValAddress) // called when a validator is deleted
OnValidatorCreated(ctx Context, address ValAddress) // Must be called when a validator is created
OnValidatorModified(ctx Context, address ValAddress) // Must be called when a validator's state changes
OnValidatorRemoved(ctx Context, address ValAddress) // Must be called when a validator is deleted

OnValidatorBonded(ctx Context, address ConsAddress) // called when a validator is bonded
OnValidatorBeginUnbonding(ctx Context, address ConsAddress) // called when a validator begins unbonding
OnValidatorBeginUnbonding(ctx Context, address ConsAddress, operator ValAddress) // called when a validator begins unbonding

OnDelegationCreated(ctx Context, delAddr AccAddress, valAddr ValAddress) // called when a delegation is created
OnDelegationSharesModified(ctx Context, delAddr AccAddress, valAddr ValAddress) // called when a delegation's shares are modified
Expand Down
5 changes: 5 additions & 0 deletions types/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,11 @@ func (d Dec) QuoInt(i Int) Dec {
return Dec{mul}
}

// is integer, e.g. decimals are zero.
func (d Dec) IsInteger() bool {
return new(big.Int).Rem(d.Int, precisionReuse).Sign() == 0
}

func (d Dec) String() string {
str := d.ToLeftPaddedWithDecimals(Precision)
placement := len(str) - Precision
Expand Down
16 changes: 8 additions & 8 deletions types/stake.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ type ValidatorSet interface {

// delegation bond for a delegated proof of stake system
type Delegation interface {
GetDelegator() AccAddress // delegator AccAddress for the bond
GetValidator() ValAddress // validator operator address
GetShares() Dec // amount of validator's shares held in this delegation
GetDelegatorAddr() AccAddress // delegator AccAddress for the bond
GetValidatorAddr() ValAddress // validator operator address
GetShares() Dec // amount of validator's shares held in this delegation
}

// properties for the set of all delegations for a particular
Expand All @@ -111,12 +111,12 @@ type DelegationSet interface {

// event hooks for staking validator object
type StakingHooks interface {
OnValidatorCreated(ctx Context, address ValAddress) // Must be called when a validator is created
OnValidatorCommissionChange(ctx Context, address ValAddress) // Must be called when a validator's commission is modified
OnValidatorRemoved(ctx Context, address ValAddress) // Must be called when a validator is deleted
OnValidatorCreated(ctx Context, address ValAddress) // Must be called when a validator is created
OnValidatorModified(ctx Context, address ValAddress) // Must be called when a validator's state changes
OnValidatorRemoved(ctx Context, address ValAddress) // Must be called when a validator is deleted

OnValidatorBonded(ctx Context, address ConsAddress) // Must be called when a validator is bonded
OnValidatorBeginUnbonding(ctx Context, address ConsAddress) // Must be called when a validator begins unbonding
OnValidatorBonded(ctx Context, address ConsAddress) // Must be called when a validator is bonded
OnValidatorBeginUnbonding(ctx Context, address ConsAddress, operator ValAddress) // Must be called when a validator begins unbonding

OnDelegationCreated(ctx Context, delAddr AccAddress, valAddr ValAddress) // Must be called when a delegation is created
OnDelegationSharesModified(ctx Context, delAddr AccAddress, valAddr ValAddress) // Must be called when a delegation's shares are modified
Expand Down
62 changes: 62 additions & 0 deletions x/auth/simulation/fake.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package simulation

import (
"errors"
"fmt"
"math/big"
"math/rand"

"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/mock/simulation"
)

// SimulateDeductFee
func SimulateDeductFee(m auth.AccountMapper, f auth.FeeCollectionKeeper) simulation.Operation {
return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context,
accs []simulation.Account, event func(string)) (
action string, fOp []simulation.FutureOperation, err error) {

account := simulation.RandomAcc(r, accs)
stored := m.GetAccount(ctx, account.Address)
initCoins := stored.GetCoins()

if len(initCoins) == 0 {
event(fmt.Sprintf("auth/SimulateDeductFee/false"))
return action, nil, nil
}

denomIndex := r.Intn(len(initCoins))
amt, err := randPositiveInt(r, initCoins[denomIndex].Amount)
if err != nil {
event(fmt.Sprintf("auth/SimulateDeductFee/false"))
return action, nil, nil
}

coins := sdk.Coins{sdk.NewCoin(initCoins[denomIndex].Denom, amt)}
err = stored.SetCoins(initCoins.Minus(coins))
if err != nil {
panic(err)
}
m.SetAccount(ctx, stored)
if !coins.IsNotNegative() {
panic("setting negative fees")
}

f.AddCollectedFees(ctx, coins)

event(fmt.Sprintf("auth/SimulateDeductFee/true"))

action = "TestDeductFee"
return action, nil, nil
}
}

func randPositiveInt(r *rand.Rand, max sdk.Int) (sdk.Int, error) {
if !max.GT(sdk.OneInt()) {
return sdk.Int{}, errors.New("max too small")
}
max = max.Sub(sdk.OneInt())
return sdk.NewIntFromBigInt(new(big.Int).Rand(r, max.BigInt())).Add(sdk.OneInt()), nil
}
2 changes: 1 addition & 1 deletion x/distribution/alias.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var (

NewMsgSetWithdrawAddress = types.NewMsgSetWithdrawAddress
NewMsgWithdrawDelegatorRewardsAll = types.NewMsgWithdrawDelegatorRewardsAll
NewMsgWithdrawDelegationReward = types.NewMsgWithdrawDelegatorReward
NewMsgWithdrawDelegatorReward = types.NewMsgWithdrawDelegatorReward
NewMsgWithdrawValidatorRewardsAll = types.NewMsgWithdrawValidatorRewardsAll
)

Expand Down
10 changes: 8 additions & 2 deletions x/distribution/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ func handleMsgWithdrawDelegatorRewardsAll(ctx sdk.Context, msg types.MsgWithdraw

func handleMsgWithdrawDelegatorReward(ctx sdk.Context, msg types.MsgWithdrawDelegatorReward, k keeper.Keeper) sdk.Result {

k.WithdrawDelegationReward(ctx, msg.DelegatorAddr, msg.ValidatorAddr)
err := k.WithdrawDelegationReward(ctx, msg.DelegatorAddr, msg.ValidatorAddr)
if err != nil {
return err.Result()
}

tags := sdk.NewTags(
tags.Action, tags.ActionWithdrawDelegatorReward,
Expand All @@ -72,7 +75,10 @@ func handleMsgWithdrawDelegatorReward(ctx sdk.Context, msg types.MsgWithdrawDele

func handleMsgWithdrawValidatorRewardsAll(ctx sdk.Context, msg types.MsgWithdrawValidatorRewardsAll, k keeper.Keeper) sdk.Result {

k.WithdrawValidatorRewardsAll(ctx, msg.ValidatorAddr)
err := k.WithdrawValidatorRewardsAll(ctx, msg.ValidatorAddr)
if err != nil {
return err.Result()
}

tags := sdk.NewTags(
tags.Action, tags.ActionWithdrawValidatorRewardsAll,
Expand Down
60 changes: 41 additions & 19 deletions x/distribution/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import (
"github.com/cosmos/cosmos-sdk/x/distribution/types"
)

// check whether a delegator distribution info exists
func (k Keeper) HasDelegationDistInfo(ctx sdk.Context, delAddr sdk.AccAddress,
valOperatorAddr sdk.ValAddress) (has bool) {
store := ctx.KVStore(k.storeKey)
return store.Has(GetDelegationDistInfoKey(delAddr, valOperatorAddr))
}

// get the delegator distribution info
func (k Keeper) GetDelegationDistInfo(ctx sdk.Context, delAddr sdk.AccAddress,
valOperatorAddr sdk.ValAddress) (ddi types.DelegationDistInfo) {
Expand Down Expand Up @@ -62,29 +69,40 @@ func (k Keeper) RemoveDelegatorWithdrawAddr(ctx sdk.Context, delAddr, withdrawAd

//___________________________________________________________________________________________

// withdraw all the rewards for a single delegation
// Withdraw all the 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,
validatorAddr sdk.ValAddress) {
valAddr sdk.ValAddress) sdk.Error {

if !k.HasDelegationDistInfo(ctx, delegatorAddr, valAddr) {
return types.ErrNoDelegationDistInfo(k.codespace)
}

// TODO: Reconcile with duplicate code in getDelegatorRewardsAll.
height := ctx.BlockHeight()
bondedTokens := k.stakeKeeper.TotalPower(ctx)
lastTotalPower := k.stakeKeeper.GetLastTotalPower(ctx)
lastValPower := k.stakeKeeper.GetLastValidatorPower(ctx, valAddr)
feePool := k.GetFeePool(ctx)
delInfo := k.GetDelegationDistInfo(ctx, delegatorAddr, validatorAddr)
valInfo := k.GetValidatorDistInfo(ctx, validatorAddr)
validator := k.stakeKeeper.Validator(ctx, validatorAddr)
delegation := k.stakeKeeper.Delegation(ctx, delegatorAddr, validatorAddr)
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, bondedTokens,
validator.GetTokens(), validator.GetDelegatorShares(), delegation.GetShares(), validator.GetCommission())
delInfo, valInfo, feePool, withdraw := delInfo.WithdrawRewards(feePool, valInfo, height, lastTotalPower,
lastValPower, validator.GetDelegatorShares(), delegation.GetShares(), validator.GetCommission())

k.SetFeePool(ctx, feePool)
k.SetValidatorDistInfo(ctx, valInfo)
k.SetDelegationDistInfo(ctx, delInfo)
withdrawAddr := k.GetDelegatorWithdrawAddr(ctx, delegatorAddr)
_, _, err := k.bankKeeper.AddCoins(ctx, withdrawAddr, withdraw.TruncateDecimal())
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)
}
return nil
}

//___________________________________________________________________________________________
Expand All @@ -93,8 +111,12 @@ func (k Keeper) WithdrawDelegationReward(ctx sdk.Context, delegatorAddr sdk.AccA
func (k Keeper) WithdrawDelegationRewardsAll(ctx sdk.Context, delegatorAddr sdk.AccAddress) {
height := ctx.BlockHeight()
withdraw := k.getDelegatorRewardsAll(ctx, delegatorAddr, height)
feePool := k.GetFeePool(ctx)
withdrawAddr := k.GetDelegatorWithdrawAddr(ctx, delegatorAddr)
_, _, err := k.bankKeeper.AddCoins(ctx, withdrawAddr, withdraw.TruncateDecimal())
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)
}
Expand All @@ -104,27 +126,27 @@ func (k Keeper) WithdrawDelegationRewardsAll(ctx sdk.Context, delegatorAddr sdk.
func (k Keeper) getDelegatorRewardsAll(ctx sdk.Context, delAddr sdk.AccAddress, height int64) types.DecCoins {

withdraw := types.DecCoins{}
bondedTokens := k.stakeKeeper.TotalPower(ctx)
feePool := k.GetFeePool(ctx)
lastTotalPower := k.stakeKeeper.GetLastTotalPower(ctx)

// iterate over all the delegations
// TODO: Reconcile with duplicate code in WithdrawDelegationReward.
operationAtDelegation := func(_ int64, del sdk.Delegation) (stop bool) {
valAddr := del.GetValidator()
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, bondedTokens,
validator.GetTokens(), validator.GetDelegatorShares(), delegation.GetShares(), validator.GetCommission())
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)

k.SetFeePool(ctx, feePool)
return withdraw
}
Loading