Skip to content

Commit 3343bd7

Browse files
authored
Constructor input parser & add test cases of input parse (#361)
* add test of input parser * add test * fix ci problems
1 parent 54757b9 commit 3343bd7

File tree

8 files changed

+100
-7
lines changed

8 files changed

+100
-7
lines changed

sdk-abi/src/main/java/org/fisco/bcos/sdk/abi/ABICodec.java

+31
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.io.IOException;
1919
import java.util.ArrayList;
2020
import java.util.List;
21+
import org.apache.commons.lang3.StringUtils;
2122
import org.apache.commons.lang3.tuple.Pair;
2223
import org.fisco.bcos.sdk.abi.wrapper.ABICodecJsonWrapper;
2324
import org.fisco.bcos.sdk.abi.wrapper.ABICodecObject;
@@ -323,6 +324,36 @@ public Pair<List<Object>, List<ABIObject>> decodeTransactionInput(String ABI, St
323324
return decodeDataByMethodId(ABI, methodId, input.substring(10), false);
324325
}
325326

327+
public Pair<List<Object>, List<ABIObject>> decodeMethodInput(
328+
String ABI, String input, String methodName, String code) throws ABICodecException {
329+
ContractABIDefinition contractABIDefinition = abiDefinitionFactory.loadABI(ABI);
330+
List<ABIDefinition> methods;
331+
ABICodecObject abiCodecObject = new ABICodecObject();
332+
ABIObjectFactory abiObjectFactory = new ABIObjectFactory();
333+
if (StringUtils.equals(methodName, "constructor")) {
334+
String lastCode = StringUtils.substring(code, code.length() - 32, code.length());
335+
String paramsInput = StringUtils.substringAfter(input, lastCode);
336+
// remove methodId of input
337+
return abiCodecObject.decodeJavaObjectAndOutputObject(
338+
abiObjectFactory.createInputObject(contractABIDefinition.getConstructor()),
339+
paramsInput);
340+
} else {
341+
methods = contractABIDefinition.getFunctions().get(methodName);
342+
}
343+
for (ABIDefinition abiDefinition : methods) {
344+
ABIObject outputABIObject = abiObjectFactory.createInputObject(abiDefinition);
345+
try {
346+
return abiCodecObject.decodeJavaObjectAndOutputObject(
347+
outputABIObject, input.substring(10));
348+
} catch (Exception e) {
349+
logger.warn(" exception in decodeMethodInput : {}", e.getMessage());
350+
}
351+
}
352+
String errorMsg = " cannot decode in decodeMethodInput with appropriate interface ABI";
353+
logger.error(errorMsg);
354+
throw new ABICodecException(errorMsg);
355+
}
356+
326357
public Pair<List<Object>, List<ABIObject>> decodeDataByMethodId(
327358
String ABI, String methodId, String data, boolean isOutput) throws ABICodecException {
328359
ContractABIDefinition contractABIDefinition = abiDefinitionFactory.loadABI(ABI);

sdk-transaction/src/main/java/org/fisco/bcos/sdk/transaction/codec/decode/TransactionDecoderInterface.java

+12
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ public TransactionResponse decodeReceiptWithoutValues(
6969
String abi, TransactionReceipt transactionReceipt)
7070
throws TransactionException, IOException, ABICodecException;
7171

72+
/**
73+
* parse the transaction information from receipt without return values, but with input values
74+
*
75+
* @param abi contract abi
76+
* @param transactionReceipt transaction receipt
77+
* @param constructorCode decode for constructor
78+
* @return the resolved status and other transaction detail
79+
*/
80+
public TransactionResponse decodeReceiptWithoutOutputValues(
81+
String abi, TransactionReceipt transactionReceipt, String constructorCode)
82+
throws TransactionException, IOException, ABICodecException;
83+
7284
/**
7385
* parse the transaction events from receipt logs
7486
*

sdk-transaction/src/main/java/org/fisco/bcos/sdk/transaction/codec/decode/TransactionDecoderService.java

+18
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,24 @@ public TransactionResponse decodeReceiptWithoutValues(
110110
return response;
111111
}
112112

113+
@Override
114+
public TransactionResponse decodeReceiptWithoutOutputValues(
115+
String abi, TransactionReceipt transactionReceipt, String constructorCode)
116+
throws TransactionException, IOException, ABICodecException {
117+
TransactionResponse response = decodeReceiptWithoutValues(abi, transactionReceipt);
118+
// parse the input
119+
if (transactionReceipt.getInput() != null && transactionReceipt.isStatusOK()) {
120+
Pair<List<Object>, List<ABIObject>> inputObject =
121+
abiCodec.decodeMethodInput(
122+
abi, transactionReceipt.getInput(), "constructor", constructorCode);
123+
String inputValues = JsonUtils.toJson(inputObject.getLeft());
124+
response.setInputData(inputValues);
125+
response.setInputObject(inputObject.getLeft());
126+
response.setInputABIObject(inputObject.getRight());
127+
}
128+
return response;
129+
}
130+
113131
@Override
114132
public TransactionResponse decodeReceiptStatus(TransactionReceipt receipt) {
115133
TransactionResponse response = new TransactionResponse();

sdk-transaction/src/main/java/org/fisco/bcos/sdk/transaction/manager/AssembleTransactionProcessor.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ public TransactionReceipt deployAndGetReceipt(String data) {
103103
@Override
104104
public TransactionResponse deployAndGetResponse(String abi, String signedData) {
105105
TransactionReceipt receipt = transactionPusher.push(signedData);
106+
String code = client.getCode(receipt.getContractAddress()).getCode();
106107
try {
107-
return transactionDecoder.decodeReceiptWithoutValues(abi, receipt);
108+
return transactionDecoder.decodeReceiptWithoutOutputValues(abi, receipt, code);
108109
} catch (TransactionException | IOException | ABICodecException e) {
109110
log.error("deploy exception: {}", e.getMessage());
110111
return new TransactionResponse(

src/integration-test/java/org/fisco/bcos/sdk/transaction/decoder/TransactionDecoderServiceTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public void testDecode() throws Exception {
6565
return;
6666
}
6767
String contractAddress = response.getContractAddress();
68+
Assert.assertEquals(2, response.getInputObject().size());
69+
// System.out.println(JsonUtils.toJson(response));
6870

6971
// increment
7072
TransactionReceipt transactionReceipt =

src/integration-test/java/org/fisco/bcos/sdk/transaction/manager/AssembleTransactionProcessorTest.java

+32-3
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ public void test1HelloWorld() throws Exception {
101101
transactionProcessor.sendTransactionAndGetResponseByContractLoader(
102102
"HelloWorld", helloWorldAddrss, "set", params);
103103
Assert.assertEquals("0x0", res.getTransactionReceipt().getStatus());
104+
Assert.assertEquals("test", res.getInputObject().get(0));
105+
// System.out.println(JsonUtils.toJson(res));
104106

105107
// test call by contract loader
106108
CallResponse callResponse2 =
@@ -293,7 +295,6 @@ public void onResponse(TransactionReceipt receipt) {
293295
abi,
294296
"getUint256",
295297
Lists.newArrayList());
296-
// System.out.println(JsonUtils.toJson(callResponse3));
297298
Assert.assertEquals("Success", callResponse3.getReturnMessage());
298299
} catch (TransactionBaseException | ABICodecException e) {
299300
System.out.println(e.getMessage());
@@ -313,6 +314,7 @@ public void test6ComplexSetValues() throws Exception {
313314
params.add("test2");
314315
TransactionResponse response =
315316
transactionProcessor.deployByContractLoader("ComplexSol", params);
317+
// System.out.println(JsonUtils.toJson(response));
316318
if (!response.getTransactionReceipt().getStatus().equals("0x0")) {
317319
return;
318320
}
@@ -322,14 +324,14 @@ public void test6ComplexSetValues() throws Exception {
322324
String[] o = {"0x1", "0x2", "0x3"};
323325
List<String> a = Arrays.asList(o);
324326
paramsSetValues.add(a);
325-
paramsSetValues.add("set values 字符串");
327+
paramsSetValues.add("set values 字符");
326328
TransactionResponse transactionResponse =
327329
transactionProcessor.sendTransactionAndGetResponse(
328330
contractAddress, abi, "setValues", paramsSetValues);
329331
// System.out.println(JsonUtils.toJson(transactionResponse));
330332
Map<String, List<List<Object>>> eventsMap = transactionResponse.getEventResultMap();
331333
Assert.assertEquals(1, eventsMap.size());
332-
Assert.assertEquals("set values 字符串", eventsMap.get("LogSetValues").get(0).get(2));
334+
Assert.assertEquals("set values 字符", eventsMap.get("LogSetValues").get(0).get(2));
333335
}
334336

335337
@Test
@@ -401,4 +403,31 @@ public void test8ComplexSetBytesFuture() throws Exception {
401403
Assert.assertEquals("0x0", response.getTransactionReceipt().getStatus());
402404
});
403405
}
406+
407+
@Test
408+
public void test9ComplexIncrementInputParser() throws Exception {
409+
AssembleTransactionProcessor transactionProcessor =
410+
TransactionProcessorFactory.createAssembleTransactionProcessor(
411+
client, cryptoKeyPair, abiFile, binFile);
412+
// deploy
413+
List<Object> params = Lists.newArrayList();
414+
params.add(1);
415+
params.add("test2");
416+
TransactionResponse response =
417+
transactionProcessor.deployByContractLoader("ComplexSol", params);
418+
if (!response.getTransactionReceipt().getStatus().equals("0x0")) {
419+
return;
420+
}
421+
Assert.assertEquals(2, response.getInputABIObject().size());
422+
Assert.assertEquals("test2", response.getInputObject().get(1));
423+
String contractAddress = response.getContractAddress();
424+
// increment v
425+
TransactionResponse transactionResponse =
426+
transactionProcessor.sendTransactionAndGetResponse(
427+
contractAddress,
428+
abi,
429+
"incrementUint256",
430+
Lists.newArrayList(BigInteger.valueOf(10)));
431+
Assert.assertEquals(BigInteger.valueOf(10), transactionResponse.getInputObject().get(0));
432+
}
404433
}

src/integration-test/java/org/fisco/bcos/sdk/transaction/manager/AssembleTransactionWithRemoteSignProcessorTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class AssembleTransactionWithRemoteSignProcessorTest {
4848
private static final String abiFile = "src/integration-test/resources/abi/";
4949
private static final String binFile = "src/integration-test/resources/bin/";
5050
private List<Object> params = Lists.newArrayList("test");
51-
// prepare sdk read from the config file
51+
// prepare sdk, read from the config file
5252
private BcosSDK sdk = BcosSDK.build(configFile);
5353
// set the group number 1
5454
private Client client = sdk.getClient(Integer.valueOf(1));

src/integration-test/java/org/fisco/bcos/sdk/transaction/mock/RemoteSignCallbackMock.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ public RemoteSignCallbackMock(
2121
}
2222

2323
/**
24-
* 签名结果回调的实现
24+
* callback of sign transaction
2525
*
2626
* @param signatureStr 签名服务回调返回的签名结果串
2727
* @return *
2828
*/
2929
@Override
3030
public int handleSignedTransaction(SignatureResult signatureStr) {
3131
System.out.println(System.currentTimeMillis() + " SignatureResult: " + signatureStr);
32-
// 完成了交易签名后,将其发送出去
32+
// after sign, send it
3333
TransactionReceipt tr =
3434
assembleTransactionWithRemoteSignProcessor.signAndPush(
3535
rawTransaction, signatureStr.convertToString());

0 commit comments

Comments
 (0)