-
Notifications
You must be signed in to change notification settings - Fork 555
/
hooks.go
89 lines (76 loc) · 2.6 KB
/
hooks.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
89
package types
import (
fmt "fmt"
"strconv"
"github.com/armon/go-metrics"
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/osmosis-labs/osmosis/osmoutils"
)
type EpochHooks interface {
// the first block whose timestamp is after the duration is counted as the end of the epoch
AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumber int64) error
// new epoch is next block of epoch end block
BeforeEpochStart(ctx sdk.Context, epochIdentifier string, epochNumber int64) error
// Returns the name of the module implementing epoch hook.
GetModuleName() string
}
const (
// flag indicating whether this is a before epoch hook
isBeforeEpoch = true
)
var _ EpochHooks = MultiEpochHooks{}
// combine multiple gamm hooks, all hook functions are run in array sequence.
type MultiEpochHooks []EpochHooks
// GetModuleName implements EpochHooks.
func (MultiEpochHooks) GetModuleName() string {
return ModuleName
}
func NewMultiEpochHooks(hooks ...EpochHooks) MultiEpochHooks {
return hooks
}
// AfterEpochEnd is called when epoch is going to be ended, epochNumber is the number of epoch that is ending.
func (h MultiEpochHooks) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, epochNumber int64) error {
for _, hook := range h {
panicCatchingEpochHook(ctx, hook.AfterEpochEnd, epochIdentifier, epochNumber, h.GetModuleName(), !isBeforeEpoch)
}
return nil
}
// BeforeEpochStart is called when epoch is going to be started, epochNumber is the number of epoch that is starting.
func (h MultiEpochHooks) BeforeEpochStart(ctx sdk.Context, epochIdentifier string, epochNumber int64) error {
for _, hook := range h {
panicCatchingEpochHook(ctx, hook.BeforeEpochStart, epochIdentifier, epochNumber, hook.GetModuleName(), isBeforeEpoch)
}
return nil
}
func panicCatchingEpochHook(
ctx sdk.Context,
hookFn func(ctx sdk.Context, epochIdentifier string, epochNumber int64) error,
epochIdentifier string,
epochNumber int64,
moduleName string,
isBeforeEpoch bool,
) {
wrappedHookFn := func(ctx sdk.Context) error {
return hookFn(ctx, epochIdentifier, epochNumber)
}
// TODO: Thread info for which hook this is, may be dependent on larger hook system refactoring
err := osmoutils.ApplyFuncIfNoError(ctx, wrappedHookFn)
if err != nil {
telemetry.IncrCounterWithLabels([]string{}, 1, []metrics.Label{
{
Name: "module_name",
Value: moduleName,
},
{
Name: "error",
Value: err.Error(),
},
{
Name: "is_before_hook",
Value: strconv.FormatBool(isBeforeEpoch),
},
})
ctx.Logger().Error(fmt.Sprintf("error in epoch hook %v", err))
}
}