Skip to content

Commit

Permalink
Update slashing hooks (cosmos#116)
Browse files Browse the repository at this point in the history
* Update all the slashing hooks

* Fix an expected keeper

* Add another needed parameter to slashing hook. (Somewhat gross, we should come back and see if we can delete, by pushing into distribution)
  • Loading branch information
ValarDragon authored and roysc committed Jul 10, 2022
1 parent 462c448 commit dd8fa1b
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 81 deletions.
17 changes: 5 additions & 12 deletions x/distribution/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,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, fraction sdk.Dec) error {
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) error {
h.k.updateValidatorSlashFraction(ctx, valAddr, effectiveSlashFactor)
return nil
}

Expand All @@ -121,13 +124,3 @@ func (h Hooks) AfterValidatorBeginUnbonding(_ sdk.Context, _ sdk.ConsAddress, _
func (h Hooks) BeforeDelegationRemoved(_ sdk.Context, _ sdk.AccAddress, _ sdk.ValAddress) error {
return nil
}

func (h Hooks) BeforeSlashingUnbondingDelegation(ctx sdk.Context, unbondingDelegation stakingtypes.UnbondingDelegation,
infractionHeight int64, slashFactor sdk.Dec) error {
return nil
}

func (h Hooks) BeforeSlashingRedelegation(ctx sdk.Context, srcValidator stakingtypes.Validator, redelegation stakingtypes.Redelegation,
infractionHeight int64, slashFactor sdk.Dec) error {
return nil
}
1 change: 1 addition & 0 deletions x/distribution/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@ type StakingKeeper interface {
type StakingHooks interface {
AfterValidatorCreated(ctx sdk.Context, valAddr sdk.ValAddress) // Must be called when a validator is created
AfterDelegationModified(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress)
BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, infractionHeight int64, fraction sdk.Dec)
}
13 changes: 1 addition & 12 deletions x/slashing/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) error {
Expand Down Expand Up @@ -99,16 +98,6 @@ func (h Hooks) AfterDelegationModified(_ sdk.Context, _ sdk.AccAddress, _ sdk.Va
return nil
}

func (h Hooks) BeforeValidatorSlashed(_ sdk.Context, _ sdk.ValAddress, _ sdk.Dec) error {
return nil
}

func (h Hooks) BeforeSlashingUnbondingDelegation(ctx sdk.Context, unbondingDelegation stakingtypes.UnbondingDelegation,
infractionHeight int64, slashFactor sdk.Dec) error {
return nil
}

func (h Hooks) BeforeSlashingRedelegation(ctx sdk.Context, srcValidator stakingtypes.Validator, redelegation stakingtypes.Redelegation,
infractionHeight int64, slashFactor sdk.Dec) error {
func (h Hooks) BeforeValidatorSlashed(_ sdk.Context, _ sdk.ValAddress, _ int64, _ sdk.Dec, _ sdk.Dec) error {
return nil
}
26 changes: 2 additions & 24 deletions x/staking/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,31 +81,9 @@ 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) error {
func (k Keeper) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, slashHeight int64, slashFactor sdk.Dec, effectiveSlashFactor sdk.Dec) error {
if k.hooks != nil {
return k.hooks.BeforeValidatorSlashed(ctx, valAddr, fraction)
}
return nil
}

// BeforeSlashingUnbondingDelegation - call hook if registered
func (k Keeper) BeforeSlashingUnbondingDelegation(ctx sdk.Context, unbondingDelegation types.UnbondingDelegation,
infractionHeight int64, slashFactor sdk.Dec) error {
if k.hooks != nil {
if err := k.hooks.BeforeSlashingUnbondingDelegation(ctx, unbondingDelegation, infractionHeight, slashFactor); err != nil {
return err
}
}
return nil
}

// BeforeSlashingRedelegation - call hook if registered
func (k Keeper) BeforeSlashingRedelegation(ctx sdk.Context, srcValidator types.Validator, redelegation types.Redelegation,
infractionHeight int64, slashFactor sdk.Dec) error {
if k.hooks != nil {
if err := k.hooks.BeforeSlashingRedelegation(ctx, srcValidator, redelegation, infractionHeight, slashFactor); err != nil {
return err
}
return k.hooks.BeforeValidatorSlashed(ctx, valAddr, slashHeight, slashFactor, effectiveSlashFactor)
}
return nil
}
6 changes: 1 addition & 5 deletions x/staking/keeper/slash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, slashFactor, effectiveFraction)
}

// Deduct from validator's bonded tokens and update the validator.
Expand Down Expand Up @@ -167,8 +167,6 @@ func (k Keeper) Unjail(ctx sdk.Context, consAddr sdk.ConsAddress) {
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()
Expand Down Expand Up @@ -223,8 +221,6 @@ func (k Keeper) SlashUnbondingDelegation(ctx sdk.Context, unbondingDelegation ty
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()
Expand Down
6 changes: 1 addition & 5 deletions x/staking/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,5 @@ type StakingHooks interface {
BeforeDelegationSharesModified(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error // Must be called when a delegation's shares are modified
BeforeDelegationRemoved(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error // Must be called when a delegation is removed
AfterDelegationModified(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error
BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, fraction sdk.Dec) error
BeforeSlashingUnbondingDelegation(ctx sdk.Context, unbondingDelegation UnbondingDelegation,
infractionHeight int64, slashFactor sdk.Dec) error
BeforeSlashingRedelegation(ctx sdk.Context, srcValidator Validator, redelegation Redelegation,
infractionHeight int64, slashFactor sdk.Dec) error
BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, infractionHeight int64, slashFactor sdk.Dec, effectiveSlashFactor sdk.Dec) error
}
25 changes: 2 additions & 23 deletions x/staking/types/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,30 +94,9 @@ func (h MultiStakingHooks) AfterDelegationModified(ctx sdk.Context, delAddr sdk.
}
return nil
}

func (h MultiStakingHooks) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, fraction sdk.Dec) error {
for i := range h {
if err := h[i].BeforeValidatorSlashed(ctx, valAddr, fraction); err != nil {
return err
}
}
return nil
}

func (h MultiStakingHooks) BeforeSlashingUnbondingDelegation(ctx sdk.Context, unbondingDelegation UnbondingDelegation,
infractionHeight int64, slashFactor sdk.Dec) error {
for i := range h {
if err := h[i].BeforeSlashingUnbondingDelegation(ctx, unbondingDelegation, infractionHeight, slashFactor); err != nil {
return err
}
}
return nil
}

func (h MultiStakingHooks) BeforeSlashingRedelegation(ctx sdk.Context, srcValidator Validator, redelegation Redelegation,
infractionHeight int64, slashFactor sdk.Dec) error {
func (h MultiStakingHooks) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, infractionHeight int64, slashFactor sdk.Dec, effectiveSlashFactor sdk.Dec) error {
for i := range h {
if err := h[i].BeforeSlashingRedelegation(ctx, srcValidator, redelegation, infractionHeight, slashFactor); err != nil {
if err := h[i].BeforeValidatorSlashed(ctx, valAddr, infractionHeight, slashFactor, effectiveSlashFactor); err != nil {
return err
}
}
Expand Down

0 comments on commit dd8fa1b

Please sign in to comment.