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: Multiple network flags should prevent the BN to start #14169

Merged
merged 21 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
abaec99
Implement Initial Logic
shyam-patel-kira Jul 2, 2024
a04554b
Include check in main.go
shyam-patel-kira Jul 2, 2024
0ff8d1d
Add tests for multiple flags
shyam-patel-kira Jul 2, 2024
c460ef8
Merge branch 'prysmaticlabs:develop' into develop
shyam-patel-kira Jul 2, 2024
04d7af1
Merge branch 'develop' into develop
shyam-patel-kira Jul 2, 2024
96111e0
remove usage of append
shyam-patel-kira Jul 3, 2024
d5c69e3
Merge branch 'develop' of https://github.com/shyam-patel-kira/prysm i…
shyam-patel-kira Jul 3, 2024
c024c3f
Merge branch 'develop' of https://github.com/prysmaticlabs/prysm into…
shyam-patel-kira Jul 3, 2024
ef21f29
remove config/features dependency
shyam-patel-kira Jul 3, 2024
14c902f
Move ValidateNetworkFlags to config/features
shyam-patel-kira Jul 3, 2024
0351392
Merge branch 'develop' of https://github.com/prysmaticlabs/prysm into…
shyam-patel-kira Jul 3, 2024
13132ae
Nit
shyam-patel-kira Jul 4, 2024
3da9b30
removed NetworkFlags from cmd
shyam-patel-kira Jul 4, 2024
f338121
remove usage of empty string literal
shyam-patel-kira Jul 4, 2024
ec7ed15
Merge branch 'develop' into develop
shyam-patel-kira Jul 7, 2024
5da62f7
add comment
shyam-patel-kira Jul 8, 2024
a332428
Merge branch 'develop' into develop
shyam-patel-kira Jul 8, 2024
aaeb3d3
Merge branch 'develop' of https://github.com/prysmaticlabs/prysm into…
shyam-patel-kira Jul 10, 2024
250aedf
Merge branch 'develop' of https://github.com/shyam-patel-kira/prysm i…
shyam-patel-kira Jul 10, 2024
dde34ba
add flag validation to prysctl validator-exit
shyam-patel-kira Jul 10, 2024
a195128
Merge branch 'develop' into develop
nalepae Jul 10, 2024
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: 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)
}
})
}
}
Loading