diff --git a/src/main/java/org/semux/consensus/SemuxBft.java b/src/main/java/org/semux/consensus/SemuxBft.java index 460b747bb..c7324981a 100644 --- a/src/main/java/org/semux/consensus/SemuxBft.java +++ b/src/main/java/org/semux/consensus/SemuxBft.java @@ -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 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()); + } } } }