-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathconfig.go
377 lines (323 loc) · 15.2 KB
/
config.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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package config
import (
"errors"
"fmt"
"os"
"path"
"github.com/creasty/defaults"
"github.com/forta-network/forta-core-go/protocol/settings"
)
type PublicAPIProxyConfig struct {
Url string `yaml:"url" json:"url" validate:"omitempty,url" default:"https://api.forta.network"`
Headers map[string]string `yaml:"headers" json:"headers"`
RateLimitConfig *RateLimitConfig `yaml:"rateLimit" json:"rateLimit"`
}
type JsonRpcConfig struct {
Url string `yaml:"url" json:"url" validate:"omitempty,url"`
Headers map[string]string `yaml:"headers" json:"headers"`
}
type ScannerConfig struct {
JsonRpc JsonRpcConfig `yaml:"jsonRpc" json:"jsonRpc"`
DisableAutostart bool `yaml:"disableAutostart" json:"disableAutostart"`
BlockRateLimit int `yaml:"blockRateLimit" json:"blockRateLimit" default:"200"`
BlockMaxAgeSeconds int64 `yaml:"blockMaxAgeSeconds" json:"blockMaxAgeSeconds" default:"600"`
RetryIntervalSeconds int64 `yaml:"retryIntervalSeconds" json:"retryIntervalSeconds"`
AlertAPIURL string `yaml:"apiUrl" json:"apiUrl" default:"https://api.forta.network/graphql" validate:"url"`
}
type TraceConfig struct {
JsonRpc JsonRpcConfig `yaml:"jsonRpc" json:"jsonRpc"`
Enabled bool `yaml:"enabled" json:"enabled"`
}
type RateLimitConfig struct {
Rate float64 `yaml:"rate" json:"rate"`
Burst int `yaml:"burst" json:"burst" validate:"min=1"`
}
type JsonRpcProxyConfig struct {
JsonRpc JsonRpcConfig `yaml:"jsonRpc" json:"jsonRpc"`
RateLimitConfig *RateLimitConfig `yaml:"rateLimit" json:"rateLimit"`
}
type JsonRpcCacheConfig struct {
DispatcherURL string `yaml:"dispatcherUrl" json:"dispatcherUrl" default:"https://dispatcher.forta.network/batch" validate:"omitempty,url"`
CacheExpirePeriodSeconds int `yaml:"cacheExpirePeriodSeconds" json:"cacheExpirePeriodSeconds" default:"300"`
}
type LogConfig struct {
Level string `yaml:"level" json:"level" default:"info" `
MaxLogSize string `yaml:"maxLogSize" json:"maxLogSize" default:"50m" `
MaxLogFiles int `yaml:"maxLogFiles" json:"maxLogFiles" default:"10" `
}
type RegistryConfig struct {
ChainID uint64 `yaml:"chainId" json:"chainId" default:"137"`
JsonRpc JsonRpcConfig `yaml:"jsonRpc" json:"jsonRpc" default:"{\"url\": \"https://polygon-rpc.com\"}"`
IPFS IPFSConfig `yaml:"ipfs" json:"ipfs"`
ContainerRegistry string `yaml:"containerRegistry" json:"containerRegistry" validate:"hostname|hostname_port" default:"disco.forta.network" `
Username string `yaml:"username" json:"username"`
Password string `yaml:"password" json:"password"`
Disable bool `yaml:"disable" json:"disable"` // for testing situations
CheckIntervalSeconds int `yaml:"checkIntervalSeconds" json:"checkIntervalSeconds" default:"15"`
ReleaseDistributionUrl string `yaml:"releaseDistributionUrl" json:"releaseDistributionUrl" default:"https://dist.forta.network/manifests/releases"`
}
type IPFSConfig struct {
GatewayURL string `yaml:"gatewayUrl" json:"gatewayUrl" validate:"url" default:"https://ipfs.forta.network" `
APIURL string `yaml:"apiUrl" json:"apiUrl" validate:"url" default:"https://ipfs.forta.network" `
Username string `yaml:"username" json:"username"`
Password string `yaml:"password" json:"password"`
}
type BatchConfig struct {
SkipEmpty bool `yaml:"skipEmpty" json:"skipEmpty"`
IntervalSeconds *int `yaml:"intervalSeconds" json:"intervalSeconds" default:"15"`
MetricsBucketIntervalSeconds *int `yaml:"metricsBucketIntervalSeconds" json:"metricsBucketIntervalSeconds" default:"60"`
MaxAlerts *int `yaml:"maxAlerts" json:"maxAlerts" default:"1000" `
}
type PublisherConfig struct {
SkipPublish bool `yaml:"skipPublish" json:"skipPublish" default:"false"`
AlwaysPublish bool `yaml:"alwaysPublish" json:"alwaysPublish" default:"false"`
APIURL string `yaml:"apiUrl" json:"apiUrl" default:"https://alerts.forta.network" validate:"url"`
IPFS IPFSConfig `yaml:"ipfs" json:"ipfs" validate:"required_unless=SkipPublish true"`
Batch BatchConfig `yaml:"batch" json:"batch"`
}
type ResourcesConfig struct {
DisableAgentLimits bool `yaml:"disableAgentLimits" json:"disableAgentLimits" default:"false" `
AgentMaxMemoryMiB int `yaml:"agentMaxMemoryMib" json:"agentMaxMemoryMib" validate:"omitempty,min=100"`
AgentMaxCPUs float64 `yaml:"agentMaxCpus" json:"agentMaxCpus" validate:"omitempty,gt=0"`
}
type ENSConfig struct {
DefaultContract bool `yaml:"defaultContract" json:"defaultContract" default:"false" `
ContractAddress string `yaml:"contractAddress" json:"contractAddress" validate:"omitempty,eth_addr" default:"0x08f42fcc52a9C2F391bF507C4E8688D0b53e1bd7"`
Override bool `yaml:"override" json:"override" default:"false"`
}
type TelemetryConfig struct {
URL string `yaml:"url" json:"url" default:"https://alerts.forta.network/telemetry" validate:"url"`
CustomURL string `yaml:"customUrl" validate:"omitempty,url"`
Disable bool `yaml:"disable" json:"disable"`
}
type AutoUpdateConfig struct {
Disable bool `yaml:"disable" json:"disable"`
UpdateDelay *int `yaml:"updateDelay" json:"updateDelay"`
TrackPrereleases bool `yaml:"trackPrereleases" json:"trackPrereleases"`
CheckIntervalSeconds int `yaml:"checkIntervalSeconds" json:"checkIntervalSeconds" default:"60"` // 1m
}
type AgentLogsConfig struct {
URL string `yaml:"url" json:"url" default:"https://alerts.forta.network/logs/agents" validate:"url"`
Disable bool `yaml:"disable" json:"disable"`
SendIntervalSeconds int `yaml:"sendIntervalSeconds" json:"sendIntervalSeconds" default:"60"`
}
type ContainerRegistryConfig struct {
Username string `yaml:"username" json:"username"`
Password string `yaml:"password" json:"password"`
}
type RuntimeLimits struct {
StartBlock *uint64 `yaml:"startBlock" json:"startBlock"`
StopBlock *uint64 `yaml:"stopBlock" json:"stopBlock" validate:"omitempty,gtfield=StartBlock"`
StopTimeoutSeconds int `yaml:"stopTimeoutSeconds" json:"stopTimeoutSeconds" default:"30"`
StartCombiner uint64 `yaml:"startCombiner" json:"startCombiner"`
StopCombiner uint64 `yaml:"stopCombiner" json:"stopCombiner"`
}
type RedisConfig struct {
Address string `yaml:"address" json:"address"`
Password string `yaml:"password" json:"password"`
DB int `yaml:"db" json:"db"`
}
type RedisClusterConfig struct {
Addresses []string `yaml:"addresses" json:"addresses"`
Password string `yaml:"password" json:"password"`
DB int `yaml:"db" json:"db"`
}
type DeduplicationConfig struct {
TTLSeconds int `yaml:"ttlSeconds" json:"ttlSeconds" default:"300"`
Redis *RedisConfig `yaml:"redis" json:"redis"`
RedisCluster *RedisClusterConfig `yaml:"redisCluster" json:"redisCluster"`
}
type StandaloneModeConfig struct {
Enable bool `yaml:"enable" json:"enable"`
BotContainers []string `yaml:"botContainers" json:"botContainers"`
}
type LocalModeConfig struct {
Enable bool `yaml:"enable" json:"enable"`
IncludeMetrics bool `yaml:"includeMetrics" json:"includeMetrics"`
BotIDs []string `yaml:"botIds" json:"botIds"`
BotImages []string `yaml:"botImages" json:"botImages"`
WebhookURL string `yaml:"webhookUrl" json:"webhookUrl"`
LogFileName string `yaml:"logFileName" json:"logFileName"`
LogToStdout bool `yaml:"logToStdout" json:"logToStdout"`
ContainerRegistry *ContainerRegistryConfig `yaml:"containerRegistry" json:"containerRegistry"`
RuntimeLimits RuntimeLimits `yaml:"runtimeLimits" json:"runtimeLimits"`
ForceEnableInspection bool `yaml:"forceEnableInspection" json:"forceEnableInspection"`
Deduplication *DeduplicationConfig `yaml:"deduplication" json:"deduplication"`
ShardedBots []*LocalShardedBot `yaml:"shardedBots" json:"shardedBots"`
PrivateKeyHex string `yaml:"privateKeyHex" json:"privateKeyHex"`
Standalone StandaloneModeConfig `yaml:"standalone" json:"standalone"`
}
// IsStandalone checks if the node is in standalone mode. It should only be available
// as another local mode setting.
func (lmc LocalModeConfig) IsStandalone() bool {
return lmc.Enable && lmc.Standalone.Enable
}
type LocalShardedBot struct {
BotImage *string `yaml:"botImage" json:"botImage"`
// number of shards for bot
Shards uint `yaml:"shards" json:"shards"`
// target per shard for bot
Target uint `yaml:"target" json:"target"`
}
type InspectionConfig struct {
BlockInterval *int `yaml:"blockInterval" json:"blockInterval"`
NetworkSavingMode bool `yaml:"networkSavingMode" json:"networkSavingMode"`
InspectAtStartup *bool `yaml:"inspectAtStartup" json:"inspectAtStartup" default:"true"`
}
type StorageConfig struct {
Provide string `yaml:"provide" json:"provide" default:"https://ipfs-router.forta.network/provide"`
Reframe string `yaml:"reframe" json:"reframe" default:"https://ipfs-router.forta.network/reframe"`
}
type CombinerConfig struct {
AlertAPIURL string `yaml:"alertApiUrl" json:"alertApiUrl" default:"http://forta-public-api:8535" validate:"url"`
CombinerCachePath string `yaml:"alertCachePath" json:"alertCachePath"`
QueryInterval uint64 `yaml:"queryInterval" json:"queryInterval"`
}
type AdvancedConfig struct {
IPFSExperiment bool `yaml:"ipfsExperiment" json:"ipfsExperiment"`
MulticallAddress string `yaml:"multicallAddress" json:"multicallAddress"`
TokenExchangeURL string `yaml:"tokenExchangeUrl" json:"tokenExchangeUrl" default:"https://alerts.forta.network/exchange-token"`
}
type PrometheusConfig struct {
Port int `yaml:"port" json:"port" default:"9107"`
}
type Config struct {
// runtime values
Development bool `yaml:"-" json:"_development"`
FortaDir string `yaml:"-" json:"_fortaDir"`
KeyDirPath string `yaml:"-" json:"_keyDirPath"`
Passphrase string `yaml:"-" json:"_passphrase"`
// yaml config values
ChainID int `yaml:"chainId" json:"chainId" default:"1" `
Scan ScannerConfig `yaml:"scan" json:"scan"`
Trace TraceConfig `yaml:"trace" json:"trace"`
Registry RegistryConfig `yaml:"registry" json:"registry"`
Publish PublisherConfig `yaml:"publish" json:"publish"`
JsonRpcProxy JsonRpcProxyConfig `yaml:"jsonRpcProxy" json:"jsonRpcProxy"`
JsonRpcCache JsonRpcCacheConfig `yaml:"jsonRpcCache" json:"jsonRpcCache"`
PublicAPIProxy PublicAPIProxyConfig `yaml:"publicApiProxy" json:"publicApiProxy"`
Log LogConfig `yaml:"log" json:"log"`
ResourcesConfig ResourcesConfig `yaml:"resources" json:"resources"`
ENSConfig ENSConfig `yaml:"ens" json:"ens"`
TelemetryConfig TelemetryConfig `yaml:"telemetry" json:"telemetry"`
AutoUpdate AutoUpdateConfig `yaml:"autoUpdate" json:"autoUpdate"`
AgentLogsConfig AgentLogsConfig `yaml:"agentLogs" json:"agentLogs"`
LocalModeConfig LocalModeConfig `yaml:"localMode" json:"localMode"`
InspectionConfig InspectionConfig `yaml:"inspection" json:"inspection"`
StorageConfig StorageConfig `yaml:"storage" json:"storage"`
CombinerConfig CombinerConfig `yaml:"combiner" json:"combiner"`
PrometheusConfig PrometheusConfig `yaml:"prometheus" json:"prometheus"`
AdvancedConfig AdvancedConfig `yaml:"advanced" json:"advanced"`
}
func (cfg *Config) ConfigFilePath() string {
return path.Join(cfg.FortaDir, DefaultConfigFileName)
}
// GetConfigForContainer is how a container gets the forta configuration (file or env var)
func GetConfigForContainer() (Config, error) {
cfg, err := getConfigFromFile()
if err != nil {
return Config{}, err
}
applyContextDefaults(&cfg)
// initialize combiner cache dump path if cache is persistent
if cfg.CombinerConfig.CombinerCachePath != "" {
_, err = os.Stat(cfg.CombinerConfig.CombinerCachePath)
if !os.IsNotExist(err) {
return cfg, err
}
if os.IsNotExist(err) {
if err := os.WriteFile(cfg.CombinerConfig.CombinerCachePath, []byte("{}"), 0666); err != nil {
return cfg, err
}
}
}
return cfg, nil
}
// BotsToWait returns the count of the bots to wait.
func (cfg *Config) BotsToWait() (waitBots int) {
if !cfg.LocalModeConfig.Enable {
return
}
waitBots += len(cfg.LocalModeConfig.BotImages)
waitBots += len(cfg.LocalModeConfig.BotIDs)
waitBots += len(cfg.LocalModeConfig.Standalone.BotContainers)
// sharded bots spawn on multiple containers, so total "wait bot" count is shards * target
for _, bot := range cfg.LocalModeConfig.ShardedBots {
if bot != nil {
waitBots += int(bot.Target * bot.Shards)
}
}
return
}
// apply defaults that apply in certain contexts
func applyContextDefaults(cfg *Config) {
chainSettings := settings.GetChainSettings(cfg.ChainID)
if chainSettings.EnableTrace && !cfg.LocalModeConfig.Enable {
cfg.Trace.Enabled = true
}
if cfg.ENSConfig.DefaultContract {
cfg.ENSConfig.ContractAddress = ""
}
cfg.FortaDir = DefaultContainerFortaDirPath
cfg.KeyDirPath = path.Join(cfg.FortaDir, DefaultKeysDirName)
cfg.CombinerConfig.CombinerCachePath = path.Join(cfg.FortaDir, DefaultCombinerCacheFileName)
}
func getConfigFromFile() (cfg Config, err error) {
var (
successfullyLoadedTimes int
wrappedErr error
)
// if the default config file exists, load from there
if err = checkIfConfigFileExists(DefaultContainerConfigPath); err == nil {
if err = readYamlFile(DefaultContainerConfigPath, &cfg); err != nil {
return
}
successfullyLoadedTimes++
}
if err != nil {
wrappedErr = err
}
// if the wrapped config file exists, load from there
if err = checkIfConfigFileExists(DefaultContainerWrappedConfigPath); err == nil {
var wrapped map[string]Config
if err = readYamlFile(DefaultContainerWrappedConfigPath, &wrapped); err != nil {
return
}
var found bool
cfg, found = wrapped[DefaultConfigWrapperKey]
if !found {
err = fmt.Errorf("wrapped config file was found but did not have the config under '%s'", DefaultConfigWrapperKey)
return
}
successfullyLoadedTimes++
}
if err != nil {
wrappedErr = fmt.Errorf("%v, %v", wrappedErr, err)
}
// at this point we expect that at least one of the config files were loaded without errors
switch successfullyLoadedTimes {
case 2:
err = errors.New("multiple config files found in the forta dir - please use only one of them")
return
case 0:
err = fmt.Errorf("failed to load any of the config files: %v", wrappedErr)
return
case 1:
// yay! (ignore)
default:
err = fmt.Errorf("successfully loaded unexpected amount of config files (%d) - errors: %w", successfullyLoadedTimes, wrappedErr)
}
// finally set the defaults
err = defaults.Set(&cfg)
return
}
func checkIfConfigFileExists(configPath string) error {
_, err := os.Stat(configPath)
if os.IsNotExist(err) {
return errors.New("config file not found")
}
if err != nil {
return fmt.Errorf("failed to check if config file exists: %w", err)
}
return nil
}