Skip to content

Commit a2860c0

Browse files
committed
Refine --custom-spec-support flag help text and connection plumbing
- Reword the --custom-spec-support help string to match the register of neighboring flag descriptions and disclose that it is currently honoured only by the 'validator exit' command. - Add a doc comment to the exported ConnectOpts type. - Replace the adjacent positional booleans in connectToBeaconNode with a named-field beaconNodeConnection struct so the three call sites are self-documenting; the mainnet fallback's forced allowInsecure is now explicit at the call site. Behaviour-preserving cleanup; no functional change to the feature.
1 parent 3f72b41 commit a2860c0

2 files changed

Lines changed: 33 additions & 8 deletions

File tree

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ func addPersistentFlags() {
253253
if err := viper.BindPFlag("allow-insecure-connections", RootCmd.PersistentFlags().Lookup("allow-insecure-connections")); err != nil {
254254
panic(err)
255255
}
256-
RootCmd.PersistentFlags().Bool("custom-spec-support", false, "use dynamic SSZ to support non-mainnet presets (e.g. Gnosis); slower than the static decoder")
256+
RootCmd.PersistentFlags().Bool("custom-spec-support", false, "use dynamic SSZ decoding for non-mainnet presets such as Gnosis (slower; currently honoured only by 'validator exit')")
257257
if err := viper.BindPFlag("custom-spec-support", RootCmd.PersistentFlags().Lookup("custom-spec-support")); err != nil {
258258
panic(err)
259259
}

util/beaconnode.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ var defaultBeaconNodeAddresses = []string{
3737
// fallbackBeaconNode is used if no other connection is supplied.
3838
var fallbackBeaconNode = "http://mainnet-consensus.attestant.io/"
3939

40+
// ConnectOpts are the options for connecting to a beacon node.
4041
type ConnectOpts struct {
4142
Address string
4243
Timeout time.Duration
@@ -57,12 +58,22 @@ func ConnectToBeaconNode(ctx context.Context, opts *ConnectOpts) (eth2client.Ser
5758

5859
if opts.Address != "" {
5960
// We have an explicit address; use it.
60-
return connectToBeaconNode(ctx, opts.Address, opts.Timeout, opts.AllowInsecure, opts.CustomSpecSupport)
61+
return connectToBeaconNode(ctx, &beaconNodeConnection{
62+
address: opts.Address,
63+
timeout: opts.Timeout,
64+
allowInsecure: opts.AllowInsecure,
65+
customSpecSupport: opts.CustomSpecSupport,
66+
})
6167
}
6268

6369
// Try the defaults.
6470
for _, address := range defaultBeaconNodeAddresses {
65-
client, err := connectToBeaconNode(ctx, address, opts.Timeout, opts.AllowInsecure, opts.CustomSpecSupport)
71+
client, err := connectToBeaconNode(ctx, &beaconNodeConnection{
72+
address: address,
73+
timeout: opts.Timeout,
74+
allowInsecure: opts.AllowInsecure,
75+
customSpecSupport: opts.CustomSpecSupport,
76+
})
6677
if err == nil {
6778
return client, nil
6879
}
@@ -72,19 +83,33 @@ func ConnectToBeaconNode(ctx context.Context, opts *ConnectOpts) (eth2client.Ser
7283
if opts.LogFallback {
7384
fmt.Fprintf(os.Stderr, "No connection supplied with --connection parameter and no local beacon node found, attempting to use mainnet fallback\n")
7485
}
75-
client, err := connectToBeaconNode(ctx, fallbackBeaconNode, opts.Timeout, true, opts.CustomSpecSupport)
86+
client, err := connectToBeaconNode(ctx, &beaconNodeConnection{
87+
address: fallbackBeaconNode,
88+
timeout: opts.Timeout,
89+
allowInsecure: true,
90+
customSpecSupport: opts.CustomSpecSupport,
91+
})
7692
if err == nil {
7793
return client, nil
7894
}
7995

8096
return nil, errors.New("failed to connect to any beacon node")
8197
}
8298

83-
func connectToBeaconNode(ctx context.Context, address string, timeout time.Duration, allowInsecure bool, customSpecSupport bool) (eth2client.Service, error) {
99+
// beaconNodeConnection holds the parameters for a single beacon node connection attempt.
100+
type beaconNodeConnection struct {
101+
address string
102+
timeout time.Duration
103+
allowInsecure bool
104+
customSpecSupport bool
105+
}
106+
107+
func connectToBeaconNode(ctx context.Context, conn *beaconNodeConnection) (eth2client.Service, error) {
108+
address := conn.address
84109
if !strings.HasPrefix(address, "http") {
85110
address = fmt.Sprintf("http://%s", address)
86111
}
87-
if !allowInsecure {
112+
if !conn.allowInsecure {
88113
// Ensure the connection is either secure or local.
89114
connectionURL, err := url.Parse(address)
90115
if err != nil {
@@ -101,8 +126,8 @@ func connectToBeaconNode(ctx context.Context, address string, timeout time.Durat
101126
eth2Client, err := http.New(ctx,
102127
http.WithLogLevel(zerolog.Disabled),
103128
http.WithAddress(address),
104-
http.WithTimeout(timeout),
105-
http.WithCustomSpecSupport(customSpecSupport),
129+
http.WithTimeout(conn.timeout),
130+
http.WithCustomSpecSupport(conn.customSpecSupport),
106131
)
107132
if err != nil {
108133
return nil, errors.Wrap(err, "failed to connect to beacon node")

0 commit comments

Comments
 (0)