From 50244c1c40a115fc75c163cf7a6c2a7e309566dd Mon Sep 17 00:00:00 2001 From: CalDescent Date: Sat, 1 May 2021 13:32:16 +0100 Subject: [PATCH] Fixed bug which would cause other peers to not be compared against each other, if we had no blocks ourselves. Again, this wouldn't have affected anything in 1.5.0 or before, but it will become more significant if we switch to same-length chain weight comparisons. --- .../org/qortal/controller/Synchronizer.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/qortal/controller/Synchronizer.java b/src/main/java/org/qortal/controller/Synchronizer.java index 05db6c54..a6131840 100644 --- a/src/main/java/org/qortal/controller/Synchronizer.java +++ b/src/main/java/org/qortal/controller/Synchronizer.java @@ -267,7 +267,7 @@ public class Synchronizer { // Calculate the length of the shortest peer chain sharing this common block, including our chain final int ourAdditionalBlocksAfterCommonBlock = ourHeight - commonBlockSummary.getHeight(); - int minChainLength = this.calculateMinChainLength(commonBlockSummary, ourAdditionalBlocksAfterCommonBlock, peersSharingCommonBlock); + int minChainLength = this.calculateMinChainLengthOfPeers(peersSharingCommonBlock, commonBlockSummary); // Fetch block summaries from each peer for (Peer peer : peersSharingCommonBlock) { @@ -325,10 +325,15 @@ public class Synchronizer { final int ourSummariesRequired = Math.min(ourAdditionalBlocksAfterCommonBlock, MAXIMUM_REQUEST_SIZE); LOGGER.trace(String.format("About to fetch our block summaries from %d to %d. Our height: %d", commonBlockSummary.getHeight() + 1, commonBlockSummary.getHeight() + ourSummariesRequired, ourHeight)); List ourBlockSummaries = repository.getBlockRepository().getBlockSummaries(commonBlockSummary.getHeight() + 1, commonBlockSummary.getHeight() + ourSummariesRequired); - if (ourBlockSummaries.isEmpty()) + if (ourBlockSummaries.isEmpty()) { LOGGER.debug(String.format("We don't have any block summaries so can't compare our chain against peers with this common block. We can still compare them against each other.")); - else + } + else { populateBlockSummariesMinterLevels(repository, ourBlockSummaries); + // Reduce minChainLength if we have less summaries + if (ourBlockSummaries.size() < minChainLength) + minChainLength = ourBlockSummaries.size(); + } // Create array to hold peers for comparison List superiorPeersForComparison = new ArrayList<>(); @@ -426,14 +431,14 @@ public class Synchronizer { return commonBlocks; } - private int calculateMinChainLength(BlockSummaryData commonBlockSummary, int ourAdditionalBlocksAfterCommonBlock, List peersSharingCommonBlock) { + private int calculateMinChainLengthOfPeers(List peersSharingCommonBlock, BlockSummaryData commonBlockSummary) { // Calculate the length of the shortest peer chain sharing this common block, including our chain - int minChainLength = ourAdditionalBlocksAfterCommonBlock; + int minChainLength = 0; for (Peer peer : peersSharingCommonBlock) { final int peerHeight = peer.getChainTipData().getLastHeight(); final int peerAdditionalBlocksAfterCommonBlock = peerHeight - commonBlockSummary.getHeight(); - if (peerAdditionalBlocksAfterCommonBlock < minChainLength) + if (peerAdditionalBlocksAfterCommonBlock < minChainLength || minChainLength == 0) minChainLength = peerAdditionalBlocksAfterCommonBlock; } return minChainLength;