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
This commit is contained in:
parent
31cf2297d2
commit
c673148bf4
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
stdlog "log"
|
stdlog "log"
|
||||||
|
@ -37,6 +38,7 @@ var (
|
||||||
networkID = flag.Int("networkid", params.RopstenNetworkID, "Network identifier (integer, 1=Homestead, 3=Ropsten, 4=Rinkeby, 777=StatusChain)")
|
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")
|
lesEnabled = flag.Bool("les", false, "Enable LES protocol")
|
||||||
whisperEnabled = flag.Bool("shh", false, "Enable Whisper 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")
|
swarmEnabled = flag.Bool("swarm", false, "Enable Swarm protocol")
|
||||||
maxPeers = flag.Int("maxpeers", 25, "maximum number of p2p peers (including all protocols)")
|
maxPeers = flag.Int("maxpeers", 25, "maximum number of p2p peers (including all protocols)")
|
||||||
httpEnabled = flag.Bool("http", false, "Enable HTTP RPC endpoint")
|
httpEnabled = flag.Bool("http", false, "Enable HTTP RPC endpoint")
|
||||||
|
@ -93,7 +95,7 @@ func main() {
|
||||||
|
|
||||||
config, err := makeNodeConfig()
|
config, err := makeNodeConfig()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
stdlog.Fatalf("Making config failed %s", err)
|
stdlog.Fatalf("Making config failed, %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if *version {
|
if *version {
|
||||||
|
@ -263,6 +265,11 @@ func makeNodeConfig() (*params.NodeConfig, error) {
|
||||||
nodeConfig.ClusterConfig.BootNodes = strings.Split(*bootnodes, ",")
|
nodeConfig.ClusterConfig.BootNodes = strings.Split(*bootnodes, ",")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
nodeConfig, err = configureStatusService(*statusService, nodeConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
if *whisperEnabled {
|
if *whisperEnabled {
|
||||||
return whisperConfig(nodeConfig)
|
return whisperConfig(nodeConfig)
|
||||||
}
|
}
|
||||||
|
@ -275,6 +282,32 @@ func makeNodeConfig() (*params.NodeConfig, error) {
|
||||||
return nodeConfig, nil
|
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.
|
// printVersion prints verbose output about version and config.
|
||||||
func printVersion(config *params.NodeConfig, gitCommit, buildStamp string) {
|
func printVersion(config *params.NodeConfig, gitCommit, buildStamp string) {
|
||||||
if gitCommit != "" && len(gitCommit) > 8 {
|
if gitCommit != "" && len(gitCommit) > 8 {
|
||||||
|
|
|
@ -631,3 +631,8 @@ func (c *NodeConfig) FormatAPIModules() []string {
|
||||||
|
|
||||||
return strings.Split(c.APIModules, ",")
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue