2017-05-16 12:09:52 +00:00
|
|
|
package node
|
2016-09-11 11:44:14 +00:00
|
|
|
|
|
|
|
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
|
|
|
"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
|
|
|
"strings"
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
gethcommon "github.com/ethereum/go-ethereum/common"
|
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-17 16:23:30 +00:00
|
|
|
"github.com/ethereum/go-ethereum/light"
|
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-05-29 16:15:34 +00:00
|
|
|
gethparams "github.com/ethereum/go-ethereum/params"
|
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-08-10 13:35:58 +00:00
|
|
|
"github.com/status-im/status-go/geth/log"
|
2017-03-15 21:03:01 +00:00
|
|
|
"github.com/status-im/status-go/geth/params"
|
2016-09-11 11:44:14 +00:00
|
|
|
)
|
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// node-related errors
|
|
|
|
var (
|
2017-05-03 14:24:48 +00:00
|
|
|
ErrEthServiceRegistrationFailure = errors.New("failed to register the Ethereum service")
|
|
|
|
ErrWhisperServiceRegistrationFailure = errors.New("failed to register the Whisper service")
|
|
|
|
ErrLightEthRegistrationFailure = errors.New("failed to register the LES service")
|
2017-05-16 12:09:52 +00:00
|
|
|
ErrNodeMakeFailure = errors.New("error creating p2p node")
|
|
|
|
ErrNodeRunFailure = errors.New("error running p2p node")
|
|
|
|
ErrNodeStartFailure = errors.New("error starting p2p node")
|
2016-12-07 21:07:08 +00:00
|
|
|
)
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
// MakeNode create a geth node entity
|
2017-05-16 12:09:52 +00:00
|
|
|
func MakeNode(config *params.NodeConfig) (*node.Node, error) {
|
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 {
|
2017-05-16 12:09:52 +00:00
|
|
|
return nil, err
|
2017-03-28 09:04:52 +00:00
|
|
|
}
|
2017-05-16 12:09:52 +00:00
|
|
|
|
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 {
|
2017-05-16 12:09:52 +00:00
|
|
|
return nil, err
|
2017-04-28 08:49:15 +00:00
|
|
|
}
|
2017-03-28 09:04:52 +00:00
|
|
|
|
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-05-16 03:24:56 +00:00
|
|
|
stackConfig := defaultEmbeddedNodeConfig(config)
|
2017-05-02 14:35:37 +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.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-01-26 00:39:20 +00:00
|
|
|
stack, err := node.New(stackConfig)
|
2016-09-11 11:44:14 +00:00
|
|
|
if err != nil {
|
2017-05-16 12:09:52 +00:00
|
|
|
return nil, ErrNodeMakeFailure
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 10:27:12 +00:00
|
|
|
// start Ethereum service if we are not expected to use an upstream server.
|
|
|
|
if !config.UpstreamConfig.Enabled {
|
|
|
|
if err := activateEthService(stack, config); err != nil {
|
|
|
|
return nil, fmt.Errorf("%v: %v", ErrEthServiceRegistrationFailure, err)
|
|
|
|
}
|
|
|
|
} else {
|
2017-09-15 08:44:31 +00:00
|
|
|
// TODO(divan): FIXME: this is rude workaround for #294 issue
|
|
|
|
// we start activate LES service to have RPC handler for `eth_accounts` call
|
|
|
|
// should be removed once proper own RPC and refactoring is completed
|
|
|
|
config.MaxPeers = 0
|
|
|
|
if err := activateEthService(stack, config); err != nil {
|
|
|
|
return nil, fmt.Errorf("%v: %v", ErrEthServiceRegistrationFailure, err)
|
|
|
|
}
|
2017-09-07 07:54:33 +00:00
|
|
|
log.Info("Blockchain synchronization is switched off, RPC requests will be proxied to " + config.UpstreamConfig.URL)
|
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 {
|
2017-05-16 12:09:52 +00:00
|
|
|
return nil, fmt.Errorf("%v: %v", ErrWhisperServiceRegistrationFailure, err)
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
return stack, nil
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2017-05-16 03:24:56 +00:00
|
|
|
// defaultEmbeddedNodeConfig returns default stack configuration for mobile client node
|
|
|
|
func defaultEmbeddedNodeConfig(config *params.NodeConfig) *node.Config {
|
2017-07-13 06:54:10 +00:00
|
|
|
nc := &node.Config{
|
2017-05-16 03:24:56 +00:00
|
|
|
DataDir: config.DataDir,
|
|
|
|
KeyStoreDir: config.KeyStoreDir,
|
|
|
|
UseLightweightKDF: true,
|
2017-05-16 12:09:52 +00:00
|
|
|
NoUSB: true,
|
2017-05-16 03:24:56 +00:00
|
|
|
Name: config.Name,
|
|
|
|
Version: config.Version,
|
|
|
|
P2P: p2p.Config{
|
|
|
|
NoDiscovery: true,
|
2017-05-29 16:15:34 +00:00
|
|
|
DiscoveryV5: true,
|
2017-05-16 03:24:56 +00:00
|
|
|
DiscoveryV5Addr: ":0",
|
|
|
|
BootstrapNodes: makeBootstrapNodes(),
|
|
|
|
BootstrapNodesV5: makeBootstrapNodesV5(),
|
|
|
|
ListenAddr: ":0",
|
|
|
|
NAT: nat.Any(),
|
|
|
|
MaxPeers: config.MaxPeers,
|
|
|
|
MaxPendingPeers: config.MaxPendingPeers,
|
|
|
|
},
|
|
|
|
IPCPath: makeIPCPath(config),
|
|
|
|
HTTPCors: []string{"*"},
|
|
|
|
HTTPModules: strings.Split(config.APIModules, ","),
|
|
|
|
WSHost: makeWSHost(config),
|
|
|
|
WSPort: config.WSPort,
|
|
|
|
WSOrigins: []string{"*"},
|
|
|
|
WSModules: strings.Split(config.APIModules, ","),
|
|
|
|
}
|
2017-07-13 06:54:10 +00:00
|
|
|
|
|
|
|
if config.RPCEnabled {
|
|
|
|
nc.HTTPHost = config.HTTPHost
|
|
|
|
nc.HTTPPort = config.HTTPPort
|
|
|
|
}
|
|
|
|
|
|
|
|
return nc
|
2017-05-16 03:24:56 +00:00
|
|
|
}
|
|
|
|
|
2017-05-17 16:23:30 +00:00
|
|
|
// updateCHT changes trusted canonical hash trie root
|
|
|
|
func updateCHT(eth *les.LightEthereum, config *params.NodeConfig) {
|
2017-08-04 16:14:17 +00:00
|
|
|
if !config.BootClusterConfig.Enabled {
|
|
|
|
return
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
if config.BootClusterConfig.RootNumber == 0 {
|
|
|
|
return
|
2017-05-17 16:23:30 +00:00
|
|
|
}
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
if config.BootClusterConfig.RootHash == "" {
|
|
|
|
return
|
2017-05-17 16:23:30 +00:00
|
|
|
}
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
eth.WriteTrustedCht(light.TrustedCht{
|
|
|
|
Number: uint64(config.BootClusterConfig.RootNumber),
|
|
|
|
Root: gethcommon.HexToHash(config.BootClusterConfig.RootHash),
|
|
|
|
})
|
|
|
|
log.Info("Added trusted CHT",
|
|
|
|
"develop", config.DevMode, "number", config.BootClusterConfig.RootNumber, "hash", config.BootClusterConfig.RootHash)
|
2017-05-17 16:23:30 +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
|
2017-05-03 14:24:48 +00:00
|
|
|
ethConf.NetworkId = config.NetworkID
|
2017-05-02 14:35:37 +00:00
|
|
|
ethConf.DatabaseCache = config.LightEthConfig.DatabaseCache
|
|
|
|
ethConf.MaxPeers = config.MaxPeers
|
2017-08-15 10:27:12 +00:00
|
|
|
|
2016-12-07 21:07:08 +00:00
|
|
|
if err := stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
|
2017-05-17 16:23:30 +00:00
|
|
|
lightEth, err := les.New(ctx, ðConf)
|
|
|
|
if err == nil {
|
|
|
|
updateCHT(lightEth, config)
|
|
|
|
}
|
|
|
|
return lightEth, err
|
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
|
|
|
|
}
|
2017-07-13 06:54:10 +00:00
|
|
|
|
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
|
2017-08-04 16:14:17 +00:00
|
|
|
whisperService := whisper.New(nil)
|
2017-04-12 18:26:54 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2017-05-03 14:24:48 +00:00
|
|
|
return stack.Register(serviceConstructor)
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-07-24 07:06:27 +00:00
|
|
|
var bootstrapNodes []*discover.Node
|
2017-02-20 09:44:38 +00:00
|
|
|
for _, enode := range enodes {
|
2017-07-24 07:06:27 +00:00
|
|
|
bootstrapNodes = append(bootstrapNodes, discover.MustParseNode(enode))
|
2017-02-20 09:44:38 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 07:06:27 +00:00
|
|
|
return bootstrapNodes
|
2017-02-20 09:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// makeBootstrapNodesV5 returns default (hence bootstrap) list of peers
|
|
|
|
func makeBootstrapNodesV5() []*discv5.Node {
|
2017-05-29 16:15:34 +00:00
|
|
|
enodes := gethparams.DiscoveryV5Bootnodes
|
2017-02-20 09:44:38 +00:00
|
|
|
|
2017-07-24 07:06:27 +00:00
|
|
|
var bootstrapNodes []*discv5.Node
|
2017-02-20 09:44:38 +00:00
|
|
|
for _, enode := range enodes {
|
2017-07-24 07:06:27 +00:00
|
|
|
bootstrapNodes = append(bootstrapNodes, discv5.MustParseNode(enode))
|
2017-02-20 09:44:38 +00:00
|
|
|
}
|
|
|
|
|
2017-07-24 07:06:27 +00:00
|
|
|
return bootstrapNodes
|
2017-02-20 09:44:38 +00:00
|
|
|
}
|