cosmos-sdk/x/distribution/keeper/hooks.go

107 lines
3.4 KiB
Go
Raw Normal View History

package keeper
2018-09-17 20:02:15 -07:00
2018-09-18 09:46:04 -07:00
import (
sdk "github.com/cosmos/cosmos-sdk/types"
2019-06-28 13:11:27 -07:00
"github.com/cosmos/cosmos-sdk/x/distribution/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
2018-09-18 09:46:04 -07:00
)
2018-09-18 09:46:04 -07:00
// Wrapper struct
type Hooks struct {
2018-09-17 20:02:15 -07:00
k Keeper
}
2019-06-28 13:11:27 -07:00
var _ stakingtypes.StakingHooks = Hooks{}
2018-10-09 18:59:07 -07:00
2019-01-16 13:38:05 -08:00
// Create new distribution hooks
2018-09-19 21:53:48 -07:00
func (k Keeper) Hooks() Hooks { return Hooks{k} }
2018-09-18 14:54:28 -07:00
// initialize validator distribution record
func (h Hooks) AfterValidatorCreated(ctx sdk.Context, valAddr sdk.ValAddress) {
2019-01-16 13:38:05 -08:00
val := h.k.stakingKeeper.Validator(ctx, valAddr)
h.k.initializeValidator(ctx, val)
2018-09-18 09:46:04 -07:00
}
// cleanup for after validator is removed
func (h Hooks) AfterValidatorRemoved(ctx sdk.Context, _ sdk.ConsAddress, valAddr sdk.ValAddress) {
// fetch outstanding
outstanding := h.k.GetValidatorOutstandingRewardsCoins(ctx, valAddr)
// force-withdraw commission
commission := h.k.GetValidatorAccumulatedCommission(ctx, valAddr).Commission
if !commission.IsZero() {
// subtract from outstanding
outstanding = outstanding.Sub(commission)
// split into integral & remainder
coins, remainder := commission.TruncateDecimal()
// remainder to community pool
feePool := h.k.GetFeePool(ctx)
feePool.CommunityPool = feePool.CommunityPool.Add(remainder...)
h.k.SetFeePool(ctx, feePool)
// add to validator account
if !coins.IsZero() {
accAddr := sdk.AccAddress(valAddr)
withdrawAddr := h.k.GetDelegatorWithdrawAddr(ctx, accAddr)
2019-06-28 13:11:27 -07:00
err := h.k.supplyKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, withdrawAddr, coins)
if err != nil {
panic(err)
}
}
}
// add outstanding to community pool
feePool := h.k.GetFeePool(ctx)
feePool.CommunityPool = feePool.CommunityPool.Add(outstanding...)
h.k.SetFeePool(ctx, feePool)
// delete outstanding
h.k.DeleteValidatorOutstandingRewards(ctx, valAddr)
// remove commission record
h.k.DeleteValidatorAccumulatedCommission(ctx, valAddr)
// clear slashes
h.k.DeleteValidatorSlashEvents(ctx, valAddr)
// clear historical rewards
h.k.DeleteValidatorHistoricalRewards(ctx, valAddr)
// clear current rewards
h.k.DeleteValidatorCurrentRewards(ctx, valAddr)
2018-09-18 09:46:04 -07:00
}
// increment period
func (h Hooks) BeforeDelegationCreated(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) {
2019-01-16 13:38:05 -08:00
val := h.k.stakingKeeper.Validator(ctx, valAddr)
h.k.IncrementValidatorPeriod(ctx, val)
2018-09-17 20:02:15 -07:00
}
// withdraw delegation rewards (which also increments period)
func (h Hooks) BeforeDelegationSharesModified(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) {
2019-01-16 13:38:05 -08:00
val := h.k.stakingKeeper.Validator(ctx, valAddr)
del := h.k.stakingKeeper.Delegation(ctx, delAddr, valAddr)
if _, err := h.k.withdrawDelegationRewards(ctx, val, del); err != nil {
2019-01-16 13:38:05 -08:00
panic(err)
}
2018-09-17 20:02:15 -07:00
}
// create new delegation period record
2019-01-16 13:38:05 -08:00
func (h Hooks) AfterDelegationModified(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) {
h.k.initializeDelegation(ctx, valAddr, delAddr)
2018-09-13 23:35:02 -07:00
}
// record the slash event
2019-01-16 13:38:05 -08:00
func (h Hooks) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, fraction sdk.Dec) {
h.k.updateValidatorSlashFraction(ctx, valAddr, fraction)
2018-10-22 19:48:28 -07:00
}
// nolint - unused hooks
func (h Hooks) BeforeValidatorModified(_ sdk.Context, _ sdk.ValAddress) {}
func (h Hooks) AfterValidatorBonded(_ sdk.Context, _ sdk.ConsAddress, _ sdk.ValAddress) {}
func (h Hooks) AfterValidatorBeginUnbonding(_ sdk.Context, _ sdk.ConsAddress, _ sdk.ValAddress) {}
func (h Hooks) BeforeDelegationRemoved(_ sdk.Context, _ sdk.AccAddress, _ sdk.ValAddress) {}