github.com/cosmos/cosmos-sdk@v0.50.10/x/slashing/keeper/hooks.go (about) 1 package keeper 2 3 import ( 4 "context" 5 "time" 6 7 "github.com/cometbft/cometbft/crypto" 8 9 sdkmath "cosmossdk.io/math" 10 11 sdk "github.com/cosmos/cosmos-sdk/types" 12 "github.com/cosmos/cosmos-sdk/x/slashing/types" 13 ) 14 15 var _ types.StakingHooks = Hooks{} 16 17 // Hooks wrapper struct for slashing keeper 18 type Hooks struct { 19 k Keeper 20 } 21 22 // Return the slashing hooks 23 func (k Keeper) Hooks() Hooks { 24 return Hooks{k} 25 } 26 27 // AfterValidatorBonded updates the signing info start height or create a new signing info 28 func (h Hooks) AfterValidatorBonded(ctx context.Context, consAddr sdk.ConsAddress, valAddr sdk.ValAddress) error { 29 sdkCtx := sdk.UnwrapSDKContext(ctx) 30 signingInfo, err := h.k.GetValidatorSigningInfo(ctx, consAddr) 31 if err == nil { 32 signingInfo.StartHeight = sdkCtx.BlockHeight() 33 } else { 34 signingInfo = types.NewValidatorSigningInfo( 35 consAddr, 36 sdkCtx.BlockHeight(), 37 0, 38 time.Unix(0, 0), 39 false, 40 0, 41 ) 42 } 43 44 return h.k.SetValidatorSigningInfo(ctx, consAddr, signingInfo) 45 } 46 47 // AfterValidatorRemoved deletes the address-pubkey relation when a validator is removed, 48 func (h Hooks) AfterValidatorRemoved(ctx context.Context, consAddr sdk.ConsAddress, _ sdk.ValAddress) error { 49 return h.k.deleteAddrPubkeyRelation(ctx, crypto.Address(consAddr)) 50 } 51 52 // AfterValidatorCreated adds the address-pubkey relation when a validator is created. 53 func (h Hooks) AfterValidatorCreated(ctx context.Context, valAddr sdk.ValAddress) error { 54 sdkCtx := sdk.UnwrapSDKContext(ctx) 55 validator, err := h.k.sk.Validator(ctx, valAddr) 56 if err != nil { 57 return err 58 } 59 60 consPk, err := validator.ConsPubKey() 61 if err != nil { 62 return err 63 } 64 65 return h.k.AddPubkey(sdkCtx, consPk) 66 } 67 68 func (h Hooks) AfterValidatorBeginUnbonding(_ context.Context, _ sdk.ConsAddress, _ sdk.ValAddress) error { 69 return nil 70 } 71 72 func (h Hooks) BeforeValidatorModified(_ context.Context, _ sdk.ValAddress) error { 73 return nil 74 } 75 76 func (h Hooks) BeforeDelegationCreated(_ context.Context, _ sdk.AccAddress, _ sdk.ValAddress) error { 77 return nil 78 } 79 80 func (h Hooks) BeforeDelegationSharesModified(_ context.Context, _ sdk.AccAddress, _ sdk.ValAddress) error { 81 return nil 82 } 83 84 func (h Hooks) BeforeDelegationRemoved(_ context.Context, _ sdk.AccAddress, _ sdk.ValAddress) error { 85 return nil 86 } 87 88 func (h Hooks) AfterDelegationModified(_ context.Context, _ sdk.AccAddress, _ sdk.ValAddress) error { 89 return nil 90 } 91 92 func (h Hooks) BeforeValidatorSlashed(_ context.Context, _ sdk.ValAddress, _ sdkmath.LegacyDec) error { 93 return nil 94 } 95 96 func (h Hooks) AfterUnbondingInitiated(_ context.Context, _ uint64) error { 97 return nil 98 }