Skip to content

Commit

Permalink
func(interface): do the same modification for estimate energy
Browse files Browse the repository at this point in the history
  • Loading branch information
yanghang8612 committed Apr 3, 2023
1 parent ad30d13 commit 6956e37
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}

Expand All @@ -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)));
}
Expand Down
32 changes: 32 additions & 0 deletions framework/src/main/java/org/tron/core/services/http/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -543,4 +550,29 @@ public static List<Log> 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.");
}
}

}

0 comments on commit 6956e37

Please sign in to comment.