Skip to content

Commit f7d24a5

Browse files
authored
Merge pull request #1410 from tronprotocol/fix/constant_exception
Polish exception
2 parents a7af7bf + 2cbc74d commit f7d24a5

File tree

2 files changed

+49
-63
lines changed

2 files changed

+49
-63
lines changed

src/main/java/org/tron/common/runtime/Runtime.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,9 @@ private double getThisTxCPULimitInUsRatio() {
293293
/*
294294
**/
295295
private void create()
296-
throws ContractExeException, ContractValidateException {
296+
throws ContractValidateException {
297297
if (!deposit.getDbManager().getDynamicPropertiesStore().supportVM()) {
298-
throw new ContractExeException("vm work is off, need to be opened by the committee");
298+
throw new ContractValidateException("vm work is off, need to be opened by the committee");
299299
}
300300

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

308308
long percent = contract.getNewContract().getConsumeUserResourcePercent();
309309
if (percent < 0 || percent > 100) {
310-
throw new ContractExeException("percent must be >= 0 and <= 100");
310+
throw new ContractValidateException("percent must be >= 0 and <= 100");
311311
}
312312

313313
// insure the new contract address haven't exist
314314
if (deposit.getAccount(contractAddress) != null) {
315-
throw new ContractExeException(
315+
throw new ContractValidateException(
316316
"Trying to create a contract with existing contract address: " + Wallet
317317
.encode58Check(contractAddress));
318318
}
@@ -355,7 +355,7 @@ private void create()
355355
Program.setRootCallConstant(isCallConstant());
356356
} catch (Exception e) {
357357
logger.error(e.getMessage());
358-
throw new ContractExeException(e.getMessage());
358+
throw new ContractValidateException(e.getMessage());
359359
}
360360

361361
program.getResult().setContractAddress(contractAddress);
@@ -380,10 +380,10 @@ private void create()
380380
*/
381381

382382
private void call()
383-
throws ContractExeException, ContractValidateException {
383+
throws ContractValidateException {
384384

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

389389
Contract.TriggerSmartContract contract = ContractCapsule.getTriggerContractFromTransaction(trx);
@@ -413,16 +413,12 @@ private void call()
413413

414414
long feeLimit = trx.getRawData().getFeeLimit();
415415
long energyLimit;
416-
try {
417-
if (isCallConstant(contractAddress)) {
418-
energyLimit = Constant.MAX_ENERGY_IN_TX;
419-
}
420-
else
421-
energyLimit = getEnergyLimit(creator, caller, contract, feeLimit, callValue);
422-
} catch (Exception e) {
423-
logger.error(e.getMessage());
424-
throw new ContractExeException(e.getMessage());
416+
417+
if (isCallConstant(contractAddress)) {
418+
energyLimit = Constant.MAX_ENERGY_IN_TX;
425419
}
420+
else
421+
energyLimit = getEnergyLimit(creator, caller, contract, feeLimit, callValue);
426422

427423
ProgramInvoke programInvoke = programInvokeFactory
428424
.createProgramInvoke(TRX_CONTRACT_CALL_TYPE, executorType, trx,

src/main/java/org/tron/core/Wallet.java

Lines changed: 37 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,8 @@ public Transaction deployContract(CreateSmartContract createSmartContract,
860860

861861
public Transaction triggerContract(TriggerSmartContract triggerSmartContract,
862862
TransactionCapsule trxCap, Builder builder,
863-
Return.Builder retBuilder) throws ContractValidateException {
863+
Return.Builder retBuilder)
864+
throws ContractValidateException, ContractExeException, HeaderNotFound {
864865

865866
ContractStore contractStore = dbManager.getContractStore();
866867
byte[] contractAddress = triggerSmartContract.getContractAddress().toByteArray();
@@ -869,54 +870,46 @@ public Transaction triggerContract(TriggerSmartContract triggerSmartContract,
869870
throw new ContractValidateException("No contract or not a smart contract");
870871
}
871872

872-
try {
873-
byte[] selector = getSelector(triggerSmartContract.getData().toByteArray());
873+
byte[] selector = getSelector(triggerSmartContract.getData().toByteArray());
874874

875-
boolean constant = isConstant(abi, selector);
876-
if (!constant) {
877-
return trxCap.getInstance();
878-
} else {
879-
if (!Args.getInstance().isSupportConstant()) {
880-
throw new ContractValidateException("this node don't support constant");
881-
}
882-
DepositImpl deposit = DepositImpl.createRoot(dbManager);
875+
if (!isConstant(abi, selector)) {
876+
return trxCap.getInstance();
877+
} else {
878+
if (!Args.getInstance().isSupportConstant()) {
879+
throw new ContractValidateException("this node don't support constant");
880+
}
881+
DepositImpl deposit = DepositImpl.createRoot(dbManager);
883882

884-
Block headBlock;
885-
List<BlockCapsule> blockCapsuleList = dbManager.getBlockStore().getBlockByLatestNum(1);
886-
if (CollectionUtils.isEmpty(blockCapsuleList)) {
887-
throw new HeaderNotFound("latest block not found");
888-
} else {
889-
headBlock = blockCapsuleList.get(0).getInstance();
890-
}
883+
Block headBlock;
884+
List<BlockCapsule> blockCapsuleList = dbManager.getBlockStore().getBlockByLatestNum(1);
885+
if (CollectionUtils.isEmpty(blockCapsuleList)) {
886+
throw new HeaderNotFound("latest block not found");
887+
} else {
888+
headBlock = blockCapsuleList.get(0).getInstance();
889+
}
891890

892-
Runtime runtime = new Runtime(trxCap.getInstance(), new BlockCapsule(headBlock), deposit,
893-
new ProgramInvokeFactoryImpl());
894-
runtime.execute();
895-
runtime.go();
896-
runtime.finalization();
897-
// TODO exception
898-
if (runtime.getResult().getException() != null) {
891+
Runtime runtime = new Runtime(trxCap.getInstance(), new BlockCapsule(headBlock), deposit,
892+
new ProgramInvokeFactoryImpl());
893+
runtime.execute();
894+
runtime.go();
895+
runtime.finalization();
896+
// TODO exception
897+
if (runtime.getResult().getException() != null) {
899898
// runtime.getResult().getException().printStackTrace();
900-
throw new RuntimeException("Runtime exe failed!");
901-
}
899+
throw new RuntimeException("Runtime exe failed!");
900+
}
902901

903-
ProgramResult result = runtime.getResult();
904-
TransactionResultCapsule ret = new TransactionResultCapsule();
902+
ProgramResult result = runtime.getResult();
903+
TransactionResultCapsule ret = new TransactionResultCapsule();
905904

906-
builder.addConstantResult(ByteString.copyFrom(result.getHReturn()));
907-
ret.setStatus(0, code.SUCESS);
908-
if (StringUtils.isNoneEmpty(runtime.getRuntimeError())) {
909-
ret.setStatus(0, code.FAILED);
910-
retBuilder.setMessage(ByteString.copyFromUtf8(runtime.getRuntimeError())).build();
911-
}
912-
trxCap.setResult(ret);
913-
return trxCap.getInstance();
905+
builder.addConstantResult(ByteString.copyFrom(result.getHReturn()));
906+
ret.setStatus(0, code.SUCESS);
907+
if (StringUtils.isNoneEmpty(runtime.getRuntimeError())) {
908+
ret.setStatus(0, code.FAILED);
909+
retBuilder.setMessage(ByteString.copyFromUtf8(runtime.getRuntimeError())).build();
914910
}
915-
} catch (ContractValidateException e) {
916-
throw e;
917-
} catch (Exception e) {
918-
logger.error(e.getMessage());
919-
return null;
911+
trxCap.setResult(ret);
912+
return trxCap.getInstance();
920913
}
921914
}
922915

@@ -948,14 +941,11 @@ private static byte[] getSelector(byte[] data) {
948941
return ret;
949942
}
950943

951-
private static boolean isConstant(SmartContract.ABI abi, byte[] selector) throws Exception {
944+
private static boolean isConstant(SmartContract.ABI abi, byte[] selector) {
952945

953-
if (selector == null || abi.getEntrysList().size() == 0) {
946+
if (selector == null || selector.length != 4 || abi.getEntrysList().size() == 0) {
954947
return false;
955948
}
956-
if (selector.length != 4) {
957-
throw new Exception("Selector's length or selector itself is invalid");
958-
}
959949

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

0 commit comments

Comments
 (0)