/
context.go
120 lines (103 loc) · 3.33 KB
/
context.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package utils
import (
"net"
"github.com/aurora-is-near/relayer2-base/log"
"github.com/spf13/viper"
"golang.org/x/net/context"
)
const (
chainIdConfigPath = "endpoint.chainID"
prehistoryChainIdConfigPath = "prehistoryIndexer.prehistoryChainID"
prehsitoryHeightConfigPath = "prehistoryIndexer.prehistoryHeight"
)
var (
defaultChainId uint64 = 1313161554
chainId *uint64
prehistoryChainId *uint64
defaultPrehistoryHeight uint64 = 37157758
prehistoryHeight *uint64
)
type chainIdKey struct{}
// GetChainId returns the chainId in the following order;
// 1. returns chainId if exists in ctx
// 2. returns chainId if exists in relayer configuration .yml file
// 3. returns defaultChainId=1313161554
func GetChainId(ctx context.Context) uint64 {
if ctx != nil {
if cid, ok := ctx.Value(chainIdKey{}).(*uint64); ok && cid != nil {
return *cid
}
}
if chainId == nil {
chainId = getChainId()
}
return *chainId
}
// PutChainId is a helper function to put chainId in the context, also see GetChainId
func PutChainId(ctx context.Context, chainId uint64) context.Context {
return context.WithValue(ctx, chainIdKey{}, &chainId)
}
func getChainId() *uint64 {
var cid *uint64
sub := viper.GetUint64(chainIdConfigPath)
if sub != 0 {
cid = &sub
} else {
log.Log().Warn().Msgf("failed to parse configuration [%s] from [%s], "+
"falling back to defaults", chainIdConfigPath, viper.ConfigFileUsed())
cid = &defaultChainId
}
return cid
}
// GetPrehistoryChainId returns the chainId config that the prehistory was indexed for;
// 1. returns prehistoryChainId if exists in relayer configuration .yml file
// 2. returns defaultChainId=1313161554
func GetPrehistoryChainId() uint64 {
if prehistoryChainId == nil {
prehistoryChainId = getPrehistoryChainId()
}
return *prehistoryChainId
}
func getPrehistoryChainId() *uint64 {
var pcid *uint64
sub := viper.GetUint64(prehistoryChainIdConfigPath)
if sub != 0 {
pcid = &sub
} else {
log.Log().Warn().Msgf("failed to parse configuration [%s] from [%s], "+
"falling back to defaults", prehistoryChainIdConfigPath, viper.ConfigFileUsed())
pcid = &defaultChainId
}
return pcid
}
// GetPrehistoryHeight returns the height of the prehistory given in relayer configuration .yml file
// 1. returns prehistoryHeight if exists in relayer configuration .yml file
// 2. returns defaultPrehistoryHeight=37157758 that is for mainnet
func GetPrehistoryHeight() uint64 {
if prehistoryHeight == nil {
prehistoryHeight = getPrehistoryHeight()
}
return *prehistoryHeight
}
func getPrehistoryHeight() *uint64 {
var ph *uint64
sub := viper.GetUint64(prehsitoryHeightConfigPath)
if sub != 0 {
ph = &sub
} else {
log.Log().Warn().Msgf("failed to parse configuration [%s] from [%s], "+
"falling back to defaults", prehsitoryHeightConfigPath, viper.ConfigFileUsed())
ph = &defaultPrehistoryHeight
}
return ph
}
type clientIpKey struct{}
// PutClientIpKey is a helper function to put clientIp in the context so that handlers can use it
func PutClientIpKey(ctx context.Context, ip net.IP) context.Context {
return context.WithValue(ctx, clientIpKey{}, ip)
}
// ClientIpFromContext returns the clientIp value stored in ctx, if any.
func ClientIpFromContext(ctx context.Context) (*net.IP, bool) {
ip, ok := ctx.Value(clientIpKey{}).(net.IP)
return &ip, ok
}