Skip to content

Commit

Permalink
refactor(x/staking): return errors on Hooks calls (#19277)
Browse files Browse the repository at this point in the history
Co-authored-by: Aleksandr Bezobchuk <alexanderbez@users.noreply.github.com>
  • Loading branch information
likhita-809 and alexanderbez authored Feb 2, 2024
1 parent 78cdf36 commit c3a2357
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 7 deletions.
1 change: 1 addition & 0 deletions x/staking/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

* [#19277](https://github.com/cosmos/cosmos-sdk/pull/19277) Hooks calls on `SetUnbondingDelegationEntry`, `SetRedelegationEntry`, `Slash` and `RemoveValidator` returns errors instead of logging just like other hooks calls.
* [#18636](https://github.com/cosmos/cosmos-sdk/pull/18636) `IterateBondedValidatorsByPower`, `GetDelegatorBonded`, `Delegate`, `Unbond`, `Slash`, `Jail`, `SlashRedelegation`, `ApplyAndReturnValidatorSetUpdates` methods no longer panics on any kind of errors but instead returns appropriate errors.
* [#18506](https://github.com/cosmos/cosmos-sdk/pull/18506) Detect the length of the ed25519 pubkey in CreateValidator to prevent panic.

Expand Down
5 changes: 2 additions & 3 deletions x/staking/keeper/delegation.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ func (k Keeper) SetUnbondingDelegationEntry(
}

if err := k.Hooks().AfterUnbondingInitiated(ctx, id); err != nil {
k.Logger(ctx).Error("failed to call after unbonding initiated hook", "error", err)
return ubd, fmt.Errorf("failed to call after unbonding initiated hook: %w", err)
}
}
return ubd, nil
Expand Down Expand Up @@ -554,8 +554,7 @@ func (k Keeper) SetRedelegationEntry(ctx context.Context,
}

if err := k.Hooks().AfterUnbondingInitiated(ctx, id); err != nil {
k.Logger(ctx).Error("failed to call after unbonding initiated hook", "error", err)
// TODO (Facu): Should we return here? We are ignoring this error
return types.Redelegation{}, fmt.Errorf("failed to call after unbonding initiated hook: %w", err)
}

return red, nil
Expand Down
6 changes: 3 additions & 3 deletions x/staking/keeper/slash.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ func (k Keeper) Slash(ctx context.Context, consAddr sdk.ConsAddress, infractionH

operatorAddress, err := k.ValidatorAddressCodec().StringToBytes(validator.GetOperator())
if err != nil {
return math.Int{}, err
return math.NewInt(0), err
}

// call the before-modification hook
if err := k.Hooks().BeforeValidatorModified(ctx, operatorAddress); err != nil {
k.Logger(ctx).Error("failed to call before validator modified hook", "error", err)
return math.NewInt(0), fmt.Errorf("failed to call before validator modified hook: %w", err)
}

// Track remaining slash amount for the validator
Expand Down Expand Up @@ -170,7 +170,7 @@ func (k Keeper) Slash(ctx context.Context, consAddr sdk.ConsAddress, infractionH
}
// call the before-slashed hook
if err := k.Hooks().BeforeValidatorSlashed(ctx, operatorAddress, effectiveFraction); err != nil {
k.Logger(ctx).Error("failed to call before validator slashed hook", "error", err)
return math.NewInt(0), fmt.Errorf("failed to call before validator slashed hook: %w", err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion x/staking/keeper/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func (k Keeper) RemoveValidator(ctx context.Context, address sdk.ValAddress) err
}

if err := k.Hooks().AfterValidatorRemoved(ctx, valConsAddr, str); err != nil {
k.Logger(ctx).Error("error in after validator removed hook", "error", err)
return fmt.Errorf("error in after validator removed hook: %w", err)
}

return nil
Expand Down

0 comments on commit c3a2357

Please sign in to comment.