Use only first block in pendingBlocks instead of iteration

This commit is contained in:
Manfred Karrer 2018-12-05 20:33:12 +01:00
parent 806fcbe7c0
commit 92c852b601
No known key found for this signature in database
GPG key ID: 401250966A6B2C46

View file

@ -215,16 +215,10 @@ public abstract class BsqNode implements DaoSetupService {
// After parsing we check if we have pending blocks we might have received earlier but which have been
// not connecting from the latest height we had. The list is sorted by height
if (!pendingBlocks.isEmpty()) {
// To avoid ConcurrentModificationException we copy the list. It might be altered in the method call
ArrayList<RawBlock> tempPendingBlocks = new ArrayList<>(pendingBlocks);
for (RawBlock tempPendingBlock : tempPendingBlocks) {
try {
doParseBlock(tempPendingBlock);
} catch (RequiredReorgFromSnapshotException e1) {
// In case we got a reorg we break the iteration
break;
}
}
// We take only first element after sorting (so it is the block with the next height) to avoid that
// we would repeat calls in recursions in case we would iterate the list.
pendingBlocks.sort(Comparator.comparing(RawBlock::getHeight));
doParseBlock(pendingBlocks.get(0));
}
return Optional.of(block);
@ -236,7 +230,6 @@ public abstract class BsqNode implements DaoSetupService {
int heightForNextBlock = daoStateService.getChainHeight() + 1;
if (rawBlock.getHeight() > heightForNextBlock) {
pendingBlocks.add(rawBlock);
pendingBlocks.sort(Comparator.comparing(RawBlock::getHeight));
log.info("We received an block with a future block height. We store it as pending and try to apply " +
"it at the next block. rawBlock: height/hash={}/{}", rawBlock.getHeight(), rawBlock.getHash());
} else if (rawBlock.getHeight() >= daoStateService.getGenesisBlockHeight()) {