2018-09-19 19:54:46 -07:00
|
|
|
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-04 13:19:37 -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
|
|
|
|
2019-06-10 02:57:38 -07:00
|
|
|
// initialize validator distribution record
|
2019-01-08 21:28:46 -08:00
|
|
|
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
|
|
|
}
|
2019-06-04 15:06:58 -07:00
|
|
|
|
2019-06-10 02:57:38 -07:00
|
|
|
// cleanup for after validator is removed
|
2019-01-08 21:28:46 -08:00
|
|
|
func (h Hooks) AfterValidatorRemoved(ctx sdk.Context, _ sdk.ConsAddress, valAddr sdk.ValAddress) {
|
2019-03-06 10:54:12 -08:00
|
|
|
// fetch outstanding
|
2020-02-11 06:58:37 -08:00
|
|
|
outstanding := h.k.GetValidatorOutstandingRewardsCoins(ctx, valAddr)
|
2019-03-06 10:54:12 -08:00
|
|
|
|
2019-01-23 03:37:03 -08:00
|
|
|
// force-withdraw commission
|
2020-02-11 06:58:37 -08:00
|
|
|
commission := h.k.GetValidatorAccumulatedCommission(ctx, valAddr).Commission
|
2019-01-23 03:37:03 -08:00
|
|
|
if !commission.IsZero() {
|
2019-03-06 10:54:12 -08:00
|
|
|
// subtract from outstanding
|
|
|
|
outstanding = outstanding.Sub(commission)
|
|
|
|
|
|
|
|
// split into integral & remainder
|
2019-01-23 03:37:03 -08:00
|
|
|
coins, remainder := commission.TruncateDecimal()
|
|
|
|
|
|
|
|
// remainder to community pool
|
|
|
|
feePool := h.k.GetFeePool(ctx)
|
2020-01-03 12:44:53 -08:00
|
|
|
feePool.CommunityPool = feePool.CommunityPool.Add(remainder...)
|
2019-01-23 03:37:03 -08:00
|
|
|
h.k.SetFeePool(ctx, feePool)
|
|
|
|
|
|
|
|
// add to validator account
|
2019-02-27 13:09:26 -08:00
|
|
|
if !coins.IsZero() {
|
2019-03-06 10:54:12 -08:00
|
|
|
|
2019-02-27 13:09:26 -08:00
|
|
|
accAddr := sdk.AccAddress(valAddr)
|
|
|
|
withdrawAddr := h.k.GetDelegatorWithdrawAddr(ctx, accAddr)
|
2020-04-20 08:22:12 -07:00
|
|
|
err := h.k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, withdrawAddr, coins)
|
2019-06-28 13:11:27 -07:00
|
|
|
if err != nil {
|
2019-02-27 13:09:26 -08:00
|
|
|
panic(err)
|
|
|
|
}
|
2019-01-23 03:37:03 -08:00
|
|
|
}
|
|
|
|
}
|
2019-03-06 10:54:12 -08:00
|
|
|
|
|
|
|
// add outstanding to community pool
|
|
|
|
feePool := h.k.GetFeePool(ctx)
|
2020-01-03 12:44:53 -08:00
|
|
|
feePool.CommunityPool = feePool.CommunityPool.Add(outstanding...)
|
2019-03-06 10:54:12 -08:00
|
|
|
h.k.SetFeePool(ctx, feePool)
|
|
|
|
|
|
|
|
// delete outstanding
|
|
|
|
h.k.DeleteValidatorOutstandingRewards(ctx, valAddr)
|
|
|
|
|
2019-01-23 03:37:03 -08:00
|
|
|
// 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
|
|
|
}
|
2019-06-04 15:06:58 -07:00
|
|
|
|
2019-06-10 02:57:38 -07:00
|
|
|
// increment period
|
2019-01-08 21:28:46 -08:00
|
|
|
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)
|
2020-03-05 06:00:45 -08:00
|
|
|
h.k.IncrementValidatorPeriod(ctx, val)
|
2018-09-17 20:02:15 -07:00
|
|
|
}
|
2019-06-04 15:06:58 -07:00
|
|
|
|
2019-06-10 02:57:38 -07:00
|
|
|
// withdraw delegation rewards (which also increments period)
|
2019-01-08 21:28:46 -08:00
|
|
|
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)
|
2019-04-26 16:11:45 -07:00
|
|
|
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
|
|
|
}
|
2019-06-04 15:06:58 -07:00
|
|
|
|
2019-06-10 02:57:38 -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
|
|
|
}
|
2019-06-04 15:06:58 -07:00
|
|
|
|
2019-06-10 02:57:38 -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
|
|
|
}
|
2019-06-04 15:06:58 -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) {}
|