forked from Qortal/qortal
Don't allow latest blocks cache to be empty
This commit is contained in:
parent
d2a92db921
commit
88da8d949f
@ -926,7 +926,7 @@ public class Controller extends Thread {
|
|||||||
this.latestBlocks.addLast(latestBlockData);
|
this.latestBlocks.addLast(latestBlockData);
|
||||||
|
|
||||||
// Trim if necessary
|
// Trim if necessary
|
||||||
if (latestBlockData.getHeight() - this.latestBlocks.peekFirst().getHeight() >= BLOCK_CACHE_SIZE)
|
if (this.latestBlocks.size() >= BLOCK_CACHE_SIZE)
|
||||||
this.latestBlocks.pollFirst();
|
this.latestBlocks.pollFirst();
|
||||||
} else {
|
} else {
|
||||||
if (cachedChainTip != null)
|
if (cachedChainTip != null)
|
||||||
@ -974,9 +974,13 @@ public class Controller extends Thread {
|
|||||||
|
|
||||||
synchronized (this.latestBlocks) {
|
synchronized (this.latestBlocks) {
|
||||||
BlockData cachedChainTip = this.latestBlocks.pollLast();
|
BlockData cachedChainTip = this.latestBlocks.pollLast();
|
||||||
|
boolean refillNeeded = false;
|
||||||
|
|
||||||
if (cachedChainTip != null && Arrays.equals(cachedChainTip.getReference(), blockDataCopy.getSignature())) {
|
if (cachedChainTip != null && Arrays.equals(cachedChainTip.getReference(), blockDataCopy.getSignature())) {
|
||||||
// Chain tip was parent for new latest block that has been orphaned, so we're good
|
// Chain tip was parent for new latest block that has been orphaned, so we're good
|
||||||
|
|
||||||
|
// However, if we've emptied the cache then we will need to refill it
|
||||||
|
refillNeeded = this.latestBlocks.isEmpty();
|
||||||
} else {
|
} else {
|
||||||
if (cachedChainTip != null)
|
if (cachedChainTip != null)
|
||||||
// Chain tip didn't match - potentially abnormal behaviour?
|
// Chain tip didn't match - potentially abnormal behaviour?
|
||||||
@ -986,6 +990,10 @@ public class Controller extends Thread {
|
|||||||
Base58.encode(blockDataCopy.getSignature())));
|
Base58.encode(blockDataCopy.getSignature())));
|
||||||
|
|
||||||
// Defensively rebuild cache
|
// Defensively rebuild cache
|
||||||
|
refillNeeded = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (refillNeeded)
|
||||||
try {
|
try {
|
||||||
this.stats.latestBlocksCacheRefills.incrementAndGet();
|
this.stats.latestBlocksCacheRefills.incrementAndGet();
|
||||||
|
|
||||||
@ -993,7 +1001,6 @@ public class Controller extends Thread {
|
|||||||
} catch (DataException e) {
|
} catch (DataException e) {
|
||||||
LOGGER.warn(() -> "Couldn't refill latest blocks cache?", e);
|
LOGGER.warn(() -> "Couldn't refill latest blocks cache?", e);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
this.onNewOrOrphanedBlock(blockDataCopy, OrphanedBlockEvent::new);
|
this.onNewOrOrphanedBlock(blockDataCopy, OrphanedBlockEvent::new);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user