/
config.go
171 lines (148 loc) · 4.83 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
package config
import (
"math/big"
"github.com/BurntSushi/toml"
"github.com/pkg/errors"
)
type Config struct {
LogLevel string `toml:"log_level"`
Signer SignerServiceConfig `toml:"signer_service"`
Metrics MetricsConfig `toml:"metrics"`
Healthz HealthzConfig `toml:"healthz"`
Wallets map[string]*WalletConfig `toml:"wallets"`
Providers map[string]*ProviderConfig `toml:"providers"`
}
type SignerServiceConfig struct {
URL string `toml:"url"`
TLSCaCert string `toml:"tls_ca_cert"`
TLSCert string `toml:"tls_cert"`
TLSKey string `toml:"tls_key"`
}
type MetricsConfig struct {
Enabled bool `toml:"enabled"`
Debug bool `toml:"debug"`
Host string `toml:"host"`
Port string `toml:"port"`
}
type HealthzConfig struct {
Enabled bool `toml:"enabled"`
Host string `toml:"host"`
Port string `toml:"port"`
}
type WalletConfig struct {
ChainID big.Int `toml:"chain_id"`
// signer | static
SignerMethod string `toml:"signer_method"`
Address string `toml:"address"`
// private key is used for static signing
PrivateKey string `toml:"private_key"`
// transaction parameters
TxValue big.Int `toml:"tx_value"`
}
type ProviderConfig struct {
Network string `toml:"network"`
URL string `toml:"url"`
ReadOnly bool `toml:"read_only"`
ReadInterval TOMLDuration `toml:"read_interval"`
SendInterval TOMLDuration `toml:"send_interval"`
SendTransactionRetryInterval TOMLDuration `toml:"send_transaction_retry_interval"`
SendTransactionRetryTimeout TOMLDuration `toml:"send_transaction_retry_timeout"`
SendTransactionCoolDown TOMLDuration `toml:"send_transaction_cool_down"`
ReceiptRetrievalInterval TOMLDuration `toml:"receipt_retrieval_interval"`
ReceiptRetrievalTimeout TOMLDuration `toml:"receipt_retrieval_timeout"`
Wallet string `toml:"wallet"`
}
func New(file string) (*Config, error) {
cfg := &Config{}
if _, err := toml.DecodeFile(file, cfg); err != nil {
return nil, err
}
return cfg, nil
}
func (c *Config) Validate() error {
if c.Metrics.Enabled {
if c.Metrics.Host == "" || c.Metrics.Port == "" {
return errors.New("metrics is enabled but host or port are missing")
}
}
if c.Healthz.Enabled {
if c.Healthz.Host == "" || c.Healthz.Port == "" {
return errors.New("healthz is enabled but host or port are missing")
}
}
if len(c.Wallets) == 0 {
return errors.New("at least one wallet must be set")
}
if len(c.Providers) == 0 {
return errors.New("at least one provider must be set")
}
for name, wallet := range c.Wallets {
if wallet.ChainID.BitLen() == 0 {
return errors.Errorf("wallet [%s] chain_id is missing", name)
}
if wallet.SignerMethod != "signer" && wallet.SignerMethod != "static" {
return errors.Errorf("wallet [%s] signer_method is invalid", name)
}
if wallet.SignerMethod == "signer" {
if c.Signer.URL == "" {
return errors.New("signer url is missing")
}
if c.Signer.TLSCaCert == "" {
return errors.New("signer tls_ca_cert is missing")
}
if c.Signer.TLSCert == "" {
return errors.New("signer tls_cert is missing")
}
if c.Signer.TLSKey == "" {
return errors.New("signer tls_key is missing")
}
}
if wallet.SignerMethod == "static" {
if wallet.PrivateKey == "" {
return errors.Errorf("wallet [%s] private_key is missing", name)
}
}
if wallet.Address == "" {
return errors.Errorf("wallet [%s] address is missing", name)
}
if wallet.TxValue.BitLen() == 0 {
return errors.Errorf("wallet [%s] tx_value is missing", name)
}
}
for name, provider := range c.Providers {
if provider.URL == "" {
return errors.Errorf("provider [%s] url is missing", name)
}
if provider.ReadInterval == 0 {
return errors.Errorf("provider [%s] read_interval is missing", name)
}
if provider.SendInterval == 0 {
return errors.Errorf("provider [%s] send_interval is missing", name)
}
if provider.SendTransactionRetryInterval == 0 {
return errors.Errorf("provider [%s] send_transaction_retry_interval is missing", name)
}
if provider.SendTransactionRetryTimeout == 0 {
return errors.Errorf("provider [%s] send_transaction_retry_timeout is missing", name)
}
if provider.SendTransactionCoolDown == 0 {
return errors.Errorf("provider [%s] send_transaction_cool_down is missing", name)
}
if provider.ReceiptRetrievalInterval == 0 {
return errors.Errorf("provider [%s] receipt_retrieval_interval is missing", name)
}
if provider.ReceiptRetrievalTimeout == 0 {
return errors.Errorf("provider [%s] receipt_retrieval_timeout is missing", name)
}
if provider.Wallet == "" {
return errors.Errorf("provider [%s] wallet is missing", name)
}
if _, ok := c.Wallets[provider.Wallet]; !ok {
return errors.Errorf("provider [%s] has an invalid wallet [%s]", name, provider.Wallet)
}
}
if c.LogLevel == "" {
c.LogLevel = "debug"
}
return nil
}