Skip to content

Commit

Permalink
cmd: add unsafe command (#2413)
Browse files Browse the repository at this point in the history
Adds 'unsafe' command to provide support for testing features from the CLI.

- The 'unsafe' command introduces additional flags in its subcommands, specifically designed for testing purposes.
- The 'charon unsafe' command is hidden from the 'charon --help' output.
- Regular usage of commands will not include any test-only flags, whereas these flags will be available in the 'unsafe' command for testing purposes.

category: feature
ticket: none
  • Loading branch information
dB2510 committed Jul 12, 2023
1 parent eceed17 commit 1ac56aa
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 5 deletions.
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type Config struct {
SyntheticBlockProposals bool
BuilderAPI bool
SimnetBMockFuzz bool
CharonP2PFuzz bool

TestConfig TestConfig
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func New() *cobra.Command {
return newRootCmd(
newVersionCmd(runVersionCmd),
newEnrCmd(runNewENR),
newRunCmd(app.Run),
newRunCmd(app.Run, false),
newRelayCmd(relay.Run),
newDKGCmd(dkg.Run),
newCreateCmd(
Expand All @@ -48,6 +48,7 @@ func New() *cobra.Command {
newAddValidatorsCmd(runAddValidatorsSolo),
newViewClusterManifestCmd(runViewClusterManifest),
),
newUnsafeCmd(newRunCmd(app.Run, true)),
)
}

Expand Down
51 changes: 50 additions & 1 deletion cmd/cmd_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,49 @@ func TestCmdFlags(t *testing.T) {
Args: slice("run"),
ErrorMsg: "either flag 'beacon-node-endpoints' or flag 'simnet-beacon-mock=true' must be specified",
},
{
Name: "unsafe run",
Args: slice("unsafe", "run", "--charon-p2p-fuzz=true"),
Envs: map[string]string{
"CHARON_BEACON_NODE_ENDPOINTS": "http://beacon.node",
},
AppConfig: &app.Config{
Log: log.Config{
Level: "info",
Format: "console",
Color: "auto",
LokiService: "charon",
},
P2P: p2p.Config{
Relays: []string{"https://0.relay.obol.tech"},
TCPAddrs: nil,
Allowlist: "",
Denylist: "",
},
Feature: featureset.Config{
MinStatus: "stable",
Enabled: nil,
Disabled: nil,
},
LockFile: ".charon/cluster-lock.json",
ManifestFile: ".charon/cluster-manifest.pb",
PrivKeyFile: ".charon/charon-enr-private-key",
PrivKeyLocking: false,
SimnetValidatorKeysDir: ".charon/validator_keys",
SimnetSlotDuration: time.Second,
MonitoringAddr: "127.0.0.1:3620",
ValidatorAPIAddr: "127.0.0.1:3600",
BeaconNodeAddrs: []string{"http://beacon.node"},
JaegerAddr: "",
JaegerService: "charon",
CharonP2PFuzz: true,
},
},
{
Name: "run with unsafe flags, unknown flags",
Args: slice("run", "--charon-p2p-fuzz=true"),
ErrorMsg: "unknown flag: --charon-p2p-fuzz",
},
}

for _, test := range tests {
Expand All @@ -114,14 +157,20 @@ func TestCmdFlags(t *testing.T) {
require.Equal(t, *test.AppConfig, config)

return nil
}),
}, false),
newCreateCmd(
newCreateEnrCmd(func(_ io.Writer, datadir string) error {
require.Equal(t, test.Datadir, datadir)

return nil
}),
),
newUnsafeCmd(newRunCmd(func(_ context.Context, config app.Config) error {
require.NotNil(t, test.AppConfig)
require.Equal(t, *test.AppConfig, config)

return nil
}, true)),
)

// Set envs (only for duration of the test)
Expand Down
2 changes: 1 addition & 1 deletion cmd/markdown_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var update = flag.Bool("update-markdown", false, "Updates the reference markdown
func TestConfigReference(t *testing.T) {
cmd := newRootCmd(newRunCmd(func(context.Context, app.Config) error {
return nil
}))
}, false))

var buf bytes.Buffer
cmd.SetOut(&buf)
Expand Down
11 changes: 10 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/obolnetwork/charon/p2p"
)

func newRunCmd(runFunc func(context.Context, app.Config) error) *cobra.Command {
func newRunCmd(runFunc func(context.Context, app.Config) error, unsafe bool) *cobra.Command {
var conf app.Config

cmd := &cobra.Command{
Expand All @@ -38,6 +38,10 @@ func newRunCmd(runFunc func(context.Context, app.Config) error) *cobra.Command {
},
}

if unsafe {
bindUnsafeRunFlags(cmd, &conf)
}

bindPrivKeyFlag(cmd, &conf.PrivKeyFile, &conf.PrivKeyLocking)
bindRunFlags(cmd, &conf)
bindNoVerifyFlag(cmd.Flags(), &conf.NoVerify)
Expand Down Expand Up @@ -86,6 +90,11 @@ func bindRunFlags(cmd *cobra.Command, config *app.Config) {
})
}

// TODO(dhruv): add more test only flags to this function.
func bindUnsafeRunFlags(cmd *cobra.Command, config *app.Config) {
cmd.Flags().BoolVar(&config.CharonP2PFuzz, "charon-p2p-fuzz", false, "Configures charon p2p network to send fuzzed data to its peers.")
}

func bindPrivKeyFlag(cmd *cobra.Command, privKeyFile *string, privkeyLockEnabled *bool) {
cmd.Flags().StringVar(privKeyFile, "private-key-file", ".charon/charon-enr-private-key", "The path to the charon enr private key file.")
cmd.Flags().BoolVar(privkeyLockEnabled, "private-key-file-lock", false, "Enables private key locking to prevent multiple instances using the same key.")
Expand Down
2 changes: 1 addition & 1 deletion cmd/run_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestBindPrivKeyFlag(t *testing.T) {
root := newRootCmd(
newRunCmd(func(_ context.Context, config app.Config) error {
return nil
}),
}, false),
)

// Set envs (only for duration of the test)
Expand Down
20 changes: 20 additions & 0 deletions cmd/unsafe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright © 2022-2023 Obol Labs Inc. Licensed under the terms of a Business Source License 1.1

package cmd

import "github.com/spf13/cobra"

func newUnsafeCmd(cmds ...*cobra.Command) *cobra.Command {
unsafe := &cobra.Command{
Use: "unsafe",
Short: "Unsafe subcommands provides regular charon commands for testing purposes",
Long: "Unsafe subcommands is a group of subcommands that includes both normal and test flags. " +
"It is intended for internal testing of the Charon client and should be used with caution.",
}

// Mark unsafe command as hidden for internal testing purposes.
unsafe.Hidden = true
unsafe.AddCommand(cmds...)

return unsafe
}

0 comments on commit 1ac56aa

Please sign in to comment.