Skip to content

Commit 2cbc74d

Browse files
committed
Polish exception
1 parent 342b71d commit 2cbc74d

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);
@@ -411,16 +411,12 @@ private void call()
411411

412412
long feeLimit = trx.getRawData().getFeeLimit();
413413
long energyLimit;
414-
try {
415-
if (isCallConstant(contractAddress)) {
416-
energyLimit = Constant.MAX_ENERGY_IN_TX;
417-
}
418-
else
419-
energyLimit = getEnergyLimit(creator, caller, contract, feeLimit, callValue);
420-
} catch (Exception e) {
421-
logger.error(e.getMessage());
422-
throw new ContractExeException(e.getMessage());
414+
415+
if (isCallConstant(contractAddress)) {
416+
energyLimit = Constant.MAX_ENERGY_IN_TX;
423417
}
418+
else
419+
energyLimit = getEnergyLimit(creator, caller, contract, feeLimit, callValue);
424420

425421
ProgramInvoke programInvoke = programInvokeFactory
426422
.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
@@ -834,7 +834,8 @@ public Transaction deployContract(CreateSmartContract createSmartContract,
834834

835835
public Transaction triggerContract(TriggerSmartContract triggerSmartContract,
836836
TransactionCapsule trxCap, Builder builder,
837-
Return.Builder retBuilder) throws ContractValidateException {
837+
Return.Builder retBuilder)
838+
throws ContractValidateException, ContractExeException, HeaderNotFound {
838839

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

846-
try {
847-
byte[] selector = getSelector(triggerSmartContract.getData().toByteArray());
847+
byte[] selector = getSelector(triggerSmartContract.getData().toByteArray());
848848

849-
boolean constant = isConstant(abi, selector);
850-
if (!constant) {
851-
return trxCap.getInstance();
852-
} else {
853-
if (!Args.getInstance().isSupportConstant()) {
854-
throw new ContractValidateException("this node don't support constant");
855-
}
856-
DepositImpl deposit = DepositImpl.createRoot(dbManager);
849+
if (!isConstant(abi, selector)) {
850+
return trxCap.getInstance();
851+
} else {
852+
if (!Args.getInstance().isSupportConstant()) {
853+
throw new ContractValidateException("this node don't support constant");
854+
}
855+
DepositImpl deposit = DepositImpl.createRoot(dbManager);
857856

858-
Block headBlock;
859-
List<BlockCapsule> blockCapsuleList = dbManager.getBlockStore().getBlockByLatestNum(1);
860-
if (CollectionUtils.isEmpty(blockCapsuleList)) {
861-
throw new HeaderNotFound("latest block not found");
862-
} else {
863-
headBlock = blockCapsuleList.get(0).getInstance();
864-
}
857+
Block headBlock;
858+
List<BlockCapsule> blockCapsuleList = dbManager.getBlockStore().getBlockByLatestNum(1);
859+
if (CollectionUtils.isEmpty(blockCapsuleList)) {
860+
throw new HeaderNotFound("latest block not found");
861+
} else {
862+
headBlock = blockCapsuleList.get(0).getInstance();
863+
}
865864

866-
Runtime runtime = new Runtime(trxCap.getInstance(), new BlockCapsule(headBlock), deposit,
867-
new ProgramInvokeFactoryImpl());
868-
runtime.execute();
869-
runtime.go();
870-
runtime.finalization();
871-
// TODO exception
872-
if (runtime.getResult().getException() != null) {
865+
Runtime runtime = new Runtime(trxCap.getInstance(), new BlockCapsule(headBlock), deposit,
866+
new ProgramInvokeFactoryImpl());
867+
runtime.execute();
868+
runtime.go();
869+
runtime.finalization();
870+
// TODO exception
871+
if (runtime.getResult().getException() != null) {
873872
// runtime.getResult().getException().printStackTrace();
874-
throw new RuntimeException("Runtime exe failed!");
875-
}
873+
throw new RuntimeException("Runtime exe failed!");
874+
}
876875

877-
ProgramResult result = runtime.getResult();
878-
TransactionResultCapsule ret = new TransactionResultCapsule();
876+
ProgramResult result = runtime.getResult();
877+
TransactionResultCapsule ret = new TransactionResultCapsule();
879878

880-
builder.addConstantResult(ByteString.copyFrom(result.getHReturn()));
881-
ret.setStatus(0, code.SUCESS);
882-
if (StringUtils.isNoneEmpty(runtime.getRuntimeError())) {
883-
ret.setStatus(0, code.FAILED);
884-
retBuilder.setMessage(ByteString.copyFromUtf8(runtime.getRuntimeError())).build();
885-
}
886-
trxCap.setResult(ret);
887-
return trxCap.getInstance();
879+
builder.addConstantResult(ByteString.copyFrom(result.getHReturn()));
880+
ret.setStatus(0, code.SUCESS);
881+
if (StringUtils.isNoneEmpty(runtime.getRuntimeError())) {
882+
ret.setStatus(0, code.FAILED);
883+
retBuilder.setMessage(ByteString.copyFromUtf8(runtime.getRuntimeError())).build();
888884
}
889-
} catch (ContractValidateException e) {
890-
throw e;
891-
} catch (Exception e) {
892-
logger.error(e.getMessage());
893-
return null;
885+
trxCap.setResult(ret);
886+
return trxCap.getInstance();
894887
}
895888
}
896889

@@ -922,14 +915,11 @@ private static byte[] getSelector(byte[] data) {
922915
return ret;
923916
}
924917

925-
private static boolean isConstant(SmartContract.ABI abi, byte[] selector) throws Exception {
918+
private static boolean isConstant(SmartContract.ABI abi, byte[] selector) {
926919

927-
if (selector == null || abi.getEntrysList().size() == 0) {
920+
if (selector == null || selector.length != 4 || abi.getEntrysList().size() == 0) {
928921
return false;
929922
}
930-
if (selector.length != 4) {
931-
throw new Exception("Selector's length or selector itself is invalid");
932-
}
933923

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

0 commit comments

Comments
 (0)