2016-09-11 11:44:14 +00:00
|
|
|
package geth
|
|
|
|
|
|
|
|
import (
|
2017-05-02 14:35:37 +00:00
|
|
|
"encoding/json"
|
2016-09-11 11:44:14 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-12-07 21:07:08 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
2017-01-26 15:48:48 +00:00
|
|
|
"path"
|
2017-03-28 09:04:52 +00:00
|
|
|
"path/filepath"
|
2016-12-07 21:07:08 +00:00
|
|
|
"reflect"
|
2017-01-12 17:56:36 +00:00
|
|
|
"runtime/debug"
|
2016-12-07 21:07:08 +00:00
|
|
|
"strings"
|
|
|
|
"syscall"
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2017-05-02 14:35:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2016-12-07 21:07:08 +00:00
|
|
|
"github.com/ethereum/go-ethereum/eth"
|
2017-05-02 14:35:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/eth/downloader"
|
2016-09-11 11:44:14 +00:00
|
|
|
"github.com/ethereum/go-ethereum/les"
|
2017-05-02 14:35:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2016-09-11 11:44:14 +00:00
|
|
|
"github.com/ethereum/go-ethereum/node"
|
2017-05-02 14:35:37 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2017-02-20 09:44:38 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/discv5"
|
2017-03-10 00:16:32 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/nat"
|
2017-04-12 18:26:54 +00:00
|
|
|
"github.com/ethereum/go-ethereum/whisper/mailserver"
|
|
|
|
"github.com/ethereum/go-ethereum/whisper/notifications"
|
2017-03-21 16:27:59 +00:00
|
|
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv5"
|
2017-03-15 21:03:01 +00:00
|
|
|
"github.com/status-im/status-go/geth/params"
|
2016-09-11 11:44:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-09-15 03:08:06 +00:00
|
|
|
EventNodeStarted = "node.started"
|
2017-01-12 17:56:36 +00:00
|
|
|
EventNodeCrashed = "node.crashed"
|
2016-09-11 11:44:14 +00:00
|
|
|
)
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// node-related errors
|
|
|
|
var (
|
|
|
|
ErrEthServiceRegistrationFailure = errors.New("failed to register the Ethereum service")
|
|
|
|
ErrSshServiceRegistrationFailure = errors.New("failed to register the Whisper service")
|
|
|
|
ErrLightEthRegistrationFailure = errors.New("failed to register the LES service")
|
|
|
|
)
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2017-01-26 00:39:20 +00:00
|
|
|
// Node represents running node (serves as a wrapper around P2P node)
|
2016-12-07 21:07:08 +00:00
|
|
|
type Node struct {
|
2017-03-15 21:03:01 +00:00
|
|
|
config *params.NodeConfig // configuration used to create Status node
|
|
|
|
geth *node.Node // reference to the running Geth node
|
|
|
|
gethConfig *node.Config // configuration used to create P2P node
|
|
|
|
started chan struct{} // channel to wait for node to start
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// Inited checks whether status node has been properly initialized
|
|
|
|
func (n *Node) Inited() bool {
|
|
|
|
return n != nil && n.geth != nil
|
2016-09-15 03:08:06 +00:00
|
|
|
}
|
|
|
|
|
2017-03-10 00:16:32 +00:00
|
|
|
// GethStack returns reference to Geth stack
|
|
|
|
func (n *Node) GethStack() *node.Node {
|
|
|
|
return n.geth
|
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// MakeNode create a geth node entity
|
2017-03-15 21:03:01 +00:00
|
|
|
func MakeNode(config *params.NodeConfig) *Node {
|
2017-03-28 09:04:52 +00:00
|
|
|
// make sure data directory exists
|
|
|
|
if err := os.MkdirAll(filepath.Join(config.DataDir), os.ModePerm); err != nil {
|
|
|
|
Fatalf(err)
|
|
|
|
}
|
2017-04-28 08:49:15 +00:00
|
|
|
// make sure keys directory exists
|
|
|
|
if err := os.MkdirAll(filepath.Join(config.KeyStoreDir), os.ModePerm); err != nil {
|
|
|
|
Fatalf(err)
|
|
|
|
}
|
2017-03-28 09:04:52 +00:00
|
|
|
|
2017-03-18 19:00:40 +00:00
|
|
|
// setup logging
|
|
|
|
if _, err := params.SetupLogger(config); err != nil {
|
|
|
|
Fatalf(err)
|
|
|
|
}
|
2016-12-07 21:07:08 +00:00
|
|
|
|
|
|
|
// configure required node (should you need to update node's config, e.g. add bootstrap nodes, see node.Config)
|
2017-01-26 00:39:20 +00:00
|
|
|
stackConfig := &node.Config{
|
2017-03-15 21:03:01 +00:00
|
|
|
DataDir: config.DataDir,
|
2017-04-28 08:49:15 +00:00
|
|
|
KeyStoreDir: config.KeyStoreDir,
|
2016-12-07 21:07:08 +00:00
|
|
|
UseLightweightKDF: true,
|
2017-03-15 21:03:01 +00:00
|
|
|
Name: config.Name,
|
|
|
|
Version: config.Version,
|
2017-05-02 14:35:37 +00:00
|
|
|
P2P: p2p.Config{
|
|
|
|
NoDiscovery: true,
|
|
|
|
DiscoveryV5: false,
|
|
|
|
DiscoveryV5Addr: ":0",
|
|
|
|
BootstrapNodes: makeBootstrapNodes(),
|
|
|
|
BootstrapNodesV5: makeBootstrapNodesV5(),
|
|
|
|
ListenAddr: ":0",
|
|
|
|
NAT: nat.Any(),
|
|
|
|
MaxPeers: config.MaxPeers,
|
|
|
|
MaxPendingPeers: config.MaxPendingPeers,
|
|
|
|
},
|
|
|
|
IPCPath: makeIPCPath(config),
|
|
|
|
HTTPHost: config.HTTPHost,
|
|
|
|
HTTPPort: config.HTTPPort,
|
|
|
|
HTTPCors: []string{"*"},
|
|
|
|
HTTPModules: strings.Split(config.APIModules, ","),
|
|
|
|
WSHost: makeWSHost(config),
|
|
|
|
WSPort: config.WSPort,
|
|
|
|
WSOrigins: []string{"*"},
|
|
|
|
WSModules: strings.Split(config.APIModules, ","),
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(config.NodeKeyFile) > 0 {
|
|
|
|
log.Info("Loading private key file", "file", config.NodeKeyFile)
|
|
|
|
pk, err := crypto.LoadECDSA(config.NodeKeyFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn(fmt.Sprintf("Failed loading private key file '%s': %v", config.NodeKeyFile, err))
|
|
|
|
}
|
|
|
|
|
|
|
|
// override node's private key
|
|
|
|
stackConfig.P2P.PrivateKey = pk
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 18:26:54 +00:00
|
|
|
if len(config.NodeKeyFile) > 0 {
|
|
|
|
log.Info("Loading private key file", "file", config.NodeKeyFile)
|
|
|
|
pk, err := crypto.LoadECDSA(config.NodeKeyFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Info("Failed loading private key file", "file", config.NodeKeyFile, "err", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// override node's private key
|
|
|
|
stackConfig.P2P.PrivateKey = pk
|
|
|
|
}
|
|
|
|
|
2017-01-26 00:39:20 +00:00
|
|
|
stack, err := node.New(stackConfig)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
2016-12-07 21:07:08 +00:00
|
|
|
Fatalf(ErrNodeMakeFailure)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// start Ethereum service
|
2017-03-15 21:03:01 +00:00
|
|
|
if err := activateEthService(stack, config); err != nil {
|
2016-12-07 21:07:08 +00:00
|
|
|
Fatalf(fmt.Errorf("%v: %v", ErrEthServiceRegistrationFailure, err))
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// start Whisper service
|
2017-03-15 21:03:01 +00:00
|
|
|
if err := activateShhService(stack, config); err != nil {
|
2016-12-07 21:07:08 +00:00
|
|
|
Fatalf(fmt.Errorf("%v: %v", ErrSshServiceRegistrationFailure, err))
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
return &Node{
|
2017-01-26 00:39:20 +00:00
|
|
|
geth: stack,
|
|
|
|
gethConfig: stackConfig,
|
|
|
|
started: make(chan struct{}),
|
|
|
|
config: config,
|
2016-12-07 21:07:08 +00:00
|
|
|
}
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// activateEthService configures and registers the eth.Ethereum service with a given node.
|
2017-03-15 21:03:01 +00:00
|
|
|
func activateEthService(stack *node.Node, config *params.NodeConfig) error {
|
2017-03-28 09:04:52 +00:00
|
|
|
if !config.LightEthConfig.Enabled {
|
2017-05-02 14:35:37 +00:00
|
|
|
log.Info("LES protocol is disabled")
|
2017-03-28 09:04:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-02 14:35:37 +00:00
|
|
|
var genesis *core.Genesis
|
|
|
|
if config.LightEthConfig.Genesis != "" {
|
|
|
|
genesis = new(core.Genesis)
|
|
|
|
if err := json.Unmarshal([]byte(config.LightEthConfig.Genesis), genesis); err != nil {
|
|
|
|
return fmt.Errorf("invalid genesis spec: %v", err)
|
|
|
|
}
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-05-02 14:35:37 +00:00
|
|
|
ethConf := eth.DefaultConfig
|
|
|
|
ethConf.Genesis = genesis
|
|
|
|
ethConf.SyncMode = downloader.LightSync
|
|
|
|
ethConf.NetworkId = config.NetworkId
|
|
|
|
ethConf.DatabaseCache = config.LightEthConfig.DatabaseCache
|
|
|
|
ethConf.MaxPeers = config.MaxPeers
|
|
|
|
ethConf.DatabaseHandles = makeDatabaseHandles()
|
2016-12-07 21:07:08 +00:00
|
|
|
if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
|
2017-05-02 14:35:37 +00:00
|
|
|
return les.New(ctx, ðConf)
|
2016-12-07 21:07:08 +00:00
|
|
|
}); err != nil {
|
|
|
|
return fmt.Errorf("%v: %v", ErrLightEthRegistrationFailure, err)
|
|
|
|
}
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
return nil
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// activateShhService configures Whisper and adds it to the given node.
|
2017-03-15 21:03:01 +00:00
|
|
|
func activateShhService(stack *node.Node, config *params.NodeConfig) error {
|
2017-03-28 09:04:52 +00:00
|
|
|
if !config.WhisperConfig.Enabled {
|
2017-05-02 14:35:37 +00:00
|
|
|
log.Info("SHH protocol is disabled")
|
2017-03-28 09:04:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
2016-12-07 21:07:08 +00:00
|
|
|
serviceConstructor := func(*node.ServiceContext) (node.Service, error) {
|
2017-04-12 18:26:54 +00:00
|
|
|
whisperConfig := config.WhisperConfig
|
|
|
|
whisperService := whisper.New()
|
|
|
|
|
|
|
|
// enable mail service
|
|
|
|
if whisperConfig.MailServerNode {
|
|
|
|
password, err := whisperConfig.ReadPasswordFile()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var mailServer mailserver.WMailServer
|
|
|
|
whisperService.RegisterServer(&mailServer)
|
|
|
|
mailServer.Init(whisperService, whisperConfig.DataDir, string(password), whisperConfig.MinimumPoW)
|
|
|
|
}
|
|
|
|
|
|
|
|
// enable notification service
|
|
|
|
if whisperConfig.NotificationServerNode {
|
|
|
|
var notificationServer notifications.NotificationServer
|
|
|
|
whisperService.RegisterNotificationServer(¬ificationServer)
|
|
|
|
|
|
|
|
notificationServer.Init(whisperService, whisperConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
return whisperService, nil
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
2016-12-07 21:07:08 +00:00
|
|
|
if err := stack.Register(serviceConstructor); err != nil {
|
|
|
|
return err
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
return nil
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 02:46:37 +00:00
|
|
|
// makeIPCPath returns IPC-RPC filename
|
2017-03-15 21:03:01 +00:00
|
|
|
func makeIPCPath(config *params.NodeConfig) string {
|
|
|
|
if !config.IPCEnabled {
|
2017-01-26 02:46:37 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-03-15 21:03:01 +00:00
|
|
|
return path.Join(config.DataDir, config.IPCFile)
|
2017-01-26 02:46:37 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 00:39:20 +00:00
|
|
|
// makeWSHost returns WS-RPC Server host, given enabled/disabled flag
|
2017-03-15 21:03:01 +00:00
|
|
|
func makeWSHost(config *params.NodeConfig) string {
|
|
|
|
if !config.WSEnabled {
|
2017-01-26 00:39:20 +00:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-03-15 21:03:01 +00:00
|
|
|
return config.WSHost
|
2017-01-26 00:39:20 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// makeDatabaseHandles makes sure that enough file descriptors are available to the process
|
|
|
|
// (and returns half of them for node's database to use)
|
|
|
|
func makeDatabaseHandles() int {
|
|
|
|
// current limit
|
|
|
|
var limit syscall.Rlimit
|
|
|
|
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
|
|
|
|
Fatalf(err)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// increase limit
|
|
|
|
limit.Cur = limit.Max
|
2017-03-15 21:03:01 +00:00
|
|
|
if limit.Cur > params.DefaultFileDescriptorLimit {
|
|
|
|
limit.Cur = params.DefaultFileDescriptorLimit
|
2016-12-07 21:07:08 +00:00
|
|
|
}
|
|
|
|
if err := syscall.Setrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
|
|
|
|
Fatalf(err)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// re-query limit
|
|
|
|
if err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &limit); err != nil {
|
|
|
|
Fatalf(err)
|
2016-10-07 14:48:36 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// cap limit
|
2017-03-15 21:03:01 +00:00
|
|
|
if limit.Cur > params.DefaultFileDescriptorLimit {
|
|
|
|
limit.Cur = params.DefaultFileDescriptorLimit
|
2016-10-07 14:48:36 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
return int(limit.Cur) / 2
|
2016-10-07 14:48:36 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 09:44:38 +00:00
|
|
|
// makeBootstrapNodes returns default (hence bootstrap) list of peers
|
|
|
|
func makeBootstrapNodes() []*discover.Node {
|
2017-03-10 00:16:32 +00:00
|
|
|
// on desktops params.TestnetBootnodes and params.MainBootnodes,
|
|
|
|
// on mobile client we deliberately keep this list empty
|
|
|
|
enodes := []string{}
|
2017-02-20 09:44:38 +00:00
|
|
|
|
|
|
|
var bootstapNodes []*discover.Node
|
|
|
|
for _, enode := range enodes {
|
|
|
|
bootstapNodes = append(bootstapNodes, discover.MustParseNode(enode))
|
|
|
|
}
|
|
|
|
|
|
|
|
return bootstapNodes
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeBootstrapNodesV5 returns default (hence bootstrap) list of peers
|
|
|
|
func makeBootstrapNodesV5() []*discv5.Node {
|
2017-03-10 00:16:32 +00:00
|
|
|
// on desktops params.DiscoveryV5Bootnodes,
|
|
|
|
// on mobile client we deliberately keep this list empty
|
|
|
|
enodes := []string{}
|
2017-02-20 09:44:38 +00:00
|
|
|
|
|
|
|
var bootstapNodes []*discv5.Node
|
|
|
|
for _, enode := range enodes {
|
|
|
|
bootstapNodes = append(bootstapNodes, discv5.MustParseNode(enode))
|
|
|
|
}
|
|
|
|
|
|
|
|
return bootstapNodes
|
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
func Fatalf(reason interface{}, args ...interface{}) {
|
|
|
|
// decide on output stream
|
|
|
|
w := io.MultiWriter(os.Stdout, os.Stderr)
|
|
|
|
outf, _ := os.Stdout.Stat()
|
|
|
|
errf, _ := os.Stderr.Stat()
|
|
|
|
if outf != nil && errf != nil && os.SameFile(outf, errf) {
|
|
|
|
w = os.Stderr
|
2016-10-11 11:33:37 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// find out whether error or string has been passed as a reason
|
|
|
|
r := reflect.ValueOf(reason)
|
|
|
|
if r.Kind() == reflect.String {
|
|
|
|
fmt.Fprintf(w, "Fatal Failure: "+reason.(string)+"\n", args)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, "Fatal Failure: %v\n", reason.(error))
|
2016-10-11 11:33:37 +00:00
|
|
|
}
|
2016-12-07 21:07:08 +00:00
|
|
|
|
2017-01-12 17:56:36 +00:00
|
|
|
debug.PrintStack()
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
os.Exit(1)
|
2016-10-11 11:33:37 +00:00
|
|
|
}
|
2017-01-12 17:56:36 +00:00
|
|
|
|
|
|
|
// HaltOnPanic recovers from panic, logs issue, sends upward notification, and exits
|
|
|
|
func HaltOnPanic() {
|
|
|
|
if r := recover(); r != nil {
|
2017-04-12 18:26:54 +00:00
|
|
|
err := fmt.Errorf("%v: %v", ErrNodeRunFailure, r)
|
2017-01-12 17:56:36 +00:00
|
|
|
|
|
|
|
// send signal up to native app
|
|
|
|
SendSignal(SignalEnvelope{
|
|
|
|
Type: EventNodeCrashed,
|
|
|
|
Event: NodeCrashEvent{
|
|
|
|
Error: err.Error(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
Fatalf(err) // os.exit(1) is called internally
|
|
|
|
}
|
|
|
|
}
|