diff --git a/types/stake.go b/types/stake.go index 3d5969f9d4..4577e60474 100644 --- a/types/stake.go +++ b/types/stake.go @@ -45,7 +45,7 @@ type Validator interface { GetPower() Dec // validation power GetTokens() Dec // validation tokens GetCommission() Dec // validator commission rate - GetDelegatorShares() Dec // Total out standing delegator shares + GetDelegatorShares() Dec // Total delegator shares GetBondHeight() int64 // height in which the validator became active } @@ -87,8 +87,8 @@ 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 - GetBondShares() Dec // amount of validator's shares + GetValidator() ValAddress // validator operator address TODO change to GetValAddr + GetShares() Dec // amount of validator's shares } // properties for the set of all delegations for a particular diff --git a/x/distribution/abci_app.go b/x/distribution/abci_app.go index e70252a8de..2ed9af0c0a 100644 --- a/x/distribution/abci_app.go +++ b/x/distribution/abci_app.go @@ -1,8 +1,10 @@ package distribution import ( - "github.com/cosmos/cosmos-sdk/x/distribution/keeper" abci "github.com/tendermint/tendermint/abci/types" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/x/distribution/keeper" ) // set the proposer for determining distribution during endblock diff --git a/x/distribution/keeper/allocation.go b/x/distribution/keeper/allocation.go index a20055bbb4..1c990dc4b5 100644 --- a/x/distribution/keeper/allocation.go +++ b/x/distribution/keeper/allocation.go @@ -27,14 +27,15 @@ func (k Keeper) AllocateFees(ctx sdk.Context) { // apply commission commission := proposerReward.Mul(proserValidator.GetCommission()) - proposerDist.PoolCommission = proposerDist.PoolCommission.Add(commission) - proposerDist.Pool = proposerDist.Pool.Add(proposerReward.Sub(commission)) + remaining := proposerReward.Mul(sdk.OneDec().Sub(proserValidator.GetCommission())) + proposerDist.PoolCommission = proposerDist.PoolCommission.Plus(commission) + proposerDist.Pool = proposerDist.Pool.Plus(remaining) // allocate community funding communityTax := k.GetCommunityTax(ctx) communityFunding := feesCollectedDec.Mul(communityTax) feePool := k.GetFeePool(ctx) - feePool.CommunityPool = feePool.CommunityPool.Add(communityFunding) + feePool.CommunityPool = feePool.CommunityPool.Plus(communityFunding) // set the global pool within the distribution module poolReceived := feesCollectedDec.Mul(sdk.OneDec().Sub(proposerMultiplier).Sub(communityTax)) diff --git a/x/distribution/keeper/delegation.go b/x/distribution/keeper/delegation.go index 1323f2dbc4..9913bfce5d 100644 --- a/x/distribution/keeper/delegation.go +++ b/x/distribution/keeper/delegation.go @@ -72,9 +72,10 @@ func (k Keeper) WithdrawDelegationReward(ctx sdk.Context, delegatorAddr sdk.AccA delInfo := k.GetDelegatorDistInfo(ctx, delegatorAddr, validatorAddr) valInfo := k.GetValidatorDistInfo(ctx, validatorAddr) validator := k.stakeKeeper.GetValidator(ctx, validatorAddr) + delegation := k.stakeKeeper.GetDelegation(ctx, delegatorAddr, validatorAddr) - delInfo, feePool, withdraw := delInfo.WithdrawRewards(ctx, feePool, valInfo, height, bondedTokens, - validator.Tokens, validator.DelegatorShares, validator.Commission) + delInfo, feePool, withdraw := delInfo.WithdrawRewards(feePool, valInfo, height, bondedTokens, + validator.GetTokens(), validator.GetDelegatorShares(), delegation.GetShares(), validator.GetCommission()) k.SetFeePool(ctx, feePool) withdrawAddr := k.GetDelegatorWithdrawAddr(ctx, delegatorAddr) @@ -86,28 +87,31 @@ 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) - withdrawAddr := k.GetDelegatorWithdrawAddr(delegatorAddr) - k.coinsKeeper.AddCoins(withdrawAddr, withdraw.Amount.TruncateDecimal()) + withdraw := k.GetDelegatorRewardsAll(ctx, delegatorAddr, height) + withdrawAddr := k.GetDelegatorWithdrawAddr(ctx, delegatorAddr) + k.bankKeeper.AddCoins(ctx, withdrawAddr, withdraw.TruncateDecimal()) } // return all rewards for all delegations of a delegator func (k Keeper) GetDelegatorRewardsAll(ctx sdk.Context, delAddr sdk.AccAddress, height int64) types.DecCoins { - withdraw := sdk.NewDec(0) - pool := k.sk.GetPool(ctx) + withdraw := types.DecCoins{} + bondedTokens := k.stakeKeeper.TotalPower(ctx) feePool := k.GetFeePool(ctx) // iterate over all the delegations - operationAtDelegation := func(_ int64, del types.Delegation) (stop bool) { - delInfo := k.GetDelegationDistInfo(ctx, delAddr, del.ValidatorAddr) - valInfo := k.GetValidatorDistInfo(ctx, del.ValidatorAddr) - validator := k.sk.GetValidator(ctx, del.ValidatorAddr) + operationAtDelegation := func(_ int64, del sdk.Delegation) (stop bool) { + valAddr := del.GetValidator() + delInfo := k.GetDelegatorDistInfo(ctx, delAddr, valAddr) + valInfo := k.GetValidatorDistInfo(ctx, valAddr) + validator := k.stakeKeeper.GetValidator(ctx, valAddr) + delegation := k.stakeKeeper.GetDelegation(ctx, delAddr, valAddr) - feePool, diWithdraw := delInfo.WithdrawRewards(feePool, valInfo, height, pool.BondedTokens, - validator.Tokens, validator.DelegatorShares, validator.Commission) - withdraw = withdraw.Add(diWithdraw) - SetFeePool(feePool) + delInfo, feePool, diWithdraw := delInfo.WithdrawRewards(feePool, valInfo, height, bondedTokens, + validator.GetTokens(), validator.GetDelegatorShares(), delegation.GetShares(), validator.GetCommission()) + withdraw = withdraw.Plus(diWithdraw) + k.SetFeePool(ctx, feePool) + k.SetDelegatorDistInfo(ctx, delInfo) return false } k.stakeKeeper.IterateDelegations(ctx, delAddr, operationAtDelegation) diff --git a/x/distribution/keeper/genesis.go b/x/distribution/keeper/genesis.go index dfc6109240..ab010f277e 100644 --- a/x/distribution/keeper/genesis.go +++ b/x/distribution/keeper/genesis.go @@ -40,10 +40,9 @@ func (k Keeper) GetAllDWs(ctx sdk.Context) (dws []types.DelegatorWithdrawInfo) { defer iterator.Close() for ; iterator.Valid(); iterator.Next() { - addr := iterator.Key()[1:] dw := types.DelegatorWithdrawInfo{ - DelegatorAddr: sdk.AccAddress{iterator.Key()}, - WithdrawAddr: sdk.AccAddress{iterator.Value()}, + DelegatorAddr: sdk.AccAddress(iterator.Key()), + WithdrawAddr: sdk.AccAddress(iterator.Value()), } dws = append(dws, dw) } diff --git a/x/distribution/hooks.go b/x/distribution/keeper/hooks.go similarity index 84% rename from x/distribution/hooks.go rename to x/distribution/keeper/hooks.go index 97be985f29..e846acd265 100644 --- a/x/distribution/hooks.go +++ b/x/distribution/keeper/hooks.go @@ -1,4 +1,4 @@ -package distribution +package keeper import ( sdk "github.com/cosmos/cosmos-sdk/types" @@ -10,11 +10,11 @@ func (k Keeper) onValidatorCreated(ctx sdk.Context, addr sdk.ValAddress) { height := ctx.BlockHeight() vdi := types.ValidatorDistInfo{ - OperatorAddr: addr, - GlobalWithdrawalHeight: height, - Pool: DecCoins{}, - PoolCommission: DecCoins{}, - DelAccum: NewTotalAccum(height), + OperatorAddr: addr, + FeePoolWithdrawalHeight: height, + Pool: types.DecCoins{}, + PoolCommission: types.DecCoins{}, + DelAccum: types.NewTotalAccum(height), } k.SetValidatorDistInfo(ctx, vdi) } @@ -68,20 +68,20 @@ type Hooks struct { func (k Keeper) ValidatorHooks() Hooks { return Hooks{k} } // nolint -func (h Hooks) OnValidatorCreated(ctx sdk.Context, addr sdk.VlAddress) { - v.k.onValidatorCreated(ctx, address) +func (h Hooks) OnValidatorCreated(ctx sdk.Context, addr sdk.ValAddress) { + h.k.onValidatorCreated(ctx, addr) } func (h Hooks) OnValidatorCommissionChange(ctx sdk.Context, addr sdk.ValAddress) { - v.k.onValidatorCommissionChange(ctx, address) + h.k.onValidatorCommissionChange(ctx, addr) } func (h Hooks) OnValidatorRemoved(ctx sdk.Context, addr sdk.ValAddress) { - v.k.onValidatorRemoved(ctx, address) + h.k.onValidatorRemoved(ctx, addr) } func (h Hooks) OnDelegationCreated(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) { h.k.onDelegationCreated(ctx, delAddr, valAddr) } func (h Hooks) OnDelegationSharesModified(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) { - d.k.onDelegationSharesModified(ctx, delAddr, valAddr) + h.k.onDelegationSharesModified(ctx, delAddr, valAddr) } func (h Hooks) OnDelegationRemoved(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) { h.k.onDelegationRemoved(ctx, delAddr, valAddr) diff --git a/x/distribution/keeper/keeper.go b/x/distribution/keeper/keeper.go index bbf4c15411..31ce345b22 100644 --- a/x/distribution/keeper/keeper.go +++ b/x/distribution/keeper/keeper.go @@ -91,11 +91,11 @@ func (k Keeper) SetProposerConsAddr(ctx sdk.Context, consAddr sdk.ConsAddress) { // nolint: errcheck func (k Keeper) GetCommunityTax(ctx sdk.Context) sdk.Dec { var communityTax sdk.Dec - keeper.ps.Get(ctx, ParamStoreKeyCommunityTax, &communityTax) + k.ps.Get(ctx, ParamStoreKeyCommunityTax, &communityTax) return communityTax } // nolint: errcheck func (k Keeper) setCommunityTax(ctx sdk.Context, communityTax sdk.Dec) { - keeper.ps.Set(ctx, ParamStoreKeyCommunityTax, &communityTax) + k.ps.Set(ctx, ParamStoreKeyCommunityTax, &communityTax) } diff --git a/x/distribution/keeper/key.go b/x/distribution/keeper/key.go index f6f93464f6..771343f017 100644 --- a/x/distribution/keeper/key.go +++ b/x/distribution/keeper/key.go @@ -23,7 +23,7 @@ func GetValidatorDistInfoKey(operatorAddr sdk.ValAddress) []byte { // gets the key for delegator distribution for a validator // VALUE: distribution/types.DelegatorDistInfo -func GetDelegationDistInfoKey(delAddr sdk.AccAddress, valOperatorAddr sdk.ValAddress) []byte { +func GetDelegationDistInfoKey(delAddr sdk.AccAddress, valAddr sdk.ValAddress) []byte { return append(GetDelegationDistInfosKey(delAddr), valAddr.Bytes()...) } diff --git a/x/distribution/keeper/validator.go b/x/distribution/keeper/validator.go index b8dece32cf..f8e04f0cb8 100644 --- a/x/distribution/keeper/validator.go +++ b/x/distribution/keeper/validator.go @@ -11,7 +11,7 @@ func (k Keeper) GetValidatorDistInfo(ctx sdk.Context, store := ctx.KVStore(k.storeKey) - b := store.Get(GetValidatorDistInfoKey(ctx, operatorAddr)) + b := store.Get(GetValidatorDistInfoKey(operatorAddr)) if b == nil { panic("Stored delegation-distribution info should not have been nil") } @@ -24,13 +24,13 @@ func (k Keeper) GetValidatorDistInfo(ctx sdk.Context, func (k Keeper) SetValidatorDistInfo(ctx sdk.Context, vdi types.ValidatorDistInfo) { store := ctx.KVStore(k.storeKey) b := k.cdc.MustMarshalBinary(vdi) - store.Set(GetValidatorDistInfoKey(ctx, vdi.OperatorAddr), b) + store.Set(GetValidatorDistInfoKey(vdi.OperatorAddr), b) } // remove a validator distribution info func (k Keeper) RemoveValidatorDistInfo(ctx sdk.Context, valAddr sdk.ValAddress) { store := ctx.KVStore(k.storeKey) - store.Delete(GetValidatorDistInfoKey(ctx, vdi.OperatorAddr)) + store.Delete(GetValidatorDistInfoKey(valAddr)) } // withdrawal all the validator rewards including the commission @@ -38,19 +38,20 @@ func (k Keeper) WithdrawValidatorRewardsAll(ctx sdk.Context, operatorAddr sdk.Va // withdraw self-delegation height := ctx.BlockHeight() - validator := k.GetValidator(ctx, operatorAddr) - accAddr := sdk.AccAddress{operatorAddr.Bytes()} + validator := k.stakeKeeper.GetValidator(ctx, operatorAddr) + accAddr := sdk.AccAddress(operatorAddr.Bytes()) withdraw := k.GetDelegatorRewardsAll(ctx, accAddr, height) // withdrawal validator commission rewards - pool := k.stakeKeeper.GetPool(ctx) + bondedTokens := k.stakeKeeper.TotalPower(ctx) valInfo := k.GetValidatorDistInfo(ctx, operatorAddr) feePool := k.GetFeePool(ctx) - feePool, commission := valInfo.WithdrawCommission(feePool, valInfo, height, pool.BondedTokens, - validator.Tokens, validator.Commission) - withdraw = withdraw.Add(commission) - k.SetFeePool(feePool) + valInfo, feePool, commission := valInfo.WithdrawCommission(feePool, height, bondedTokens, + validator.GetTokens(), validator.GetCommission()) + withdraw = withdraw.Plus(commission) + k.SetValidatorDistInfo(ctx, valInfo) + k.SetFeePool(ctx, feePool) - withdrawAddr := k.GetDelegatorWithdrawAddr(accAddr) - k.coinKeeper.AddCoins(withdrawAddr, withdraw.TruncateDecimal()) + withdrawAddr := k.GetDelegatorWithdrawAddr(ctx, accAddr) + k.bankKeeper.AddCoins(ctx, withdrawAddr, withdraw.TruncateDecimal()) } diff --git a/x/distribution/types/keepers.go b/x/distribution/types/keepers.go index b33e916b87..14ea30dc0f 100644 --- a/x/distribution/types/keepers.go +++ b/x/distribution/types/keepers.go @@ -6,8 +6,8 @@ import sdk "github.com/cosmos/cosmos-sdk/types" type StakeKeeper interface { IterateDelegations(ctx sdk.Context, delegator sdk.AccAddress, fn func(index int64, delegation sdk.Delegation) (stop bool)) - GetDelegation(ctx sdk.Context, delAddr sdk.AccAddress) sdk.Delegation - GetValidator(ctx sdk.Context, valAddr sdk.AccAddress) sdk.Validator + GetDelegation(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) sdk.Delegation + GetValidator(ctx sdk.Context, valAddr sdk.ValAddress) sdk.Validator GetValidatorFromConsAddr(ctx sdk.Context, consAddr sdk.ConsAddress) sdk.Validator TotalPower(ctx sdk.Context) sdk.Dec } diff --git a/x/gov/tally.go b/x/gov/tally.go index a756eaf926..55fd81da24 100644 --- a/x/gov/tally.go +++ b/x/gov/tally.go @@ -53,10 +53,10 @@ func tally(ctx sdk.Context, keeper Keeper, proposal Proposal) (passes bool, tall valAddrStr := delegation.GetValidator().String() if val, ok := currValidators[valAddrStr]; ok { - val.Minus = val.Minus.Add(delegation.GetBondShares()) + val.Minus = val.Minus.Add(delegation.GetShares()) currValidators[valAddrStr] = val - delegatorShare := delegation.GetBondShares().Quo(val.DelegatorShares) + delegatorShare := delegation.GetShares().Quo(val.DelegatorShares) votingPower := val.Power.Mul(delegatorShare) results[vote.Option] = results[vote.Option].Add(votingPower) diff --git a/x/stake/types/delegation.go b/x/stake/types/delegation.go index 38a94c3695..57ac70a571 100644 --- a/x/stake/types/delegation.go +++ b/x/stake/types/delegation.go @@ -89,7 +89,7 @@ var _ sdk.Delegation = Delegation{} // nolint - for sdk.Delegation func (d Delegation) GetDelegator() sdk.AccAddress { return d.DelegatorAddr } func (d Delegation) GetValidator() sdk.ValAddress { return d.ValidatorAddr } -func (d Delegation) GetBondShares() sdk.Dec { return d.Shares } +func (d Delegation) GetShares() sdk.Dec { return d.Shares } // HumanReadableString returns a human readable string representation of a // Delegation. An error is returned if the Delegation's delegator or validator