Skip to content

Commit 6956e37

Browse files
committed
func(interface): do the same modification for estimate energy
1 parent ad30d13 commit 6956e37

4 files changed

Lines changed: 54 additions & 69 deletions

File tree

framework/src/main/java/org/tron/core/services/http/EstimateEnergyServlet.java

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.google.protobuf.ByteString;
55
import io.netty.util.internal.StringUtil;
66
import java.io.IOException;
7-
import java.security.InvalidParameterException;
87
import java.util.stream.Collectors;
98
import javax.servlet.http.HttpServletRequest;
109
import javax.servlet.http.HttpServletResponse;
@@ -25,26 +24,12 @@
2524
@Slf4j(topic = "API")
2625
public class EstimateEnergyServlet extends RateLimiterServlet {
2726

28-
private final String functionSelector = "function_selector";
29-
3027
@Autowired
3128
private Wallet wallet;
3229

3330
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
3431
}
3532

36-
protected void validateParameter(String contract) {
37-
JSONObject jsonObject = JSONObject.parseObject(contract);
38-
if (!jsonObject.containsKey("owner_address")
39-
|| StringUtil.isNullOrEmpty(jsonObject.getString("owner_address"))) {
40-
throw new InvalidParameterException("owner_address isn't set.");
41-
}
42-
if (!jsonObject.containsKey("contract_address")
43-
|| StringUtil.isNullOrEmpty(jsonObject.getString("contract_address"))) {
44-
throw new InvalidParameterException("contract_address isn't set.");
45-
}
46-
}
47-
4833
protected void doPost(HttpServletRequest request, HttpServletResponse response)
4934
throws IOException {
5035
TriggerSmartContract.Builder build = TriggerSmartContract.newBuilder();
@@ -57,21 +42,19 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
5742
.collect(Collectors.joining(System.lineSeparator()));
5843
Util.checkBodySize(contract);
5944
visible = Util.getVisiblePost(contract);
60-
validateParameter(contract);
45+
Util.validateParameter(contract);
6146
JsonFormat.merge(contract, build, visible);
6247
JSONObject jsonObject = JSONObject.parseObject(contract);
6348

64-
boolean isFunctionSelectorSet = jsonObject.containsKey(functionSelector)
65-
&& !StringUtil.isNullOrEmpty(jsonObject.getString(functionSelector));
66-
String data;
49+
boolean isFunctionSelectorSet =
50+
!StringUtil.isNullOrEmpty(jsonObject.getString(Util.FUNCTION_SELECTOR));
6751
if (isFunctionSelectorSet) {
68-
String selector = jsonObject.getString(functionSelector);
69-
String parameter = jsonObject.getString("parameter");
70-
data = Util.parseMethod(selector, parameter);
52+
String selector = jsonObject.getString(Util.FUNCTION_SELECTOR);
53+
String parameter = jsonObject.getString(Util.FUNCTION_PARAMETER);
54+
String data = Util.parseMethod(selector, parameter);
7155
build.setData(ByteString.copyFrom(ByteArray.fromHexString(data)));
72-
} else {
73-
build.setData(ByteString.copyFrom(new byte[0]));
7456
}
57+
7558
TransactionCapsule trxCap = wallet.createTransactionCapsule(build.build(),
7659
Protocol.Transaction.Contract.ContractType.TriggerSmartContract);
7760

framework/src/main/java/org/tron/core/services/http/TriggerConstantContractServlet.java

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.google.protobuf.ByteString;
55
import io.netty.util.internal.StringUtil;
66
import java.io.IOException;
7-
import java.security.InvalidParameterException;
87
import java.util.stream.Collectors;
98
import javax.servlet.http.HttpServletRequest;
109
import javax.servlet.http.HttpServletResponse;
@@ -27,35 +26,12 @@
2726
@Slf4j(topic = "API")
2827
public class TriggerConstantContractServlet extends RateLimiterServlet {
2928

30-
private final String OWNER_ADDRESS = "owner_address";
31-
private final String CONTRACT_ADDRESS = "contract_address";
32-
private final String FUNCTION_SELECTOR = "function_selector";
33-
private final String FUNCTION_PARAMETER = "parameter";
34-
private final String CALL_DATA = "data";
35-
3629
@Autowired
3730
private Wallet wallet;
3831

3932
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
4033
}
4134

42-
protected void validateParameter(String contract) {
43-
JSONObject jsonObject = JSONObject.parseObject(contract);
44-
if (StringUtil.isNullOrEmpty(jsonObject.getString(OWNER_ADDRESS))) {
45-
throw new InvalidParameterException(OWNER_ADDRESS + " isn't set.");
46-
}
47-
if (StringUtil.isNullOrEmpty(jsonObject.getString(CONTRACT_ADDRESS))
48-
&& StringUtil.isNullOrEmpty(jsonObject.getString(CALL_DATA))) {
49-
throw new InvalidParameterException("At least one of "
50-
+ CONTRACT_ADDRESS + " and " + CALL_DATA + " must be set.");
51-
}
52-
if (!StringUtil.isNullOrEmpty(jsonObject.getString(FUNCTION_SELECTOR))
53-
^ StringUtil.isNullOrEmpty(jsonObject.getString(CALL_DATA))) {
54-
throw new InvalidParameterException("Only one of "
55-
+ FUNCTION_SELECTOR + " and " + CALL_DATA + " can be set.");
56-
}
57-
}
58-
5935
protected void doPost(HttpServletRequest request, HttpServletResponse response)
6036
throws IOException {
6137
TriggerSmartContract.Builder build = TriggerSmartContract.newBuilder();
@@ -67,15 +43,15 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
6743
.collect(Collectors.joining(System.lineSeparator()));
6844
Util.checkBodySize(contract);
6945
visible = Util.getVisiblePost(contract);
70-
validateParameter(contract);
46+
Util.validateParameter(contract);
7147
JsonFormat.merge(contract, build, visible);
7248
JSONObject jsonObject = JSONObject.parseObject(contract);
7349

7450
boolean isFunctionSelectorSet =
75-
!StringUtil.isNullOrEmpty(jsonObject.getString(FUNCTION_SELECTOR));
51+
!StringUtil.isNullOrEmpty(jsonObject.getString(Util.FUNCTION_SELECTOR));
7652
if (isFunctionSelectorSet) {
77-
String selector = jsonObject.getString(FUNCTION_SELECTOR);
78-
String parameter = jsonObject.getString(FUNCTION_PARAMETER);
53+
String selector = jsonObject.getString(Util.FUNCTION_SELECTOR);
54+
String parameter = jsonObject.getString(Util.FUNCTION_PARAMETER);
7955
String data = Util.parseMethod(selector, parameter);
8056
build.setData(ByteString.copyFrom(ByteArray.fromHexString(data)));
8157
}

framework/src/main/java/org/tron/core/services/http/TriggerSmartContractServlet.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,30 +27,24 @@
2727
@Slf4j(topic = "API")
2828
public class TriggerSmartContractServlet extends RateLimiterServlet {
2929

30-
private final String OWNER_ADDRESS = "owner_address";
31-
private final String CONTRACT_ADDRESS = "contract_address";
32-
private final String FUNCTION_SELECTOR = "function_selector";
33-
private final String FUNCTION_PARAMETER = "parameter";
34-
private final String CALL_DATA = "data";
35-
3630
@Autowired
3731
private Wallet wallet;
3832

3933
protected void doGet(HttpServletRequest request, HttpServletResponse response) {
4034
}
4135

42-
protected void validateParameter(String contract) {
36+
private void validateParameter(String contract) {
4337
JSONObject jsonObject = JSONObject.parseObject(contract);
44-
if (StringUtil.isNullOrEmpty(jsonObject.getString(OWNER_ADDRESS))) {
45-
throw new InvalidParameterException("owner_address isn't set.");
38+
if (StringUtil.isNullOrEmpty(jsonObject.getString(Util.OWNER_ADDRESS))) {
39+
throw new InvalidParameterException(Util.OWNER_ADDRESS + " isn't set.");
4640
}
47-
if (StringUtil.isNullOrEmpty(jsonObject.getString(CONTRACT_ADDRESS))) {
48-
throw new InvalidParameterException("contract_address isn't set.");
41+
if (StringUtil.isNullOrEmpty(jsonObject.getString(Util.CONTRACT_ADDRESS))) {
42+
throw new InvalidParameterException(Util.CONTRACT_ADDRESS + " isn't set.");
4943
}
50-
if (!StringUtil.isNullOrEmpty(jsonObject.getString(FUNCTION_SELECTOR))
51-
^ StringUtil.isNullOrEmpty(jsonObject.getString(CALL_DATA))) {
44+
if (!StringUtil.isNullOrEmpty(jsonObject.getString(Util.FUNCTION_SELECTOR))
45+
^ StringUtil.isNullOrEmpty(jsonObject.getString(Util.CALL_DATA))) {
5246
throw new InvalidParameterException("Only one of "
53-
+ FUNCTION_SELECTOR + " and " + CALL_DATA + " can be set.");
47+
+ Util.FUNCTION_SELECTOR + " and " + Util.CALL_DATA + " can be set.");
5448
}
5549
}
5650

@@ -70,10 +64,10 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
7064
JSONObject jsonObject = JSONObject.parseObject(contract);
7165

7266
boolean isFunctionSelectorSet =
73-
!StringUtil.isNullOrEmpty(jsonObject.getString(FUNCTION_SELECTOR));
67+
!StringUtil.isNullOrEmpty(jsonObject.getString(Util.FUNCTION_SELECTOR));
7468
if (isFunctionSelectorSet) {
75-
String selector = jsonObject.getString(FUNCTION_SELECTOR);
76-
String parameter = jsonObject.getString(FUNCTION_PARAMETER);
69+
String selector = jsonObject.getString(Util.FUNCTION_SELECTOR);
70+
String parameter = jsonObject.getString(Util.FUNCTION_PARAMETER);
7771
String data = Util.parseMethod(selector, parameter);
7872
build.setData(ByteString.copyFrom(ByteArray.fromHexString(data)));
7973
}

framework/src/main/java/org/tron/core/services/http/Util.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ public class Util {
6565
public static final String EXTRA_DATA = "extra_data";
6666
public static final String PARAMETER = "parameter";
6767

68+
// Used for TVM http interfaces
69+
public static final String OWNER_ADDRESS = "owner_address";
70+
public static final String CONTRACT_ADDRESS = "contract_address";
71+
public static final String FUNCTION_SELECTOR = "function_selector";
72+
public static final String FUNCTION_PARAMETER = "parameter";
73+
public static final String CALL_DATA = "data";
74+
6875
public static String printTransactionFee(String transactionFee) {
6976
JSONObject jsonObject = new JSONObject();
7077
JSONObject receipt = JSONObject.parseObject(transactionFee);
@@ -543,4 +550,29 @@ public static List<Log> convertLogAddressToTronAddress(TransactionInfo transacti
543550
return newLogList;
544551
}
545552

553+
/**
554+
* Validate parameters for trigger constant and estimate energy
555+
* - Rule-1: owner address must be set
556+
* - Rule-2: either contract address is set or call data is set
557+
* - Rule-3: only one of function selector and call data can be set
558+
* @param contract parameters in json format
559+
* @throws InvalidParameterException if validation is not passed, this kind of exception is thrown
560+
*/
561+
public static void validateParameter(String contract) throws InvalidParameterException {
562+
JSONObject jsonObject = JSONObject.parseObject(contract);
563+
if (StringUtils.isEmpty(jsonObject.getString(OWNER_ADDRESS))) {
564+
throw new InvalidParameterException(OWNER_ADDRESS + " isn't set.");
565+
}
566+
if (StringUtils.isEmpty(jsonObject.getString(CONTRACT_ADDRESS))
567+
&& StringUtils.isEmpty(jsonObject.getString(CALL_DATA))) {
568+
throw new InvalidParameterException("At least one of "
569+
+ CONTRACT_ADDRESS + " and " + CALL_DATA + " must be set.");
570+
}
571+
if (!StringUtils.isEmpty(jsonObject.getString(FUNCTION_SELECTOR))
572+
^ StringUtils.isEmpty(jsonObject.getString(CALL_DATA))) {
573+
throw new InvalidParameterException("Only one of "
574+
+ FUNCTION_SELECTOR + " and " + CALL_DATA + " can be set.");
575+
}
576+
}
577+
546578
}

0 commit comments

Comments
 (0)