Browse Source

Select a random host when importing a bootstrap, and started adding support for multiple bootstrap types.

bootstrap
CalDescent 3 years ago
parent
commit
51bb776e56
  1. 28
      src/main/java/org/qortal/repository/Bootstrap.java
  2. 9
      src/main/java/org/qortal/settings/Settings.java
  3. 20
      src/test/java/org/qortal/test/BootstrapTests.java

28
src/main/java/org/qortal/repository/Bootstrap.java

@ -18,6 +18,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.nio.file.*; import java.nio.file.*;
import java.security.SecureRandom;
import java.util.List; import java.util.List;
import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeoutException;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
@ -256,7 +257,7 @@ public class Bootstrap {
); );
LOGGER.info("Compressing..."); LOGGER.info("Compressing...");
String compressedOutputPath = String.format("%s%s", Settings.getInstance().getBootstrapFilenamePrefix(), "bootstrap.7z"); String compressedOutputPath = String.format("%s%s", Settings.getInstance().getBootstrapFilenamePrefix(), this.getFilename());
try { try {
Files.delete(Paths.get(compressedOutputPath)); Files.delete(Paths.get(compressedOutputPath));
} catch (NoSuchFileException e) { } catch (NoSuchFileException e) {
@ -305,7 +306,8 @@ public class Bootstrap {
Path path = null; Path path = null;
try { try {
Path tempDir = Files.createTempDirectory("qortal-bootstrap"); Path tempDir = Files.createTempDirectory("qortal-bootstrap");
path = Paths.get(tempDir.toString(), "bootstrap.7z"); String filename = String.format("%s%s", Settings.getInstance().getBootstrapFilenamePrefix(), this.getFilename());
path = Paths.get(tempDir.toString(), filename);
this.downloadToPath(path); this.downloadToPath(path);
this.importFromPath(path); this.importFromPath(path);
@ -325,11 +327,31 @@ public class Bootstrap {
} }
} }
private String getFilename() {
boolean pruningEnabled = Settings.getInstance().isPruningEnabled();
boolean archiveEnabled = Settings.getInstance().isArchiveEnabled();
if (pruningEnabled) {
return "bootstrap-toponly.7z";
}
else if (archiveEnabled) {
return "bootstrap-archive.7z";
}
else {
return "bootstrap-full.7z";
}
}
private void downloadToPath(Path path) throws DataException { private void downloadToPath(Path path) throws DataException {
String bootstrapUrl = "http://bootstrap.qortal.org/bootstrap.7z"; // Select a random host from bootstrapHosts
String[] hosts = Settings.getInstance().getBootstrapHosts();
int index = new SecureRandom().nextInt(hosts.length);
String bootstrapHost = hosts[index];
String bootstrapFilename = this.getFilename();
try { try {
LOGGER.info("Downloading bootstrap..."); LOGGER.info("Downloading bootstrap...");
String bootstrapUrl = String.format("%s/%s", bootstrapHost, bootstrapFilename);
InputStream in = new URL(bootstrapUrl).openStream(); InputStream in = new URL(bootstrapUrl).openStream();
Files.copy(in, path, REPLACE_EXISTING); Files.copy(in, path, REPLACE_EXISTING);

9
src/main/java/org/qortal/settings/Settings.java

@ -194,6 +194,11 @@ public class Settings {
// Bootstrap // Bootstrap
private String bootstrapFilenamePrefix = ""; private String bootstrapFilenamePrefix = "";
// Bootstrap sources
private String[] bootstrapHosts = new String[] {
"http://bootstrap.qortal.org"
};
// Auto-update sources // Auto-update sources
private String[] autoUpdateRepos = new String[] { private String[] autoUpdateRepos = new String[] {
"https://github.com/Qortal/qortal/raw/%s/qortal.update", "https://github.com/Qortal/qortal/raw/%s/qortal.update",
@ -528,6 +533,10 @@ public class Settings {
return this.autoUpdateRepos; return this.autoUpdateRepos;
} }
public String[] getBootstrapHosts() {
return this.bootstrapHosts;
}
public String getListsPath() { public String getListsPath() {
return this.listsPath; return this.listsPath;
} }

20
src/test/java/org/qortal/test/BootstrapTests.java

@ -67,7 +67,7 @@ public class BootstrapTests extends Common {
@Test @Test
public void testCreateAndImportBootstrap() throws DataException, InterruptedException, TransformationException, IOException { public void testCreateAndImportBootstrap() throws DataException, InterruptedException, TransformationException, IOException {
Path bootstrapPath = Paths.get(String.format("%s%s", Settings.getInstance().getBootstrapFilenamePrefix(), "bootstrap.7z")); Path bootstrapPath = Paths.get(String.format("%s%s", Settings.getInstance().getBootstrapFilenamePrefix(), "bootstrap-archive.7z"));
Path archivePath = Paths.get(Settings.getInstance().getRepositoryPath(), "archive", "2-900.dat"); Path archivePath = Paths.get(Settings.getInstance().getRepositoryPath(), "archive", "2-900.dat");
BlockData block1000; BlockData block1000;
byte[] originalArchiveContents; byte[] originalArchiveContents;
@ -183,7 +183,23 @@ public class BootstrapTests extends Common {
private void deleteBootstraps() throws IOException { private void deleteBootstraps() throws IOException {
try { try {
Path path = Paths.get(String.format("%s%s", Settings.getInstance().getBootstrapFilenamePrefix(), "bootstrap.7z")); Path path = Paths.get(String.format("%s%s", Settings.getInstance().getBootstrapFilenamePrefix(), "bootstrap-archive.7z"));
Files.delete(path);
} catch (NoSuchFileException e) {
// Nothing to delete
}
try {
Path path = Paths.get(String.format("%s%s", Settings.getInstance().getBootstrapFilenamePrefix(), "bootstrap-toponly.7z"));
Files.delete(path);
} catch (NoSuchFileException e) {
// Nothing to delete
}
try {
Path path = Paths.get(String.format("%s%s", Settings.getInstance().getBootstrapFilenamePrefix(), "bootstrap-full.7z"));
Files.delete(path); Files.delete(path);
} catch (NoSuchFileException e) { } catch (NoSuchFileException e) {

Loading…
Cancel
Save