From 66711c2e9db5afa02a28b41d72f29a5030f83010 Mon Sep 17 00:00:00 2001 From: CalDescent Date: Sun, 16 May 2021 08:45:23 +0100 Subject: [PATCH] Require a complete sync in syncToPeerChain() We have gone backwards and forwards on this one a lot recently, but now that stability has returned, it is best to tighten this up. Previously it was loosened to help reduce network load, but that is no longer a problem. With this stricter approach, it should prevent a node ending up in an incomplete state after syncing, which is the main cause of the shorter re-orgs we are seeing. --- .../org/qortal/controller/Synchronizer.java | 21 ++++--------------- 1 file changed, 4 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/qortal/controller/Synchronizer.java b/src/main/java/org/qortal/controller/Synchronizer.java index 42b46a31..948e9785 100644 --- a/src/main/java/org/qortal/controller/Synchronizer.java +++ b/src/main/java/org/qortal/controller/Synchronizer.java @@ -785,19 +785,13 @@ public class Synchronizer { if (cachedCommonBlockData != null) cachedCommonBlockData.setBlockSummariesAfterCommonBlock(null); - // If we have already received recent or newer blocks from this peer, go ahead and apply them + // If we have already received newer blocks from this peer that what we have already, go ahead and apply them if (peerBlocks.size() > 0) { final BlockData ourLatestBlockData = repository.getBlockRepository().getLastBlock(); final Block peerLatestBlock = peerBlocks.get(peerBlocks.size() - 1); final Long minLatestBlockTimestamp = Controller.getMinimumLatestBlockTimestamp(); if (ourLatestBlockData != null && peerLatestBlock != null && minLatestBlockTimestamp != null) { - // If we have received at least one recent block, we can apply them - if (peerLatestBlock.getBlockData().getTimestamp() > minLatestBlockTimestamp) { - LOGGER.debug("Newly received blocks are recent, so we will apply them"); - break; - } - // If our latest block is very old.... if (ourLatestBlockData.getTimestamp() < minLatestBlockTimestamp) { // ... and we have received a block that is more recent than our latest block ... @@ -813,7 +807,7 @@ public class Synchronizer { } } } - // Otherwise, give up and move on to the next peer, to avoid putting our chain into an outdated state + // Otherwise, give up and move on to the next peer, to avoid putting our chain into an outdated or incomplete state return SynchronizationResult.NO_REPLY; } @@ -837,20 +831,13 @@ public class Synchronizer { nextHeight, Base58.encode(nextPeerSignature))); if (retryCount >= maxRetries) { - - // If we have already received recent or newer blocks from this peer, go ahead and apply them + // If we have already received newer blocks from this peer that what we have already, go ahead and apply them if (peerBlocks.size() > 0) { final BlockData ourLatestBlockData = repository.getBlockRepository().getLastBlock(); final Block peerLatestBlock = peerBlocks.get(peerBlocks.size() - 1); final Long minLatestBlockTimestamp = Controller.getMinimumLatestBlockTimestamp(); if (ourLatestBlockData != null && peerLatestBlock != null && minLatestBlockTimestamp != null) { - // If we have received at least one recent block, we can apply them - if (peerLatestBlock.getBlockData().getTimestamp() > minLatestBlockTimestamp) { - LOGGER.debug("Newly received blocks are recent, so we will apply them"); - break; - } - // If our latest block is very old.... if (ourLatestBlockData.getTimestamp() < minLatestBlockTimestamp) { // ... and we have received a block that is more recent than our latest block ... @@ -866,7 +853,7 @@ public class Synchronizer { } } } - // Otherwise, give up and move on to the next peer, to avoid putting our chain into an outdated state + // Otherwise, give up and move on to the next peer, to avoid putting our chain into an outdated or incomplete state return SynchronizationResult.NO_REPLY; } else {