2017-05-16 12:09:52 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
2021-06-30 11:40:54 +00:00
|
|
|
"database/sql"
|
2017-05-16 12:09:52 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2018-08-17 06:25:55 +00:00
|
|
|
"net"
|
2017-05-16 12:09:52 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2018-08-17 06:25:55 +00:00
|
|
|
"reflect"
|
2017-05-16 12:09:52 +00:00
|
|
|
"sync"
|
|
|
|
|
2020-01-02 09:10:19 +00:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
"github.com/ethereum/go-ethereum/accounts"
|
2018-03-20 18:35:28 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2017-05-16 12:09:52 +00:00
|
|
|
"github.com/ethereum/go-ethereum/node"
|
2018-06-27 07:55:25 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
2018-11-14 07:03:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enr"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
2021-06-30 11:40:54 +00:00
|
|
|
"github.com/status-im/status-go/account"
|
2021-07-09 13:19:33 +00:00
|
|
|
"github.com/status-im/status-go/common"
|
2021-05-14 10:55:42 +00:00
|
|
|
"github.com/status-im/status-go/connection"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/db"
|
2018-07-04 10:51:47 +00:00
|
|
|
"github.com/status-im/status-go/discovery"
|
2021-06-30 11:40:54 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
|
|
|
"github.com/status-im/status-go/peers"
|
|
|
|
"github.com/status-im/status-go/rpc"
|
2021-06-30 11:40:54 +00:00
|
|
|
accountssvc "github.com/status-im/status-go/services/accounts"
|
|
|
|
appmetricsservice "github.com/status-im/status-go/services/appmetrics"
|
2019-07-17 05:28:37 +00:00
|
|
|
"github.com/status-im/status-go/services/browsers"
|
2020-10-28 07:56:14 +00:00
|
|
|
localnotifications "github.com/status-im/status-go/services/local-notifications"
|
2021-06-30 11:40:54 +00:00
|
|
|
"github.com/status-im/status-go/services/mailservers"
|
2018-07-16 07:40:40 +00:00
|
|
|
"github.com/status-im/status-go/services/peer"
|
2019-07-21 05:41:30 +00:00
|
|
|
"github.com/status-im/status-go/services/permissions"
|
2021-06-30 11:40:54 +00:00
|
|
|
"github.com/status-im/status-go/services/personal"
|
|
|
|
"github.com/status-im/status-go/services/rpcfilters"
|
|
|
|
"github.com/status-im/status-go/services/rpcstats"
|
|
|
|
"github.com/status-im/status-go/services/subscriptions"
|
2020-01-20 20:56:06 +00:00
|
|
|
"github.com/status-im/status-go/services/wakuext"
|
2021-06-16 20:19:45 +00:00
|
|
|
"github.com/status-im/status-go/services/wakuv2ext"
|
2019-06-14 10:16:30 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet"
|
2021-06-30 11:40:54 +00:00
|
|
|
"github.com/status-im/status-go/timesource"
|
2020-01-20 20:56:06 +00:00
|
|
|
"github.com/status-im/status-go/waku"
|
2021-06-16 20:19:45 +00:00
|
|
|
"github.com/status-im/status-go/wakuv2"
|
2017-05-16 12:09:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// errors
|
|
|
|
var (
|
2018-04-16 12:36:09 +00:00
|
|
|
ErrNodeRunning = errors.New("node is already running")
|
|
|
|
ErrNoGethNode = errors.New("geth node is not available")
|
2018-04-09 07:16:43 +00:00
|
|
|
ErrNoRunningNode = errors.New("there is no running node")
|
|
|
|
ErrAccountKeyStoreMissing = errors.New("account key store is not set")
|
2018-04-20 15:39:53 +00:00
|
|
|
ErrServiceUnknown = errors.New("service unknown")
|
2019-01-02 18:57:36 +00:00
|
|
|
ErrDiscoveryRunning = errors.New("discovery is already running")
|
2021-07-09 13:19:33 +00:00
|
|
|
ErrRPCMethodUnavailable = `{"jsonrpc":"2.0","id":1,"error":{"code":-32601,"message":"the method called does not exist/is not available"}}`
|
2017-05-16 12:09:52 +00:00
|
|
|
)
|
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
// StatusNode abstracts contained geth node and provides helper methods to
|
|
|
|
// interact with it.
|
|
|
|
type StatusNode struct {
|
2018-04-12 16:17:10 +00:00
|
|
|
mu sync.RWMutex
|
|
|
|
|
2021-06-30 11:40:54 +00:00
|
|
|
appDB *sql.DB
|
|
|
|
multiaccountsDB *multiaccounts.Database
|
|
|
|
|
2021-07-09 13:19:33 +00:00
|
|
|
config *params.NodeConfig // Status node configuration
|
|
|
|
gethNode *node.Node // reference to Geth P2P stack/node
|
|
|
|
rpcClient *rpc.Client // reference to an RPC client
|
2018-04-09 07:16:43 +00:00
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
discovery discovery.Discovery
|
2018-07-03 11:27:04 +00:00
|
|
|
register *peers.Register
|
|
|
|
peerPool *peers.PeerPool
|
|
|
|
db *leveldb.DB // used as a cache for PeerPool
|
2018-04-10 06:44:09 +00:00
|
|
|
|
2018-04-12 16:17:10 +00:00
|
|
|
log log.Logger
|
2021-06-30 11:40:54 +00:00
|
|
|
|
|
|
|
gethAccountManager *account.GethManager
|
|
|
|
accountsManager *accounts.Manager
|
|
|
|
|
|
|
|
// services
|
2021-07-09 13:19:33 +00:00
|
|
|
services []common.StatusService
|
|
|
|
publicMethods map[string]bool
|
2021-07-07 06:11:09 +00:00
|
|
|
// we explicitly list every service, we could use interfaces
|
2021-07-09 13:19:33 +00:00
|
|
|
// and store them in a nicer way and user reflection, but for now stupid is good
|
2021-06-30 11:40:54 +00:00
|
|
|
rpcFiltersSrvc *rpcfilters.Service
|
|
|
|
subscriptionsSrvc *subscriptions.Service
|
|
|
|
rpcStatsSrvc *rpcstats.Service
|
|
|
|
accountsSrvc *accountssvc.Service
|
|
|
|
browsersSrvc *browsers.Service
|
|
|
|
permissionsSrvc *permissions.Service
|
|
|
|
mailserversSrvc *mailservers.Service
|
|
|
|
appMetricsSrvc *appmetricsservice.Service
|
|
|
|
walletSrvc *wallet.Service
|
|
|
|
peerSrvc *peer.Service
|
|
|
|
localNotificationsSrvc *localnotifications.Service
|
|
|
|
personalSrvc *personal.Service
|
|
|
|
timeSourceSrvc *timesource.NTPTimeSource
|
|
|
|
wakuSrvc *waku.Waku
|
|
|
|
wakuExtSrvc *wakuext.Service
|
|
|
|
wakuV2Srvc *wakuv2.Waku
|
|
|
|
wakuV2ExtSrvc *wakuv2ext.Service
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
// New makes new instance of StatusNode.
|
|
|
|
func New() *StatusNode {
|
|
|
|
return &StatusNode{
|
2021-06-30 11:40:54 +00:00
|
|
|
gethAccountManager: account.NewGethManager(),
|
|
|
|
log: log.New("package", "status-go/node.StatusNode"),
|
2021-07-09 13:19:33 +00:00
|
|
|
publicMethods: make(map[string]bool),
|
2018-03-20 18:35:28 +00:00
|
|
|
}
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2018-04-16 12:36:09 +00:00
|
|
|
// Config exposes reference to running node's configuration
|
|
|
|
func (n *StatusNode) Config() *params.NodeConfig {
|
|
|
|
n.mu.RLock()
|
|
|
|
defer n.mu.RUnlock()
|
|
|
|
|
|
|
|
return n.config
|
|
|
|
}
|
|
|
|
|
|
|
|
// GethNode returns underlying geth node.
|
|
|
|
func (n *StatusNode) GethNode() *node.Node {
|
|
|
|
n.mu.RLock()
|
|
|
|
defer n.mu.RUnlock()
|
|
|
|
|
|
|
|
return n.gethNode
|
|
|
|
}
|
|
|
|
|
2018-06-27 07:55:25 +00:00
|
|
|
// Server retrieves the currently running P2P network layer.
|
|
|
|
func (n *StatusNode) Server() *p2p.Server {
|
|
|
|
n.mu.RLock()
|
|
|
|
defer n.mu.RUnlock()
|
|
|
|
|
|
|
|
if n.gethNode == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return n.gethNode.Server()
|
|
|
|
}
|
|
|
|
|
2019-01-02 18:57:36 +00:00
|
|
|
// Start starts current StatusNode, failing if it's already started.
|
|
|
|
// It accepts a list of services that should be added to the node.
|
2021-06-30 11:40:54 +00:00
|
|
|
func (n *StatusNode) Start(config *params.NodeConfig, accs *accounts.Manager) error {
|
2019-01-02 18:57:36 +00:00
|
|
|
return n.StartWithOptions(config, StartOptions{
|
2019-08-20 15:38:40 +00:00
|
|
|
StartDiscovery: true,
|
|
|
|
AccountsManager: accs,
|
2019-01-02 18:57:36 +00:00
|
|
|
})
|
|
|
|
}
|
2018-04-16 08:01:37 +00:00
|
|
|
|
2019-01-02 18:57:36 +00:00
|
|
|
// StartOptions allows to control some parameters of Start() method.
|
|
|
|
type StartOptions struct {
|
2019-08-20 15:38:40 +00:00
|
|
|
StartDiscovery bool
|
|
|
|
AccountsManager *accounts.Manager
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2019-01-02 18:57:36 +00:00
|
|
|
// StartWithOptions starts current StatusNode, failing if it's already started.
|
|
|
|
// It takes some options that allows to further configure starting process.
|
|
|
|
func (n *StatusNode) StartWithOptions(config *params.NodeConfig, options StartOptions) error {
|
2018-05-02 12:14:08 +00:00
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
|
|
|
|
|
|
|
if n.isRunning() {
|
2020-02-26 19:35:47 +00:00
|
|
|
n.log.Debug("node is already running")
|
2018-05-02 12:14:08 +00:00
|
|
|
return ErrNodeRunning
|
|
|
|
}
|
2018-04-20 11:26:54 +00:00
|
|
|
|
2021-06-30 11:40:54 +00:00
|
|
|
n.accountsManager = options.AccountsManager
|
|
|
|
|
2020-02-26 19:35:47 +00:00
|
|
|
n.log.Debug("starting with options", "ClusterConfig", config.ClusterConfig)
|
2018-09-13 16:31:29 +00:00
|
|
|
|
2018-05-02 12:14:08 +00:00
|
|
|
db, err := db.Create(config.DataDir, params.StatusDatabase)
|
|
|
|
if err != nil {
|
2019-08-20 15:38:40 +00:00
|
|
|
return fmt.Errorf("failed to create database at %s: %v", config.DataDir, err)
|
2018-04-20 11:26:54 +00:00
|
|
|
}
|
2018-05-02 12:14:08 +00:00
|
|
|
|
2018-05-11 07:12:50 +00:00
|
|
|
n.db = db
|
|
|
|
|
2021-06-30 11:40:54 +00:00
|
|
|
err = n.startWithDB(config, options.AccountsManager, db)
|
2019-01-02 18:57:36 +00:00
|
|
|
|
|
|
|
// continue only if there was no error when starting node with a db
|
|
|
|
if err == nil && options.StartDiscovery && n.discoveryEnabled() {
|
|
|
|
err = n.startDiscovery()
|
|
|
|
}
|
2018-05-02 12:14:08 +00:00
|
|
|
|
2018-04-20 11:26:54 +00:00
|
|
|
if err != nil {
|
2018-05-02 12:14:08 +00:00
|
|
|
if dberr := db.Close(); dberr != nil {
|
|
|
|
n.log.Error("error while closing leveldb after node crash", "error", dberr)
|
|
|
|
}
|
|
|
|
n.db = nil
|
2018-04-20 11:26:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-02 12:14:08 +00:00
|
|
|
return nil
|
2018-04-20 11:26:54 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 11:40:54 +00:00
|
|
|
func (n *StatusNode) startWithDB(config *params.NodeConfig, accs *accounts.Manager, db *leveldb.DB) error {
|
2019-08-20 15:38:40 +00:00
|
|
|
if err := n.createNode(config, accs, db); err != nil {
|
2019-01-02 18:57:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
n.config = config
|
|
|
|
|
2021-06-30 11:40:54 +00:00
|
|
|
if err := n.setupRPCClient(); err != nil {
|
2019-01-02 18:57:36 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-30 11:40:54 +00:00
|
|
|
if err := n.initServices(config); err != nil {
|
2019-01-02 18:57:36 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-06-30 11:40:54 +00:00
|
|
|
return n.startGethNode()
|
2019-01-02 18:57:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 15:38:40 +00:00
|
|
|
func (n *StatusNode) createNode(config *params.NodeConfig, accs *accounts.Manager, db *leveldb.DB) (err error) {
|
|
|
|
n.gethNode, err = MakeNode(config, accs, db)
|
2018-05-02 12:14:08 +00:00
|
|
|
return err
|
2018-04-16 12:36:09 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 19:35:47 +00:00
|
|
|
// startGethNode starts current StatusNode, will fail if it's already started.
|
2021-06-30 11:40:54 +00:00
|
|
|
func (n *StatusNode) startGethNode() error {
|
2018-04-16 12:36:09 +00:00
|
|
|
return n.gethNode.Start()
|
|
|
|
}
|
|
|
|
|
2018-04-16 08:01:37 +00:00
|
|
|
func (n *StatusNode) setupRPCClient() (err error) {
|
2021-07-09 13:19:33 +00:00
|
|
|
// setup RPC client
|
2021-06-30 11:40:54 +00:00
|
|
|
gethNodeClient, err := n.gethNode.Attach()
|
2018-04-16 08:01:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n.rpcClient, err = rpc.NewClient(gethNodeClient, n.config.UpstreamConfig)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-03 11:27:04 +00:00
|
|
|
func (n *StatusNode) discoveryEnabled() bool {
|
2018-09-13 16:31:29 +00:00
|
|
|
return n.config != nil && (!n.config.NoDiscovery || n.config.Rendezvous) && n.config.ClusterConfig.Enabled
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
func (n *StatusNode) discoverNode() (*enode.Node, error) {
|
2018-08-17 06:25:55 +00:00
|
|
|
if !n.isRunning() {
|
2018-11-14 07:03:58 +00:00
|
|
|
return nil, nil
|
2018-08-17 06:25:55 +00:00
|
|
|
}
|
|
|
|
|
2019-01-25 18:17:08 +00:00
|
|
|
server := n.gethNode.Server()
|
|
|
|
discNode := server.Self()
|
2018-11-14 07:03:58 +00:00
|
|
|
|
|
|
|
if n.config.AdvertiseAddr == "" {
|
|
|
|
return discNode, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
n.log.Info("Using AdvertiseAddr for rendezvous", "addr", n.config.AdvertiseAddr)
|
|
|
|
|
|
|
|
r := discNode.Record()
|
|
|
|
r.Set(enr.IP(net.ParseIP(n.config.AdvertiseAddr)))
|
2019-01-25 18:17:08 +00:00
|
|
|
if err := enode.SignV4(r, server.PrivateKey); err != nil {
|
2018-11-14 07:03:58 +00:00
|
|
|
return nil, err
|
2018-08-17 06:25:55 +00:00
|
|
|
}
|
2018-11-14 07:03:58 +00:00
|
|
|
return enode.New(enode.ValidSchemes[r.IdentityScheme()], r)
|
2018-08-17 06:25:55 +00:00
|
|
|
}
|
|
|
|
|
2018-07-20 06:46:29 +00:00
|
|
|
func (n *StatusNode) startRendezvous() (discovery.Discovery, error) {
|
2018-07-04 10:51:47 +00:00
|
|
|
if !n.config.Rendezvous {
|
2018-07-20 06:46:29 +00:00
|
|
|
return nil, errors.New("rendezvous is not enabled")
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
if len(n.config.ClusterConfig.RendezvousNodes) == 0 {
|
2018-07-20 06:46:29 +00:00
|
|
|
return nil, errors.New("rendezvous node must be provided if rendezvous discovery is enabled")
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
maddrs := make([]ma.Multiaddr, len(n.config.ClusterConfig.RendezvousNodes))
|
|
|
|
for i, addr := range n.config.ClusterConfig.RendezvousNodes {
|
|
|
|
var err error
|
|
|
|
maddrs[i], err = ma.NewMultiaddr(addr)
|
|
|
|
if err != nil {
|
2018-07-20 06:46:29 +00:00
|
|
|
return nil, fmt.Errorf("failed to parse rendezvous node %s: %v", n.config.ClusterConfig.RendezvousNodes[0], err)
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-14 07:03:58 +00:00
|
|
|
node, err := n.discoverNode()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get a discover node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return discovery.NewRendezvous(maddrs, n.gethNode.Server().PrivateKey, node)
|
2018-07-03 11:27:04 +00:00
|
|
|
}
|
|
|
|
|
2019-01-02 18:57:36 +00:00
|
|
|
// StartDiscovery starts the peers discovery protocols depending on the node config.
|
|
|
|
func (n *StatusNode) StartDiscovery() error {
|
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
|
|
|
|
|
|
|
if n.discoveryEnabled() {
|
|
|
|
return n.startDiscovery()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-03 11:27:04 +00:00
|
|
|
func (n *StatusNode) startDiscovery() error {
|
2019-01-02 18:57:36 +00:00
|
|
|
if n.isDiscoveryRunning() {
|
|
|
|
return ErrDiscoveryRunning
|
|
|
|
}
|
|
|
|
|
2018-07-20 06:46:29 +00:00
|
|
|
discoveries := []discovery.Discovery{}
|
2018-07-04 10:51:47 +00:00
|
|
|
if !n.config.NoDiscovery {
|
2018-07-20 06:46:29 +00:00
|
|
|
discoveries = append(discoveries, discovery.NewDiscV5(
|
2018-07-04 10:51:47 +00:00
|
|
|
n.gethNode.Server().PrivateKey,
|
|
|
|
n.config.ListenAddr,
|
2018-07-20 06:46:29 +00:00
|
|
|
parseNodesV5(n.config.ClusterConfig.BootNodes)))
|
|
|
|
}
|
|
|
|
if n.config.Rendezvous {
|
|
|
|
d, err := n.startRendezvous()
|
|
|
|
if err != nil {
|
2018-07-04 10:51:47 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-07-20 06:46:29 +00:00
|
|
|
discoveries = append(discoveries, d)
|
|
|
|
}
|
|
|
|
if len(discoveries) == 0 {
|
|
|
|
return errors.New("wasn't able to register any discovery")
|
|
|
|
} else if len(discoveries) > 1 {
|
|
|
|
n.discovery = discovery.NewMultiplexer(discoveries)
|
|
|
|
} else {
|
|
|
|
n.discovery = discoveries[0]
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
2018-08-17 06:25:55 +00:00
|
|
|
log.Debug(
|
|
|
|
"using discovery",
|
|
|
|
"instance", reflect.TypeOf(n.discovery),
|
|
|
|
"registerTopics", n.config.RegisterTopics,
|
|
|
|
"requireTopics", n.config.RequireTopics,
|
|
|
|
)
|
2018-07-03 11:27:04 +00:00
|
|
|
n.register = peers.NewRegister(n.discovery, n.config.RegisterTopics...)
|
2018-05-15 09:16:25 +00:00
|
|
|
options := peers.NewDefaultOptions()
|
2018-04-10 06:44:09 +00:00
|
|
|
// TODO(dshulyak) consider adding a flag to define this behaviour
|
2018-05-15 09:16:25 +00:00
|
|
|
options.AllowStop = len(n.config.RegisterTopics) == 0
|
2018-07-25 14:48:02 +00:00
|
|
|
options.TrustedMailServers = parseNodesToNodeID(n.config.ClusterConfig.TrustedMailServers)
|
2018-08-20 13:55:43 +00:00
|
|
|
|
2018-05-15 09:16:25 +00:00
|
|
|
n.peerPool = peers.NewPeerPool(
|
2018-07-03 11:27:04 +00:00
|
|
|
n.discovery,
|
2018-05-15 09:16:25 +00:00
|
|
|
n.config.RequireTopics,
|
2018-04-10 06:44:09 +00:00
|
|
|
peers.NewCache(n.db),
|
2018-05-15 09:16:25 +00:00
|
|
|
options,
|
2018-04-10 06:44:09 +00:00
|
|
|
)
|
2018-07-03 11:27:04 +00:00
|
|
|
if err := n.discovery.Start(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := n.register.Start(); err != nil {
|
2018-04-10 06:44:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-08-20 13:55:43 +00:00
|
|
|
return n.peerPool.Start(n.gethNode.Server(), n.rpcClient)
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
// Stop will stop current StatusNode. A stopped node cannot be resumed.
|
|
|
|
func (n *StatusNode) Stop() error {
|
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2018-04-16 12:36:09 +00:00
|
|
|
|
|
|
|
if !n.isRunning() {
|
|
|
|
return ErrNoRunningNode
|
|
|
|
}
|
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
return n.stop()
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
// stop will stop current StatusNode. A stopped node cannot be resumed.
|
|
|
|
func (n *StatusNode) stop() error {
|
2019-01-17 11:02:45 +00:00
|
|
|
if n.isDiscoveryRunning() {
|
2018-07-03 11:27:04 +00:00
|
|
|
if err := n.stopDiscovery(); err != nil {
|
2019-01-17 11:02:45 +00:00
|
|
|
n.log.Error("Error stopping the discovery components", "error", err)
|
2018-07-03 11:27:04 +00:00
|
|
|
}
|
|
|
|
n.register = nil
|
|
|
|
n.peerPool = nil
|
2019-01-17 11:02:45 +00:00
|
|
|
n.discovery = nil
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
2018-04-16 12:36:09 +00:00
|
|
|
|
2021-06-30 11:40:54 +00:00
|
|
|
if err := n.gethNode.Close(); err != nil {
|
2018-02-09 13:37:56 +00:00
|
|
|
return err
|
2017-05-24 14:13:30 +00:00
|
|
|
}
|
2018-04-16 12:36:09 +00:00
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
n.rpcClient = nil
|
2018-04-16 12:36:09 +00:00
|
|
|
// We need to clear `gethNode` because config is passed to `Start()`
|
|
|
|
// and may be completely different. Similarly with `config`.
|
|
|
|
n.gethNode = nil
|
|
|
|
n.config = nil
|
|
|
|
|
2018-05-02 12:14:08 +00:00
|
|
|
if n.db != nil {
|
|
|
|
err := n.db.Close()
|
|
|
|
|
|
|
|
n.db = nil
|
|
|
|
|
2018-04-20 11:26:54 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-07 06:11:09 +00:00
|
|
|
n.rpcFiltersSrvc = nil
|
|
|
|
n.subscriptionsSrvc = nil
|
|
|
|
n.rpcStatsSrvc = nil
|
|
|
|
n.accountsSrvc = nil
|
|
|
|
n.browsersSrvc = nil
|
|
|
|
n.permissionsSrvc = nil
|
|
|
|
n.mailserversSrvc = nil
|
|
|
|
n.appMetricsSrvc = nil
|
|
|
|
n.walletSrvc = nil
|
|
|
|
n.peerSrvc = nil
|
|
|
|
n.localNotificationsSrvc = nil
|
|
|
|
n.personalSrvc = nil
|
|
|
|
n.timeSourceSrvc = nil
|
|
|
|
n.wakuSrvc = nil
|
|
|
|
n.wakuExtSrvc = nil
|
|
|
|
n.wakuV2Srvc = nil
|
|
|
|
n.wakuV2ExtSrvc = nil
|
2021-07-09 13:19:33 +00:00
|
|
|
n.publicMethods = make(map[string]bool)
|
2021-07-07 06:11:09 +00:00
|
|
|
|
2018-02-09 13:37:56 +00:00
|
|
|
return nil
|
2019-01-02 18:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n *StatusNode) isDiscoveryRunning() bool {
|
|
|
|
return n.register != nil || n.peerPool != nil || n.discovery != nil
|
2018-02-09 13:37:56 +00:00
|
|
|
}
|
2017-05-24 14:13:30 +00:00
|
|
|
|
2018-07-03 11:27:04 +00:00
|
|
|
func (n *StatusNode) stopDiscovery() error {
|
2018-04-10 06:44:09 +00:00
|
|
|
n.register.Stop()
|
|
|
|
n.peerPool.Stop()
|
2018-07-03 11:27:04 +00:00
|
|
|
return n.discovery.Stop()
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 13:37:56 +00:00
|
|
|
// ResetChainData removes chain data if node is not running.
|
2018-04-05 09:45:26 +00:00
|
|
|
func (n *StatusNode) ResetChainData(config *params.NodeConfig) error {
|
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2018-04-16 12:36:09 +00:00
|
|
|
|
|
|
|
if n.isRunning() {
|
|
|
|
return ErrNodeRunning
|
|
|
|
}
|
|
|
|
|
2018-02-14 16:32:36 +00:00
|
|
|
chainDataDir := filepath.Join(config.DataDir, config.Name, "lightchaindata")
|
2018-02-09 13:37:56 +00:00
|
|
|
if _, err := os.Stat(chainDataDir); os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err := os.RemoveAll(chainDataDir)
|
|
|
|
if err == nil {
|
2018-04-05 09:45:26 +00:00
|
|
|
n.log.Info("Chain data has been removed", "dir", chainDataDir)
|
2018-02-09 13:37:56 +00:00
|
|
|
}
|
|
|
|
return err
|
2017-05-25 13:14:52 +00:00
|
|
|
}
|
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
// IsRunning confirm that node is running.
|
|
|
|
func (n *StatusNode) IsRunning() bool {
|
|
|
|
n.mu.RLock()
|
|
|
|
defer n.mu.RUnlock()
|
2017-05-25 13:14:52 +00:00
|
|
|
|
2018-04-16 12:36:09 +00:00
|
|
|
return n.isRunning()
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2018-04-16 12:36:09 +00:00
|
|
|
func (n *StatusNode) isRunning() bool {
|
|
|
|
return n.gethNode != nil && n.gethNode.Server() != nil
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// populateStaticPeers connects current node with our publicly available LES/SHH/Swarm cluster
|
2018-04-05 09:45:26 +00:00
|
|
|
func (n *StatusNode) populateStaticPeers() error {
|
2018-09-13 16:31:29 +00:00
|
|
|
if !n.config.ClusterConfig.Enabled {
|
2018-04-05 09:45:26 +00:00
|
|
|
n.log.Info("Static peers are disabled")
|
2017-05-16 12:09:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
for _, enode := range n.config.ClusterConfig.StaticNodes {
|
2018-04-16 12:36:09 +00:00
|
|
|
if err := n.addPeer(enode); err != nil {
|
|
|
|
n.log.Error("Static peer addition failed", "error", err)
|
|
|
|
return err
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
2018-04-05 09:45:26 +00:00
|
|
|
n.log.Info("Static peer added", "enode", enode)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
func (n *StatusNode) removeStaticPeers() error {
|
2018-09-13 16:31:29 +00:00
|
|
|
if !n.config.ClusterConfig.Enabled {
|
2018-04-05 09:45:26 +00:00
|
|
|
n.log.Info("Static peers are disabled")
|
2018-02-19 15:32:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-04-16 12:36:09 +00:00
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
for _, enode := range n.config.ClusterConfig.StaticNodes {
|
2018-04-16 12:36:09 +00:00
|
|
|
if err := n.removePeer(enode); err != nil {
|
|
|
|
n.log.Error("Static peer deletion failed", "error", err)
|
2018-02-19 15:32:58 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-04-05 09:45:26 +00:00
|
|
|
n.log.Info("Static peer deleted", "enode", enode)
|
2018-02-19 15:32:58 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReconnectStaticPeers removes and adds static peers to a server.
|
2018-04-05 09:45:26 +00:00
|
|
|
func (n *StatusNode) ReconnectStaticPeers() error {
|
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
2018-04-16 12:36:09 +00:00
|
|
|
|
|
|
|
if !n.isRunning() {
|
|
|
|
return ErrNoRunningNode
|
|
|
|
}
|
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
if err := n.removeStaticPeers(); err != nil {
|
2018-02-19 15:32:58 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-04-16 12:36:09 +00:00
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
return n.populateStaticPeers()
|
2018-02-19 15:32:58 +00:00
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
// AddPeer adds new static peer node
|
2018-04-05 09:45:26 +00:00
|
|
|
func (n *StatusNode) AddPeer(url string) error {
|
|
|
|
n.mu.RLock()
|
|
|
|
defer n.mu.RUnlock()
|
2018-04-16 12:36:09 +00:00
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
return n.addPeer(url)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// addPeer adds new static peer node
|
2018-04-05 09:45:26 +00:00
|
|
|
func (n *StatusNode) addPeer(url string) error {
|
2018-11-14 07:03:58 +00:00
|
|
|
parsedNode, err := enode.ParseV4(url)
|
2017-05-16 12:09:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-16 12:36:09 +00:00
|
|
|
|
|
|
|
if !n.isRunning() {
|
|
|
|
return ErrNoRunningNode
|
|
|
|
}
|
|
|
|
|
2018-04-09 07:16:43 +00:00
|
|
|
n.gethNode.Server().AddPeer(parsedNode)
|
2018-04-16 12:36:09 +00:00
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
func (n *StatusNode) removePeer(url string) error {
|
2018-11-14 07:03:58 +00:00
|
|
|
parsedNode, err := enode.ParseV4(url)
|
2018-02-19 15:32:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-04-16 12:36:09 +00:00
|
|
|
|
|
|
|
if !n.isRunning() {
|
|
|
|
return ErrNoRunningNode
|
|
|
|
}
|
|
|
|
|
2018-04-09 07:16:43 +00:00
|
|
|
n.gethNode.Server().RemovePeer(parsedNode)
|
2018-04-16 12:36:09 +00:00
|
|
|
|
2018-02-19 15:32:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-25 10:16:59 +00:00
|
|
|
// PeerCount returns the number of connected peers.
|
2018-04-05 09:45:26 +00:00
|
|
|
func (n *StatusNode) PeerCount() int {
|
|
|
|
n.mu.RLock()
|
|
|
|
defer n.mu.RUnlock()
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2018-04-16 12:36:09 +00:00
|
|
|
if !n.isRunning() {
|
|
|
|
return 0
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
2018-04-16 12:36:09 +00:00
|
|
|
|
|
|
|
return n.gethNode.Server().PeerCount()
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 11:40:54 +00:00
|
|
|
func (n *StatusNode) ConnectionChanged(state connection.State) {
|
|
|
|
if n.wakuExtSrvc == nil {
|
|
|
|
return
|
2019-07-17 05:28:37 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 11:40:54 +00:00
|
|
|
n.wakuExtSrvc.ConnectionChanged(state)
|
2019-07-21 05:41:30 +00:00
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
// AccountManager exposes reference to node's accounts manager
|
2018-04-05 09:45:26 +00:00
|
|
|
func (n *StatusNode) AccountManager() (*accounts.Manager, error) {
|
|
|
|
n.mu.RLock()
|
|
|
|
defer n.mu.RUnlock()
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2018-04-16 12:36:09 +00:00
|
|
|
if n.gethNode == nil {
|
|
|
|
return nil, ErrNoGethNode
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
2018-04-16 12:36:09 +00:00
|
|
|
|
|
|
|
return n.gethNode.AccountManager(), nil
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-09-14 20:14:31 +00:00
|
|
|
// RPCClient exposes reference to RPC client connected to the running node.
|
2018-04-05 09:45:26 +00:00
|
|
|
func (n *StatusNode) RPCClient() *rpc.Client {
|
2018-08-20 13:55:43 +00:00
|
|
|
n.mu.RLock()
|
|
|
|
defer n.mu.RUnlock()
|
2018-04-05 09:45:26 +00:00
|
|
|
return n.rpcClient
|
2017-05-28 13:57:30 +00:00
|
|
|
}
|
2017-09-01 18:44:50 +00:00
|
|
|
|
2018-07-16 07:40:40 +00:00
|
|
|
// Discover sets up the discovery for a specific topic.
|
|
|
|
func (n *StatusNode) Discover(topic string, max, min int) (err error) {
|
|
|
|
if n.peerPool == nil {
|
|
|
|
return errors.New("peerPool not running")
|
|
|
|
}
|
|
|
|
return n.peerPool.UpdateTopic(topic, params.Limits{
|
|
|
|
Max: max,
|
|
|
|
Min: min,
|
|
|
|
})
|
|
|
|
}
|
2021-06-30 11:40:54 +00:00
|
|
|
|
|
|
|
func (n *StatusNode) SetAppDB(db *sql.DB) {
|
|
|
|
n.appDB = db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *StatusNode) SetMultiaccountsDB(db *multiaccounts.Database) {
|
|
|
|
n.multiaccountsDB = db
|
|
|
|
}
|