Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<fix>(V3): fix get raw function interface bug, fix transaction version option conflict. #24

Merged
merged 1 commit into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions src/main/java/org/fisco/bcos/codegen/CodeGenMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -117,7 +117,7 @@ public void generateJavaFiles() throws CodeGenException, IOException, ClassNotFo
destinationDir.toString(),
basePackageName,
enableAsyncCall,
useNewTransactionManager);
transactionVersion);
}

private byte[] calculateWasmBytes(byte[] binary) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -102,7 +103,7 @@ public class ContractWrapper {
private static final List<ABIDefinition.NamedType> structsNamedTypeList = new ArrayList<>();

private boolean enableAsyncCall = false;
private boolean useNewTransactionManager = false;
private int transactionVersion = 0;

public ContractWrapper(boolean isWasm) {
this.isWasm = isWasm;
Expand All @@ -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]);
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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("
Expand Down Expand Up @@ -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)",
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<TypeName> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<JavaFileObject> collector = new DiagnosticCollector<>();
Expand Down
Loading