-
Notifications
You must be signed in to change notification settings - Fork 117
/
abci.go
39 lines (30 loc) · 991 Bytes
/
abci.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
package utils
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/go-errors/errors"
"github.com/tendermint/tendermint/libs/log"
)
//go:generate moq -out ./mock/abci.go -pkg mock . Logger
// Logger wraps keepers which expose a Logger method
type Logger interface {
Logger(ctx sdk.Context) log.Logger
}
// RunCached wraps the given function, handles error/panic and rolls back the state if necessary
func RunCached[T any](c sdk.Context, l Logger, f func(sdk.Context) (T, error)) T {
ctx, writeCache := c.CacheContext()
defer func() {
if r := recover(); r != nil {
l.Logger(ctx).Error(fmt.Sprintf("recovered from panic in cached context: %v", r))
l.Logger(ctx).Error(string(errors.Wrap(r, 1).Stack()))
}
}()
result, err := f(ctx)
if err != nil {
l.Logger(ctx).Debug(fmt.Sprintf("recovered from error in cached context: %s", err.Error()))
return *new(T)
}
writeCache()
c.EventManager().EmitEvents(ctx.EventManager().Events())
return result
}