-
Notifications
You must be signed in to change notification settings - Fork 176
/
paramscontext.go
68 lines (56 loc) · 1.58 KB
/
paramscontext.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
package util
import (
"time"
"github.com/patrickmn/go-cache"
cliContext "github.com/cosmos/cosmos-sdk/client/context"
chainmanagerTypes "github.com/maticnetwork/heimdall/chainmanager/types"
checkpointTypes "github.com/maticnetwork/heimdall/checkpoint/types"
)
type ParamsContext struct {
cliCtx cliContext.CLIContext
paramsCache *cache.Cache
key string
}
type Params struct {
ChainmanagerParams *chainmanagerTypes.Params
CheckpointParams *checkpointTypes.Params
}
// NewParamsContext creates new params context
func NewParamsContext(cliCtx cliContext.CLIContext) *ParamsContext {
paramsContext := ParamsContext{
key: "params",
cliCtx: cliCtx,
paramsCache: cache.New(1*time.Hour, 1*time.Hour),
}
return ¶msContext
}
// GetParams updates cache if required and returns params
func (paramsContext *ParamsContext) GetParams() (params Params, err error) {
var found bool
data, found := paramsContext.paramsCache.Get(paramsContext.key)
if found {
params = data.(Params)
} else {
// Fetch params and add to cache
params, err = fetchLatestParams(paramsContext.cliCtx)
if err == nil {
paramsContext.paramsCache.Set(paramsContext.key, params, 1*time.Hour)
}
}
return
}
func fetchLatestParams(cliContext cliContext.CLIContext) (params Params, err error) {
chainmanagerParams, err := GetChainmanagerParams(cliContext)
if err != nil {
return
}
checkpointParams, err := GetCheckpointParams(cliContext)
if err != nil {
return
}
params = Params{
ChainmanagerParams: chainmanagerParams,
CheckpointParams: checkpointParams,
}
return
}