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"
|
|
|
|
"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"
|
2017-08-10 13:35:58 +00:00
|
|
|
"github.com/status-im/status-go/geth/log"
|
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
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
// NodeManager 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
|
2017-05-16 12:09:52 +00:00
|
|
|
type NodeManager 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNodeManager makes new instance of node manager
|
|
|
|
func NewNodeManager() *NodeManager {
|
2018-01-23 05:16:13 +00:00
|
|
|
return &NodeManager{}
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StartNode start Status node, fails if node is already started
|
2018-02-09 13:37:56 +00:00
|
|
|
func (m *NodeManager) StartNode(config *params.NodeConfig) error {
|
|
|
|
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-02-09 13:37:56 +00:00
|
|
|
func (m *NodeManager) startNode(config *params.NodeConfig) error {
|
|
|
|
if err := m.isNodeAvailable(); err == nil {
|
|
|
|
return ErrNodeExists
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
2017-09-01 18:44:50 +00:00
|
|
|
m.initLog(config)
|
|
|
|
|
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 {
|
|
|
|
log.Error("Failed to create an RPC client", "error", err)
|
|
|
|
return RPCClientError(err)
|
|
|
|
}
|
|
|
|
// populate static peers exits when node stopped
|
2017-05-16 12:09:52 +00:00
|
|
|
go func() {
|
2018-02-09 13:37:56 +00:00
|
|
|
if err := m.PopulateStaticPeers(); err != nil {
|
|
|
|
log.Error("Static peers population", "error", err)
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
2017-05-25 13:14:52 +00:00
|
|
|
}()
|
2018-02-09 13:37:56 +00:00
|
|
|
return nil
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// StopNode stop Status node. Stopped node cannot be resumed.
|
2018-02-09 13:37:56 +00:00
|
|
|
func (m *NodeManager) StopNode() error {
|
|
|
|
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-02-09 13:37:56 +00:00
|
|
|
func (m *NodeManager) stopNode() error {
|
|
|
|
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-02-14 16:32:36 +00:00
|
|
|
func (m *NodeManager) ResetChainData(config *params.NodeConfig) error {
|
|
|
|
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 {
|
|
|
|
log.Info("Chain data has been removed", "dir", chainDataDir)
|
|
|
|
}
|
|
|
|
return err
|
2017-05-25 13:14:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsNodeRunning confirm that node is running
|
|
|
|
func (m *NodeManager) 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
|
|
|
|
func (m *NodeManager) 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
|
|
|
|
func (m *NodeManager) 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
|
|
|
|
func (m *NodeManager) populateStaticPeers() error {
|
2018-02-09 13:37:56 +00:00
|
|
|
if err := m.isNodeAvailable(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-16 12:09:52 +00:00
|
|
|
if !m.config.BootClusterConfig.Enabled {
|
|
|
|
log.Info("Boot cluster is disabled")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-04 16:14:17 +00:00
|
|
|
for _, enode := range m.config.BootClusterConfig.BootNodes {
|
2017-05-16 12:09:52 +00:00
|
|
|
err := m.addPeer(enode)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("Boot node addition failed", "error", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
log.Info("Boot node added", "enode", enode)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-02-19 15:32:58 +00:00
|
|
|
func (m *NodeManager) removeStaticPeers() error {
|
|
|
|
if !m.config.BootClusterConfig.Enabled {
|
|
|
|
log.Info("Boot cluster is disabled")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
server := m.node.Server()
|
|
|
|
if server == nil {
|
|
|
|
return ErrNoRunningNode
|
|
|
|
}
|
|
|
|
for _, enode := range m.config.BootClusterConfig.BootNodes {
|
|
|
|
err := m.removePeer(enode)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn("Boot node deletion failed", "error", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
log.Info("Boot node deleted", "enode", enode)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReconnectStaticPeers removes and adds static peers to a server.
|
|
|
|
func (m *NodeManager) ReconnectStaticPeers() error {
|
|
|
|
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
|
|
|
|
func (m *NodeManager) 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
|
|
|
|
func (m *NodeManager) addPeer(url string) error {
|
|
|
|
// 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-02-19 15:32:58 +00:00
|
|
|
func (m *NodeManager) removePeer(url string) error {
|
|
|
|
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-01-25 07:26:34 +00:00
|
|
|
func (m *NodeManager) 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
|
|
|
|
func (m *NodeManager) 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
|
|
|
|
func (m *NodeManager) 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 {
|
|
|
|
log.Warn("Cannot obtain LES service", "error", err)
|
|
|
|
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
|
|
|
|
func (m *NodeManager) 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 {
|
|
|
|
log.Warn("Cannot obtain whisper service", "error", err)
|
|
|
|
return nil, ErrInvalidWhisperService
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if m.whisperService == nil {
|
|
|
|
return nil, ErrInvalidWhisperService
|
|
|
|
}
|
|
|
|
return m.whisperService, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AccountManager exposes reference to node's accounts manager
|
|
|
|
func (m *NodeManager) 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
|
|
|
|
func (m *NodeManager) 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.
|
|
|
|
func (m *NodeManager) 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
|
|
|
|
|
|
|
// initLog initializes global logger parameters based on
|
|
|
|
// provided node configurations.
|
|
|
|
func (m *NodeManager) initLog(config *params.NodeConfig) {
|
|
|
|
log.SetLevel(config.LogLevel)
|
|
|
|
|
|
|
|
if config.LogFile != "" {
|
|
|
|
err := log.SetLogFile(config.LogFile)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Failed to open log file, using stdout")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-11 14:07:35 +00:00
|
|
|
|
|
|
|
// isNodeAvailable check if we have a node running and make sure is fully started
|
|
|
|
func (m *NodeManager) 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.
|
|
|
|
func (m *NodeManager) EnsureSync(ctx context.Context) error {
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *NodeManager) 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 {
|
|
|
|
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 {
|
|
|
|
log.Debug("No established connections with any peers, continue waiting for a sync")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if downloader.Synchronising() {
|
|
|
|
log.Debug("Synchronization is in progress")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
progress = downloader.Progress()
|
|
|
|
if progress.CurrentBlock >= progress.HighestBlock {
|
2018-02-08 08:51:53 +00:00
|
|
|
log.Info("Synchronization completed", "current block", progress.CurrentBlock, "highest block", progress.HighestBlock)
|
2018-02-07 10:48:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-02-08 08:51:53 +00:00
|
|
|
log.Debug("Synchronization is not finished", "current", progress.CurrentBlock, "highest", progress.HighestBlock)
|
|
|
|
case <-progressTicker.C:
|
|
|
|
progress = downloader.Progress()
|
|
|
|
log.Warn("Synchronization is not finished", "current", progress.CurrentBlock, "highest", progress.HighestBlock)
|
2018-02-07 10:48:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|