Skip to content

Commit

Permalink
Add panic guard, bug fix (#1005) (#1006)
Browse files Browse the repository at this point in the history
(cherry picked from commit 9a78e77)

Co-authored-by: Dev Ojha <[email protected]>
  • Loading branch information
mergify[bot] and ValarDragon authored Feb 28, 2022
1 parent 2ce527e commit fcdece1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* [#984](https://github.com/osmosis-labs/osmosis/pull/984) Add wasm support to Dockerfile

## [v7.0.2 - Carbon](https://github.com/osmosis-labs/osmosis/releases/tag/v7.0.2)

This release fixes an instance of undefined behaviour present in v7.0.0.
Parts of the code use a function called [`ApplyFuncIfNoErr`]() whose purpose is to catch errors, and if found undo state updates during its execution.
It is intended to also catch panics and undo the problematic code's execution.
Right now a panic in this code block would halt the node, as it would not know how to proceed.
(But no state change would be committed)

## [v7.0.0 - Carbon](https://github.com/osmosis-labs/osmosis/releases/tag/v7.0.0)

The Osmosis Carbon Release! The changes are primarily
Expand Down
16 changes: 13 additions & 3 deletions osmoutils/cache_ctx.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
package osmoutils

import (
"errors"
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
)

// This function lets you run the function f, but if theres an error
// This function lets you run the function f, but if theres an error or panic
// drop the state machine change and log the error.
// If there is no error, proceeds as normal (but with some slowdown due to SDK store weirdness)
// Try to avoid usage of iterators in f.
func ApplyFuncIfNoError(ctx sdk.Context, f func(ctx sdk.Context) error) error {
func ApplyFuncIfNoError(ctx sdk.Context, f func(ctx sdk.Context) error) (err error) {
// Add a panic safeguard
defer func() {
if recovErr := recover(); recovErr != nil {
fmt.Println(recovErr)
err = errors.New("panic occured during execution")
}
}()
// makes a new cache context, which all state changes get wrapped inside of.
cacheCtx, write := ctx.CacheContext()
err := f(cacheCtx)
err = f(cacheCtx)
if err != nil {
ctx.Logger().Error(err.Error())
} else {
Expand Down
2 changes: 1 addition & 1 deletion x/superfluid/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (h Hooks) BeforeDelegationRemoved(ctx sdk.Context, delAddr sdk.AccAddress,
func (h Hooks) AfterDelegationModified(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) {
}
func (h Hooks) BeforeValidatorSlashed(ctx sdk.Context, valAddr sdk.ValAddress, infractionHeight int64, slashFactor sdk.Dec, effectiveSlashFactor sdk.Dec) {
if slashFactor == sdk.ZeroDec() {
if slashFactor.IsZero() {
return
}
h.k.SlashLockupsForValidatorSlash(ctx, valAddr, infractionHeight, slashFactor)
Expand Down

0 comments on commit fcdece1

Please sign in to comment.