Skip to content

Commit

Permalink
Merge pull request #843 from aionnetwork/vm-fatal-case
Browse files Browse the repository at this point in the history
add handling for the vm fatal status
  • Loading branch information
AionJayT authored Mar 14, 2019
2 parents e993ad6 + 9470582 commit 284c1cf
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 72 deletions.
19 changes: 17 additions & 2 deletions modAionImpl/src/org/aion/zero/impl/AionBlockchainImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.aion.vm.BulkExecutor;
import org.aion.vm.ExecutionBatch;
import org.aion.vm.PostExecutionWork;
import org.aion.vm.exception.VMException;
import org.aion.zero.exceptions.HeaderStructureException;
import org.aion.zero.impl.blockchain.ChainConfiguration;
import org.aion.zero.impl.config.CfgAion;
Expand Down Expand Up @@ -1148,7 +1149,14 @@ private RetValidPreBlock generatePreBlock(IAionBlock block) {
block.getNrgLimit(),
LOGGER_VM,
getPostExecutionWorkForGeneratePreBlock());
List<AionTxExecSummary> executionSummaries = executor.execute();

List<AionTxExecSummary> executionSummaries = null;
try {
executionSummaries = executor.execute();
} catch (VMException e) {
LOG.error("Shutdown due to a VM fatal error.", e);
System.exit(-1);
}

for (AionTxExecSummary summary : executionSummaries) {
if (!summary.isRejected()) {
Expand Down Expand Up @@ -1212,7 +1220,14 @@ private AionBlockSummary applyBlock(IAionBlock block) {
block.getNrgLimit(),
LOGGER_VM,
getPostExecutionWorkForApplyBlock());
List<AionTxExecSummary> executionSummaries = executor.execute();

List<AionTxExecSummary> executionSummaries = null;
try {
executionSummaries = executor.execute();
} catch (VMException e) {
LOG.error("Shutdown due to a VM fatal error.", e);
System.exit(-1);
}

for (AionTxExecSummary summary : executionSummaries) {
receipts.add(summary.getReceipt());
Expand Down
10 changes: 9 additions & 1 deletion modAionImpl/src/org/aion/zero/impl/blockchain/AionImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.aion.vm.BulkExecutor;
import org.aion.vm.ExecutionBatch;
import org.aion.vm.PostExecutionWork;
import org.aion.vm.exception.VMException;
import org.aion.zero.impl.AionHub;
import org.aion.zero.impl.config.CfgAion;
import org.aion.zero.impl.tx.TxCollector;
Expand Down Expand Up @@ -94,7 +95,6 @@ public IMineRunner getBlockMiner() {
LOG_GEN.info("Miner address is not set");
return null;
}

}

@Override
Expand Down Expand Up @@ -149,6 +149,10 @@ public long estimateTxNrg(AionTransaction tx, IAionBlock block) {
LOG_VM,
getPostExecutionWork());
return executor.execute().get(0).getReceipt().getEnergyUsed();
} catch (VMException e) {
LOG_GEN.error("Shutdown due to a VM fatal error.", e);
System.exit(-1);
return 0;
} finally {
repository.rollback();
}
Expand Down Expand Up @@ -176,6 +180,10 @@ public AionTxReceipt callConstant(AionTransaction tx, IAionBlock block) {
LOG_VM,
getPostExecutionWork());
return executor.execute().get(0).getReceipt();
} catch (VMException e) {
LOG_GEN.error("Shutdown due to a VM fatal error.", e);
System.exit(-1);
return null;
} finally {
repository.rollback();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.aion.vm.BulkExecutor;
import org.aion.vm.ExecutionBatch;
import org.aion.vm.PostExecutionWork;
import org.aion.vm.exception.VMException;
import org.aion.zero.impl.AionBlockchainImpl;
import org.aion.zero.impl.config.CfgAion;
import org.aion.zero.impl.core.IAionBlockchain;
Expand Down Expand Up @@ -1073,7 +1074,13 @@ private AionTxExecSummary executeTx(AionTransaction tx, boolean inPool) {
bestBlk.getNrgLimit(),
LOGGER_VM,
getPostExecutionWork());
return txExe.execute().get(0);
try {
return txExe.execute().get(0);
} catch (VMException e) {
LOGGER_VM.error("Shutdown due to a VM fatal error.", e);
System.exit(-1);
return null;
}
}

/**
Expand Down
8 changes: 2 additions & 6 deletions modAionImpl/src/org/aion/zero/impl/db/AionRepositoryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -823,11 +823,7 @@ public byte[] getTrieNode(byte[] key, DatabaseType dbType) {
ByteArrayKeyValueDatabase db = selectDatabase(dbType);

Optional<byte[]> value = db.get(key);
if (value.isPresent()) {
return value.get();
} else {
return null;
}
return value.orElse(null);
}

/**
Expand Down Expand Up @@ -861,9 +857,9 @@ public Map<ByteArrayWrapper, byte[]> getReferencedTrieNodes(
* @param key the hash key of the trie node to be imported
* @param value the value of the trie node to be imported
* @param dbType the database where the key-value pair should be stored
* @return a {@link TrieNodeResult} indicating the success or failure of the import operation
* @throws IllegalArgumentException if the given key is null or the database type is not
* supported
* @return a {@link TrieNodeResult} indicating the success or failure of the import operation
*/
public TrieNodeResult importTrieNode(byte[] key, byte[] value, DatabaseType dbType) {
// empty keys are not allowed
Expand Down
9 changes: 5 additions & 4 deletions modAionImpl/test/org/aion/zero/impl/vm/Benchmark.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.aion.vm.ExecutionBatch;
import org.aion.vm.PostExecutionWork;

import org.aion.vm.exception.VMException;
import org.aion.zero.impl.db.AionRepositoryImpl;
import org.aion.zero.impl.types.AionBlock;
import org.aion.zero.impl.vm.contracts.ContractUtils;
Expand Down Expand Up @@ -78,7 +79,7 @@ public class Benchmark {

private static Logger LOGGER = AionLoggerFactory.getLogger(LogEnum.VM.name());

private static void prepare() throws IOException {
private static void prepare() throws IOException, VMException {
long t1 = System.currentTimeMillis();

// create owner account
Expand Down Expand Up @@ -179,7 +180,7 @@ private static List<AionTxReceipt> validateTransactions(List<AionTransaction> tx
return list;
}

private static List<AionTxReceipt> executeTransactions(List<AionTransaction> txs) {
private static List<AionTxReceipt> executeTransactions(List<AionTransaction> txs) throws VMException {
long t1 = System.currentTimeMillis();
List<AionTxReceipt> list = new ArrayList<>();

Expand Down Expand Up @@ -216,7 +217,7 @@ private static void flush() {
timeFlush = t2 - t1;
}

private static void verifyState(int num) {
private static void verifyState(int num) throws VMException {
long ownerNonce = repo.getNonce(owner).longValue();

for (int i = 0; i < recipients.size(); i++) {
Expand Down Expand Up @@ -248,7 +249,7 @@ private static void verifyState(int num) {
}
}

public static void main(String args[]) throws IOException {
public static void main(String args[]) throws IOException, VMException {
int n = 10000;
prepare();
List<AionTransaction> list = signTransactions(n);
Expand Down
33 changes: 17 additions & 16 deletions modAionImpl/test/org/aion/zero/impl/vm/ContractIntegTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.aion.vm.PostExecutionWork;

import org.aion.vm.api.interfaces.ResultCode;
import org.aion.vm.exception.VMException;
import org.aion.zero.db.AionRepositoryCache;
import org.aion.zero.impl.StandaloneBlockchain;
import org.aion.zero.impl.StandaloneBlockchain.Builder;
Expand Down Expand Up @@ -97,7 +98,7 @@ public void tearDown() {
}

@Test
public void testEmptyContract() throws IOException {
public void testEmptyContract() throws IOException, VMException {
String contractName = "EmptyContract";
byte[] deployCode = getDeployCode(contractName);
long nrg = 1_000_000;
Expand Down Expand Up @@ -140,7 +141,7 @@ public void testEmptyContract() throws IOException {
}

@Test
public void testContractDeployCodeIsEmpty() {
public void testContractDeployCodeIsEmpty() throws VMException {
long nrg = 1_000_000;
long nrgPrice = 1;
BigInteger value = BigInteger.ZERO;
Expand Down Expand Up @@ -175,7 +176,7 @@ public void testContractDeployCodeIsEmpty() {
}

@Test
public void testContractDeployCodeIsNonsensical() {
public void testContractDeployCodeIsNonsensical() throws VMException {
byte[] deployCode = new byte[1];
deployCode[0] = 0x1;
long nrg = 1_000_000;
Expand Down Expand Up @@ -213,7 +214,7 @@ public void testContractDeployCodeIsNonsensical() {
}

@Test
public void testTransferValueToNonPayableConstructor() throws IOException {
public void testTransferValueToNonPayableConstructor() throws IOException, VMException {
String contractName = "EmptyContract";
byte[] deployCode = getDeployCode(contractName);
long nrg = 1_000_000;
Expand Down Expand Up @@ -252,7 +253,7 @@ public void testTransferValueToNonPayableConstructor() throws IOException {
}

@Test
public void testTransferValueToPayableConstructor() throws IOException {
public void testTransferValueToPayableConstructor() throws IOException, VMException {
String contractName = "PayableConstructor";
byte[] deployCode = getDeployCode(contractName);
long nrg = 1_000_000;
Expand Down Expand Up @@ -286,7 +287,7 @@ public void testTransferValueToPayableConstructor() throws IOException {
}

@Test
public void testTransferValueToPayableConstructorInsufficientFunds() throws IOException {
public void testTransferValueToPayableConstructorInsufficientFunds() throws IOException, VMException {
String contractName = "PayableConstructor";
byte[] deployCode = getDeployCode(contractName);
long nrg = 1_000_000;
Expand Down Expand Up @@ -324,7 +325,7 @@ public void testTransferValueToPayableConstructorInsufficientFunds() throws IOEx
}

@Test
public void testConstructorIsCalledOnCodeDeployment() throws IOException {
public void testConstructorIsCalledOnCodeDeployment() throws IOException, VMException {
String contractName = "MultiFeatureContract";
byte[] deployCode =
ContractUtils.getContractDeployer(
Expand Down Expand Up @@ -368,7 +369,7 @@ public void testConstructorIsCalledOnCodeDeployment() throws IOException {
}

@Test
public void testCallFunction() throws IOException {
public void testCallFunction() throws IOException, VMException {
String contractName = "MultiFeatureContract";
byte[] deployCode = getDeployCode(contractName);
long nrg = 1_000_000;
Expand Down Expand Up @@ -439,7 +440,7 @@ public void testCallFunction() throws IOException {
}

@Test
public void testOverWithdrawFromContract() throws IOException {
public void testOverWithdrawFromContract() throws IOException, VMException {
String contractName = "MultiFeatureContract";
byte[] deployCode = getDeployCode(contractName);
long nrg = 1_000_000;
Expand Down Expand Up @@ -486,7 +487,7 @@ public void testOverWithdrawFromContract() throws IOException {
}

@Test
public void testWithdrawFromContract() throws IOException {
public void testWithdrawFromContract() throws IOException, VMException {
String contractName = "MultiFeatureContract";
byte[] deployCode = getDeployCode(contractName);
long nrg = 1_000_000;
Expand Down Expand Up @@ -530,7 +531,7 @@ public void testWithdrawFromContract() throws IOException {
}

@Test
public void testSendContractFundsToOtherAddress() throws IOException {
public void testSendContractFundsToOtherAddress() throws IOException, VMException {
String contractName = "MultiFeatureContract";
byte[] deployCode = getDeployCode(contractName);
long nrg = 1_000_000;
Expand Down Expand Up @@ -581,7 +582,7 @@ public void testSendContractFundsToOtherAddress() throws IOException {
}

@Test
public void testSendContractFundsToNonexistentAddress() throws IOException {
public void testSendContractFundsToNonexistentAddress() throws IOException, VMException {
String contractName = "MultiFeatureContract";
byte[] deployCode = getDeployCode(contractName);
long nrg = 1_000_000;
Expand Down Expand Up @@ -631,7 +632,7 @@ public void testSendContractFundsToNonexistentAddress() throws IOException {
}

@Test
public void testCallContractViaAnotherContract() throws IOException {
public void testCallContractViaAnotherContract() throws IOException, VMException {
// Deploy the MultiFeatureContract.
String contractName = "MultiFeatureContract";
byte[] deployCode = getDeployCode(contractName);
Expand Down Expand Up @@ -722,7 +723,7 @@ public void testCallContractViaAnotherContract() throws IOException {
}

@Test
public void testRecursiveStackoverflow() throws IOException {
public void testRecursiveStackoverflow() throws IOException, VMException {
String contractName = "Recursive";
byte[] deployCode = getDeployCode(contractName);
long nrg = Constants.NRG_TRANSACTION_MAX;
Expand Down Expand Up @@ -847,7 +848,7 @@ public void testCallPrecompiledContract() {
}

@Test
public void testRedeployContractAtExistentContractAddress() throws IOException {
public void testRedeployContractAtExistentContractAddress() throws IOException, VMException {
String contractName = "MultiFeatureContract";
byte[] deployCode = getDeployCode(contractName);
long nrg = 1_000_000;
Expand Down Expand Up @@ -960,7 +961,7 @@ private Address deployContract(
long nrg,
long nrgPrice,
BigInteger nonce)
throws IOException {
throws IOException, VMException {

tx.sign(deployerKey);
assertTrue(tx.isContractCreationTransaction());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.aion.vm.PostExecutionWork;

import org.aion.vm.api.interfaces.InternalTransactionInterface;
import org.aion.vm.exception.VMException;
import org.aion.zero.impl.BlockContext;
import org.aion.zero.impl.StandaloneBlockchain;
import org.aion.zero.impl.types.AionTxInfo;
Expand Down Expand Up @@ -222,7 +223,7 @@ function f(int n) {
}
*/
@Test
public void testRecursiveCall() throws InterruptedException {
public void testRecursiveCall() throws InterruptedException, VMException {
String contractA =
"0x605060405234156100105760006000fd5b610015565b60e9806100236000396000f30060506040526000356c01000000000000000000000000900463ffffffff168063ec77996414603157602b565b60006000fd5b3415603c5760006000fd5b605060048080359060100190919050506052565b005b600081131560b9573063ec779964600184036040518263ffffffff166c01000000000000000000000000028152600401808281526010019150506000604051808303816000888881813b151560a75760006000fd5b5af1151560b45760006000fd5b505050505b5b505600a165627a7a7230582033f76d593b80b3468bfb0f873882bc00903a790a9b996cb8ca3bac51295994cd0029";

Expand Down Expand Up @@ -313,7 +314,7 @@ function A() {
}
*/
@Test
public void testNestedCreate() throws InterruptedException {
public void testNestedCreate() throws InterruptedException, VMException {
String contractA =
"0x60506040523415600f5760006000fd5b5b60166048565b604051809103906000f0801582151615602f5760006000fd5b60006000508282909180600101839055555050505b6057565b604051605a8061009f83390190565b603a806100656000396000f30060506040526008565b60006000fd00a165627a7a72305820c0eea40d4778b01848164e58898e9e8c8ab068ed5ee36ed6f0582d119ecbbede002960506040523415600f5760006000fd5b6013565b603a8060206000396000f30060506040526008565b60006000fd00a165627a7a723058208c13bc92baf844f8574632dca44c49776516cb6cd537b10ed700bf61392b6ae80029";

Expand Down
9 changes: 5 additions & 4 deletions modAionImpl/test/org/aion/zero/impl/vm/OldTxExecutorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.aion.vm.ExecutionBatch;
import org.aion.vm.PostExecutionWork;

import org.aion.vm.exception.VMException;
import org.aion.zero.impl.StandaloneBlockchain;
import org.aion.zero.impl.db.AionRepositoryImpl;
import org.aion.zero.impl.types.AionBlock;
Expand Down Expand Up @@ -83,7 +84,7 @@ public void tearDown() {
}

@Test
public void testCallTransaction() throws IOException {
public void testCallTransaction() throws IOException, VMException {
Compiler.Result r =
Compiler.getInstance()
.compile(
Expand Down Expand Up @@ -134,7 +135,7 @@ public void testCallTransaction() throws IOException {
}

@Test
public void testCreateTransaction() throws IOException {
public void testCreateTransaction() throws IOException, VMException {
Compiler.Result r =
Compiler.getInstance()
.compile(
Expand Down Expand Up @@ -180,7 +181,7 @@ public void testCreateTransaction() throws IOException {
}

@Test
public void testPerformance() throws IOException {
public void testPerformance() throws IOException, VMException {
Compiler.Result r =
Compiler.getInstance()
.compile(
Expand Down Expand Up @@ -234,7 +235,7 @@ public void testPerformance() throws IOException {
}

@Test
public void testBasicTransactionCost() {
public void testBasicTransactionCost() throws VMException {
byte[] txNonce = DataWordImpl.ZERO.getData();
Address from =
Address.wrap(
Expand Down
Loading

0 comments on commit 284c1cf

Please sign in to comment.