diff --git a/src/main/java/org/qortal/repository/Bootstrap.java b/src/main/java/org/qortal/repository/Bootstrap.java index aa464fe4..311c6be4 100644 --- a/src/main/java/org/qortal/repository/Bootstrap.java +++ b/src/main/java/org/qortal/repository/Bootstrap.java @@ -368,10 +368,7 @@ public class Bootstrap { } private void downloadToPath(Path path) throws DataException { - // Select a random host from bootstrapHosts - String[] hosts = Settings.getInstance().getBootstrapHosts(); - int index = new SecureRandom().nextInt(hosts.length); - String bootstrapHost = hosts[index]; + String bootstrapHost = this.getRandomHost(); String bootstrapFilename = this.getFilename(); String bootstrapUrl = String.format("%s/%s", bootstrapHost, bootstrapFilename); @@ -420,6 +417,14 @@ public class Bootstrap { } } + public String getRandomHost() { + // Select a random host from bootstrapHosts + String[] hosts = Settings.getInstance().getBootstrapHosts(); + int index = new SecureRandom().nextInt(hosts.length); + String bootstrapHost = hosts[index]; + return bootstrapHost; + } + public void importFromPath(Path path) throws InterruptedException, DataException, IOException { ReentrantLock blockchainLock = Controller.getInstance().getBlockchainLock(); diff --git a/src/test/java/org/qortal/test/BootstrapTests.java b/src/test/java/org/qortal/test/BootstrapTests.java index a15311f5..cc29794c 100644 --- a/src/test/java/org/qortal/test/BootstrapTests.java +++ b/src/test/java/org/qortal/test/BootstrapTests.java @@ -22,6 +22,7 @@ import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import static org.junit.Assert.*; @@ -181,6 +182,28 @@ public class BootstrapTests extends Common { repository.saveChanges(); } + @Test + public void testGetRandomHost() { + String[] bootstrapHosts = Settings.getInstance().getBootstrapHosts(); + List uniqueHosts = new ArrayList<>(); + + for (int i=0; i<1000; i++) { + Bootstrap bootstrap = new Bootstrap(); + String randomHost = bootstrap.getRandomHost(); + assertNotNull(randomHost); + + if (!uniqueHosts.contains(randomHost)){ + uniqueHosts.add(randomHost); + } + } + + // Ensure we have more than one bootstrap host in the settings + assertTrue(Arrays.asList(bootstrapHosts).size() > 1); + + // Ensure that all have been given the opportunity to be used + assertEquals(uniqueHosts.size(), Arrays.asList(bootstrapHosts).size()); + } + private void deleteBootstraps() throws IOException { try { Path path = Paths.get(String.format("%s%s", Settings.getInstance().getBootstrapFilenamePrefix(), "bootstrap-archive.7z"));