diff --git a/x/distribution/keeper/hooks.go b/x/distribution/keeper/hooks.go index 1d12fcc55513..86c812e25dee 100644 --- a/x/distribution/keeper/hooks.go +++ b/x/distribution/keeper/hooks.go @@ -101,6 +101,14 @@ func (h Hooks) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, f h.k.updateValidatorSlashFraction(ctx, valAddr, fraction) } +func (h Hooks) BeforeSlashingUnbondingDelegation(ctx sdk.Context, unbondingDelegation stakingtypes.UnbondingDelegation, + infractionHeight int64, slashFactor sdk.Dec) { +} + +func (h Hooks) BeforeSlashingRedelegation(ctx sdk.Context, srcValidator stakingtypes.Validator, redelegation stakingtypes.Redelegation, + infractionHeight int64, slashFactor 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 e3a00e9f5556..8c1c944fb0dd 100644 --- a/x/slashing/keeper/hooks.go +++ b/x/slashing/keeper/hooks.go @@ -7,6 +7,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/x/slashing/types" + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" ) func (k Keeper) AfterValidatorBonded(ctx sdk.Context, address sdk.ConsAddress, _ sdk.ValAddress) { @@ -76,3 +77,10 @@ func (h Hooks) BeforeDelegationSharesModified(_ sdk.Context, _ sdk.AccAddress, _ func (h Hooks) BeforeDelegationRemoved(_ sdk.Context, _ sdk.AccAddress, _ sdk.ValAddress) {} func (h Hooks) AfterDelegationModified(_ sdk.Context, _ sdk.AccAddress, _ sdk.ValAddress) {} func (h Hooks) BeforeValidatorSlashed(_ sdk.Context, _ sdk.ValAddress, _ sdk.Dec) {} +func (h Hooks) BeforeSlashingUnbondingDelegation(ctx sdk.Context, unbondingDelegation stakingtypes.UnbondingDelegation, + infractionHeight int64, slashFactor sdk.Dec) { +} + +func (h Hooks) BeforeSlashingRedelegation(ctx sdk.Context, srcValidator stakingtypes.Validator, redelegation stakingtypes.Redelegation, + infractionHeight int64, slashFactor sdk.Dec) { +} diff --git a/x/staking/keeper/hooks.go b/x/staking/keeper/hooks.go index b8f25908babd..6bf2c0afbc42 100644 --- a/x/staking/keeper/hooks.go +++ b/x/staking/keeper/hooks.go @@ -77,3 +77,19 @@ func (k Keeper) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, k.hooks.BeforeValidatorSlashed(ctx, valAddr, fraction) } } + +// BeforeSlashingUnbondingDelegation - call hook if registered +func (k Keeper) BeforeSlashingUnbondingDelegation(ctx sdk.Context, unbondingDelegation types.UnbondingDelegation, + infractionHeight int64, slashFactor sdk.Dec) { + if k.hooks != nil { + k.hooks.BeforeSlashingUnbondingDelegation(ctx, unbondingDelegation, infractionHeight, slashFactor) + } +} + +// BeforeSlashingRedelegation - call hook if registered +func (k Keeper) BeforeSlashingRedelegation(ctx sdk.Context, srcValidator types.Validator, redelegation types.Redelegation, + infractionHeight int64, slashFactor sdk.Dec) { + if k.hooks != nil { + k.hooks.BeforeSlashingRedelegation(ctx, srcValidator, redelegation, infractionHeight, slashFactor) + } +} diff --git a/x/staking/keeper/slash.go b/x/staking/keeper/slash.go index 7b88fbfca17c..b887bee35acd 100644 --- a/x/staking/keeper/slash.go +++ b/x/staking/keeper/slash.go @@ -165,6 +165,9 @@ func (k Keeper) Unjail(ctx sdk.Context, consAddr sdk.ConsAddress) { // insufficient stake remaining) func (k Keeper) SlashUnbondingDelegation(ctx sdk.Context, unbondingDelegation types.UnbondingDelegation, infractionHeight int64, slashFactor sdk.Dec) (totalSlashAmount sdk.Int) { + // hook before slashing unbonding delegation + k.BeforeSlashingUnbondingDelegation(ctx, unbondingDelegation, infractionHeight, slashFactor) + now := ctx.BlockHeader().Time totalSlashAmount = sdk.ZeroInt() burnedAmount := sdk.ZeroInt() @@ -218,6 +221,9 @@ func (k Keeper) SlashUnbondingDelegation(ctx sdk.Context, unbondingDelegation ty // NOTE this is only slashing for prior infractions from the source validator func (k Keeper) SlashRedelegation(ctx sdk.Context, srcValidator types.Validator, redelegation types.Redelegation, infractionHeight int64, slashFactor sdk.Dec) (totalSlashAmount sdk.Int) { + // hook before slashing redelegation + k.BeforeSlashingRedelegation(ctx, srcValidator, redelegation, infractionHeight, slashFactor) + now := ctx.BlockHeader().Time totalSlashAmount = sdk.ZeroInt() bondedBurnedAmount, notBondedBurnedAmount := sdk.ZeroInt(), sdk.ZeroInt() diff --git a/x/staking/types/expected_keepers.go b/x/staking/types/expected_keepers.go index 313db78e7b1c..960fea031759 100644 --- a/x/staking/types/expected_keepers.go +++ b/x/staking/types/expected_keepers.go @@ -101,4 +101,8 @@ 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, fraction sdk.Dec) + BeforeSlashingUnbondingDelegation(ctx sdk.Context, unbondingDelegation UnbondingDelegation, + infractionHeight int64, slashFactor sdk.Dec) + BeforeSlashingRedelegation(ctx sdk.Context, srcValidator Validator, redelegation Redelegation, + infractionHeight int64, slashFactor sdk.Dec) } diff --git a/x/staking/types/hooks.go b/x/staking/types/hooks.go index 694caca5405a..b8c746d095ec 100644 --- a/x/staking/types/hooks.go +++ b/x/staking/types/hooks.go @@ -61,3 +61,17 @@ func (h MultiStakingHooks) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.V h[i].BeforeValidatorSlashed(ctx, valAddr, fraction) } } + +func (h MultiStakingHooks) BeforeSlashingUnbondingDelegation(ctx sdk.Context, unbondingDelegation UnbondingDelegation, + infractionHeight int64, slashFactor sdk.Dec) { + for i := range h { + h[i].BeforeSlashingUnbondingDelegation(ctx, unbondingDelegation, infractionHeight, slashFactor) + } +} + +func (h MultiStakingHooks) BeforeSlashingRedelegation(ctx sdk.Context, srcValidator Validator, redelegation Redelegation, + infractionHeight int64, slashFactor sdk.Dec) { + for i := range h { + h[i].BeforeSlashingRedelegation(ctx, srcValidator, redelegation, infractionHeight, slashFactor) + } +}