From 6956e371bf884a7e630c4e13bbd38063ff7bf73c Mon Sep 17 00:00:00 2001 From: Asuka Date: Mon, 3 Apr 2023 11:49:08 +0800 Subject: [PATCH] func(interface): do the same modification for estimate energy --- .../services/http/EstimateEnergyServlet.java | 31 ++++-------------- .../http/TriggerConstantContractServlet.java | 32 +++---------------- .../http/TriggerSmartContractServlet.java | 28 +++++++--------- .../org/tron/core/services/http/Util.java | 32 +++++++++++++++++++ 4 files changed, 54 insertions(+), 69 deletions(-) diff --git a/framework/src/main/java/org/tron/core/services/http/EstimateEnergyServlet.java b/framework/src/main/java/org/tron/core/services/http/EstimateEnergyServlet.java index 6b82199bd0e..d88f7dd1af1 100644 --- a/framework/src/main/java/org/tron/core/services/http/EstimateEnergyServlet.java +++ b/framework/src/main/java/org/tron/core/services/http/EstimateEnergyServlet.java @@ -4,7 +4,6 @@ import com.google.protobuf.ByteString; import io.netty.util.internal.StringUtil; import java.io.IOException; -import java.security.InvalidParameterException; import java.util.stream.Collectors; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -25,26 +24,12 @@ @Slf4j(topic = "API") public class EstimateEnergyServlet extends RateLimiterServlet { - private final String functionSelector = "function_selector"; - @Autowired private Wallet wallet; protected void doGet(HttpServletRequest request, HttpServletResponse response) { } - protected void validateParameter(String contract) { - JSONObject jsonObject = JSONObject.parseObject(contract); - if (!jsonObject.containsKey("owner_address") - || StringUtil.isNullOrEmpty(jsonObject.getString("owner_address"))) { - throw new InvalidParameterException("owner_address isn't set."); - } - if (!jsonObject.containsKey("contract_address") - || StringUtil.isNullOrEmpty(jsonObject.getString("contract_address"))) { - throw new InvalidParameterException("contract_address isn't set."); - } - } - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { TriggerSmartContract.Builder build = TriggerSmartContract.newBuilder(); @@ -57,21 +42,19 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) .collect(Collectors.joining(System.lineSeparator())); Util.checkBodySize(contract); visible = Util.getVisiblePost(contract); - validateParameter(contract); + Util.validateParameter(contract); JsonFormat.merge(contract, build, visible); JSONObject jsonObject = JSONObject.parseObject(contract); - boolean isFunctionSelectorSet = jsonObject.containsKey(functionSelector) - && !StringUtil.isNullOrEmpty(jsonObject.getString(functionSelector)); - String data; + boolean isFunctionSelectorSet = + !StringUtil.isNullOrEmpty(jsonObject.getString(Util.FUNCTION_SELECTOR)); if (isFunctionSelectorSet) { - String selector = jsonObject.getString(functionSelector); - String parameter = jsonObject.getString("parameter"); - data = Util.parseMethod(selector, parameter); + String selector = jsonObject.getString(Util.FUNCTION_SELECTOR); + String parameter = jsonObject.getString(Util.FUNCTION_PARAMETER); + String data = Util.parseMethod(selector, parameter); build.setData(ByteString.copyFrom(ByteArray.fromHexString(data))); - } else { - build.setData(ByteString.copyFrom(new byte[0])); } + TransactionCapsule trxCap = wallet.createTransactionCapsule(build.build(), Protocol.Transaction.Contract.ContractType.TriggerSmartContract); diff --git a/framework/src/main/java/org/tron/core/services/http/TriggerConstantContractServlet.java b/framework/src/main/java/org/tron/core/services/http/TriggerConstantContractServlet.java index fa3336df5eb..8a46ee1ed74 100644 --- a/framework/src/main/java/org/tron/core/services/http/TriggerConstantContractServlet.java +++ b/framework/src/main/java/org/tron/core/services/http/TriggerConstantContractServlet.java @@ -4,7 +4,6 @@ import com.google.protobuf.ByteString; import io.netty.util.internal.StringUtil; import java.io.IOException; -import java.security.InvalidParameterException; import java.util.stream.Collectors; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -27,35 +26,12 @@ @Slf4j(topic = "API") public class TriggerConstantContractServlet extends RateLimiterServlet { - private final String OWNER_ADDRESS = "owner_address"; - private final String CONTRACT_ADDRESS = "contract_address"; - private final String FUNCTION_SELECTOR = "function_selector"; - private final String FUNCTION_PARAMETER = "parameter"; - private final String CALL_DATA = "data"; - @Autowired private Wallet wallet; protected void doGet(HttpServletRequest request, HttpServletResponse response) { } - protected void validateParameter(String contract) { - JSONObject jsonObject = JSONObject.parseObject(contract); - if (StringUtil.isNullOrEmpty(jsonObject.getString(OWNER_ADDRESS))) { - throw new InvalidParameterException(OWNER_ADDRESS + " isn't set."); - } - if (StringUtil.isNullOrEmpty(jsonObject.getString(CONTRACT_ADDRESS)) - && StringUtil.isNullOrEmpty(jsonObject.getString(CALL_DATA))) { - throw new InvalidParameterException("At least one of " - + CONTRACT_ADDRESS + " and " + CALL_DATA + " must be set."); - } - if (!StringUtil.isNullOrEmpty(jsonObject.getString(FUNCTION_SELECTOR)) - ^ StringUtil.isNullOrEmpty(jsonObject.getString(CALL_DATA))) { - throw new InvalidParameterException("Only one of " - + FUNCTION_SELECTOR + " and " + CALL_DATA + " can be set."); - } - } - protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { TriggerSmartContract.Builder build = TriggerSmartContract.newBuilder(); @@ -67,15 +43,15 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) .collect(Collectors.joining(System.lineSeparator())); Util.checkBodySize(contract); visible = Util.getVisiblePost(contract); - validateParameter(contract); + Util.validateParameter(contract); JsonFormat.merge(contract, build, visible); JSONObject jsonObject = JSONObject.parseObject(contract); boolean isFunctionSelectorSet = - !StringUtil.isNullOrEmpty(jsonObject.getString(FUNCTION_SELECTOR)); + !StringUtil.isNullOrEmpty(jsonObject.getString(Util.FUNCTION_SELECTOR)); if (isFunctionSelectorSet) { - String selector = jsonObject.getString(FUNCTION_SELECTOR); - String parameter = jsonObject.getString(FUNCTION_PARAMETER); + String selector = jsonObject.getString(Util.FUNCTION_SELECTOR); + String parameter = jsonObject.getString(Util.FUNCTION_PARAMETER); String data = Util.parseMethod(selector, parameter); build.setData(ByteString.copyFrom(ByteArray.fromHexString(data))); } diff --git a/framework/src/main/java/org/tron/core/services/http/TriggerSmartContractServlet.java b/framework/src/main/java/org/tron/core/services/http/TriggerSmartContractServlet.java index 5ce27e7cd0b..d8f142edb46 100644 --- a/framework/src/main/java/org/tron/core/services/http/TriggerSmartContractServlet.java +++ b/framework/src/main/java/org/tron/core/services/http/TriggerSmartContractServlet.java @@ -27,30 +27,24 @@ @Slf4j(topic = "API") public class TriggerSmartContractServlet extends RateLimiterServlet { - private final String OWNER_ADDRESS = "owner_address"; - private final String CONTRACT_ADDRESS = "contract_address"; - private final String FUNCTION_SELECTOR = "function_selector"; - private final String FUNCTION_PARAMETER = "parameter"; - private final String CALL_DATA = "data"; - @Autowired private Wallet wallet; protected void doGet(HttpServletRequest request, HttpServletResponse response) { } - protected void validateParameter(String contract) { + private void validateParameter(String contract) { JSONObject jsonObject = JSONObject.parseObject(contract); - if (StringUtil.isNullOrEmpty(jsonObject.getString(OWNER_ADDRESS))) { - throw new InvalidParameterException("owner_address isn't set."); + if (StringUtil.isNullOrEmpty(jsonObject.getString(Util.OWNER_ADDRESS))) { + throw new InvalidParameterException(Util.OWNER_ADDRESS + " isn't set."); } - if (StringUtil.isNullOrEmpty(jsonObject.getString(CONTRACT_ADDRESS))) { - throw new InvalidParameterException("contract_address isn't set."); + if (StringUtil.isNullOrEmpty(jsonObject.getString(Util.CONTRACT_ADDRESS))) { + throw new InvalidParameterException(Util.CONTRACT_ADDRESS + " isn't set."); } - if (!StringUtil.isNullOrEmpty(jsonObject.getString(FUNCTION_SELECTOR)) - ^ StringUtil.isNullOrEmpty(jsonObject.getString(CALL_DATA))) { + if (!StringUtil.isNullOrEmpty(jsonObject.getString(Util.FUNCTION_SELECTOR)) + ^ StringUtil.isNullOrEmpty(jsonObject.getString(Util.CALL_DATA))) { throw new InvalidParameterException("Only one of " - + FUNCTION_SELECTOR + " and " + CALL_DATA + " can be set."); + + Util.FUNCTION_SELECTOR + " and " + Util.CALL_DATA + " can be set."); } } @@ -70,10 +64,10 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response) JSONObject jsonObject = JSONObject.parseObject(contract); boolean isFunctionSelectorSet = - !StringUtil.isNullOrEmpty(jsonObject.getString(FUNCTION_SELECTOR)); + !StringUtil.isNullOrEmpty(jsonObject.getString(Util.FUNCTION_SELECTOR)); if (isFunctionSelectorSet) { - String selector = jsonObject.getString(FUNCTION_SELECTOR); - String parameter = jsonObject.getString(FUNCTION_PARAMETER); + String selector = jsonObject.getString(Util.FUNCTION_SELECTOR); + String parameter = jsonObject.getString(Util.FUNCTION_PARAMETER); String data = Util.parseMethod(selector, parameter); build.setData(ByteString.copyFrom(ByteArray.fromHexString(data))); } diff --git a/framework/src/main/java/org/tron/core/services/http/Util.java b/framework/src/main/java/org/tron/core/services/http/Util.java index 6c8ce0b832c..2e91a5693fc 100644 --- a/framework/src/main/java/org/tron/core/services/http/Util.java +++ b/framework/src/main/java/org/tron/core/services/http/Util.java @@ -65,6 +65,13 @@ public class Util { public static final String EXTRA_DATA = "extra_data"; public static final String PARAMETER = "parameter"; + // Used for TVM http interfaces + public static final String OWNER_ADDRESS = "owner_address"; + public static final String CONTRACT_ADDRESS = "contract_address"; + public static final String FUNCTION_SELECTOR = "function_selector"; + public static final String FUNCTION_PARAMETER = "parameter"; + public static final String CALL_DATA = "data"; + public static String printTransactionFee(String transactionFee) { JSONObject jsonObject = new JSONObject(); JSONObject receipt = JSONObject.parseObject(transactionFee); @@ -543,4 +550,29 @@ public static List convertLogAddressToTronAddress(TransactionInfo transacti return newLogList; } + /** + * Validate parameters for trigger constant and estimate energy + * - Rule-1: owner address must be set + * - Rule-2: either contract address is set or call data is set + * - Rule-3: only one of function selector and call data can be set + * @param contract parameters in json format + * @throws InvalidParameterException if validation is not passed, this kind of exception is thrown + */ + public static void validateParameter(String contract) throws InvalidParameterException { + JSONObject jsonObject = JSONObject.parseObject(contract); + if (StringUtils.isEmpty(jsonObject.getString(OWNER_ADDRESS))) { + throw new InvalidParameterException(OWNER_ADDRESS + " isn't set."); + } + if (StringUtils.isEmpty(jsonObject.getString(CONTRACT_ADDRESS)) + && StringUtils.isEmpty(jsonObject.getString(CALL_DATA))) { + throw new InvalidParameterException("At least one of " + + CONTRACT_ADDRESS + " and " + CALL_DATA + " must be set."); + } + if (!StringUtils.isEmpty(jsonObject.getString(FUNCTION_SELECTOR)) + ^ StringUtils.isEmpty(jsonObject.getString(CALL_DATA))) { + throw new InvalidParameterException("Only one of " + + FUNCTION_SELECTOR + " and " + CALL_DATA + " can be set."); + } + } + }