Skip to content

Commit 6dcedbd

Browse files
authored
Merge pull request #4 from cocoleecoco/0.3.0
0.3.0
2 parents 3f06f8b + 35101cd commit 6dcedbd

32 files changed

Lines changed: 2793 additions & 234 deletions

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@
1212

1313
# Use
1414

15-
* config maven repository: http://sdk.platon.network/content/groups/public/
15+
* config maven repository: https://sdk.platon.network/nexus/content/groups/public/
1616
* config maven or gradle in project
1717

1818
```
1919
<dependency>
2020
<groupId>com.platon.client</groupId>
2121
<artifactId>core</artifactId>
22-
<version>0.2.0</version>
22+
<version>0.3.0</version>
2323
</dependency>
2424
```
2525

2626
or
2727

2828
```
29-
compile "com.platon.client:core:0.2.0"
29+
compile "com.platon.client:core:0.3.0"
3030
```
3131

3232
* use in project

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ configure(subprojects.findAll { it.name != 'integration-tests' }) {
154154
ossrhUsername != '' && ossrhPassword != ''
155155
}
156156

157-
String repoUrl = isSnapshotVersion ? "http://sdk.juzix.net/content/repositories/releases/" : "http://sdk.juzix.net/content/repositories/releases/"
157+
String repoUrl = isSnapshotVersion ? "$mavenReleases" : "$mavenSnapshots";
158158
repository(url: repoUrl) {
159159
authentication(
160160
userName: ossrhUsername,

codegen/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ dependencies {
55
compile project(':core'),
66
"com.squareup:javapoet:$javapoetVersion"
77
testCompile project(path: ':core', configuration: 'testArtifacts')
8+
testCompile group: 'com.alibaba', name: 'fastjson', version: '1.2.54'
89
}

codegen/src/main/java/org/web3j/codegen/SophiaFunctionWrapper.java

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@
2222
import org.web3j.tx.PlatOnContract;
2323
import org.web3j.tx.TransactionManager;
2424
import org.web3j.tx.gas.ContractGasProvider;
25+
import org.web3j.utils.*;
2526
import org.web3j.utils.Collection;
26-
import org.web3j.utils.Strings;
27-
import org.web3j.utils.Version;
2827
import rx.functions.Func1;
2928

3029
import javax.lang.model.element.Modifier;
@@ -75,14 +74,16 @@ public class SophiaFunctionWrapper extends Generator {
7574
private static final String regex = "(\\w+)(?:\\[(.*?)\\])(?:\\[(.*?)\\])?";
7675
private static final Pattern pattern = Pattern.compile(regex);
7776
private final GenerationReporter reporter;
77+
private final TXTypeEnum txType;
7878

79-
public SophiaFunctionWrapper(boolean useNativeJavaTypes) {
80-
this(useNativeJavaTypes, new LogGenerationReporter(LOGGER));
79+
public SophiaFunctionWrapper(boolean useNativeJavaTypes,TXTypeEnum txType) {
80+
this(useNativeJavaTypes, new LogGenerationReporter(LOGGER),txType);
8181
}
8282

83-
SophiaFunctionWrapper(boolean useNativeJavaTypes, GenerationReporter reporter) {
83+
SophiaFunctionWrapper(boolean useNativeJavaTypes, GenerationReporter reporter,TXTypeEnum txType) {
8484
this.useNativeJavaTypes = useNativeJavaTypes;
8585
this.reporter = reporter;
86+
this.txType = txType;
8687
}
8788

8889
@SuppressWarnings("unchecked")
@@ -128,12 +129,16 @@ void generateJavaFiles(
128129
classBuilder.addMethod(buildLoad(className, TransactionManager.class,
129130
TRANSACTION_MANAGER, true));
130131

132+
addAddressesSupport(classBuilder, addresses);
133+
131134
// 添加获取合约部署数据的方法
132135
classBuilder.addMethod(buildGetDeployData());
133136
// 添加估算合约部署Gas的方法
134137
classBuilder.addMethod(buildGetDeployGasLimit());
135-
136-
addAddressesSupport(classBuilder, addresses);
138+
// 添加自定义交易类型方法
139+
classBuilder.addMethod(buildCustomTransactionType(txType));
140+
// 添加获取交易类型的方法实现
141+
classBuilder.addMethod(buildGetTransactionType());
137142

138143
write(basePackageName, classBuilder.build(), destinationDir);
139144
}
@@ -506,7 +511,7 @@ private static MethodSpec buildGetInvokeData(String functionName) {
506511
.addParameter(Function.class,functionName)
507512
.returns(String.class);
508513

509-
toReturn.addStatement("return getInvokeData($L)",functionName);
514+
toReturn.addStatement("return $T.invokeEncode($L,getTransactionType($L))", PlatOnUtil.class,functionName,functionName);
510515
return toReturn.build();
511516
}
512517

@@ -520,7 +525,7 @@ private static MethodSpec buildGetDeployData() {
520525
.addParameter(String.class,CONTRACT_BINARY)
521526
.returns(String.class);
522527

523-
toReturn.addStatement("return getDeployData($L, $L)",CONTRACT_BINARY,ABI);
528+
toReturn.addStatement("return $T.deployEncode($L, $L)",PlatOnUtil.class,CONTRACT_BINARY,ABI);
524529
return toReturn.build();
525530
}
526531

@@ -537,7 +542,33 @@ private static MethodSpec buildGetDeployGasLimit() {
537542
.addParameter(String.class,CONTRACT_BINARY)
538543
.returns(BigInteger.class)
539544
.addException(IOException.class);
540-
toReturn.addStatement("return getDeployGasLimit($L, $L, $L, $L, $L)",WEB3J,ESTIMATE_GAS_FROM,ESTIMATE_GAS_TO,CONTRACT_BINARY,ABI);
545+
toReturn.addStatement("return $T.estimateGasLimit($L, $L, $L, getDeployData($L))",PlatOnUtil.class,WEB3J,ESTIMATE_GAS_FROM,ESTIMATE_GAS_TO,CONTRACT_BINARY);
546+
return toReturn.build();
547+
}
548+
549+
/**
550+
* 构建获取交易类型的方法
551+
* @return
552+
*/
553+
private static MethodSpec buildGetTransactionType() {
554+
MethodSpec.Builder toReturn = MethodSpec.methodBuilder("getTransactionType")
555+
.addModifiers(Modifier.PROTECTED)
556+
.addParameter(Function.class,"function")
557+
.addStatement("return customTransactionType(function)")
558+
.returns(long.class);
559+
return toReturn.build();
560+
}
561+
562+
/**
563+
* 构建获取自定义交易类型的方法
564+
* @return
565+
*/
566+
private static MethodSpec buildCustomTransactionType(TXTypeEnum txType) {
567+
MethodSpec.Builder toReturn = MethodSpec.methodBuilder("customTransactionType")
568+
.returns(long.class);
569+
toReturn.addModifiers(Modifier.PRIVATE, Modifier.STATIC)
570+
.addParameter(Function.class,"function");
571+
toReturn.addStatement("return $T.$L.type", TXTypeEnum.class,txType.name());
541572
return toReturn.build();
542573
}
543574

@@ -894,8 +925,7 @@ private MethodSpec buildTransactionFunctionData(AbiDefinition functionDefinition
894925

895926
String functionName = functionDefinition.getName()+"Data";
896927
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(functionName)
897-
.addModifiers(Modifier.STATIC)
898-
.addModifiers(Modifier.PUBLIC);
928+
.addModifiers(Modifier.PUBLIC,Modifier.STATIC);
899929
String inputParams = addParameters(methodBuilder, functionDefinition.getInputs());
900930

901931
if (!functionDefinition.isConstant()) {
@@ -906,7 +936,7 @@ private MethodSpec buildTransactionFunctionData(AbiDefinition functionDefinition
906936
Function.class, Function.class, funcNameToConst(functionName),
907937
Arrays.class, Type.class, inputParams, Collections.class,
908938
TypeReference.class);
909-
methodBuilder.addStatement("return getInvokeData(function)");
939+
methodBuilder.addStatement("return $T.invokeEncode(function,customTransactionType(function))",PlatOnUtil.class);
910940
return methodBuilder.build();
911941
}
912942
return null;
@@ -925,8 +955,7 @@ private MethodSpec buildTransactionFunctionGasLimit(AbiDefinition functionDefini
925955
if (!functionDefinition.isConstant()) {
926956
String functionName = functionDefinition.getName()+"GasLimit";
927957
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(functionName)
928-
.addModifiers(Modifier.STATIC)
929-
.addModifiers(Modifier.PUBLIC)
958+
.addModifiers(Modifier.PUBLIC,Modifier.STATIC)
930959
.addParameter(Web3j.class,WEB3J)
931960
.addParameter(String.class,ESTIMATE_GAS_FROM)
932961
.addParameter(String.class,ESTIMATE_GAS_TO)
@@ -935,7 +964,7 @@ private MethodSpec buildTransactionFunctionGasLimit(AbiDefinition functionDefini
935964

936965
methodBuilder.returns(BigInteger.class);
937966
methodBuilder.addStatement("String ethEstimateGasData = $N($L)",functionDefinition.getName()+"Data",getParameterNames(functionDefinition.getInputs()));
938-
methodBuilder.addStatement("return estimateGasLimit($L,$L,$L,ethEstimateGasData)", WEB3J,ESTIMATE_GAS_FROM,ESTIMATE_GAS_TO);
967+
methodBuilder.addStatement("return $T.estimateGasLimit($L,$L,$L,ethEstimateGasData)",PlatOnUtil.class,WEB3J,ESTIMATE_GAS_FROM,ESTIMATE_GAS_TO);
939968
return methodBuilder.build();
940969
}
941970
return null;

codegen/src/main/java/org/web3j/codegen/SophiaFunctionWrapperGenerator.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.web3j.utils.Strings;
1616

1717
import com.fasterxml.jackson.databind.ObjectMapper;
18+
import org.web3j.utils.TXTypeEnum;
1819

1920
/**
2021
* Java wrapper source code generator for Solidity ABI format.
@@ -25,21 +26,26 @@ public class SophiaFunctionWrapperGenerator extends FunctionWrapperGenerator {
2526
+ "[--javaTypes|--solidityTypes] "
2627
+ "<input binary file>.bin <input abi file>.abi "
2728
+ "-p|--package <base package name> "
28-
+ "-o|--output <destination base directory>";
29+
+ "-o|--output <destination base directory>"
30+
+ "-t|--txType <Transaction Type: [wasm|mpc|default]>"
31+
;
2932

3033
private final String binaryFileLocation;
3134
private final String absFileLocation;
35+
private final String txType;
3236

3337
private SophiaFunctionWrapperGenerator(
3438
String binaryFileLocation,
3539
String absFileLocation,
3640
String destinationDirLocation,
3741
String basePackageName,
42+
String txType,
3843
boolean useJavaNativeTypes) {
3944

4045
super(destinationDirLocation, basePackageName, useJavaNativeTypes);
4146
this.binaryFileLocation = binaryFileLocation;
4247
this.absFileLocation = absFileLocation;
48+
this.txType = txType;
4349
}
4450

4551
public static void run(String[] args) throws Exception {
@@ -53,15 +59,15 @@ public static void run(String[] args) throws Exception {
5359
public static void main(String[] args) throws Exception {
5460

5561
String[] fullArgs;
56-
if (args.length == 6) {
62+
if (args.length == 7) {
5763
fullArgs = new String[args.length + 1];
5864
fullArgs[0] = JAVA_TYPES_ARG;
5965
System.arraycopy(args, 0, fullArgs, 1, args.length);
6066
} else {
6167
fullArgs = args;
6268
}
6369

64-
if (fullArgs.length != 7) {
70+
if (fullArgs.length != 9) {
6571
exitError(USAGE);
6672
}
6773

@@ -71,11 +77,14 @@ public static void main(String[] args) throws Exception {
7177
String absFileLocation = parsePositionalArg(fullArgs, 2);
7278
String destinationDirLocation = parseParameterArgument(fullArgs, "-o", "--outputDir");
7379
String basePackageName = parseParameterArgument(fullArgs, "-p", "--package");
80+
String txType = parseParameterArgument(fullArgs, "-t", "--txType");
7481

7582
if (binaryFileLocation.equals("")
7683
|| absFileLocation.equals("")
7784
|| destinationDirLocation.equals("")
78-
|| basePackageName.equals("")) {
85+
|| basePackageName.equals("")
86+
|| txType.equals("")
87+
) {
7988
exitError(USAGE);
8089
}
8190

@@ -84,6 +93,7 @@ public static void main(String[] args) throws Exception {
8493
absFileLocation,
8594
destinationDirLocation,
8695
basePackageName,
96+
txType,
8797
useJavaNativeTypes)
8898
.generate();
8999
}
@@ -106,6 +116,7 @@ private void generate() throws IOException, ClassNotFoundException {
106116
if (!absFile.exists() || !absFile.canRead()) {
107117
exitError("Invalid input ABI file specified: " + absFileLocation);
108118
}
119+
109120
String fileName = binaryFile.getName();
110121
String contractName = getFileNameNoExtension(fileName);
111122
byte [] bytes = Files.readBytes(new File(absFile.toURI()));
@@ -122,8 +133,8 @@ private void generate() throws IOException, ClassNotFoundException {
122133
} else {
123134
String className = Strings.capitaliseFirstLetter(contractName);
124135
System.out.printf("Generating " + basePackageName + "." + className + " ... ");
125-
new SophiaFunctionWrapper(useJavaNativeTypes).generateJavaFiles(
126-
contractName, binaryFile, abi, destinationDirLocation.toString(), basePackageName);
136+
137+
new SophiaFunctionWrapper(useJavaNativeTypes, TXTypeEnum.valueOf(txType.toUpperCase())).generateJavaFiles(contractName, binaryFile, abi, destinationDirLocation.toString(), basePackageName);
127138
System.out.println("File written to " + destinationDirLocation.toString() + "\n");
128139
}
129140
}

codegen/src/test/java/org/web3j/codegen/MultisigContractTest.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

codegen/src/test/java/org/web3j/codegen/SophiaFunctionWrapperGeneratorTest.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.junit.Test;
1919
import org.web3j.TempFileProvider;
2020
import org.web3j.utils.Strings;
21+
import org.web3j.utils.TXTypeEnum;
2122

2223

2324
public class SophiaFunctionWrapperGeneratorTest extends TempFileProvider {
@@ -37,35 +38,33 @@ public void testHumanStandardTokenGeneration() throws Exception {
3738
// testCodeGenerationJvmTypes("contracts", "token");
3839
// testCodeGenerationSolidityTypes("contracts", "token");
3940

40-
testCodeGenerationJvmTypes("contracts", "multisig");
41-
testCodeGenerationSolidityTypes("contracts", "multisig");
41+
testCodeGenerationJvmTypes("contracts", "multisig", TXTypeEnum.WASM.name());
42+
testCodeGenerationJvmTypes("contracts", "candidateContract", TXTypeEnum.WASM.name());
43+
testCodeGenerationJvmTypes("contracts", "ticketContract", TXTypeEnum.WASM.name());
44+
//testCodeGenerationSolidityTypes("contracts", "multisig");
4245
}
4346

44-
private void testCodeGenerationJvmTypes(String contractName, String inputFileName) throws Exception {
45-
46-
testCodeGeneration(contractName, inputFileName, "org.web3j.unittests.java", JAVA_TYPES_ARG);
47-
47+
private void testCodeGenerationJvmTypes(String contractName, String inputFileName, String txType) throws Exception {
48+
testCodeGeneration(contractName, inputFileName, "org.web3j.contract.java", JAVA_TYPES_ARG,txType);
4849
}
4950

50-
private void testCodeGenerationSolidityTypes(
51-
String contractName, String inputFileName) throws Exception {
52-
53-
testCodeGeneration(
54-
contractName, inputFileName, "org.web3j.unittests.solidity", SOLIDITY_TYPES_ARG);
51+
private void testCodeGenerationSolidityTypes(String contractName, String inputFileName,String txType) throws Exception {
52+
testCodeGeneration(contractName, inputFileName, "org.web3j.contract.solidity", SOLIDITY_TYPES_ARG,txType);
5553
}
5654

57-
private void testCodeGeneration(
58-
String contractName, String inputFileName, String packageName, String types)
55+
private void testCodeGeneration(String contractName, String inputFileName, String packageName, String types, String txType)
5956
throws Exception {
6057

58+
//tempDirPath = "D:\\Workspace\\client-sdk-java\\codegen\\src\\test\\java";
6159
SophiaFunctionWrapperGenerator.main(Arrays.asList(
6260
types,
6361
solidityBaseDir + File.separator + contractName + File.separator
6462
+ "build" + File.separator + inputFileName + ".wasm",
6563
solidityBaseDir + File.separator + contractName + File.separator
6664
+ "build" + File.separator + inputFileName + ".cpp.abi.json",
6765
"-p", packageName,
68-
"-o", tempDirPath
66+
"-o", tempDirPath,
67+
"-t", txType
6968
).toArray(new String[0])); // https://shipilev.net/blog/2016/arrays-wisdom-ancients/
7069

7170
verifyGeneratedCode(tempDirPath + File.separator

0 commit comments

Comments
 (0)