2016-09-11 11:44:14 +00:00
|
|
|
package geth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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"
|
2016-09-11 11:44:14 +00:00
|
|
|
"runtime"
|
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
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2016-12-07 21:07:08 +00:00
|
|
|
"github.com/ethereum/go-ethereum/eth"
|
2016-09-11 11:44:14 +00:00
|
|
|
"github.com/ethereum/go-ethereum/les"
|
2017-03-28 09:04:52 +00:00
|
|
|
"github.com/ethereum/go-ethereum/logger"
|
2016-09-11 11:44:14 +00:00
|
|
|
"github.com/ethereum/go-ethereum/logger/glog"
|
|
|
|
"github.com/ethereum/go-ethereum/node"
|
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-03-15 21:03:01 +00:00
|
|
|
gethparams "github.com/ethereum/go-ethereum/params"
|
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-03-18 19:00:40 +00:00
|
|
|
// setup logging
|
2016-12-07 21:07:08 +00:00
|
|
|
glog.CopyStandardLogTo("INFO")
|
|
|
|
glog.SetToStderr(true)
|
2017-03-18 19:00:40 +00:00
|
|
|
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,
|
2016-12-07 21:07:08 +00:00
|
|
|
UseLightweightKDF: true,
|
2017-03-15 21:03:01 +00:00
|
|
|
Name: config.Name,
|
|
|
|
Version: config.Version,
|
2016-12-07 21:07:08 +00:00
|
|
|
NoDiscovery: true,
|
2017-03-10 00:16:32 +00:00
|
|
|
DiscoveryV5: false,
|
|
|
|
DiscoveryV5Addr: ":0",
|
2017-02-20 09:44:38 +00:00
|
|
|
BootstrapNodes: makeBootstrapNodes(),
|
|
|
|
BootstrapNodesV5: makeBootstrapNodesV5(),
|
2017-03-10 00:16:32 +00:00
|
|
|
ListenAddr: ":0",
|
|
|
|
NAT: nat.Any(),
|
2017-03-15 21:03:01 +00:00
|
|
|
MaxPeers: config.MaxPeers,
|
|
|
|
MaxPendingPeers: config.MaxPendingPeers,
|
|
|
|
IPCPath: makeIPCPath(config),
|
|
|
|
HTTPHost: config.HTTPHost,
|
2017-01-26 00:39:20 +00:00
|
|
|
HTTPPort: config.HTTPPort,
|
2016-12-07 21:07:08 +00:00
|
|
|
HTTPCors: "*",
|
2017-03-15 21:03:01 +00:00
|
|
|
HTTPModules: strings.Split(config.APIModules, ","),
|
|
|
|
WSHost: makeWSHost(config),
|
2017-01-26 00:39:20 +00:00
|
|
|
WSPort: config.WSPort,
|
|
|
|
WSOrigins: "*",
|
2017-03-15 21:03:01 +00:00
|
|
|
WSModules: strings.Split(config.APIModules, ","),
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
glog.V(logger.Info).Infoln("LES protocol is disabled")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
ethConf := ð.Config{
|
|
|
|
Etherbase: common.Address{},
|
2017-03-15 21:03:01 +00:00
|
|
|
ChainConfig: makeChainConfig(config),
|
2016-12-07 21:07:08 +00:00
|
|
|
FastSync: false,
|
|
|
|
LightMode: true,
|
2017-03-15 21:03:01 +00:00
|
|
|
MaxPeers: config.MaxPeers,
|
|
|
|
DatabaseCache: config.LightEthConfig.DatabaseCache,
|
2016-12-07 21:07:08 +00:00
|
|
|
DatabaseHandles: makeDatabaseHandles(),
|
2017-03-15 21:03:01 +00:00
|
|
|
NetworkId: config.NetworkId,
|
|
|
|
Genesis: config.LightEthConfig.Genesis,
|
2016-12-07 21:07:08 +00:00
|
|
|
MinerThreads: runtime.NumCPU(),
|
2017-03-15 21:03:01 +00:00
|
|
|
GasPrice: params.GasPrice,
|
|
|
|
GpoMinGasPrice: params.GpoMinGasPrice,
|
|
|
|
GpoMaxGasPrice: params.GpoMaxGasPrice,
|
|
|
|
GpoFullBlockRatio: params.GpoFullBlockRatio,
|
|
|
|
GpobaseStepDown: params.GpobaseStepDown,
|
|
|
|
GpobaseStepUp: params.GpobaseStepUp,
|
|
|
|
GpobaseCorrectionFactor: params.GpobaseCorrectionFactor,
|
2016-12-07 21:07:08 +00:00
|
|
|
SolcPath: "solc",
|
|
|
|
AutoDAG: false,
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
|
|
|
|
return les.New(ctx, ethConf)
|
|
|
|
}); 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 {
|
|
|
|
glog.V(logger.Info).Infoln("SHH protocol is disabled")
|
|
|
|
return nil
|
|
|
|
}
|
2016-12-07 21:07:08 +00:00
|
|
|
serviceConstructor := func(*node.ServiceContext) (node.Service, error) {
|
|
|
|
return whisper.New(), 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
|
|
|
// makeChainConfig reads the chain configuration from the database in the datadir.
|
2017-03-15 21:03:01 +00:00
|
|
|
func makeChainConfig(config *params.NodeConfig) *gethparams.ChainConfig {
|
|
|
|
chainConfig := new(gethparams.ChainConfig)
|
|
|
|
|
|
|
|
// Homestead fork
|
|
|
|
chainConfig.HomesteadBlock = config.HomesteadBlock
|
|
|
|
|
|
|
|
// DAO fork
|
|
|
|
chainConfig.DAOForkBlock = config.DAOForkBlock
|
|
|
|
chainConfig.DAOForkSupport = config.DAOForkSupport
|
|
|
|
|
|
|
|
// DoS reprice fork
|
|
|
|
chainConfig.EIP150Block = config.EIP150Block
|
|
|
|
chainConfig.EIP150Hash = config.EIP150Hash
|
|
|
|
|
|
|
|
// DoS state cleanup fork
|
|
|
|
chainConfig.EIP155Block = config.EIP155Block
|
|
|
|
chainConfig.EIP158Block = config.EIP158Block
|
|
|
|
chainConfig.ChainId = config.ChainId
|
2016-12-07 21:07:08 +00:00
|
|
|
|
2017-03-15 21:03:01 +00:00
|
|
|
return chainConfig
|
2016-09-11 11:44:14 +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 {
|
|
|
|
err := fmt.Errorf("%v: %v", ErrNodeStartFailure, r)
|
|
|
|
|
|
|
|
// send signal up to native app
|
|
|
|
SendSignal(SignalEnvelope{
|
|
|
|
Type: EventNodeCrashed,
|
|
|
|
Event: NodeCrashEvent{
|
|
|
|
Error: err.Error(),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
Fatalf(err) // os.exit(1) is called internally
|
|
|
|
}
|
|
|
|
}
|