2017-05-16 12:09:52 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
2018-02-07 10:48:03 +00:00
|
|
|
"context"
|
2017-05-16 12:09:52 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
2018-02-07 10:48:03 +00:00
|
|
|
"time"
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/accounts"
|
|
|
|
"github.com/ethereum/go-ethereum/accounts/keystore"
|
|
|
|
"github.com/ethereum/go-ethereum/les"
|
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"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
2018-03-02 09:25:30 +00:00
|
|
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
|
2018-04-10 06:44:09 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
2018-03-20 18:35:28 +00:00
|
|
|
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/db"
|
|
|
|
"github.com/status-im/status-go/params"
|
|
|
|
"github.com/status-im/status-go/peers"
|
|
|
|
"github.com/status-im/status-go/rpc"
|
2018-05-03 10:36:56 +00:00
|
|
|
"github.com/status-im/status-go/services/status"
|
2017-05-16 12:09:52 +00:00
|
|
|
)
|
|
|
|
|
2018-04-16 12:36:09 +00:00
|
|
|
// tickerResolution is the delta to check blockchain sync progress.
|
|
|
|
const tickerResolution = time.Second
|
|
|
|
|
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")
|
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
|
|
|
|
|
2018-04-16 08:01:37 +00:00
|
|
|
config *params.NodeConfig // Status node configuration
|
|
|
|
gethNode *node.Node // reference to Geth P2P stack/node
|
|
|
|
rpcClient *rpc.Client // reference to public RPC client
|
|
|
|
rpcPrivateClient *rpc.Client // reference to private RPC client (can call private APIs)
|
2018-04-09 07:16:43 +00:00
|
|
|
|
2018-04-10 06:44:09 +00:00
|
|
|
register *peers.Register
|
|
|
|
peerPool *peers.PeerPool
|
2018-04-12 16:17:10 +00:00
|
|
|
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
|
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{
|
2018-06-08 11:29:50 +00:00
|
|
|
log: log.New("package", "status-go/node.StatusNode"),
|
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-05-02 12:14:08 +00:00
|
|
|
func (n *StatusNode) startWithDB(config *params.NodeConfig, db *leveldb.DB, services []node.ServiceConstructor) error {
|
|
|
|
if err := n.createNode(config, db); err != nil {
|
2018-02-09 13:37:56 +00:00
|
|
|
return err
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
2018-04-05 09:45:26 +00:00
|
|
|
n.config = config
|
2017-07-13 06:54:10 +00:00
|
|
|
|
2018-04-16 12:36:09 +00:00
|
|
|
if err := n.start(services); err != nil {
|
|
|
|
return err
|
2018-02-09 13:37:56 +00:00
|
|
|
}
|
2018-04-16 08:01:37 +00:00
|
|
|
|
|
|
|
if err := n.setupRPCClient(); err != nil {
|
2018-04-16 12:36:09 +00:00
|
|
|
return err
|
2018-02-09 13:37:56 +00:00
|
|
|
}
|
2018-04-16 08:01:37 +00:00
|
|
|
|
2018-05-10 11:45:51 +00:00
|
|
|
if n.config.NoDiscovery {
|
|
|
|
return nil
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
2018-05-10 11:45:51 +00:00
|
|
|
return n.startPeerPool()
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2018-05-02 12:14:08 +00:00
|
|
|
// Start starts current StatusNode, will fail if it's already started.
|
|
|
|
func (n *StatusNode) Start(config *params.NodeConfig, services ...node.ServiceConstructor) error {
|
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
|
|
|
|
|
|
|
if n.isRunning() {
|
|
|
|
return ErrNodeRunning
|
|
|
|
}
|
2018-04-20 11:26:54 +00:00
|
|
|
|
2018-05-02 12:14:08 +00:00
|
|
|
db, err := db.Create(config.DataDir, params.StatusDatabase)
|
|
|
|
if err != nil {
|
|
|
|
return 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
|
|
|
|
|
2018-05-02 12:14:08 +00:00
|
|
|
err = n.startWithDB(config, db, services)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-05-02 12:14:08 +00:00
|
|
|
func (n *StatusNode) createNode(config *params.NodeConfig, db *leveldb.DB) (err error) {
|
2018-05-11 07:12:50 +00:00
|
|
|
n.gethNode, err = MakeNode(config, db)
|
2018-05-02 12:14:08 +00:00
|
|
|
return err
|
2018-04-16 12:36:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// start starts current StatusNode, will fail if it's already started.
|
|
|
|
func (n *StatusNode) start(services []node.ServiceConstructor) error {
|
|
|
|
for _, service := range services {
|
|
|
|
if err := n.gethNode.Register(service); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return n.gethNode.Start()
|
|
|
|
}
|
|
|
|
|
2018-04-16 08:01:37 +00:00
|
|
|
func (n *StatusNode) setupRPCClient() (err error) {
|
|
|
|
// setup public RPC client
|
|
|
|
gethNodeClient, err := n.gethNode.AttachPublic()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n.rpcClient, err = rpc.NewClient(gethNodeClient, n.config.UpstreamConfig)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup private RPC client
|
|
|
|
gethNodePrivateClient, err := n.gethNode.Attach()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n.rpcPrivateClient, err = rpc.NewClient(gethNodePrivateClient, n.config.UpstreamConfig)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-10 06:44:09 +00:00
|
|
|
func (n *StatusNode) startPeerPool() error {
|
|
|
|
n.register = peers.NewRegister(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
|
|
|
|
n.peerPool = peers.NewPeerPool(
|
|
|
|
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
|
|
|
)
|
|
|
|
if err := n.register.Start(n.gethNode.Server()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return n.peerPool.Start(n.gethNode.Server())
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-04-16 12:36:09 +00:00
|
|
|
if err := n.stopPeerPool(); err != nil {
|
|
|
|
n.log.Error("Error stopping the PeerPool", "error", err)
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
2018-04-16 12:36:09 +00:00
|
|
|
n.register = nil
|
|
|
|
n.peerPool = nil
|
|
|
|
|
2018-04-09 07:16:43 +00:00
|
|
|
if err := n.gethNode.Stop(); 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 08:01:37 +00:00
|
|
|
n.rpcPrivateClient = 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
|
|
|
|
}
|
|
|
|
|
2018-02-09 13:37:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-05-24 14:13:30 +00:00
|
|
|
|
2018-04-16 12:36:09 +00:00
|
|
|
func (n *StatusNode) stopPeerPool() error {
|
2018-05-02 12:14:08 +00:00
|
|
|
if n.config == nil || n.config.NoDiscovery {
|
2018-04-16 12:36:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-04-10 06:44:09 +00:00
|
|
|
n.register.Stop()
|
|
|
|
n.peerPool.Stop()
|
2018-04-20 11:26:54 +00:00
|
|
|
return nil
|
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-04-16 12:36:09 +00:00
|
|
|
if n.config.ClusterConfig == nil || !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-04-16 12:36:09 +00:00
|
|
|
if n.config.ClusterConfig == nil || !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 {
|
2017-05-16 12:09:52 +00:00
|
|
|
parsedNode, err := discover.ParseNode(url)
|
|
|
|
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-02-19 15:32:58 +00:00
|
|
|
parsedNode, err := discover.ParseNode(url)
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-04-09 07:16:43 +00:00
|
|
|
// gethService is a wrapper for gethNode.Service which retrieves a currently
|
|
|
|
// running service registered of a specific type.
|
2018-04-16 12:36:09 +00:00
|
|
|
func (n *StatusNode) gethService(serviceInstance interface{}) error {
|
|
|
|
if !n.isRunning() {
|
|
|
|
return ErrNoRunningNode
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
2018-04-16 12:36:09 +00:00
|
|
|
|
|
|
|
if err := n.gethNode.Service(serviceInstance); err != nil {
|
2018-04-20 15:39:53 +00:00
|
|
|
return err
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
2018-04-09 07:16:43 +00:00
|
|
|
|
|
|
|
return nil
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2018-04-09 07:16:43 +00:00
|
|
|
// LightEthereumService exposes reference to LES service running on top of the node
|
|
|
|
func (n *StatusNode) LightEthereumService() (l *les.LightEthereum, err error) {
|
2018-04-23 13:35:48 +00:00
|
|
|
n.mu.RLock()
|
|
|
|
defer n.mu.RUnlock()
|
|
|
|
|
2018-04-20 15:39:53 +00:00
|
|
|
err = n.gethService(&l)
|
|
|
|
if err == node.ErrServiceUnknown {
|
|
|
|
err = ErrServiceUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2018-04-09 07:16:43 +00:00
|
|
|
}
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2018-05-03 10:36:56 +00:00
|
|
|
// StatusService exposes reference to status service running on top of the node
|
|
|
|
func (n *StatusNode) StatusService() (st *status.Service, err error) {
|
|
|
|
n.mu.RLock()
|
|
|
|
defer n.mu.RUnlock()
|
|
|
|
|
|
|
|
err = n.gethService(&st)
|
|
|
|
if err == node.ErrServiceUnknown {
|
|
|
|
err = ErrServiceUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-04-09 07:16:43 +00:00
|
|
|
// WhisperService exposes reference to Whisper service running on top of the node
|
|
|
|
func (n *StatusNode) WhisperService() (w *whisper.Whisper, err error) {
|
2018-04-23 13:35:48 +00:00
|
|
|
n.mu.RLock()
|
|
|
|
defer n.mu.RUnlock()
|
|
|
|
|
2018-04-20 15:39:53 +00:00
|
|
|
err = n.gethService(&w)
|
|
|
|
if err == node.ErrServiceUnknown {
|
|
|
|
err = ErrServiceUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// AccountKeyStore exposes reference to accounts key store
|
2018-04-05 09:45:26 +00:00
|
|
|
func (n *StatusNode) AccountKeyStore() (*keystore.KeyStore, 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
|
|
|
accountManager := n.gethNode.AccountManager()
|
2017-05-16 12:09:52 +00:00
|
|
|
backends := accountManager.Backends(keystore.KeyStoreType)
|
|
|
|
if len(backends) == 0 {
|
|
|
|
return nil, ErrAccountKeyStoreMissing
|
|
|
|
}
|
|
|
|
|
|
|
|
keyStore, ok := backends[0].(*keystore.KeyStore)
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrAccountKeyStoreMissing
|
|
|
|
}
|
|
|
|
|
|
|
|
return keyStore, nil
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
|
|
|
return n.rpcClient
|
2017-05-28 13:57:30 +00:00
|
|
|
}
|
2017-09-01 18:44:50 +00:00
|
|
|
|
2018-04-16 08:01:37 +00:00
|
|
|
// RPCPrivateClient exposes reference to RPC client connected to the running node
|
|
|
|
// that can call both public and private APIs.
|
|
|
|
func (n *StatusNode) RPCPrivateClient() *rpc.Client {
|
|
|
|
n.mu.Lock()
|
|
|
|
defer n.mu.Unlock()
|
|
|
|
return n.rpcPrivateClient
|
|
|
|
}
|
|
|
|
|
2018-02-07 10:48:03 +00:00
|
|
|
// EnsureSync waits until blockchain synchronization
|
|
|
|
// is complete and returns.
|
2018-04-05 09:45:26 +00:00
|
|
|
func (n *StatusNode) EnsureSync(ctx context.Context) error {
|
2018-02-07 10:48:03 +00:00
|
|
|
// Don't wait for any blockchain sync for the
|
|
|
|
// local private chain as blocks are never mined.
|
2018-04-16 12:36:09 +00:00
|
|
|
if n.config.NetworkID == 0 || n.config.NetworkID == params.StatusChainNetworkID {
|
2018-02-07 10:48:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-02-08 08:51:53 +00:00
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
return n.ensureSync(ctx)
|
2018-02-07 10:48:03 +00:00
|
|
|
}
|
|
|
|
|
2018-04-05 09:45:26 +00:00
|
|
|
func (n *StatusNode) ensureSync(ctx context.Context) error {
|
|
|
|
les, err := n.LightEthereumService()
|
2018-02-08 08:51:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to get LES service: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
downloader := les.Downloader()
|
2018-02-07 10:48:03 +00:00
|
|
|
if downloader == nil {
|
|
|
|
return errors.New("LightEthereumService downloader is nil")
|
|
|
|
}
|
2018-02-08 08:51:53 +00:00
|
|
|
|
2018-02-07 10:48:03 +00:00
|
|
|
progress := downloader.Progress()
|
2018-04-05 09:45:26 +00:00
|
|
|
if n.PeerCount() > 0 && progress.CurrentBlock >= progress.HighestBlock {
|
|
|
|
n.log.Debug("Synchronization completed", "current block", progress.CurrentBlock, "highest block", progress.HighestBlock)
|
2018-02-07 10:48:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ticker := time.NewTicker(tickerResolution)
|
|
|
|
defer ticker.Stop()
|
2018-02-08 08:51:53 +00:00
|
|
|
|
|
|
|
progressTicker := time.NewTicker(time.Minute)
|
|
|
|
defer progressTicker.Stop()
|
|
|
|
|
2018-02-07 10:48:03 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return errors.New("timeout during node synchronization")
|
|
|
|
case <-ticker.C:
|
2018-04-05 09:45:26 +00:00
|
|
|
if n.PeerCount() == 0 {
|
|
|
|
n.log.Debug("No established connections with any peers, continue waiting for a sync")
|
2018-02-07 10:48:03 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
if downloader.Synchronising() {
|
2018-04-05 09:45:26 +00:00
|
|
|
n.log.Debug("Synchronization is in progress")
|
2018-02-07 10:48:03 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
progress = downloader.Progress()
|
|
|
|
if progress.CurrentBlock >= progress.HighestBlock {
|
2018-04-05 09:45:26 +00:00
|
|
|
n.log.Info("Synchronization completed", "current block", progress.CurrentBlock, "highest block", progress.HighestBlock)
|
2018-02-07 10:48:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-04-05 09:45:26 +00:00
|
|
|
n.log.Debug("Synchronization is not finished", "current", progress.CurrentBlock, "highest", progress.HighestBlock)
|
2018-02-08 08:51:53 +00:00
|
|
|
case <-progressTicker.C:
|
|
|
|
progress = downloader.Progress()
|
2018-04-05 09:45:26 +00:00
|
|
|
n.log.Warn("Synchronization is not finished", "current", progress.CurrentBlock, "highest", progress.HighestBlock)
|
2018-02-07 10:48:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|