Skip to content

Commit 13604c3

Browse files
authored
Merge pull request #566 from dalaocu/master-2.0
fix bug of constructor args decoding & add maven mirror repos
2 parents d5ae0ac + fd38ae0 commit 13604c3

File tree

10 files changed

+140
-3
lines changed

10 files changed

+140
-3
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ allprojects {
6363
// In this section you declare where to find the dependencies of your project
6464
repositories {
6565
mavenCentral()
66+
maven { url "http://mirrors.cloud.tencent.com/nexus/repository/maven-public/" }
67+
maven { url "https://mirrors.huaweicloud.com/repository/maven/" }
6668
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
6769
maven { url "https://oss.sonatype.org/service/local/staging/deploy/maven2"}
6870
maven { url "https://oss.sonatype.org/content/repositories/snapshots" }

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,26 @@ public Pair<List<Object>, List<ABIObject>> decodeTransactionInput(String ABI, St
324324
return decodeDataByMethodId(ABI, methodId, input.substring(10), false);
325325
}
326326

327+
public Pair<List<Object>, List<ABIObject>> decodeConstructorInput(
328+
String ABI, String binary, String input, String methodName) 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 paramsInput = StringUtils.substringAfter(input, binary);
335+
// remove methodId of input
336+
return abiCodecObject.decodeJavaObjectAndOutputObject(
337+
abiObjectFactory.createInputObject(contractABIDefinition.getConstructor()),
338+
paramsInput);
339+
}
340+
String errorMsg = " cannot decode in decodeConstructorInput with appropriate interface ABI";
341+
logger.error(errorMsg);
342+
throw new ABICodecException(errorMsg);
343+
}
344+
345+
/* This method can only used in transaction send. Do not use in constructor!
346+
* In case of extends constructor, it won't work correct. */
327347
public Pair<List<Object>, List<ABIObject>> decodeMethodInput(
328348
String ABI, String input, String methodName, String code) throws ABICodecException {
329349
ContractABIDefinition contractABIDefinition = abiDefinitionFactory.loadABI(ABI);

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public TransactionResponse decodeReceiptWithValues(
5959
throws TransactionException, IOException, ABICodecException;
6060

6161
/**
62-
* parse the transaction information from receipt without return values
62+
* parse the transaction information from receipt without return values NOTE: Only used to
63+
* decode transaction. If you want to decode constructor, please see decodeConstructorReceipt.
6364
*
6465
* @param abi contract abi
6566
* @param transactionReceipt transaction receipt
@@ -69,6 +70,18 @@ public TransactionResponse decodeReceiptWithoutValues(
6970
String abi, TransactionReceipt transactionReceipt)
7071
throws TransactionException, IOException, ABICodecException;
7172

73+
/**
74+
* parse the transaction information from receipt without return values
75+
*
76+
* @param abi contract abi
77+
* @param binary contract binary
78+
* @param transactionReceipt transaction receipt
79+
* @return the resolved status and other transaction detail
80+
*/
81+
TransactionResponse decodeConstructorReceipt(
82+
String abi, String binary, TransactionReceipt transactionReceipt)
83+
throws TransactionException, IOException, ABICodecException;
84+
7285
/**
7386
* parse the transaction information from receipt without return values, but with input values
7487
*

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ public TransactionResponse decodeReceiptWithoutValues(
110110
return response;
111111
}
112112

113+
/*
114+
* In case of extends constructor, it won't work correct. */
115+
@Deprecated
113116
@Override
114117
public TransactionResponse decodeReceiptWithoutOutputValues(
115118
String abi, TransactionReceipt transactionReceipt, String constructorCode)
@@ -128,6 +131,24 @@ public TransactionResponse decodeReceiptWithoutOutputValues(
128131
return response;
129132
}
130133

134+
@Override
135+
public TransactionResponse decodeConstructorReceipt(
136+
String abi, String binary, TransactionReceipt transactionReceipt)
137+
throws TransactionException, IOException, ABICodecException {
138+
TransactionResponse response = decodeReceiptWithoutValues(abi, transactionReceipt);
139+
// parse the input
140+
if (transactionReceipt.getInput() != null && transactionReceipt.isStatusOK()) {
141+
Pair<List<Object>, List<ABIObject>> inputObject =
142+
abiCodec.decodeConstructorInput(
143+
abi, binary, transactionReceipt.getInput(), "constructor");
144+
String inputValues = JsonUtils.toJson(inputObject.getLeft());
145+
response.setInputData(inputValues);
146+
response.setInputObject(inputObject.getLeft());
147+
response.setInputABIObject(inputObject.getRight());
148+
}
149+
return response;
150+
}
151+
131152
@Override
132153
public TransactionResponse decodeReceiptStatus(TransactionReceipt receipt) {
133154
TransactionResponse response = new TransactionResponse();

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,21 @@ public TransactionReceipt deployAndGetReceipt(String data) {
101101
}
102102

103103
@Override
104+
public TransactionResponse deployAndGetResponse(String abi, String binary, String signedData) {
105+
TransactionReceipt receipt = transactionPusher.push(signedData);
106+
try {
107+
return transactionDecoder.decodeConstructorReceipt(abi, binary, receipt);
108+
} catch (TransactionException | IOException | ABICodecException e) {
109+
log.error("decode transaction receipt exception: {}", e);
110+
return new TransactionResponse(
111+
receipt, ResultCodeEnum.EXCEPTION_OCCUR.getCode(), e.getMessage());
112+
}
113+
}
114+
115+
/*
116+
* In case of extends constructor, it won't work correct. */
117+
@Override
118+
@Deprecated
104119
public TransactionResponse deployAndGetResponse(String abi, String signedData) {
105120
TransactionReceipt receipt = transactionPusher.push(signedData);
106121

@@ -127,14 +142,15 @@ public TransactionResponse deployAndGetResponse(String abi, String signedData) {
127142
@Override
128143
public TransactionResponse deployAndGetResponse(String abi, String bin, List<Object> params)
129144
throws ABICodecException {
130-
return deployAndGetResponse(abi, createSignedConstructor(abi, bin, params));
145+
return deployAndGetResponse(abi, bin, createSignedConstructor(abi, bin, params));
131146
}
132147

133148
@Override
134149
public TransactionResponse deployAndGetResponseWithStringParams(
135150
String abi, String bin, List<String> params) throws ABICodecException {
136151
return deployAndGetResponse(
137152
abi,
153+
bin,
138154
createSignedTransaction(
139155
null,
140156
abiCodec.encodeConstructorFromString(abi, bin, params),

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,19 @@ public interface AssembleTransactionProcessorInterface {
5353
* @param signedData signed and encoded constructor data
5454
* @return transaction response @See TransactionResponse
5555
*/
56+
@Deprecated
5657
public TransactionResponse deployAndGetResponse(String abi, String signedData);
5758

59+
/**
60+
* deploy contract to fisco bcos node and get response.
61+
*
62+
* @param abi contract abi, which could be obtained by compiling solidity contract.
63+
* @param bin contract binary, which could be obtained by compiling solidity contract.
64+
* @param signedData signed and encoded constructor data
65+
* @return transaction response @See TransactionResponse
66+
*/
67+
public TransactionResponse deployAndGetResponse(String abi, String bin, String signedData);
68+
5869
/**
5970
* deploy contract to fisco bcos node and get response.
6071
*

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public AssembleTransactionWithRemoteSignProcessor(
5151
@Override
5252
public TransactionResponse deployAndGetResponse(String abi, String bin, List<Object> params)
5353
throws ABICodecException {
54-
return deployAndGetResponse(abi, createSignedConstructor(abi, bin, params));
54+
return deployAndGetResponse(abi, bin, createSignedConstructor(abi, bin, params));
5555
}
5656

5757
@Override
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.fisco.bcos.sdk.transaction.manager;
2+
3+
import com.google.common.collect.Lists;
4+
import java.io.File;
5+
import java.util.List;
6+
import org.apache.commons.io.FileUtils;
7+
import org.fisco.bcos.sdk.BcosSDK;
8+
import org.fisco.bcos.sdk.client.Client;
9+
import org.fisco.bcos.sdk.crypto.keypair.CryptoKeyPair;
10+
import org.fisco.bcos.sdk.model.ConstantConfig;
11+
import org.fisco.bcos.sdk.transaction.model.dto.TransactionResponse;
12+
import org.junit.Assert;
13+
import org.junit.Test;
14+
15+
public class ContractConstructorTest {
16+
17+
private static final String configFile =
18+
"src/integration-test/resources/" + ConstantConfig.CONFIG_FILE_NAME;
19+
private static final String abiFile = "src/integration-test/resources/abi/";
20+
private static final String binFile = "src/integration-test/resources/bin/";
21+
// init the sdk, and set the config options.
22+
private BcosSDK sdk = BcosSDK.build(configFile);
23+
// group 1
24+
private Client client = sdk.getClient(Integer.valueOf(1));
25+
private CryptoKeyPair cryptoKeyPair = client.getCryptoSuite().createKeyPair();
26+
27+
@Test
28+
public void test2ComplexDeploy() throws Exception {
29+
AssembleTransactionProcessor transactionProcessor =
30+
TransactionProcessorFactory.createAssembleTransactionProcessor(
31+
client, cryptoKeyPair, abiFile, binFile);
32+
// deploy
33+
List<Object> params = Lists.newArrayList();
34+
params.add("1");
35+
params.add("2");
36+
params.add("3");
37+
params.add("4");
38+
39+
String abi = FileUtils.readFileToString(new File(abiFile + "CheckInfoManager.abi"));
40+
String bin = FileUtils.readFileToString(new File(binFile + "CheckInfoManager.bin"));
41+
42+
TransactionResponse txResponse =
43+
transactionProcessor.deployAndGetResponse(abi, bin, params);
44+
// System.out.println(JsonUtils.toJson(txResponse));
45+
Assert.assertEquals("0x0", txResponse.getTransactionReceipt().getStatus());
46+
47+
TransactionResponse response =
48+
transactionProcessor.deployByContractLoader("CheckInfoManager", params);
49+
// System.out.println(JsonUtils.toJson(response));
50+
Assert.assertEquals("0x0", response.getTransactionReceipt().getStatus());
51+
}
52+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"meta","type":"address"}],"name":"setMetaAddress","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"check_code","type":"int256"},{"name":"org_id","type":"bytes32"}],"name":"getClearCenterCheckInfo","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"currentCheckCode","outputs":[{"name":"","type":"int256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"check_info_meta","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"check_code","type":"int256"},{"name":"org_id","type":"bytes32"}],"name":"getIssueBankCheckInfo","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"orgId","type":"bytes32"},{"name":"checkCode","type":"int256"}],"name":"getOrgCheckStatus","outputs":[{"name":"","type":"int256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"abi","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"orgId","type":"bytes32"},{"name":"checkCode","type":"int256"},{"name":"retStatus","type":"int256"}],"name":"setOrgCheckStatus","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getMeta","outputs":[{"name":"","type":"string"},{"name":"","type":"string"},{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"check_code","type":"int256"},{"name":"org_id","type":"bytes32"}],"name":"getCheckInfo","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"check_code","type":"int256"},{"name":"org_id","type":"bytes32"}],"name":"getAcquirerBankCheckInfo","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"check_code","type":"int256"}],"name":"setCheckCode","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"name","type":"string"},{"name":"abi","type":"string"},{"name":"check_info_name","type":"string"},{"name":"check_info_abi","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]

src/integration-test/resources/bin/CheckInfoManager.bin

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)