2017-03-28 09:04:52 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-01-30 11:51:48 +00:00
|
|
|
"context"
|
2018-05-08 21:57:29 +00:00
|
|
|
"errors"
|
2017-11-03 22:07:13 +00:00
|
|
|
"flag"
|
2017-03-28 09:04:52 +00:00
|
|
|
"fmt"
|
2018-03-23 13:26:28 +00:00
|
|
|
stdlog "log"
|
2017-03-28 09:04:52 +00:00
|
|
|
"os"
|
2018-01-23 05:16:13 +00:00
|
|
|
"os/signal"
|
2018-09-21 14:09:31 +00:00
|
|
|
"path/filepath"
|
2017-03-28 09:04:52 +00:00
|
|
|
"runtime"
|
2017-11-03 22:07:13 +00:00
|
|
|
"strings"
|
2018-06-25 15:21:04 +00:00
|
|
|
"time"
|
2017-03-28 09:04:52 +00:00
|
|
|
|
2018-03-20 18:35:28 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2018-06-25 15:21:04 +00:00
|
|
|
gethmetrics "github.com/ethereum/go-ethereum/metrics"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/api"
|
2017-12-02 18:51:55 +00:00
|
|
|
"github.com/status-im/status-go/cmd/statusd/debug"
|
2018-09-13 16:31:29 +00:00
|
|
|
"github.com/status-im/status-go/logutils"
|
2018-01-30 11:51:48 +00:00
|
|
|
nodemetrics "github.com/status-im/status-go/metrics/node"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/node"
|
|
|
|
"github.com/status-im/status-go/params"
|
2018-03-23 13:58:40 +00:00
|
|
|
"github.com/status-im/status-go/profiling"
|
2018-06-12 07:49:38 +00:00
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
2017-03-28 09:04:52 +00:00
|
|
|
)
|
|
|
|
|
2018-06-08 09:20:30 +00:00
|
|
|
const (
|
|
|
|
serverClientName = "Statusd"
|
|
|
|
)
|
|
|
|
|
2017-03-28 09:04:52 +00:00
|
|
|
var (
|
2017-11-03 22:07:13 +00:00
|
|
|
buildStamp = "N/A" // rely on linker: -ldflags -X main.buildStamp"
|
2017-03-28 09:04:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-09-13 16:31:29 +00:00
|
|
|
configFiles configFlags
|
|
|
|
logLevel = flag.String("log", "", `Log level, one of: "ERROR", "WARN", "INFO", "DEBUG", and "TRACE"`)
|
2018-09-21 14:09:31 +00:00
|
|
|
logWithoutColors = flag.Bool("log-without-color", false, "Disables log colors")
|
2018-09-13 16:31:29 +00:00
|
|
|
cliEnabled = flag.Bool("cli", false, "Enable debugging CLI server")
|
|
|
|
cliPort = flag.String("cli-port", debug.CLIPort, "CLI server's listening port")
|
|
|
|
pprofEnabled = flag.Bool("pprof", false, "Enable runtime profiling via pprof")
|
|
|
|
pprofPort = flag.Int("pprof-port", 52525, "Port for runtime profiling via pprof")
|
|
|
|
version = flag.Bool("version", false, "Print version and dump configuration")
|
2018-01-17 20:07:45 +00:00
|
|
|
|
2018-09-21 14:09:31 +00:00
|
|
|
dataDir = flag.String("dir", getDefaultDataDir(), "Directory used by node to store data")
|
|
|
|
register = flag.Bool("register", false, "Register and make the node discoverable by other nodes")
|
|
|
|
mailserver = flag.Bool("mailserver", false, "Enable Mail Server with default configuration")
|
|
|
|
|
2018-02-05 19:25:40 +00:00
|
|
|
// don't change the name of this flag, https://github.com/ethereum/go-ethereum/blob/master/metrics/metrics.go#L41
|
2018-09-13 16:31:29 +00:00
|
|
|
metrics = flag.Bool("metrics", false, "Expose ethereum metrics with debug_metrics jsonrpc call")
|
2018-02-07 10:48:03 +00:00
|
|
|
|
|
|
|
syncAndExit = flag.Int("sync-and-exit", -1, "Timeout in minutes for blockchain sync and exit, zero means no timeout unless sync is finished")
|
2017-11-03 22:07:13 +00:00
|
|
|
)
|
2017-05-03 14:24:48 +00:00
|
|
|
|
2018-03-20 18:35:28 +00:00
|
|
|
// All general log messages in this package should be routed through this logger.
|
|
|
|
var logger = log.New("package", "status-go/cmd/statusd")
|
|
|
|
|
2018-07-25 14:03:35 +00:00
|
|
|
func init() {
|
2018-09-13 16:31:29 +00:00
|
|
|
flag.Var(&configFiles, "c", "JSON configuration file(s). Multiple configuration files can be specified, and will be merged in occurrence order")
|
|
|
|
|
|
|
|
colors := terminal.IsTerminal(int(os.Stdin.Fd()))
|
|
|
|
if err := logutils.OverrideRootLog(true, "ERROR", "", colors); err != nil {
|
|
|
|
stdlog.Fatalf("Error initializing logger: %v", err)
|
|
|
|
}
|
2018-04-10 06:44:09 +00:00
|
|
|
|
2017-11-03 22:07:13 +00:00
|
|
|
flag.Usage = printUsage
|
|
|
|
flag.Parse()
|
2018-05-17 13:03:23 +00:00
|
|
|
if flag.NArg() > 0 {
|
|
|
|
printUsage()
|
2018-09-13 16:31:29 +00:00
|
|
|
logger.Error("Extra args in command line: %v", flag.Args())
|
2018-05-17 13:03:23 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-07-25 14:03:35 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 14:09:31 +00:00
|
|
|
// nolint:gocyclo
|
2018-07-25 14:03:35 +00:00
|
|
|
func main() {
|
2018-09-21 14:09:31 +00:00
|
|
|
opts := []params.Option{params.WithFleet(params.FleetBeta)}
|
|
|
|
if *mailserver {
|
|
|
|
opts = append(opts, params.WithMailserver())
|
|
|
|
}
|
|
|
|
|
2018-09-19 18:29:42 +00:00
|
|
|
config, err := params.NewNodeConfigWithDefaultsAndFiles(
|
2018-09-21 14:09:31 +00:00
|
|
|
*dataDir,
|
|
|
|
uint64(params.RopstenNetworkID),
|
|
|
|
opts,
|
|
|
|
configFiles,
|
2018-09-19 18:29:42 +00:00
|
|
|
)
|
2017-11-03 22:07:13 +00:00
|
|
|
if err != nil {
|
2018-09-13 16:31:29 +00:00
|
|
|
printUsage()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(err.Error())
|
|
|
|
}
|
|
|
|
os.Exit(1)
|
2017-05-16 03:24:56 +00:00
|
|
|
}
|
2018-09-13 16:31:29 +00:00
|
|
|
|
2018-09-21 14:09:31 +00:00
|
|
|
if *register && *mailserver {
|
|
|
|
config.RegisterTopics = append(config.RegisterTopics, params.MailServerDiscv5Topic)
|
|
|
|
} else if *register {
|
|
|
|
config.RegisterTopics = append(config.RegisterTopics, params.WhisperDiscv5Topic)
|
2018-09-19 18:29:42 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 14:09:31 +00:00
|
|
|
// set up logging options
|
|
|
|
setupLogging(config)
|
2018-09-13 16:31:29 +00:00
|
|
|
|
2018-06-08 09:20:30 +00:00
|
|
|
// We want statusd to be distinct from StatusIM client.
|
|
|
|
config.Name = serverClientName
|
2017-05-16 03:24:56 +00:00
|
|
|
|
2017-11-03 22:07:13 +00:00
|
|
|
if *version {
|
2018-06-08 09:20:30 +00:00
|
|
|
printVersion(config, buildStamp)
|
2017-11-03 22:07:13 +00:00
|
|
|
return
|
2017-03-28 09:04:52 +00:00
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
|
2017-11-03 22:07:13 +00:00
|
|
|
backend := api.NewStatusBackend()
|
2018-02-09 13:37:56 +00:00
|
|
|
err = backend.StartNode(config)
|
2017-11-03 22:07:13 +00:00
|
|
|
if err != nil {
|
2018-03-20 18:35:28 +00:00
|
|
|
logger.Error("Node start failed", "error", err)
|
2017-11-03 22:07:13 +00:00
|
|
|
return
|
2017-03-28 09:04:52 +00:00
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
|
2018-01-23 05:16:13 +00:00
|
|
|
// handle interrupt signals
|
2018-04-05 09:45:26 +00:00
|
|
|
interruptCh := haltOnInterruptSignal(backend.StatusNode())
|
2018-03-23 13:58:40 +00:00
|
|
|
|
2017-12-02 18:51:55 +00:00
|
|
|
// Check if debugging CLI connection shall be enabled.
|
|
|
|
if *cliEnabled {
|
|
|
|
err := startDebug(backend)
|
|
|
|
if err != nil {
|
2018-03-20 18:35:28 +00:00
|
|
|
logger.Error("Starting debugging CLI server failed", "error", err)
|
2017-12-02 18:51:55 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 13:58:40 +00:00
|
|
|
// Check if profiling shall be enabled.
|
|
|
|
if *pprofEnabled {
|
|
|
|
profiling.NewProfiler(*pprofPort).Go()
|
|
|
|
}
|
|
|
|
|
2018-01-30 11:51:48 +00:00
|
|
|
// Run stats server.
|
2018-05-16 15:36:59 +00:00
|
|
|
if *metrics {
|
|
|
|
go startCollectingNodeMetrics(interruptCh, backend.StatusNode())
|
2018-06-25 15:21:04 +00:00
|
|
|
go gethmetrics.CollectProcessMetrics(3 * time.Second)
|
2018-01-30 11:51:48 +00:00
|
|
|
}
|
|
|
|
|
2018-02-07 10:48:03 +00:00
|
|
|
// Sync blockchain and stop.
|
|
|
|
if *syncAndExit >= 0 {
|
2018-04-05 09:45:26 +00:00
|
|
|
exitCode := syncAndStopNode(interruptCh, backend.StatusNode(), *syncAndExit)
|
2018-02-08 08:51:53 +00:00
|
|
|
// Call was interrupted. Wait for graceful shutdown.
|
|
|
|
if exitCode == -1 {
|
2018-06-12 16:50:25 +00:00
|
|
|
if gethNode := backend.StatusNode().GethNode(); gethNode != nil {
|
|
|
|
gethNode.Wait()
|
2018-02-08 08:51:53 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Otherwise, exit immediately with a returned exit code.
|
2018-02-07 10:48:03 +00:00
|
|
|
os.Exit(exitCode)
|
|
|
|
}
|
|
|
|
|
2018-06-12 16:50:25 +00:00
|
|
|
gethNode := backend.StatusNode().GethNode()
|
|
|
|
if gethNode != nil {
|
2018-04-16 12:36:09 +00:00
|
|
|
// wait till node has been stopped
|
2018-06-12 16:50:25 +00:00
|
|
|
gethNode.Wait()
|
2017-03-28 09:04:52 +00:00
|
|
|
}
|
2017-11-03 22:07:13 +00:00
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
|
2018-09-21 14:09:31 +00:00
|
|
|
func getDefaultDataDir() string {
|
|
|
|
if home := os.Getenv("HOME"); home != "" {
|
|
|
|
return filepath.Join(home, ".statusd")
|
|
|
|
}
|
|
|
|
return "./statusd-data"
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupLogging(config *params.NodeConfig) {
|
|
|
|
if *logLevel != "" {
|
|
|
|
config.LogLevel = *logLevel
|
|
|
|
}
|
|
|
|
|
|
|
|
colors := !(*logWithoutColors) && terminal.IsTerminal(int(os.Stdin.Fd()))
|
|
|
|
if err := logutils.OverrideRootLog(
|
|
|
|
logEnabled(config),
|
|
|
|
config.LogLevel,
|
|
|
|
config.LogFile,
|
|
|
|
colors,
|
|
|
|
); err != nil {
|
|
|
|
stdlog.Fatalf("Error initializing logger: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-02 18:51:55 +00:00
|
|
|
// startDebug starts the debugging API server.
|
|
|
|
func startDebug(backend *api.StatusBackend) error {
|
2018-06-19 07:49:24 +00:00
|
|
|
_, err := debug.New(backend, *cliPort)
|
2017-12-02 18:51:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-30 11:51:48 +00:00
|
|
|
// startCollectingStats collects various stats about the node and other protocols like Whisper.
|
2018-05-16 15:36:59 +00:00
|
|
|
func startCollectingNodeMetrics(interruptCh <-chan struct{}, statusNode *node.StatusNode) {
|
|
|
|
logger.Info("Starting collecting node metrics")
|
2018-01-30 11:51:48 +00:00
|
|
|
|
2018-06-12 16:50:25 +00:00
|
|
|
gethNode := statusNode.GethNode()
|
|
|
|
if gethNode == nil {
|
2018-04-16 12:36:09 +00:00
|
|
|
logger.Error("Failed to run metrics because it could not get the node")
|
2018-01-30 11:51:48 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
go func() {
|
2018-06-12 16:50:25 +00:00
|
|
|
if err := nodemetrics.SubscribeServerEvents(ctx, gethNode); err != nil {
|
2018-03-20 18:35:28 +00:00
|
|
|
logger.Error("Failed to subscribe server events", "error", err)
|
2018-01-30 11:51:48 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-interruptCh
|
|
|
|
}
|
|
|
|
|
2018-09-13 16:31:29 +00:00
|
|
|
func logEnabled(config *params.NodeConfig) bool {
|
|
|
|
return config.LogLevel != "" || config.LogFile != ""
|
2018-07-25 14:03:35 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 16:31:29 +00:00
|
|
|
var (
|
|
|
|
errStatusServiceRequiresIPC = errors.New("to enable the StatusService on IPC, -ipc flag must be set")
|
|
|
|
errStatusServiceRequiresHTTP = errors.New("to enable the StatusService on HTTP, -http flag must be set")
|
|
|
|
errStatusServiceInvalidFlag = errors.New("-status flag valid values are: ipc, http")
|
|
|
|
)
|
2018-05-08 21:57:29 +00:00
|
|
|
|
|
|
|
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":
|
2018-09-21 14:09:31 +00:00
|
|
|
if !nodeConfig.HTTPEnabled {
|
2018-05-08 21:57:29 +00:00
|
|
|
return nil, errStatusServiceRequiresHTTP
|
|
|
|
}
|
|
|
|
nodeConfig.StatusServiceEnabled = true
|
|
|
|
nodeConfig.AddAPIModule("status")
|
|
|
|
case "":
|
|
|
|
nodeConfig.StatusServiceEnabled = false
|
|
|
|
default:
|
|
|
|
return nil, errStatusServiceInvalidFlag
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodeConfig, nil
|
|
|
|
}
|
|
|
|
|
2017-11-03 22:07:13 +00:00
|
|
|
// printVersion prints verbose output about version and config.
|
2018-06-08 09:20:30 +00:00
|
|
|
func printVersion(config *params.NodeConfig, buildStamp string) {
|
|
|
|
fmt.Println(strings.Title(config.Name))
|
|
|
|
fmt.Println("Version:", config.Version)
|
2017-03-28 09:04:52 +00:00
|
|
|
|
2017-11-03 22:07:13 +00:00
|
|
|
if buildStamp != "" {
|
|
|
|
fmt.Println("Build Stamp:", buildStamp)
|
2017-03-28 09:04:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 22:07:13 +00:00
|
|
|
fmt.Println("Network Id:", config.NetworkID)
|
|
|
|
fmt.Println("Go Version:", runtime.Version())
|
|
|
|
fmt.Println("OS:", runtime.GOOS)
|
|
|
|
fmt.Printf("GOPATH=%s\n", os.Getenv("GOPATH"))
|
|
|
|
fmt.Printf("GOROOT=%s\n", runtime.GOROOT())
|
|
|
|
|
|
|
|
fmt.Println("Loaded Config: ", config)
|
2017-03-28 09:04:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 22:07:13 +00:00
|
|
|
func printUsage() {
|
2018-01-17 16:46:21 +00:00
|
|
|
usage := `
|
|
|
|
Usage: statusd [options]
|
2017-11-03 22:07:13 +00:00
|
|
|
Examples:
|
2018-09-21 14:09:31 +00:00
|
|
|
statusd # run regular Whisper node that joins Status network
|
2018-09-13 16:31:29 +00:00
|
|
|
statusd -c ./default.json # run node with configuration specified in ./default.json file
|
|
|
|
statusd -c ./default.json -c ./standalone.json # run node with configuration specified in ./default.json file, after merging ./standalone.json file
|
|
|
|
statusd -c ./default.json -metrics # run node with configuration specified in ./default.json file, and expose ethereum metrics with debug_metrics jsonrpc call
|
|
|
|
statusd -c ./default.json -cli # run node with configuration specified in ./default.json file, and enable connection by statusd-cli on default port
|
2017-11-03 22:07:13 +00:00
|
|
|
|
|
|
|
Options:
|
2018-01-17 16:46:21 +00:00
|
|
|
`
|
Remove //nolint: gas directives from fmt.Fprintf #590 (#656)
Summary:
Filter out gas linter error checks for fmt.Fprintf commands. This required defining a custom linter around gas that additionally included the offending code.
Notes:
Gas format, without piping it through gometalinter, gives output like this:
$ gas -fmt=csv geth/jail/console/console.go
geth/jail/console/console.go,21,Errors unhandled.,LOW,HIGH,"fmt.Fprintf(w, ""%s: %s"", consoleEventName, formatForConsole(fn.ArgumentList))"
Gometalinter, by default, does not grab the line of code when it filters gas errors. To resolve this, I created a wrapper around gas (I wasn't sure what to call this "gas wrapper", I opted for gasv2, open to other names).
The first part of the regular expression was taken directly from gometalinter (see https://github.com/alecthomas/gometalinter/blob/master/linters.go#L236), and I then appended ,\".*\" to additionally grab the line of code of the offending line. Lastly, I excluded ".*Errors unhandled.*fmt.Fprintf.*" to filter out only fmt.Fprintf errors around omitted errors.
Also as a result of this change, gas lint output will now include the offending code.
Closes #590
2018-02-14 17:58:20 +00:00
|
|
|
fmt.Fprintf(os.Stderr, usage)
|
2017-11-03 22:07:13 +00:00
|
|
|
flag.PrintDefaults()
|
2017-03-28 09:04:52 +00:00
|
|
|
}
|
2018-01-23 05:16:13 +00:00
|
|
|
|
|
|
|
// haltOnInterruptSignal catches interrupt signal (SIGINT) and
|
|
|
|
// stops the node. It times out after 5 seconds
|
|
|
|
// if the node can not be stopped.
|
2018-04-05 09:45:26 +00:00
|
|
|
func haltOnInterruptSignal(statusNode *node.StatusNode) <-chan struct{} {
|
2018-01-30 11:51:48 +00:00
|
|
|
interruptCh := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
signalCh := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(signalCh, os.Interrupt)
|
|
|
|
defer signal.Stop(signalCh)
|
|
|
|
<-signalCh
|
|
|
|
close(interruptCh)
|
2018-03-20 18:35:28 +00:00
|
|
|
logger.Info("Got interrupt, shutting down...")
|
2018-04-05 09:45:26 +00:00
|
|
|
if err := statusNode.Stop(); err != nil {
|
2018-03-20 18:35:28 +00:00
|
|
|
logger.Error("Failed to stop node", "error", err)
|
2018-01-30 11:51:48 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return interruptCh
|
2018-01-23 05:16:13 +00:00
|
|
|
}
|