-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.go
159 lines (133 loc) · 5.32 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
// Package confg is a placeholder package for settings that will
// probably be config values at some point in the future. I couldn't
// decide on a config package or library. It's hard coded here for now
// in a way that I hope is convenient for refactoring in the future.
package config
import (
"log"
"strings"
"github.com/go-playground/validator/v10"
"github.com/spf13/viper"
)
// Runner holds the config values that are needed to manage the job system.
type Runner struct {
Interval uint `mapstructure:"interval" validate:"required"`
}
type Providers struct {
RPCs []RPC `mapstructure:"rpc" validate:"dive"`
HeimdallEndpoints []HeimdallEndpoint `mapstructure:"heimdall" validate:"dive"`
SensorNetworks []SensorNetwork `mapstructure:"sensor_network" validate:"dive"`
HashDivergence *HashDivergence `mapstructure:"hash_divergence"`
System *System `mapstructure:"system"`
ExchangeRates *ExchangeRates `mapstructure:"exchange_rates"`
}
// RPC defines the various RPC providers that will be monitored.
type RPC struct {
Name string `mapstructure:"name"`
URL string `mapstructure:"url" validate:"url,required_with=Name"`
Label string `mapstructure:"label" validate:"required_with=Name"`
Interval uint `mapstructure:"interval"`
Contracts ContractAddresses `mapstructure:"contracts"`
TimeToMine *TimeToMine `mapstructure:"time_to_mine"`
Accounts []string `mapstructure:"accounts"`
}
type ContractAddresses struct {
// PoS
StateSyncSenderAddress *string `mapstructure:"state_sync_sender_address"`
StateSyncReceiverAddress *string `mapstructure:"state_sync_receiver_address"`
CheckpointAddress *string `mapstructure:"checkpoint_address"`
// zkEVM
GlobalExitRootL2Address *string `mapstructure:"global_exit_root_l2_address"`
ZkEVMBridgeAddress *string `mapstructure:"zkevm_bridge_address"`
RollupManagerAddress *string `mapstructure:"rollup_manager_address"`
}
type TimeToMine struct {
Sender string `mapstructure:"sender" validate:"required"`
SenderPrivateKey string `mapstructure:"sender_private_key" validate:"required"`
Receiver string `mapstructure:"receiver" validate:"required"`
Value int64 `mapstructure:"value" validate:"required"`
Data string `mapstructure:"data"`
GasPriceFactor int64 `mapstructure:"gas_price_factor"`
GasLimit uint64 `mapstructure:"gas_limit" validate:"required"`
}
type HashDivergence struct {
Interval uint `mapstructure:"interval"`
}
type System struct {
Interval uint `mapstructure:"interval"`
}
type ExchangeRates struct {
CoinbaseURL string `mapstructure:"coinbase_url" validate:"required"`
Tokens map[string][]string `mapstructure:"tokens"`
Interval uint `mapstructure:"interval"`
}
type HeimdallEndpoint struct {
Name string `mapstructure:"name"`
TendermintURL string `mapstructure:"tendermint_url" validate:"url,required_with=Name"`
HeimdallURL string `mapstructure:"heimdall_url" validate:"url,required_with=Name"`
Label string `mapstructure:"label" validate:"required_with=Name"`
Interval uint `mapstructure:"interval"`
}
type SensorNetwork struct {
Name string `mapstructure:"name"`
Label string `mapstructure:"label" validate:"required_with=Name"`
Project string `mapstructure:"project" validate:"required_with=Name"`
Database string `mapstructure:"database"`
Interval uint `mapstructure:"interval"`
}
// HTTP defines the HTTP properties that we'll use for exposing metrics.
type HTTP struct {
Port int `mapstructure:"port" validate:"required"`
Address string `mapstructure:"address" validate:"required"`
Path string `mapstructure:"path" validate:"required"`
}
type CustomNetwork struct {
Name string `mapstructure:"name"`
ChainID uint64 `mapstructure:"chain_id"`
}
// GetName returns the network name.
func (n CustomNetwork) GetName() string {
return n.Name
}
// GetChainID returns the network chain ID.
func (n CustomNetwork) GetChainID() uint64 {
return n.ChainID
}
type Logs struct {
Pretty bool `mapstructure:"pretty"`
Verbosity string `mapstructure:"verbosity"`
}
type config struct {
Namespace string `mapstructure:"namespace" validate:"required"`
Runner Runner `mapstructure:"runner"`
HTTP HTTP `mapstructure:"http"`
Providers *Providers `mapstructure:"providers"`
Observers []string `mapstructure:"observers"`
Networks []CustomNetwork `mapstructure:"networks"`
Logs Logs `mapstructure:"logs"`
}
var c *config
// Config will return the current configuration for the entire application
// (assuming it's been initialized).
func Config() *config {
return c
}
func init() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
viper.AddConfigPath("/etc/panoptichain/")
viper.AutomaticEnv()
viper.SetEnvPrefix("panoptichain")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
if err := viper.ReadInConfig(); err != nil {
log.Panicf("Failed to read config: %v", err)
}
if err := viper.Unmarshal(&c); err != nil {
log.Panicf("Failed to load config: %v", err)
}
validate := validator.New()
if err := validate.Struct(c); err != nil {
log.Panicf("Failed to validate config: %v", err)
}
}