From ced2b33d02dcac1fe558d5a55e8df183f8a51f05 Mon Sep 17 00:00:00 2001 From: kyonRay Date: Thu, 28 Dec 2023 15:45:18 +0800 Subject: [PATCH] (V3): fix get raw function interface bug, fix transaction version option conflict. --- .../org/fisco/bcos/codegen/CodeGenMain.java | 23 ++++++-- .../codegen/v3/wrapper/ContractGenerator.java | 8 +-- .../codegen/v3/wrapper/ContractWrapper.java | 59 +++++++++++++++---- .../bcos/codegen/v3/test/CodeGenV3Test.java | 3 +- 4 files changed, 73 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/fisco/bcos/codegen/CodeGenMain.java b/src/main/java/org/fisco/bcos/codegen/CodeGenMain.java index df59062..f980190 100644 --- a/src/main/java/org/fisco/bcos/codegen/CodeGenMain.java +++ b/src/main/java/org/fisco/bcos/codegen/CodeGenMain.java @@ -24,6 +24,21 @@ public class CodeGenMain { public static final String COMMAND_GENERATE = "generate"; public static final String COMMAND_PREFIX = COMMAND_SOLIDITY + " " + COMMAND_GENERATE; + public enum TransactionVersion { + V0(0), + V1(1); + + private final int v; + + TransactionVersion(int v) { + this.v = v; + } + + public int getV() { + return v; + } + } + public enum Version { V3(3), V2(2); @@ -108,9 +123,9 @@ static class PicocliRunner implements Runnable { private boolean enableAsyncCall = false; @Option( - names = {"-n", "--newTxManager"}, - description = "use new transaction manager interface, only V3 enable.") - private boolean useNewTransactionManager = false; + names = {"-t", "--txVersion"}, + description = "specify transaction version, default is 0, only V3 enable.") + private TransactionVersion transactionVersion = TransactionVersion.V0; @Override public void run() { @@ -131,7 +146,7 @@ public void run() { destinationFileDir, packageName, enableAsyncCall, - useNewTransactionManager) + transactionVersion.getV()) .generateJavaFiles(); } catch (Exception e) { org.fisco.bcos.codegen.v3.utils.CodeGenUtils.exitError(e); diff --git a/src/main/java/org/fisco/bcos/codegen/v3/wrapper/ContractGenerator.java b/src/main/java/org/fisco/bcos/codegen/v3/wrapper/ContractGenerator.java index b5cbc90..6909893 100644 --- a/src/main/java/org/fisco/bcos/codegen/v3/wrapper/ContractGenerator.java +++ b/src/main/java/org/fisco/bcos/codegen/v3/wrapper/ContractGenerator.java @@ -53,7 +53,7 @@ public class ContractGenerator { private String basePackageName; private boolean enableAsyncCall = false; - private boolean useNewTransactionManager = false; + private int transactionVersion = 0; public ContractGenerator( File binFile, @@ -86,9 +86,9 @@ public ContractGenerator( File destinationDir, String basePackageName, boolean enableAsyncCall, - boolean useNewTransactionManager) { + int transactionVersion) { this(binFile, smBinFile, abiFile, destinationDir, basePackageName, enableAsyncCall); - this.useNewTransactionManager = useNewTransactionManager; + this.transactionVersion = transactionVersion; } public void generateJavaFiles() throws CodeGenException, IOException, ClassNotFoundException { @@ -117,7 +117,7 @@ public void generateJavaFiles() throws CodeGenException, IOException, ClassNotFo destinationDir.toString(), basePackageName, enableAsyncCall, - useNewTransactionManager); + transactionVersion); } private byte[] calculateWasmBytes(byte[] binary) throws IOException { diff --git a/src/main/java/org/fisco/bcos/codegen/v3/wrapper/ContractWrapper.java b/src/main/java/org/fisco/bcos/codegen/v3/wrapper/ContractWrapper.java index 82b7c9b..4e2b181 100644 --- a/src/main/java/org/fisco/bcos/codegen/v3/wrapper/ContractWrapper.java +++ b/src/main/java/org/fisco/bcos/codegen/v3/wrapper/ContractWrapper.java @@ -28,6 +28,7 @@ import java.util.stream.Collectors; import javax.lang.model.SourceVersion; import javax.lang.model.element.Modifier; +import org.fisco.bcos.codegen.CodeGenMain; import org.fisco.bcos.codegen.v3.utils.CodeGenUtils; import org.fisco.bcos.sdk.v3.client.Client; import org.fisco.bcos.sdk.v3.client.protocol.model.TransactionAttribute; @@ -102,7 +103,7 @@ public class ContractWrapper { private static final List structsNamedTypeList = new ArrayList<>(); private boolean enableAsyncCall = false; - private boolean useNewTransactionManager = false; + private int transactionVersion = 0; public ContractWrapper(boolean isWasm) { this.isWasm = isWasm; @@ -116,10 +117,10 @@ public void generateJavaFiles( String destinationDir, String basePackageName, boolean enableAsyncCall, - boolean useNewTransactionManager) + int transactionVersion) throws IOException, ClassNotFoundException, UnsupportedOperationException { this.enableAsyncCall = enableAsyncCall; - this.useNewTransactionManager = useNewTransactionManager; + this.transactionVersion = transactionVersion; String[] nameParts = contractName.split("_"); for (int i = 0; i < nameParts.length; ++i) { nameParts[i] = StringUtils.capitaliseFirstLetter(nameParts[i]); @@ -160,7 +161,7 @@ public void generateJavaFiles( .collect(Collectors.toList())); classBuilder.addMethods(this.buildFunctionDefinitions(classBuilder, abiDefinitions)); classBuilder.addMethod(buildLoad(className)); - if (useNewTransactionManager) { + if (transactionVersion == CodeGenMain.TransactionVersion.V1.getV()) { classBuilder.addMethod(buildDefaultLoad(className)); } classBuilder.addMethods(this.buildDeployMethods(isWasm, className, abiDefinitions)); @@ -669,7 +670,7 @@ private static MethodSpec buildGetABIMethod() { private MethodSpec buildConstructor() { MethodSpec.Builder toReturn; - if (this.useNewTransactionManager) { + if (this.transactionVersion == CodeGenMain.TransactionVersion.V1.getV()) { toReturn = MethodSpec.constructorBuilder() .addModifiers(Modifier.PROTECTED) @@ -726,7 +727,7 @@ private MethodSpec buildDeployWithParams( Arrays.class, Type.class, inputParams); - if (this.useNewTransactionManager) { + if (this.transactionVersion == CodeGenMain.TransactionVersion.V1.getV()) { methodBuilder .addStatement( "$L contract = deploy(" @@ -758,7 +759,7 @@ private MethodSpec buildDeployWithParams( private MethodSpec buildDeployNoParams( boolean isWasm, MethodSpec.Builder methodBuilder, String className) { - if (this.useNewTransactionManager) { + if (this.transactionVersion == CodeGenMain.TransactionVersion.V1.getV()) { methodBuilder .addStatement( "$L contract = deploy($L.class, $L, $L, $L, $L, null, $L)", @@ -804,7 +805,7 @@ private static MethodSpec.Builder getDeployMethodSpec(boolean isWasm, String cla private MethodSpec buildLoad(String className) { MethodSpec.Builder toReturn; - if (this.useNewTransactionManager) { + if (this.transactionVersion == CodeGenMain.TransactionVersion.V1.getV()) { toReturn = MethodSpec.methodBuilder("load") .addModifiers(Modifier.PUBLIC, Modifier.STATIC) @@ -1370,9 +1371,27 @@ private MethodSpec buildRawFunctionReturn(ABIDefinition functionDefinition) methodBuilder.addException(ContractException.class); if (outputParameterTypes.isEmpty()) { - methodBuilder.addStatement( - "throw new RuntimeException" - + "(\"cannot call constant function with void return type\")"); + if (functionDefinition.isConstant()) { + methodBuilder.addStatement( + "throw new RuntimeException" + + "(\"cannot call constant function with void return type\")"); + } else { + methodBuilder.addStatement( + "final $T function = " + + "new $T($N, \n$T.<$T>asList($L), " + + "\n$T.<$T>asList())", + Function.class, + Function.class, + funcNameToConst(functionName), + Arrays.class, + Type.class, + inputParams, + Arrays.class, + TypeReference.class); + CodeBlock.Builder callCode = CodeBlock.builder(); + callCode.addStatement("return function"); + methodBuilder.returns(Function.class).addCode(callCode.build()); + } } else if (outputParameterTypes.size() == 1) { TypeName typeName = outputParameterTypes.get(0); TypeName nativeReturnTypeName; @@ -1410,6 +1429,24 @@ private MethodSpec buildRawFunctionReturn(ABIDefinition functionDefinition) CodeBlock.Builder callCode = CodeBlock.builder(); callCode.addStatement("return function"); + methodBuilder.returns(Function.class).addCode(callCode.build()); + } else { + List returnTypes = new ArrayList<>(); + for (int i = 0; i < functionDefinition.getOutputs().size(); ++i) { + ABIDefinition.NamedType outputType = functionDefinition.getOutputs().get(i); + if (outputType.getType().equals("tuple")) { + returnTypes.add(structClassNameMap.get(outputType.structIdentifier())); + } else if (outputType.getType().startsWith("tuple") + && outputType.getType().contains("[")) { + returnTypes.add(buildStructArrayTypeName(outputType)); + } else { + returnTypes.add(getNativeType(outputParameterTypes.get(i))); + } + } + buildVariableLengthReturnFunctionConstructor( + methodBuilder, functionName, inputParams, outputParameterTypes); + CodeBlock.Builder callCode = CodeBlock.builder(); + callCode.addStatement("return function"); methodBuilder.returns(Function.class).addCode(callCode.build()); } return methodBuilder.build(); diff --git a/src/test/java/org/fisco/bcos/codegen/v3/test/CodeGenV3Test.java b/src/test/java/org/fisco/bcos/codegen/v3/test/CodeGenV3Test.java index cec4bc6..2d97394 100644 --- a/src/test/java/org/fisco/bcos/codegen/v3/test/CodeGenV3Test.java +++ b/src/test/java/org/fisco/bcos/codegen/v3/test/CodeGenV3Test.java @@ -163,7 +163,8 @@ private void codeGenTest(String abiFileName, String codeFilePath, String contrac "-o", javaOutPut, "-e", - "-n") + "-t", + "V1") .toArray(new String[0])); JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); DiagnosticCollector collector = new DiagnosticCollector<>();