-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
185 lines (172 loc) · 6.81 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
package config
import (
"bytes"
"path/filepath"
"strings"
"github.com/hoaleee/zkevm-node/aggregator"
"github.com/hoaleee/zkevm-node/db"
"github.com/hoaleee/zkevm-node/etherman"
"github.com/hoaleee/zkevm-node/ethtxmanager"
"github.com/hoaleee/zkevm-node/event"
"github.com/hoaleee/zkevm-node/gasprice"
"github.com/hoaleee/zkevm-node/jsonrpc"
"github.com/hoaleee/zkevm-node/log"
"github.com/hoaleee/zkevm-node/merkletree"
"github.com/hoaleee/zkevm-node/metrics"
"github.com/hoaleee/zkevm-node/pool"
"github.com/hoaleee/zkevm-node/sequencer"
"github.com/hoaleee/zkevm-node/sequencesender"
"github.com/hoaleee/zkevm-node/state"
"github.com/hoaleee/zkevm-node/state/runtime/executor"
"github.com/hoaleee/zkevm-node/synchronizer"
"github.com/mitchellh/mapstructure"
"github.com/spf13/viper"
"github.com/urfave/cli/v2"
)
const (
// FlagYes is the flag for yes.
FlagYes = "yes"
// FlagCfg is the flag for cfg.
FlagCfg = "cfg"
// FlagNetwork is the flag for the network name. Valid values: ["testnet", "mainnet", "custom"].
FlagNetwork = "network"
// FlagCustomNetwork is the flag for the custom network file. This is required if --network=custom
FlagCustomNetwork = "custom-network-file"
// FlagAmount is the flag for amount.
FlagAmount = "amount"
// FlagRemoteMT is the flag for remote-merkletree.
FlagRemoteMT = "remote-merkletree"
// FlagComponents is the flag for components.
FlagComponents = "components"
// FlagHTTPAPI is the flag for http.api.
FlagHTTPAPI = "http.api"
// FlagKeyStorePath is the path of the key store file containing the private key of the account going to sing and approve the tokens
FlagKeyStorePath = "key-store-path"
// FlagPassword is the password needed to decrypt the key store
FlagPassword = "password"
// FlagMigrations is the flag for migrations.
FlagMigrations = "migrations"
// FlagOutputFile is the flag for the output file
FlagOutputFile = "output"
// FlagMaxAmount is the flag to avoid to use the flag FlagAmount
FlagMaxAmount = "max-amount"
// FlagDocumentationFileType is the flag for the choose which file generate json-schema
FlagDocumentationFileType = "config-file"
)
/*
Config represents the configuration of the entire Hermez Node
The file is [TOML format]
You could find some examples:
- `config/environments/local/local.node.config.toml`: running a permisionless node
- `config/environments/mainnet/node.config.toml`
- `config/environments/public/node.config.toml`
- `test/config/test.node.config.toml`: configuration for a trusted node used in CI
[TOML format]: https://en.wikipedia.org/wiki/TOML
*/
type Config struct {
// This define is a trusted node (`true`) or a permission less (`false`). If you don't known
// set to `false`
IsTrustedSequencer bool `mapstructure:"IsTrustedSequencer"`
// Last batch number before a forkid change (fork upgrade). That implies that
// greater batch numbers are going to be trusted but no virtualized neither verified.
// So after the batch number `ForkUpgradeBatchNumber` is virtualized and verified you could update
// the system (SC,...) to new forkId and remove this value to allow the system to keep
// Virtualizing and verifying the new batchs.
// Check issue [#2236](https://github.com/hoaleee/zkevm-node/issues/2236) to known more
// This value overwrite `SequenceSender.ForkUpgradeBatchNumber`
ForkUpgradeBatchNumber uint64 `mapstructure:"ForkUpgradeBatchNumber"`
// Which is the new forkId
ForkUpgradeNewForkId uint64 `mapstructure:"ForkUpgradeNewForkId"`
// Configure Log level for all the services, allow also to store the logs in a file
Log log.Config
// Configuration of the etherman (client for access L1)
Etherman etherman.Config
// Configuration for ethereum transaction manager
EthTxManager ethtxmanager.Config
// Pool service configuration
Pool pool.Config
// Configuration for RPC service. THis one offers a extended Ethereum JSON-RPC API interface to interact with the node
RPC jsonrpc.Config
// Configuration of service `Syncrhonizer`. For this service is also really important the value of `IsTrustedSequencer`
// because depending of this values is going to ask to a trusted node for trusted transactions or not
Synchronizer synchronizer.Config
// Configuration of the sequencer service
Sequencer sequencer.Config
// Configuration of the sequence sender service
SequenceSender sequencesender.Config
// Configuration of the aggregator service
Aggregator aggregator.Config
// Configuration of the genesis of the network. This is used to known the initial state of the network
NetworkConfig NetworkConfig
// Configuration of the gas price suggester service
L2GasPriceSuggester gasprice.Config
// Configuration of the executor service
Executor executor.Config
// Configuration of the merkle tree client service. Not use in the node, only for testing
MTClient merkletree.Config
// Configuration of the metrics service, basically is where is going to publish the metrics
Metrics metrics.Config
// Configuration of the event database connection
EventLog event.Config
// Configuration of the hash database connection
HashDB db.Config
// State service configuration
State state.Config
}
// Default parses the default configuration values.
func Default() (*Config, error) {
var cfg Config
viper.SetConfigType("toml")
err := viper.ReadConfig(bytes.NewBuffer([]byte(DefaultValues)))
if err != nil {
return nil, err
}
err = viper.Unmarshal(&cfg, viper.DecodeHook(mapstructure.TextUnmarshallerHookFunc()))
if err != nil {
return nil, err
}
return &cfg, nil
}
// Load loads the configuration
func Load(ctx *cli.Context, loadNetworkConfig bool) (*Config, error) {
cfg, err := Default()
if err != nil {
return nil, err
}
configFilePath := ctx.String(FlagCfg)
if configFilePath != "" {
dirName, fileName := filepath.Split(configFilePath)
fileExtension := strings.TrimPrefix(filepath.Ext(fileName), ".")
fileNameWithoutExtension := strings.TrimSuffix(fileName, "."+fileExtension)
viper.AddConfigPath(dirName)
viper.SetConfigName(fileNameWithoutExtension)
viper.SetConfigType(fileExtension)
}
viper.AutomaticEnv()
replacer := strings.NewReplacer(".", "_")
viper.SetEnvKeyReplacer(replacer)
viper.SetEnvPrefix("ZKEVM_NODE")
err = viper.ReadInConfig()
if err != nil {
_, ok := err.(viper.ConfigFileNotFoundError)
if ok {
log.Infof("config file not found")
} else {
log.Infof("error reading config file: ", err)
return nil, err
}
}
decodeHooks := []viper.DecoderConfigOption{
// this allows arrays to be decoded from env var separated by ",", example: MY_VAR="value1,value2,value3"
viper.DecodeHook(mapstructure.ComposeDecodeHookFunc(mapstructure.TextUnmarshallerHookFunc(), mapstructure.StringToSliceHookFunc(","))),
}
err = viper.Unmarshal(&cfg, decodeHooks...)
if err != nil {
return nil, err
}
if loadNetworkConfig {
// Load genesis parameters
cfg.loadNetworkConfig(ctx)
}
return cfg, nil
}