Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: optimize trigger interface #5079

6 changes: 5 additions & 1 deletion framework/src/main/java/org/tron/core/Wallet.java
Original file line number Diff line number Diff line change
Expand Up @@ -3011,7 +3011,8 @@ public Transaction callConstantContract(TransactionCapsule trxCap,
headBlock = blockCapsuleList.get(0).getInstance();
}

TransactionContext context = new TransactionContext(new BlockCapsule(headBlock), trxCap,
BlockCapsule headBlockCapsule = new BlockCapsule(headBlock);
TransactionContext context = new TransactionContext(headBlockCapsule, trxCap,
StoreFactory.getInstance(), true, false);
VMActuator vmActuator = new VMActuator(true);

Expand All @@ -3028,6 +3029,9 @@ public Transaction callConstantContract(TransactionCapsule trxCap,
TransactionResultCapsule ret = new TransactionResultCapsule();
builder.setEnergyUsed(result.getEnergyUsed());
builder.setEnergyPenalty(result.getEnergyPenaltyTotal());
builder.setBlockNumber(headBlockCapsule.getNum());
builder.setBlockHash(ByteString.copyFrom(headBlockCapsule.getBlockId().getBytes()));
builder.setEnergyPenalty(result.getEnergyPenaltyTotal());
builder.addConstantResult(ByteString.copyFrom(result.getHReturn()));
result.getLogInfoList().forEach(logInfo ->
builder.addLogs(LogInfo.buildLog(logInfo)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@
@Slf4j(topic = "API")
public class TriggerConstantContractServlet extends RateLimiterServlet {

private final String functionSelector = "function_selector";
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;
Expand All @@ -37,13 +41,18 @@ 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 (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.");
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.");
}
}

Expand All @@ -62,17 +71,15 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
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(FUNCTION_SELECTOR));
if (isFunctionSelectorSet) {
String selector = jsonObject.getString(functionSelector);
String parameter = jsonObject.getString("parameter");
data = Util.parseMethod(selector, parameter);
String selector = jsonObject.getString(FUNCTION_SELECTOR);
String parameter = jsonObject.getString(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(), ContractType.TriggerSmartContract);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@
@Slf4j(topic = "API")
public class TriggerSmartContractServlet extends RateLimiterServlet {

private final String functionSelector = "function_selector";
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;
Expand All @@ -37,14 +41,17 @@ 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"))) {
if (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"))) {
if (StringUtil.isNullOrEmpty(jsonObject.getString(CONTRACT_ADDRESS))) {
throw new InvalidParameterException("contract_address isn't 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)
Expand All @@ -62,16 +69,13 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
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(FUNCTION_SELECTOR));
if (isFunctionSelectorSet) {
String selector = jsonObject.getString(functionSelector);
String parameter = jsonObject.getString("parameter");
data = Util.parseMethod(selector, parameter);
String selector = jsonObject.getString(FUNCTION_SELECTOR);
String parameter = jsonObject.getString(FUNCTION_PARAMETER);
String data = Util.parseMethod(selector, parameter);
build.setData(ByteString.copyFrom(ByteArray.fromHexString(data)));
} else {
build.setData(ByteString.copyFrom(new byte[0]));
}

build.setCallTokenValue(Util.getJsonLongValue(jsonObject, "call_token_value"));
Expand Down
2 changes: 2 additions & 0 deletions protocol/src/main/protos/api/api.proto
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,8 @@ message TransactionExtention {
repeated TransactionInfo.Log logs = 6;
repeated InternalTransaction internal_transactions = 7;
int64 energy_penalty = 8;
int64 block_number = 9;
bytes block_hash = 10;
}

message EstimateEnergyMessage {
Expand Down