Skip to content

Commit

Permalink
Merge pull request #186 from kyonRay/feature-1.3.1
Browse files Browse the repository at this point in the history
Release 1.3.1
  • Loading branch information
kyonRay authored Jul 31, 2023
2 parents dd6e304 + 6225955 commit 64e950e
Show file tree
Hide file tree
Showing 18 changed files with 319 additions and 39 deletions.
9 changes: 9 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
### v1.3.1

(2023-07-31)

**新增**

* 支持FISCO BCOS 3.+ WASM执行版本,支持WASM合约部署、调用等功能。


### v1.3.0

(2023-03-15)
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ apply plugin: 'java'
apply plugin: 'jacoco'

group = 'com.webank'
version = '1.3.0'
version = '1.3.1'
sourceCompatibility = '1.8'

repositories {
Expand Down Expand Up @@ -47,7 +47,7 @@ dependencies {
compile logger
compile 'org.jline:jline:3.15.0'
compile 'org.codehaus.groovy:groovy-all:3.0.10'
compile 'com.webank:wecross-java-sdk:1.3.0'
compile 'com.webank:wecross-java-sdk:1.3.1'
compile 'org.apache.commons:commons-compress:1.21'
compile 'commons-io:commons-io:2.8.0'
compile 'com.moandjiezana.toml:toml4j:0.7.2'
Expand Down
2 changes: 1 addition & 1 deletion release_note.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.3.0
v1.3.1
2 changes: 1 addition & 1 deletion src/main/java/com/webank/wecross/console/Shell.java
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public static void main(String[] args) {
case "bcosDeploy":
{
bcosCommand.deploy(params);
if (params.length >= 6 && isPath(params[1])) {
if (params.length >= 4 && isPath(params[1])) {
JlineUtils.addPathCompleters(completers, params[1]);
}
break;
Expand Down
15 changes: 13 additions & 2 deletions src/main/java/com/webank/wecross/console/common/ConsoleUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,21 @@ public class ConsoleUtils {
public static final String BCOSGMType = "GM_BCOS2.0";
public static final String BCOSType3 = "BCOS3_ECDSA_EVM";
public static final String BCOSGMType3 = "BCOS3_GM_EVM";
public static final String BCOSWASMType3 = "BCOS3_ECDSA_WASM";
public static final String BCOSWASMGMType3 = "BCOS3_GM_WASM";
public static final List<String> bcosChainList =
Arrays.asList(BCOSType, BCOSGMType, BCOSType3, BCOSGMType3);
Arrays.asList(
BCOSType, BCOSGMType, BCOSType3, BCOSGMType3, BCOSWASMType3, BCOSWASMGMType3);
public static final List<String> supportChainList =
Arrays.asList(fabricType, BCOSType, BCOSGMType, fabricType2, BCOSType3, BCOSGMType3);
Arrays.asList(
fabricType,
BCOSType,
BCOSGMType,
fabricType2,
BCOSType3,
BCOSGMType3,
BCOSWASMType3,
BCOSWASMGMType3);
private static final Logger logger = LoggerFactory.getLogger(ConsoleUtils.class);

public static boolean isValidPath(String path) {
Expand Down
22 changes: 22 additions & 0 deletions src/main/java/com/webank/wecross/console/common/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.bouncycastle.util.encoders.Hex;
import org.jline.reader.Completer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -125,6 +126,27 @@ public static String readFileContent(String fileName) throws IOException {
}
}

public static String readFileContentToHexString(String fileName) throws IOException {
try {
// to avoid path manipulation
fileName = fileName.replace("..", "");
Path path;

if (fileName.indexOf("classpath:") != 0) {
path = Paths.get(fileName);
} else {
// Start with "classpath:"
PathMatchingResourcePatternResolver resolver =
new PathMatchingResourcePatternResolver();
path = Paths.get(resolver.getResource(fileName).getURI());
}
return Hex.toHexString(Files.readAllBytes(path));
} catch (Exception e) {
logger.error("Read file error: ", e);
throw new IOException("Read file error: " + e);
}
}

public static String readFileToBytesString(String filePath) throws Exception {
String content = readFileContent(filePath);
return Base64.getEncoder().encodeToString(content.getBytes(StandardCharsets.UTF_8));
Expand Down
30 changes: 22 additions & 8 deletions src/main/java/com/webank/wecross/console/common/HelpInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -350,17 +350,31 @@ public static void getCurrentTransactionIDHelp() {

public static void BCOSDeployHelp() {
ConsoleUtils.singleLine();
System.out.println("Deploy contract and register contract info to CNS in BCOS chain ");
System.out.println("Usage: bcosDeploy [Path] [Source file path] [Class name] [Version]");
System.out.println("Path -- e.g: [zone.chain.res], specify which the path to be deployed");
System.out.println("Deploy contract and register contract info to CNS/BFS in BCOS chain ");
System.out.println("If you deploy Solidity contract:");
System.out.println("\tUsage: bcosDeploy [Path] [Source file path] [Class name] [Version]");
System.out.println(
"Source file path from conf/ -- The solidity source code file path, e.g: HelloWorld.sol");
System.out.println("Contract name -- The contract to be deploy");
System.out.println("Version -- The contract version");
System.out.println("Example:");
"\tPath -- e.g: [zone.chain.res], specify which the path to be deployed");
System.out.println(
"\tSource file path from conf/ -- The solidity source code file path, e.g: HelloWorld.sol");
System.out.println("\tContract name -- The contract to be deploy");
System.out.println(
"\tVersion -- The contract version, if chain version is BCOS3.0, should not use version");
System.out.println("\tExample:");
System.out.println(
" bcosDeploy payment.bcos.HelloWorld contracts/solidity/HelloWorld.sol HelloWorld 1.0");
" \tbcosDeploy payment.bcos.HelloWorld contracts/solidity/HelloWorld.sol HelloWorld 1.0");
ConsoleUtils.singleLine();
System.out.println("If you deploy WASM contract:");
System.out.println("\tUsage: bcosDeploy [Path] [BIN file path] [ABI file path]");
System.out.println(
"\tPath -- e.g: [zone.chain.res], specify which the path to be deployed");
System.out.println(
"\tBIN file path from conf/ -- The binary file after contract being compiled via cargo-liquid, e.g: hello_world.wasm");
System.out.println(
"\tABI file path from conf/ -- The ABI file after contract being compiled via cargo-liquid, e.g: hello_world.abi");
System.out.println("\tExample:");
System.out.println(
" \tbcosDeploy payment.bcos3.HelloWorld contracts/liquid/hello_world.wasm contracts/liquid/hello_world.abi");
}

public static void BCOSRegisterHelp() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public static void printRoutineIDResponse(XATransactionListResponse response) th

public static void printCommandResponse(CommandResponse response)
throws WeCrossConsoleException {
logger.debug("response: {}", response.getResult());
if (response == null) {
throw new WeCrossConsoleException(ErrorCode.NO_RESPONSE, "Error: no response");
} else if (response.getErrorCode() != StatusCode.SUCCESS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public class Version {

public static final String Version = "v1.3.0";
public static final String Version = "v1.3.1";
}
84 changes: 60 additions & 24 deletions src/main/java/com/webank/wecross/console/custom/BCOSCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import com.webank.wecross.console.exception.ErrorCode;
import com.webank.wecross.console.exception.WeCrossConsoleException;
import com.webank.wecrosssdk.rpc.WeCrossRPC;
import com.webank.wecrosssdk.rpc.common.ResourceDetail;
import com.webank.wecrosssdk.rpc.methods.response.CommandResponse;
import com.webank.wecrosssdk.rpc.methods.response.ResourceResponse;
import com.webank.wecrosssdk.utils.RPCUtils;
import java.io.File;
import java.util.*;
Expand Down Expand Up @@ -36,40 +38,74 @@ public void deploy(String[] params) throws Exception {
HelpInfo.BCOSDeployHelp();
return;
}
if (params.length < 5) {
if (params.length < 4) {
throw new WeCrossConsoleException(ErrorCode.PARAM_MISSING, "bcosDeploy");
}

String path = params[1];
String chain = path.substring(0, path.lastIndexOf('.') + 1);
RPCUtils.checkPath(path);
String cnsName = path.split("\\.")[2];
String sourcePath = params[2];
String contractName = params[3];
String version = params[4];

String stubType = "";
ResourceResponse resources = weCrossRPC.listResources(false).send();
for (ResourceDetail resourceDetail : resources.getResources().getResourceDetails()) {
if (resourceDetail.getPath().startsWith(chain)) {
stubType = resourceDetail.getStubType();
break;
}
}
if (stubType.equals("")) {
throw new WeCrossConsoleException(ErrorCode.INVALID_PATH, path);
}
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
org.springframework.core.io.Resource resource = resolver.getResource("file:" + sourcePath);
if (!resource.exists()) {
resource = resolver.getResource("classpath:" + sourcePath);
List<Object> args = new ArrayList<>();
// BCOSDeployWasm [path] [abi] [bin]
if (stubType.contains("WASM")) {
String name = path.split("\\.")[2];
String binContent = FileUtils.readFileContentToHexString(params[2]);
String abiContent = FileUtils.readFileContent(params[3]);
args.addAll(Arrays.asList(name, abiContent, binContent));
for (int i = 4; i < params.length; i++) {
// for constructor
args.add(ConsoleUtils.parseString(params[i]));
}
} else {
// Solidity
String cnsName = path.split("\\.")[2];
String sourcePath = params[2];
String contractName = params[3];

org.springframework.core.io.Resource resource =
resolver.getResource("file:" + sourcePath);
if (!resource.exists()) {
logger.error("Source file: {} not exists", sourcePath);
throw new Exception("Source file: " + sourcePath + " not exists");
resource = resolver.getResource("classpath:" + sourcePath);
if (!resource.exists()) {
logger.error("Source file: {} not exists", sourcePath);
throw new Exception("Source file: " + sourcePath + " not exists");
}
}
}

String filename = resource.getFilename();
String realPath = resource.getFile().getAbsolutePath();
String dir = realPath.substring(0, realPath.lastIndexOf(File.separator)) + File.separator;

String sourceContent = FileUtils.mergeSource(dir, filename, resolver, new HashSet<>());

List<Object> args =
new ArrayList<>(Arrays.asList(cnsName, sourceContent, contractName, version));
for (int i = 5; i < params.length; i++) {
// for constructor
args.add(ConsoleUtils.parseString(params[i]));
String filename = resource.getFilename();
String realPath = resource.getFile().getAbsolutePath();
String dir =
realPath.substring(0, realPath.lastIndexOf(File.separator)) + File.separator;

String sourceContent = FileUtils.mergeSource(dir, filename, resolver, new HashSet<>());

if (stubType.contains("BCOS3")) {
args.addAll(Arrays.asList(cnsName, sourceContent, contractName));
for (int i = 4; i < params.length; i++) {
// for constructor
args.add(ConsoleUtils.parseString(params[i]));
}
} else {
String version = params[4];
args.addAll(Arrays.asList(cnsName, sourceContent, contractName, version));
for (int i = 5; i < params.length; i++) {
// for constructor
args.add(ConsoleUtils.parseString(params[i]));
}
}
}

CommandResponse response = weCrossRPC.customCommand("deploy", path, args.toArray()).send();
PrintUtils.printCommandResponse(response);
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/resources/contracts/liquid/hello_world/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Generated by Cargo
# will have compiled files and executables
/target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "abi-gen"
version = "1.0.0-rc2"
authors = ["vita-dounai <lichenxi.webank@gmail.com>"]
edition = "2018"
publish = false

[[bin]]
name = "abi-gen"
path = "main.rs"

[dependencies.contract]
path = "../../"
package = "hello_world"
default-features = false
features = ["liquid-abi-gen"]

[dependencies.liquid_lang]
git = "https://github.com/WeBankBlockchain/liquid"
branch = "dev"
package = "liquid_lang"
default-features = false
features = ["contract-abi-gen"]

[dependencies]
serde = "1.0"
serde_json = "1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use std::{collections::HashMap, env};

fn main() -> Result<(), std::io::Error> {
let mut abi = HashMap::new();

let contract_abi = <contract::__LIQUID_ABI_GEN as liquid_lang::GenerateAbi>::generate_abi();

let mut local_abi =
Vec::with_capacity(contract_abi.event_abis.len() + contract_abi.fn_abis.len() + 1);
local_abi.extend(
contract_abi
.event_abis
.iter()
.map(|event_abi| liquid_lang::AbiKind::Event(event_abi.clone())),
);
local_abi.push(liquid_lang::AbiKind::Constructor(
contract_abi.constructor_abi,
));
local_abi.extend(
contract_abi
.fn_abis
.iter()
.map(|fn_abi| liquid_lang::AbiKind::ExternalFn(fn_abi.clone())),
);
abi.insert(String::from("$local"), local_abi);

for (iface_name, fn_abis) in contract_abi.iface_abis {
let fn_abis = fn_abis
.iter()
.map(|fn_abi| liquid_lang::AbiKind::ExternalFn(fn_abi.clone()))
.collect::<Vec<liquid_lang::AbiKind>>();
abi.insert(iface_name, fn_abis);
}

let target_dir = env::var("CARGO_TARGET_DIR").unwrap_or("target".into());
std::fs::create_dir(&target_dir).ok();
std::fs::write("hello_world.abi", serde_json::to_string(&abi).unwrap())?;
Ok(())
}
Loading

0 comments on commit 64e950e

Please sign in to comment.