Skip to content

Commit

Permalink
fix: Multiple network flags should prevent the BN to start (#14169)
Browse files Browse the repository at this point in the history
* Implement Initial Logic

* Include check in main.go

* Add tests for multiple flags

* remove usage of append

* remove config/features dependency

* Move ValidateNetworkFlags to config/features

* Nit

* removed NetworkFlags from cmd

* remove usage of empty string literal

* add comment

* add flag validation to prysctl validator-exit

---------

Co-authored-by: Manu NALEPA <[email protected]>
  • Loading branch information
shyam-patel-kira and nalepae authored Jul 10, 2024
1 parent f8950c8 commit 7c81c7d
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
4 changes: 4 additions & 0 deletions cmd/beacon-chain/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ func before(ctx *cli.Context) error {
return errors.Wrap(err, "failed to set max fd limits")
}

if err := features.ValidateNetworkFlags(ctx); err != nil {
return errors.Wrap(err, "provided multiple network flags")
}

return cmd.ValidateNoArgs(ctx)
}

Expand Down
3 changes: 3 additions & 0 deletions cmd/prysmctl/validator/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ var Commands = []*cli.Command{
if err := cmd.LoadFlagsFromConfig(cliCtx, cliCtx.Command.Flags); err != nil {
return err
}
if err := features.ValidateNetworkFlags(cliCtx); err != nil {
return err
}
if err := tos.VerifyTosAcceptedOrPrompt(cliCtx); err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/validator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ func main() {
return errors.Wrap(err, "failed to setup debug")
}

if err := features.ValidateNetworkFlags(ctx); err != nil {
return errors.Wrap(err, "provided multiple network flags")
}

return cmd.ValidateNoArgs(ctx)
},
After: func(ctx *cli.Context) error {
Expand Down
24 changes: 24 additions & 0 deletions config/features/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ The process for implementing new features using this package is as follows:
package features

import (
"fmt"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -337,3 +339,25 @@ func logDisabled(flag cli.DocGenerationFlag) {
}
log.WithField(name, flag.GetUsage()).Warn(disabledFeatureFlag)
}

// ValidateNetworkFlags validates provided flags and
// prevents beacon node or validator to start
// if more than one network flag is provided
func ValidateNetworkFlags(ctx *cli.Context) error {
networkFlagsCount := 0
for _, flag := range NetworkFlags {
if ctx.IsSet(flag.Names()[0]) {
networkFlagsCount++
if networkFlagsCount > 1 {
// using a forLoop so future addition
// doesn't require changes in this function
var flagNames []string
for _, flag := range NetworkFlags {
flagNames = append(flagNames, "--"+flag.Names()[0])
}
return fmt.Errorf("cannot use more than one network flag at the same time. Possible network flags are: %s", strings.Join(flagNames, ", "))
}
}
}
return nil
}
47 changes: 47 additions & 0 deletions config/features/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,50 @@ func TestConfigureBeaconConfig(t *testing.T) {
c := Get()
assert.Equal(t, true, c.EnableSlasher)
}

func TestValidateNetworkFlags(t *testing.T) {
// Define the test cases
tests := []struct {
name string
args []string
wantErr bool
}{
{
name: "No network flags",
args: []string{"command"},
wantErr: false,
},
{
name: "One network flag",
args: []string{"command", "--sepolia"},
wantErr: false,
},
{
name: "Two network flags",
args: []string{"command", "--sepolia", "--holesky"},
wantErr: true,
},
{
name: "All network flags",
args: []string{"command", "--sepolia", "--holesky", "--mainnet"},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a new CLI app with the ValidateNetworkFlags function as the Before action
app := &cli.App{
Before: ValidateNetworkFlags,
Action: func(c *cli.Context) error {
return nil
},
// Set the network flags for the app
Flags: NetworkFlags,
}
err := app.Run(tt.args)
if (err != nil) != tt.wantErr {
t.Errorf("ValidateNetworkFlags() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

0 comments on commit 7c81c7d

Please sign in to comment.