|
|
|
@ -199,8 +199,6 @@ public class Synchronizer extends Thread {
|
|
|
|
|
if (this.isSynchronizing) |
|
|
|
|
return true; |
|
|
|
|
|
|
|
|
|
boolean isNewConsensusActive = NTP.getTime() >= BlockChain.getInstance().getNewConsensusTimestamp(); |
|
|
|
|
|
|
|
|
|
// Needs a mutable copy of the unmodifiableList
|
|
|
|
|
List<Peer> peers = new ArrayList<>(Network.getInstance().getImmutableHandshakedPeers()); |
|
|
|
|
|
|
|
|
@ -229,10 +227,6 @@ public class Synchronizer extends Thread {
|
|
|
|
|
if (peers.size() < Settings.getInstance().getMinBlockchainPeers()) |
|
|
|
|
return true; |
|
|
|
|
|
|
|
|
|
if (isNewConsensusActive) |
|
|
|
|
// Disregard peers with a shorter chain
|
|
|
|
|
peers.removeIf(Controller.hasShorterBlockchain); |
|
|
|
|
|
|
|
|
|
// Disregard peers that have no block signature or the same block signature as us
|
|
|
|
|
peers.removeIf(Controller.hasNoOrSameBlock); |
|
|
|
|
|
|
|
|
@ -241,13 +235,11 @@ public class Synchronizer extends Thread {
|
|
|
|
|
|
|
|
|
|
final int peersBeforeComparison = peers.size(); |
|
|
|
|
|
|
|
|
|
if (isNewConsensusActive) { |
|
|
|
|
// Request recent block summaries from the remaining peers, and locate our common block with each
|
|
|
|
|
// Synchronizer.getInstance().findCommonBlocksWithPeers(peers);
|
|
|
|
|
Synchronizer.getInstance().findCommonBlocksWithPeers(peers); |
|
|
|
|
|
|
|
|
|
// Compare the peers against each other, and against our chain, which will return an updated list excluding those without common blocks
|
|
|
|
|
// peers = Synchronizer.getInstance().comparePeers(peers);
|
|
|
|
|
} |
|
|
|
|
peers = Synchronizer.getInstance().comparePeers(peers); |
|
|
|
|
|
|
|
|
|
// We may have added more inferior chain tips when comparing peers, so remove any peers that are currently on those chains
|
|
|
|
|
peers.removeIf(Controller.hasInferiorChainTip); |
|
|
|
@ -993,13 +985,8 @@ public class Synchronizer extends Thread {
|
|
|
|
|
return SynchronizationResult.NOTHING_TO_DO; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
boolean isNewConsensusActive = NTP.getTime() >= BlockChain.getInstance().getNewConsensusTimestamp(); |
|
|
|
|
|
|
|
|
|
// Unless we're doing a forced sync, we might need to compare blocks after common block
|
|
|
|
|
boolean isBlockComparisonNeeded = isNewConsensusActive |
|
|
|
|
? ourInitialHeight == peerHeight |
|
|
|
|
: ourInitialHeight > commonBlockHeight; |
|
|
|
|
if (!force && isBlockComparisonNeeded) { |
|
|
|
|
if (!force && ourInitialHeight > commonBlockHeight) { |
|
|
|
|
SynchronizationResult chainCompareResult = compareChains(repository, commonBlockData, ourLatestBlockData, peer, peerHeight, peerBlockSummaries); |
|
|
|
|
if (chainCompareResult != SynchronizationResult.OK) |
|
|
|
|
return chainCompareResult; |
|
|
|
@ -1200,34 +1187,6 @@ public class Synchronizer extends Thread {
|
|
|
|
|
peerBlockSummaries.addAll(moreBlockSummaries); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
boolean isNewConsensusActive = NTP.getTime() >= BlockChain.getInstance().getNewConsensusTimestamp(); |
|
|
|
|
if (isNewConsensusActive) { |
|
|
|
|
int parentHeight = ourLatestBlockData.getHeight() - 1; |
|
|
|
|
|
|
|
|
|
BlockSummaryData ourLatestBlockSummary = new BlockSummaryData(ourLatestBlockData); |
|
|
|
|
byte[] ourParentBlockSignature = ourLatestBlockData.getReference(); |
|
|
|
|
|
|
|
|
|
BlockSummaryData peersLatestBlockSummary = peerBlockSummaries.get(peerBlockSummaries.size() - 1); |
|
|
|
|
byte[] peersParentBlockSignature = peerBlockSummaries.size() > 1 |
|
|
|
|
? peerBlockSummaries.get(peerBlockSummaries.size() - 1 - 1).getSignature() |
|
|
|
|
: commonBlockSig; |
|
|
|
|
|
|
|
|
|
// Populate minter account levels for both lists of block summaries
|
|
|
|
|
populateBlockSummariesMinterLevels(repository, Collections.singletonList(ourLatestBlockSummary)); |
|
|
|
|
populateBlockSummariesMinterLevels(repository, Collections.singletonList(peersLatestBlockSummary)); |
|
|
|
|
|
|
|
|
|
BigInteger ourChainWeight = Block.calcBlockWeight(parentHeight, ourParentBlockSignature, ourLatestBlockSummary); |
|
|
|
|
BigInteger peerChainWeight = Block.calcBlockWeight(parentHeight, peersParentBlockSignature, peersLatestBlockSummary); |
|
|
|
|
|
|
|
|
|
NumberFormat accurateFormatter = new DecimalFormat("0.################E0"); |
|
|
|
|
LOGGER.debug(String.format("Our chain weight: %s, peer's chain weight: %s (higher is better)", accurateFormatter.format(ourChainWeight), accurateFormatter.format(peerChainWeight))); |
|
|
|
|
|
|
|
|
|
// If our blockchain has greater weight then don't synchronize with peer
|
|
|
|
|
if (ourChainWeight.compareTo(peerChainWeight) >= 0) { |
|
|
|
|
LOGGER.debug(String.format("Not synchronizing with peer %s as we have better blockchain", peer)); |
|
|
|
|
return SynchronizationResult.INFERIOR_CHAIN; |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
// Fetch our corresponding block summaries
|
|
|
|
|
List<BlockSummaryData> ourBlockSummaries = repository.getBlockRepository().getBlockSummaries(commonBlockHeight + 1, ourLatestBlockData.getHeight()); |
|
|
|
|
|
|
|
|
@ -1251,7 +1210,6 @@ public class Synchronizer extends Thread {
|
|
|
|
|
return SynchronizationResult.INFERIOR_CHAIN; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return SynchronizationResult.OK; |
|
|
|
|
} |
|
|
|
|