Add a workaround for waiting till node is synced with network (#565)

This commit is contained in:
Dmitry Shulyak 2018-01-25 09:26:34 +02:00 committed by Adam Babik
parent 755a646bd4
commit 889eeca31d
5 changed files with 43 additions and 10 deletions

View File

@ -181,7 +181,7 @@ func (s *RPCTestSuite) TestCallContextResult() {
defer cancel()
var balance hexutil.Big
err := client.CallContext(ctx, &balance, "eth_getBalance", "0xbF164ca341326a03b547c05B343b2E21eFAe24b9", "latest")
err := client.CallContext(ctx, &balance, "eth_getBalance", TestConfig.Account1.Address, "latest")
s.NoError(err)
s.True(balance.ToInt().Cmp(big.NewInt(0)) > 0, "balance should be higher than 0")
}

View File

@ -75,6 +75,9 @@ type NodeManager interface {
// AddPeer adds URL of static peer
AddPeer(url string) error
// PeerCount returns number of connected peers
PeerCount() int
// LightEthereumService exposes reference to LES service running on top of the node
LightEthereumService() (*les.LightEthereum, error)

View File

@ -156,6 +156,18 @@ func (mr *MockNodeManagerMockRecorder) AddPeer(url interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "AddPeer", reflect.TypeOf((*MockNodeManager)(nil).AddPeer), url)
}
// PeerCount mocks base method
func (m *MockNodeManager) PeerCount() int {
ret := m.ctrl.Call(m, "PeerCount")
ret0, _ := ret[0].(int)
return ret0
}
// PeerCount indicates an expected call of PeerCount
func (mr *MockNodeManagerMockRecorder) PeerCount() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PeerCount", reflect.TypeOf((*MockNodeManager)(nil).PeerCount))
}
// LightEthereumService mocks base method
func (m *MockNodeManager) LightEthereumService() (*les.LightEthereum, error) {
ret := m.ctrl.Call(m, "LightEthereumService")

View File

@ -295,6 +295,13 @@ func (m *NodeManager) addPeer(url string) error {
return nil
}
func (m *NodeManager) PeerCount() int {
if m.node == nil || m.node.Server() == nil {
return 0
}
return m.node.Server().PeerCount()
}
// ResetChainData remove chain data from data directory.
// Node is stopped, and new node is started, with clean data directory.
func (m *NodeManager) ResetChainData() (<-chan struct{}, error) {

View File

@ -12,6 +12,7 @@ import (
"time"
"github.com/status-im/status-go/geth/common"
"github.com/status-im/status-go/geth/log"
"github.com/status-im/status-go/geth/params"
_ "github.com/stretchr/testify/suite" // required to register testify flags
@ -113,22 +114,32 @@ func EnsureNodeSync(nodeManager common.NodeManager) {
defer timeout.Stop()
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
for {
select {
case <-timeout.C:
panic("timeout during node synchronization")
case <-ticker.C:
downloader := les.Downloader()
if downloader != nil {
isSyncing := downloader.Synchronising()
if downloader == nil {
continue
}
if nodeManager.PeerCount() == 0 {
log.Debug("No establishished connections with a peers, continue waiting for a sync")
continue
}
if downloader.Synchronising() {
log.Debug("synchronization is in progress")
continue
}
progress := downloader.Progress()
if !isSyncing && progress.HighestBlock > 0 && progress.CurrentBlock >= progress.HighestBlock {
if progress.CurrentBlock >= progress.HighestBlock {
return
}
}
log.Debug(
fmt.Sprintf("synchronization is not finished yet: current block %d < highest block %d",
progress.CurrentBlock, progress.HighestBlock),
)
}
}
}