Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config migration #718

Merged
merged 5 commits into from
Nov 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions modAionImpl/src/org/aion/zero/impl/config/CfgAion.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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());
Expand Down
70 changes: 63 additions & 7 deletions modAionImpl/test/org/aion/zero/impl/cli/CliTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> 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
Expand All @@ -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()) {
Expand All @@ -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 { <i>-c</i>, <i>--config</i> } 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() {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions modBoot/src/org/aion/Aion.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down
112 changes: 103 additions & 9 deletions modMcf/src/org/aion/mcf/config/Cfg.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,6 +35,8 @@ public abstract class Cfg {

protected String id;

protected String keystorePath = null;

protected CfgApi api;

protected CfgNet net;
Expand Down Expand Up @@ -157,12 +158,13 @@ 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;
private File keystoreDir = null;
private boolean absoluteLogDir = false;
private boolean absoluteDatabaseDir = false;
private boolean absoluteKeystoreDir = false;

// impact execution path
private String network = null;
Expand All @@ -185,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;
Expand All @@ -205,15 +209,78 @@ 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 {
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: "
+ baseConfigFile.getAbsolutePath()
+ ". Please do it manually!");
}

// delete old genesis
try {
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: "
+ baseGenesisFile.getAbsolutePath()
+ ". Please do it manually!");
}

// 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;
keystoreDir = new File(INITIAL_PATH, keystoreDirName);
keystorePath = keystoreDir.getAbsolutePath();

updateNetworkExecPaths();

this.toXML(new String[] {}, baseConfigFile);
}
}

Expand Down Expand Up @@ -259,6 +326,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);
}
}

/**
Expand Down Expand Up @@ -345,8 +424,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. */
Expand Down