Skip to content

Commit

Permalink
add precompile runtime exception
Browse files Browse the repository at this point in the history
  • Loading branch information
taihaofu committed Aug 24, 2018
1 parent 8f941cd commit 833d703
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
37 changes: 25 additions & 12 deletions src/main/java/org/tron/common/runtime/vm/PrecompiledContracts.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import org.tron.common.crypto.zksnark.BN128G2;
import org.tron.common.crypto.zksnark.Fp;
import org.tron.common.crypto.zksnark.PairingCheck;
import org.tron.common.runtime.vm.program.Program;
import org.tron.common.runtime.vm.program.Program.PrecompiledContractException;
import org.tron.common.runtime.vm.program.ProgramResult;
import org.tron.common.storage.Deposit;
import org.tron.common.utils.BIUtil;
Expand Down Expand Up @@ -714,13 +716,15 @@ public Pair<Boolean, byte[]> execute(byte[] data) {
} catch (ContractExeException e) {
logger.debug("ContractExeException when calling voteWitness in vm");
logger.debug("ContractExeException: {}", e.getMessage());
return null;
this.getResult().setException(new Program.Exception().contractExecuteException(e));
return Pair.of(false, new DataWord(0).getData());
} catch (ContractValidateException e) {
logger.debug("ContractValidateException when calling voteWitness in vm");
logger.debug("ContractValidateException: {}", e.getMessage());
return null;
this.getResult().setException(new Program.Exception().contractValidateException(e));
return Pair.of(false, new DataWord(0).getData());
}
return Pair.of(true, new DataWord(count).getData());
return Pair.of(true, new DataWord(1).getData());
}
}

Expand Down Expand Up @@ -886,11 +890,13 @@ public Pair<Boolean, byte[]> execute(byte[] data) {
} catch (ContractExeException e) {
logger.debug("ContractExeException when calling withdrawBalanceNative in vm");
logger.debug("ContractExeException: {}", e.getMessage());
return null;
this.getResult().setException(new Program.Exception().contractExecuteException(e));
return Pair.of(false, new DataWord(0).getData());
} catch (ContractValidateException e) {
logger.debug("ContractValidateException when calling withdrawBalanceNative in vm");
logger.debug("ContractValidateException: {}", e.getMessage());
return null;
this.getResult().setException(new Program.Exception().contractValidateException(e));
return Pair.of(false, new DataWord(0).getData());
}
return Pair.of(true, new DataWord(1).getData());
}
Expand Down Expand Up @@ -944,7 +950,8 @@ public Pair<Boolean, byte[]> execute(byte[] data) {
} catch (ContractExeException e) {
logger.debug("ContractExeException when calling proposalApproveNative in vm");
logger.debug("ContractExeException: {}", e.getMessage());
return null;
this.getResult().setException(new Program.Exception().contractExecuteException(e));
return Pair.of(false, new DataWord(0).getData());
} catch (ContractValidateException e) {
logger.debug("ContractValidateException when calling proposalApproveNative in vm");
logger.debug("ContractValidateException: {}", e.getMessage());
Expand Down Expand Up @@ -1009,11 +1016,13 @@ public Pair<Boolean, byte[]> execute(byte[] data) {
} catch (ContractExeException e) {
logger.debug("ContractExeException when calling proposalCreateNative in vm");
logger.debug("ContractExeException: {}", e.getMessage());
return null;
this.getResult().setException(new Program.Exception().contractExecuteException(e));
return Pair.of(false, new DataWord(0).getData());
} catch (ContractValidateException e) {
logger.debug("ContractValidateException when calling proposalCreateNative in vm");
logger.debug("ContractValidateException: {}", e.getMessage());
return null;
this.getResult().setException(new Program.Exception().contractValidateException(e));
return Pair.of(false, new DataWord(0).getData());
}
return Pair.of(true, new DataWord(id).getData());
}
Expand Down Expand Up @@ -1058,11 +1067,13 @@ public Pair<Boolean, byte[]> execute(byte[] data) {
} catch (ContractExeException e) {
logger.debug("ContractExeException when calling proposalDeleteContract in vm");
logger.debug("ContractExeException: {}", e.getMessage());
return null;
this.getResult().setException(new Program.Exception().contractExecuteException(e));
return Pair.of(false, new DataWord(0).getData());
} catch (ContractValidateException e) {
logger.debug("ContractValidateException when calling proposalDeleteContract in vm");
logger.debug("ContractValidateException: {}", e.getMessage());
return null;
this.getResult().setException(new Program.Exception().contractValidateException(e));
return Pair.of(false, new DataWord(0).getData());
}
return Pair.of(true, new DataWord(1).getData());
}
Expand Down Expand Up @@ -1181,11 +1192,13 @@ public Pair<Boolean, byte[]> execute(byte[] data) {
} catch (ContractExeException e) {
logger.debug("ContractExeException when calling transferAssetContract in vm");
logger.debug("ContractExeException: {}", e.getMessage());
return null;
this.getResult().setException(new Program.Exception().contractExecuteException(e));
return Pair.of(false, new DataWord(0).getData());
} catch (ContractValidateException e) {
logger.debug("ContractValidateException when calling transferAssetContract in vm");
logger.debug("ContractValidateException: {}", e.getMessage());
return null;
this.getResult().setException(new Program.Exception().contractValidateException(e));
return Pair.of(false, new DataWord(0).getData());
}
return Pair.of(true, new DataWord(1).getData());
}
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/org/tron/common/runtime/vm/program/Program.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.List;
import java.util.Map;
import java.util.NavigableSet;
import java.util.Objects;
import java.util.TreeSet;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
Expand Down Expand Up @@ -69,6 +70,7 @@
import org.tron.core.capsule.ContractCapsule;
import org.tron.core.config.args.Args;
import org.tron.core.exception.ContractValidateException;
import org.tron.core.exception.TronException;
import org.tron.protos.Protocol;
import org.tron.protos.Protocol.SmartContract;

Expand Down Expand Up @@ -1306,6 +1308,9 @@ public void callToPrecompiledAddress(MessageCall msg,
// spend all energy on failure, push zero and revert state changes
this.refundEnergy(0, "call pre-compiled");
this.stackPushZero();
if (Objects.nonNull(this.result.getException())) {
throw result.getException();
}
// deposit.rollback();
}

Expand Down Expand Up @@ -1367,6 +1372,14 @@ public OutOfStorageException(String message, Object... args) {
}
}

@SuppressWarnings("serial")
public static class PrecompiledContractException extends BytecodeExecutionException {

public PrecompiledContractException(String message, Object... args) {
super(format(message, args));
}
}

@SuppressWarnings("serial")
public static class IllegalOperationException extends BytecodeExecutionException {

Expand Down Expand Up @@ -1441,6 +1454,14 @@ public static OutOfStorageException notEnoughStorage() {
return new OutOfStorageException("Not enough ContractState resource");
}

public static PrecompiledContractException contractValidateException(TronException e) {
return new PrecompiledContractException(e.getMessage());
}

public static PrecompiledContractException contractExecuteException(TronException e) {
return new PrecompiledContractException(e.getMessage());
}

public static OutOfEnergyException energyOverflow(BigInteger actualEnergy,
BigInteger energyLimit) {
return new OutOfEnergyException("Energy value overflow: actualEnergy[%d], energyLimit[%d];",
Expand Down

0 comments on commit 833d703

Please sign in to comment.