2017-03-28 09:04:52 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-01-30 11:51:48 +00:00
|
|
|
"context"
|
2017-11-03 22:07:13 +00:00
|
|
|
"flag"
|
2017-03-28 09:04:52 +00:00
|
|
|
"fmt"
|
2017-11-03 22:07:13 +00:00
|
|
|
"log"
|
2018-01-30 11:51:48 +00:00
|
|
|
"net/http"
|
2017-03-28 09:04:52 +00:00
|
|
|
"os"
|
2018-01-23 05:16:13 +00:00
|
|
|
"os/signal"
|
2017-03-28 09:04:52 +00:00
|
|
|
"runtime"
|
2017-11-03 22:07:13 +00:00
|
|
|
"strings"
|
2017-03-28 09:04:52 +00:00
|
|
|
|
2017-12-02 18:51:55 +00:00
|
|
|
"github.com/status-im/status-go/cmd/statusd/debug"
|
2017-05-16 12:09:52 +00:00
|
|
|
"github.com/status-im/status-go/geth/api"
|
2018-01-23 05:16:13 +00:00
|
|
|
"github.com/status-im/status-go/geth/common"
|
2017-03-28 09:04:52 +00:00
|
|
|
"github.com/status-im/status-go/geth/params"
|
2018-01-30 11:51:48 +00:00
|
|
|
"github.com/status-im/status-go/metrics"
|
|
|
|
nodemetrics "github.com/status-im/status-go/metrics/node"
|
2017-03-28 09:04:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-11-03 22:07:13 +00:00
|
|
|
gitCommit = "N/A" // rely on linker: -ldflags -X main.GitCommit"
|
|
|
|
buildStamp = "N/A" // rely on linker: -ldflags -X main.buildStamp"
|
2017-03-28 09:04:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2018-03-19 16:22:09 +00:00
|
|
|
clusterConfigFile = flag.String("clusterconfig", "", "Cluster configuration file")
|
|
|
|
prodMode = flag.Bool("production", false, "Whether production settings should be loaded")
|
|
|
|
nodeKeyFile = flag.String("nodekey", "", "P2P node key file (private key)")
|
|
|
|
dataDir = flag.String("datadir", params.DataDir, "Data directory for the databases and keystore")
|
|
|
|
networkID = flag.Int("networkid", params.RopstenNetworkID, "Network identifier (integer, 1=Homestead, 3=Ropsten, 4=Rinkeby, 777=StatusChain)")
|
|
|
|
lesEnabled = flag.Bool("les", false, "LES protocol enabled (default is disabled)")
|
|
|
|
whisperEnabled = flag.Bool("shh", false, "Whisper protocol enabled (default is disabled)")
|
|
|
|
swarmEnabled = flag.Bool("swarm", false, "Swarm protocol enabled")
|
|
|
|
maxPeers = flag.Int("maxpeers", 25, "maximum number of p2p peers (including all protocols)")
|
|
|
|
httpEnabled = flag.Bool("http", false, "HTTP RPC endpoint enabled (default: false)")
|
|
|
|
httpHost = flag.String("httphost", "127.0.0.1", "HTTP RPC host of the listening socket")
|
|
|
|
httpPort = flag.Int("httpport", params.HTTPPort, "HTTP RPC server's listening port")
|
|
|
|
ipcEnabled = flag.Bool("ipc", false, "IPC RPC endpoint enabled")
|
|
|
|
cliEnabled = flag.Bool("cli", false, "Enable debugging CLI server")
|
|
|
|
cliPort = flag.String("cliport", debug.CLIPort, "CLI server's listening port")
|
|
|
|
logLevel = flag.String("log", "INFO", `Log level, one of: "ERROR", "WARN", "INFO", "DEBUG", and "TRACE"`)
|
|
|
|
logFile = flag.String("logfile", "", "Path to the log file")
|
|
|
|
version = flag.Bool("version", false, "Print version")
|
2018-01-17 20:07:45 +00:00
|
|
|
|
|
|
|
listenAddr = flag.String("listenaddr", ":30303", "IP address and port of this node (e.g. 127.0.0.1:30303)")
|
|
|
|
standalone = flag.Bool("standalone", true, "Don't actively connect to peers, wait for incoming connections")
|
2018-01-23 05:16:13 +00:00
|
|
|
bootnodes = flag.String("bootnodes", "", "A list of bootnodes separated by comma")
|
2018-02-06 12:53:04 +00:00
|
|
|
discovery = flag.Bool("discovery", false, "Enable discovery protocol")
|
2018-01-17 20:07:45 +00:00
|
|
|
|
2018-01-30 11:51:48 +00:00
|
|
|
// stats
|
|
|
|
statsEnabled = flag.Bool("stats", false, "Expose node stats via /debug/vars expvar endpoint or Prometheus (log by default)")
|
2018-02-01 16:47:26 +00:00
|
|
|
statsAddr = flag.String("stats.addr", "0.0.0.0:8080", "HTTP address with /debug/vars endpoint")
|
2018-01-30 11:51:48 +00:00
|
|
|
|
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-02-06 11:59:40 +00:00
|
|
|
_ = flag.Bool("metrics", false, "Expose ethereum metrics with debug_metrics jsonrpc call.")
|
2018-01-17 20:07:45 +00:00
|
|
|
// shh stuff
|
|
|
|
passwordFile = flag.String("shh.passwordfile", "", "Password file (password is used for symmetric encryption)")
|
|
|
|
minPow = flag.Float64("shh.pow", params.WhisperMinimumPoW, "PoW for messages to be added to queue, in float format")
|
|
|
|
ttl = flag.Int("shh.ttl", params.WhisperTTL, "Time to live for messages, in seconds")
|
2018-03-02 09:25:30 +00:00
|
|
|
lightClient = flag.Bool("shh.lightclient", false, "Start with empty bloom filter, and don't forward messages")
|
2018-01-17 20:07:45 +00:00
|
|
|
|
|
|
|
// MailServer
|
|
|
|
enableMailServer = flag.Bool("shh.mailserver", false, "Delivers expired messages on demand")
|
|
|
|
|
|
|
|
// Push Notification
|
|
|
|
firebaseAuth = flag.String("shh.firebaseauth", "", "FCM Authorization Key used for sending Push Notifications")
|
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
|
|
|
|
2017-11-03 22:07:13 +00:00
|
|
|
func main() {
|
|
|
|
flag.Usage = printUsage
|
|
|
|
flag.Parse()
|
2017-05-03 14:24:48 +00:00
|
|
|
|
2017-11-03 22:07:13 +00:00
|
|
|
config, err := makeNodeConfig()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Making config failed: %v", err)
|
|
|
|
return
|
2017-05-16 03:24:56 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 22:07:13 +00:00
|
|
|
if *version {
|
|
|
|
printVersion(config, gitCommit, buildStamp)
|
|
|
|
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 {
|
|
|
|
log.Fatalf("Node start failed: %v", err)
|
|
|
|
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-01-30 11:51:48 +00:00
|
|
|
interruptCh := haltOnInterruptSignal(backend.NodeManager())
|
2017-12-02 18:51:55 +00:00
|
|
|
// Check if debugging CLI connection shall be enabled.
|
|
|
|
if *cliEnabled {
|
|
|
|
err := startDebug(backend)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Starting debugging CLI server failed: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 11:51:48 +00:00
|
|
|
// Run stats server.
|
|
|
|
if *statsEnabled {
|
|
|
|
go startCollectingStats(interruptCh, backend.NodeManager())
|
|
|
|
}
|
|
|
|
|
2018-02-07 10:48:03 +00:00
|
|
|
// Sync blockchain and stop.
|
|
|
|
if *syncAndExit >= 0 {
|
2018-02-08 08:51:53 +00:00
|
|
|
exitCode := syncAndStopNode(interruptCh, backend.NodeManager(), *syncAndExit)
|
|
|
|
// Call was interrupted. Wait for graceful shutdown.
|
|
|
|
if exitCode == -1 {
|
|
|
|
if node, err := backend.NodeManager().Node(); err == nil && node != nil {
|
|
|
|
node.Wait()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Otherwise, exit immediately with a returned exit code.
|
2018-02-07 10:48:03 +00:00
|
|
|
os.Exit(exitCode)
|
|
|
|
}
|
|
|
|
|
2017-11-03 22:07:13 +00:00
|
|
|
node, err := backend.NodeManager().Node()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Getting node failed: %v", err)
|
|
|
|
return
|
2017-03-28 09:04:52 +00:00
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
|
2018-02-08 08:51:53 +00:00
|
|
|
// wait till node has been stopped
|
2017-11-03 22:07:13 +00:00
|
|
|
node.Wait()
|
|
|
|
}
|
2017-05-03 14:24:48 +00:00
|
|
|
|
2017-12-02 18:51:55 +00:00
|
|
|
// startDebug starts the debugging API server.
|
|
|
|
func startDebug(backend *api.StatusBackend) error {
|
|
|
|
statusAPI := api.NewStatusAPIWithBackend(backend)
|
|
|
|
_, err := debug.New(statusAPI, *cliPort)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-30 11:51:48 +00:00
|
|
|
// startCollectingStats collects various stats about the node and other protocols like Whisper.
|
|
|
|
func startCollectingStats(interruptCh <-chan struct{}, nodeManager common.NodeManager) {
|
|
|
|
log.Printf("Starting stats on %v", *statsAddr)
|
|
|
|
|
|
|
|
node, err := nodeManager.Node()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to run metrics because could not get node: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
go func() {
|
|
|
|
if err := nodemetrics.SubscribeServerEvents(ctx, node); err != nil {
|
|
|
|
log.Printf("Failed to subscribe server events: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
server := metrics.NewMetricsServer(*statsAddr)
|
2018-02-26 18:36:04 +00:00
|
|
|
defer func() {
|
|
|
|
// server may be nil if `-stats` flag is used
|
|
|
|
// but the binary is compiled without metrics enabled
|
|
|
|
if server == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := server.Shutdown(context.TODO()); err != nil {
|
|
|
|
log.Printf("Failed to shutdown metrics server: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
2018-01-30 11:51:48 +00:00
|
|
|
go func() {
|
2018-02-13 13:02:22 +00:00
|
|
|
// server may be nil if `-stats` flag is used
|
|
|
|
// but the binary is compiled without metrics enabled
|
|
|
|
if server == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-30 11:51:48 +00:00
|
|
|
err := server.ListenAndServe()
|
|
|
|
switch err {
|
|
|
|
case http.ErrServerClosed:
|
|
|
|
default:
|
|
|
|
log.Printf("Metrics server failed: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
<-interruptCh
|
|
|
|
}
|
|
|
|
|
2017-11-03 22:07:13 +00:00
|
|
|
// makeNodeConfig parses incoming CLI options and returns node configuration object
|
|
|
|
func makeNodeConfig() (*params.NodeConfig, error) {
|
|
|
|
devMode := !*prodMode
|
2018-03-19 16:22:09 +00:00
|
|
|
nodeConfig, err := params.NewNodeConfig(*dataDir, *clusterConfigFile, uint64(*networkID), devMode)
|
2017-11-03 22:07:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-09-01 18:44:50 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 12:53:04 +00:00
|
|
|
nodeConfig.ListenAddr = *listenAddr
|
|
|
|
|
2017-11-03 22:07:13 +00:00
|
|
|
// TODO(divan): move this logic into params package
|
|
|
|
if *nodeKeyFile != "" {
|
|
|
|
nodeConfig.NodeKeyFile = *nodeKeyFile
|
2017-03-28 09:04:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 22:07:13 +00:00
|
|
|
if *logLevel != "" {
|
|
|
|
nodeConfig.LogLevel = *logLevel
|
2017-03-28 09:04:52 +00:00
|
|
|
}
|
2017-11-03 22:07:13 +00:00
|
|
|
if *logFile != "" {
|
|
|
|
nodeConfig.LogFile = *logFile
|
2017-03-28 09:04:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 22:07:13 +00:00
|
|
|
nodeConfig.RPCEnabled = *httpEnabled
|
|
|
|
nodeConfig.WhisperConfig.Enabled = *whisperEnabled
|
2018-02-05 10:12:57 +00:00
|
|
|
nodeConfig.MaxPeers = *maxPeers
|
2018-01-17 20:07:45 +00:00
|
|
|
|
2018-02-12 14:17:10 +00:00
|
|
|
nodeConfig.HTTPHost = *httpHost
|
2018-01-17 20:07:45 +00:00
|
|
|
nodeConfig.HTTPPort = *httpPort
|
|
|
|
nodeConfig.IPCEnabled = *ipcEnabled
|
|
|
|
|
2018-01-17 21:40:14 +00:00
|
|
|
nodeConfig.LightEthConfig.Enabled = *lesEnabled
|
2017-11-03 22:07:13 +00:00
|
|
|
nodeConfig.SwarmConfig.Enabled = *swarmEnabled
|
2017-03-28 09:04:52 +00:00
|
|
|
|
2018-01-23 05:16:13 +00:00
|
|
|
if *standalone {
|
2018-03-20 14:05:21 +00:00
|
|
|
nodeConfig.ClusterConfig.Enabled = false
|
|
|
|
nodeConfig.ClusterConfig.BootNodes = nil
|
2018-01-23 05:16:13 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 12:53:04 +00:00
|
|
|
nodeConfig.Discovery = *discovery
|
|
|
|
|
2018-01-23 05:16:13 +00:00
|
|
|
// Even if standalone is true and discovery is disabled,
|
2018-03-20 14:05:21 +00:00
|
|
|
// it's possible to use bootnodes.
|
2018-01-23 05:16:13 +00:00
|
|
|
if *bootnodes != "" {
|
2018-03-20 14:05:21 +00:00
|
|
|
nodeConfig.ClusterConfig.BootNodes = strings.Split(*bootnodes, ",")
|
2018-01-23 05:16:13 +00:00
|
|
|
}
|
|
|
|
|
2018-01-17 20:07:45 +00:00
|
|
|
if *whisperEnabled {
|
|
|
|
return whisperConfig(nodeConfig)
|
|
|
|
}
|
|
|
|
|
2017-11-03 22:07:13 +00:00
|
|
|
// RPC configuration
|
|
|
|
if !*httpEnabled {
|
|
|
|
nodeConfig.HTTPHost = "" // HTTP RPC is disabled
|
2017-03-28 09:04:52 +00:00
|
|
|
}
|
2017-11-03 22:07:13 +00:00
|
|
|
|
|
|
|
return nodeConfig, nil
|
2017-03-28 09:04:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 22:07:13 +00:00
|
|
|
// printVersion prints verbose output about version and config.
|
|
|
|
func printVersion(config *params.NodeConfig, gitCommit, buildStamp string) {
|
|
|
|
if gitCommit != "" && len(gitCommit) > 8 {
|
|
|
|
params.Version += "-" + gitCommit[:8]
|
2017-03-28 09:04:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-03 22:07:13 +00:00
|
|
|
fmt.Println(strings.Title(params.ClientIdentifier))
|
|
|
|
fmt.Println("Version:", params.Version)
|
|
|
|
if gitCommit != "" {
|
|
|
|
fmt.Println("Git Commit:", gitCommit)
|
2017-09-01 18:44:50 +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())
|
|
|
|
|
|
|
|
config.LightEthConfig.Genesis = "SKIP"
|
|
|
|
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:
|
|
|
|
statusd # run status node with defaults
|
|
|
|
statusd -networkid 4 # run node on Rinkeby network
|
|
|
|
statusd -datadir /dir # specify different dir for data
|
|
|
|
statusd -ipc # enable IPC for usage with "geth attach"
|
2017-12-02 18:51:55 +00:00
|
|
|
statusd -cli # 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-01-30 11:51:48 +00:00
|
|
|
func haltOnInterruptSignal(nodeManager common.NodeManager) <-chan struct{} {
|
|
|
|
interruptCh := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
signalCh := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(signalCh, os.Interrupt)
|
|
|
|
defer signal.Stop(signalCh)
|
|
|
|
<-signalCh
|
|
|
|
close(interruptCh)
|
|
|
|
log.Println("Got interrupt, shutting down...")
|
2018-02-09 13:37:56 +00:00
|
|
|
if err := nodeManager.StopNode(); err != nil {
|
|
|
|
log.Printf("Failed to stop node: %v", err.Error())
|
2018-01-30 11:51:48 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return interruptCh
|
2018-01-23 05:16:13 +00:00
|
|
|
}
|