Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hooks for superfluid #51

Merged
merged 1 commit into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions x/distribution/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Expand Down
8 changes: 8 additions & 0 deletions x/slashing/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
}
16 changes: 16 additions & 0 deletions x/staking/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
6 changes: 6 additions & 0 deletions x/staking/keeper/slash.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
4 changes: 4 additions & 0 deletions x/staking/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
14 changes: 14 additions & 0 deletions x/staking/types/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}