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-03-20 18:35:28 +00:00
|
|
|
|
2017-12-21 10:26:01 +00:00
|
|
|
"github.com/status-im/status-go/geth/mailservice"
|
2017-05-16 12:09:52 +00:00
|
|
|
"github.com/status-im/status-go/geth/params"
|
2017-09-14 20:14:31 +00:00
|
|
|
"github.com/status-im/status-go/geth/rpc"
|
2017-05-16 12:09:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// errors
|
|
|
|
var (
|
2017-05-25 13:14:52 +00:00
|
|
|
ErrNodeExists = errors.New("node is already running")
|
2017-05-16 12:09:52 +00:00
|
|
|
ErrNoRunningNode = errors.New("there is no running node")
|
|
|
|
ErrInvalidNodeManager = errors.New("node manager is not properly initialized")
|
|
|
|
ErrInvalidWhisperService = errors.New("whisper service is unavailable")
|
|
|
|
ErrInvalidLightEthereumService = errors.New("LES service is unavailable")
|
|
|
|
ErrInvalidAccountManager = errors.New("could not retrieve account manager")
|
|
|
|
ErrAccountKeyStoreMissing = errors.New("account key store is not set")
|
2017-09-14 20:14:31 +00:00
|
|
|
ErrRPCClient = errors.New("failed to init RPC client")
|
2017-05-16 12:09:52 +00:00
|
|
|
)
|
|
|
|
|
2018-02-09 13:37:56 +00:00
|
|
|
// RPCClientError reported when rpc client is initialized.
|
|
|
|
type RPCClientError error
|
|
|
|
|
|
|
|
// EthNodeError is reported when node crashed on start up.
|
|
|
|
type EthNodeError error
|
|
|
|
|
2018-03-27 16:30:37 +00:00
|
|
|
// Manager manages Status node (which abstracts contained geth node)
|
2017-10-20 09:06:22 +00:00
|
|
|
// nolint: golint
|
|
|
|
// should be fixed at https://github.com/status-im/status-go/issues/200
|
2018-03-27 16:30:37 +00:00
|
|
|
type Manager struct {
|
2018-02-09 13:37:56 +00:00
|
|
|
mu sync.RWMutex
|
|
|
|
config *params.NodeConfig // Status node configuration
|
|
|
|
node *node.Node // reference to Geth P2P stack/node
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
whisperService *whisper.Whisper // reference to Whisper service
|
|
|
|
lesService *les.LightEthereum // reference to LES service
|
|
|
|
rpcClient *rpc.Client // reference to RPC client
|
2018-03-20 18:35:28 +00:00
|
|
|
log log.Logger
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 16:30:37 +00:00
|
|
|
// NewManager makes new instance of node manager
|
|
|
|
func NewManager() *Manager {
|
|
|
|
return &Manager{
|
|
|
|
log: log.New("package", "status-go/geth/node.Manager"),
|
2018-03-20 18:35:28 +00:00
|
|
|
}
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StartNode start Status node, fails if node is already started
|
2018-03-27 16:30:37 +00:00
|
|
|
func (m *Manager) StartNode(config *params.NodeConfig) error {
|
2018-02-09 13:37:56 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2017-05-16 12:09:52 +00:00
|
|
|
return m.startNode(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
// startNode start Status node, fails if node is already started
|
2018-03-27 16:30:37 +00:00
|
|
|
func (m *Manager) startNode(config *params.NodeConfig) error {
|
2018-02-09 13:37:56 +00:00
|
|
|
if err := m.isNodeAvailable(); err == nil {
|
|
|
|
return ErrNodeExists
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
2017-09-01 18:44:50 +00:00
|
|
|
|
2018-01-19 14:53:16 +00:00
|
|
|
ethNode, err := MakeNode(config)
|
2017-05-16 12:09:52 +00:00
|
|
|
if err != nil {
|
2018-02-09 13:37:56 +00:00
|
|
|
return err
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
2018-02-09 13:37:56 +00:00
|
|
|
m.node = ethNode
|
|
|
|
m.config = config
|
2017-07-13 06:54:10 +00:00
|
|
|
|
2017-12-21 10:26:01 +00:00
|
|
|
// activate MailService required for Offline Inboxing
|
|
|
|
if err := ethNode.Register(func(_ *node.ServiceContext) (node.Service, error) {
|
|
|
|
return mailservice.New(m), nil
|
|
|
|
}); err != nil {
|
2018-02-09 13:37:56 +00:00
|
|
|
return err
|
2017-12-21 10:26:01 +00:00
|
|
|
}
|
|
|
|
|
2018-02-09 13:37:56 +00:00
|
|
|
// start underlying node
|
|
|
|
if err := ethNode.Start(); err != nil {
|
|
|
|
return EthNodeError(err)
|
|
|
|
}
|
|
|
|
// init RPC client for this node
|
|
|
|
localRPCClient, err := m.node.Attach()
|
|
|
|
if err == nil {
|
|
|
|
m.rpcClient, err = rpc.NewClient(localRPCClient, m.config.UpstreamConfig)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2018-03-20 18:35:28 +00:00
|
|
|
m.log.Error("Failed to create an RPC client", "error", err)
|
2018-02-09 13:37:56 +00:00
|
|
|
return RPCClientError(err)
|
|
|
|
}
|
|
|
|
return nil
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StopNode stop Status node. Stopped node cannot be resumed.
|
2018-03-27 16:30:37 +00:00
|
|
|
func (m *Manager) StopNode() error {
|
2018-02-09 13:37:56 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2017-05-16 12:09:52 +00:00
|
|
|
return m.stopNode()
|
|
|
|
}
|
|
|
|
|
|
|
|
// stopNode stop Status node. Stopped node cannot be resumed.
|
2018-03-27 16:30:37 +00:00
|
|
|
func (m *Manager) stopNode() error {
|
2018-02-09 13:37:56 +00:00
|
|
|
if err := m.isNodeAvailable(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-25 13:14:52 +00:00
|
|
|
if err := m.node.Stop(); err != nil {
|
2018-02-09 13:37:56 +00:00
|
|
|
return err
|
2017-05-24 14:13:30 +00:00
|
|
|
}
|
2018-02-09 13:37:56 +00:00
|
|
|
m.node = nil
|
|
|
|
m.config = nil
|
|
|
|
m.lesService = nil
|
|
|
|
m.whisperService = nil
|
|
|
|
m.rpcClient = nil
|
|
|
|
return nil
|
|
|
|
}
|
2017-05-24 14:13:30 +00:00
|
|
|
|
2018-02-09 13:37:56 +00:00
|
|
|
// ResetChainData removes chain data if node is not running.
|
2018-03-27 16:30:37 +00:00
|
|
|
func (m *Manager) ResetChainData(config *params.NodeConfig) error {
|
2018-02-14 16:32:36 +00:00
|
|
|
if m.IsNodeRunning() {
|
|
|
|
return ErrNodeExists
|
2018-02-09 13:37:56 +00:00
|
|
|
}
|
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
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) {
|
|
|
|
// is it really an error, if we want to remove it as next step?
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err := os.RemoveAll(chainDataDir)
|
|
|
|
if err == nil {
|
2018-03-20 18:35:28 +00:00
|
|
|
m.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
|
|
|
}
|
|
|
|
|
|
|
|
// IsNodeRunning confirm that node is running
|
2018-03-27 16:30:37 +00:00
|
|
|
func (m *Manager) IsNodeRunning() bool {
|
2018-02-09 13:37:56 +00:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
2017-05-25 13:14:52 +00:00
|
|
|
|
2017-09-11 14:07:35 +00:00
|
|
|
if err := m.isNodeAvailable(); err != nil {
|
2017-05-25 13:14:52 +00:00
|
|
|
return false
|
2017-05-24 14:13:30 +00:00
|
|
|
}
|
2017-05-25 13:14:52 +00:00
|
|
|
return true
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Node returns underlying Status node
|
2018-03-27 16:30:37 +00:00
|
|
|
func (m *Manager) Node() (*node.Node, error) {
|
2018-02-09 13:37:56 +00:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-09-11 14:07:35 +00:00
|
|
|
if err := m.isNodeAvailable(); err != nil {
|
|
|
|
return nil, err
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
return m.node, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PopulateStaticPeers connects current node with our publicly available LES/SHH/Swarm cluster
|
2018-03-27 16:30:37 +00:00
|
|
|
func (m *Manager) PopulateStaticPeers() error {
|
2018-02-09 13:37:56 +00:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
2017-05-16 12:09:52 +00:00
|
|
|
return m.populateStaticPeers()
|
|
|
|
}
|
|
|
|
|
|
|
|
// populateStaticPeers connects current node with our publicly available LES/SHH/Swarm cluster
|
2018-03-27 16:30:37 +00:00
|
|
|
func (m *Manager) populateStaticPeers() error {
|
2018-02-09 13:37:56 +00:00
|
|
|
if err := m.isNodeAvailable(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-03-20 14:05:21 +00:00
|
|
|
if !m.config.ClusterConfig.Enabled {
|
2018-03-20 18:35:28 +00:00
|
|
|
m.log.Info("Static peers are disabled")
|
2017-05-16 12:09:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-20 14:05:21 +00:00
|
|
|
for _, enode := range m.config.ClusterConfig.StaticNodes {
|
2017-05-16 12:09:52 +00:00
|
|
|
err := m.addPeer(enode)
|
|
|
|
if err != nil {
|
2018-03-20 18:35:28 +00:00
|
|
|
m.log.Warn("Static peer addition failed", "error", err)
|
2017-05-16 12:09:52 +00:00
|
|
|
continue
|
|
|
|
}
|
2018-03-20 18:35:28 +00:00
|
|
|
m.log.Info("Static peer added", "enode", enode)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-27 16:30:37 +00:00
|
|
|
func (m *Manager) removeStaticPeers() error {
|
2018-03-20 14:05:21 +00:00
|
|
|
if !m.config.ClusterConfig.Enabled {
|
2018-03-20 18:35:28 +00:00
|
|
|
m.log.Info("Static peers are disabled")
|
2018-02-19 15:32:58 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
server := m.node.Server()
|
|
|
|
if server == nil {
|
|
|
|
return ErrNoRunningNode
|
|
|
|
}
|
2018-03-20 14:05:21 +00:00
|
|
|
for _, enode := range m.config.ClusterConfig.StaticNodes {
|
2018-02-19 15:32:58 +00:00
|
|
|
err := m.removePeer(enode)
|
|
|
|
if err != nil {
|
2018-03-20 18:35:28 +00:00
|
|
|
m.log.Warn("Static peer deletion failed", "error", err)
|
2018-02-19 15:32:58 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-03-20 18:35:28 +00:00
|
|
|
m.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-03-27 16:30:37 +00:00
|
|
|
func (m *Manager) ReconnectStaticPeers() error {
|
2018-02-19 15:32:58 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
|
|
|
if err := m.removeStaticPeers(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return m.populateStaticPeers()
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
// AddPeer adds new static peer node
|
2018-03-27 16:30:37 +00:00
|
|
|
func (m *Manager) AddPeer(url string) error {
|
2018-02-09 13:37:56 +00:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
2017-09-11 14:07:35 +00:00
|
|
|
if err := m.isNodeAvailable(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-16 12:09:52 +00:00
|
|
|
return m.addPeer(url)
|
|
|
|
}
|
|
|
|
|
|
|
|
// addPeer adds new static peer node
|
2018-03-27 16:30:37 +00:00
|
|
|
func (m *Manager) addPeer(url string) error {
|
2017-05-16 12:09:52 +00:00
|
|
|
// Try to add the url as a static peer and return
|
|
|
|
parsedNode, err := discover.ParseNode(url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-09 13:37:56 +00:00
|
|
|
m.node.Server().AddPeer(parsedNode)
|
2017-05-16 12:09:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-27 16:30:37 +00:00
|
|
|
func (m *Manager) removePeer(url string) error {
|
2018-02-19 15:32:58 +00:00
|
|
|
parsedNode, err := discover.ParseNode(url)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.node.Server().RemovePeer(parsedNode)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-25 10:16:59 +00:00
|
|
|
// PeerCount returns the number of connected peers.
|
2018-03-27 16:30:37 +00:00
|
|
|
func (m *Manager) PeerCount() int {
|
2018-02-09 13:37:56 +00:00
|
|
|
if !m.IsNodeRunning() {
|
2018-01-25 07:26:34 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return m.node.Server().PeerCount()
|
|
|
|
}
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
// NodeConfig exposes reference to running node's configuration
|
2018-03-27 16:30:37 +00:00
|
|
|
func (m *Manager) NodeConfig() (*params.NodeConfig, error) {
|
2018-02-09 13:37:56 +00:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-09-11 14:07:35 +00:00
|
|
|
if err := m.isNodeAvailable(); err != nil {
|
|
|
|
return nil, err
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
return m.config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LightEthereumService exposes reference to LES service running on top of the node
|
2018-03-27 16:30:37 +00:00
|
|
|
func (m *Manager) LightEthereumService() (*les.LightEthereum, error) {
|
2018-02-09 13:37:56 +00:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-09-11 14:07:35 +00:00
|
|
|
if err := m.isNodeAvailable(); err != nil {
|
|
|
|
return nil, err
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
if m.lesService == nil {
|
|
|
|
if err := m.node.Service(&m.lesService); err != nil {
|
2018-03-20 18:35:28 +00:00
|
|
|
m.log.Warn("Cannot obtain LES service", "error", err)
|
2017-05-16 12:09:52 +00:00
|
|
|
return nil, ErrInvalidLightEthereumService
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if m.lesService == nil {
|
|
|
|
return nil, ErrInvalidLightEthereumService
|
|
|
|
}
|
|
|
|
return m.lesService, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// WhisperService exposes reference to Whisper service running on top of the node
|
2018-03-27 16:30:37 +00:00
|
|
|
func (m *Manager) WhisperService() (*whisper.Whisper, error) {
|
2018-02-09 13:37:56 +00:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-09-11 14:07:35 +00:00
|
|
|
if err := m.isNodeAvailable(); err != nil {
|
|
|
|
return nil, err
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
if m.whisperService == nil {
|
|
|
|
if err := m.node.Service(&m.whisperService); err != nil {
|
2018-03-20 18:35:28 +00:00
|
|
|
m.log.Warn("Cannot obtain whisper service", "error", err)
|
2017-05-16 12:09:52 +00:00
|
|
|
return nil, ErrInvalidWhisperService
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if m.whisperService == nil {
|
|
|
|
return nil, ErrInvalidWhisperService
|
|
|
|
}
|
|
|
|
return m.whisperService, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AccountManager exposes reference to node's accounts manager
|
2018-03-27 16:30:37 +00:00
|
|
|
func (m *Manager) AccountManager() (*accounts.Manager, error) {
|
2018-02-09 13:37:56 +00:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-09-11 14:07:35 +00:00
|
|
|
if err := m.isNodeAvailable(); err != nil {
|
|
|
|
return nil, err
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
accountManager := m.node.AccountManager()
|
|
|
|
if accountManager == nil {
|
|
|
|
return nil, ErrInvalidAccountManager
|
|
|
|
}
|
|
|
|
return accountManager, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AccountKeyStore exposes reference to accounts key store
|
2018-03-27 16:30:37 +00:00
|
|
|
func (m *Manager) AccountKeyStore() (*keystore.KeyStore, error) {
|
2018-02-09 13:37:56 +00:00
|
|
|
m.mu.RLock()
|
|
|
|
defer m.mu.RUnlock()
|
2017-05-16 12:09:52 +00:00
|
|
|
|
2017-09-11 14:07:35 +00:00
|
|
|
if err := m.isNodeAvailable(); err != nil {
|
|
|
|
return nil, err
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
accountManager := m.node.AccountManager()
|
|
|
|
if accountManager == nil {
|
|
|
|
return nil, ErrInvalidAccountManager
|
|
|
|
}
|
|
|
|
|
|
|
|
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-03-27 16:30:37 +00:00
|
|
|
func (m *Manager) RPCClient() *rpc.Client {
|
2018-02-09 13:37:56 +00:00
|
|
|
m.mu.Lock()
|
|
|
|
defer m.mu.Unlock()
|
2017-09-14 20:14:31 +00:00
|
|
|
return m.rpcClient
|
2017-05-28 13:57:30 +00:00
|
|
|
}
|
2017-09-01 18:44:50 +00:00
|
|
|
|
2017-09-11 14:07:35 +00:00
|
|
|
// isNodeAvailable check if we have a node running and make sure is fully started
|
2018-03-27 16:30:37 +00:00
|
|
|
func (m *Manager) isNodeAvailable() error {
|
2018-02-09 13:37:56 +00:00
|
|
|
if m.node == nil || m.node.Server() == nil {
|
2017-09-11 14:07:35 +00:00
|
|
|
return ErrNoRunningNode
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-07 10:48:03 +00:00
|
|
|
|
|
|
|
// tickerResolution is the delta to check blockchain sync progress.
|
|
|
|
const tickerResolution = time.Second
|
|
|
|
|
|
|
|
// EnsureSync waits until blockchain synchronization
|
|
|
|
// is complete and returns.
|
2018-03-27 16:30:37 +00:00
|
|
|
func (m *Manager) 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.
|
|
|
|
if m.config.NetworkID == params.StatusChainNetworkID {
|
|
|
|
return nil
|
|
|
|
}
|
2018-02-08 08:51:53 +00:00
|
|
|
|
2018-02-07 10:48:03 +00:00
|
|
|
return m.ensureSync(ctx)
|
|
|
|
}
|
|
|
|
|
2018-03-27 16:30:37 +00:00
|
|
|
func (m *Manager) ensureSync(ctx context.Context) error {
|
2018-02-08 08:51:53 +00:00
|
|
|
les, err := m.LightEthereumService()
|
|
|
|
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-02-08 08:51:53 +00:00
|
|
|
if m.PeerCount() > 0 && progress.CurrentBlock >= progress.HighestBlock {
|
2018-03-20 18:35:28 +00:00
|
|
|
m.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:
|
|
|
|
if m.PeerCount() == 0 {
|
2018-03-20 18:35:28 +00:00
|
|
|
m.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-03-20 18:35:28 +00:00
|
|
|
m.log.Debug("Synchronization is in progress")
|
2018-02-07 10:48:03 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
progress = downloader.Progress()
|
|
|
|
if progress.CurrentBlock >= progress.HighestBlock {
|
2018-03-20 18:35:28 +00:00
|
|
|
m.log.Info("Synchronization completed", "current block", progress.CurrentBlock, "highest block", progress.HighestBlock)
|
2018-02-07 10:48:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-03-20 18:35:28 +00:00
|
|
|
m.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-03-20 18:35:28 +00:00
|
|
|
m.log.Warn("Synchronization is not finished", "current", progress.CurrentBlock, "highest", progress.HighestBlock)
|
2018-02-07 10:48:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|