Skip to content

Commit

Permalink
feat: support multiple version solc and dev doc (#846)
Browse files Browse the repository at this point in the history
Co-authored-by: dwzhan <[email protected]>
  • Loading branch information
HelloAldis and dwzhan authored May 7, 2024
1 parent c138fd9 commit d4ebca8
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 81 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ dependencies {
//implementation 'org.fisco-bcos:solcJ:0.4.25.1'
//implementation 'org.fisco-bcos:solcJ:0.6.10.1'
//implementation 'org.fisco-bcos:solcJ:0.5.2.1'
implementation 'org.fisco-bcos:solcJ:0.8.11.1'
implementation 'org.fisco-bcos:solcJ:1.0.0-SNAPSHOT'

implementation ('org.fisco-bcos.java-sdk:fisco-bcos-java-sdk:3.7.0') {
exclude group: "org.slf4j"
Expand All @@ -53,7 +53,7 @@ dependencies {
implementation('org.jline:jline:3.21.0')
implementation('io.bretty:console-table-builder:1.2')
implementation('com.github.jsqlparser:jsqlparser:2.0')
implementation('org.fisco-bcos.code-generator:bcos-code-generator:1.5.0') {
implementation('org.fisco-bcos.code-generator:bcos-code-generator:1.6.0-SNAPSHOT') {
exclude group: "org.fisco-bcos.java-sdk"
exclude group: "org.slf4j"
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/console/command/model/HelpInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public static void deployHelp(boolean isWasm) {
System.out.println(
"Deploy a " + "\033[32m" + "Solidity" + "\033[m" + " contract on blockchain.");
System.out.println(
"Usage: \ndeploy contractNameOrPath parameters... [--parallel-analysis/-p]");
"Usage: \ndeploy contractNameOrPath parameters... [--parallel-analysis/-p --sol-version/-v 0.8.11]");
System.out.println(
"* contractNameOrPath -- The name of a contract or the path of a contract (Default load contract from the \"contracts/solidity\" path when using contractName).");
System.out.println(
Expand All @@ -237,6 +237,8 @@ public static void deployHelp(boolean isWasm) {
+ " link must locate under '/apps', and be composed of contract name and version ");
System.out.println(
"* --parallel-analysis/-p[Optional] -- parallel conflict analysis with the contract, default: no.");
System.out.println(
"* --sol-version/-v[Optional] -- The version of solidity compiler supported 0.4.25, 0.5.2, 0.6.10, 0.8.11, default is 0.8.11.");
} else {
System.out.println(
"Deploy a " + "\033[32m" + "Liquid" + "\033[m" + " contract on blockchain.");
Expand Down
56 changes: 48 additions & 8 deletions src/main/java/console/common/ConsoleUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
import org.fisco.bcos.sdk.v3.codec.datatypes.generated.tuples.generated.Tuple2;
import org.fisco.bcos.sdk.v3.codec.wrapper.ABIObject;
import org.fisco.bcos.sdk.v3.codec.wrapper.ContractCodecTools;
import org.fisco.bcos.sdk.v3.utils.StringUtils;
import org.fisco.solc.compiler.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -52,6 +54,7 @@ public class ConsoleUtils {
public static final String JAVA_PATH = "contracts/sdk/java/";
public static final String ABI_PATH = "contracts/sdk/abi/";
public static final String BIN_PATH = "contracts/sdk/bin/";
public static final String DOC_PATH = "contracts/sdk/doc/";
public static final String SOL_SUFFIX = ".sol";
public static final String WASM_SUFFIX = ".wasm";
public static final String GM_ACCOUNT_SUFFIX = "_gm";
Expand Down Expand Up @@ -315,11 +318,13 @@ public static void compileSolToJava(
File solFile,
String abiDir,
String binDir,
String docDir,
String librariesOption,
String specifyContract,
boolean isContractParallelAnalysis,
boolean enableAsyncCall,
String transactionVersion)
String transactionVersion,
Version version)
throws IOException, CompileContractException {

String contractName = solFile.getName().split("\\.")[0];
Expand All @@ -334,7 +339,8 @@ public static void compileSolToJava(
ContractCompiler.All,
librariesOption,
specifyContract,
isContractParallelAnalysis);
isContractParallelAnalysis,
version);
System.out.println("INFO: Compile for solidity " + solFile.getName() + " success.");
File abiFile = new File(abiDir + contractName + ".abi");
File binFile = new File(binDir + contractName + ".bin");
Expand All @@ -344,18 +350,24 @@ public static void compileSolToJava(
FileUtils.writeStringToFile(binFile, abiAndBin.getBin());

File smBinFile = new File(binDir + "/sm/" + contractName + ".bin");
File smAbiFile = new File(abiDir + "/sm/" + contractName + ".abi");
String smBinFilePath = smBinFile.getAbsolutePath();
FileUtils.writeStringToFile(
new File(abiDir + "/sm/" + contractName + ".abi"), abiAndBin.getAbi());
FileUtils.writeStringToFile(smAbiFile, abiAndBin.getAbi());
FileUtils.writeStringToFile(smBinFile, abiAndBin.getSmBin());

File devdocFile = new File(docDir + contractName + ".devdoc");
if (!StringUtils.isEmpty(abiAndBin.getDevdoc())) {
FileUtils.writeStringToFile(devdocFile, abiAndBin.getDevdoc());
}

List<String> args =
new ArrayList<>(
Arrays.asList(
"-v", "V3",
"-a", abiFilePath,
"-b", binFilePath,
"-s", smBinFilePath,
"-d", devdocFile.getAbsolutePath(),
"-p", packageName,
"-o", javaDir));
if (enableAsyncCall) {
Expand All @@ -376,9 +388,11 @@ public static void compileAllSolToJava(
File solFileList,
String abiDir,
String binDir,
String docDir,
boolean isContractParallelAnalysis,
boolean enableAsyncCall,
String transactionVersion)
String transactionVersion,
Version version)
throws IOException {
File[] solFiles = solFileList.listFiles();
if (solFiles.length == 0) {
Expand All @@ -400,11 +414,13 @@ public static void compileAllSolToJava(
solFile,
abiDir,
binDir,
docDir,
null,
null,
isContractParallelAnalysis,
enableAsyncCall,
transactionVersion);
transactionVersion,
version);
} catch (Exception e) {
System.out.println(
"ERROR:convert solidity to java for "
Expand Down Expand Up @@ -843,6 +859,9 @@ public static void main(String[] args) {
String DEFAULT_SOL = SOLIDITY_PATH;
String LIBS_OPTION = "libraries";

String SOL_VERSION_OPTION = "sol-version";
Version DEFAULT_SOL_VERSION = Version.V0_8_11;

String BIN_OPTION = "bin";
String SM_BIN_OPTION = "sm-bin";
String ABI_OPTION = "abi";
Expand Down Expand Up @@ -870,6 +889,16 @@ public static void main(String[] args) {
solidityFilePathOption.setRequired(false);
options.addOption(solidityFilePathOption);

Option solidityVersionPathOption =
new Option(
"v",
SOL_VERSION_OPTION,
true,
"[Optional] The solidity compiler version, default is "
+ DEFAULT_SOL_VERSION);
solidityFilePathOption.setRequired(false);
options.addOption(solidityVersionPathOption);

// libraries
Option libraryOption =
new Option(
Expand Down Expand Up @@ -953,6 +982,9 @@ public static void main(String[] args) {
String transactionVersionStr = "V" + cmd.getOptionValue(TRANSACTION_VERSION, "0");
if (mode.equals("solidity")) {
String solPathOrDir = cmd.getOptionValue(SOL_OPTION, DEFAULT_SOL);
Version solVersion =
convertStringToVersion(
cmd.getOptionValue(SOL_VERSION_OPTION, DEFAULT_SOL_VERSION.toString()));
String librariesOption = cmd.getOptionValue(LIBS_OPTION, "");
boolean useDagAnalysis = !cmd.hasOption(NO_ANALYSIS_OPTION);
boolean enableAsyncCall = cmd.hasOption(ENABLE_ASYNC_CALL_OPTION);
Expand All @@ -977,21 +1009,25 @@ public static void main(String[] args) {
sol,
ABI_PATH,
BIN_PATH,
DOC_PATH,
librariesOption,
specifyContract,
useDagAnalysis,
enableAsyncCall,
transactionVersionStr);
transactionVersionStr,
solVersion);
} else { // input dir
compileAllSolToJava(
fullJavaDir,
pkgName,
sol,
ABI_PATH,
BIN_PATH,
DOC_PATH,
useDagAnalysis,
enableAsyncCall,
transactionVersionStr);
transactionVersionStr,
solVersion);
}
} catch (IOException | CompileContractException e) {
System.out.print(e.getMessage());
Expand Down Expand Up @@ -1020,4 +1056,8 @@ public static void main(String[] args) {
CodeGenMain.main(params.toArray(new String[0]));
}
}

public static Version convertStringToVersion(String version) {
return Version.valueOf("V" + version.replace('.', '_'));
}
}
75 changes: 48 additions & 27 deletions src/main/java/console/contract/ConsoleContractImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
import org.fisco.bcos.sdk.v3.utils.StringUtils;
import org.fisco.solc.compiler.CompilationResult;
import org.fisco.solc.compiler.SolidityCompiler;
import org.fisco.solc.compiler.Version;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -244,16 +245,38 @@ private void deployLink(String linkPath, String address, String abiString) throw
public TransactionResponse deploySolidity(
String contractName, String contractNameOrPath, List<String> inputParams)
throws ConsoleMessageException {
List<String> tempInputParams = inputParams;
try {
boolean isContractParallelAnalysis = false;
Version version = Version.V0_8_11;
if (!inputParams.isEmpty()) {
if ("-p".equals(inputParams.get(inputParams.size() - 1))
|| "--parallel-analysis".equals(inputParams.get(inputParams.size() - 1))) {
int lastIndexOf = inputParams.lastIndexOf("-p");
if (lastIndexOf != -1) {
isContractParallelAnalysis = true;
tempInputParams = inputParams.subList(0, inputParams.size() - 1);
logger.info(
"deploy contract {} with '--parallel-analysis' or '-p'", contractName);
inputParams.remove(lastIndexOf);
logger.info("deploy contract {} with '-p'", contractName);
}

lastIndexOf = inputParams.lastIndexOf("--parallel-analysis");
if (lastIndexOf != -1) {
isContractParallelAnalysis = true;
inputParams.remove(lastIndexOf);
logger.info("deploy contract {} with '--parallel-analysis'", contractName);
}

lastIndexOf = inputParams.lastIndexOf("-v");
if (lastIndexOf != -1 && lastIndexOf != inputParams.size() - 1) {
version = ConsoleUtils.convertStringToVersion(inputParams.get(lastIndexOf + 1));
inputParams.remove(lastIndexOf);
inputParams.remove(lastIndexOf);
logger.info("deploy contract {} with '-v'", contractName);
}

lastIndexOf = inputParams.lastIndexOf("--sol-version");
if (lastIndexOf != -1 && lastIndexOf != inputParams.size() - 1) {
version = ConsoleUtils.convertStringToVersion(inputParams.get(lastIndexOf + 1));
inputParams.remove(lastIndexOf);
inputParams.remove(lastIndexOf);
logger.info("deploy contract {} with '--sol-version'", contractName);
}
}

Expand All @@ -263,7 +286,11 @@ public TransactionResponse deploySolidity(
== CryptoType.HSM_TYPE);
AbiAndBin abiAndBin =
ContractCompiler.compileContract(
contractNameOrPath, contractName, sm, isContractParallelAnalysis);
contractNameOrPath,
contractName,
sm,
isContractParallelAnalysis,
version);
String bin = abiAndBin.getBin();
if (sm) {
bin = abiAndBin.getSmBin();
Expand All @@ -273,12 +300,12 @@ public TransactionResponse deploySolidity(
DeployTransactionRequestWithStringParams request =
new TransactionRequestBuilder(abiAndBin.getAbi(), bin)
.setExtension(extension)
.buildDeployStringParamsRequest(tempInputParams);
.buildDeployStringParamsRequest(inputParams);
response = assembleTransactionService.deployContract(request);
} else {
response =
this.assembleTransactionProcessor.deployAndGetResponseWithStringParams(
abiAndBin.getAbi(), bin, tempInputParams, null);
abiAndBin.getAbi(), bin, inputParams, null);
}
if (response.getReturnCode() != PrecompiledRetCode.CODE_SUCCESS.getCode()) {
System.out.println("deploy contract for " + contractName + " failed!");
Expand Down Expand Up @@ -367,8 +394,8 @@ public TransactionResponse deployWasm(
// save the bin and abi
AbiAndBin abiAndBin =
client.getCryptoSuite().getCryptoTypeConfig() == CryptoType.SM_TYPE
? new AbiAndBin(abi, null, binStr)
: new AbiAndBin(abi, binStr, null);
? new AbiAndBin(abi, null, binStr, null)
: new AbiAndBin(abi, binStr, null, null);

String contractAddress =
Base64.getUrlEncoder()
Expand Down Expand Up @@ -644,7 +671,7 @@ public void call(String[] params, String pwd) throws Exception {
}
abi = list.getValue2().get(0).getExt().get(1);
}
AbiAndBin abiAndBin = new AbiAndBin(abi, "", "");
AbiAndBin abiAndBin = new AbiAndBin(abi, "", "", null);
String functionName = params[2];
List<String> inputParams = Arrays.asList(params).subList(3, params.length);
callContract(abiAndBin, "", address, functionName, inputParams);
Expand Down Expand Up @@ -1051,28 +1078,22 @@ private String getSolidityAbi(String contractFileName) throws Exception {
List<SolidityCompiler.Option> defaultOptions = Arrays.asList(ABI, BIN, METADATA);
List<SolidityCompiler.Option> options = new ArrayList<>(defaultOptions);

if (ContractCompiler.solcJVersion.compareToIgnoreCase(ConsoleUtils.COMPILE_WITH_BASE_PATH)
>= 0) {
logger.debug(
"compileSolToBinAndAbi, solc version:{} ,basePath: {}",
ContractCompiler.solcJVersion,
solFile.getParentFile().getCanonicalPath());
SolidityCompiler.Option basePath =
new SolidityCompiler.CustomOption(
"base-path", solFile.getParentFile().getCanonicalPath());
options.add(basePath);
} else {
logger.debug(
"compileSolToBinAndAbi, solc version:{}",
org.fisco.solc.compiler.Version.version);
}
logger.debug(
"compileSolToBinAndAbi, solc version:{} ,basePath: {}",
Version.V0_8_11,
solFile.getParentFile().getCanonicalPath());
SolidityCompiler.Option basePath =
new SolidityCompiler.CustomOption(
"base-path", solFile.getParentFile().getCanonicalPath());
options.add(basePath);

// compile ecdsa
SolidityCompiler.Result res =
SolidityCompiler.compile(
solFile,
(client.getCryptoType() == CryptoType.SM_TYPE),
true,
Version.V0_8_11,
options.toArray(new SolidityCompiler.Option[0]));

if (logger.isDebugEnabled()) {
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/console/contract/model/AbiAndBin.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ public class AbiAndBin {
private String abi = "";
private String bin = "";
private String smBin = "";
private String devdoc = "";

public AbiAndBin() {}

public AbiAndBin(String abi, String bin, String smBin) {
public AbiAndBin(String abi, String bin, String smBin, String devdoc) {
this.abi = abi;
this.bin = bin;
this.smBin = smBin;
this.devdoc = devdoc;
}

public String getSmBin() {
Expand All @@ -32,4 +34,8 @@ public String getBin() {
public void setBin(String bin) {
this.bin = bin;
}

public String getDevdoc() {
return devdoc;
}
}
Loading

0 comments on commit d4ebca8

Please sign in to comment.