2017-05-16 12:09:52 +00:00
|
|
|
package node
|
2016-09-11 11:44:14 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-12-07 21:07:08 +00:00
|
|
|
"os"
|
2017-03-28 09:04:52 +00:00
|
|
|
"path/filepath"
|
2021-06-16 20:19:45 +00:00
|
|
|
|
2020-01-02 09:10:19 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts"
|
2018-03-20 18:35:28 +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"
|
2018-04-10 06:44:09 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/discv5"
|
2018-11-14 07:03:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2017-03-10 00:16:32 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/nat"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2016-09-11 11:44:14 +00:00
|
|
|
)
|
|
|
|
|
2018-04-16 12:36:09 +00:00
|
|
|
// Errors related to node and services creation.
|
2016-12-07 21:07:08 +00:00
|
|
|
var (
|
2018-09-13 16:31:29 +00:00
|
|
|
ErrNodeMakeFailureFormat = "error creating p2p node: %s"
|
2020-01-08 11:12:23 +00:00
|
|
|
ErrWakuServiceRegistrationFailure = errors.New("failed to register the Waku service")
|
2021-06-16 20:19:45 +00:00
|
|
|
ErrWakuV2ServiceRegistrationFailure = errors.New("failed to register the WakuV2 service")
|
2018-09-13 16:31:29 +00:00
|
|
|
ErrLightEthRegistrationFailure = errors.New("failed to register the LES service")
|
|
|
|
ErrLightEthRegistrationFailureUpstreamEnabled = errors.New("failed to register the LES service, upstream is also configured")
|
|
|
|
ErrPersonalServiceRegistrationFailure = errors.New("failed to register the personal api service")
|
|
|
|
ErrStatusServiceRegistrationFailure = errors.New("failed to register the Status service")
|
|
|
|
ErrPeerServiceRegistrationFailure = errors.New("failed to register the Peer service")
|
2016-12-07 21:07:08 +00:00
|
|
|
)
|
2016-09-11 11:44:14 +00:00
|
|
|
|
2018-03-20 18:35:28 +00:00
|
|
|
// All general log messages in this package should be routed through this logger.
|
2018-06-08 11:29:50 +00:00
|
|
|
var logger = log.New("package", "status-go/node")
|
2018-03-20 18:35:28 +00:00
|
|
|
|
2018-09-13 16:31:29 +00:00
|
|
|
// MakeNode creates a geth node entity
|
2019-08-20 15:38:40 +00:00
|
|
|
func MakeNode(config *params.NodeConfig, accs *accounts.Manager, db *leveldb.DB) (*node.Node, error) {
|
2018-04-16 12:36:09 +00:00
|
|
|
// If DataDir is empty, it means we want to create an ephemeral node
|
|
|
|
// keeping data only in memory.
|
|
|
|
if config.DataDir != "" {
|
|
|
|
// make sure data directory exists
|
|
|
|
if err := os.MkdirAll(filepath.Clean(config.DataDir), os.ModePerm); err != nil {
|
|
|
|
return nil, fmt.Errorf("make node: make data directory: %v", err)
|
|
|
|
}
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2018-04-16 12:36:09 +00:00
|
|
|
// make sure keys directory exists
|
|
|
|
if err := os.MkdirAll(filepath.Clean(config.KeyStoreDir), os.ModePerm); err != nil {
|
|
|
|
return nil, fmt.Errorf("make node: make keys directory: %v", err)
|
|
|
|
}
|
2017-04-28 08:49:15 +00:00
|
|
|
}
|
2017-03-28 09:04:52 +00:00
|
|
|
|
2018-09-13 16:31:29 +00:00
|
|
|
stackConfig, err := newGethNodeConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
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 {
|
2018-07-21 15:32:53 +00:00
|
|
|
return nil, fmt.Errorf(ErrNodeMakeFailureFormat, err.Error())
|
2016-09-11 11:44:14 +00:00
|
|
|
}
|
|
|
|
|
2019-07-21 05:41:30 +00:00
|
|
|
return stack, nil
|
|
|
|
}
|
|
|
|
|
2018-09-13 16:31:29 +00:00
|
|
|
// newGethNodeConfig returns default stack configuration for mobile client node
|
|
|
|
func newGethNodeConfig(config *params.NodeConfig) (*node.Config, error) {
|
2021-07-12 11:18:46 +00:00
|
|
|
// NOTE: I haven't changed anything related to this parameters, but
|
2021-07-07 06:11:09 +00:00
|
|
|
// it seems they were previously ignored if set to 0, but now they seem
|
|
|
|
// to be used, so they need to be set to something
|
|
|
|
maxPeers := 100
|
|
|
|
maxPendingPeers := 100
|
|
|
|
|
|
|
|
if config.MaxPeers != 0 {
|
|
|
|
maxPeers = config.MaxPeers
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.MaxPendingPeers != 0 {
|
|
|
|
maxPendingPeers = config.MaxPendingPeers
|
|
|
|
}
|
|
|
|
|
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{
|
2021-07-07 06:11:09 +00:00
|
|
|
NoDiscovery: true, // we always use only v5 server
|
2023-02-23 16:07:18 +00:00
|
|
|
ListenAddr: config.ListenAddr,
|
2021-07-07 06:11:09 +00:00
|
|
|
NAT: nat.Any(),
|
|
|
|
MaxPeers: maxPeers,
|
|
|
|
MaxPendingPeers: maxPendingPeers,
|
2017-05-16 03:24:56 +00:00
|
|
|
},
|
|
|
|
}
|
2017-07-13 06:54:10 +00:00
|
|
|
|
2018-10-11 10:29:59 +00:00
|
|
|
if config.IPCEnabled {
|
|
|
|
// use well-known defaults
|
|
|
|
if config.IPCFile == "" {
|
|
|
|
config.IPCFile = "geth.ipc"
|
|
|
|
}
|
|
|
|
|
|
|
|
nc.IPCPath = config.IPCFile
|
|
|
|
}
|
|
|
|
|
2018-09-21 14:09:31 +00:00
|
|
|
if config.HTTPEnabled {
|
2024-06-26 14:33:22 +00:00
|
|
|
nc.HTTPModules = config.FormatAPIModules()
|
2017-07-13 06:54:10 +00:00
|
|
|
nc.HTTPHost = config.HTTPHost
|
|
|
|
nc.HTTPPort = config.HTTPPort
|
2018-10-12 12:58:32 +00:00
|
|
|
nc.HTTPVirtualHosts = config.HTTPVirtualHosts
|
|
|
|
nc.HTTPCors = config.HTTPCors
|
2017-07-13 06:54:10 +00:00
|
|
|
}
|
|
|
|
|
2024-06-26 14:33:22 +00:00
|
|
|
if config.WSEnabled {
|
|
|
|
nc.WSModules = config.FormatAPIModules()
|
|
|
|
nc.WSHost = config.WSHost
|
|
|
|
nc.WSPort = config.WSPort
|
2024-07-18 15:30:10 +00:00
|
|
|
// FIXME: this is a temporary solution to allow all origins
|
|
|
|
nc.WSOrigins = []string{"*"}
|
2024-06-26 14:33:22 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 16:31:29 +00:00
|
|
|
if config.ClusterConfig.Enabled {
|
2018-04-10 06:44:09 +00:00
|
|
|
nc.P2P.BootstrapNodesV5 = parseNodesV5(config.ClusterConfig.BootNodes)
|
2018-04-20 12:23:18 +00:00
|
|
|
nc.P2P.StaticNodes = parseNodes(config.ClusterConfig.StaticNodes)
|
2017-12-07 12:07:45 +00:00
|
|
|
}
|
2018-09-13 16:31:29 +00:00
|
|
|
|
|
|
|
if config.NodeKey != "" {
|
|
|
|
sk, err := crypto.HexToECDSA(config.NodeKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// override node's private key
|
|
|
|
nc.P2P.PrivateKey = sk
|
|
|
|
}
|
|
|
|
|
|
|
|
return nc, nil
|
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
// parseNodes creates list of enode.Node out of enode strings.
|
|
|
|
func parseNodes(enodes []string) []*enode.Node {
|
|
|
|
var nodes []*enode.Node
|
|
|
|
for _, item := range enodes {
|
|
|
|
parsedPeer, err := enode.ParseV4(item)
|
2018-06-04 13:47:13 +00:00
|
|
|
if err == nil {
|
|
|
|
nodes = append(nodes, parsedPeer)
|
|
|
|
} else {
|
2018-11-14 07:03:58 +00:00
|
|
|
logger.Error("Failed to parse enode", "enode", item, "err", err)
|
2018-06-04 13:47:13 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 09:44:38 +00:00
|
|
|
}
|
2018-03-20 14:05:21 +00:00
|
|
|
return nodes
|
2017-02-20 09:44:38 +00:00
|
|
|
}
|
2018-04-10 06:44:09 +00:00
|
|
|
|
|
|
|
// parseNodesV5 creates list of discv5.Node out of enode strings.
|
|
|
|
func parseNodesV5(enodes []string) []*discv5.Node {
|
2018-06-04 13:47:13 +00:00
|
|
|
var nodes []*discv5.Node
|
|
|
|
for _, enode := range enodes {
|
|
|
|
parsedPeer, err := discv5.ParseNode(enode)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
nodes = append(nodes, parsedPeer)
|
|
|
|
} else {
|
|
|
|
logger.Error("Failed to parse enode", "enode", enode, "err", err)
|
|
|
|
}
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
|
|
|
return nodes
|
|
|
|
}
|
2018-06-13 11:24:04 +00:00
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
func parseNodesToNodeID(enodes []string) []enode.ID {
|
|
|
|
nodeIDs := make([]enode.ID, 0, len(enodes))
|
2018-07-25 14:48:02 +00:00
|
|
|
for _, node := range parseNodes(enodes) {
|
2018-11-14 07:03:58 +00:00
|
|
|
nodeIDs = append(nodeIDs, node.ID())
|
2018-07-25 14:48:02 +00:00
|
|
|
}
|
|
|
|
return nodeIDs
|
|
|
|
}
|