From 608ef3c78884fcc8a2641fdd76edcaa1c7cedd77 Mon Sep 17 00:00:00 2001 From: ValarDragon Date: Sat, 19 Feb 2022 14:12:22 -0800 Subject: [PATCH 1/3] Update all the slashing hooks --- x/distribution/keeper/hooks.go | 10 +--------- x/slashing/keeper/hooks.go | 10 +--------- x/staking/keeper/hooks.go | 20 ++------------------ x/staking/keeper/slash.go | 6 +----- x/staking/types/expected_keepers.go | 6 +----- x/staking/types/hooks.go | 18 ++---------------- 6 files changed, 8 insertions(+), 62 deletions(-) diff --git a/x/distribution/keeper/hooks.go b/x/distribution/keeper/hooks.go index 86c812e25dee..ac9c1b3005a9 100644 --- a/x/distribution/keeper/hooks.go +++ b/x/distribution/keeper/hooks.go @@ -97,18 +97,10 @@ func (h Hooks) AfterDelegationModified(ctx sdk.Context, delAddr sdk.AccAddress, } // record the slash event -func (h Hooks) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, fraction sdk.Dec) { +func (h Hooks) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, slashHeight int64, fraction sdk.Dec) { 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 8c1c944fb0dd..abb3cb41ccd6 100644 --- a/x/slashing/keeper/hooks.go +++ b/x/slashing/keeper/hooks.go @@ -7,7 +7,6 @@ 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,11 +75,4 @@ func (h Hooks) BeforeDelegationCreated(_ sdk.Context, _ sdk.AccAddress, _ sdk.Va func (h Hooks) BeforeDelegationSharesModified(_ sdk.Context, _ sdk.AccAddress, _ sdk.ValAddress) {} 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) { -} +func (h Hooks) BeforeValidatorSlashed(_ sdk.Context, _ sdk.ValAddress, _ int64, _ sdk.Dec) {} diff --git a/x/staking/keeper/hooks.go b/x/staking/keeper/hooks.go index 6bf2c0afbc42..5d17d2403ec1 100644 --- a/x/staking/keeper/hooks.go +++ b/x/staking/keeper/hooks.go @@ -72,24 +72,8 @@ func (k Keeper) AfterDelegationModified(ctx sdk.Context, delAddr sdk.AccAddress, } // BeforeValidatorSlashed - call hook if registered -func (k Keeper) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, fraction sdk.Dec) { +func (k Keeper) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, slashHeight int64, fraction sdk.Dec) { if k.hooks != nil { - 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) + k.hooks.BeforeValidatorSlashed(ctx, valAddr, slashHeight, fraction) } } diff --git a/x/staking/keeper/slash.go b/x/staking/keeper/slash.go index b887bee35acd..7d7f953d6376 100644 --- a/x/staking/keeper/slash.go +++ b/x/staking/keeper/slash.go @@ -114,7 +114,7 @@ func (k Keeper) Slash(ctx sdk.Context, consAddr sdk.ConsAddress, infractionHeigh effectiveFraction = sdk.OneDec() } // call the before-slashed hook - k.BeforeValidatorSlashed(ctx, operatorAddress, effectiveFraction) + k.BeforeValidatorSlashed(ctx, operatorAddress, infractionHeight, effectiveFraction) } // Deduct from validator's bonded tokens and update the validator. @@ -165,8 +165,6 @@ 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() @@ -221,8 +219,6 @@ 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() diff --git a/x/staking/types/expected_keepers.go b/x/staking/types/expected_keepers.go index 960fea031759..b31d9529d23e 100644 --- a/x/staking/types/expected_keepers.go +++ b/x/staking/types/expected_keepers.go @@ -100,9 +100,5 @@ type StakingHooks interface { BeforeDelegationSharesModified(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) // Must be called when a delegation's shares are modified 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) + BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, infractionHeight int64, slashFactor sdk.Dec) } diff --git a/x/staking/types/hooks.go b/x/staking/types/hooks.go index b8c746d095ec..0c94c0968bc4 100644 --- a/x/staking/types/hooks.go +++ b/x/staking/types/hooks.go @@ -56,22 +56,8 @@ func (h MultiStakingHooks) AfterDelegationModified(ctx sdk.Context, delAddr sdk. h[i].AfterDelegationModified(ctx, delAddr, valAddr) } } -func (h MultiStakingHooks) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, fraction sdk.Dec) { +func (h MultiStakingHooks) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, infractionHeight int64, slashFactor sdk.Dec) { for i := range h { - 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) + h[i].BeforeValidatorSlashed(ctx, valAddr, infractionHeight, slashFactor) } } From 299c024606a33d58883c82d797a35a0af0beb345 Mon Sep 17 00:00:00 2001 From: ValarDragon Date: Sat, 19 Feb 2022 14:52:05 -0800 Subject: [PATCH 2/3] Fix an expected keeper --- x/distribution/types/expected_keepers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/x/distribution/types/expected_keepers.go b/x/distribution/types/expected_keepers.go index ac9ebc3a4eea..6a991f0b16e6 100644 --- a/x/distribution/types/expected_keepers.go +++ b/x/distribution/types/expected_keepers.go @@ -75,5 +75,5 @@ type StakingHooks interface { BeforeDelegationCreated(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) // Must be called when a delegation is created BeforeDelegationSharesModified(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) // Must be called when a delegation's shares are modified AfterDelegationModified(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) - BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, fraction sdk.Dec) + BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, infractionHeight int64, fraction sdk.Dec) } From 689410c34e4e4ee26cd73bd54719a17e44882797 Mon Sep 17 00:00:00 2001 From: ValarDragon Date: Sat, 19 Feb 2022 17:05:12 -0800 Subject: [PATCH 3/3] Add another needed parameter to slashing hook. (Somewhat gross, we should come back and see if we can delete, by pushing into distribution) --- x/distribution/keeper/hooks.go | 7 +++++-- x/slashing/keeper/hooks.go | 3 ++- x/staking/keeper/hooks.go | 4 ++-- x/staking/keeper/slash.go | 2 +- x/staking/types/expected_keepers.go | 2 +- x/staking/types/hooks.go | 4 ++-- 6 files changed, 13 insertions(+), 9 deletions(-) diff --git a/x/distribution/keeper/hooks.go b/x/distribution/keeper/hooks.go index ac9c1b3005a9..7ad3ae80e92c 100644 --- a/x/distribution/keeper/hooks.go +++ b/x/distribution/keeper/hooks.go @@ -97,8 +97,11 @@ func (h Hooks) AfterDelegationModified(ctx sdk.Context, delAddr sdk.AccAddress, } // record the slash event -func (h Hooks) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, slashHeight int64, fraction sdk.Dec) { - h.k.updateValidatorSlashFraction(ctx, valAddr, fraction) +// distribution only cares about the 'effective slash factor', not the 'actual slash factor'. +// This is due to {details} of what it wants to be tracking. +// TODO: Can effective slash factor be calculated in distribution? Would simplify the hook. +func (h Hooks) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, slashHeight int64, slashFactor sdk.Dec, effectiveSlashFactor sdk.Dec) { + h.k.updateValidatorSlashFraction(ctx, valAddr, effectiveSlashFactor) } func (h Hooks) BeforeValidatorModified(_ sdk.Context, _ sdk.ValAddress) {} diff --git a/x/slashing/keeper/hooks.go b/x/slashing/keeper/hooks.go index abb3cb41ccd6..f4c29be47f9d 100644 --- a/x/slashing/keeper/hooks.go +++ b/x/slashing/keeper/hooks.go @@ -75,4 +75,5 @@ func (h Hooks) BeforeDelegationCreated(_ sdk.Context, _ sdk.AccAddress, _ sdk.Va func (h Hooks) BeforeDelegationSharesModified(_ sdk.Context, _ sdk.AccAddress, _ sdk.ValAddress) {} 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, _ int64, _ sdk.Dec) {} +func (h Hooks) BeforeValidatorSlashed(_ sdk.Context, _ sdk.ValAddress, _ int64, _ sdk.Dec, _ sdk.Dec) { +} diff --git a/x/staking/keeper/hooks.go b/x/staking/keeper/hooks.go index 5d17d2403ec1..85bd1559cf50 100644 --- a/x/staking/keeper/hooks.go +++ b/x/staking/keeper/hooks.go @@ -72,8 +72,8 @@ func (k Keeper) AfterDelegationModified(ctx sdk.Context, delAddr sdk.AccAddress, } // BeforeValidatorSlashed - call hook if registered -func (k Keeper) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, slashHeight int64, fraction sdk.Dec) { +func (k Keeper) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, slashHeight int64, slashFactor sdk.Dec, effectiveSlashFactor sdk.Dec) { if k.hooks != nil { - k.hooks.BeforeValidatorSlashed(ctx, valAddr, slashHeight, fraction) + k.hooks.BeforeValidatorSlashed(ctx, valAddr, slashHeight, slashFactor, effectiveSlashFactor) } } diff --git a/x/staking/keeper/slash.go b/x/staking/keeper/slash.go index 7d7f953d6376..e141d8c572aa 100644 --- a/x/staking/keeper/slash.go +++ b/x/staking/keeper/slash.go @@ -114,7 +114,7 @@ func (k Keeper) Slash(ctx sdk.Context, consAddr sdk.ConsAddress, infractionHeigh effectiveFraction = sdk.OneDec() } // call the before-slashed hook - k.BeforeValidatorSlashed(ctx, operatorAddress, infractionHeight, effectiveFraction) + k.BeforeValidatorSlashed(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 b31d9529d23e..b778dec3c6e1 100644 --- a/x/staking/types/expected_keepers.go +++ b/x/staking/types/expected_keepers.go @@ -100,5 +100,5 @@ type StakingHooks interface { BeforeDelegationSharesModified(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) // Must be called when a delegation's shares are modified 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) + BeforeValidatorSlashed(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 0c94c0968bc4..6ea4ee35b702 100644 --- a/x/staking/types/hooks.go +++ b/x/staking/types/hooks.go @@ -56,8 +56,8 @@ func (h MultiStakingHooks) AfterDelegationModified(ctx sdk.Context, delAddr sdk. h[i].AfterDelegationModified(ctx, delAddr, valAddr) } } -func (h MultiStakingHooks) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, infractionHeight int64, slashFactor sdk.Dec) { +func (h MultiStakingHooks) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, infractionHeight int64, slashFactor sdk.Dec, effectiveSlashFactor sdk.Dec) { for i := range h { - h[i].BeforeValidatorSlashed(ctx, valAddr, infractionHeight, slashFactor) + h[i].BeforeValidatorSlashed(ctx, valAddr, infractionHeight, slashFactor, effectiveSlashFactor) } }