Skip to content

Commit

Permalink
predefined error codes for exiting the application #856
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandraRoatis committed Jul 26, 2019
1 parent cc696b6 commit 627a00c
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 39 deletions.
7 changes: 4 additions & 3 deletions modAionImpl/src/org/aion/zero/impl/AionBlockchainImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,8 @@ public synchronized ImportResult tryToConnect(final Block block) {
LOG.error("Unable to dump heap due to exception:", e);
}

System.exit(0);
// requested shutdown
System.exit(SystemExitCodes.NORMAL);
}
return tryToConnectInternal(block, System.currentTimeMillis() / THOUSAND_MS);
}
Expand Down Expand Up @@ -1401,7 +1402,7 @@ private RetValidPreBlock generatePreBlock(Block block) {
}
} catch (VMException e) {
LOG.error("Shutdown due to a VM fatal error.", e);
System.exit(-1);
System.exit(SystemExitCodes.FATAL_VM_ERROR);
}
}

Expand Down Expand Up @@ -1456,7 +1457,7 @@ private AionBlockSummary applyBlock(Block block) {
}
} catch (VMException e) {
LOG.error("Shutdown due to a VM fatal error.", e);
System.exit(-1);
System.exit(SystemExitCodes.FATAL_VM_ERROR);
}
}
Map<AionAddress, BigInteger> rewards = addReward(block);
Expand Down
8 changes: 3 additions & 5 deletions modAionImpl/src/org/aion/zero/impl/AionHub.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,6 @@ public static AionHub inst() {
return Holder.INSTANCE;
}

public static final int INIT_ERROR_EXIT_CODE = -1;

public AionHub() {
initializeHub(CfgAion.inst(), AionBlockchainImpl.inst(), AionRepositoryImpl.inst(), false);
}
Expand Down Expand Up @@ -299,7 +297,7 @@ private void loadBlockchain() {
this.repository.getBlockStore().load();
} catch (RuntimeException re) {
genLOG.error("Fatal: can't load blockstore; exiting.", re);
System.exit(INIT_ERROR_EXIT_CODE);
System.exit(SystemExitCodes.INITIALIZATION_ERROR);
}

// Note: if block DB corruption, the bestBlock may not match with the indexDB.
Expand Down Expand Up @@ -479,7 +477,7 @@ private void loadBlockchain() {
+ "\t3) Reboot with correct genesis and empty database\n",
genesisHash == null ? "null" : ByteUtil.toHexString(genesisHash),
databaseGenHash == null ? "null" : ByteUtil.toHexString(databaseGenHash));
System.exit(INIT_ERROR_EXIT_CODE);
System.exit(SystemExitCodes.INITIALIZATION_ERROR);
}

if (!Arrays.equals(blockchain.getBestBlock().getStateRoot(), EMPTY_TRIE_HASH)) {
Expand Down Expand Up @@ -555,7 +553,7 @@ private void registerBlockEvents() {
this.eventMgr.registerEvent(evts);
} else {
genLOG.error("Event manager is null!");
System.exit(INIT_ERROR_EXIT_CODE);
System.exit(SystemExitCodes.INITIALIZATION_ERROR);
}
}
}
2 changes: 2 additions & 0 deletions modAionImpl/src/org/aion/zero/impl/SystemExitCodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ public class SystemExitCodes {
public static final int NORMAL = 0;
public static final int OUT_OF_DISK_SPACE = 1;
public static final int DATABASE_CORRUPTION = 2;
public static final int FATAL_VM_ERROR = 3;
public static final int INITIALIZATION_ERROR = 4;
}
5 changes: 3 additions & 2 deletions modAionImpl/src/org/aion/zero/impl/blockchain/AionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.aion.vm.BulkExecutor;
import org.aion.vm.exception.VMException;
import org.aion.zero.impl.AionHub;
import org.aion.zero.impl.SystemExitCodes;
import org.aion.zero.impl.config.CfgAion;
import org.aion.zero.impl.tx.TxCollector;
import org.aion.zero.impl.types.AionBlock;
Expand Down Expand Up @@ -155,7 +156,7 @@ public long estimateTxNrg(AionTransaction tx, Block block) {
.getEnergyUsed();
} catch (VMException e) {
LOG_GEN.error("Shutdown due to a VM fatal error.", e);
System.exit(-1);
System.exit(SystemExitCodes.FATAL_VM_ERROR);
return 0;
} finally {
repository.rollback();
Expand Down Expand Up @@ -197,7 +198,7 @@ public AionTxReceipt callConstant(AionTransaction tx, Block block) {
.getReceipt();
} catch (VMException e) {
LOG_GEN.error("Shutdown due to a VM fatal error.", e);
System.exit(-1);
System.exit(SystemExitCodes.FATAL_VM_ERROR);
return null;
} finally {
repository.rollback();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.aion.vm.BulkExecutor;
import org.aion.vm.exception.VMException;
import org.aion.zero.impl.AionBlockchainImpl;
import org.aion.zero.impl.SystemExitCodes;
import org.aion.zero.impl.config.CfgAion;
import org.aion.zero.impl.core.IAionBlockchain;
import org.aion.zero.impl.db.AionRepositoryImpl;
Expand Down Expand Up @@ -1116,7 +1117,7 @@ private AionTxExecSummary executeTx(AionTransaction tx, boolean inPool) {
bestBlk.getNumber());
} catch (VMException e) {
LOGGER_VM.error("Shutdown due to a VM fatal error.", e);
System.exit(-1);
System.exit(SystemExitCodes.FATAL_VM_ERROR);
return null;
}
}
Expand Down
17 changes: 9 additions & 8 deletions modAionImpl/src/org/aion/zero/impl/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.aion.util.conversions.Hex;
import org.aion.vm.LongLivedAvm;
import org.aion.zero.impl.AionBlockchainImpl;
import org.aion.zero.impl.SystemExitCodes;
import org.aion.zero.impl.Version;
import org.aion.zero.impl.config.Network;
import org.aion.zero.impl.db.DBUtils;
Expand All @@ -54,8 +55,8 @@ public class Cli {

public enum ReturnType {
RUN(2),
EXIT(0),
ERROR(1);
EXIT(SystemExitCodes.NORMAL),
ERROR(SystemExitCodes.INITIALIZATION_ERROR);
private final int value;

ReturnType(int _value) {
Expand Down Expand Up @@ -1201,7 +1202,7 @@ private String readPasswordFromReader(String prompt, BufferedReader reader) {
} catch (IOException e) {
System.err.println("Error reading from BufferedReader: " + reader);
e.printStackTrace();
System.exit(1);
System.exit(SystemExitCodes.INITIALIZATION_ERROR);
}
return null; // Make compiler happy; never get here.
}
Expand All @@ -1227,7 +1228,7 @@ private void createKeystoreDirIfMissing() {
System.out.println(
"Ssl keystore directory could not be created. "
+ "Please check user permissions or create directory manually.");
System.exit(1);
System.exit(SystemExitCodes.INITIALIZATION_ERROR);
}
System.out.println();
}
Expand All @@ -1238,7 +1239,7 @@ private void checkConsoleExists(Console console) {
if (console == null) {
System.out.println(
"No console found. This command can only be run interactively in a console environment.");
System.exit(1);
System.exit(SystemExitCodes.INITIALIZATION_ERROR);
}
}

Expand All @@ -1247,7 +1248,7 @@ private String getCertName(Console console) {
String certName = console.readLine();
if ((certName == null) || (certName.isEmpty())) {
System.out.println("Error: no certificate name entered.");
System.exit(1);
System.exit(SystemExitCodes.INITIALIZATION_ERROR);
}
return certName;
}
Expand All @@ -1262,13 +1263,13 @@ private String getCertPass(Console console) {
+ " characters):\n"));
if (certPass.isEmpty()) {
System.out.println("Error: no certificate password entered.");
System.exit(1);
System.exit(SystemExitCodes.INITIALIZATION_ERROR);
} else if (certPass.length() < minPassLen) {
System.out.println(
"Error: certificate password must be at least "
+ minPassLen
+ " characters long.");
System.exit(1);
System.exit(SystemExitCodes.INITIALIZATION_ERROR);
}
return certPass;
}
Expand Down
11 changes: 6 additions & 5 deletions modAionImpl/src/org/aion/zero/impl/config/CfgAion.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.aion.zero.exceptions.HeaderStructureException;
import org.aion.zero.impl.AionGenesis;
import org.aion.zero.impl.GenesisBlockLoader;
import org.aion.zero.impl.SystemExitCodes;

/** @author chris */
public final class CfgAion extends Cfg {
Expand Down Expand Up @@ -112,7 +113,7 @@ private void closeFileInputStream(final FileInputStream fis) {
fis.close();
} catch (IOException e) {
System.out.println("<error on-close-file-input-stream>");
System.exit(1);
System.exit(SystemExitCodes.INITIALIZATION_ERROR);
}
}
}
Expand Down Expand Up @@ -183,7 +184,7 @@ public void dbFromXML() {
}
} catch (Exception e) {
System.out.println("<error on-parsing-config-xml msg=" + e.getLocalizedMessage() + ">");
System.exit(1);
System.exit(SystemExitCodes.INITIALIZATION_ERROR);
} finally {
closeFileInputStream(fis);
}
Expand Down Expand Up @@ -270,7 +271,7 @@ public boolean fromXML(File cfgFile) {
closeFileInputStream(fis);
} catch (Exception e) {
System.out.println("<error on-parsing-config-xml msg=" + e.getLocalizedMessage() + ">");
System.exit(1);
System.exit(SystemExitCodes.INITIALIZATION_ERROR);
}

// checks for absolute path for database
Expand Down Expand Up @@ -400,14 +401,14 @@ public void toXML(final String[] args, File file) {
} catch (Exception e) {
e.printStackTrace();
System.out.println("<error on-write-config-xml-to-file>");
System.exit(1);
System.exit(SystemExitCodes.INITIALIZATION_ERROR);
} finally {
if (sw != null) {
try {
sw.close();
} catch (XMLStreamException e) {
System.out.println("<error on-close-stream-writer>");
System.exit(1);
System.exit(SystemExitCodes.INITIALIZATION_ERROR);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions modAionImpl/src/org/aion/zero/impl/db/AionRepositoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static org.aion.crypto.HashUtil.EMPTY_TRIE_HASH;
import static org.aion.crypto.HashUtil.h256;
import static org.aion.util.bytes.ByteUtil.EMPTY_BYTE_ARRAY;
import static org.aion.zero.impl.AionHub.INIT_ERROR_EXIT_CODE;

import com.google.common.annotations.VisibleForTesting;
import java.math.BigInteger;
Expand Down Expand Up @@ -44,6 +43,7 @@
import org.aion.util.bytes.ByteUtil;
import org.aion.util.conversions.Hex;
import org.aion.util.types.ByteArrayWrapper;
import org.aion.zero.impl.SystemExitCodes;
import org.aion.zero.impl.config.CfgAion;
import org.aion.zero.impl.sync.DatabaseType;
import org.aion.zero.impl.types.AionTxInfo;
Expand Down Expand Up @@ -106,7 +106,7 @@ private void init() {
LOGGEN.error("Shutdown due to failure to initialize repository.");
// the above message does not get logged without the printStackTrace below
e.printStackTrace();
System.exit(INIT_ERROR_EXIT_CODE);
System.exit(SystemExitCodes.INITIALIZATION_ERROR);
}
}

Expand Down
9 changes: 5 additions & 4 deletions modAionImpl/src/org/aion/zero/impl/sync/TaskImportBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.aion.p2p.P2pConstant;
import org.aion.util.types.ByteArrayWrapper;
import org.aion.zero.impl.AionBlockchainImpl;
import org.aion.zero.impl.SystemExitCodes;
import org.aion.zero.impl.db.AionBlockStore;
import org.aion.zero.impl.sync.PeerState.Mode;
import org.aion.zero.impl.sync.statistics.BlockType;
Expand Down Expand Up @@ -269,8 +270,8 @@ private PeerState processBatch(PeerState givenState, List<Block> batch, String d
log.error("<import-block throw> ", e);

if (e.getMessage() != null && e.getMessage().contains("No space left on device")) {
log.error("Shutdown due to lack of disk space.");
System.exit(0);
log.error("Shutdown due to lack of disk space.", e);
System.exit(SystemExitCodes.OUT_OF_DISK_SPACE);
}
break;
}
Expand Down Expand Up @@ -698,8 +699,8 @@ private int importFromStorage(PeerState state, long first, long last) {
log.error("<import-block throw> ", e);
if (e.getMessage() != null
&& e.getMessage().contains("No space left on device")) {
log.error("Shutdown due to lack of disk space.");
System.exit(0);
log.error("Shutdown due to lack of disk space.", e);
System.exit(SystemExitCodes.OUT_OF_DISK_SPACE);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.aion.api.server.http.RpcServerBuilder;
import org.aion.log.AionLoggerFactory;
import org.aion.log.LogEnum;
import org.aion.zero.impl.SystemExitCodes;
import org.slf4j.Logger;

public class NanoRpcServer extends RpcServer {
Expand Down Expand Up @@ -125,7 +126,7 @@ public void start() {
} catch (Exception e) {
LOG.error("<rpc-server - failed bind on {}:{}>", hostName, port);
LOG.error("<rpc-server - " + e.getMessage() + ">");
System.exit(1);
System.exit(SystemExitCodes.INITIALIZATION_ERROR);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.aion.api.server.http.RpcServerBuilder;
import org.aion.log.AionLoggerFactory;
import org.aion.log.LogEnum;
import org.aion.zero.impl.SystemExitCodes;
import org.slf4j.Logger;

public class UndertowRpcServer extends RpcServer {
Expand Down Expand Up @@ -175,7 +176,7 @@ public void start() {
} catch (Exception e) {
LOG.error("<rpc-server - failed bind on {}:{}>", hostName, port);
LOG.error("<rpc-server - " + e.getMessage() + ">");
System.exit(1);
System.exit(SystemExitCodes.INITIALIZATION_ERROR);
}
}

Expand Down
12 changes: 6 additions & 6 deletions modBoot/src/org/aion/Aion.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.aion;

import static java.lang.System.exit;
import static org.aion.crypto.ECKeyFac.ECKeyType.ED25519;
import static org.aion.crypto.HashUtil.H256Type.BLAKE2B_256;
import static org.aion.zero.impl.Version.KERNEL_VERSION;
Expand Down Expand Up @@ -46,6 +45,7 @@
import org.aion.solidity.Compiler;
import org.aion.utils.NativeLibrary;
import org.aion.vm.LongLivedAvm;
import org.aion.zero.impl.SystemExitCodes;
import org.aion.zero.impl.blockchain.AionFactory;
import org.aion.zero.impl.blockchain.IAionChain;
import org.aion.zero.impl.cli.Cli;
Expand Down Expand Up @@ -79,7 +79,7 @@ public static void main(String args[]) {

ReturnType ret = new Cli().call(args, cfg);
if (ret != ReturnType.RUN) {
exit(ret.getValue());
System.exit(ret.getValue());
}

Properties p = cfg.getFork().getProperties();
Expand All @@ -101,15 +101,15 @@ public static void main(String args[]) {
checkZmqKeyPair();
} catch (Exception e) {
System.out.println("Check zmq keypair fail! " + e.toString());
exit(1);
System.exit(SystemExitCodes.INITIALIZATION_ERROR);
}
}

// UUID check
String UUID = cfg.getId();
if (!UUID.matches("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")) {
System.out.println("Invalid UUID; please check <id> setting in config.xml");
exit(1);
System.exit(SystemExitCodes.INITIALIZATION_ERROR);
}

ServiceLoader.load(EventMgrModule.class);
Expand Down Expand Up @@ -395,7 +395,7 @@ private static void checkZmqKeyPair() throws IOException {
System.out.println(
"zmq keystore directory could not be created. "
+ "Please check user permissions or create directory manually.");
System.exit(1);
System.exit(SystemExitCodes.INITIALIZATION_ERROR);
}
System.out.println();
}
Expand Down Expand Up @@ -481,7 +481,7 @@ private static char[] getSslPassword(CfgAion cfg) {
System.out.println(
"SSL-certificate-use requested with RPC server and no console found. "
+ "Please set the ssl password in the config file (insecure) to run kernel non-interactively with this option.");
exit(1);
System.exit(SystemExitCodes.INITIALIZATION_ERROR);
} else {
console.printf("---------------------------------------------\n");
console.printf("----------- INTERACTION REQUIRED ------------\n");
Expand Down
3 changes: 2 additions & 1 deletion modDbImpl/src/org/aion/db/impl/leveldb/LevelDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ public boolean open() {

if (e1.getMessage() != null && e1.getMessage().contains("No space left on device")) {
LOG.error("Shutdown due to lack of disk space.");
System.exit(0);
// defined as SystemExitCodes.OUT_OF_DISK_SPACE in modAionImpl
System.exit(1);
}

try {
Expand Down

0 comments on commit 627a00c

Please sign in to comment.