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

add decodeTransactionInput to ABICodec #360

Merged
merged 1 commit into from Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .ci/ci_check.sh
@@ -1,7 +1,7 @@
#!/bin/bash

set -e
tag="v2.8.0"
tag="v2.7.2"
LOG_INFO() {
local content=${1}
echo -e "\033[32m ${content}\033[0m"
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -37,7 +37,7 @@ ext {
// integrationTest.mustRunAfter test
allprojects {
group = 'org.fisco-bcos.java-sdk'
version = '2.8.0-GMT0018'
version = '2.8.1-SNAPSHOT'
apply plugin: 'maven'
apply plugin: 'maven-publish'
apply plugin: 'idea'
Expand Down
56 changes: 51 additions & 5 deletions sdk-abi/src/main/java/org/fisco/bcos/sdk/abi/ABICodec.java
Expand Up @@ -303,20 +303,44 @@ public List<Object> decodeMethod(String ABI, String methodName, String output)
throws ABICodecException {
return decodeMethodAndGetOutputObject(ABI, methodName, output).getLeft();
}
/**
* decode the input string into json
*
* @param input the transaction input
* @return the decoded json string of the input
*/
public List<String> decodeTransactionInputToString(String ABI, String input)
throws ABICodecException {
String inputWithPrefix = addHexPrefixToString(input);
String methodId = inputWithPrefix.substring(0, 10);
return decodeMethodByIdToString(ABI, methodId, input.substring(10), false);
}

public List<Object> decodeMethodById(String ABI, String methodId, String output)
public Pair<List<Object>, List<ABIObject>> decodeTransactionInput(String ABI, String input)
throws ABICodecException {
String inputWithPrefix = addHexPrefixToString(input);
String methodId = inputWithPrefix.substring(0, 10);
return decodeDataByMethodId(ABI, methodId, input.substring(10), false);
}

public Pair<List<Object>, List<ABIObject>> decodeDataByMethodId(
String ABI, String methodId, String data, boolean isOutput) throws ABICodecException {
ContractABIDefinition contractABIDefinition = abiDefinitionFactory.loadABI(ABI);
ABIDefinition abiDefinition = contractABIDefinition.getABIDefinitionByMethodId(methodId);
if (abiDefinition == null) {
String errorMsg = " methodId " + methodId + " is invalid";
logger.error(errorMsg);
throw new ABICodecException(errorMsg);
}
ABIObject outputABIObject = abiObjectFactory.createOutputObject(abiDefinition);
ABIObject outputABIObject = null;
if (isOutput) {
outputABIObject = abiObjectFactory.createOutputObject(abiDefinition);
} else {
outputABIObject = abiObjectFactory.createInputObject(abiDefinition);
}
ABICodecObject abiCodecObject = new ABICodecObject();
try {
return abiCodecObject.decodeJavaObject(outputABIObject, output);
return abiCodecObject.decodeJavaObjectAndOutputObject(outputABIObject, data);
} catch (Exception e) {
logger.error(" exception in decodeMethodByIdToObject : {}", e.getMessage());
}
Expand All @@ -326,6 +350,11 @@ public List<Object> decodeMethodById(String ABI, String methodId, String output)
throw new ABICodecException(errorMsg);
}

public List<Object> decodeMethodById(String ABI, String methodId, String output)
throws ABICodecException {
return decodeDataByMethodId(ABI, methodId, output, true).getLeft();
}

public List<Object> decodeMethodByInterface(String ABI, String methodInterface, String output)
throws ABICodecException {
FunctionEncoder functionEncoder = new FunctionEncoder(cryptoSuite);
Expand Down Expand Up @@ -361,17 +390,27 @@ public List<String> decodeMethodToString(String ABI, String methodName, String o

public List<String> decodeMethodByIdToString(String ABI, String methodId, String output)
throws ABICodecException {
return decodeMethodByIdToString(ABI, methodId, output, true);
}

public List<String> decodeMethodByIdToString(
String ABI, String methodId, String data, boolean isOutput) throws ABICodecException {
ContractABIDefinition contractABIDefinition = abiDefinitionFactory.loadABI(ABI);
ABIDefinition abiDefinition = contractABIDefinition.getABIDefinitionByMethodId(methodId);
if (abiDefinition == null) {
String errorMsg = " methodId " + methodId + " is invalid";
logger.error(errorMsg);
throw new ABICodecException(errorMsg);
}
ABIObject outputABIObject = abiObjectFactory.createOutputObject(abiDefinition);
ABIObject outputABIObject = null;
if (isOutput) {
outputABIObject = abiObjectFactory.createOutputObject(abiDefinition);
} else {
outputABIObject = abiObjectFactory.createInputObject(abiDefinition);
}
ABICodecJsonWrapper abiCodecJsonWrapper = new ABICodecJsonWrapper();
try {
return abiCodecJsonWrapper.decode(outputABIObject, output);
return abiCodecJsonWrapper.decode(outputABIObject, data);
} catch (UnsupportedOperationException e) {
logger.error(" exception in decodeMethodByIdToString : {}", e.getMessage());
}
Expand Down Expand Up @@ -513,6 +552,13 @@ public List<String> decodeEventByInterfaceToString(
return decodeEventByTopicToString(ABI, methodId, log);
}

private String addHexPrefixToString(String s) {
if (!s.startsWith("0x")) {
return "0x" + s;
}
return s;
}

private List<Object> mergeEventParamsAndTopics(
ABIDefinition abiDefinition, List<Object> params, List<String> topics) {
List<Object> ret = new ArrayList<>();
Expand Down
Expand Up @@ -72,6 +72,15 @@ public TransactionResponse decodeReceiptWithValues(
String abi, String functionName, TransactionReceipt transactionReceipt)
throws IOException, ABICodecException, TransactionException {
TransactionResponse response = decodeReceiptWithoutValues(abi, transactionReceipt);
// parse the input
if (transactionReceipt.getInput() != null) {
Pair<List<Object>, List<ABIObject>> inputObject =
abiCodec.decodeTransactionInput(abi, transactionReceipt.getInput());
String inputValues = JsonUtils.toJson(inputObject.getLeft());
response.setInputData(inputValues);
response.setInputObject(inputObject.getLeft());
response.setInputABIObject(inputObject.getRight());
}
// only successful tx has return values.
if (transactionReceipt.getStatus().equals("0x0")) {
Pair<List<Object>, List<ABIObject>> returnObject =
Expand Down
Expand Up @@ -37,6 +37,10 @@ public class TransactionResponse extends CommonResponse {
private List<Object> returnObject;
private List<ABIObject> returnABIObject;

private String inputData;
private List<Object> inputObject;
private List<ABIObject> inputABIObject;

public TransactionResponse() {
super();
}
Expand Down Expand Up @@ -134,4 +138,28 @@ public List<ABIObject> getReturnABIObject() {
public void setReturnABIObject(List<ABIObject> returnABIObject) {
this.returnABIObject = returnABIObject;
}

public List<Object> getInputObject() {
return inputObject;
}

public void setInputObject(List<Object> inputObject) {
this.inputObject = inputObject;
}

public List<ABIObject> getInputABIObject() {
return inputABIObject;
}

public void setInputABIObject(List<ABIObject> inputABIObject) {
this.inputABIObject = inputABIObject;
}

public String getInputData() {
return inputData;
}

public void setInputData(String inputData) {
this.inputData = inputData;
}
}