From d81fb5657a15adedf900bc605f13932b4c219aee Mon Sep 17 00:00:00 2001 From: antstalepresh Date: Thu, 10 Mar 2022 03:46:35 +1000 Subject: [PATCH] add after validator slash hook --- x/distribution/keeper/hooks.go | 3 +++ x/slashing/keeper/hooks.go | 2 ++ x/staking/keeper/hooks.go | 7 +++++++ x/staking/keeper/slash.go | 1 + x/staking/types/expected_keepers.go | 1 + x/staking/types/hooks.go | 5 +++++ 6 files changed, 19 insertions(+) diff --git a/x/distribution/keeper/hooks.go b/x/distribution/keeper/hooks.go index 7ad3ae80e92c..08fce94d8650 100644 --- a/x/distribution/keeper/hooks.go +++ b/x/distribution/keeper/hooks.go @@ -104,6 +104,9 @@ func (h Hooks) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, s h.k.updateValidatorSlashFraction(ctx, valAddr, effectiveSlashFactor) } +func (h Hooks) AfterValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, slashHeight int64, slashFactor sdk.Dec, effectiveSlashFactor sdk.Dec) { +} + 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) {} diff --git a/x/slashing/keeper/hooks.go b/x/slashing/keeper/hooks.go index f4c29be47f9d..26e14b947a60 100644 --- a/x/slashing/keeper/hooks.go +++ b/x/slashing/keeper/hooks.go @@ -77,3 +77,5 @@ func (h Hooks) BeforeDelegationRemoved(_ sdk.Context, _ sdk.AccAddress, _ sdk.Va func (h Hooks) AfterDelegationModified(_ sdk.Context, _ sdk.AccAddress, _ sdk.ValAddress) {} func (h Hooks) BeforeValidatorSlashed(_ sdk.Context, _ sdk.ValAddress, _ int64, _ sdk.Dec, _ sdk.Dec) { } +func (h Hooks) AfterValidatorSlashed(_ sdk.Context, _ sdk.ValAddress, _ int64, _ sdk.Dec, _ sdk.Dec) { +} diff --git a/x/staking/keeper/hooks.go b/x/staking/keeper/hooks.go index 85bd1559cf50..ccffe4bc2c0a 100644 --- a/x/staking/keeper/hooks.go +++ b/x/staking/keeper/hooks.go @@ -77,3 +77,10 @@ func (k Keeper) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, k.hooks.BeforeValidatorSlashed(ctx, valAddr, slashHeight, slashFactor, effectiveSlashFactor) } } + +// AfterValidatorSlashed - call hook if registered +func (k Keeper) AfterValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, slashHeight int64, slashFactor sdk.Dec, effectiveSlashFactor sdk.Dec) { + if k.hooks != nil { + k.hooks.AfterValidatorSlashed(ctx, valAddr, slashHeight, slashFactor, effectiveSlashFactor) + } +} diff --git a/x/staking/keeper/slash.go b/x/staking/keeper/slash.go index e141d8c572aa..b28a9cf60a32 100644 --- a/x/staking/keeper/slash.go +++ b/x/staking/keeper/slash.go @@ -115,6 +115,7 @@ func (k Keeper) Slash(ctx sdk.Context, consAddr sdk.ConsAddress, infractionHeigh } // call the before-slashed hook k.BeforeValidatorSlashed(ctx, operatorAddress, infractionHeight, slashFactor, effectiveFraction) + defer k.AfterValidatorSlashed(ctx, operatorAddress, infractionHeight, slashFactor, effectiveFraction) } // Deduct from validator's bonded tokens and update the validator. diff --git a/x/staking/types/expected_keepers.go b/x/staking/types/expected_keepers.go index b778dec3c6e1..15d673d5e210 100644 --- a/x/staking/types/expected_keepers.go +++ b/x/staking/types/expected_keepers.go @@ -101,4 +101,5 @@ type StakingHooks interface { BeforeDelegationRemoved(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) // Must be called when a delegation is removed AfterDelegationModified(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, infractionHeight int64, slashFactor sdk.Dec, effectiveSlashFactor sdk.Dec) + AfterValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, infractionHeight int64, slashFactor sdk.Dec, effectiveSlashFactor sdk.Dec) } diff --git a/x/staking/types/hooks.go b/x/staking/types/hooks.go index 6ea4ee35b702..1c79701267a8 100644 --- a/x/staking/types/hooks.go +++ b/x/staking/types/hooks.go @@ -61,3 +61,8 @@ func (h MultiStakingHooks) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.V h[i].BeforeValidatorSlashed(ctx, valAddr, infractionHeight, slashFactor, effectiveSlashFactor) } } +func (h MultiStakingHooks) AfterValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, infractionHeight int64, slashFactor sdk.Dec, effectiveSlashFactor sdk.Dec) { + for i := range h { + h[i].AfterValidatorSlashed(ctx, valAddr, infractionHeight, slashFactor, effectiveSlashFactor) + } +}