This repository has been archived by the owner on Jun 17, 2022. It is now read-only.
/
config.go
325 lines (284 loc) · 10 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
package main
import (
"bufio"
"errors"
"fmt"
"os"
"path"
"path/filepath"
"reflect"
"strings"
"github.com/Fantom-foundation/go-opera/cmd/opera/launcher"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/metrics"
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/p2p/discv5"
"github.com/ethereum/go-ethereum/params"
"github.com/naoina/toml"
"gopkg.in/urfave/cli.v1"
"github.com/Fantom-foundation/go-lachesis/evmcore"
"github.com/Fantom-foundation/go-lachesis/gossip"
"github.com/Fantom-foundation/go-lachesis/gossip/gasprice"
"github.com/Fantom-foundation/go-lachesis/lachesis"
)
var (
dumpConfigCommand = cli.Command{
Action: utils.MigrateFlags(dumpConfig),
Name: "dumpconfig",
Usage: "Show configuration values",
ArgsUsage: "",
Flags: append(nodeFlags, testFlags...),
Category: "MISCELLANEOUS COMMANDS",
Description: `The dumpconfig command shows configuration values.`,
}
configFileFlag = cli.StringFlag{
Name: "legacy_config",
Usage: "Lachesis TOML configuration file",
}
operaConfigFileFlag = cli.StringFlag{
Name: "config",
Usage: "Opera TOML configuration file",
}
// DataDirFlag defines directory to store Lachesis state and user's wallets
DataDirFlag = utils.DirectoryFlag{
Name: "datadir",
Usage: "Data directory for the databases and keystore",
Value: utils.DirectoryString(DefaultDataDir()),
}
)
// These settings ensure that TOML keys use the same names as Go struct fields.
var tomlSettings = toml.Config{
NormFieldName: func(rt reflect.Type, key string) string {
return key
},
FieldToKey: func(rt reflect.Type, field string) string {
return field
},
MissingField: func(rt reflect.Type, field string) error {
return fmt.Errorf("field '%s' is not defined in %s", field, rt.String())
},
}
type config struct {
Node node.Config
Lachesis gossip.Config
}
func loadAllConfigs(file string, cfg *config) error {
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
err = tomlSettings.NewDecoder(bufio.NewReader(f)).Decode(cfg)
// Add file name to errors that have a line number.
if _, ok := err.(*toml.LineError); ok {
err = errors.New(file + ", " + err.Error())
}
if err != nil {
return errors.New(fmt.Sprintf("TOML config file error: %v.\n"+
"Use 'dumpconfig' command to get an example config file.\n"+
"If node was recently upgraded and a previous network config file is used, then check updates for the config file.", err))
}
return err
}
func defaultLachesisConfig(ctx *cli.Context) lachesis.Config {
var cfg lachesis.Config
switch {
case ctx.GlobalIsSet(FakeNetFlag.Name):
_, accs, err := parseFakeGen(ctx.GlobalString(FakeNetFlag.Name))
if err != nil {
log.Crit("Invalid flag", "flag", FakeNetFlag.Name, "err", err)
}
cfg = lachesis.FakeNetConfig(accs)
case ctx.GlobalBool(utils.LegacyTestnetFlag.Name):
cfg = lachesis.TestNetConfig()
default:
cfg = lachesis.MainNetConfig()
}
return cfg
}
func setBootnodes(ctx *cli.Context, urls []string, cfg *node.Config) {
for _, url := range urls {
if url != "" {
node, err := discv5.ParseNode(url)
if err != nil {
log.Error("Bootstrap URL invalid", "enode", url, "err", err)
continue
}
cfg.P2P.BootstrapNodesV5 = append(cfg.P2P.BootstrapNodesV5, node)
}
}
}
func setDataDir(ctx *cli.Context, cfg *node.Config) {
defaultDataDir := DefaultDataDir()
switch {
case ctx.GlobalIsSet(utils.DataDirFlag.Name):
cfg.DataDir = ctx.GlobalString(utils.DataDirFlag.Name)
case ctx.GlobalIsSet(FakeNetFlag.Name):
_, accs, err := parseFakeGen(ctx.GlobalString(FakeNetFlag.Name))
if err != nil {
log.Crit("Invalid flag", "flag", FakeNetFlag.Name, "err", err)
}
cfg.DataDir = filepath.Join(defaultDataDir, fmt.Sprintf("fakenet-%d", len(accs.Accounts)))
case ctx.GlobalBool(utils.LegacyTestnetFlag.Name):
cfg.DataDir = filepath.Join(defaultDataDir, "testnet")
default:
cfg.DataDir = defaultDataDir
}
}
func setGPO(ctx *cli.Context, cfg *gasprice.Config) {
if ctx.GlobalIsSet(utils.LegacyGpoBlocksFlag.Name) {
cfg.Blocks = ctx.GlobalInt(utils.LegacyGpoBlocksFlag.Name)
}
if ctx.GlobalIsSet(utils.GpoBlocksFlag.Name) {
cfg.Blocks = ctx.GlobalInt(utils.GpoBlocksFlag.Name)
}
if ctx.GlobalIsSet(utils.LegacyGpoPercentileFlag.Name) {
cfg.Percentile = ctx.GlobalInt(utils.LegacyGpoPercentileFlag.Name)
}
if ctx.GlobalIsSet(utils.GpoPercentileFlag.Name) {
cfg.Percentile = ctx.GlobalInt(utils.GpoPercentileFlag.Name)
}
}
func setTxPool(ctx *cli.Context, cfg *evmcore.TxPoolConfig) {
if ctx.GlobalIsSet(utils.TxPoolLocalsFlag.Name) {
locals := strings.Split(ctx.GlobalString(utils.TxPoolLocalsFlag.Name), ",")
for _, account := range locals {
if trimmed := strings.TrimSpace(account); !common.IsHexAddress(trimmed) {
utils.Fatalf("Invalid account in --txpool.locals: %s", trimmed)
} else {
cfg.Locals = append(cfg.Locals, common.HexToAddress(account))
}
}
}
if ctx.GlobalIsSet(utils.TxPoolNoLocalsFlag.Name) {
cfg.NoLocals = ctx.GlobalBool(utils.TxPoolNoLocalsFlag.Name)
}
if ctx.GlobalIsSet(utils.TxPoolJournalFlag.Name) {
cfg.Journal = ctx.GlobalString(utils.TxPoolJournalFlag.Name)
}
if ctx.GlobalIsSet(utils.TxPoolRejournalFlag.Name) {
cfg.Rejournal = ctx.GlobalDuration(utils.TxPoolRejournalFlag.Name)
}
if ctx.GlobalIsSet(utils.TxPoolPriceLimitFlag.Name) {
cfg.PriceLimit = ctx.GlobalUint64(utils.TxPoolPriceLimitFlag.Name)
}
if ctx.GlobalIsSet(utils.TxPoolPriceBumpFlag.Name) {
cfg.PriceBump = ctx.GlobalUint64(utils.TxPoolPriceBumpFlag.Name)
}
if ctx.GlobalIsSet(utils.TxPoolAccountSlotsFlag.Name) {
cfg.AccountSlots = ctx.GlobalUint64(utils.TxPoolAccountSlotsFlag.Name)
}
if ctx.GlobalIsSet(utils.TxPoolGlobalSlotsFlag.Name) {
cfg.GlobalSlots = ctx.GlobalUint64(utils.TxPoolGlobalSlotsFlag.Name)
}
if ctx.GlobalIsSet(utils.TxPoolAccountQueueFlag.Name) {
cfg.AccountQueue = ctx.GlobalUint64(utils.TxPoolAccountQueueFlag.Name)
}
if ctx.GlobalIsSet(utils.TxPoolGlobalQueueFlag.Name) {
cfg.GlobalQueue = ctx.GlobalUint64(utils.TxPoolGlobalQueueFlag.Name)
}
if ctx.GlobalIsSet(utils.TxPoolLifetimeFlag.Name) {
cfg.Lifetime = ctx.GlobalDuration(utils.TxPoolLifetimeFlag.Name)
}
}
func gossipConfigWithFlags(ctx *cli.Context, src gossip.Config) gossip.Config {
cfg := src
// Avoid conflicting network flags
utils.CheckExclusive(ctx, FakeNetFlag, utils.DeveloperFlag, utils.LegacyTestnetFlag)
utils.CheckExclusive(ctx, FakeNetFlag, utils.DeveloperFlag, utils.ExternalSignerFlag) // Can't use both ephemeral unlocked and external signer
setGPO(ctx, &cfg.GPO)
setTxPool(ctx, &cfg.TxPool)
if ctx.GlobalIsSet(utils.NetworkIdFlag.Name) {
cfg.Net.NetworkID = ctx.GlobalUint64(utils.NetworkIdFlag.Name)
}
// TODO cache config
//if ctx.GlobalIsSet(utils.CacheFlag.Name) || ctx.GlobalIsSet(utils.CacheDatabaseFlag.Name) {
// cfg.DatabaseCache = ctx.GlobalInt(utils.CacheFlag.Name) * ctx.GlobalInt(utils.CacheDatabaseFlag.Name) / 100
//}
//if ctx.GlobalIsSet(utils.CacheFlag.Name) || ctx.GlobalIsSet(CacheTrieFlag.Name) {
// cfg.TrieCleanCache = ctx.GlobalInt(utils.CacheFlag.Name) * ctx.GlobalInt(CacheTrieFlag.Name) / 100
//}
//if ctx.GlobalIsSet(utils.CacheFlag.Name) || ctx.GlobalIsSet(CacheGCFlag.Name) {
// cfg.TrieDirtyCache = ctx.GlobalInt(utils.CacheFlag.Name) * ctx.GlobalInt(CacheGCFlag.Name) / 100
//}
if ctx.GlobalIsSet(utils.VMEnableDebugFlag.Name) {
cfg.EnablePreimageRecording = ctx.GlobalBool(utils.VMEnableDebugFlag.Name)
}
if ctx.GlobalIsSet(utils.EWASMInterpreterFlag.Name) {
cfg.EWASMInterpreter = ctx.GlobalString(utils.EWASMInterpreterFlag.Name)
}
if ctx.GlobalIsSet(utils.EVMInterpreterFlag.Name) {
cfg.EVMInterpreter = ctx.GlobalString(utils.EVMInterpreterFlag.Name)
}
if ctx.GlobalIsSet(utils.RPCGlobalGasCap.Name) {
cfg.RPCGasCap = ctx.GlobalUint64(utils.RPCGlobalGasCap.Name)
}
if ctx.GlobalIsSet(utils.RPCGlobalTxFeeCap.Name) {
cfg.RPCTxFeeCap = ctx.GlobalFloat64(utils.RPCGlobalTxFeeCap.Name)
}
return cfg
}
func nodeConfigWithFlags(ctx *cli.Context, cfg node.Config) node.Config {
utils.SetNodeConfig(ctx, &cfg)
if !ctx.GlobalIsSet(FakeNetFlag.Name) {
setBootnodes(ctx, Bootnodes, &cfg)
}
setDataDir(ctx, &cfg)
return cfg
}
func makeAllConfigs(ctx *cli.Context) *config {
// Defaults (low priority)
net := defaultLachesisConfig(ctx)
cfg := config{Lachesis: gossip.DefaultConfig(net), Node: defaultNodeConfig()}
// Load config file (medium priority)
if file := ctx.GlobalString(configFileFlag.Name); file != "" {
if err := loadAllConfigs(file, &cfg); err != nil {
utils.Fatalf("%v", err)
}
}
// Apply flags (high priority)
cfg.Lachesis = gossipConfigWithFlags(ctx, cfg.Lachesis)
cfg.Node = nodeConfigWithFlags(ctx, cfg.Node)
return &cfg
}
func defaultNodeConfig() node.Config {
cfg := NodeDefaultConfig
cfg.Name = clientIdentifier
cfg.Version = params.VersionWithCommit(gitCommit, gitDate)
cfg.HTTPModules = append(cfg.HTTPModules, "eth", "ftm", "sfc", "web3")
cfg.WSModules = append(cfg.WSModules, "eth", "ftm", "sfc", "web3")
cfg.IPCPath = "lachesis.ipc"
cfg.DataDir = DefaultDataDir()
return cfg
}
// dumpConfig is the dumpconfig command.
func dumpConfig(ctx *cli.Context) error {
cfg := makeAllConfigs(ctx)
comment := ""
out, err := tomlSettings.Marshal(&cfg)
if err != nil {
return err
}
log.Info("Dumping legacy Lachesis config file")
dump := os.Stdout
if ctx.NArg() > 0 {
dump, err = os.OpenFile(ctx.Args().Get(0), os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer dump.Close()
}
dump.WriteString(comment)
dump.Write(out)
log.Info("Dumping Opera config file")
operaDatadir := path.Join(cfg.Node.DataDir, "opera")
operaArgs := excludeArg(os.Args, "--"+validatorFlag.Name, true)
operaArgs = excludeArg(operaArgs, "--"+configFileFlag.Name, true)
operaArgs = excludeArg(operaArgs, "--"+DataDirFlag.Name, true)
operaArgs = addFrontArgs(operaArgs, []string{"--" + DataDirFlag.Name, operaDatadir})
operaArgs = excludeArg(operaArgs, "--"+utils.LegacyTestnetFlag.Name, false)
metrics.Enabled = false
return launcher.Launch(operaArgs)
}