From dc876d9c96391ddf487805f1d7f3940dc355f829 Mon Sep 17 00:00:00 2001 From: CalDescent Date: Sun, 3 Oct 2021 16:00:11 +0100 Subject: [PATCH] Force a bootstrap if the block archive isn't intact on launch This allows the topOnly setting to be disabled and the node will automatically bootstrap to the archive version. A rebuild isn't attempted if bootstrapping is disabled, in order to reduce risk. --- src/main/java/org/qortal/block/BlockChain.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/qortal/block/BlockChain.java b/src/main/java/org/qortal/block/BlockChain.java index 33899011..78bf4b8e 100644 --- a/src/main/java/org/qortal/block/BlockChain.java +++ b/src/main/java/org/qortal/block/BlockChain.java @@ -502,13 +502,22 @@ public class BlockChain { */ public static void validate() throws DataException { + boolean isTopOnly = Settings.getInstance().isTopOnly(); + boolean archiveEnabled = Settings.getInstance().isArchiveEnabled(); + boolean canBootstrap = Settings.getInstance().getBootstrap(); + boolean needsArchiveRebuild = false; BlockData chainTip; + try (final Repository repository = RepositoryManager.getRepository()) { chainTip = repository.getBlockRepository().getLastBlock(); + + // Ensure archive is (at least partially) intact, and force a bootstrap if it isn't + if (!isTopOnly && archiveEnabled && canBootstrap) { + needsArchiveRebuild = (repository.getBlockArchiveRepository().fromHeight(2) == null); + LOGGER.info("Couldn't retrieve block 2 from archive. Bootstrapping..."); + } } - boolean isTopOnly = Settings.getInstance().isTopOnly(); - boolean archiveEnabled = Settings.getInstance().isArchiveEnabled(); boolean hasBlocks = (chainTip != null && chainTip.getHeight() > 1); if (isTopOnly && hasBlocks) { @@ -516,7 +525,7 @@ public class BlockChain { // It's best not to validate it, and there's no real need to } else { // Check first block is Genesis Block - if (!isGenesisBlockValid()) { + if (!isGenesisBlockValid() || needsArchiveRebuild) { try { rebuildBlockchain();