-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathconfig.go
More file actions
258 lines (214 loc) · 7.56 KB
/
Copy pathconfig.go
File metadata and controls
258 lines (214 loc) · 7.56 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package conf
import (
"fmt"
"strings"
"time"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/yaml"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
"github.com/oasisprotocol/oasis-core/go/common/logging"
)
// Config contains the CLI configuration.
type Config struct {
RuntimeID string `koanf:"runtime_id"`
NodeAddress string `koanf:"node_address"`
EnablePruning bool `koanf:"enable_pruning"`
PruningStep uint64 `koanf:"pruning_step"`
// IndexingStart. Skip indexing before this block number. Use this to avoid trying to index
// blocks that the node doesn't have data for, such as by skipping them in checkpoint sync.
// For sensible reasons, indexing may actually start at an even later block, such as if
// this block is already indexed or the node indicates that it doesn't have this block.
IndexingStart uint64 `koanf:"indexing_start"`
IndexingDisable bool `koanf:"indexing_disable"`
Log *LogConfig `koanf:"log"`
Cache *CacheConfig `koanf:"cache"`
Database *DatabaseConfig `koanf:"database"`
Gateway *GatewayConfig `koanf:"gateway"`
// ArchiveURI is the URI of an archival web3 gateway instance
// for servicing historical queries.
ArchiveURI string `koanf:"archive_uri"`
// ArchiveHeightMax is the maximum height (inclusive) to query the
// archvie node (ArchiveURI). If the archive node is configured
// with it's own SQL database instance, this parameter should not
// be needed.
ArchiveHeightMax uint64 `koanf:"archive_height_max"`
}
// Validate performs config validation.
func (cfg *Config) Validate() error {
if cfg.RuntimeID == "" {
return fmt.Errorf("malformed runtime ID '%s'", cfg.RuntimeID)
}
if cfg.NodeAddress == "" {
return fmt.Errorf("malformed node address '%s'", cfg.NodeAddress)
}
if cfg.Log != nil {
if err := cfg.Log.Validate(); err != nil {
return err
}
}
if cfg.Database != nil {
if err := cfg.Database.Validate(); err != nil {
return fmt.Errorf("database: %w", err)
}
}
if cfg.Gateway != nil {
if err := cfg.Gateway.Validate(); err != nil {
return fmt.Errorf("gateway: %w", err)
}
}
return nil
}
// LogConfig contains the logging configuration.
type LogConfig struct {
Format string `koanf:"format"`
Level string `koanf:"level"`
File string `koanf:"file"`
}
// Validate validates the logging configuration.
func (cfg *LogConfig) Validate() error {
var format logging.Format
if err := format.Set(cfg.Format); err != nil {
return err
}
var level logging.Level
return level.Set(cfg.Level)
}
// CacheConfig contains the cache configuration.
type CacheConfig struct {
// BlockSize is the size of the block cache in BLOCKS.
BlockSize uint64 `koanf:"block_size"`
// Metrics enables the cache metrics collection.
Metrics bool `koanf:"metrics"`
}
// DatabaseConfig is the postgresql database configuration.
type DatabaseConfig struct {
Host string `koanf:"host"`
Port int `koanf:"port"`
DB string `koanf:"db"`
User string `koanf:"user"`
Password string `koanf:"password"`
DialTimeout int `koanf:"dial_timeout"`
ReadTimeout int `koanf:"read_timeout"`
WriteTimeout int `koanf:"write_timeout"`
MaxOpenConns int `koanf:"max_open_conns"`
}
// Validate validates the database configuration.
func (cfg *DatabaseConfig) Validate() error {
if cfg.Host == "" {
return fmt.Errorf("malformed database host: ''")
}
// TODO:
return nil
}
// GatewayConfig is the gateway server configuration.
type GatewayConfig struct {
// HTTP is the gateway http endpoint config.
HTTP *GatewayHTTPConfig `koanf:"http"`
// Monitoring is the gateway prometheus configuration.
Monitoring *GatewayMonitoringConfig `koanf:"monitoring"`
// WS is the gateway websocket endpoint config.
WS *GatewayWSConfig `koanf:"ws"`
// ChainID defines the Ethereum network chain id.
ChainID uint32 `koanf:"chain_id"`
// MethodLimits is the gateway method limits config.
MethodLimits *MethodLimits `koanf:"method_limits"`
// OasisRPCs controls whether to enable the `oasis_*` methods. Default is not exposed.
ExposeOasisRPCs bool `koanf:"oasis_rpcs"`
// AllowUnencryptedTxs also accepts transacions with CallFormat=0. Default is false.
AllowUnencryptedTxs bool `koanf:"allow_unencrypted_txs"`
}
// GatewayMonitoringConfig is the gateway prometheus configuration.
type GatewayMonitoringConfig struct {
// Host is the host interface on which to start the prometheus http server. Disabled if unset.
Host string `koanf:"host"`
// Port is the port number on which to start the prometheus http server.
Port int `koanf:"port"`
}
// Enabled returns true if monitoring is configured.
func (cfg *GatewayMonitoringConfig) Enabled() bool {
if cfg == nil {
return false
}
if cfg.Host == "" {
return false
}
return true
}
// Address returns the prometheus listen address.
//
// Returns empty string if monitoring is not configured.
func (cfg *GatewayMonitoringConfig) Address() string {
if !cfg.Enabled() {
return ""
}
return fmt.Sprintf("%s:%d", cfg.Host, cfg.Port)
}
// Validate validates the gateway configuration.
func (cfg *GatewayConfig) Validate() error {
// TODO:
return nil
}
type GatewayHTTPConfig struct {
// Host is the host interface on which to start the HTTP RPC server. Defaults to localhost.
Host string `koanf:"host"`
// Port is the port number on which to start the HTTP RPC server. Defaults to 8545.
Port int `koanf:"port"`
// Cors are the CORS allowed urls.
Cors []string `koanf:"cors"`
// VirtualHosts is the list of virtual hostnames which are allowed on incoming requests.
VirtualHosts []string `koanf:"virtual_hosts"`
// PathPrefix specifies a path prefix on which http-rpc is to be served. Defaults to '/'.
PathPrefix string `koanf:"path_prefix"`
// Timeouts allows for customization of the timeout values used by the HTTP RPC
// interface.
Timeouts *HTTPTimeouts `koanf:"timeouts"`
}
type HTTPTimeouts struct {
Read *time.Duration `koanf:"read"`
Write *time.Duration `koanf:"write"`
Idle *time.Duration `koanf:"idle"`
}
type GatewayWSConfig struct {
// Host is the host interface on which to start the HTTP RPC server. Defaults to localhost.
Host string `koanf:"host"`
// Port is the port number on which to start the HTTP RPC server. Defaults to 8545.
Port int `koanf:"port"`
// PathPrefix specifies a path prefix on which http-rpc is to be served. Defaults to '/'.
PathPrefix string `koanf:"path_prefix"`
// Origins is the list of domain to accept websocket requests from.
Origins []string `koanf:"origins"`
// Timeouts allows for customization of the timeout values used by the HTTP RPC
// interface.
Timeouts *HTTPTimeouts `koanf:"timeouts"`
}
// MethodLimits are the configured gateway method limits.
type MethodLimits struct {
// GetLogsMaxRounds is the maximum number of rounds to query for in a get logs query.
GetLogsMaxRounds uint64 `koanf:"get_logs_max_rounds"`
}
// InitConfig initializes configuration from file.
func InitConfig(f string) (*Config, error) {
var config Config
k := koanf.New(".")
// Load configuration from the yaml config.
if err := k.Load(file.Provider(f), yaml.Parser()); err != nil {
return nil, err
}
// Load environment variables and merge into the loaded config.
if err := k.Load(env.Provider("", ".", func(s string) string {
// `__` is used as a hierarchy delimiter.
return strings.ReplaceAll(strings.ToLower(s), "__", ".")
}), nil); err != nil {
return nil, err
}
// Unmarshal into config.
if err := k.Unmarshal("", &config); err != nil {
return nil, err
}
// Validate config.
if err := config.Validate(); err != nil {
return nil, err
}
return &config, nil
}