Skip to content

Commit 448efee

Browse files
authored
fix: Pass Geth NAT option using flags, not TOML config (#271)
* Add ip to CLI config during init * Start geth with nat flag * Fix legacy build * Move ip log * Add context key type
1 parent d4ad4bb commit 448efee

10 files changed

Lines changed: 121 additions & 56 deletions

File tree

cmd/lukso-legacy/download.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

1717
const (
18-
configPerms = 0750
18+
configPerms = 0o750
1919
binaryPerms = int(os.ModePerm)
2020
)
2121

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

259-
err = cfg.Create(selectedExecution, selectedConsensus, "")
259+
err = cfg.Create(selectedExecution, selectedConsensus, "", "")
260260
if err != nil {
261261
return exit(fmt.Sprintf("❌ There was an error while creating configration file: %v", err), 1)
262262
}

cmd/lukso-legacy/init.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func initializeDirectory(ctx *cli.Context) error {
6969
case false:
7070
log.Info("⚙️ Creating LUKSO configuration file...")
7171

72-
err = cfg.Create("", "", "")
72+
err = cfg.Create("", "", "", "")
7373
if err != nil {
7474
return exit(fmt.Sprintf("❌ There was an error while preparing LUKSO configuration: %v", err), 1)
7575
}

cmd/lukso/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ REPO: https://github.com/lukso-network/tools-lukso-cli
5656
Subcommands: cli.Commands{
5757
&cli.Command{
5858
Name: "configs",
59-
Usage: "Updates chain configuration files, without overwriting client configuration files",
59+
Usage: "Updates chain configuration and CLI configuration files, without overwriting client configuration files",
6060
Flags: flags.UpdateConfigFlags,
6161
Action: commands.UpdateConfigs,
6262
HideHelpCommand: true,

commands/init.go

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -57,38 +57,40 @@ func InitializeDirectory(ctx *cli.Context) error {
5757
case true:
5858
log.Info("⚙️ LUKSO configuration already exists - continuing...")
5959
case false:
60+
log.Info("⚙️ LUKSO CLI can replace your p2p communication flags with your public IP for better connection (optional)")
61+
message := "Do you want to proceed? [Y/n]\n> "
62+
63+
ip := "0.0.0.0"
64+
input := utils.RegisterInputWithMessage(message)
65+
if strings.EqualFold(input, "y") || input == "" {
66+
ip, err = setIPInConfigs()
67+
if err != nil {
68+
return err
69+
}
70+
}
71+
6072
log.Info("⚙️ Creating LUKSO configuration file...")
6173

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

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

70-
log.Info("⚙️ LUKSO CLI can replace your p2p communication flags with your public IP for better connection (optional)")
71-
message := "Do you want to proceed? [Y/n]\n> "
72-
73-
input := utils.RegisterInputWithMessage(message)
74-
if strings.EqualFold(input, "y") || input == "" {
75-
err = setIPInConfigs()
76-
if err != nil {
77-
return err
78-
}
79-
}
80-
8183
displayNetworksHardforkTimestamps()
8284

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

8587
return nil
8688
}
8789

88-
func setIPInConfigs() (err error) {
89-
ip, err := getPublicIP()
90+
func setIPInConfigs() (ip string, err error) {
91+
ip, err = getPublicIP()
9092
if err != nil {
91-
return err
93+
return
9294
}
9395

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

107-
gethFlags := []string{"nat"}
108109
erigonFlags := []string{"nat"}
109110
besuFlags := []string{"p2p-host"}
110-
nethermindFlags := []string{"Network.ExternalIp"}
111+
nethermindFlags := []string{"Network:ExternalIp"} // : because of the non-standard delimiter
111112
prysmFlags := []string{"p2p-host-ip"}
112113
lighthouseFlags := []string{"enr-address"}
113114
tekuFlags := []string{"p2p-advertised-ip"}
114115
nimbusFlags := []string{"nat"}
115116

116117
configs := []tempConfig{
117-
{
118-
path: "./configs/%s/geth",
119-
name: "geth",
120-
fileType: "toml",
121-
flagsToChange: gethFlags,
122-
wrap: natWrap,
123-
},
124118
{
125119
path: "./configs/%s/erigon",
126120
name: "erigon",
@@ -178,8 +172,7 @@ func setIPInConfigs() (err error) {
178172
for _, cfg := range configs {
179173
cfg.path = fmt.Sprintf(cfg.path, network)
180174

181-
v := viper.New()
182-
175+
v := viper.NewWithOptions(viper.KeyDelimiter(":"))
183176
v.AddConfigPath(cfg.path)
184177
v.SetConfigName(cfg.name)
185178
v.SetConfigType(cfg.fileType)
@@ -205,7 +198,14 @@ func setIPInConfigs() (err error) {
205198
}
206199
}
207200

201+
names := ""
202+
for _, cfg := range configs {
203+
names += fmt.Sprintf("- %s\n", cfg.name)
204+
}
205+
208206
log.Info("✅ IP Address updated!\n\n")
207+
log.Infof("▶️ Your IPv4 Address has been wrtitten into the cli-config.yaml file.\n\n"+
208+
"Additionally, the following clients' configs have been updated: \n%s\n", names)
209209

210210
return
211211
}

commands/install.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,19 @@ func InstallBinaries(ctx *cli.Context) (err error) {
159159
selectedValidator = clients.Nimbus2Validator
160160
}
161161

162-
err = cfg.Create(selectedExecution.Name(), selectedConsensus.Name(), selectedValidator.Name())
162+
err = cfg.WriteExecution(selectedExecution.Name())
163163
if err != nil {
164-
return utils.Exit(fmt.Sprintf("❌ There was an error while creating configration file: %v", err), 1)
164+
return utils.Exit(fmt.Sprintf("❌ There was an error while writing execution client: %v", err), 1)
165+
}
166+
167+
err = cfg.WriteConsensus(selectedConsensus.Name())
168+
if err != nil {
169+
return utils.Exit(fmt.Sprintf("❌ There was an error while writing consensus client: %v", err), 1)
170+
}
171+
172+
err = cfg.WriteValidator(selectedValidator.Name())
173+
if err != nil {
174+
return utils.Exit(fmt.Sprintf("❌ There was an error while writing validator client: %v", err), 1)
165175
}
166176

167177
log.Info("✅ Configuration files created!")

commands/start.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package commands
22

33
import (
4+
"context"
45
"fmt"
56
"os"
67
"strings"
@@ -9,6 +10,7 @@ import (
910
log "github.com/sirupsen/logrus"
1011
"github.com/urfave/cli/v2"
1112

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

32+
// Pass down the IP to any client that needs to set it in flag
33+
ctx.Context = context.WithValue(ctx.Context, common.ConfigKey("ip"), cfg.IPv4())
34+
3035
selectedExecution := cfg.Execution()
3136
selectedConsensus := cfg.Consensus()
3237
if selectedExecution == "" || selectedConsensus == "" {

commands/update.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ func UpdateConfigs(ctx *cli.Context) (err error) {
6969
return cli.Exit(errors.FolderNotInitialized, 1)
7070
}
7171

72+
log.Info("⬇️ Updating IPv4 Addres...")
73+
74+
ip, err := getPublicIP()
75+
if err != nil {
76+
log.Warn("⚠️ There was an error while getting the IPv4 Address: continuing...")
77+
} else {
78+
err = cfg.WriteIPv4(ip)
79+
if err != nil {
80+
return utils.Exit(fmt.Sprintf("❌ There was an error while writing IPv4 Address: %v", err), 1)
81+
}
82+
}
83+
7284
if ctx.Bool(flags.AllFlag) {
7385
message := "⚠️ Warning - this action will overwrite all of your client configuration files, are you sure you want to continue? [y/N]\n> "
7486

common/ctxkeys.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package common
2+
3+
// ConfigKey is a string alias, used for distincting between different key types
4+
type ConfigKey string

config/config.go

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@ import (
66
"strconv"
77
"strings"
88

9-
log "github.com/sirupsen/logrus"
109
"github.com/spf13/viper"
1110
"github.com/urfave/cli/v2"
12-
13-
"github.com/lukso-network/tools-lukso-cli/common/errors"
1411
)
1512

1613
const (
@@ -59,11 +56,18 @@ func parsePath(path string) (dir, fileName, extension string) {
5956
}
6057

6158
type Config struct {
62-
path string
63-
viper *viper.Viper
64-
executionClient string `mapstructure:"execution"`
65-
consensusClient string `mapstructure:"consensus"`
66-
validatorClient string `mapstructure:"validator"`
59+
path string
60+
viper *viper.Viper
61+
configValues
62+
}
63+
64+
type configValues struct {
65+
UseClients struct {
66+
ExecutionClient string `mapstructure:"execution"`
67+
ConsensusClient string `mapstructure:"consensus"`
68+
ValidatorClient string `mapstructure:"validator"`
69+
} `mapstructure:"useclients"`
70+
Ipv4 string `mapstructure:"ipv4"`
6771
}
6872

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

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

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

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

107112
func (c *Config) WriteExecution(selectedExecution string) (err error) {
113+
err = c.viper.ReadInConfig()
114+
if err != nil {
115+
return
116+
}
108117
c.viper.Set("useClients.execution", selectedExecution)
109118

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

115124
func (c *Config) WriteConsensus(selectedConsensus string) (err error) {
125+
err = c.viper.ReadInConfig()
126+
if err != nil {
127+
return
128+
}
116129
c.viper.Set("useClients.consensus", selectedConsensus)
117130

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

120133
return
121134
}
122135

123-
// Read reads from config file passed during config instance into c
124-
func (c *Config) Read() (err error) {
136+
func (c *Config) WriteValidator(selectedValidator string) (err error) {
125137
err = c.viper.ReadInConfig()
126138
if err != nil {
127139
return
128140
}
141+
c.viper.Set("useClients.validator", selectedValidator)
142+
143+
err = c.viper.WriteConfigAs(c.path)
144+
145+
return
146+
}
129147

130-
exec, execOk := c.viper.Get("useClients.execution").(string)
131-
cons, consOk := c.viper.Get("useClients.consensus").(string)
132-
val, valOk := c.viper.Get("useClients.validator").(string)
148+
func (c *Config) WriteIPv4(ipv4 string) (err error) {
149+
err = c.viper.ReadInConfig()
150+
if err != nil {
151+
return
152+
}
153+
c.viper.Set("ipv4", ipv4)
133154

134-
if !execOk || !consOk || !valOk {
135-
log.Error(errors.ErrOlderFolderDetected)
155+
err = c.viper.WriteConfigAs(c.path)
136156

137-
os.Exit(1)
157+
return
158+
}
138159

160+
// Read reads from config file passed during config instance into c
161+
func (c *Config) Read() (err error) {
162+
err = c.viper.ReadInConfig()
163+
if err != nil {
139164
return
140165
}
141166

142-
c.executionClient = exec
143-
c.consensusClient = cons
144-
c.validatorClient = val
167+
err = c.viper.Unmarshal(&c.configValues)
145168

146169
return
147170
}
148171

149172
func (c *Config) Execution() string {
150-
return c.executionClient
173+
return c.UseClients.ExecutionClient
151174
}
152175

153176
func (c *Config) Consensus() string {
154-
return c.consensusClient
177+
return c.UseClients.ConsensusClient
155178
}
156179

157180
func (c *Config) Validator() string {
158-
return c.validatorClient
181+
return c.UseClients.ValidatorClient
182+
}
183+
184+
func (c *Config) IPv4() string {
185+
ip := c.Ipv4
186+
if ip == "" {
187+
ip = "0.0.0.0"
188+
}
189+
190+
return ip
159191
}
160192

161193
func LoadLighthouseConfig(path string) (args []string, err error) {

dependencies/clients/geth.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ func (g *GethClient) PrepareStartFlags(ctx *cli.Context) (startFlags []string, e
6767
return
6868
}
6969

70+
ip := ctx.Context.Value(common.ConfigKey("ip"))
71+
7072
startFlags = g.ParseUserFlags(ctx)
7173
startFlags = append(startFlags, fmt.Sprintf("--config=%s", ctx.String(flags.GethConfigFileFlag)))
72-
74+
startFlags = append(startFlags, fmt.Sprintf("--nat=extip:%s", ip))
7375
return
7476
}
7577

0 commit comments

Comments
 (0)