From 2cbc74dbf5372fb51e7e44346d4c533ad4d28d2e Mon Sep 17 00:00:00 2001 From: ashu Date: Wed, 29 Aug 2018 17:35:55 +0800 Subject: [PATCH] Polish exception --- .../java/org/tron/common/runtime/Runtime.java | 28 +++---- src/main/java/org/tron/core/Wallet.java | 84 ++++++++----------- 2 files changed, 49 insertions(+), 63 deletions(-) diff --git a/src/main/java/org/tron/common/runtime/Runtime.java b/src/main/java/org/tron/common/runtime/Runtime.java index 8536a0ca070..df860a59430 100644 --- a/src/main/java/org/tron/common/runtime/Runtime.java +++ b/src/main/java/org/tron/common/runtime/Runtime.java @@ -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); @@ -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)); } @@ -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); @@ -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); @@ -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, diff --git a/src/main/java/org/tron/core/Wallet.java b/src/main/java/org/tron/core/Wallet.java index 083db86b939..3ccb0b52fdf 100755 --- a/src/main/java/org/tron/core/Wallet.java +++ b/src/main/java/org/tron/core/Wallet.java @@ -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(); @@ -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 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 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(); } } @@ -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);