From 47b1b6daba9681da5240b26189c5234a03de2442 Mon Sep 17 00:00:00 2001 From: CalDescent Date: Sat, 2 Oct 2021 14:43:26 +0100 Subject: [PATCH] Retry the entire bootstrap import process on failure, rather than just the download. --- .../java/org/qortal/block/BlockChain.java | 9 +++- .../java/org/qortal/repository/Bootstrap.java | 45 ++++++++++--------- 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/qortal/block/BlockChain.java b/src/main/java/org/qortal/block/BlockChain.java index a0aca44d..e5a0da5f 100644 --- a/src/main/java/org/qortal/block/BlockChain.java +++ b/src/main/java/org/qortal/block/BlockChain.java @@ -517,7 +517,12 @@ public class BlockChain { } else { // Check first block is Genesis Block if (!isGenesisBlockValid()) { - rebuildBlockchain(); + try { + rebuildBlockchain(); + + } catch (InterruptedException e) { + throw new DataException(String.format("Interrupted when trying to rebuild blockchain: %s", e.getMessage())); + } } } @@ -600,7 +605,7 @@ public class BlockChain { } } - private static void rebuildBlockchain() throws DataException { + private static void rebuildBlockchain() throws DataException, InterruptedException { boolean shouldBootstrap = Settings.getInstance().getBootstrap(); if (shouldBootstrap) { // Settings indicate that we should apply a bootstrap rather than rebuilding and syncing from genesis diff --git a/src/main/java/org/qortal/repository/Bootstrap.java b/src/main/java/org/qortal/repository/Bootstrap.java index bd76918e..5a130839 100644 --- a/src/main/java/org/qortal/repository/Bootstrap.java +++ b/src/main/java/org/qortal/repository/Bootstrap.java @@ -27,7 +27,7 @@ import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; public class Bootstrap { - private Repository repository; + private final Repository repository; private static final Logger LOGGER = LogManager.getLogger(Bootstrap.class); @@ -286,7 +286,22 @@ public class Bootstrap { } } - public void startImport() throws DataException { + public void startImport() throws InterruptedException { + while (!Controller.isStopping()) { + try { + LOGGER.info("Starting import of bootstrap..."); + + this.doImport(); + + } catch (DataException e) { + LOGGER.info("Bootstrap import failed: {}", e.getMessage()); + LOGGER.info("Retrying in 5 minutes"); + Thread.sleep(5 * 60 * 1000L); + } + } + } + + private void doImport() throws DataException { Path path = null; try { Path tempDir = Files.createTempDirectory("qortal-bootstrap"); @@ -313,28 +328,14 @@ public class Bootstrap { private void downloadToPath(Path path) throws DataException { String bootstrapUrl = "http://bootstrap.qortal.org/bootstrap.7z"; - while (!Controller.isStopping()) { - try { - LOGGER.info("Downloading bootstrap..."); - InputStream in = new URL(bootstrapUrl).openStream(); - Files.copy(in, path, REPLACE_EXISTING); - break; - - } catch (IOException e) { - LOGGER.info("Unable to download bootstrap: {}", e.getMessage()); - LOGGER.info("Retrying in 5 minutes"); + try { + LOGGER.info("Downloading bootstrap..."); + InputStream in = new URL(bootstrapUrl).openStream(); + Files.copy(in, path, REPLACE_EXISTING); - try { - repository.discardChanges(); - Thread.sleep(5 * 60 * 1000L); - } catch (InterruptedException e2) { - break; - } - } + } catch (IOException e) { + throw new DataException(String.format("Unable to download bootstrap: {}", e.getMessage())); } - - // It's best to throw an exception on all failures, even though we're most likely just stopping - throw new DataException("Unable to download bootstrap"); } public void importFromPath(Path path) throws InterruptedException, DataException, IOException {