Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Jan 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion e2e/rpc/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
3 changes: 3 additions & 0 deletions geth/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
12 changes: 12 additions & 0 deletions geth/common/types_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions geth/node/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,13 @@ func (m *NodeManager) addPeer(url string) error {
return nil
}

func (m *NodeManager) PeerCount() int {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m.node and m.node.Server() may be nil. In such cases, I would return 0.

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) {
Expand Down
29 changes: 20 additions & 9 deletions testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
progress := downloader.Progress()

if !isSyncing && progress.HighestBlock > 0 && progress.CurrentBlock >= progress.HighestBlock {
return
}
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 progress.CurrentBlock >= progress.HighestBlock {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend running statusd -les -http and checking how the sync progress looks like using https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_syncing. As far as I remember, with LES it's a bit strange and that's why progress.HighestBlock > 0 was added.

Maybe these checks are good, though but it would be great to confirm.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It uses the same source of info as we are https://github.com/ethereum/go-ethereum/blob/master/internal/ethapi/api.go#L79-L94, so I just printed the progress:

INFO [01-23|16:38:58] Imported new block headers               count=384 elapsed=112.830ms number=1226112 hash=f197f6…495d1f ignored=0
INFO [01-23|16:38:58] Imported new block headers               count=192 elapsed=57.979ms  number=1226304 hash=ce5203…750e53 ignored=0
INFO [01-23|16:38:58] synchronization is not finished yet: current block 1226304 < highest block 1643240 geth=StatusIM
INFO [01-23|16:38:58] Imported new block headers               count=576 elapsed=165.479ms number=1226880 hash=60bd3c…9ba1e4 ignored=0
INFO [01-23|16:38:58] Imported new block headers               count=192 elapsed=55.092ms  number=1227072 hash=fcbc33…ef23af ignored=0
INFO [01-23|16:38:59] Imported new block headers               count=960 elapsed=283.560ms number=1228032 hash=cf13d1…68d1ba ignored=0
INFO [01-23|16:38:59] Imported new block headers               count=384 elapsed=116.356ms number=1228416 hash=db9630…6e83b3 ignored=0
INFO [01-23|16:38:59] Imported new block headers               count=768 elapsed=212.664ms number=1229184 hash=87f2da…b5ad22 ignored=0
INFO [01-23|16:38:59] synchronization is not finished yet: current block 1229184 < highest block 1643240 geth=StatusIM
INFO [01-23|16:38:59] Imported new block headers               count=768 elapsed=215.851ms number=1229952 hash=27dfbb…2ea2b3 ignored=0

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good. Let's stay with this then.

return
}
log.Debug(
fmt.Sprintf("synchronization is not finished yet: current block %d < highest block %d",
progress.CurrentBlock, progress.HighestBlock),
)

}
}
}
Expand Down