-
Notifications
You must be signed in to change notification settings - Fork 608
/
cache_ctx.go
88 lines (81 loc) · 3.01 KB
/
cache_ctx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package osmoutils
import (
"errors"
"fmt"
"runtime"
"runtime/debug"
"cosmossdk.io/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// This function lets you run the function f, but if there's 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.
//
// If its an out of gas panic, this function will also panic like in normal tx execution flow.
// This is still safe for beginblock / endblock code though, as they do not have out of gas panics.
func ApplyFuncIfNoError(ctx sdk.Context, f func(ctx sdk.Context) error) (err error) {
return applyFunc(ctx, f, ctx.Logger().Error)
}
// ApplyFuncIfNoErrorLogToDebug is the same as ApplyFuncIfNoError, but sends logs to debug instead of error if there is an error.
func ApplyFuncIfNoErrorLogToDebug(ctx sdk.Context, f func(ctx sdk.Context) error) (err error) {
return applyFunc(ctx, f, ctx.Logger().Debug)
}
func applyFunc(ctx sdk.Context, f func(ctx sdk.Context) error, logFunc func(string, ...interface{})) (err error) {
// Add a panic safeguard
defer func() {
if recoveryError := recover(); recoveryError != nil {
if isErr, _ := IsOutOfGasError(recoveryError); isErr {
// We panic with the same error, to replicate the normal tx execution flow.
panic(recoveryError)
} else {
PrintPanicRecoveryError(ctx, recoveryError)
err = errors.New("panic occurred during execution")
}
}
}()
// makes a new cache context, which all state changes get wrapped inside of.
cacheCtx, write := ctx.CacheContext()
err = f(cacheCtx)
if err != nil {
logFunc(err.Error())
} else {
// no error, write the output of f
write()
}
return err
}
// Frustratingly, this has to return the error descriptor, not an actual error itself
// because the SDK errors here are not actually errors. (They don't implement error interface)
func IsOutOfGasError(err any) (bool, string) {
switch e := err.(type) {
case types.ErrorOutOfGas:
return true, e.Descriptor
case types.ErrorGasOverflow:
return true, e.Descriptor
default:
return false, ""
}
}
// PrintPanicRecoveryError error logs the recoveryError, along with the stacktrace, if it can be parsed.
// If not emits them to stdout.
func PrintPanicRecoveryError(ctx sdk.Context, recoveryError interface{}) {
errStackTrace := string(debug.Stack())
switch e := recoveryError.(type) {
case types.ErrorOutOfGas:
ctx.Logger().Debug("out of gas error inside panic recovery block: " + e.Descriptor)
return
case string:
ctx.Logger().Error("Recovering from (string) panic: " + e)
case runtime.Error:
ctx.Logger().Error("recovered (runtime.Error) panic: " + e.Error())
case error:
ctx.Logger().Error("recovered (error) panic: " + e.Error())
default:
ctx.Logger().Error("recovered (default) panic. Could not capture logs in ctx, see stdout")
fmt.Println("Recovering from panic ", recoveryError)
debug.PrintStack()
return
}
ctx.Logger().Error("stack trace: " + errStackTrace)
}