Filter out peers of divergent or significantly inferior chains when syncing.

This commit is contained in:
CalDescent 2022-12-14 16:39:43 +00:00
parent cdeb2052b0
commit 1dc7f056f9
2 changed files with 16 additions and 0 deletions

View File

@ -752,6 +752,19 @@ public class Controller extends Thread {
return peerChainTipData == null || peerChainTipData.getSignature() == null || inferiorChainTips.contains(ByteArray.wrap(peerChainTipData.getSignature()));
};
/**
* If a peer has a recent block timestamp, but its height is more than 25 blocks behind ours,
* we can assume it has a significantly inferior chain, and is most likely too divergent.
* Early filtering of these peers prevents a lot of very expensive chain weight comparisons.
*/
public static final Predicate<Peer> hasInferiorChain = peer -> {
final Long minLatestBlockTimestamp = getMinimumLatestBlockTimestamp();
final int ourHeight = Controller.getInstance().getChainHeight();
final BlockSummaryData peerChainTipData = peer.getChainTipData();
boolean peerUpToDate = peerChainTipData != null && peerChainTipData.getTimestamp() != null && peerChainTipData.getTimestamp() >= minLatestBlockTimestamp;
return peerUpToDate && ourHeight - peerChainTipData.getHeight() > 25;
};
public static final Predicate<Peer> hasOldVersion = peer -> {
final String minPeerVersion = Settings.getInstance().getMinPeerVersion();
return peer.isAtLeastVersion(minPeerVersion) == false;

View File

@ -247,6 +247,9 @@ public class Synchronizer extends Thread {
// Disregard peers that are on the same block as last sync attempt and we didn't like their chain
peers.removeIf(Controller.hasInferiorChainTip);
// Disregard peers that are on a very inferior chain, based on their heights and timestamps
peers.removeIf(Controller.hasInferiorChain);
// Disregard peers that have a block with an invalid signer
peers.removeIf(Controller.hasInvalidSigner);