Skip to content

Commit

Permalink
Merge pull request #4 from cocoleecoco/0.3.0
Browse files Browse the repository at this point in the history
0.3.0
  • Loading branch information
danielgogo authored Jan 18, 2019
2 parents 3f06f8b + 35101cd commit 6dcedbd
Show file tree
Hide file tree
Showing 32 changed files with 2,793 additions and 234 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@

# Use

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

```
<dependency>
<groupId>com.platon.client</groupId>
<artifactId>core</artifactId>
<version>0.2.0</version>
<version>0.3.0</version>
</dependency>
```

or

```
compile "com.platon.client:core:0.2.0"
compile "com.platon.client:core:0.3.0"
```

* use in project
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ configure(subprojects.findAll { it.name != 'integration-tests' }) {
ossrhUsername != '' && ossrhPassword != ''
}

String repoUrl = isSnapshotVersion ? "http://sdk.juzix.net/content/repositories/releases/" : "http://sdk.juzix.net/content/repositories/releases/"
String repoUrl = isSnapshotVersion ? "$mavenReleases" : "$mavenSnapshots";
repository(url: repoUrl) {
authentication(
userName: ossrhUsername,
Expand Down
1 change: 1 addition & 0 deletions codegen/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ dependencies {
compile project(':core'),
"com.squareup:javapoet:$javapoetVersion"
testCompile project(path: ':core', configuration: 'testArtifacts')
testCompile group: 'com.alibaba', name: 'fastjson', version: '1.2.54'
}
61 changes: 45 additions & 16 deletions codegen/src/main/java/org/web3j/codegen/SophiaFunctionWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@
import org.web3j.tx.PlatOnContract;
import org.web3j.tx.TransactionManager;
import org.web3j.tx.gas.ContractGasProvider;
import org.web3j.utils.*;
import org.web3j.utils.Collection;
import org.web3j.utils.Strings;
import org.web3j.utils.Version;
import rx.functions.Func1;

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

public SophiaFunctionWrapper(boolean useNativeJavaTypes) {
this(useNativeJavaTypes, new LogGenerationReporter(LOGGER));
public SophiaFunctionWrapper(boolean useNativeJavaTypes,TXTypeEnum txType) {
this(useNativeJavaTypes, new LogGenerationReporter(LOGGER),txType);
}

SophiaFunctionWrapper(boolean useNativeJavaTypes, GenerationReporter reporter) {
SophiaFunctionWrapper(boolean useNativeJavaTypes, GenerationReporter reporter,TXTypeEnum txType) {
this.useNativeJavaTypes = useNativeJavaTypes;
this.reporter = reporter;
this.txType = txType;
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -128,12 +129,16 @@ void generateJavaFiles(
classBuilder.addMethod(buildLoad(className, TransactionManager.class,
TRANSACTION_MANAGER, true));

addAddressesSupport(classBuilder, addresses);

// 添加获取合约部署数据的方法
classBuilder.addMethod(buildGetDeployData());
// 添加估算合约部署Gas的方法
classBuilder.addMethod(buildGetDeployGasLimit());

addAddressesSupport(classBuilder, addresses);
// 添加自定义交易类型方法
classBuilder.addMethod(buildCustomTransactionType(txType));
// 添加获取交易类型的方法实现
classBuilder.addMethod(buildGetTransactionType());

write(basePackageName, classBuilder.build(), destinationDir);
}
Expand Down Expand Up @@ -506,7 +511,7 @@ private static MethodSpec buildGetInvokeData(String functionName) {
.addParameter(Function.class,functionName)
.returns(String.class);

toReturn.addStatement("return getInvokeData($L)",functionName);
toReturn.addStatement("return $T.invokeEncode($L,getTransactionType($L))", PlatOnUtil.class,functionName,functionName);
return toReturn.build();
}

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

toReturn.addStatement("return getDeployData($L, $L)",CONTRACT_BINARY,ABI);
toReturn.addStatement("return $T.deployEncode($L, $L)",PlatOnUtil.class,CONTRACT_BINARY,ABI);
return toReturn.build();
}

Expand All @@ -537,7 +542,33 @@ private static MethodSpec buildGetDeployGasLimit() {
.addParameter(String.class,CONTRACT_BINARY)
.returns(BigInteger.class)
.addException(IOException.class);
toReturn.addStatement("return getDeployGasLimit($L, $L, $L, $L, $L)",WEB3J,ESTIMATE_GAS_FROM,ESTIMATE_GAS_TO,CONTRACT_BINARY,ABI);
toReturn.addStatement("return $T.estimateGasLimit($L, $L, $L, getDeployData($L))",PlatOnUtil.class,WEB3J,ESTIMATE_GAS_FROM,ESTIMATE_GAS_TO,CONTRACT_BINARY);
return toReturn.build();
}

/**
* 构建获取交易类型的方法
* @return
*/
private static MethodSpec buildGetTransactionType() {
MethodSpec.Builder toReturn = MethodSpec.methodBuilder("getTransactionType")
.addModifiers(Modifier.PROTECTED)
.addParameter(Function.class,"function")
.addStatement("return customTransactionType(function)")
.returns(long.class);
return toReturn.build();
}

/**
* 构建获取自定义交易类型的方法
* @return
*/
private static MethodSpec buildCustomTransactionType(TXTypeEnum txType) {
MethodSpec.Builder toReturn = MethodSpec.methodBuilder("customTransactionType")
.returns(long.class);
toReturn.addModifiers(Modifier.PRIVATE, Modifier.STATIC)
.addParameter(Function.class,"function");
toReturn.addStatement("return $T.$L.type", TXTypeEnum.class,txType.name());
return toReturn.build();
}

Expand Down Expand Up @@ -894,8 +925,7 @@ private MethodSpec buildTransactionFunctionData(AbiDefinition functionDefinition

String functionName = functionDefinition.getName()+"Data";
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(functionName)
.addModifiers(Modifier.STATIC)
.addModifiers(Modifier.PUBLIC);
.addModifiers(Modifier.PUBLIC,Modifier.STATIC);
String inputParams = addParameters(methodBuilder, functionDefinition.getInputs());

if (!functionDefinition.isConstant()) {
Expand All @@ -906,7 +936,7 @@ private MethodSpec buildTransactionFunctionData(AbiDefinition functionDefinition
Function.class, Function.class, funcNameToConst(functionName),
Arrays.class, Type.class, inputParams, Collections.class,
TypeReference.class);
methodBuilder.addStatement("return getInvokeData(function)");
methodBuilder.addStatement("return $T.invokeEncode(function,customTransactionType(function))",PlatOnUtil.class);
return methodBuilder.build();
}
return null;
Expand All @@ -925,8 +955,7 @@ private MethodSpec buildTransactionFunctionGasLimit(AbiDefinition functionDefini
if (!functionDefinition.isConstant()) {
String functionName = functionDefinition.getName()+"GasLimit";
MethodSpec.Builder methodBuilder = MethodSpec.methodBuilder(functionName)
.addModifiers(Modifier.STATIC)
.addModifiers(Modifier.PUBLIC)
.addModifiers(Modifier.PUBLIC,Modifier.STATIC)
.addParameter(Web3j.class,WEB3J)
.addParameter(String.class,ESTIMATE_GAS_FROM)
.addParameter(String.class,ESTIMATE_GAS_TO)
Expand All @@ -935,7 +964,7 @@ private MethodSpec buildTransactionFunctionGasLimit(AbiDefinition functionDefini

methodBuilder.returns(BigInteger.class);
methodBuilder.addStatement("String ethEstimateGasData = $N($L)",functionDefinition.getName()+"Data",getParameterNames(functionDefinition.getInputs()));
methodBuilder.addStatement("return estimateGasLimit($L,$L,$L,ethEstimateGasData)", WEB3J,ESTIMATE_GAS_FROM,ESTIMATE_GAS_TO);
methodBuilder.addStatement("return $T.estimateGasLimit($L,$L,$L,ethEstimateGasData)",PlatOnUtil.class,WEB3J,ESTIMATE_GAS_FROM,ESTIMATE_GAS_TO);
return methodBuilder.build();
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.web3j.utils.Strings;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.web3j.utils.TXTypeEnum;

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

private final String binaryFileLocation;
private final String absFileLocation;
private final String txType;

private SophiaFunctionWrapperGenerator(
String binaryFileLocation,
String absFileLocation,
String destinationDirLocation,
String basePackageName,
String txType,
boolean useJavaNativeTypes) {

super(destinationDirLocation, basePackageName, useJavaNativeTypes);
this.binaryFileLocation = binaryFileLocation;
this.absFileLocation = absFileLocation;
this.txType = txType;
}

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

String[] fullArgs;
if (args.length == 6) {
if (args.length == 7) {
fullArgs = new String[args.length + 1];
fullArgs[0] = JAVA_TYPES_ARG;
System.arraycopy(args, 0, fullArgs, 1, args.length);
} else {
fullArgs = args;
}

if (fullArgs.length != 7) {
if (fullArgs.length != 9) {
exitError(USAGE);
}

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

if (binaryFileLocation.equals("")
|| absFileLocation.equals("")
|| destinationDirLocation.equals("")
|| basePackageName.equals("")) {
|| basePackageName.equals("")
|| txType.equals("")
) {
exitError(USAGE);
}

Expand All @@ -84,6 +93,7 @@ public static void main(String[] args) throws Exception {
absFileLocation,
destinationDirLocation,
basePackageName,
txType,
useJavaNativeTypes)
.generate();
}
Expand All @@ -106,6 +116,7 @@ private void generate() throws IOException, ClassNotFoundException {
if (!absFile.exists() || !absFile.canRead()) {
exitError("Invalid input ABI file specified: " + absFileLocation);
}

String fileName = binaryFile.getName();
String contractName = getFileNameNoExtension(fileName);
byte [] bytes = Files.readBytes(new File(absFile.toURI()));
Expand All @@ -122,8 +133,8 @@ private void generate() throws IOException, ClassNotFoundException {
} else {
String className = Strings.capitaliseFirstLetter(contractName);
System.out.printf("Generating " + basePackageName + "." + className + " ... ");
new SophiaFunctionWrapper(useJavaNativeTypes).generateJavaFiles(
contractName, binaryFile, abi, destinationDirLocation.toString(), basePackageName);

new SophiaFunctionWrapper(useJavaNativeTypes, TXTypeEnum.valueOf(txType.toUpperCase())).generateJavaFiles(contractName, binaryFile, abi, destinationDirLocation.toString(), basePackageName);
System.out.println("File written to " + destinationDirLocation.toString() + "\n");
}
}
Expand Down
42 changes: 0 additions & 42 deletions codegen/src/test/java/org/web3j/codegen/MultisigContractTest.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.junit.Test;
import org.web3j.TempFileProvider;
import org.web3j.utils.Strings;
import org.web3j.utils.TXTypeEnum;


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

testCodeGenerationJvmTypes("contracts", "multisig");
testCodeGenerationSolidityTypes("contracts", "multisig");
testCodeGenerationJvmTypes("contracts", "multisig", TXTypeEnum.WASM.name());
testCodeGenerationJvmTypes("contracts", "candidateContract", TXTypeEnum.WASM.name());
testCodeGenerationJvmTypes("contracts", "ticketContract", TXTypeEnum.WASM.name());
//testCodeGenerationSolidityTypes("contracts", "multisig");
}

private void testCodeGenerationJvmTypes(String contractName, String inputFileName) throws Exception {

testCodeGeneration(contractName, inputFileName, "org.web3j.unittests.java", JAVA_TYPES_ARG);

private void testCodeGenerationJvmTypes(String contractName, String inputFileName, String txType) throws Exception {
testCodeGeneration(contractName, inputFileName, "org.web3j.contract.java", JAVA_TYPES_ARG,txType);
}

private void testCodeGenerationSolidityTypes(
String contractName, String inputFileName) throws Exception {

testCodeGeneration(
contractName, inputFileName, "org.web3j.unittests.solidity", SOLIDITY_TYPES_ARG);
private void testCodeGenerationSolidityTypes(String contractName, String inputFileName,String txType) throws Exception {
testCodeGeneration(contractName, inputFileName, "org.web3j.contract.solidity", SOLIDITY_TYPES_ARG,txType);
}

private void testCodeGeneration(
String contractName, String inputFileName, String packageName, String types)
private void testCodeGeneration(String contractName, String inputFileName, String packageName, String types, String txType)
throws Exception {

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

verifyGeneratedCode(tempDirPath + File.separator
Expand Down
Loading

0 comments on commit 6dcedbd

Please sign in to comment.