Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Pass Geth NAT option using flags, not TOML config #271

Merged
merged 5 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmd/lukso-legacy/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

const (
configPerms = 0750
configPerms = 0o750
binaryPerms = int(os.ModePerm)
)

Expand Down Expand Up @@ -256,7 +256,7 @@ func installBinaries(ctx *cli.Context) (err error) {
return exit(fmt.Sprintf("❌ There was an error while downloading validator: %v", err), 1)
}

err = cfg.Create(selectedExecution, selectedConsensus, "")
err = cfg.Create(selectedExecution, selectedConsensus, "", "")
if err != nil {
return exit(fmt.Sprintf("❌ There was an error while creating configration file: %v", err), 1)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/lukso-legacy/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func initializeDirectory(ctx *cli.Context) error {
case false:
log.Info("⚙️ Creating LUKSO configuration file...")

err = cfg.Create("", "", "")
err = cfg.Create("", "", "", "")
if err != nil {
return exit(fmt.Sprintf("❌ There was an error while preparing LUKSO configuration: %v", err), 1)
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/lukso/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ REPO: https://github.com/lukso-network/tools-lukso-cli
Subcommands: cli.Commands{
&cli.Command{
Name: "configs",
Usage: "Updates chain configuration files, without overwriting client configuration files",
Usage: "Updates chain configuration and CLI configuration files, without overwriting client configuration files",
Flags: flags.UpdateConfigFlags,
Action: commands.UpdateConfigs,
HideHelpCommand: true,
Expand Down
52 changes: 26 additions & 26 deletions commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,38 +57,40 @@ func InitializeDirectory(ctx *cli.Context) error {
case true:
log.Info("⚙️ LUKSO configuration already exists - continuing...")
case false:
log.Info("⚙️ LUKSO CLI can replace your p2p communication flags with your public IP for better connection (optional)")
message := "Do you want to proceed? [Y/n]\n> "

ip := "0.0.0.0"
input := utils.RegisterInputWithMessage(message)
if strings.EqualFold(input, "y") || input == "" {
ip, err = setIPInConfigs()
if err != nil {
return err
}
}

log.Info("⚙️ Creating LUKSO configuration file...")

err = cfg.Create("", "", "")
err = cfg.Create("", "", "", ip)
if err != nil {
return utils.Exit(fmt.Sprintf("❌ There was an error while preparing LUKSO configuration: %v", err), 1)
}

log.Infof("⚙️ IPv4 found: %s", ip)
log.Infof("✅ LUKSO configuration created under %s", config.Path)
}

log.Info("⚙️ LUKSO CLI can replace your p2p communication flags with your public IP for better connection (optional)")
message := "Do you want to proceed? [Y/n]\n> "

input := utils.RegisterInputWithMessage(message)
if strings.EqualFold(input, "y") || input == "" {
err = setIPInConfigs()
if err != nil {
return err
}
}

displayNetworksHardforkTimestamps()

log.Info("✅ Working directory initialized! \n1. ⚙️ Use 'lukso install' to install clients. \n2. ▶️ Use 'lukso start' to start your node.")

return nil
}

func setIPInConfigs() (err error) {
ip, err := getPublicIP()
func setIPInConfigs() (ip string, err error) {
ip, err = getPublicIP()
if err != nil {
return err
return
}

type tempConfig struct {
Expand All @@ -104,23 +106,15 @@ func setIPInConfigs() (err error) {
return "extip:" + s
}

gethFlags := []string{"nat"}
erigonFlags := []string{"nat"}
besuFlags := []string{"p2p-host"}
nethermindFlags := []string{"Network.ExternalIp"}
nethermindFlags := []string{"Network:ExternalIp"} // : because of the non-standard delimiter
prysmFlags := []string{"p2p-host-ip"}
lighthouseFlags := []string{"enr-address"}
tekuFlags := []string{"p2p-advertised-ip"}
nimbusFlags := []string{"nat"}

configs := []tempConfig{
{
path: "./configs/%s/geth",
name: "geth",
fileType: "toml",
flagsToChange: gethFlags,
wrap: natWrap,
},
{
path: "./configs/%s/erigon",
name: "erigon",
Expand Down Expand Up @@ -178,8 +172,7 @@ func setIPInConfigs() (err error) {
for _, cfg := range configs {
cfg.path = fmt.Sprintf(cfg.path, network)

v := viper.New()

v := viper.NewWithOptions(viper.KeyDelimiter(":"))
v.AddConfigPath(cfg.path)
v.SetConfigName(cfg.name)
v.SetConfigType(cfg.fileType)
Expand All @@ -205,7 +198,14 @@ func setIPInConfigs() (err error) {
}
}

names := ""
for _, cfg := range configs {
names += fmt.Sprintf("- %s\n", cfg.name)
}

log.Info("✅ IP Address updated!\n\n")
Wolmin marked this conversation as resolved.
Show resolved Hide resolved
log.Infof("▶️ Your IPv4 Address has been wrtitten into the cli-config.yaml file.\n\n"+
"Additionally, the following clients' configs have been updated: \n%s\n", names)

return
}
Expand Down
14 changes: 12 additions & 2 deletions commands/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,19 @@ func InstallBinaries(ctx *cli.Context) (err error) {
selectedValidator = clients.Nimbus2Validator
}

err = cfg.Create(selectedExecution.Name(), selectedConsensus.Name(), selectedValidator.Name())
err = cfg.WriteExecution(selectedExecution.Name())
if err != nil {
return utils.Exit(fmt.Sprintf("❌ There was an error while creating configration file: %v", err), 1)
return utils.Exit(fmt.Sprintf("❌ There was an error while writing execution client: %v", err), 1)
}

err = cfg.WriteConsensus(selectedConsensus.Name())
if err != nil {
return utils.Exit(fmt.Sprintf("❌ There was an error while writing consensus client: %v", err), 1)
}

err = cfg.WriteValidator(selectedValidator.Name())
if err != nil {
return utils.Exit(fmt.Sprintf("❌ There was an error while writing validator client: %v", err), 1)
}

log.Info("✅ Configuration files created!")
Expand Down
5 changes: 5 additions & 0 deletions commands/start.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"context"
"fmt"
"os"
"strings"
Expand All @@ -9,6 +10,7 @@ import (
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"

"github.com/lukso-network/tools-lukso-cli/common"
"github.com/lukso-network/tools-lukso-cli/common/errors"
"github.com/lukso-network/tools-lukso-cli/common/network"
"github.com/lukso-network/tools-lukso-cli/common/utils"
Expand All @@ -27,6 +29,9 @@ func StartClients(ctx *cli.Context) (err error) {
return utils.Exit(fmt.Sprintf("❌ Couldn't read from config file: %v", err), 1)
}

// Pass down the IP to any client that needs to set it in flag
ctx.Context = context.WithValue(ctx.Context, common.ConfigKey("ip"), cfg.IPv4())

selectedExecution := cfg.Execution()
selectedConsensus := cfg.Consensus()
if selectedExecution == "" || selectedConsensus == "" {
Expand Down
12 changes: 12 additions & 0 deletions commands/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ func UpdateConfigs(ctx *cli.Context) (err error) {
return cli.Exit(errors.FolderNotInitialized, 1)
}

log.Info("⬇️ Updating IPv4 Addres...")

ip, err := getPublicIP()
if err != nil {
log.Warn("⚠️ There was an error while getting the IPv4 Address: continuing...")
} else {
err = cfg.WriteIPv4(ip)
if err != nil {
return utils.Exit(fmt.Sprintf("❌ There was an error while writing IPv4 Address: %v", err), 1)
}
}

if ctx.Bool(flags.AllFlag) {
message := "⚠️ Warning - this action will overwrite all of your client configuration files, are you sure you want to continue? [y/N]\n> "

Expand Down
4 changes: 4 additions & 0 deletions common/ctxkeys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package common

// ConfigKey is a string alias, used for distincting between different key types
type ConfigKey string
78 changes: 55 additions & 23 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ import (
"strconv"
"strings"

log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/urfave/cli/v2"

"github.com/lukso-network/tools-lukso-cli/common/errors"
)

const (
Expand Down Expand Up @@ -59,11 +56,18 @@ func parsePath(path string) (dir, fileName, extension string) {
}

type Config struct {
path string
viper *viper.Viper
executionClient string `mapstructure:"execution"`
consensusClient string `mapstructure:"consensus"`
validatorClient string `mapstructure:"validator"`
path string
viper *viper.Viper
configValues
}

type configValues struct {
UseClients struct {
ExecutionClient string `mapstructure:"execution"`
ConsensusClient string `mapstructure:"consensus"`
ValidatorClient string `mapstructure:"validator"`
} `mapstructure:"useclients"`
Ipv4 string `mapstructure:"ipv4"`
}

// NewConfig creates and initializes viper config instance - it doesn't load config, to load use c.Read().
Expand All @@ -83,7 +87,7 @@ func NewConfig(path string) *Config {

// Create creates a new config that keeps track of selected dependencies and writes to it.
// By default, this file should be present in root of initialized lukso directory
func (c *Config) Create(selectedExecution, selectedConsensus, selectedValidator string) (err error) {
func (c *Config) Create(selectedExecution, selectedConsensus, selectedValidator, ipv4 string) (err error) {
_, err = os.Create(c.path)
if err != nil {
return
Expand All @@ -92,6 +96,7 @@ func (c *Config) Create(selectedExecution, selectedConsensus, selectedValidator
c.viper.Set("useClients.execution", selectedExecution)
c.viper.Set("useClients.consensus", selectedConsensus)
c.viper.Set("useClients.validator", selectedValidator)
c.viper.Set("ipv4", ipv4)

err = c.viper.WriteConfigAs(c.path)

Expand All @@ -105,6 +110,10 @@ func (c *Config) Exists() bool {
}

func (c *Config) WriteExecution(selectedExecution string) (err error) {
err = c.viper.ReadInConfig()
if err != nil {
return
}
c.viper.Set("useClients.execution", selectedExecution)

err = c.viper.WriteConfigAs(c.path)
Expand All @@ -113,49 +122,72 @@ func (c *Config) WriteExecution(selectedExecution string) (err error) {
}

func (c *Config) WriteConsensus(selectedConsensus string) (err error) {
err = c.viper.ReadInConfig()
if err != nil {
return
}
c.viper.Set("useClients.consensus", selectedConsensus)

err = c.viper.WriteConfigAs(c.path)

return
}

// Read reads from config file passed during config instance into c
func (c *Config) Read() (err error) {
func (c *Config) WriteValidator(selectedValidator string) (err error) {
err = c.viper.ReadInConfig()
if err != nil {
return
}
c.viper.Set("useClients.validator", selectedValidator)

err = c.viper.WriteConfigAs(c.path)

return
}

exec, execOk := c.viper.Get("useClients.execution").(string)
cons, consOk := c.viper.Get("useClients.consensus").(string)
val, valOk := c.viper.Get("useClients.validator").(string)
func (c *Config) WriteIPv4(ipv4 string) (err error) {
err = c.viper.ReadInConfig()
if err != nil {
return
}
c.viper.Set("ipv4", ipv4)

if !execOk || !consOk || !valOk {
log.Error(errors.ErrOlderFolderDetected)
err = c.viper.WriteConfigAs(c.path)

os.Exit(1)
return
}

// Read reads from config file passed during config instance into c
func (c *Config) Read() (err error) {
err = c.viper.ReadInConfig()
if err != nil {
return
}

c.executionClient = exec
c.consensusClient = cons
c.validatorClient = val
err = c.viper.Unmarshal(&c.configValues)

return
}

func (c *Config) Execution() string {
return c.executionClient
return c.UseClients.ExecutionClient
}

func (c *Config) Consensus() string {
return c.consensusClient
return c.UseClients.ConsensusClient
}

func (c *Config) Validator() string {
return c.validatorClient
return c.UseClients.ValidatorClient
}

func (c *Config) IPv4() string {
ip := c.Ipv4
if ip == "" {
ip = "0.0.0.0"
}

return ip
}

func LoadLighthouseConfig(path string) (args []string, err error) {
Expand Down
4 changes: 3 additions & 1 deletion dependencies/clients/geth.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,11 @@ func (g *GethClient) PrepareStartFlags(ctx *cli.Context) (startFlags []string, e
return
}

ip := ctx.Context.Value(common.ConfigKey("ip"))

startFlags = g.ParseUserFlags(ctx)
startFlags = append(startFlags, fmt.Sprintf("--config=%s", ctx.String(flags.GethConfigFileFlag)))

startFlags = append(startFlags, fmt.Sprintf("--nat=extip:%s", ip))
return
}

Expand Down
Loading