From a89e939a8f1960534183b2652aba9598d6aee85f Mon Sep 17 00:00:00 2001 From: Alexandra Date: Wed, 21 Nov 2018 16:58:49 -0500 Subject: [PATCH 1/5] introduced migration of old kernel configuration --- modMcf/src/org/aion/mcf/config/Cfg.java | 62 +++++++++++++++++++++---- 1 file changed, 54 insertions(+), 8 deletions(-) diff --git a/modMcf/src/org/aion/mcf/config/Cfg.java b/modMcf/src/org/aion/mcf/config/Cfg.java index f316d1addf..fefdb3f901 100644 --- a/modMcf/src/org/aion/mcf/config/Cfg.java +++ b/modMcf/src/org/aion/mcf/config/Cfg.java @@ -24,7 +24,6 @@ import com.google.common.annotations.VisibleForTesting; import java.io.File; -import java.io.IOException; import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import org.aion.mcf.types.AbstractBlock; @@ -157,7 +156,6 @@ public void setConsensus(CfgConsensus _consensus) { private File baseGenesisFile = null; private File baseForkFile = null; - // can be absolute in config file OR depend on execution path private File logDir = null; private File databaseDir = null; @@ -205,15 +203,63 @@ protected void initializeConfiguration() { baseConfigFile = new File(CONFIG_DIR, configFileName); baseGenesisFile = new File(CONFIG_DIR, genesisFileName); - if (!baseConfigFile.exists() || !baseGenesisFile.exists()) { updateNetworkExecPaths(); } else { - execDir = INITIAL_PATH; - execConfigDir = CONFIG_DIR; - execConfigFile = baseConfigFile; - execGenesisFile = baseGenesisFile; - updateStoragePaths(); + System.out.println("Migrating to the new configuration style for Aion kernels."); + + // reading the old config to get setup + this.fromXML(baseConfigFile); + + // determine the network from the read config + switch (this.net.getId()) { + case 256: + network = "mainnet"; + break; + case 128: + network = "conquest"; + break; + case 32: + network = "mastery"; + break; + default: + network = "custom"; + break; + } + + // delete old config + try { + baseConfigFile.delete(); + } catch (Exception e) { + System.out.println( + "Unable to delete old configuration file: " + + baseConfigFile.getAbsolutePath() + + ". Please do it manually!"); + } + + // delete old genesis + try { + baseGenesisFile.delete(); + } catch (Exception e) { + System.out.println( + "Unable to delete old genesis file: " + + baseGenesisFile.getAbsolutePath() + + ". Please do it manually!"); + } + + // using absolute path for database + absoluteDatabaseDir = true; + databaseDir = new File(INITIAL_PATH, getDb().getPath()); + + // using absolute path for log + absoluteLogDir = true; + logDir = new File(INITIAL_PATH, getLog().getLogPath()); + + // TODO: keystore + + updateNetworkExecPaths(); + + this.toXML(new String[] {}, baseConfigFile); } } From 21a51b1dc9658b048bbc2a5e652770eda5ca7123 Mon Sep 17 00:00:00 2001 From: Alexandra Date: Wed, 21 Nov 2018 17:45:17 -0500 Subject: [PATCH 2/5] setting the keystore path from the config file --- .../org/aion/zero/impl/config/CfgAion.java | 29 +++++++--- modMcf/src/org/aion/mcf/config/Cfg.java | 54 +++++++++++++++++-- 2 files changed, 72 insertions(+), 11 deletions(-) diff --git a/modAionImpl/src/org/aion/zero/impl/config/CfgAion.java b/modAionImpl/src/org/aion/zero/impl/config/CfgAion.java index ffb0ff682b..0b07d5223b 100644 --- a/modAionImpl/src/org/aion/zero/impl/config/CfgAion.java +++ b/modAionImpl/src/org/aion/zero/impl/config/CfgAion.java @@ -65,6 +65,7 @@ public final class CfgAion extends Cfg { public CfgAion() { this.mode = "aion"; this.id = UUID.randomUUID().toString(); + this.keystorePath = null; this.net = new CfgNet(); this.consensus = new CfgConsensusPow(); this.sync = new CfgSync(); @@ -139,10 +140,10 @@ private void closeFileInputStream(final FileInputStream fis) { } } -// /** @implNote the default fork settings is looking for the fork config of the mainnet. */ -// public void setForkProperties() { -// setForkProperties("mainnet", null); -// } + // /** @implNote the default fork settings is looking for the fork config of the mainnet. */ + // public void setForkProperties() { + // setForkProperties("mainnet", null); + // } public void setForkProperties(String networkName, File forkFile) { Properties properties = new Properties(); @@ -172,9 +173,9 @@ public void setForkProperties(String networkName, File forkFile) { } } -// public void setForkProperties(String networkName) { -// setForkProperties(networkName, null); -// } + // public void setForkProperties(String networkName) { + // setForkProperties(networkName, null); + // } public void dbFromXML() { File cfgFile = getInitialConfigFile(); @@ -307,6 +308,13 @@ public boolean fromXML(File cfgFile) { this.setLogDir(log); } + if (keystorePath != null) { + File ks = new File(keystorePath); + if (ks.isAbsolute()) { + this.setKeystoreDir(ks); + } + } + return shouldWriteBackToFile; } @@ -387,6 +395,13 @@ public void toXML(final String[] args, File file) { sw.writeCharacters(this.getId()); sw.writeEndElement(); + if (keystorePath != null) { + sw.writeCharacters("\r\n\t"); + sw.writeStartElement("keystore"); + sw.writeCharacters(keystorePath); + sw.writeEndElement(); + } + sw.writeCharacters(this.getApi().toXML()); sw.writeCharacters(this.getNet().toXML()); sw.writeCharacters(this.getSync().toXML()); diff --git a/modMcf/src/org/aion/mcf/config/Cfg.java b/modMcf/src/org/aion/mcf/config/Cfg.java index fefdb3f901..a6e13d8aa4 100644 --- a/modMcf/src/org/aion/mcf/config/Cfg.java +++ b/modMcf/src/org/aion/mcf/config/Cfg.java @@ -35,6 +35,8 @@ public abstract class Cfg { protected String id; + protected String keystorePath = null; + protected CfgApi api; protected CfgNet net; @@ -159,8 +161,10 @@ public void setConsensus(CfgConsensus _consensus) { // can be absolute in config file OR depend on execution path private File logDir = null; private File databaseDir = null; + private File keystoreDir = null; private boolean absoluteLogDir = false; private boolean absoluteDatabaseDir = false; + private boolean absoluteKeystoreDir = false; // impact execution path private String network = null; @@ -183,8 +187,10 @@ public void resetInternal() { baseForkFile = null; logDir = null; databaseDir = null; + keystoreDir = null; absoluteLogDir = false; absoluteDatabaseDir = false; + absoluteKeystoreDir = false; network = null; dataDir = null; execDir = null; @@ -229,7 +235,12 @@ protected void initializeConfiguration() { // delete old config try { - baseConfigFile.delete(); + if (!baseConfigFile.delete()) { + System.out.println( + "Unable to delete old configuration file: " + + baseConfigFile.getAbsolutePath() + + ". Please do it manually!"); + } } catch (Exception e) { System.out.println( "Unable to delete old configuration file: " @@ -239,7 +250,12 @@ protected void initializeConfiguration() { // delete old genesis try { - baseGenesisFile.delete(); + if (!baseGenesisFile.delete()) { + System.out.println( + "Unable to delete old genesis file: " + + baseGenesisFile.getAbsolutePath() + + ". Please do it manually!"); + } } catch (Exception e) { System.out.println( "Unable to delete old genesis file: " @@ -255,7 +271,10 @@ protected void initializeConfiguration() { absoluteLogDir = true; logDir = new File(INITIAL_PATH, getLog().getLogPath()); - // TODO: keystore + // using absolute path for keystore + absoluteKeystoreDir = true; + keystoreDir = new File(INITIAL_PATH, keystoreDirName); + keystorePath = keystoreDir.getAbsolutePath(); updateNetworkExecPaths(); @@ -305,6 +324,18 @@ private void updateStoragePaths() { } else if (databaseDir == null) { databaseDir = new File(getDb().getPath()); } + if (!absoluteKeystoreDir) { + if (keystorePath != null) { + // absolute paths are set when reading the file + // so this must be a relative path + keystoreDir = new File(execDir, keystorePath); + } else { + // path not set so using defaults + keystoreDir = new File(execDir, keystoreDirName); + } + } else if (keystoreDir == null) { + keystoreDir = new File(keystorePath); + } } /** @@ -391,8 +422,23 @@ public void setDatabaseDir(File _databaseDirectory) { this.absoluteDatabaseDir = true; } + /** + * Used to set an absolute path for the keystore directory. + * + * @param _keystoreDirectory the path to be used for the keystore. + */ + public void setKeystoreDir(File _keystoreDirectory) { + this.keystoreDir = _keystoreDirectory; + this.absoluteKeystoreDir = true; + } + public File getKeystoreDir() { - return new File(getExecDir(), keystoreDirName); + if (keystoreDir == null) { + // was not updated with absolute path + keystoreDir = + new File(getExecDir(), keystorePath != null ? keystorePath : keystoreDirName); + } + return keystoreDir; } /** Returns the configuration directory location for the kernel execution. */ From 19e5f8ade35889524c4044f94350b4e79af3d2d4 Mon Sep 17 00:00:00 2001 From: Alexandra Date: Thu, 22 Nov 2018 11:34:56 -0500 Subject: [PATCH 3/5] set the absolute path for db and log to be written back to file --- modMcf/src/org/aion/mcf/config/Cfg.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modMcf/src/org/aion/mcf/config/Cfg.java b/modMcf/src/org/aion/mcf/config/Cfg.java index a6e13d8aa4..2e503d3e13 100644 --- a/modMcf/src/org/aion/mcf/config/Cfg.java +++ b/modMcf/src/org/aion/mcf/config/Cfg.java @@ -266,10 +266,12 @@ protected void initializeConfiguration() { // using absolute path for database absoluteDatabaseDir = true; databaseDir = new File(INITIAL_PATH, getDb().getPath()); + getDb().setPath(databaseDir.getAbsolutePath()); // using absolute path for log absoluteLogDir = true; logDir = new File(INITIAL_PATH, getLog().getLogPath()); + getLog().setLogPath(logDir.getAbsolutePath()); // using absolute path for keystore absoluteKeystoreDir = true; From 74973367f5824c52810d05e46a5b304d3fd6b0d5 Mon Sep 17 00:00:00 2001 From: Alexandra Date: Thu, 22 Nov 2018 12:06:32 -0500 Subject: [PATCH 4/5] updated unit tests for migration of old kernel config --- .../test/org/aion/zero/impl/cli/CliTest.java | 70 +++++++++++++++++-- 1 file changed, 63 insertions(+), 7 deletions(-) diff --git a/modAionImpl/test/org/aion/zero/impl/cli/CliTest.java b/modAionImpl/test/org/aion/zero/impl/cli/CliTest.java index e1da5d1577..75600174f4 100644 --- a/modAionImpl/test/org/aion/zero/impl/cli/CliTest.java +++ b/modAionImpl/test/org/aion/zero/impl/cli/CliTest.java @@ -498,15 +498,13 @@ public void testConfig(String[] input, File expectedFile, String expectedPath) { /** Parameters for testing {@link #testConfig_oldLocation(String[], String)}. */ @SuppressWarnings("unused") - private Object parametersWithConfigForOldLocation() { + private Object parametersWithoutMigration() { List parameters = new ArrayList<>(); String[] options = new String[] {"-c", "--config"}; String expected = MAIN_BASE_PATH.getAbsolutePath(); for (String op : options) { - // without parameter - parameters.add(new Object[] {new String[] {op}, BASE_PATH}); // invalid parameter parameters.add(new Object[] {new String[] {op, "invalid"}, expected}); // mainnet as parameter @@ -530,7 +528,7 @@ private Object parametersWithConfigForOldLocation() { * location. */ @Test - @Parameters(method = "parametersWithConfigForOldLocation") + @Parameters(method = "parametersWithoutMigration") public void testConfig_oldLocation(String[] input, String expectedPath) { // ensure config exists on disk at expected location for old kernel if (!oldConfig.exists()) { @@ -542,19 +540,69 @@ public void testConfig_oldLocation(String[] input, String expectedPath) { Cli.copyRecursively(genesis, oldGenesis); } - assertThat(cli.call(input, cfg)).isEqualTo(EXIT); + + // the config used it for mainnet, therefore will use the MAIN_BASE_PATH assertThat(cfg.getBasePath()).isEqualTo(expectedPath); + assertThat(cfg.getExecConfigFile()) .isEqualTo(new File(expectedPath, "config" + File.separator + configFileName)); assertThat(cfg.getExecGenesisFile()) .isEqualTo(new File(expectedPath, "config" + File.separator + genesisFileName)); + // database, keystore & log are absolute and at old location + assertThat(cfg.getDatabaseDir()).isEqualTo(new File(expectedPath, "database")); + assertThat(cfg.getLogDir()).isEqualTo(new File(expectedPath, "log")); + assertThat(cfg.getKeystoreDir()).isEqualTo(new File(expectedPath, "keystore")); + if (verbose) { printPaths(cfg); } } + /** + * Ensures that the { -c, --config } arguments work when using old config + * location. + */ + @Test + @Parameters({"-c", "--config"}) + public void testConfig_withMigration(String option) { + // ensure config exists on disk at expected location for old kernel + if (!oldConfig.exists()) { + File configPath = CONFIG_PATH; + if (!configPath.exists()) { + assertThat(configPath.mkdirs()).isTrue(); + } + cfg.toXML(null, oldConfig); + Cli.copyRecursively(genesis, oldGenesis); + } + + assertThat(cli.call(new String[] {option}, cfg)).isEqualTo(EXIT); + + // the config used it for mainnet, therefore will use the MAIN_BASE_PATH + assertThat(cfg.getBasePath()).isEqualTo(MAIN_BASE_PATH.getAbsolutePath()); + + assertThat(cfg.getInitialConfigFile()).isEqualTo(mainnetConfig); + assertThat(cfg.getInitialGenesisFile()).isEqualTo(mainnetGenesis); + + assertThat(cfg.getExecConfigFile()) + .isEqualTo(new File(MAIN_BASE_PATH, "config" + File.separator + configFileName)); + assertThat(cfg.getExecGenesisFile()) + .isEqualTo(new File(MAIN_BASE_PATH, "config" + File.separator + genesisFileName)); + + // database, keystore & log are absolute and at old location + assertThat(cfg.getDatabaseDir()).isEqualTo(new File(BASE_PATH, "database")); + assertThat(cfg.getLogDir()).isEqualTo(new File(BASE_PATH, "log")); + assertThat(cfg.getKeystoreDir()).isEqualTo(new File(BASE_PATH, "keystore")); + + if (verbose) { + printPaths(cfg); + } + + // cleanup: resetting the mainnet config to original + Cli.copyRecursively(config, mainnetConfig); + } + /** Parameters for testing {@link #testInfo(String[], ReturnType, String)}. */ @SuppressWarnings("unused") private Object parametersWithInfo() { @@ -659,7 +707,7 @@ public void testInfo(String[] input, ReturnType expectedReturn, String expectedP */ @Test @Parameters({"-i", "--info"}) - public void testInfo_oldLocation(String option) { + public void testInfoWithMigration(String option) { // ensure config exists on disk at expected location for old kernel if (!oldConfig.exists()) { File configPath = CONFIG_PATH; @@ -671,7 +719,15 @@ public void testInfo_oldLocation(String option) { } assertThat(cli.call(new String[] {option}, cfg)).isEqualTo(EXIT); - assertThat(cfg.getBasePath()).isEqualTo(BASE_PATH); + assertThat(cfg.getBasePath()).isEqualTo(MAIN_BASE_PATH.getAbsolutePath()); + + // database, keystore & log are absolute and at old location + assertThat(cfg.getDatabaseDir()).isEqualTo(new File(BASE_PATH, "database")); + assertThat(cfg.getLogDir()).isEqualTo(new File(BASE_PATH, "log")); + assertThat(cfg.getKeystoreDir()).isEqualTo(new File(BASE_PATH, "keystore")); + + // cleanup: resetting the mainnet config to original + Cli.copyRecursively(config, mainnetConfig); } /** From 2a19f3ec101b4daa122dac91704fdf7caa0807a9 Mon Sep 17 00:00:00 2001 From: Alexandra Date: Thu, 22 Nov 2018 12:11:32 -0500 Subject: [PATCH 5/5] minor spacking adjustment --- modBoot/src/org/aion/Aion.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modBoot/src/org/aion/Aion.java b/modBoot/src/org/aion/Aion.java index a0e1205b9a..937cf0fdf5 100644 --- a/modBoot/src/org/aion/Aion.java +++ b/modBoot/src/org/aion/Aion.java @@ -191,14 +191,14 @@ public static void main(String args[]) { + filePath[3] + "\n> Genesis write: " + filePath[4] - + "\n> Fork write: " + + "\n> Fork write: " + filePath[5] + "\n----------------------------------------------------------------------------" + "\n> Config read: " + filePath[6] + "\n> Genesis read: " + filePath[7] - + "\n> Fork read: " + + "\n> Fork read: " + filePath[8] + "\n----------------------------------------------------------------------------\n\n";