From 1c073837357c55a86c75906158fd4c51378f6eca Mon Sep 17 00:00:00 2001 From: S29 Date: Wed, 4 Dec 2024 17:49:07 +0100 Subject: [PATCH 1/5] feat: added sample config.toml.template && updated Readme to reflect this --- .gitignore | 4 +++ README.md | 39 +++++++++++++++++++++++++++- config.toml.template | 61 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 config.toml.template diff --git a/.gitignore b/.gitignore index 5f3e460b..3b448bae 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,7 @@ go.work.sum /main dist/ cache/ + +# Ignore customized configuration files +*.toml +!*.toml.template diff --git a/README.md b/README.md index d314df9a..d7f82dec 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,44 @@ brew install ethereum-optimism/tap/supersim Download the executable for your platform from the [GitHub releases page](https://github.com/ethereum-optimism/supersim/releases). -### 3. Start `supersim` in vanilla mode +### 3. Configure `supersim` (Optional) + +#### Option 1: Use the .toml.template File +The configuration file for Supersim is provided as a `.toml.template` file. This ensures you can start with a pre-defined setup while allowing you to customize it to your needs. + +#### Copy the .toml.template file to .toml: +Rename the template file to remove the `.template` extension. Use the following command: +```sh +cp config.toml.template config.toml +``` +#### Edit the .toml File: +Open `config.toml` in your preferred text editor (e.g., VS Code, Vim, or Nano) and customize the parameters to suit your setup. For example: +```toml +# Supersim Configuration File Example + +# L1 instance settings +# Host address for the L1 instance +# Default: "127.0.0.1" +l1.host = "0.0.0.0" + +# Listening port for the L1 instance. `0` binds to any available port +# Default: 8545 +l1.port = 3000 +``` + +Save the file. + + +#### Option 2: Configure via CLI +Supersim can also be configured directly via the CLI without editing a configuration file. For example, you can specify the required parameters when starting Supersim: +```sh +supersim --l1.host "0.0.0.0" --l1.port 3000 --l2.host "0.0.0.0" --l2.starting.port 3001 + +``` + + + +### 4. Start `supersim` in vanilla mode ```sh supersim diff --git a/config.toml.template b/config.toml.template new file mode 100644 index 00000000..5190c50b --- /dev/null +++ b/config.toml.template @@ -0,0 +1,61 @@ +# Supersim Configuration File Example + +[global] +# Admin server settings +# Listening port for the admin server +# Default: 8420 +admin.port = 8420 + +# Interoperability settings +# Automatically relay messages sent to the L2ToL2CrossDomainMessenger +# Default: false +interop.autorelay = false + +# Delay before relaying messages sent to the L2ToL2CrossDomainMessenger +# Default: 0 +interop.delay = 0 + +# L1 instance settings +# Host address for the L1 instance +# Default: "127.0.0.1" +l1.host = "127.0.0.1" + +# Listening port for the L1 instance. `0` binds to any available port +# Default: 8545 +l1.port = 8545 + +# L2 instance settings +# Host address for L2 instances +# Default: "127.0.0.1" +l2.host = "127.0.0.1" + +# Starting port to increment from for L2 chains. `0` binds each chain to any available port +# Default: 9545 +l2.starting.port = 9545 + +# Logging settings +# Color the log output if in terminal mode +# Default: false +log.color = false + +# Format the log output. Supported formats: 'text', 'terminal', 'logfmt', 'json', 'json-pretty' +# Default: "text" +log.format = "text" + +# The lowest log level that will be output +# Default: "INFO" +log.level = "INFO" + +# Show pid in the log +# Default: false +log.pid = false + +# Directory to store logs +logs.directory = "" + +[commands] +[fork] +l1.fork.height = 0 # L1 height to fork the superchain (bounds L2 time). `0` for latest +chains = ["automata", "base"] # Chains to fork in the superchain +network = "mainnet" # Superchain network. Options: sepolia-dev-0, mainnet, sepolia +interop.enabled = true \ No newline at end of file From cded86198839e970943aa4a6be5b9aa8aca83c03 Mon Sep 17 00:00:00 2001 From: S29 Date: Thu, 5 Dec 2024 10:33:27 +0100 Subject: [PATCH 2/5] added tests for toml config --- config.toml.template | 68 +++++++--------------- config/cli.go | 130 ++++++++++++++++++++++++++++++++++++------ config/config_test.go | 69 ++++++++++++++++++++++ go.mod | 2 +- go.sum | 2 + 5 files changed, 204 insertions(+), 67 deletions(-) create mode 100644 config/config_test.go diff --git a/config.toml.template b/config.toml.template index 5190c50b..3f11febd 100644 --- a/config.toml.template +++ b/config.toml.template @@ -1,61 +1,33 @@ -# Supersim Configuration File Example +Title = "Supersim Configuration" +Desc = "Configuration for the Supersim application." + -[global] # Admin server settings -# Listening port for the admin server -# Default: 8420 -admin.port = 8420 +admin_port = 8420 # Listening port for the admin server (Default: 8420) # Interoperability settings -# Automatically relay messages sent to the L2ToL2CrossDomainMessenger -# Default: false -interop.autorelay = false - -# Delay before relaying messages sent to the L2ToL2CrossDomainMessenger -# Default: 0 -interop.delay = 0 +interop_autorelay = false # Automatically relay messages sent to the L2ToL2CrossDomainMessenger (Default: false) +interop_delay = 0 # Delay before relaying messages sent to the L2ToL2CrossDomainMessenger (Default: 0) # L1 instance settings -# Host address for the L1 instance -# Default: "127.0.0.1" -l1.host = "127.0.0.1" - -# Listening port for the L1 instance. `0` binds to any available port -# Default: 8545 -l1.port = 8545 +l1_host = "127.0.0.1" # Host address for the L1 instance (Default: "127.0.0.1") +l1_port = 8545 # Listening port for the L1 instance. `0` binds to any available port (Default: 8545) # L2 instance settings -# Host address for L2 instances -# Default: "127.0.0.1" -l2.host = "127.0.0.1" - -# Starting port to increment from for L2 chains. `0` binds each chain to any available port -# Default: 9545 -l2.starting.port = 9545 +l2_host = "127.0.0.1" # Host address for L2 instances (Default: "127.0.0.1") +l2_starting_port = 9545 # Starting port to increment from for L2 chains. `0` binds each chain to any available port (Default: 9545) # Logging settings -# Color the log output if in terminal mode -# Default: false -log.color = false - -# Format the log output. Supported formats: 'text', 'terminal', 'logfmt', 'json', 'json-pretty' -# Default: "text" -log.format = "text" - -# The lowest log level that will be output -# Default: "INFO" -log.level = "INFO" - -# Show pid in the log -# Default: false -log.pid = false - -# Directory to store logs -logs.directory = "" +log_color = false # Color the log output if in terminal mode (Default: false) +log_format = "text" # Format the log output. Supported formats: 'text', 'terminal', 'logfmt', 'json', 'json-pretty' (Default: "text") +log_level = "INFO" # The lowest log level that will be output (Default: "INFO") +log_pid = false # Show pid in the log (Default: false) +logs_directory = "" # Directory to store logs [commands] + [fork] -l1.fork.height = 0 # L1 height to fork the superchain (bounds L2 time). `0` for latest -chains = ["automata", "base"] # Chains to fork in the superchain -network = "mainnet" # Superchain network. Options: sepolia-dev-0, mainnet, sepolia -interop.enabled = true \ No newline at end of file +# l1_fork_height = 0 # L1 height to fork the superchain (bounds L2 time). `0` for latest +# chains = ["automata", "base"] # Chains to fork in the superchain +# network = "mainnet" # Superchain network. Options: sepolia-dev-0, mainnet, sepolia +# interop_enabled = true \ No newline at end of file diff --git a/config/cli.go b/config/cli.go index c6a7f73a..310681ac 100644 --- a/config/cli.go +++ b/config/cli.go @@ -2,9 +2,11 @@ package config import ( "fmt" + "os" "strings" opservice "github.com/ethereum-optimism/optimism/op-service" + "github.com/pelletier/go-toml/v2" "net" "regexp" @@ -154,32 +156,40 @@ type CLIConfig struct { L2Host string } -func ReadCLIConfig(ctx *cli.Context) (*CLIConfig, error) { - cfg := &CLIConfig{ - AdminPort: ctx.Uint64(AdminPortFlagName), +type ForkTOMLConfig struct { + L1ForkHeight uint64 `toml:"l1_fork_height"` + Network string `toml:"network"` + Chains []string `toml:"chains"` - L1Port: ctx.Uint64(L1PortFlagName), - L2StartingPort: ctx.Uint64(L2StartingPortFlagName), + InteropEnabled bool `toml:"interop_enabled"` +} - InteropAutoRelay: ctx.Bool(InteropAutoRelayFlagName), - InteropDelay: ctx.Uint64(InteropDelayFlagName), +type TOMLConfig struct { + AdminPort uint64 `toml:"admin_port"` - LogsDirectory: ctx.String(LogsDirectoryFlagName), + L1Port uint64 `toml:"l1_port"` + L2StartingPort uint64 `toml:"l2_starting_port"` - L1Host: ctx.String(L1HostFlagName), - L2Host: ctx.String(L2HostFlagName), - } + InteropAutoRelay bool `toml:"interop_autorelay"` + InteropDelay uint64 `toml:"interop_delay"` - if ctx.Command.Name == ForkCommandName { - cfg.ForkConfig = &ForkCLIConfig{ - L1ForkHeight: ctx.Uint64(L1ForkHeightFlagName), - Network: ctx.String(NetworkFlagName), - Chains: ctx.StringSlice(ChainsFlagName), + LogsDirectory string `toml:"logs_directory"` - InteropEnabled: ctx.Bool(InteropEnabledFlagName), - } + ForkConfig *ForkTOMLConfig `toml:"fork"` + + L1Host string `toml:"l1_host"` + L2Host string `toml:"l2_host"` +} + +func ReadCLIConfig(ctx *cli.Context) (*CLIConfig, error) { + cfg := &CLIConfig{} + + if err := readTOMLConfig(cfg); err != nil { + return nil, err } + populateFromCLIContext(cfg, ctx) + return cfg, cfg.Check() } @@ -244,3 +254,87 @@ func validateHost(host string) error { return nil } + +func readTOMLConfig(cfg *CLIConfig) error { + tomlCfgFile := "config.toml" + if _, err := os.Stat(tomlCfgFile); err == nil { + content, err := os.ReadFile(tomlCfgFile) + if err != nil { + return fmt.Errorf("failed to read TOML config: %w", err) + } + + var tomlCfg TOMLConfig + if err := toml.Unmarshal(content, &tomlCfg); err != nil { + return fmt.Errorf("error parsing TOML: %v", err) + } + + cfg.AdminPort = tomlCfg.AdminPort + cfg.L1Port = tomlCfg.L1Port + cfg.L2StartingPort = tomlCfg.L2StartingPort + cfg.InteropAutoRelay = tomlCfg.InteropAutoRelay + cfg.InteropDelay = tomlCfg.InteropDelay + cfg.LogsDirectory = tomlCfg.LogsDirectory + cfg.L1Host = tomlCfg.L1Host + cfg.L2Host = tomlCfg.L2Host + + if tomlCfg.ForkConfig != nil { + cfg.ForkConfig = &ForkCLIConfig{} + if tomlCfg.ForkConfig.L1ForkHeight != 0 { + cfg.ForkConfig.L1ForkHeight = tomlCfg.ForkConfig.L1ForkHeight + } + if len(tomlCfg.ForkConfig.Chains) > 0 { + cfg.ForkConfig.Chains = tomlCfg.ForkConfig.Chains + } + if tomlCfg.ForkConfig.Network != "" { + cfg.ForkConfig.Network = tomlCfg.ForkConfig.Network + } + if tomlCfg.ForkConfig.InteropEnabled { + cfg.ForkConfig.InteropEnabled = tomlCfg.ForkConfig.InteropEnabled + } + + if cfg.ForkConfig.L1ForkHeight == 0 && len(cfg.ForkConfig.Chains) == 0 && cfg.ForkConfig.Network == "" { + cfg.ForkConfig = nil + } + } + } else if !os.IsNotExist(err) { + return fmt.Errorf("error checking TOML config file: %w", err) + } + + return nil +} + +func populateFromCLIContext(cfg *CLIConfig, ctx *cli.Context) { + if cfg.AdminPort == 0 { + cfg.AdminPort = ctx.Uint64(AdminPortFlagName) + } + if cfg.L1Port == 0 { + cfg.L1Port = ctx.Uint64(L1PortFlagName) + } + if cfg.L2StartingPort == 0 { + cfg.L2StartingPort = ctx.Uint64(L2StartingPortFlagName) + } + if !cfg.InteropAutoRelay { + cfg.InteropAutoRelay = ctx.Bool(InteropAutoRelayFlagName) + } + if cfg.InteropDelay == 0 { + cfg.InteropDelay = ctx.Uint64(InteropDelayFlagName) + } + if cfg.LogsDirectory == "" { + cfg.LogsDirectory = ctx.String(LogsDirectoryFlagName) + } + if cfg.L1Host == "" { + cfg.L1Host = ctx.String(L1HostFlagName) + } + if cfg.L2Host == "" { + cfg.L2Host = ctx.String(L2HostFlagName) + } + + if ctx.Command.Name == ForkCommandName { + cfg.ForkConfig = &ForkCLIConfig{ + L1ForkHeight: ctx.Uint64(L1ForkHeightFlagName), + Network: ctx.String(NetworkFlagName), + Chains: ctx.StringSlice(ChainsFlagName), + InteropEnabled: ctx.Bool(InteropEnabledFlagName), + } + } +} diff --git a/config/config_test.go b/config/config_test.go new file mode 100644 index 00000000..8eaddacc --- /dev/null +++ b/config/config_test.go @@ -0,0 +1,69 @@ +package config + +import ( + "os" + "path/filepath" + "testing" + + "github.com/urfave/cli/v2" +) + +func TestReadCLIConfig(t *testing.T) { + cwd, err := os.Getwd() + if err != nil { + t.Fatalf("unable to determine executable path: %v", err) + } + + cfgPath := filepath.Join(cwd, "config.toml") + + tomlContent := ` + admin_port = 8420 + l1_port = 8545 + l2_starting_port = 9545 + interop_autorelay = true + interop_delay = 0 + logs_directory = "/var/logs" + l1_host = "127.0.0.1" + l2_host = "127.0.0.1" + ` + + tmpFile, err := os.Create(cfgPath) + if err != nil { + t.Fatalf("failed to create config.toml file: %v", err) + } + defer os.Remove(cfgPath) + + if _, err := tmpFile.WriteString(tomlContent); err != nil { + t.Fatalf("failed to write to config.toml file: %v", err) + } + tmpFile.Close() + + ctx := cli.NewContext(nil, nil, nil) + + cfg, err := ReadCLIConfig(ctx) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + + if cfg.AdminPort != 8420 { + t.Errorf("expected AdminPort to be 8420, got %d", cfg.AdminPort) + } + if cfg.L1Port != 8545 { + t.Errorf("expected L1Port to be 8545, got %d", cfg.L1Port) + } + if cfg.L2StartingPort != 9545 { + t.Errorf("expected L2StartingPort to be 9545, got %d", cfg.L2StartingPort) + } + if cfg.InteropAutoRelay != true { + t.Errorf("expected InteropAutoRelay to be true, got %v", cfg.InteropAutoRelay) + } + if cfg.LogsDirectory != "/var/logs" { + t.Errorf("expected LogsDirectory to be '/var/logs', got '%s'", cfg.LogsDirectory) + } + if cfg.L1Host != "127.0.0.1" { + t.Errorf("expected L1Host to be '127.0.0.1', got '%s'", cfg.L1Host) + } + if cfg.L2Host != "127.0.0.1" { + t.Errorf("expected L2Host to be '127.0.0.1', got '%s'", cfg.L2Host) + } +} diff --git a/go.mod b/go.mod index b19d1a1a..53b326eb 100644 --- a/go.mod +++ b/go.mod @@ -88,7 +88,7 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect github.com/olekukonko/tablewriter v0.0.5 // indirect - github.com/pelletier/go-toml/v2 v2.2.2 // indirect + github.com/pelletier/go-toml/v2 v2.2.3 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/prometheus/client_golang v1.20.5 // indirect diff --git a/go.sum b/go.sum index 51a5c45a..4a34b5e0 100644 --- a/go.sum +++ b/go.sum @@ -304,6 +304,8 @@ github.com/onsi/gomega v1.19.0 h1:4ieX6qQjPP/BfC3mpsAtIGGlxTWPeA3Inl/7DtXw1tw= github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro= github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= +github.com/pelletier/go-toml/v2 v2.2.3 h1:YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M= +github.com/pelletier/go-toml/v2 v2.2.3/go.mod h1:MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc= github.com/pingcap/errors v0.11.4 h1:lFuQV/oaUMGcD2tqt+01ROSmJs75VG1ToEOkZIZ4nE4= github.com/pingcap/errors v0.11.4/go.mod h1:Oi8TUi2kEtXXLMJk9l1cGmz20kV3TaQ0usTwv5KuLY8= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= From 70c856dfad3dc79ec7d651af7bcade3a21026ac0 Mon Sep 17 00:00:00 2001 From: S29 Date: Wed, 8 Jan 2025 06:02:19 +0100 Subject: [PATCH 3/5] update readme --- README.md | 74 ++++++++++++++++++++++++++----------------------------- 1 file changed, 35 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 6f1e03c1..42e6eb73 100644 --- a/README.md +++ b/README.md @@ -31,44 +31,7 @@ brew install ethereum-optimism/tap/supersim Download the executable for your platform from the [GitHub releases page](https://github.com/ethereum-optimism/supersim/releases). -### 3. Configure `supersim` (Optional) - -#### Option 1: Use the .toml.template File -The configuration file for Supersim is provided as a `.toml.template` file. This ensures you can start with a pre-defined setup while allowing you to customize it to your needs. - -#### Copy the .toml.template file to .toml: -Rename the template file to remove the `.template` extension. Use the following command: -```sh -cp config.toml.template config.toml -``` -#### Edit the .toml File: -Open `config.toml` in your preferred text editor (e.g., VS Code, Vim, or Nano) and customize the parameters to suit your setup. For example: -```toml -# Supersim Configuration File Example - -# L1 instance settings -# Host address for the L1 instance -# Default: "127.0.0.1" -l1.host = "0.0.0.0" - -# Listening port for the L1 instance. `0` binds to any available port -# Default: 8545 -l1.port = 3000 -``` - -Save the file. - - -#### Option 2: Configure via CLI -Supersim can also be configured directly via the CLI without editing a configuration file. For example, you can specify the required parameters when starting Supersim: -```sh -supersim --l1.host "0.0.0.0" --l1.port 3000 --l2.host "0.0.0.0" --l2.starting.port 3001 - -``` - - - -### 4. Start `supersim` in vanilla mode +### 3. Start `supersim` in vanilla mode ```sh supersim @@ -95,7 +58,6 @@ L2s: Predeploy Contracts Spec ( https://specs.optimism.io/protocol/predeploys.ht - L1StandardBridge: 0x2D8543c236a4d626f54B51Fa8bc229a257C5143E ``` - ### 4. Start testing multichain features 🚀 For **getting up to speed with supersim**, watch the [**🎥 Supersim 101 Training Session video**](https://www.youtube.com/live/Kh4fNshcl5Y?t=30s) @@ -237,6 +199,40 @@ L2s: Predeploy Contracts Spec ( https://specs.optimism.io/protocol/predeploys.ht - L1CrossDomainMessenger: 0xdC40a14d9abd6F410226f1E6de71aE03441ca506 - L1StandardBridge: 0x3e2Ea9B92B7E48A52296fD261dc26fd995284631 ``` +## ⚙️ Configuration + +#### Option 1: Use the .toml.template File +The configuration file for Supersim is provided as a `.toml.template` file. This ensures you can start with a pre-defined setup while allowing you to customize it to your needs. + +#### Copy the .toml.template file to .toml: +Rename the template file to remove the `.template` extension. Use the following command: +```sh +cp config.toml.template config.toml +``` +#### Edit the .toml File: +Open `config.toml` in your preferred text editor (e.g., VS Code, Vim, or Nano) and customize the parameters to suit your setup. For example: +```toml +# Supersim Configuration File Example + +# L1 instance settings +# Host address for the L1 instance +# Default: "127.0.0.1" +l1.host = "0.0.0.0" + +# Listening port for the L1 instance. `0` binds to any available port +# Default: 8545 +l1.port = 3000 +``` + +Save the file. + + +#### Option 2: Configure via CLI +Supersim can also be configured directly via the CLI without editing a configuration file. For example, you can specify the required parameters when starting Supersim: +```sh +supersim --l1.host "0.0.0.0" --l1.port 3000 --l2.host "0.0.0.0" --l2.starting.port 3001 + +``` ### Development From 4aa6d0114347cfa5ada2cef1ba50a7dd4463de80 Mon Sep 17 00:00:00 2001 From: S29 Date: Thu, 9 Jan 2025 14:30:20 +0100 Subject: [PATCH 4/5] single config struct & applyToml Fn --- config/cli.go | 139 +++++++++++++----------------------------- config/config_test.go | 5 ++ 2 files changed, 48 insertions(+), 96 deletions(-) diff --git a/config/cli.go b/config/cli.go index 310681ac..d53289ca 100644 --- a/config/cli.go +++ b/config/cli.go @@ -132,31 +132,6 @@ func ForkCLIFlags(envPrefix string) []cli.Flag { } type ForkCLIConfig struct { - L1ForkHeight uint64 - Network string - Chains []string - - InteropEnabled bool -} - -type CLIConfig struct { - AdminPort uint64 - - L1Port uint64 - L2StartingPort uint64 - - InteropAutoRelay bool - InteropDelay uint64 - - LogsDirectory string - - ForkConfig *ForkCLIConfig - - L1Host string - L2Host string -} - -type ForkTOMLConfig struct { L1ForkHeight uint64 `toml:"l1_fork_height"` Network string `toml:"network"` Chains []string `toml:"chains"` @@ -164,7 +139,7 @@ type ForkTOMLConfig struct { InteropEnabled bool `toml:"interop_enabled"` } -type TOMLConfig struct { +type CLIConfig struct { AdminPort uint64 `toml:"admin_port"` L1Port uint64 `toml:"l1_port"` @@ -175,7 +150,7 @@ type TOMLConfig struct { LogsDirectory string `toml:"logs_directory"` - ForkConfig *ForkTOMLConfig `toml:"fork"` + ForkConfig *ForkCLIConfig `toml:"fork"` L1Host string `toml:"l1_host"` L2Host string `toml:"l2_host"` @@ -184,11 +159,11 @@ type TOMLConfig struct { func ReadCLIConfig(ctx *cli.Context) (*CLIConfig, error) { cfg := &CLIConfig{} - if err := readTOMLConfig(cfg); err != nil { + if err := applyTOMLConfig(cfg); err != nil { return nil, err } - populateFromCLIContext(cfg, ctx) + // populateFromCLIContext(cfg, ctx) return cfg, cfg.Check() } @@ -255,7 +230,7 @@ func validateHost(host string) error { return nil } -func readTOMLConfig(cfg *CLIConfig) error { +func applyTOMLConfig(cfg *CLIConfig) error { tomlCfgFile := "config.toml" if _, err := os.Stat(tomlCfgFile); err == nil { content, err := os.ReadFile(tomlCfgFile) @@ -263,39 +238,9 @@ func readTOMLConfig(cfg *CLIConfig) error { return fmt.Errorf("failed to read TOML config: %w", err) } - var tomlCfg TOMLConfig - if err := toml.Unmarshal(content, &tomlCfg); err != nil { + if err := toml.Unmarshal(content, &cfg); err != nil { return fmt.Errorf("error parsing TOML: %v", err) } - - cfg.AdminPort = tomlCfg.AdminPort - cfg.L1Port = tomlCfg.L1Port - cfg.L2StartingPort = tomlCfg.L2StartingPort - cfg.InteropAutoRelay = tomlCfg.InteropAutoRelay - cfg.InteropDelay = tomlCfg.InteropDelay - cfg.LogsDirectory = tomlCfg.LogsDirectory - cfg.L1Host = tomlCfg.L1Host - cfg.L2Host = tomlCfg.L2Host - - if tomlCfg.ForkConfig != nil { - cfg.ForkConfig = &ForkCLIConfig{} - if tomlCfg.ForkConfig.L1ForkHeight != 0 { - cfg.ForkConfig.L1ForkHeight = tomlCfg.ForkConfig.L1ForkHeight - } - if len(tomlCfg.ForkConfig.Chains) > 0 { - cfg.ForkConfig.Chains = tomlCfg.ForkConfig.Chains - } - if tomlCfg.ForkConfig.Network != "" { - cfg.ForkConfig.Network = tomlCfg.ForkConfig.Network - } - if tomlCfg.ForkConfig.InteropEnabled { - cfg.ForkConfig.InteropEnabled = tomlCfg.ForkConfig.InteropEnabled - } - - if cfg.ForkConfig.L1ForkHeight == 0 && len(cfg.ForkConfig.Chains) == 0 && cfg.ForkConfig.Network == "" { - cfg.ForkConfig = nil - } - } } else if !os.IsNotExist(err) { return fmt.Errorf("error checking TOML config file: %w", err) } @@ -303,38 +248,40 @@ func readTOMLConfig(cfg *CLIConfig) error { return nil } -func populateFromCLIContext(cfg *CLIConfig, ctx *cli.Context) { - if cfg.AdminPort == 0 { - cfg.AdminPort = ctx.Uint64(AdminPortFlagName) - } - if cfg.L1Port == 0 { - cfg.L1Port = ctx.Uint64(L1PortFlagName) - } - if cfg.L2StartingPort == 0 { - cfg.L2StartingPort = ctx.Uint64(L2StartingPortFlagName) - } - if !cfg.InteropAutoRelay { - cfg.InteropAutoRelay = ctx.Bool(InteropAutoRelayFlagName) - } - if cfg.InteropDelay == 0 { - cfg.InteropDelay = ctx.Uint64(InteropDelayFlagName) - } - if cfg.LogsDirectory == "" { - cfg.LogsDirectory = ctx.String(LogsDirectoryFlagName) - } - if cfg.L1Host == "" { - cfg.L1Host = ctx.String(L1HostFlagName) - } - if cfg.L2Host == "" { - cfg.L2Host = ctx.String(L2HostFlagName) - } - - if ctx.Command.Name == ForkCommandName { - cfg.ForkConfig = &ForkCLIConfig{ - L1ForkHeight: ctx.Uint64(L1ForkHeightFlagName), - Network: ctx.String(NetworkFlagName), - Chains: ctx.StringSlice(ChainsFlagName), - InteropEnabled: ctx.Bool(InteropEnabledFlagName), - } - } -} +// If toml config is set then + +// func populateFromCLIContext(cfg *CLIConfig, ctx *cli.Context) { +// if cfg.AdminPort == 0 { +// cfg.AdminPort = ctx.Uint64(AdminPortFlagName) +// } +// if cfg.L1Port == 0 { +// cfg.L1Port = ctx.Uint64(L1PortFlagName) +// } +// if cfg.L2StartingPort == 0 { +// cfg.L2StartingPort = ctx.Uint64(L2StartingPortFlagName) +// } +// if !cfg.InteropAutoRelay { +// cfg.InteropAutoRelay = ctx.Bool(InteropAutoRelayFlagName) +// } +// if cfg.InteropDelay == 0 { +// cfg.InteropDelay = ctx.Uint64(InteropDelayFlagName) +// } +// if cfg.LogsDirectory == "" { +// cfg.LogsDirectory = ctx.String(LogsDirectoryFlagName) +// } +// if cfg.L1Host == "" { +// cfg.L1Host = ctx.String(L1HostFlagName) +// } +// if cfg.L2Host == "" { +// cfg.L2Host = ctx.String(L2HostFlagName) +// } + +// if ctx.Command.Name == ForkCommandName { +// cfg.ForkConfig = &ForkCLIConfig{ +// L1ForkHeight: ctx.Uint64(L1ForkHeightFlagName), +// Network: ctx.String(NetworkFlagName), +// Chains: ctx.StringSlice(ChainsFlagName), +// InteropEnabled: ctx.Bool(InteropEnabledFlagName), +// } +// } +// } diff --git a/config/config_test.go b/config/config_test.go index 8eaddacc..150752f5 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -25,6 +25,11 @@ func TestReadCLIConfig(t *testing.T) { logs_directory = "/var/logs" l1_host = "127.0.0.1" l2_host = "127.0.0.1" + [fork] + l1_fork_height = 0 + chains = ["automata", "base"] # Chains to fork in the superchain + network = "mainnet" # Superchain network. Options: sepolia-dev-0, mainnet, sepolia + interop_enabled = true ` tmpFile, err := os.Create(cfgPath) From fed5e889d86c09ee7c4d9e3745efee2247dbb2dd Mon Sep 17 00:00:00 2001 From: S29 Date: Thu, 9 Jan 2025 15:01:18 +0100 Subject: [PATCH 5/5] cli precedence --- config/cli.go | 88 +++++++++++++++++++++++++++++---------------------- 1 file changed, 50 insertions(+), 38 deletions(-) diff --git a/config/cli.go b/config/cli.go index d53289ca..0dde9d9a 100644 --- a/config/cli.go +++ b/config/cli.go @@ -163,7 +163,7 @@ func ReadCLIConfig(ctx *cli.Context) (*CLIConfig, error) { return nil, err } - // populateFromCLIContext(cfg, ctx) + populateFromCLIContext(cfg, ctx) return cfg, cfg.Check() } @@ -248,40 +248,52 @@ func applyTOMLConfig(cfg *CLIConfig) error { return nil } -// If toml config is set then - -// func populateFromCLIContext(cfg *CLIConfig, ctx *cli.Context) { -// if cfg.AdminPort == 0 { -// cfg.AdminPort = ctx.Uint64(AdminPortFlagName) -// } -// if cfg.L1Port == 0 { -// cfg.L1Port = ctx.Uint64(L1PortFlagName) -// } -// if cfg.L2StartingPort == 0 { -// cfg.L2StartingPort = ctx.Uint64(L2StartingPortFlagName) -// } -// if !cfg.InteropAutoRelay { -// cfg.InteropAutoRelay = ctx.Bool(InteropAutoRelayFlagName) -// } -// if cfg.InteropDelay == 0 { -// cfg.InteropDelay = ctx.Uint64(InteropDelayFlagName) -// } -// if cfg.LogsDirectory == "" { -// cfg.LogsDirectory = ctx.String(LogsDirectoryFlagName) -// } -// if cfg.L1Host == "" { -// cfg.L1Host = ctx.String(L1HostFlagName) -// } -// if cfg.L2Host == "" { -// cfg.L2Host = ctx.String(L2HostFlagName) -// } - -// if ctx.Command.Name == ForkCommandName { -// cfg.ForkConfig = &ForkCLIConfig{ -// L1ForkHeight: ctx.Uint64(L1ForkHeightFlagName), -// Network: ctx.String(NetworkFlagName), -// Chains: ctx.StringSlice(ChainsFlagName), -// InteropEnabled: ctx.Bool(InteropEnabledFlagName), -// } -// } -// } +func populateFromCLIContext(cfg *CLIConfig, ctx *cli.Context) { + adminPort := ctx.Uint64(AdminPortFlagName) + if adminPort != 0 { + cfg.AdminPort = adminPort + } + + l1Port := ctx.Uint64(L1PortFlagName) + if l1Port != 0 { + cfg.L1Port = l1Port + } + + l2StartingPort := ctx.Uint64(L2StartingPortFlagName) + if l2StartingPort != 0 { + cfg.L2StartingPort = l2StartingPort + } + + if ctx.Bool(InteropAutoRelayFlagName) { + cfg.InteropAutoRelay = true + } + + interopDelay := ctx.Uint64(InteropDelayFlagName) + if interopDelay != 0 { + cfg.InteropDelay = interopDelay + } + + logsDirectory := ctx.String(LogsDirectoryFlagName) + if len(logsDirectory) != 0 { + cfg.LogsDirectory = logsDirectory + } + + l1Host := ctx.String(L1HostFlagName) + if len(l1Host) != 0 { + cfg.L1Host = l1Host + } + + l2Host := ctx.String(L2HostFlagName) + if len(l2Host) != 0 { + cfg.L2Host = l2Host + } + + if ctx.Command.Name == ForkCommandName { + cfg.ForkConfig = &ForkCLIConfig{ + L1ForkHeight: ctx.Uint64(L1ForkHeightFlagName), + Network: ctx.String(NetworkFlagName), + Chains: ctx.StringSlice(ChainsFlagName), + InteropEnabled: ctx.Bool(InteropEnabledFlagName), + } + } +}