From c673148bf4b0507b1e78d15cca730ac785909df0 Mon Sep 17 00:00:00 2001 From: Andrea Franz Date: Tue, 8 May 2018 23:57:29 +0200 Subject: [PATCH] add `-status` flag to enable the Status service (#902) * add `-status` flag to enable the Status service * remove status from default APIModules and add it only from statusd if specified * remove AddAPIModule method * allow -status flag values to be http or ipc --- cmd/statusd/flags_test.go | 94 +++++++++++++++++++++++++++++++++++++++ cmd/statusd/main.go | 35 ++++++++++++++- geth/params/config.go | 5 +++ 3 files changed, 133 insertions(+), 1 deletion(-) create mode 100644 cmd/statusd/flags_test.go diff --git a/cmd/statusd/flags_test.go b/cmd/statusd/flags_test.go new file mode 100644 index 000000000..e13fcea73 --- /dev/null +++ b/cmd/statusd/flags_test.go @@ -0,0 +1,94 @@ +package main + +import ( + "fmt" + "testing" + + "github.com/status-im/status-go/geth/params" + "github.com/stretchr/testify/require" +) + +func TestStatusFlag(t *testing.T) { + service := "status" + + scenarios := []struct { + ipcEnabled bool + httpEnabled bool + flag string + err error + enabled bool + public bool + }{ + // no flags + {}, + // -status=ipc -ipc + { + ipcEnabled: true, + flag: "ipc", + enabled: true, + }, + // -status=http -http + { + httpEnabled: true, + flag: "http", + enabled: true, + public: true, + }, + // -status=ipc -http -ipc + { + httpEnabled: true, + ipcEnabled: true, + flag: "ipc", + enabled: true, + }, + // -http -ipc + { + httpEnabled: true, + ipcEnabled: true, + flag: "", + }, + // -status=ipc + { + err: errStatusServiceRequiresIPC, + flag: "ipc", + }, + // -status=http + { + err: errStatusServiceRequiresHTTP, + flag: "http", + }, + // -status=bad-value + { + err: errStatusServiceInvalidFlag, + flag: "bad-value", + }, + } + + for i, s := range scenarios { + msg := fmt.Sprintf("scenario %d", i) + + c, err := params.NewNodeConfig("", "", 0) + require.Nil(t, err, msg) + + c.IPCEnabled = s.ipcEnabled + c.RPCEnabled = s.httpEnabled + + c, err = configureStatusService(s.flag, c) + + if s.err != nil { + require.Equal(t, s.err, err, msg) + require.Nil(t, c, msg) + continue + } + + require.Nil(t, err, msg) + require.Equal(t, s.enabled, c.StatusServiceEnabled, msg) + + modules := c.FormatAPIModules() + if s.public { + require.Contains(t, modules, service, msg) + } else { + require.NotContains(t, modules, service, msg) + } + } +} diff --git a/cmd/statusd/main.go b/cmd/statusd/main.go index 8233a136d..0378b365e 100644 --- a/cmd/statusd/main.go +++ b/cmd/statusd/main.go @@ -2,6 +2,7 @@ package main import ( "context" + "errors" "flag" "fmt" stdlog "log" @@ -37,6 +38,7 @@ var ( networkID = flag.Int("networkid", params.RopstenNetworkID, "Network identifier (integer, 1=Homestead, 3=Ropsten, 4=Rinkeby, 777=StatusChain)") lesEnabled = flag.Bool("les", false, "Enable LES protocol") whisperEnabled = flag.Bool("shh", false, "Enable Whisper protocol") + statusService = flag.String("status", "", `Enable StatusService, possible values: "ipc", "http"`) swarmEnabled = flag.Bool("swarm", false, "Enable Swarm protocol") maxPeers = flag.Int("maxpeers", 25, "maximum number of p2p peers (including all protocols)") httpEnabled = flag.Bool("http", false, "Enable HTTP RPC endpoint") @@ -93,7 +95,7 @@ func main() { config, err := makeNodeConfig() if err != nil { - stdlog.Fatalf("Making config failed %s", err) + stdlog.Fatalf("Making config failed, %s", err) } if *version { @@ -263,6 +265,11 @@ func makeNodeConfig() (*params.NodeConfig, error) { nodeConfig.ClusterConfig.BootNodes = strings.Split(*bootnodes, ",") } + nodeConfig, err = configureStatusService(*statusService, nodeConfig) + if err != nil { + return nil, err + } + if *whisperEnabled { return whisperConfig(nodeConfig) } @@ -275,6 +282,32 @@ func makeNodeConfig() (*params.NodeConfig, error) { return nodeConfig, nil } +var errStatusServiceRequiresIPC = errors.New("to enable the StatusService on IPC, -ipc flag must be set") +var errStatusServiceRequiresHTTP = errors.New("to enable the StatusService on HTTP, -http flag must be set") +var errStatusServiceInvalidFlag = errors.New("-status flag valid values are: ipc, http") + +func configureStatusService(flagValue string, nodeConfig *params.NodeConfig) (*params.NodeConfig, error) { + switch flagValue { + case "ipc": + if !nodeConfig.IPCEnabled { + return nil, errStatusServiceRequiresIPC + } + nodeConfig.StatusServiceEnabled = true + case "http": + if !nodeConfig.RPCEnabled { + return nil, errStatusServiceRequiresHTTP + } + nodeConfig.StatusServiceEnabled = true + nodeConfig.AddAPIModule("status") + case "": + nodeConfig.StatusServiceEnabled = false + default: + return nil, errStatusServiceInvalidFlag + } + + return nodeConfig, nil +} + // printVersion prints verbose output about version and config. func printVersion(config *params.NodeConfig, gitCommit, buildStamp string) { if gitCommit != "" && len(gitCommit) > 8 { diff --git a/geth/params/config.go b/geth/params/config.go index 145691ebd..e65862b95 100644 --- a/geth/params/config.go +++ b/geth/params/config.go @@ -631,3 +631,8 @@ func (c *NodeConfig) FormatAPIModules() []string { return strings.Split(c.APIModules, ",") } + +// AddAPIModule adds a mobule to APIModules +func (c *NodeConfig) AddAPIModule(m string) { + c.APIModules = fmt.Sprintf("%s,%s", c.APIModules, m) +}