Skip to content

Commit

Permalink
Merge pull request #130 from orogvany/no_node_sync
Browse files Browse the repository at this point in the history
Core: if original validators are unable to connect, do not deny sync
  • Loading branch information
orogvany authored Mar 3, 2019
2 parents 69a0c58 + a9f483a commit 6234f71
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/main/java/org/semux/consensus/SemuxBft.java
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,20 @@ protected void onNewHeight(long newHeight) {

if (target.isPresent() && target.getAsLong() > height) {
sync(target.getAsLong());
} else if (activeValidators.isEmpty()) {
logger.warn("Unable to connect to active validators. Syncing from peers.");
// If the original validators are missing (a fresh sync, or no direct connection
// to validators)
// we still should be able to sync just based on peers.
List<Channel> activePeers = channelMgr.getActiveChannels();
OptionalLong activePeersTarget = activePeers.stream()
.mapToLong(c -> c.getRemotePeer().getLatestBlockNumber() + 1)
.sorted()
.limit((int) Math.floor(activePeers.size() * 2.0 / 3.0))
.max();
if (activePeersTarget.isPresent() && activePeersTarget.getAsLong() > height) {
sync(activePeersTarget.getAsLong());
}
}
}
}
Expand Down

0 comments on commit 6234f71

Please sign in to comment.