Skip to content
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
28 changes: 12 additions & 16 deletions src/main/java/org/tron/common/runtime/Runtime.java
Original file line number Diff line number Diff line change
Expand Up @@ -293,9 +293,9 @@ private double getThisTxCPULimitInUsRatio() {
/*
**/
private void create()
throws ContractExeException, ContractValidateException {
throws ContractValidateException {
if (!deposit.getDbManager().getDynamicPropertiesStore().supportVM()) {
throw new ContractExeException("vm work is off, need to be opened by the committee");
throw new ContractValidateException("vm work is off, need to be opened by the committee");
}

CreateSmartContract contract = ContractCapsule.getSmartContractFromTransaction(trx);
Expand All @@ -307,12 +307,12 @@ private void create()

long percent = contract.getNewContract().getConsumeUserResourcePercent();
if (percent < 0 || percent > 100) {
throw new ContractExeException("percent must be >= 0 and <= 100");
throw new ContractValidateException("percent must be >= 0 and <= 100");
}

// insure the new contract address haven't exist
if (deposit.getAccount(contractAddress) != null) {
throw new ContractExeException(
throw new ContractValidateException(
"Trying to create a contract with existing contract address: " + Wallet
.encode58Check(contractAddress));
}
Expand Down Expand Up @@ -355,7 +355,7 @@ private void create()
Program.setRootCallConstant(isCallConstant());
} catch (Exception e) {
logger.error(e.getMessage());
throw new ContractExeException(e.getMessage());
throw new ContractValidateException(e.getMessage());
}

program.getResult().setContractAddress(contractAddress);
Expand All @@ -380,10 +380,10 @@ private void create()
*/

private void call()
throws ContractExeException, ContractValidateException {
throws ContractValidateException {

if (!deposit.getDbManager().getDynamicPropertiesStore().supportVM()) {
throw new ContractExeException("VM work is off, need to be opened by the committee");
throw new ContractValidateException("VM work is off, need to be opened by the committee");
}

Contract.TriggerSmartContract contract = ContractCapsule.getTriggerContractFromTransaction(trx);
Expand Down Expand Up @@ -411,16 +411,12 @@ private void call()

long feeLimit = trx.getRawData().getFeeLimit();
long energyLimit;
try {
if (isCallConstant(contractAddress)) {
energyLimit = Constant.MAX_ENERGY_IN_TX;
}
else
energyLimit = getEnergyLimit(creator, caller, contract, feeLimit, callValue);
} catch (Exception e) {
logger.error(e.getMessage());
throw new ContractExeException(e.getMessage());

if (isCallConstant(contractAddress)) {
energyLimit = Constant.MAX_ENERGY_IN_TX;
}
else
energyLimit = getEnergyLimit(creator, caller, contract, feeLimit, callValue);

ProgramInvoke programInvoke = programInvokeFactory
.createProgramInvoke(TRX_CONTRACT_CALL_TYPE, executorType, trx,
Expand Down
84 changes: 37 additions & 47 deletions src/main/java/org/tron/core/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,8 @@ public Transaction deployContract(CreateSmartContract createSmartContract,

public Transaction triggerContract(TriggerSmartContract triggerSmartContract,
TransactionCapsule trxCap, Builder builder,
Return.Builder retBuilder) throws ContractValidateException {
Return.Builder retBuilder)
throws ContractValidateException, ContractExeException, HeaderNotFound {

ContractStore contractStore = dbManager.getContractStore();
byte[] contractAddress = triggerSmartContract.getContractAddress().toByteArray();
Expand All @@ -843,54 +844,46 @@ public Transaction triggerContract(TriggerSmartContract triggerSmartContract,
throw new ContractValidateException("No contract or not a smart contract");
}

try {
byte[] selector = getSelector(triggerSmartContract.getData().toByteArray());
byte[] selector = getSelector(triggerSmartContract.getData().toByteArray());

boolean constant = isConstant(abi, selector);
if (!constant) {
return trxCap.getInstance();
} else {
if (!Args.getInstance().isSupportConstant()) {
throw new ContractValidateException("this node don't support constant");
}
DepositImpl deposit = DepositImpl.createRoot(dbManager);
if (!isConstant(abi, selector)) {
return trxCap.getInstance();
} else {
if (!Args.getInstance().isSupportConstant()) {
throw new ContractValidateException("this node don't support constant");
}
DepositImpl deposit = DepositImpl.createRoot(dbManager);

Block headBlock;
List<BlockCapsule> blockCapsuleList = dbManager.getBlockStore().getBlockByLatestNum(1);
if (CollectionUtils.isEmpty(blockCapsuleList)) {
throw new HeaderNotFound("latest block not found");
} else {
headBlock = blockCapsuleList.get(0).getInstance();
}
Block headBlock;
List<BlockCapsule> blockCapsuleList = dbManager.getBlockStore().getBlockByLatestNum(1);
if (CollectionUtils.isEmpty(blockCapsuleList)) {
throw new HeaderNotFound("latest block not found");
} else {
headBlock = blockCapsuleList.get(0).getInstance();
}

Runtime runtime = new Runtime(trxCap.getInstance(), new BlockCapsule(headBlock), deposit,
new ProgramInvokeFactoryImpl());
runtime.execute();
runtime.go();
runtime.finalization();
// TODO exception
if (runtime.getResult().getException() != null) {
Runtime runtime = new Runtime(trxCap.getInstance(), new BlockCapsule(headBlock), deposit,
new ProgramInvokeFactoryImpl());
runtime.execute();
runtime.go();
runtime.finalization();
// TODO exception
if (runtime.getResult().getException() != null) {
// runtime.getResult().getException().printStackTrace();
throw new RuntimeException("Runtime exe failed!");
}
throw new RuntimeException("Runtime exe failed!");
}

ProgramResult result = runtime.getResult();
TransactionResultCapsule ret = new TransactionResultCapsule();
ProgramResult result = runtime.getResult();
TransactionResultCapsule ret = new TransactionResultCapsule();

builder.addConstantResult(ByteString.copyFrom(result.getHReturn()));
ret.setStatus(0, code.SUCESS);
if (StringUtils.isNoneEmpty(runtime.getRuntimeError())) {
ret.setStatus(0, code.FAILED);
retBuilder.setMessage(ByteString.copyFromUtf8(runtime.getRuntimeError())).build();
}
trxCap.setResult(ret);
return trxCap.getInstance();
builder.addConstantResult(ByteString.copyFrom(result.getHReturn()));
ret.setStatus(0, code.SUCESS);
if (StringUtils.isNoneEmpty(runtime.getRuntimeError())) {
ret.setStatus(0, code.FAILED);
retBuilder.setMessage(ByteString.copyFromUtf8(runtime.getRuntimeError())).build();
}
} catch (ContractValidateException e) {
throw e;
} catch (Exception e) {
logger.error(e.getMessage());
return null;
trxCap.setResult(ret);
return trxCap.getInstance();
}
}

Expand Down Expand Up @@ -922,14 +915,11 @@ private static byte[] getSelector(byte[] data) {
return ret;
}

private static boolean isConstant(SmartContract.ABI abi, byte[] selector) throws Exception {
private static boolean isConstant(SmartContract.ABI abi, byte[] selector) {

if (selector == null || abi.getEntrysList().size() == 0) {
if (selector == null || selector.length != 4 || abi.getEntrysList().size() == 0) {
return false;
}
if (selector.length != 4) {
throw new Exception("Selector's length or selector itself is invalid");
}

for (int i = 0; i < abi.getEntrysCount(); i++) {
ABI.Entry entry = abi.getEntrys(i);
Expand Down