From 2310d7c30530afaadbc03ac9cad9c5e18fbd5cdd Mon Sep 17 00:00:00 2001 From: Joe Clapis Date: Tue, 23 Jul 2024 09:30:02 -0400 Subject: [PATCH] Refactored network settings loading to be at app start for headless mode support --- go.mod | 2 + install/deploy/networks/devnet.yml | 58 +++++++++---------- install/deploy/networks/holesky.yml | 58 +++++++++---------- install/deploy/networks/mainnet.yml | 58 +++++++++---------- rocketpool-cli/client/config.go | 18 +----- rocketpool-cli/client/utils.go | 9 --- rocketpool-cli/commands/service/commands.go | 4 +- rocketpool-cli/rocketpool-cli.go | 13 ++++- rocketpool-cli/rocketpool-cli_test.go | 38 ++++++++++++ rocketpool-cli/settings/smartnode_settings.go | 53 ++++------------- rocketpool-cli/settings/system_settings.go | 55 ++++++++++++++++++ 11 files changed, 210 insertions(+), 156 deletions(-) create mode 100644 rocketpool-cli/settings/system_settings.go diff --git a/go.mod b/go.mod index 8672f1464..a6785d079 100644 --- a/go.mod +++ b/go.mod @@ -173,4 +173,6 @@ require ( rsc.io/tmplfunc v0.0.3 // indirect ) +require github.com/d4l3k/messagediff v1.2.1 // indirect + replace github.com/wealdtech/go-merkletree v1.0.1-0.20190605192610-2bb163c2ea2a => github.com/rocket-pool/go-merkletree v1.0.1-0.20220406020931-c262d9b976dd diff --git a/install/deploy/networks/devnet.yml b/install/deploy/networks/devnet.yml index ba79d58ee..ee5651a48 100644 --- a/install/deploy/networks/devnet.yml +++ b/install/deploy/networks/devnet.yml @@ -10,32 +10,32 @@ networkResources: txWatchUrl: https://holesky.etherscan.io/tx flashbotsProtectUrl: https://rpc-holesky.flashbots.net/ smartNodeResources: - stakeUrl: "TBD" - storageAddress: 0xf04de123993761Bb9F08c9C39112b0E0b0eccE50 - rethAddress: 0x4be7161080b5d890500194cee2c40B1428002Bd3 - rplTokenAddress: 0x59A1a7AebCbF103B3C4f85261fbaC166117E1979 - v1_0_0_RewardsPoolAddress: null - v1_0_0_ClaimNodeAddress: null - v1_0_0_ClaimTrustedNodeAddress: null - v1_0_0_MinipoolManagerAddress: null - v1_1_0_NetworkPricesAddress: null - v1_1_0_NodeStakingAddress: null - v1_1_0_NodeDepositAddress: null - v1_1_0_MinipoolQueueAddress: null - v1_1_0_MinipoolFactoryAddress: null - v1_2_0_NetworkPricesAddress: 0xBba3FBCD4Bdbfc79118B1B31218602E5A71B426c - v1_2_0_NetworkBalancesAddress: 0xBe8Dc8CA5f339c196Aef634DfcDFbA61E30DC743 - snapshotDelegationAddress: null - snapshotApiDomain: "" - previousRewardsPoolAddresses: - - 0x4d581a552490fb6fce5F978e66560C8b7E481818 - PreviousProtocolDaoVerifierAddresses: [] - OptimismPriceMessengerAddress: null - PolygonPriceMessengerAddress: null - ArbitrumPriceMessengerAddress: null - ArbitrumPriceMessengerAddressV2: null - ZkSyncEraPriceMessengerAddress: null - BasePriceMessengerAddress: null - ScrollPriceMessengerAddress: null - ScrollFeeEstimatorAddress: null - RplTwapPoolAddress: 0x7bb10d2a3105ed5cc150c099a06cafe43d8aa15d \ No newline at end of file + stakeUrl: TBD + storageAddress: 0xf04de123993761Bb9F08c9C39112b0E0b0eccE50 + rethAddress: 0x4be7161080b5d890500194cee2c40B1428002Bd3 + rplTokenAddress: 0x59A1a7AebCbF103B3C4f85261fbaC166117E1979 + v1_0_0_RewardsPoolAddress: null + v1_0_0_ClaimNodeAddress: null + v1_0_0_ClaimTrustedNodeAddress: null + v1_0_0_MinipoolManagerAddress: null + v1_1_0_NetworkPricesAddress: null + v1_1_0_NodeStakingAddress: null + v1_1_0_NodeDepositAddress: null + v1_1_0_MinipoolQueueAddress: null + v1_1_0_MinipoolFactoryAddress: null + v1_2_0_NetworkPricesAddress: 0xBba3FBCD4Bdbfc79118B1B31218602E5A71B426c + v1_2_0_NetworkBalancesAddress: 0xBe8Dc8CA5f339c196Aef634DfcDFbA61E30DC743 + snapshotDelegationAddress: null + snapshotApiDomain: "" + previousRewardsPoolAddresses: + - 0x4d581a552490fb6fce5F978e66560C8b7E481818 + previousProtocolDaoVerifierAddresses: [] + optimismPriceMessengerAddress: null + polygonPriceMessengerAddress: null + arbitrumPriceMessengerAddress: null + arbitrumPriceMessengerAddressV2: null + zkSyncEraPriceMessengerAddress: null + basePriceMessengerAddress: null + scrollPriceMessengerAddress: null + scrollFeeEstimatorAddress: null + rplTwapPoolAddress: 0x7bb10d2a3105ed5cc150c099a06cafe43d8aa15d \ No newline at end of file diff --git a/install/deploy/networks/holesky.yml b/install/deploy/networks/holesky.yml index bddf734ed..58b472853 100644 --- a/install/deploy/networks/holesky.yml +++ b/install/deploy/networks/holesky.yml @@ -10,32 +10,32 @@ networkResources: txWatchUrl: https://holesky.etherscan.io/tx flashbotsProtectUrl: https://rpc-holesky.flashbots.net/ smartNodeResources: - stakeUrl: "https://testnet.rocketpool.net" - storageAddress: 0x594Fb75D3dc2DFa0150Ad03F99F97817747dd4E1 - rethAddress: 0x7322c24752f79c05FFD1E2a6FCB97020C1C264F1 - rplTokenAddress: 0x1Cc9cF5586522c6F483E84A19c3C2B0B6d027bF0 - v1_0_0_RewardsPoolAddress: null - v1_0_0_ClaimNodeAddress: null - v1_0_0_ClaimTrustedNodeAddress: null - v1_0_0_MinipoolManagerAddress: null - v1_1_0_NetworkPricesAddress: null - v1_1_0_NodeStakingAddress: null - v1_1_0_NodeDepositAddress: null - v1_1_0_MinipoolQueueAddress: null - v1_1_0_MinipoolFactoryAddress: null - v1_2_0_NetworkPricesAddress: 0x029d946F28F93399a5b0D09c879FC8c94E596AEb - v1_2_0_NetworkBalancesAddress: 0x9294Fc6F03c64Cc217f5BE8697EA3Ed2De77e2F8 - snapshotDelegationAddress: null - snapshotApiDomain: "" - previousRewardsPoolAddresses: - - 0x4a625C617a44E60F74E3fe3bf6d6333b63766e91 - PreviousProtocolDaoVerifierAddresses: [] - OptimismPriceMessengerAddress: null - PolygonPriceMessengerAddress: null - ArbitrumPriceMessengerAddress: null - ArbitrumPriceMessengerAddressV2: null - ZkSyncEraPriceMessengerAddress: null - BasePriceMessengerAddress: null - ScrollPriceMessengerAddress: null - ScrollFeeEstimatorAddress: null - RplTwapPoolAddress: 0x7bb10d2a3105ed5cc150c099a06cafe43d8aa15d \ No newline at end of file + stakeUrl: "https://testnet.rocketpool.net" + storageAddress: 0x594Fb75D3dc2DFa0150Ad03F99F97817747dd4E1 + rethAddress: 0x7322c24752f79c05FFD1E2a6FCB97020C1C264F1 + rplTokenAddress: 0x1Cc9cF5586522c6F483E84A19c3C2B0B6d027bF0 + v1_0_0_RewardsPoolAddress: null + v1_0_0_ClaimNodeAddress: null + v1_0_0_ClaimTrustedNodeAddress: null + v1_0_0_MinipoolManagerAddress: null + v1_1_0_NetworkPricesAddress: null + v1_1_0_NodeStakingAddress: null + v1_1_0_NodeDepositAddress: null + v1_1_0_MinipoolQueueAddress: null + v1_1_0_MinipoolFactoryAddress: null + v1_2_0_NetworkPricesAddress: 0x029d946F28F93399a5b0D09c879FC8c94E596AEb + v1_2_0_NetworkBalancesAddress: 0x9294Fc6F03c64Cc217f5BE8697EA3Ed2De77e2F8 + snapshotDelegationAddress: null + snapshotApiDomain: "" + previousRewardsPoolAddresses: + - 0x4a625C617a44E60F74E3fe3bf6d6333b63766e91 + previousProtocolDaoVerifierAddresses: [] + optimismPriceMessengerAddress: null + polygonPriceMessengerAddress: null + arbitrumPriceMessengerAddress: null + arbitrumPriceMessengerAddressV2: null + zkSyncEraPriceMessengerAddress: null + basePriceMessengerAddress: null + scrollPriceMessengerAddress: null + scrollFeeEstimatorAddress: null + rplTwapPoolAddress: 0x7bb10d2a3105ed5cc150c099a06cafe43d8aa15d \ No newline at end of file diff --git a/install/deploy/networks/mainnet.yml b/install/deploy/networks/mainnet.yml index 494d2c6c8..c8e0a1ab3 100644 --- a/install/deploy/networks/mainnet.yml +++ b/install/deploy/networks/mainnet.yml @@ -10,32 +10,32 @@ networkResources: txWatchUrl: https://etherscan.io/tx flashbotsProtectUrl: https://rpc.flashbots.net/ smartNodeResources: - stakeUrl: "https://stake.rocketpool.net" - storageAddress: 0x1d8f8f00cfa6758d7bE78336684788Fb0ee0Fa46 - rethAddress: 0xae78736Cd615f374D3085123A210448E74Fc6393 - rplTokenAddress: 0xD33526068D116cE69F19A9ee46F0bd304F21A51f - v1_0_0_RewardsPoolAddress: 0xA3a18348e6E2d3897B6f2671bb8c120e36554802 - v1_0_0_ClaimNodeAddress: 0x899336A2a86053705E65dB61f52C686dcFaeF548 - v1_0_0_ClaimTrustedNodeAddress: 0x6af730deB0463b432433318dC8002C0A4e9315e8 - v1_0_0_MinipoolManagerAddress: 0x6293B8abC1F36aFB22406Be5f96D893072A8cF3a - v1_1_0_NetworkPricesAddress: 0xd3f500F550F46e504A4D2153127B47e007e11166 - v1_1_0_NodeStakingAddress: 0xA73ec45Fe405B5BFCdC0bF4cbc9014Bb32a01cd2 - v1_1_0_NodeDepositAddress: 0x1Cc9cF5586522c6F483E84A19c3C2B0B6d027bF0 - v1_1_0_MinipoolQueueAddress: 0x5870dA524635D1310Dc0e6F256Ce331012C9C19E - v1_1_0_MinipoolFactoryAddress: 0x54705f80D7C51Fcffd9C659ce3f3C9a7dCCf5788 - v1_2_0_NetworkPricesAddress: 0x751826b107672360b764327631cC5764515fFC37 - v1_2_0_NetworkBalancesAddress: 0x07FCaBCbe4ff0d80c2b1eb42855C0131b6cba2F4 - snapshotDelegationAddress: 0x469788fE6E9E9681C6ebF3bF78e7Fd26Fc015446 - snapshotApiDomain: "hub.snapshot.org" - previousRewardsPoolAddresses: - - 0x594Fb75D3dc2DFa0150Ad03F99F97817747dd4E1 - PreviousProtocolDaoVerifierAddresses: [] - OptimismPriceMessengerAddress: 0xdddcf2c25d50ec22e67218e873d46938650d03a7 - PolygonPriceMessengerAddress: 0xb1029Ac2Be4e08516697093e2AFeC435057f3511 - ArbitrumPriceMessengerAddress: 0x05330300f829AD3fC8f33838BC88CFC4093baD53 - ArbitrumPriceMessengerAddressV2: 0x312FcFB03eC9B1Ea38CB7BFCd26ee7bC3b505aB1 - ZkSyncEraPriceMessengerAddress: 0x6cf6CB29754aEBf88AF12089224429bD68b0b8c8 - BasePriceMessengerAddress: 0x64A5856869C06B0188C84A5F83d712bbAc03517d - ScrollPriceMessengerAddress: 0x0f22dc9b9c03757d4676539203d7549c8f22c15c - ScrollFeeEstimatorAddress: 0x0d7E906BD9cAFa154b048cFa766Cc1E54E39AF9B - RplTwapPoolAddress: 0xe42318ea3b998e8355a3da364eb9d48ec725eb45 \ No newline at end of file + stakeUrl: "https://stake.rocketpool.net" + storageAddress: 0x1d8f8f00cfa6758d7bE78336684788Fb0ee0Fa46 + rethAddress: 0xae78736Cd615f374D3085123A210448E74Fc6393 + rplTokenAddress: 0xD33526068D116cE69F19A9ee46F0bd304F21A51f + v1_0_0_RewardsPoolAddress: 0xA3a18348e6E2d3897B6f2671bb8c120e36554802 + v1_0_0_ClaimNodeAddress: 0x899336A2a86053705E65dB61f52C686dcFaeF548 + v1_0_0_ClaimTrustedNodeAddress: 0x6af730deB0463b432433318dC8002C0A4e9315e8 + v1_0_0_MinipoolManagerAddress: 0x6293B8abC1F36aFB22406Be5f96D893072A8cF3a + v1_1_0_NetworkPricesAddress: 0xd3f500F550F46e504A4D2153127B47e007e11166 + v1_1_0_NodeStakingAddress: 0xA73ec45Fe405B5BFCdC0bF4cbc9014Bb32a01cd2 + v1_1_0_NodeDepositAddress: 0x1Cc9cF5586522c6F483E84A19c3C2B0B6d027bF0 + v1_1_0_MinipoolQueueAddress: 0x5870dA524635D1310Dc0e6F256Ce331012C9C19E + v1_1_0_MinipoolFactoryAddress: 0x54705f80D7C51Fcffd9C659ce3f3C9a7dCCf5788 + v1_2_0_NetworkPricesAddress: 0x751826b107672360b764327631cC5764515fFC37 + v1_2_0_NetworkBalancesAddress: 0x07FCaBCbe4ff0d80c2b1eb42855C0131b6cba2F4 + snapshotDelegationAddress: 0x469788fE6E9E9681C6ebF3bF78e7Fd26Fc015446 + snapshotApiDomain: "hub.snapshot.org" + previousRewardsPoolAddresses: + - 0x594Fb75D3dc2DFa0150Ad03F99F97817747dd4E1 + previousProtocolDaoVerifierAddresses: [] + optimismPriceMessengerAddress: 0xdddcf2c25d50ec22e67218e873d46938650d03a7 + polygonPriceMessengerAddress: 0xb1029Ac2Be4e08516697093e2AFeC435057f3511 + arbitrumPriceMessengerAddress: 0x05330300f829AD3fC8f33838BC88CFC4093baD53 + arbitrumPriceMessengerAddressV2: 0x312FcFB03eC9B1Ea38CB7BFCd26ee7bC3b505aB1 + zkSyncEraPriceMessengerAddress: 0x6cf6CB29754aEBf88AF12089224429bD68b0b8c8 + basePriceMessengerAddress: 0x64A5856869C06B0188C84A5F83d712bbAc03517d + scrollPriceMessengerAddress: 0x0f22dc9b9c03757d4676539203d7549c8f22c15c + scrollFeeEstimatorAddress: 0x0d7E906BD9cAFa154b048cFa766Cc1E54E39AF9B + rplTwapPoolAddress: 0xe42318ea3b998e8355a3da364eb9d48ec725eb45 \ No newline at end of file diff --git a/rocketpool-cli/client/config.go b/rocketpool-cli/client/config.go index 40d5c4da3..8b36468cc 100644 --- a/rocketpool-cli/client/config.go +++ b/rocketpool-cli/client/config.go @@ -27,19 +27,13 @@ func (c *Client) LoadConfig() (*config.SmartNodeConfig, bool, error) { return c.cfg, c.isNewCfg, nil } - // Load the network settings - settingsList, err := config.LoadSettingsFiles(c.Context.NetworksDir) - if err != nil { - return nil, false, fmt.Errorf("error loading network settings: %w", err) - } - // Load the config settingsFilePath := filepath.Join(c.Context.ConfigPath, SettingsFile) expandedPath, err := homedir.Expand(settingsFilePath) if err != nil { return nil, false, fmt.Errorf("error expanding settings file path: %w", err) } - cfg, err := LoadConfigFromFile(expandedPath, settingsList) + cfg, err := LoadConfigFromFile(expandedPath, c.Context.NetworkSettings) if err != nil { return nil, false, err } @@ -51,7 +45,7 @@ func (c *Client) LoadConfig() (*config.SmartNodeConfig, bool, error) { } // Config wasn't loaded, but there was no error- we should create one. - cfg, err = config.NewSmartNodeConfig(c.Context.ConfigPath, c.Context.NativeMode, settingsList) + cfg, err = config.NewSmartNodeConfig(c.Context.ConfigPath, c.Context.NativeMode, c.Context.NetworkSettings) if err != nil { return nil, false, fmt.Errorf("error creating new Smart Node config: %w", err) } @@ -68,13 +62,7 @@ func (c *Client) LoadBackupConfig() (*config.SmartNodeConfig, error) { return nil, fmt.Errorf("error expanding backup settings file path: %w", err) } - // Load the network settings - settingsList, err := config.LoadSettingsFiles(c.Context.NetworksDir) - if err != nil { - return nil, fmt.Errorf("error loading network settings: %w", err) - } - - return LoadConfigFromFile(expandedPath, settingsList) + return LoadConfigFromFile(expandedPath, c.Context.NetworkSettings) } // Save the config diff --git a/rocketpool-cli/client/utils.go b/rocketpool-cli/client/utils.go index f9152f6ea..189267f67 100644 --- a/rocketpool-cli/client/utils.go +++ b/rocketpool-cli/client/utils.go @@ -20,15 +20,6 @@ func SyncRatioToPercent(in float64) float64 { return math.Min(99.99, in*100) } -// Load the Smart Node settings from the network settings files on disk -func LoadSmartNodeSettings(networksDir string) ([]*config.SmartNodeSettings, error) { - settings, err := config.LoadSettingsFiles(networksDir) - if err != nil { - return nil, fmt.Errorf("error loading Smart Node settings files from [%s]: %w", networksDir, err) - } - return settings, nil -} - // Loads a config without updating it if it exists func LoadConfigFromFile(path string, networks []*config.SmartNodeSettings) (*config.SmartNodeConfig, error) { _, err := os.Stat(path) diff --git a/rocketpool-cli/commands/service/commands.go b/rocketpool-cli/commands/service/commands.go index 9311b1aaa..9593469c1 100644 --- a/rocketpool-cli/commands/service/commands.go +++ b/rocketpool-cli/commands/service/commands.go @@ -111,9 +111,9 @@ func createFlagsFromConfigParams(prefix string, section config.IConfigSection, c } // Register commands -func RegisterCommands(app *cli.App, name string, aliases []string) { +func RegisterCommands(app *cli.App, name string, aliases []string, networkSettings []*snCfg.SmartNodeSettings) { // Create config flags from parameters - cfgTemplate, _ := snCfg.NewSmartNodeConfig("", false, []*snCfg.SmartNodeSettings{}) + cfgTemplate, _ := snCfg.NewSmartNodeConfig("", false, networkSettings) network := cfgTemplate.Network.Value configFlags := createFlagsFromConfigParams("", cfgTemplate, []cli.Flag{ installUpdateDefaultsFlag, diff --git a/rocketpool-cli/rocketpool-cli.go b/rocketpool-cli/rocketpool-cli.go index 96f3c78c5..f30f4fbb4 100644 --- a/rocketpool-cli/rocketpool-cli.go +++ b/rocketpool-cli/rocketpool-cli.go @@ -19,6 +19,7 @@ import ( "github.com/rocket-pool/smartnode/v2/rocketpool-cli/commands/wallet" "github.com/rocket-pool/smartnode/v2/rocketpool-cli/settings" "github.com/rocket-pool/smartnode/v2/rocketpool-cli/utils" + "github.com/rocket-pool/smartnode/v2/shared/config" ) // allowRootFlag is the only one this file deals with- simply so it can exit early. @@ -97,6 +98,14 @@ func newCliApp() *cli.App { // Set utility flags app.Flags = utils.AppendFlags(app.Flags) + // Load the network settings + systemSettings := settings.NewSystemSettings() + networkSettings, err := config.LoadSettingsFiles(systemSettings.NetworksDir) + if err != nil { + fmt.Fprintf(os.Stderr, "Error loading network settings from path [%s]: %s", systemSettings.NetworksDir, err.Error()) + os.Exit(1) + } + // Register commands auction.RegisterCommands(app, "auction", []string{"a"}) minipool.RegisterCommands(app, "minipool", []string{"m"}) @@ -106,7 +115,7 @@ func newCliApp() *cli.App { pdao.RegisterCommands(app, "pdao", []string{"p"}) queue.RegisterCommands(app, "queue", []string{"q"}) security.RegisterCommands(app, "security", []string{"c"}) - service.RegisterCommands(app, "service", []string{"s"}) + service.RegisterCommands(app, "service", []string{"s"}, networkSettings) wallet.RegisterCommands(app, "wallet", []string{"w"}) var snSettings *settings.SmartNodeSettings @@ -119,7 +128,7 @@ func newCliApp() *cli.App { } var err error - snSettings, err = settings.NewSmartNodeSettings(c) + snSettings, err = settings.NewSmartNodeSettings(c, systemSettings, networkSettings) if err != nil { fmt.Fprint(os.Stderr, err.Error()) os.Exit(1) diff --git a/rocketpool-cli/rocketpool-cli_test.go b/rocketpool-cli/rocketpool-cli_test.go index 52c9a7a0f..cadc7bad6 100644 --- a/rocketpool-cli/rocketpool-cli_test.go +++ b/rocketpool-cli/rocketpool-cli_test.go @@ -4,11 +4,13 @@ import ( "bytes" "errors" "fmt" + "io" "os" "path/filepath" "strings" "testing" + "github.com/prysmaticlabs/prysm/v5/testing/require" "github.com/rocket-pool/smartnode/v2/rocketpool-cli/settings" "github.com/urfave/cli/v2" ) @@ -34,6 +36,42 @@ func TestGlobalFlagsDefaults(t *testing.T) { "--help", ) + // Make a system path + systemPath := filepath.Join(tempPath, "rocketpool") + err := os.MkdirAll(systemPath, 0755) + require.NoError(t, err) + + // Emulating a (partial) installation by deploying a networks folder + // Other installation files can be copied here later if necessary + networkSettingsPath := filepath.Join(systemPath, "networks") + err = os.MkdirAll(networkSettingsPath, 0755) + require.NoError(t, err) + + // Copy the settings files + networksSource := filepath.Join("..", "install", "deploy", "networks") + entries, err := os.ReadDir(networksSource) + require.NoError(t, err) + for _, file := range entries { + fileName := file.Name() + + sourceFile, err := os.Open(filepath.Join(networksSource, fileName)) + require.NoError(t, err) + defer sourceFile.Close() + + targetFile, err := os.Create(filepath.Join(networkSettingsPath, fileName)) + require.NoError(t, err) + defer targetFile.Close() + + _, err = io.Copy(targetFile, sourceFile) + require.NoError(t, err) + err = targetFile.Sync() + require.NoError(t, err) + } + + // Set the install dir env var + err = os.Setenv(settings.TestSystemDirEnvVar, systemPath) + require.NoError(t, err) + app := newCliApp() // Capture stdout diff --git a/rocketpool-cli/settings/smartnode_settings.go b/rocketpool-cli/settings/smartnode_settings.go index f29142531..0f24fc818 100644 --- a/rocketpool-cli/settings/smartnode_settings.go +++ b/rocketpool-cli/settings/smartnode_settings.go @@ -6,7 +6,6 @@ import ( "net/url" "os" "path/filepath" - "runtime" "strings" "github.com/mitchellh/go-homedir" @@ -17,15 +16,6 @@ import ( const ( traceMode os.FileMode = 0644 defaultConfigFolder string = ".rocketpool" - - // System dir path for Linux - linuxSystemDir string = "/usr/share/rocketpool" - - // Subfolders under the system dir - scriptsDir string = "scripts" - templatesDir string = "templates" - overrideSourceDir string = "override" - networksDir string = "networks" ) var ( @@ -83,6 +73,8 @@ const ( // Context for global settings type SmartNodeSettings struct { + *SystemSettings + // The path to the configuration file ConfigPath string @@ -110,17 +102,8 @@ type SmartNodeSettings struct { // The HTTP trace file if tracing is enabled HttpTraceFile *os.File - // The system path for Smart Node scripts used in the Docker containers - ScriptsDir string - - // The system path for Smart Node templates - TemplatesDir string - - // The system path for the source files to put in the user's override directory - OverrideSourceDir string - - // The system path for built-in network settings and resource definitions - NetworksDir string + // The list of networks options and corresponding settings + NetworkSettings []*config.SmartNodeSettings } // Get the Smart Node settings from a CLI context @@ -145,13 +128,15 @@ func AppendSmartNodeSettingsFlags(flags []cli.Flag) []cli.Flag { ) } -// Validate the global flags -func NewSmartNodeSettings(c *cli.Context) (*SmartNodeSettings, error) { +// Creates a new Smart Node settings instance +func NewSmartNodeSettings(c *cli.Context, systemSettings *SystemSettings, networkSettings []*config.SmartNodeSettings) (*SmartNodeSettings, error) { snSettings := &SmartNodeSettings{ - MaxFee: c.Float64(maxFeeFlag.Name), - MaxPriorityFee: c.Float64(maxPriorityFeeFlag.Name), - DebugEnabled: c.Bool(debugFlag.Name), - SecureSession: c.Bool(secureSessionFlag.Name), + SystemSettings: systemSettings, + MaxFee: c.Float64(maxFeeFlag.Name), + MaxPriorityFee: c.Float64(maxPriorityFeeFlag.Name), + DebugEnabled: c.Bool(debugFlag.Name), + SecureSession: c.Bool(secureSessionFlag.Name), + NetworkSettings: networkSettings, } // If set, validate custom nonce @@ -192,21 +177,7 @@ func NewSmartNodeSettings(c *cli.Context) (*SmartNodeSettings, error) { } } - var systemDir string - switch runtime.GOOS { - // This is where to add different paths for different OS's like macOS - default: - // By default just use the Linux path - systemDir = linuxSystemDir - } - - snSettings.ScriptsDir = filepath.Join(systemDir, scriptsDir) - snSettings.TemplatesDir = filepath.Join(systemDir, templatesDir) - snSettings.OverrideSourceDir = filepath.Join(systemDir, overrideSourceDir) - snSettings.NetworksDir = filepath.Join(systemDir, networksDir) - c.App.Metadata[contextMetadataName] = snSettings - return snSettings, nil } diff --git a/rocketpool-cli/settings/system_settings.go b/rocketpool-cli/settings/system_settings.go new file mode 100644 index 000000000..c236402cf --- /dev/null +++ b/rocketpool-cli/settings/system_settings.go @@ -0,0 +1,55 @@ +package settings + +import ( + "os" + "path/filepath" + "runtime" +) + +const ( + // Environment variable to set the system path for unit tests + TestSystemDirEnvVar string = "SMARTNODE_TEST_SYSTEM_DIR" + + // System dir path for Linux + linuxSystemDir string = "/usr/share/rocketpool" + + // Subfolders under the system dir + scriptsDir string = "scripts" + templatesDir string = "templates" + overrideSourceDir string = "override" + networksDir string = "networks" +) + +// Holds the location of various Smart Node system directories +type SystemSettings struct { + // The system path for Smart Node scripts used in the Docker containers + ScriptsDir string + + // The system path for Smart Node templates + TemplatesDir string + + // The system path for the source files to put in the user's override directory + OverrideSourceDir string + + // The system path for built-in network settings and resource definitions + NetworksDir string +} + +func NewSystemSettings() *SystemSettings { + systemDir := os.Getenv(TestSystemDirEnvVar) + if systemDir == "" { + switch runtime.GOOS { + // This is where to add different paths for different OS's like macOS + default: + // By default just use the Linux path + systemDir = linuxSystemDir + } + } + + return &SystemSettings{ + ScriptsDir: filepath.Join(systemDir, scriptsDir), + TemplatesDir: filepath.Join(systemDir, templatesDir), + OverrideSourceDir: filepath.Join(systemDir, overrideSourceDir), + NetworksDir: filepath.Join(systemDir, networksDir), + } +}