From 58fbb930b53e3a2a28b57d7dc37e2ee8d4b9b6cd Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Mon, 30 Jan 2023 16:32:34 +0800 Subject: [PATCH 01/18] (command): adapt 3.3 dynamic auth check feature. (#735) --- build.gradle | 2 +- release_note.txt | 2 +- src/main/java/console/command/JlineUtils.java | 5 +++++ src/main/java/console/common/Common.java | 1 + src/main/java/console/common/ConsoleVersion.java | 2 +- 5 files changed, 9 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index 9adb9eb8..ffd0627f 100644 --- a/build.gradle +++ b/build.gradle @@ -40,7 +40,7 @@ dependencies { //compile 'org.fisco-bcos:solcJ:0.5.2.1' compile 'org.fisco-bcos:solcJ:0.8.11.1' - compile('org.fisco-bcos.java-sdk:fisco-bcos-java-sdk:3.2.0') { + compile('org.fisco-bcos.java-sdk:fisco-bcos-java-sdk:3.3.0-SNAPSHOT') { exclude group: "org.slf4j" } compile('org.fisco-bcos:evm-static-analysis:1.0.0') { diff --git a/release_note.txt b/release_note.txt index 6d260c3a..b299be97 100644 --- a/release_note.txt +++ b/release_note.txt @@ -1 +1 @@ -v3.2.0 +v3.3.0 diff --git a/src/main/java/console/command/JlineUtils.java b/src/main/java/console/command/JlineUtils.java index 6117a466..ed6fc861 100644 --- a/src/main/java/console/command/JlineUtils.java +++ b/src/main/java/console/command/JlineUtils.java @@ -190,6 +190,11 @@ public static LineReader getLineReader(Client client) throws IOException { new StringsCompleter(command), new StringsCompleter(Common.compatibilityVersion), new StringsCompleterIgnoreCase())); + completers.add( + new ArgumentCompleter( + new StringsCompleter(command), + new StringsCompleter(Common.authCheckStatus), + new StringsCompleterIgnoreCase())); } completers.add( new ArgumentCompleter( diff --git a/src/main/java/console/common/Common.java b/src/main/java/console/common/Common.java index dfbc2be2..96843583 100644 --- a/src/main/java/console/common/Common.java +++ b/src/main/java/console/common/Common.java @@ -11,6 +11,7 @@ public class Common { public static final String TxGasLimit = "tx_gas_limit"; public static final String ConsensusLeaderPeriod = "consensus_leader_period"; public static final String compatibilityVersion = "compatibility_version"; + public static final String authCheckStatus = "auth_check_status"; public static final int InvalidReturnNumber = -100; public static final long InvalidLongValue = Long.MAX_VALUE; diff --git a/src/main/java/console/common/ConsoleVersion.java b/src/main/java/console/common/ConsoleVersion.java index 67838175..1d8b8ec3 100644 --- a/src/main/java/console/common/ConsoleVersion.java +++ b/src/main/java/console/common/ConsoleVersion.java @@ -2,7 +2,7 @@ public class ConsoleVersion { - public static final String Version = "3.2.0"; + public static final String Version = "3.3.0"; public static void main(String[] args) { System.out.println("console version: " + Version); From 91b4b17a81901b093b16b3c3891c572dabd53929 Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Wed, 1 Feb 2023 11:36:17 +0800 Subject: [PATCH 02/18] (precompiled): adapt initAuth method. (#736) --- src/main/java/console/auth/AuthFace.java | 2 ++ src/main/java/console/auth/AuthImpl.java | 8 ++++++++ .../console/command/category/AuthOpCommand.java | 13 +++++++++++++ src/main/java/console/command/model/HelpInfo.java | 10 ++++++++++ 4 files changed, 33 insertions(+) diff --git a/src/main/java/console/auth/AuthFace.java b/src/main/java/console/auth/AuthFace.java index ec3b02be..06b3994e 100644 --- a/src/main/java/console/auth/AuthFace.java +++ b/src/main/java/console/auth/AuthFace.java @@ -69,4 +69,6 @@ public interface AuthFace { void abolishAccount(String[] params) throws Exception; void getAccountStatus(String[] params) throws Exception; + + void initAuth(String[] params) throws Exception; } diff --git a/src/main/java/console/auth/AuthImpl.java b/src/main/java/console/auth/AuthImpl.java index ee768990..29225116 100644 --- a/src/main/java/console/auth/AuthImpl.java +++ b/src/main/java/console/auth/AuthImpl.java @@ -637,6 +637,14 @@ public void getAccountStatus(String[] params) throws Exception { System.out.println("Account " + account + " status: " + (isNormal ? "Normal" : "Abnormal")); } + @Override + public void initAuth(String[] params) throws Exception { + String admin = params[1]; + checkValidAddress(admin, "adminAddress"); + RetCode retCode = authManager.initAuth(admin); + ConsoleUtils.printJson(retCode.toString()); + } + void checkValidAddress(String address, String valueName) throws TransactionException { if (!ConsoleUtils.isValidAddress(address)) { throw new TransactionException( diff --git a/src/main/java/console/command/category/AuthOpCommand.java b/src/main/java/console/command/category/AuthOpCommand.java index 581e5b53..2c52457f 100644 --- a/src/main/java/console/command/category/AuthOpCommand.java +++ b/src/main/java/console/command/category/AuthOpCommand.java @@ -485,6 +485,19 @@ public Map getAllCommandInfo(boolean isWasm) { false, true); + public static final CommandInfo INIT_AUTH = + new CommandInfo( + "initAuth", + "Initialize committee contract system.", + HelpInfo::initAuthHelp, + (consoleInitializer, params, pwd) -> + consoleInitializer.getAuthFace().initAuth(params), + 1, + 1, + false, + false, + true); + static { Field[] fields = AuthOpCommand.class.getDeclaredFields(); for (Field field : fields) { diff --git a/src/main/java/console/command/model/HelpInfo.java b/src/main/java/console/command/model/HelpInfo.java index 6023f393..5667a786 100644 --- a/src/main/java/console/command/model/HelpInfo.java +++ b/src/main/java/console/command/model/HelpInfo.java @@ -338,6 +338,16 @@ public static void getAccountStatusHelp() { System.out.println("* account -- 20 Bytes - The address of a account."); } + public static void initAuthHelp() { + System.out.println("Initialize committee contract system."); + System.out.println( + "\033[32m" + + "[Note]: this command is only can be used when first init chain committee." + + "\033[m"); + System.out.println("Usage: \ninitAuth admin"); + System.out.println("* account -- 20 Bytes - The address of a account."); + } + public static void setSystemConfigByKeyHelp() { System.out.println("Set a system config."); System.out.println("Usage: \nsetSystemConfigByKey key value"); From 920c3fe1dd3d8804d37ff39244db493f82e7275e Mon Sep 17 00:00:00 2001 From: jimmyshi <417711026@qq.com> Date: Thu, 9 Feb 2023 11:12:59 +0800 Subject: [PATCH 03/18] (shard): Add shard command (#740) --- .../console/command/SupportedCommand.java | 2 + .../command/category/ShardingCommand.java | 95 +++++++++++++++++++ .../console/command/model/CommandType.java | 5 +- .../java/console/command/model/HelpInfo.java | 15 +++ .../console/precompiled/PrecompiledFace.java | 6 ++ .../console/precompiled/PrecompiledImpl.java | 41 ++++++++ 6 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 src/main/java/console/command/category/ShardingCommand.java diff --git a/src/main/java/console/command/SupportedCommand.java b/src/main/java/console/command/SupportedCommand.java index 08338adc..d9eda798 100644 --- a/src/main/java/console/command/SupportedCommand.java +++ b/src/main/java/console/command/SupportedCommand.java @@ -21,6 +21,7 @@ import console.command.category.ContractOpCommand; import console.command.category.CrudCommand; import console.command.category.GroupCommand; +import console.command.category.ShardingCommand; import console.command.category.StatusQueryCommand; import console.command.model.BasicCategoryCommand; import console.command.model.CommandInfo; @@ -57,6 +58,7 @@ public static void setIsWasm(boolean wasm) { public static final GroupCommand groupCommand = new GroupCommand(); public static final AuthOpCommand authOpCommand = new AuthOpCommand(); public static final AccountOpCommand accountOpCommand = new AccountOpCommand(); + public static final ShardingCommand shardingCommand = new ShardingCommand(); /// FIXME: not supported now // public static CollaborationOpCommand collaborationOpCommand = new CollaborationOpCommand(); diff --git a/src/main/java/console/command/category/ShardingCommand.java b/src/main/java/console/command/category/ShardingCommand.java new file mode 100644 index 00000000..584891b0 --- /dev/null +++ b/src/main/java/console/command/category/ShardingCommand.java @@ -0,0 +1,95 @@ +package console.command.category; + +import console.command.model.BasicCategoryCommand; +import console.command.model.CommandInfo; +import console.command.model.CommandType; +import console.command.model.HelpInfo; +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class ShardingCommand extends BasicCategoryCommand { + protected static final Map commandToCommandInfo = new HashMap<>(); + + public ShardingCommand() { + super(CommandType.SHARDING_OP); + } + + @Override + public CommandInfo getCommandInfo(String command) { + if (commandToCommandInfo.containsKey(command)) { + return commandToCommandInfo.get(command); + } + return null; + } + + @Override + public List getAllCommand(boolean isWasm, boolean isAuthOpen) { + return commandToCommandInfo + .keySet() + .stream() + .filter( + key -> + !(isWasm && !commandToCommandInfo.get(key).isWasmSupport() + || (!isAuthOpen + && commandToCommandInfo.get(key).isNeedAuthOpen()))) + .collect(Collectors.toList()); + } + + @Override + public Map getAllCommandInfo(boolean isWasm) { + return commandToCommandInfo; + } + + public static final CommandInfo GET_CONTRACT_SHARD = + new CommandInfo( + "getContractShard", + "Get a contract's belonging shard.", + HelpInfo::getContractShardHelp, + (consoleInitializer, params, pwd) -> + consoleInitializer.getPrecompiledFace().getContractShard(params), + 1, + 1); + + public static final CommandInfo MAKE_SHARD = + new CommandInfo( + "makeShard", + "Make a shard.", + HelpInfo::makeShardHelp, + (consoleInitializer, params, pwd) -> + consoleInitializer.getPrecompiledFace().makeShard(params), + 1, + 1); + + public static final CommandInfo LINK_SHARD = + new CommandInfo( + "linkShard", + "Add a contract to a shard.", + HelpInfo::linkShardHelp, + (consoleInitializer, params, pwd) -> + consoleInitializer.getPrecompiledFace().linkShard(params), + 2, + 2); + + static { + Field[] fields = ShardingCommand.class.getDeclaredFields(); + for (Field field : fields) { + if (field.getType().equals(CommandInfo.class)) { + try { + CommandInfo constantCommandInfo = (CommandInfo) field.get(null); + commandToCommandInfo.put(constantCommandInfo.getCommand(), constantCommandInfo); + if (constantCommandInfo.getOptionCommand() != null) { + List subCommandList = constantCommandInfo.getOptionCommand(); + for (String s : subCommandList) { + commandToCommandInfo.put(s, constantCommandInfo); + } + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + } + } +} diff --git a/src/main/java/console/command/model/CommandType.java b/src/main/java/console/command/model/CommandType.java index 231fcf7a..558aa0c2 100644 --- a/src/main/java/console/command/model/CommandType.java +++ b/src/main/java/console/command/model/CommandType.java @@ -10,7 +10,8 @@ public enum CommandType { GROUP_QUERY, AUTH_OP, ACCOUNT_OP, - COLLABORATE_OP; + COLLABORATE_OP, + SHARDING_OP; @Override public String toString() { @@ -35,6 +36,8 @@ public String toString() { return "Account Operation"; case COLLABORATE_OP: return "Wasm Collaboration Operation"; + case SHARDING_OP: + return "Sharding Operation"; default: return "Unknown Command"; } diff --git a/src/main/java/console/command/model/HelpInfo.java b/src/main/java/console/command/model/HelpInfo.java index 5667a786..99bf4c85 100644 --- a/src/main/java/console/command/model/HelpInfo.java +++ b/src/main/java/console/command/model/HelpInfo.java @@ -475,6 +475,21 @@ public static void pwdHelp() { System.out.println("Usage: pwd"); } + public static void getContractShardHelp() { + System.out.println("Get a contract's belonging shard"); + System.out.println("Usage: getContractShard [contractAddress]"); + } + + public static void makeShardHelp() { + System.out.println("Make a shard"); + System.out.println("Usage: makeShard [shardName]"); + } + + public static void linkShardHelp() { + System.out.println("Add a contract to a shard"); + System.out.println("Usage: linkShard [shardName] [contractAddress]"); + } + public static void initializeHelp() { System.out.println("Usage: \ninitialize binPath abiPath"); System.out.println("* binPath -- The path of contract template."); diff --git a/src/main/java/console/precompiled/PrecompiledFace.java b/src/main/java/console/precompiled/PrecompiledFace.java index c2856aae..92513dc5 100644 --- a/src/main/java/console/precompiled/PrecompiledFace.java +++ b/src/main/java/console/precompiled/PrecompiledFace.java @@ -36,5 +36,11 @@ public interface PrecompiledFace { void link(String[] params) throws Exception; + void getContractShard(String[] params) throws Exception; + + void makeShard(String[] params) throws Exception; + + void linkShard(String[] params) throws Exception; + String getPwd(); } diff --git a/src/main/java/console/precompiled/PrecompiledImpl.java b/src/main/java/console/precompiled/PrecompiledImpl.java index 6121a05d..96d3c889 100644 --- a/src/main/java/console/precompiled/PrecompiledImpl.java +++ b/src/main/java/console/precompiled/PrecompiledImpl.java @@ -30,6 +30,7 @@ import org.fisco.bcos.sdk.v3.contract.precompiled.crud.common.ConditionV320; import org.fisco.bcos.sdk.v3.contract.precompiled.crud.common.Entry; import org.fisco.bcos.sdk.v3.contract.precompiled.crud.common.UpdateFields; +import org.fisco.bcos.sdk.v3.contract.precompiled.sharding.ShardingService; import org.fisco.bcos.sdk.v3.contract.precompiled.sysconfig.SystemConfigService; import org.fisco.bcos.sdk.v3.crypto.keypair.CryptoKeyPair; import org.fisco.bcos.sdk.v3.model.EnumNodeVersion; @@ -52,6 +53,7 @@ public class PrecompiledImpl implements PrecompiledFace { private SystemConfigService systemConfigService; private TableCRUDService tableCRUDService; private BFSService bfsService; + private ShardingService shardingService; private String pwd = "/apps"; public PrecompiledImpl(Client client) { @@ -61,6 +63,7 @@ public PrecompiledImpl(Client client) { this.systemConfigService = new SystemConfigService(client, cryptoKeyPair); this.tableCRUDService = new TableCRUDService(client, cryptoKeyPair); this.bfsService = new BFSService(client, cryptoKeyPair); + this.shardingService = new ShardingService(client, cryptoKeyPair); } @Override @@ -671,6 +674,44 @@ public void link(String[] params) throws Exception { System.out.println(); } + @Override + public void getContractShard(String[] params) throws Exception { + String shard = this.shardingService.getContractShard(params[1]); + System.out.println(shard); + } + + @Override + public void makeShard(String[] params) throws Exception { + String shardName = params[1]; + RetCode retCode = this.shardingService.makeShard(shardName); + + logger.info("makeShard: {}, retCode {}", shardName, retCode); + // parse the result + if (retCode.getCode() == PrecompiledRetCode.CODE_SUCCESS.getCode()) { + System.out.println("make shard " + shardName + " Ok. You can use 'ls' to check"); + } else { + System.out.println("make shard " + shardName + " failed "); + ConsoleUtils.printJson(retCode.toString()); + } + } + + @Override + public void linkShard(String[] params) throws Exception { + String shardName = params[1]; + String address = params[2]; + RetCode retCode = this.shardingService.linkShard(shardName, address); + + logger.info("linkShard: add {} to {}, retCode {}", address, shardName, retCode); + // parse the result + if (retCode.getCode() == PrecompiledRetCode.CODE_SUCCESS.getCode()) { + System.out.println( + "Add " + address + " to " + shardName + " Ok. You can use 'ls' to check"); + } else { + System.out.println("Add " + address + " to " + shardName + " failed "); + ConsoleUtils.printJson(retCode.toString()); + } + } + @Override public String getPwd() { return pwd; From 5bd0b93e186144970d11324e9f841ee67c0ad579 Mon Sep 17 00:00:00 2001 From: jimmyshi <417711026@qq.com> Date: Tue, 14 Feb 2023 18:33:45 +0800 Subject: [PATCH 04/18] (Sharding): Switch linkShard command param order (#741) --- src/main/java/console/command/model/HelpInfo.java | 2 +- src/main/java/console/precompiled/PrecompiledImpl.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/console/command/model/HelpInfo.java b/src/main/java/console/command/model/HelpInfo.java index 99bf4c85..a07ad435 100644 --- a/src/main/java/console/command/model/HelpInfo.java +++ b/src/main/java/console/command/model/HelpInfo.java @@ -487,7 +487,7 @@ public static void makeShardHelp() { public static void linkShardHelp() { System.out.println("Add a contract to a shard"); - System.out.println("Usage: linkShard [shardName] [contractAddress]"); + System.out.println("Usage: linkShard [contractAddress] [shardName]"); } public static void initializeHelp() { diff --git a/src/main/java/console/precompiled/PrecompiledImpl.java b/src/main/java/console/precompiled/PrecompiledImpl.java index 96d3c889..e22d1bdd 100644 --- a/src/main/java/console/precompiled/PrecompiledImpl.java +++ b/src/main/java/console/precompiled/PrecompiledImpl.java @@ -697,8 +697,8 @@ public void makeShard(String[] params) throws Exception { @Override public void linkShard(String[] params) throws Exception { - String shardName = params[1]; - String address = params[2]; + String address = params[1]; + String shardName = params[2]; RetCode retCode = this.shardingService.linkShard(shardName, address); logger.info("linkShard: add {} to {}, retCode {}", address, shardName, retCode); From 06d92957212ebb339f800e9de3ba4714b297ff6f Mon Sep 17 00:00:00 2001 From: jimmyshi <417711026@qq.com> Date: Wed, 15 Feb 2023 15:11:58 +0800 Subject: [PATCH 05/18] (shard): Print more info in getContractShard (#742) --- src/main/java/console/precompiled/PrecompiledImpl.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/console/precompiled/PrecompiledImpl.java b/src/main/java/console/precompiled/PrecompiledImpl.java index e22d1bdd..a6c531a6 100644 --- a/src/main/java/console/precompiled/PrecompiledImpl.java +++ b/src/main/java/console/precompiled/PrecompiledImpl.java @@ -677,6 +677,12 @@ public void link(String[] params) throws Exception { @Override public void getContractShard(String[] params) throws Exception { String shard = this.shardingService.getContractShard(params[1]); + if (shard.isEmpty()) { + shard = "default"; + } else { + shard = "/shards/" + shard; + } + System.out.println(shard); } From a91e2ae7de1bfcb3eb03631af68b6e2cfa1465ec Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Thu, 16 Feb 2023 14:31:10 +0800 Subject: [PATCH 06/18] (precompiled): add switch group after set system version config. (#743) --- src/main/java/console/Console.java | 17 ++++++++++------- src/main/java/console/ConsoleInitializer.java | 10 ++++++++++ .../console/command/category/AuthOpCommand.java | 17 +++++++++++------ .../command/category/StatusQueryCommand.java | 4 +++- .../console/precompiled/PrecompiledFace.java | 5 ++++- .../console/precompiled/PrecompiledImpl.java | 15 +++++++++++++-- 6 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/main/java/console/Console.java b/src/main/java/console/Console.java index e29f79ce..a1b35bff 100644 --- a/src/main/java/console/Console.java +++ b/src/main/java/console/Console.java @@ -51,6 +51,7 @@ public static void main(String[] args) { keymap.bind(new Reference("beginning-of-line"), "\033[1~"); keymap.bind(new Reference("end-of-line"), "\033[4~"); } + consoleInitializer.setLineReader(lineReader); } catch (Exception e) { System.out.println(e.getMessage()); logger.error(" message: {}, e: {}", e.getMessage(), e); @@ -66,19 +67,21 @@ public static void main(String[] args) { while (true) { try { - if (lineReader == null) { + if (consoleInitializer.getLineReader() == null) { System.out.println("Console can not read commands."); break; } String request; if (!consoleInitializer.isDisableAutoCompleter()) { request = - lineReader.readLine( - "[" - + consoleInitializer.getGroupID() - + "]: " - + ConsoleUtils.prettyPwd(pwd) - + "> "); + consoleInitializer + .getLineReader() + .readLine( + "[" + + consoleInitializer.getGroupID() + + "]: " + + ConsoleUtils.prettyPwd(pwd) + + "> "); } else { System.out.print( "[group:" diff --git a/src/main/java/console/ConsoleInitializer.java b/src/main/java/console/ConsoleInitializer.java index 9d3824fb..076b8229 100644 --- a/src/main/java/console/ConsoleInitializer.java +++ b/src/main/java/console/ConsoleInitializer.java @@ -24,6 +24,7 @@ import org.fisco.bcos.sdk.v3.crypto.exceptions.LoadKeyStoreException; import org.fisco.bcos.sdk.v3.crypto.keypair.CryptoKeyPair; import org.fisco.bcos.sdk.v3.model.CryptoType; +import org.jline.reader.LineReader; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,6 +43,15 @@ public class ConsoleInitializer { private AuthFace authFace; private CollaborationFace collaborationFace; private boolean disableAutoCompleter = false; + private LineReader lineReader; + + public LineReader getLineReader() { + return lineReader; + } + + public void setLineReader(LineReader lineReader) { + this.lineReader = lineReader; + } public void init(String[] args) throws ConfigException { AccountInfo accountInfo = null; diff --git a/src/main/java/console/command/category/AuthOpCommand.java b/src/main/java/console/command/category/AuthOpCommand.java index 2c52457f..7b1d193b 100644 --- a/src/main/java/console/command/category/AuthOpCommand.java +++ b/src/main/java/console/command/category/AuthOpCommand.java @@ -5,10 +5,10 @@ import console.command.model.CommandType; import console.command.model.HelpInfo; import java.lang.reflect.Field; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; public class AuthOpCommand extends BasicCategoryCommand { protected static final Map commandToCommandInfo = new HashMap<>(); @@ -27,10 +27,15 @@ public CommandInfo getCommandInfo(String command) { @Override public List getAllCommand(boolean isWasm, boolean isAuthOpen) { - if (!isWasm && isAuthOpen) { - return new ArrayList<>(commandToCommandInfo.keySet()); - } - return new ArrayList<>(); + return commandToCommandInfo + .keySet() + .stream() + .filter( + key -> + !(isWasm && !commandToCommandInfo.get(key).isWasmSupport() + || (!isAuthOpen + && commandToCommandInfo.get(key).isNeedAuthOpen()))) + .collect(Collectors.toList()); } @Override @@ -496,7 +501,7 @@ public Map getAllCommandInfo(boolean isWasm) { 1, false, false, - true); + false); static { Field[] fields = AuthOpCommand.class.getDeclaredFields(); diff --git a/src/main/java/console/command/category/StatusQueryCommand.java b/src/main/java/console/command/category/StatusQueryCommand.java index 278e42e5..0847b6d4 100644 --- a/src/main/java/console/command/category/StatusQueryCommand.java +++ b/src/main/java/console/command/category/StatusQueryCommand.java @@ -187,7 +187,9 @@ public Map getAllCommandInfo(boolean isWasm) { "Set a system config value by key", HelpInfo::setSystemConfigByKeyHelp, (consoleInitializer, params, pwd) -> - consoleInitializer.getPrecompiledFace().setSystemConfigByKey(params), + consoleInitializer + .getPrecompiledFace() + .setSystemConfigByKey(consoleInitializer, params), 2, 2); diff --git a/src/main/java/console/precompiled/PrecompiledFace.java b/src/main/java/console/precompiled/PrecompiledFace.java index 92513dc5..7c9f5eca 100644 --- a/src/main/java/console/precompiled/PrecompiledFace.java +++ b/src/main/java/console/precompiled/PrecompiledFace.java @@ -1,5 +1,7 @@ package console.precompiled; +import console.ConsoleInitializer; + public interface PrecompiledFace { // ConsensusPrecompiled void addSealer(String[] params) throws Exception; @@ -8,7 +10,8 @@ public interface PrecompiledFace { void removeNode(String[] params) throws Exception; - void setSystemConfigByKey(String[] params) throws Exception; + void setSystemConfigByKey(ConsoleInitializer consoleInitializer, String[] params) + throws Exception; void createTable(String sql) throws Exception; diff --git a/src/main/java/console/precompiled/PrecompiledImpl.java b/src/main/java/console/precompiled/PrecompiledImpl.java index a6c531a6..1e235c69 100644 --- a/src/main/java/console/precompiled/PrecompiledImpl.java +++ b/src/main/java/console/precompiled/PrecompiledImpl.java @@ -1,5 +1,8 @@ package console.precompiled; +import static console.common.Common.compatibilityVersion; + +import console.ConsoleInitializer; import console.common.Common; import console.common.ConsoleUtils; import console.contract.model.AbiAndBin; @@ -111,10 +114,18 @@ public void setConsensusNodeWeight(String[] params) throws Exception { } @Override - public void setSystemConfigByKey(String[] params) throws Exception { + public void setSystemConfigByKey(ConsoleInitializer consoleInitializer, String[] params) + throws Exception { String key = params[1]; String value = params[2]; - ConsoleUtils.printJson(this.systemConfigService.setValueByKey(key, value).toString()); + RetCode retCode = this.systemConfigService.setValueByKey(key, value); + ConsoleUtils.printJson(retCode.toString()); + if (key.equals(compatibilityVersion) + && retCode.code == PrecompiledRetCode.CODE_SUCCESS.code) { + String[] param = new String[2]; + param[1] = consoleInitializer.getGroupID(); + consoleInitializer.switchGroup(param); + } } @Override From 8ae23afbed6ce3076b9e16c3e4ce4d9f53ae6684 Mon Sep 17 00:00:00 2001 From: LucasLi1024 <410567249@qq.com> Date: Mon, 13 Feb 2023 11:39:51 +0800 Subject: [PATCH 07/18] add hsm java-sdk --- build.gradle | 15 ++++++++------- src/main/java/console/ConsoleInitializer.java | 11 ++++++++++- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/build.gradle b/build.gradle index ffd0627f..c760edce 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ plugins { - id 'com.github.sherter.google-java-format' version '0.8' + // id 'com.github.sherter.google-java-format' version '0.8' } apply plugin: 'maven' apply plugin: 'java' @@ -18,11 +18,11 @@ repositories { maven { url "https://oss.sonatype.org/content/repositories/snapshots" } } -googleJavaFormat { - options style: 'AOSP' - source = sourceSets*.allJava - include '**/*.java' -} +// googleJavaFormat { +// options style: 'AOSP' +// source = sourceSets*.allJava +// include '**/*.java' +// } def log4j_version = '2.19.0' List logger = [ @@ -40,9 +40,10 @@ dependencies { //compile 'org.fisco-bcos:solcJ:0.5.2.1' compile 'org.fisco-bcos:solcJ:0.8.11.1' - compile('org.fisco-bcos.java-sdk:fisco-bcos-java-sdk:3.3.0-SNAPSHOT') { + compile('org.fisco-bcos.java-sdk:fisco-bcos-java-sdk:3.3.0-debugHsm-SNAPSHOT') { exclude group: "org.slf4j" } + compile('org.fisco-bcos:evm-static-analysis:1.0.0') { exclude group: "org.slf4j" } diff --git a/src/main/java/console/ConsoleInitializer.java b/src/main/java/console/ConsoleInitializer.java index 076b8229..4ac22f6f 100644 --- a/src/main/java/console/ConsoleInitializer.java +++ b/src/main/java/console/ConsoleInitializer.java @@ -150,6 +150,7 @@ private void loadAccountInfo(AccountInfo accountInfo, String groupID) { try { this.client = groupID == null ? bcosSDK.getClient() : bcosSDK.getClient(groupID); if (accountInfo != null) { + logger.debug("===== loadAccountInfo accountInfo != null"); this.client .getCryptoSuite() .loadAccount( @@ -160,8 +161,10 @@ private void loadAccountInfo(AccountInfo accountInfo, String groupID) { .getConfig() .getAccountConfig() .isAccountConfigured()) { + logger.debug("===== loadAccountRandomly"); accountInfo = loadAccountRandomly(bcosSDK, client); if (accountInfo != null) { + logger.debug("===== accountInfo != null"); this.client .getCryptoSuite() .loadAccount( @@ -170,8 +173,10 @@ private void loadAccountInfo(AccountInfo accountInfo, String groupID) { accountInfo.password); } if (accountInfo == null) { + logger.debug("===== accountInfo == null"); // save the keyPair client.getCryptoSuite().getCryptoKeyPair().storeKeyPairWithPemFormat(); + logger.debug("===== save the keyPair 11111111111111"); } } } catch (LoadKeyStoreException e) { @@ -183,7 +188,7 @@ private void loadAccountInfo(AccountInfo accountInfo, String groupID) { System.out.println( "Failed to create BcosSDK failed! Please check the node status and the console configuration, error info: " + e.getMessage()); - logger.error(" message: {}, e: {}", e.getMessage(), e); + logger.error(" message: {}", e.getMessage(), e); System.exit(0); } } @@ -191,11 +196,14 @@ private void loadAccountInfo(AccountInfo accountInfo, String groupID) { private AccountInfo loadAccountRandomly(BcosSDK bcosSDK, Client client) { ConfigOption config = bcosSDK.getConfig(); if (config.getAccountConfig() == null) { + logger.debug("======= loadAccountRandomly config.getAccountConfig() == null"); return null; } String keyStoreDir = config.getAccountConfig().getKeyStoreDir(); + logger.debug("======= keyStoreDir: {}", keyStoreDir); File keyStoreDirPath = new File(keyStoreDir); if (!keyStoreDirPath.exists() || !keyStoreDirPath.isDirectory()) { + logger.debug("======= loadAccountRandomly !keyStoreDirPath.exists() || !keyStoreDirPath.isDirectory()"); return null; } String subDir = client.getCryptoSuite().getKeyPairFactory().getKeyStoreSubDir(); @@ -203,6 +211,7 @@ private AccountInfo loadAccountRandomly(BcosSDK bcosSDK, Client client) { File keyStoreFileDirPath = new File(keyStoreFileDir); logger.debug("loadAccountRandomly, keyStoreFileDirPath:{}", keyStoreFileDir); if (!keyStoreFileDirPath.exists() || !keyStoreFileDirPath.isDirectory()) { + logger.debug("======= loadAccountRandomly !keyStoreFileDirPath.exists() || !keyStoreFileDirPath.isDirectory()"); return null; } // load account from the keyStoreDir From fb44c392dc185fdb2408fed0ec7d49c97f610103 Mon Sep 17 00:00:00 2001 From: LucasLi1024 <410567249@qq.com> Date: Wed, 15 Feb 2023 20:31:21 +0800 Subject: [PATCH 08/18] loadAccount adapt to HSM Author: Lucasli Date: Thu Feb 16 21:13:21 2023 +0800 --- build.gradle | 14 +++++++------- src/main/java/console/ConsoleInitializer.java | 13 ++++--------- .../java/console/contract/ConsoleContractImpl.java | 5 ++++- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/build.gradle b/build.gradle index c760edce..261506d5 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ plugins { - // id 'com.github.sherter.google-java-format' version '0.8' + id 'com.github.sherter.google-java-format' version '0.8' } apply plugin: 'maven' apply plugin: 'java' @@ -18,11 +18,11 @@ repositories { maven { url "https://oss.sonatype.org/content/repositories/snapshots" } } -// googleJavaFormat { -// options style: 'AOSP' -// source = sourceSets*.allJava -// include '**/*.java' -// } +googleJavaFormat { + options style: 'AOSP' + source = sourceSets*.allJava + include '**/*.java' +} def log4j_version = '2.19.0' List logger = [ @@ -40,7 +40,7 @@ dependencies { //compile 'org.fisco-bcos:solcJ:0.5.2.1' compile 'org.fisco-bcos:solcJ:0.8.11.1' - compile('org.fisco-bcos.java-sdk:fisco-bcos-java-sdk:3.3.0-debugHsm-SNAPSHOT') { + compile('org.fisco-bcos.java-sdk:fisco-bcos-java-sdk:3.3.0-SNAPSHOT') { exclude group: "org.slf4j" } diff --git a/src/main/java/console/ConsoleInitializer.java b/src/main/java/console/ConsoleInitializer.java index 4ac22f6f..a1ed779d 100644 --- a/src/main/java/console/ConsoleInitializer.java +++ b/src/main/java/console/ConsoleInitializer.java @@ -135,6 +135,10 @@ private AccountInfo loadConfig(String[] args) throws ConfigException { System.exit(0); } + if (config.getCryptoMaterialConfig().getEnableHsm()) { + return new AccountInfo("HSM", "", ""); + } + if (args.length == 3) { return loadAccount(bcosSDK, args); } @@ -150,7 +154,6 @@ private void loadAccountInfo(AccountInfo accountInfo, String groupID) { try { this.client = groupID == null ? bcosSDK.getClient() : bcosSDK.getClient(groupID); if (accountInfo != null) { - logger.debug("===== loadAccountInfo accountInfo != null"); this.client .getCryptoSuite() .loadAccount( @@ -161,10 +164,8 @@ private void loadAccountInfo(AccountInfo accountInfo, String groupID) { .getConfig() .getAccountConfig() .isAccountConfigured()) { - logger.debug("===== loadAccountRandomly"); accountInfo = loadAccountRandomly(bcosSDK, client); if (accountInfo != null) { - logger.debug("===== accountInfo != null"); this.client .getCryptoSuite() .loadAccount( @@ -173,10 +174,8 @@ private void loadAccountInfo(AccountInfo accountInfo, String groupID) { accountInfo.password); } if (accountInfo == null) { - logger.debug("===== accountInfo == null"); // save the keyPair client.getCryptoSuite().getCryptoKeyPair().storeKeyPairWithPemFormat(); - logger.debug("===== save the keyPair 11111111111111"); } } } catch (LoadKeyStoreException e) { @@ -196,14 +195,11 @@ private void loadAccountInfo(AccountInfo accountInfo, String groupID) { private AccountInfo loadAccountRandomly(BcosSDK bcosSDK, Client client) { ConfigOption config = bcosSDK.getConfig(); if (config.getAccountConfig() == null) { - logger.debug("======= loadAccountRandomly config.getAccountConfig() == null"); return null; } String keyStoreDir = config.getAccountConfig().getKeyStoreDir(); - logger.debug("======= keyStoreDir: {}", keyStoreDir); File keyStoreDirPath = new File(keyStoreDir); if (!keyStoreDirPath.exists() || !keyStoreDirPath.isDirectory()) { - logger.debug("======= loadAccountRandomly !keyStoreDirPath.exists() || !keyStoreDirPath.isDirectory()"); return null; } String subDir = client.getCryptoSuite().getKeyPairFactory().getKeyStoreSubDir(); @@ -211,7 +207,6 @@ private AccountInfo loadAccountRandomly(BcosSDK bcosSDK, Client client) { File keyStoreFileDirPath = new File(keyStoreFileDir); logger.debug("loadAccountRandomly, keyStoreFileDirPath:{}", keyStoreFileDir); if (!keyStoreFileDirPath.exists() || !keyStoreFileDirPath.isDirectory()) { - logger.debug("======= loadAccountRandomly !keyStoreFileDirPath.exists() || !keyStoreFileDirPath.isDirectory()"); return null; } // load account from the keyStoreDir diff --git a/src/main/java/console/contract/ConsoleContractImpl.java b/src/main/java/console/contract/ConsoleContractImpl.java index 298848e2..be5fcc8d 100644 --- a/src/main/java/console/contract/ConsoleContractImpl.java +++ b/src/main/java/console/contract/ConsoleContractImpl.java @@ -296,7 +296,10 @@ public TransactionResponse deploySolidity( } } - boolean sm = client.getCryptoSuite().getCryptoTypeConfig() == CryptoType.SM_TYPE; + boolean sm = + (client.getCryptoSuite().getCryptoTypeConfig() == CryptoType.SM_TYPE) + || (client.getCryptoSuite().getCryptoTypeConfig() + == CryptoType.HSM_TYPE); AbiAndBin abiAndBin = ContractCompiler.compileContract( contractNameOrPath, contractName, sm, isContractParallelAnalysis); From b1aad4d98aa9dc1499a1f1c80d5c8f0ad63991d1 Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Fri, 17 Feb 2023 12:30:35 +0800 Subject: [PATCH 09/18] (help): fix system config helper. (#745) --- .../console/client/ConsoleClientImpl.java | 6 +- src/main/java/console/command/JlineUtils.java | 10 +-- .../java/console/command/model/HelpInfo.java | 62 +++++++++---------- src/main/java/console/common/Common.java | 56 ++++++++++------- .../java/console/common/ConsoleUtils.java | 8 +-- .../console/contract/ConsoleContractImpl.java | 39 ++++++------ .../console/precompiled/PrecompiledImpl.java | 4 +- 7 files changed, 96 insertions(+), 89 deletions(-) diff --git a/src/main/java/console/client/ConsoleClientImpl.java b/src/main/java/console/client/ConsoleClientImpl.java index c2a9e129..6cc9f6d1 100644 --- a/src/main/java/console/client/ConsoleClientImpl.java +++ b/src/main/java/console/client/ConsoleClientImpl.java @@ -117,7 +117,7 @@ public void getBlockByHash(String[] params) throws IOException { public void getBlockByNumber(String[] params) throws IOException { String blockNumberStr = params[1]; int blockNumber = ConsoleUtils.processNonNegativeNumber("blockNumber", blockNumberStr); - if (blockNumber == Common.InvalidReturnNumber) { + if (blockNumber == Common.INVALID_RETURN_NUMBER) { return; } boolean flag = false; @@ -155,7 +155,7 @@ public void getBlockHeaderByHash(String[] params) throws IOException { public void getBlockHashByNumber(String[] params) throws IOException { String blockNumberStr = params[1]; int blockNumber = ConsoleUtils.processNonNegativeNumber("blockNumber", blockNumberStr); - if (blockNumber == Common.InvalidReturnNumber) { + if (blockNumber == Common.INVALID_RETURN_NUMBER) { return; } String blockHashByNumber = @@ -173,7 +173,7 @@ public void getBlockHashByNumber(String[] params) throws IOException { public void getBlockHeaderByNumber(String[] params) throws IOException { String blockNumberStr = params[1]; int blockNumber = ConsoleUtils.processNonNegativeNumber("blockNumber", blockNumberStr); - if (blockNumber == Common.InvalidReturnNumber) { + if (blockNumber == Common.INVALID_RETURN_NUMBER) { return; } ConsoleUtils.printJson( diff --git a/src/main/java/console/command/JlineUtils.java b/src/main/java/console/command/JlineUtils.java index ed6fc861..36f941da 100644 --- a/src/main/java/console/command/JlineUtils.java +++ b/src/main/java/console/command/JlineUtils.java @@ -172,28 +172,28 @@ public static LineReader getLineReader(Client client) throws IOException { completers.add( new ArgumentCompleter( new StringsCompleter(command), - new StringsCompleter(Common.TxCountLimit), + new StringsCompleter(Common.TX_COUNT_LIMIT), new StringsCompleterIgnoreCase())); completers.add( new ArgumentCompleter( new StringsCompleter(command), - new StringsCompleter(Common.TxGasLimit), + new StringsCompleter(Common.TX_GAS_LIMIT), new StringsCompleterIgnoreCase())); completers.add( new ArgumentCompleter( new StringsCompleter(command), - new StringsCompleter(Common.ConsensusLeaderPeriod), + new StringsCompleter(Common.CONSENSUS_LEADER_PERIOD), new StringsCompleterIgnoreCase())); completers.add( new ArgumentCompleter( new StringsCompleter(command), - new StringsCompleter(Common.compatibilityVersion), + new StringsCompleter(Common.COMPATIBILITY_VERSION), new StringsCompleterIgnoreCase())); completers.add( new ArgumentCompleter( new StringsCompleter(command), - new StringsCompleter(Common.authCheckStatus), + new StringsCompleter(Common.AUTH_CHECK_STATUS), new StringsCompleterIgnoreCase())); } completers.add( diff --git a/src/main/java/console/command/model/HelpInfo.java b/src/main/java/console/command/model/HelpInfo.java index a07ad435..210c4692 100644 --- a/src/main/java/console/command/model/HelpInfo.java +++ b/src/main/java/console/command/model/HelpInfo.java @@ -65,7 +65,7 @@ public static void getDeployLogHelp() { System.out.println("Usage: \ngetDeployLog [recordNumber]"); System.out.println( "* recordNumber -- (optional) The number of deployed contract records, " - + Common.DeployLogIntegerRange + + Common.DEPLOY_LOG_INTEGER_RANGE + "(default 20)."); } @@ -141,7 +141,7 @@ public static void getBlockByNumberHelp() { System.out.println("Usage: \ngetBlockByNumber blockNumber [boolean]"); System.out.println( "* blockNumber -- Integer of a block number, " - + Common.NonNegativeIntegerRange + + Common.NON_NEGATIVE_INTEGER_RANGE + "."); System.out.println( "* boolean -- (optional) If true it returns only the hashes of the transactions, if false then return the full transaction objects."); @@ -158,7 +158,7 @@ public static void getBlockHeaderByNumberHelp() { System.out.println("Usage: \ngetBlockHeaderByNumber blockNumber"); System.out.println( "* blockNumber -- Integer of a block number, " - + Common.NonNegativeIntegerRange + + Common.NON_NEGATIVE_INTEGER_RANGE + "."); } @@ -167,7 +167,7 @@ public static void getBlockHashByNumberHelp() { System.out.println("Usage: \ngetBlockHashByNumber blockNumber"); System.out.println( "* blockNumber -- Integer of a block number, " - + Common.NonNegativeIntegerRange + + Common.NON_NEGATIVE_INTEGER_RANGE + "."); } @@ -351,23 +351,39 @@ public static void initAuthHelp() { public static void setSystemConfigByKeyHelp() { System.out.println("Set a system config."); System.out.println("Usage: \nsetSystemConfigByKey key value"); + systemConfigHelper(); + } + + public static void systemConfigHelper() { + System.out.println("* key -- The name of system config."); System.out.println( - "* key -- The name of system config(tx_count_limit/tx_gas_limit/consensus_leader_period supported currently)."); + " -- supported keys: " + String.join(",", Common.SUPPORTED_SYSTEM_KEYS)); System.out.println("* value -- The value of system config to be set."); System.out.println( - " -- The value of tx_count_limit " - + Common.TxCountLimitRange + " -- the value of " + + Common.COMPATIBILITY_VERSION + + " " + + Common.COMPATIBILITY_VERSION_DESC); + System.out.println( + " -- The value of tx_count_limit " + + Common.SYS_CONFIG_RANGE + "(default 1000)."); System.out.println( - " -- the value of tx_gas_limit " - + Common.TxGasLimitRange + " -- the value of tx_gas_limit " + + Common.TX_GAS_LIMIT_RANGE + "(default 3000000000)."); System.out.println( - " -- the value of " - + Common.ConsensusLeaderPeriod + " -- the value of " + + Common.CONSENSUS_LEADER_PERIOD + " " - + Common.ConsensusLeaderPeriodRange + + Common.SYS_CONFIG_RANGE + "(default 1)."); + System.out.println( + " -- the value of " + + Common.AUTH_CHECK_STATUS + + " " + + Common.SYS_CONFIG_RANGE + + "(default 0)."); } public static void getSystemConfigByKeyHelp() { @@ -690,27 +706,7 @@ public static void setSysConfigProposalHelp() { + "\033[m"); ConsoleUtils.singleLine(); System.out.println("Usage: setSysConfigProposal [key] [value]"); - System.out.println( - "* key -- The name of system config(tx_count_limit/tx_gas_limit/consensus_leader_period supported currently)."); - System.out.println("* value -- The value of system config to be set."); - System.out.println( - " -- The value of " - + Common.TxCountLimit - + " " - + Common.TxCountLimitRange - + "(default 1000)."); - System.out.println( - " -- the value of " - + Common.TxGasLimit - + " " - + Common.TxGasLimitRange - + "(default 3000000000)."); - System.out.println( - " -- the value of " - + Common.ConsensusLeaderPeriod - + " " - + Common.ConsensusLeaderPeriodRange - + "(default 1)."); + systemConfigHelper(); } public static void upgradeVoteProposalHelp() { diff --git a/src/main/java/console/common/Common.java b/src/main/java/console/common/Common.java index 96843583..20bbd61f 100644 --- a/src/main/java/console/common/Common.java +++ b/src/main/java/console/common/Common.java @@ -1,36 +1,46 @@ package console.common; import java.math.BigInteger; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; public class Common { - public static final String ContractLogFileName = "deploylog.txt"; + private Common() {} + + public static final String CONTRACT_LOG_FILE_NAME = "deploylog.txt"; // SystemConfig key - public static final String TxCountLimit = "tx_count_limit"; - public static final String TxGasLimit = "tx_gas_limit"; - public static final String ConsensusLeaderPeriod = "consensus_leader_period"; - public static final String compatibilityVersion = "compatibility_version"; - public static final String authCheckStatus = "auth_check_status"; - - public static final int InvalidReturnNumber = -100; - public static final long InvalidLongValue = Long.MAX_VALUE; - - public static final int QueryLogCount = 20; - public static final int LogMaxCount = 10000; - public static final String PositiveIntegerRange = "from 1 to 2147483647"; - public static final String NonNegativeIntegerRange = "from 0 to 2147483647"; - public static final String DeployLogIntegerRange = "from 1 to 100"; - public static final String TxGasLimitRange = "must be greater than 100000"; - public static final String TxCountLimitRange = "must be no smaller than 1"; - public static final String ConsensusLeaderPeriodRange = "must be no smaller than 1"; + public static final String TX_COUNT_LIMIT = "tx_count_limit"; + public static final String TX_GAS_LIMIT = "tx_gas_limit"; + public static final String CONSENSUS_LEADER_PERIOD = "consensus_leader_period"; + public static final String COMPATIBILITY_VERSION = "compatibility_version"; + public static final String AUTH_CHECK_STATUS = "auth_check_status"; + + public static final List SUPPORTED_SYSTEM_KEYS = + new ArrayList<>( + Arrays.asList( + TX_COUNT_LIMIT, + TX_GAS_LIMIT, + CONSENSUS_LEADER_PERIOD, + COMPATIBILITY_VERSION, + AUTH_CHECK_STATUS)); + + public static final int INVALID_RETURN_NUMBER = -100; + public static final long INVALID_LONG_VALUE = Long.MAX_VALUE; + + public static final int QUERY_LOG_COUNT = 20; + public static final int LOG_MAX_COUNT = 10000; + public static final String NON_NEGATIVE_INTEGER_RANGE = "from 0 to 2147483647"; + public static final String DEPLOY_LOG_INTEGER_RANGE = "from 1 to 100"; + public static final String TX_GAS_LIMIT_RANGE = "must be greater than 100000"; + public static final String SYS_CONFIG_RANGE = "must be greater than 1"; + public static final String COMPATIBILITY_VERSION_DESC = + "must be in this format: 3.0.0, 3.1.0, etc. Latest version now is " + + ConsoleVersion.Version; public static final String EMPTY_CONTRACT_ADDRESS = "0x0000000000000000000000000000000000000000"; - public static final String EMPTY_OUTPUT = "0x"; - public static final int TxGasLimitMin = 100000; - - public static int SYS_TABLE_KEY_MAX_LENGTH = 48; - // BFS common public static final String BFS_TYPE_DIR = "directory"; public static final String BFS_TYPE_CON = "contract"; diff --git a/src/main/java/console/common/ConsoleUtils.java b/src/main/java/console/common/ConsoleUtils.java index 4c9e18e9..b370ec83 100644 --- a/src/main/java/console/common/ConsoleUtils.java +++ b/src/main/java/console/common/ConsoleUtils.java @@ -220,7 +220,7 @@ public static long processLong(String name, String number, long minValue, long m + " and " + maxValue + "."); - return Common.InvalidLongValue; + return Common.INVALID_LONG_VALUE; } return value; } catch (NumberFormatException e) { @@ -234,7 +234,7 @@ public static long processLong(String name, String number, long minValue, long m + Long.MAX_VALUE + "."); logger.debug("processLong for {} failed, error info: {}", name, e.getMessage()); - return Common.InvalidLongValue; + return Common.INVALID_LONG_VALUE; } } @@ -256,7 +256,7 @@ public static int processNonNegativeNumber( + " and " + maxValue + "."); - return Common.InvalidReturnNumber; + return Common.INVALID_RETURN_NUMBER; } } catch (NumberFormatException e) { System.out.println("Invalid " + name + ": \"" + intStr + "\"!"); @@ -268,7 +268,7 @@ public static int processNonNegativeNumber( + " and " + maxValue + "."); - return Common.InvalidReturnNumber; + return Common.INVALID_RETURN_NUMBER; } return intParam; } diff --git a/src/main/java/console/contract/ConsoleContractImpl.java b/src/main/java/console/contract/ConsoleContractImpl.java index be5fcc8d..7799a79a 100644 --- a/src/main/java/console/contract/ConsoleContractImpl.java +++ b/src/main/java/console/contract/ConsoleContractImpl.java @@ -405,40 +405,40 @@ public TransactionResponse deployWasm( private synchronized void writeLog(String contractName, String contractAddress) { contractName = ConsoleUtils.removeSolSuffix(contractName); - File logFile = new File(Common.ContractLogFileName); + File logFile = new File(Common.CONTRACT_LOG_FILE_NAME); try { if (!logFile.exists() && !logFile.createNewFile()) { - System.out.println("Failed to create log file: " + Common.ContractLogFileName); + System.out.println("Failed to create log file: " + Common.CONTRACT_LOG_FILE_NAME); return; } } catch (IOException e) { - System.out.println("Failed to create log file: " + Common.ContractLogFileName); + System.out.println("Failed to create log file: " + Common.CONTRACT_LOG_FILE_NAME); logger.error("create file exception", e); return; } try (BufferedReader reader = - new BufferedReader(new FileReader(Common.ContractLogFileName)); + new BufferedReader(new FileReader(Common.CONTRACT_LOG_FILE_NAME)); PrintWriter pw = - new PrintWriter(new FileWriter(Common.ContractLogFileName, true)); ) { + new PrintWriter(new FileWriter(Common.CONTRACT_LOG_FILE_NAME, true)); ) { String line; List textList = new ArrayList<>(); while ((line = reader.readLine()) != null) { textList.add(line); } int i = 0; - if (textList.size() >= Common.LogMaxCount) { - i = textList.size() - Common.LogMaxCount + 1; + if (textList.size() >= Common.LOG_MAX_COUNT) { + i = textList.size() - Common.LOG_MAX_COUNT + 1; if (logFile.exists()) { if (!logFile.delete()) { System.out.println( - "Failed to delete log file: " + Common.ContractLogFileName); + "Failed to delete log file: " + Common.CONTRACT_LOG_FILE_NAME); return; } if (!logFile.createNewFile()) { System.out.println( - "Failed to create log file: " + Common.ContractLogFileName); + "Failed to create log file: " + Common.CONTRACT_LOG_FILE_NAME); return; } } @@ -465,9 +465,10 @@ private synchronized void writeLog(String contractName, String contractAddress) + contractName + " " + contractAddress; - try (PrintWriter pw = new PrintWriter(new FileWriter(Common.ContractLogFileName, true))) { + try (PrintWriter pw = + new PrintWriter(new FileWriter(Common.CONTRACT_LOG_FILE_NAME, true))) { if (!logFile.exists() && !logFile.createNewFile()) { - System.out.println("Failed to create file " + Common.ContractLogFileName); + System.out.println("Failed to create file " + Common.CONTRACT_LOG_FILE_NAME); } pw.println(log); pw.flush(); @@ -480,7 +481,7 @@ private synchronized void writeLog(String contractName, String contractAddress) @Override public void getDeployLog(String[] params) throws Exception { String queryRecordNumber = ""; - int recordNumber = Common.QueryLogCount; + int recordNumber = Common.QUERY_LOG_COUNT; if (params.length == 2) { queryRecordNumber = params[1]; try { @@ -488,24 +489,24 @@ public void getDeployLog(String[] params) throws Exception { if (recordNumber <= 0 || recordNumber > 100) { System.out.println( "Please provide record number by integer mode, " - + Common.DeployLogIntegerRange + + Common.DEPLOY_LOG_INTEGER_RANGE + "."); return; } } catch (NumberFormatException e) { System.out.println( "Please provide record number by integer mode, " - + Common.DeployLogIntegerRange + + Common.DEPLOY_LOG_INTEGER_RANGE + "."); return; } } - File logFile = new File(Common.ContractLogFileName); + File logFile = new File(Common.CONTRACT_LOG_FILE_NAME); if (!logFile.exists() && !logFile.createNewFile()) { - System.out.println("Failed to create file " + Common.ContractLogFileName); + System.out.println("Failed to create file " + Common.CONTRACT_LOG_FILE_NAME); return; } - BufferedReader reader = new BufferedReader(new FileReader(Common.ContractLogFileName)); + BufferedReader reader = new BufferedReader(new FileReader(Common.CONTRACT_LOG_FILE_NAME)); String line; String ls = System.getProperty("line.separator"); List textList = new ArrayList(); @@ -535,7 +536,7 @@ public void getDeployLog(String[] params) throws Exception { System.out.println(stringBuilder); } } catch (Exception e) { - logger.error(" load {} failed, e: {}", Common.ContractLogFileName, e); + logger.error(" load {} failed, e: {}", Common.CONTRACT_LOG_FILE_NAME, e); } finally { reader.close(); } @@ -919,7 +920,7 @@ public void listDeployContractAddress(ConsoleInitializer consoleInitializer, Str recordNum = ConsoleUtils.processNonNegativeNumber( "recordNum", params[2], 1, Integer.MAX_VALUE); - if (recordNum == Common.InvalidReturnNumber) { + if (recordNum == Common.INVALID_RETURN_NUMBER) { return; } } diff --git a/src/main/java/console/precompiled/PrecompiledImpl.java b/src/main/java/console/precompiled/PrecompiledImpl.java index 1e235c69..adce9e83 100644 --- a/src/main/java/console/precompiled/PrecompiledImpl.java +++ b/src/main/java/console/precompiled/PrecompiledImpl.java @@ -1,6 +1,6 @@ package console.precompiled; -import static console.common.Common.compatibilityVersion; +import static console.common.Common.COMPATIBILITY_VERSION; import console.ConsoleInitializer; import console.common.Common; @@ -120,7 +120,7 @@ public void setSystemConfigByKey(ConsoleInitializer consoleInitializer, String[] String value = params[2]; RetCode retCode = this.systemConfigService.setValueByKey(key, value); ConsoleUtils.printJson(retCode.toString()); - if (key.equals(compatibilityVersion) + if (key.equals(COMPATIBILITY_VERSION) && retCode.code == PrecompiledRetCode.CODE_SUCCESS.code) { String[] param = new String[2]; param[1] = consoleInitializer.getGroupID(); From 5d0b0e22c6f1a711ebecc5034a0afe6e8a92fc0c Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Mon, 27 Feb 2023 21:27:13 +0800 Subject: [PATCH 10/18] (console,precompiled): support struct abi print, fix changeDir bug when files count over 500. (#746) --- build.gradle | 4 +- .../java/console/common/ConsoleUtils.java | 82 ++++++++++++++++- .../console/contract/ConsoleContractImpl.java | 91 +++---------------- .../console/precompiled/PrecompiledImpl.java | 47 ++++++---- 4 files changed, 124 insertions(+), 100 deletions(-) diff --git a/build.gradle b/build.gradle index 261506d5..05b41092 100644 --- a/build.gradle +++ b/build.gradle @@ -51,11 +51,11 @@ dependencies { compile('org.jline:jline:3.21.0') compile('io.bretty:console-table-builder:1.2') compile('com.github.jsqlparser:jsqlparser:2.0') - compile('org.fisco-bcos.code-generator:bcos-code-generator:1.0.0') { + compile('org.fisco-bcos.code-generator:bcos-code-generator:1.1.0-SNAPSHOT') { exclude group: "org.fisco-bcos.java-sdk" exclude group: "org.slf4j" } - compile ('com.fasterxml.jackson.core:jackson-databind:2.14.0'){ + compile ('com.fasterxml.jackson.core:jackson-databind:2.14.2'){ force true } testCompile('com.github.stefanbirkner:system-rules:1.19.0') diff --git a/src/main/java/console/common/ConsoleUtils.java b/src/main/java/console/common/ConsoleUtils.java index b370ec83..f3e13f5d 100644 --- a/src/main/java/console/common/ConsoleUtils.java +++ b/src/main/java/console/common/ConsoleUtils.java @@ -37,6 +37,8 @@ import org.fisco.bcos.sdk.v3.codec.datatypes.StructType; import org.fisco.bcos.sdk.v3.codec.datatypes.Type; 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.Numeric; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -579,14 +581,16 @@ public static String getContractNameWithoutCheckExists(String contractNameOrPath public static String bytesToHex(byte[] bytes) { String strHex = ""; - StringBuilder sb = new StringBuilder(""); - for (int n = 0; n < bytes.length; n++) { - strHex = Integer.toHexString(bytes[n] & 0xFF); + StringBuilder sb = new StringBuilder(); + for (byte aByte : bytes) { + strHex = Integer.toHexString(aByte & 0xFF); sb.append((strHex.length() == 1) ? "0" + strHex : strHex); } return sb.toString().trim(); } + // for compatibility, if AbiObject not exist, use this method print results + @Deprecated public static void getReturnResults( StringBuilder resultType, StringBuilder resultData, Type result) { if (result instanceof Array) { @@ -619,6 +623,7 @@ public static void getReturnResults( } } + @Deprecated public static void printReturnResults(List results) { if (results == null) { return; @@ -641,6 +646,77 @@ public static void printReturnResults(List results) { System.out.println("Return values:" + resultData); } + public static void printResults( + List returnABIObject, List returnObject, List results) { + if (returnABIObject == null + || returnObject == null + || returnObject.isEmpty() + || returnABIObject.isEmpty()) { + // if AbiObject not exist, use this method print results + printReturnResults(results); + return; + } + StringBuilder resultType = new StringBuilder(); + StringBuilder resultData = new StringBuilder(); + resultType.append("("); + resultData.append("("); + getReturnObjectOutputData(resultType, resultData, returnObject, returnABIObject); + if (resultType.toString().endsWith(", ")) { + resultType.delete(resultType.length() - 2, resultType.length()); + } + if (resultData.toString().endsWith(", ")) { + resultData.delete(resultData.length() - 2, resultData.length()); + } + resultType.append(")"); + resultData.append(")"); + System.out.println("Return value size:" + returnObject.size()); + System.out.println("Return types: " + resultType); + System.out.println("Return values:" + resultData); + } + + public static void getReturnObjectOutputData( + StringBuilder resultType, + StringBuilder resultData, + List returnObject, + List returnABIObject) { + int i = 0; + for (ABIObject abiObject : returnABIObject) { + if (abiObject.getListValues() != null) { + resultType.append("["); + resultData.append("["); + getReturnObjectOutputData( + resultType, + resultData, + (List) returnObject.get(i), + abiObject.getListValues()); + if (resultType.toString().endsWith(", ")) { + resultType.delete(resultType.length() - 2, resultType.length()); + } + if (resultData.toString().endsWith(", ")) { + resultData.delete(resultData.length() - 2, resultData.length()); + } + resultData.append("] "); + resultType.append("] "); + i += 1; + continue; + } + if (abiObject.getValueType() == null && returnObject.size() > i) { + resultData.append(returnObject.get(i).toString()).append(", "); + i += 1; + continue; + } + resultType.append(abiObject.getValueType()).append(", "); + if (abiObject.getValueType().equals(ABIObject.ValueType.BYTES) + || abiObject.getValueType().equals(ABIObject.ValueType.DBYTES)) { + String data = "hex://0x" + bytesToHex(ContractCodecTools.formatBytesN(abiObject)); + resultData.append(data).append(", "); + } else if (returnObject.size() > i) { + resultData.append(returnObject.get(i).toString()).append(", "); + } + i += 1; + } + } + public static void main(String[] args) { if (args.length < 1) { diff --git a/src/main/java/console/contract/ConsoleContractImpl.java b/src/main/java/console/contract/ConsoleContractImpl.java index 7799a79a..ea27cfd6 100644 --- a/src/main/java/console/contract/ConsoleContractImpl.java +++ b/src/main/java/console/contract/ConsoleContractImpl.java @@ -42,9 +42,7 @@ import org.fisco.bcos.sdk.v3.codec.EventEncoder; import org.fisco.bcos.sdk.v3.codec.wrapper.ABIDefinition; import org.fisco.bcos.sdk.v3.codec.wrapper.ABIDefinitionFactory; -import org.fisco.bcos.sdk.v3.codec.wrapper.ABIObject; import org.fisco.bcos.sdk.v3.codec.wrapper.ContractABIDefinition; -import org.fisco.bcos.sdk.v3.codec.wrapper.ContractCodecTools; import org.fisco.bcos.sdk.v3.contract.precompiled.bfs.BFSService; import org.fisco.bcos.sdk.v3.crypto.keypair.CryptoKeyPair; import org.fisco.bcos.sdk.v3.model.CryptoType; @@ -211,75 +209,6 @@ private void deployLink(String linkPath, String address, String abiString) throw System.out.println("link path: " + linkPath); } - public void printReturnObject( - List returnObject, List returnABIObject, String returnValue) { - if (returnABIObject == null || returnObject.isEmpty() || returnABIObject.isEmpty()) { - System.out.println("Return values:" + returnValue); - return; - } - StringBuilder resultType = new StringBuilder(); - StringBuilder resultData = new StringBuilder(); - resultType.append("("); - resultData.append("("); - getReturnObjectOutputData(resultType, resultData, returnObject, returnABIObject); - if (resultType.toString().endsWith(", ")) { - resultType.delete(resultType.length() - 2, resultType.length()); - } - if (resultData.toString().endsWith(", ")) { - resultData.delete(resultData.length() - 2, resultData.length()); - } - resultType.append(")"); - resultData.append(")"); - System.out.println("Return value size:" + returnObject.size()); - System.out.println("Return types: " + resultType); - System.out.println("Return values:" + resultData); - } - - public void getReturnObjectOutputData( - StringBuilder resultType, - StringBuilder resultData, - List returnObject, - List returnABIObject) { - int i = 0; - for (ABIObject abiObject : returnABIObject) { - if (abiObject.getListValues() != null) { - resultType.append("["); - resultData.append("["); - getReturnObjectOutputData( - resultType, - resultData, - (List) returnObject.get(i), - abiObject.getListValues()); - if (resultType.toString().endsWith(", ")) { - resultType.delete(resultType.length() - 2, resultType.length()); - } - if (resultData.toString().endsWith(", ")) { - resultData.delete(resultData.length() - 2, resultData.length()); - } - resultData.append("] "); - resultType.append("] "); - i += 1; - continue; - } - if (abiObject.getValueType() == null && returnObject.size() > i) { - resultData.append(returnObject.get(i).toString()).append(", "); - i += 1; - continue; - } - resultType.append(abiObject.getValueType()).append(", "); - if (abiObject.getValueType().equals(ABIObject.ValueType.BYTES)) { - String data = - "hex://0x" - + ConsoleUtils.bytesToHex( - ContractCodecTools.formatBytesN(abiObject)); - resultData.append(data).append(", "); - } else if (returnObject.size() > i) { - resultData.append(returnObject.get(i).toString()).append(", "); - } - i += 1; - } - } - public TransactionResponse deploySolidity( String contractName, String contractNameOrPath, List inputParams) throws ConsoleMessageException { @@ -314,10 +243,10 @@ public TransactionResponse deploySolidity( System.out.println("deploy contract for " + contractName + " failed!"); System.out.println("return message: " + response.getReturnMessage()); System.out.println("return code:" + response.getReturnCode()); - printReturnObject( - response.getReturnObject(), + ConsoleUtils.printResults( response.getReturnABIObject(), - response.getValues()); + response.getReturnObject(), + response.getResults()); return response; } String contractAddress = response.getTransactionReceipt().getContractAddress(); @@ -368,10 +297,10 @@ public TransactionResponse deployWasm( System.out.println("deploy contract for " + path + " failed!"); System.out.println("return message: " + response.getReturnMessage()); System.out.println("return code:" + response.getReturnCode()); - printReturnObject( - response.getReturnObject(), + ConsoleUtils.printResults( response.getReturnABIObject(), - response.getValues()); + response.getReturnObject(), + response.getResults()); return response; } @@ -786,7 +715,8 @@ private void sendTransaction( ConsoleUtils.singleLine(); System.out.println("Receipt message: " + response.getReceiptMessages()); System.out.println("Return message: " + response.getReturnMessage()); - ConsoleUtils.printReturnResults(response.getResults()); + ConsoleUtils.printResults( + response.getReturnABIObject(), response.getReturnObject(), response.getResults()); ConsoleUtils.singleLine(); if (response.getEvents() != null && !response.getEvents().isEmpty()) { System.out.println("Event logs"); @@ -825,7 +755,10 @@ private void sendCall( System.out.println("description: " + "transaction executed successfully"); System.out.println("Return message: " + response.getReturnMessage()); ConsoleUtils.singleLine(); - ConsoleUtils.printReturnResults(response.getResults()); + ConsoleUtils.printResults( + response.getReturnABIObject(), + response.getReturnObject(), + response.getResults()); } else { String errorMessage = response.getReturnMessage(); System.out.println( diff --git a/src/main/java/console/precompiled/PrecompiledImpl.java b/src/main/java/console/precompiled/PrecompiledImpl.java index adce9e83..edcfa36c 100644 --- a/src/main/java/console/precompiled/PrecompiledImpl.java +++ b/src/main/java/console/precompiled/PrecompiledImpl.java @@ -25,6 +25,7 @@ import org.fisco.bcos.sdk.v3.client.Client; import org.fisco.bcos.sdk.v3.client.protocol.response.Abi; import org.fisco.bcos.sdk.v3.codec.datatypes.generated.tuples.generated.Tuple2; +import org.fisco.bcos.sdk.v3.contract.precompiled.bfs.BFSInfo; import org.fisco.bcos.sdk.v3.contract.precompiled.bfs.BFSPrecompiled.BfsInfo; import org.fisco.bcos.sdk.v3.contract.precompiled.bfs.BFSService; import org.fisco.bcos.sdk.v3.contract.precompiled.consensus.ConsensusService; @@ -465,26 +466,40 @@ public void changeDir(String[] params) throws Exception { pwd = path; return; } - Tuple2 parentAndBase = ConsoleUtils.getParentPathAndBaseName(path); - String parentDir = parentAndBase.getValue1(); - String baseName = parentAndBase.getValue2(); - List listResult = bfsService.list(parentDir); - if (!listResult.isEmpty()) { - boolean findFlag = false; - for (BfsInfo bfsInfo : listResult) { - if (bfsInfo.getFileName().equals(baseName)) { - findFlag = true; - if (!bfsInfo.getFileType().equals(Common.BFS_TYPE_DIR)) { - throw new Exception("cd: not a directory: " + bfsInfo.getFileName()); - } + EnumNodeVersion.Version supportedVersion = + EnumNodeVersion.valueOf((int) bfsService.getCurrentVersion()).toVersionObj(); + if (supportedVersion.compareTo(EnumNodeVersion.BCOS_3_1_0.toVersionObj()) >= 0) { + BFSInfo bfsInfo = bfsService.isExist(path); + if (bfsInfo != null) { + if (!bfsInfo.getFileType().equals(Common.BFS_TYPE_DIR)) { + throw new Exception("cd: not a directory: " + bfsInfo.getFileName()); } - } - if (!findFlag) { + } else { logger.error("cd: no such file or directory: '{}'", path); - throw new Exception("cd: no such file or directory: " + baseName); + throw new Exception("cd: no such file or directory: " + params[1]); } } else { - throw new Exception("cd: no such file or directory: " + params[1]); + Tuple2 parentAndBase = ConsoleUtils.getParentPathAndBaseName(path); + String parentDir = parentAndBase.getValue1(); + String baseName = parentAndBase.getValue2(); + List listResult = bfsService.list(parentDir); + if (!listResult.isEmpty()) { + boolean findFlag = false; + for (BfsInfo bfsInfo : listResult) { + if (bfsInfo.getFileName().equals(baseName)) { + findFlag = true; + if (!bfsInfo.getFileType().equals(Common.BFS_TYPE_DIR)) { + throw new Exception("cd: not a directory: " + bfsInfo.getFileName()); + } + } + } + if (!findFlag) { + logger.error("cd: no such file or directory: '{}'", path); + throw new Exception("cd: no such file or directory: " + baseName); + } + } else { + throw new Exception("cd: no such file or directory: " + params[1]); + } } pwd = path; } From b8db7698b61e559bec83a50ba904ef6aeb39d709 Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Mon, 13 Mar 2023 15:04:32 +0800 Subject: [PATCH 11/18] (console): fix dynamic bytes return bug. (#747) --- src/main/java/console/Console.java | 4 ++-- src/main/java/console/common/ConsoleUtils.java | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/main/java/console/Console.java b/src/main/java/console/Console.java index a1b35bff..0d56961d 100644 --- a/src/main/java/console/Console.java +++ b/src/main/java/console/Console.java @@ -53,8 +53,8 @@ public static void main(String[] args) { } consoleInitializer.setLineReader(lineReader); } catch (Exception e) { - System.out.println(e.getMessage()); - logger.error(" message: {}, e: {}", e.getMessage(), e); + e.printStackTrace(); + logger.error(" message: {}", e.getMessage(), e); System.exit(-1); } diff --git a/src/main/java/console/common/ConsoleUtils.java b/src/main/java/console/common/ConsoleUtils.java index f3e13f5d..474f88c0 100644 --- a/src/main/java/console/common/ConsoleUtils.java +++ b/src/main/java/console/common/ConsoleUtils.java @@ -706,10 +706,12 @@ public static void getReturnObjectOutputData( continue; } resultType.append(abiObject.getValueType()).append(", "); - if (abiObject.getValueType().equals(ABIObject.ValueType.BYTES) - || abiObject.getValueType().equals(ABIObject.ValueType.DBYTES)) { + if (abiObject.getValueType().equals(ABIObject.ValueType.BYTES)) { String data = "hex://0x" + bytesToHex(ContractCodecTools.formatBytesN(abiObject)); resultData.append(data).append(", "); + } else if (abiObject.getValueType().equals(ABIObject.ValueType.DBYTES)) { + String data = "hex://0x" + bytesToHex(abiObject.getDynamicBytesValue().getValue()); + resultData.append(data).append(", "); } else if (returnObject.size() > i) { resultData.append(returnObject.get(i).toString()).append(", "); } From a857b6c4b4a7d691499c722a04c3f1e2a030fbec Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Mon, 13 Mar 2023 18:53:38 +0800 Subject: [PATCH 12/18] (tools): fix get_account not support openssl 3.x. (#748) --- tools/get_account.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/get_account.sh b/tools/get_account.sh index ca0e0d58..ff582fda 100755 --- a/tools/get_account.sh +++ b/tools/get_account.sh @@ -55,7 +55,7 @@ LOG_INFO() } check_env() { - [ ! -z "$(openssl version | grep 1.0.2)" ] || [ ! -z "$(openssl version | grep 1.1)" ] || [ ! -z "$(openssl version | grep reSSL)" ] || { + [ ! -z "$(openssl version | grep 1.0.2)" ] || [ ! -z "$(openssl version | grep 1.1)" ] || [ ! -z "$(openssl version | grep '3.')" ] || [ ! -z "$(openssl version | grep reSSL)" ] || { echo "please install openssl! use \"openssl version\" command to check." LOG_INFO " Ubuntu : sudo apt install -y openssl" LOG_INFO " CentOS : sudo yum install -y openssl" From 7f7276ca3c402066fafb12f254b2d475b0e9020d Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Fri, 31 Mar 2023 17:21:55 +0800 Subject: [PATCH 13/18] (console): fix address check bug, adapt new codeGen, more print for get_account, add TableTest for distinguish Table interfaces. (#749) --- .ci/ci_check.sh | 4 +- src/main/java/console/auth/AuthImpl.java | 2 +- .../console/client/ConsoleClientImpl.java | 3 +- .../command/completer/AccountCompleter.java | 4 +- .../completer/ConsoleFilesCompleter.java | 2 +- .../completer/ContractAddressCompleter.java | 3 +- .../java/console/common/ConsoleUtils.java | 49 +++-- .../console/contract/ConsoleContractImpl.java | 7 +- .../resources/contract/solidity/Table.sol | 54 +++-- .../resources/contract/solidity/TableTest.sol | 208 +++++++----------- .../contract/solidity/TableTestV320.sol | 131 +++++++++++ .../resources/contract/solidity/TableV320.sol | 87 ++++++++ tools/get_account.sh | 2 +- tools/get_gm_account.sh | 2 +- 14 files changed, 374 insertions(+), 184 deletions(-) create mode 100644 src/main/resources/contract/solidity/TableTestV320.sol create mode 100644 src/main/resources/contract/solidity/TableV320.sol diff --git a/.ci/ci_check.sh b/.ci/ci_check.sh index 5d6bd9f1..fe106be9 100755 --- a/.ci/ci_check.sh +++ b/.ci/ci_check.sh @@ -1,7 +1,7 @@ #!/bin/bash set -e -default_tag=v3.1.1 +default_tag=v3.2.0 LOG_INFO() { local content=${1} echo -e "\033[32m ${content}\033[0m" @@ -48,8 +48,6 @@ build_node() local node_type="${1}" if [ "${node_type}" == "sm" ];then bash -x build_chain.sh -l 127.0.0.1:4 -s -A - sed_cmd=$(get_sed_cmd) - $sed_cmd 's/sm_crypto_channel=false/sm_crypto_channel=true/g' nodes/127.0.0.1/node*/config.ini else bash -x build_chain.sh -l 127.0.0.1:4 -A fi diff --git a/src/main/java/console/auth/AuthImpl.java b/src/main/java/console/auth/AuthImpl.java index 29225116..a6bfdd9c 100644 --- a/src/main/java/console/auth/AuthImpl.java +++ b/src/main/java/console/auth/AuthImpl.java @@ -646,7 +646,7 @@ public void initAuth(String[] params) throws Exception { } void checkValidAddress(String address, String valueName) throws TransactionException { - if (!ConsoleUtils.isValidAddress(address)) { + if (!AddressUtils.isValidAddress(address)) { throw new TransactionException( "Invalid address " + (valueName.isEmpty() ? "" : ("for " + valueName)) diff --git a/src/main/java/console/client/ConsoleClientImpl.java b/src/main/java/console/client/ConsoleClientImpl.java index 6cc9f6d1..34033ab3 100644 --- a/src/main/java/console/client/ConsoleClientImpl.java +++ b/src/main/java/console/client/ConsoleClientImpl.java @@ -24,6 +24,7 @@ import org.fisco.bcos.sdk.v3.crypto.keypair.CryptoKeyPair; import org.fisco.bcos.sdk.v3.model.CryptoType; import org.fisco.bcos.sdk.v3.model.TransactionReceipt; +import org.fisco.bcos.sdk.v3.utils.AddressUtils; import org.fisco.bcos.sdk.v3.utils.Numeric; import org.fisco.bcos.sdk.v3.utils.ObjectMapperFactory; import org.slf4j.Logger; @@ -264,7 +265,7 @@ public void getPendingTxSize(String[] params) throws IOException { public void getCode(String[] params, boolean isWasm, String pwd) throws IOException { String address = params[1]; if (!isWasm) { - if (!ConsoleUtils.isValidAddress(address)) { + if (!AddressUtils.isValidAddress(address)) { System.out.println("This address is invalid."); return; } diff --git a/src/main/java/console/command/completer/AccountCompleter.java b/src/main/java/console/command/completer/AccountCompleter.java index b66acc48..ba7738bb 100644 --- a/src/main/java/console/command/completer/AccountCompleter.java +++ b/src/main/java/console/command/completer/AccountCompleter.java @@ -1,10 +1,10 @@ package console.command.completer; import console.client.ConsoleClientImpl; -import console.common.ConsoleUtils; import java.io.File; import java.util.List; import org.fisco.bcos.sdk.v3.client.Client; +import org.fisco.bcos.sdk.v3.utils.AddressUtils; import org.jline.reader.Candidate; import org.jline.reader.LineReader; import org.jline.reader.ParsedLine; @@ -35,7 +35,7 @@ public void complete(LineReader reader, ParsedLine commandLine, List // list the account String prefix = "[ Account."; for (String account : accountList) { - if (!ConsoleUtils.isValidAddress(account)) { + if (!AddressUtils.isValidAddress(account)) { continue; } candidates.add( diff --git a/src/main/java/console/command/completer/ConsoleFilesCompleter.java b/src/main/java/console/command/completer/ConsoleFilesCompleter.java index 01ab4530..cc26d75f 100644 --- a/src/main/java/console/command/completer/ConsoleFilesCompleter.java +++ b/src/main/java/console/command/completer/ConsoleFilesCompleter.java @@ -29,7 +29,7 @@ public class ConsoleFilesCompleter extends Completers.FilesCompleter { private boolean isWasm = false; static { - EXCLUDE_SOL.add("Table.sol"); + EXCLUDE_SOL.add("TableV320.sol"); EXCLUDE_SOL.add("Crypto.sol"); } diff --git a/src/main/java/console/command/completer/ContractAddressCompleter.java b/src/main/java/console/command/completer/ContractAddressCompleter.java index cd19a930..80d1eea3 100644 --- a/src/main/java/console/command/completer/ContractAddressCompleter.java +++ b/src/main/java/console/command/completer/ContractAddressCompleter.java @@ -5,6 +5,7 @@ import java.io.File; import java.util.List; import org.fisco.bcos.sdk.v3.client.Client; +import org.fisco.bcos.sdk.v3.utils.AddressUtils; import org.jline.reader.Candidate; import org.jline.reader.LineReader; import org.jline.reader.ParsedLine; @@ -58,7 +59,7 @@ public void complete(LineReader reader, ParsedLine commandLine, List int recordNum = 0; for (File contractAddressFile : contractAddressFiles) { - if (!ConsoleUtils.isValidAddress(contractAddressFile.getName())) { + if (!AddressUtils.isValidAddress(contractAddressFile.getName())) { continue; } candidates.add( diff --git a/src/main/java/console/common/ConsoleUtils.java b/src/main/java/console/common/ConsoleUtils.java index 474f88c0..1bc63ead 100644 --- a/src/main/java/console/common/ConsoleUtils.java +++ b/src/main/java/console/common/ConsoleUtils.java @@ -39,7 +39,6 @@ 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.Numeric; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -313,7 +312,8 @@ public static void compileSolToJava( String binDir, String librariesOption, String specifyContract, - boolean isContractParallelAnalysis) + boolean isContractParallelAnalysis, + boolean enableAsyncCall) throws IOException, CompileContractException { String contractName = solFile.getName().split("\\.")[0]; @@ -343,15 +343,19 @@ public static void compileSolToJava( new File(abiDir + "/sm/" + contractName + ".abi"), abiAndBin.getAbi()); FileUtils.writeStringToFile(smBinFile, abiAndBin.getSmBin()); - CodeGenMain.main( - Arrays.asList( + List args = + new ArrayList<>( + Arrays.asList( "-v", "V3", "-a", abiFilePath, "-b", binFilePath, "-s", smBinFilePath, "-p", packageName, - "-o", javaDir) - .toArray(new String[0])); + "-o", javaDir)); + if (enableAsyncCall) { + args.add("-e"); + } + CodeGenMain.main(args.toArray(new String[0])); System.out.println( "*** Convert solidity to java for " + solFile.getName() + " success ***\n"); } @@ -362,7 +366,8 @@ public static void compileAllSolToJava( File solFileList, String abiDir, String binDir, - boolean isContractParallelAnalysis) + boolean isContractParallelAnalysis, + boolean enableAsyncCall) throws IOException { File[] solFiles = solFileList.listFiles(); if (solFiles.length == 0) { @@ -386,7 +391,8 @@ public static void compileAllSolToJava( binDir, null, null, - isContractParallelAnalysis); + isContractParallelAnalysis, + enableAsyncCall); } catch (Exception e) { System.out.println( "ERROR:convert solidity to java for " @@ -486,12 +492,6 @@ public static void sortFiles(File[] files) { }); } - public static boolean isValidAddress(String address) { - String addressNoPrefix = Numeric.cleanHexPrefix(address); - return addressNoPrefix.length() == ADDRESS_LENGTH_IN_HEX - && addressNoPrefix.matches("^[0-9a-fA-F]{40}$"); - } - public static String getFileCreationTime(File file) { if (file == null) { return null; @@ -781,6 +781,7 @@ public static void main(String[] args) { String ABI_OPTION = "abi"; String NO_ANALYSIS_OPTION = "no-analysis"; + String ENABLE_ASYNC_CALL_OPTION = "enable-async-call"; if (mode.equals("solidity")) { Option solidityFilePathOption = @@ -811,6 +812,14 @@ public static void main(String[] args) { false, "[Optional] NOT use evm static parallel-able analysis. It will not active DAG analysis, but will speedup compile speed."); options.addOption(noAnalysisOption); + + Option enableAsyncCall = + new Option( + "e", + ENABLE_ASYNC_CALL_OPTION, + false, + "[Optional] Enable generate async interfaces for constant call, java file only compilable when java-sdk >= 3.3.0."); + options.addOption(enableAsyncCall); } else if (mode.equals("liquid")) { Option liquidBinPathOption = new Option( @@ -869,6 +878,7 @@ public static void main(String[] args) { String solPathOrDir = cmd.getOptionValue(SOL_OPTION, DEFAULT_SOL); String librariesOption = cmd.getOptionValue(LIBS_OPTION, ""); boolean useDagAnalysis = !cmd.hasOption(NO_ANALYSIS_OPTION); + boolean enableAsyncCall = cmd.hasOption(ENABLE_ASYNC_CALL_OPTION); String fullJavaDir = new File(javaDir).getAbsolutePath(); String specifyContract = null; if (solPathOrDir.contains(":") @@ -892,10 +902,17 @@ public static void main(String[] args) { BIN_PATH, librariesOption, specifyContract, - useDagAnalysis); + useDagAnalysis, + enableAsyncCall); } else { // input dir compileAllSolToJava( - fullJavaDir, pkgName, sol, ABI_PATH, BIN_PATH, useDagAnalysis); + fullJavaDir, + pkgName, + sol, + ABI_PATH, + BIN_PATH, + useDagAnalysis, + enableAsyncCall); } } catch (IOException | CompileContractException e) { System.out.print(e.getMessage()); diff --git a/src/main/java/console/contract/ConsoleContractImpl.java b/src/main/java/console/contract/ConsoleContractImpl.java index ea27cfd6..91dfb405 100644 --- a/src/main/java/console/contract/ConsoleContractImpl.java +++ b/src/main/java/console/contract/ConsoleContractImpl.java @@ -55,6 +55,7 @@ import org.fisco.bcos.sdk.v3.transaction.model.dto.TransactionResponse; import org.fisco.bcos.sdk.v3.transaction.model.exception.ContractException; import org.fisco.bcos.sdk.v3.transaction.model.exception.TransactionBaseException; +import org.fisco.bcos.sdk.v3.utils.AddressUtils; import org.fisco.bcos.sdk.v3.utils.Hex; import org.fisco.bcos.sdk.v3.utils.Numeric; import org.fisco.bcos.sdk.v3.utils.StringUtils; @@ -540,7 +541,7 @@ private void callSolidity(String[] params) throws Exception { ConsoleUtils.sortFiles(contractAddressFiles); for (File contractAddressFile : contractAddressFiles) { if (contractAddressFile.isDirectory() - && ConsoleUtils.isValidAddress(contractAddressFile.getName())) { + && AddressUtils.isValidAddress(contractAddressFile.getName())) { contractAddressStr = contractAddressFile.getName(); break; } @@ -554,7 +555,7 @@ private void callSolidity(String[] params) throws Exception { } // check contract address - if (!ConsoleUtils.isValidAddress(contractAddressStr)) { + if (!AddressUtils.isValidAddress(contractAddressStr)) { System.out.println("Invalid contract address: " + contractAddressStr); return; } @@ -868,7 +869,7 @@ public void listDeployContractAddress(ConsoleInitializer consoleInitializer, Str } ConsoleUtils.sortFiles(contractFileList); for (File contractAddressFile : contractFileList) { - if (!isWasm && !ConsoleUtils.isValidAddress(contractAddressFile.getName())) { + if (!isWasm && !AddressUtils.isValidAddress(contractAddressFile.getName())) { continue; } String contractAddress = diff --git a/src/main/resources/contract/solidity/Table.sol b/src/main/resources/contract/solidity/Table.sol index 1a8c6c9f..65169a9e 100644 --- a/src/main/resources/contract/solidity/Table.sol +++ b/src/main/resources/contract/solidity/Table.sol @@ -1,37 +1,43 @@ // SPDX-License-Identifier: Apache-2.0 +// 该接口文件定义了FISCO BCOS v3.1.0及以前版本的接口,使用时需要将该文件放在合约目录下 +// 若要使用FISCO BCOS v3.2.0及以后版本的接口,请使用TableV320.sol,旧合约仍然能在新节点中使用 pragma solidity >=0.6.10 <0.8.20; pragma experimental ABIEncoderV2; -import "./EntryWrapper.sol"; // KeyOrder指定Key的排序规则,字典序和数字序,如果指定为数字序,key只能为数字 -enum KeyOrder {Lexicographic, Numerical} -struct TableInfo { - KeyOrder keyOrder; - string keyColumn; - string[] valueColumns; -} +// enum KeyOrder {Lexicographic, Numerical} + struct TableInfo { + string keyColumn; + string[] valueColumns; + } + +// 记录,用于select和insert + struct Entry { + string key; + string[] fields; // 考虑2.0的Entry接口,临时Precompiled的问题,考虑加工具类接口 + } // 更新字段,用于update -struct UpdateField { - string columnName; - // 考虑工具类 - string value; -} + struct UpdateField { + string columnName; + // 考虑工具类 + string value; + } // 筛选条件,大于、大于等于、小于、小于等于 -enum ConditionOP {GT, GE, LT, LE, EQ, NE, STARTS_WITH, ENDS_WITH, CONTAINS} -struct Condition { - ConditionOP op; - string field; - string value; -} + enum ConditionOP {GT, GE, LT, LE} + struct Condition { + ConditionOP op; + // string field; + string value; + } // 数量限制 -struct Limit { - uint32 offset; - // count limit max is 500 - uint32 count; -} + struct Limit { + uint32 offset; + // count limit max is 500 + uint32 count; + } // 表管理合约,是静态Precompiled,有固定的合约地址 abstract contract TableManager { @@ -49,7 +55,7 @@ abstract contract TableManager { function appendColumns(string memory path, string[] memory newColumns) public virtual returns (int32); // 获取表信息 - function descWithKeyOrder(string memory tableName) public view virtual returns (TableInfo memory); + function desc(string memory tableName) public view virtual returns (TableInfo memory); } // 表合约,是动态Precompiled,TableManager创建时指定地址 diff --git a/src/main/resources/contract/solidity/TableTest.sol b/src/main/resources/contract/solidity/TableTest.sol index 4e032bb5..05ba86b4 100644 --- a/src/main/resources/contract/solidity/TableTest.sol +++ b/src/main/resources/contract/solidity/TableTest.sol @@ -1,131 +1,79 @@ -// SPDX-License-Identifier: Apache-2.0 -pragma solidity >=0.6.10 <0.8.20; -pragma experimental ABIEncoderV2; - -import "./Table.sol"; -import "./Cast.sol"; - -contract TableTest { - event CreateResult(int256 count); - event InsertResult(int256 count); - event UpdateResult(int256 count); - event RemoveResult(int256 count); - - Cast constant cast = Cast(address(0x100f)); - TableManager constant tm = TableManager(address(0x1002)); - Table table; - string constant TABLE_NAME = "t_testV320"; - constructor () public { - // create table - string[] memory columnNames = new string[](3); - columnNames[0] = "name"; - columnNames[1] = "age"; - columnNames[2] = "status"; - TableInfo memory tf = TableInfo(KeyOrder.Numerical ,"id", columnNames); - - tm.createTable(TABLE_NAME, tf); - address t_address = tm.openTable(TABLE_NAME); - require(t_address!=address(0x0),""); - table = Table(t_address); - } - - function select(int64 id) public view returns (string memory, string memory) - { - Entry memory entry = table.select(cast.s64ToString(id)); - string memory name; - string memory age; - if(entry.fields.length == 3){ - name = entry.fields[0]; - age = entry.fields[1]; - } - return (name, age); - } - - function insert(int64 id, string memory name, string memory age) public returns (int32){ - Entry memory entry = Entry(cast.s64ToString(id), new string[](3)); - entry.fields[0] = name; - entry.fields[1] = age; - entry.fields[2] = "init"; - int32 result = table.insert(entry); - emit InsertResult(result); - return result; - } - - function update(int64 id, string memory name, string memory age) public returns (int32){ - UpdateField[] memory updateFields = new UpdateField[](2); - updateFields[0] = UpdateField("name", name); - updateFields[1] = UpdateField("age", age); - - int32 result = table.update(cast.s64ToString(id), updateFields); - emit UpdateResult(result); - return result; - } - - function remove(int64 id) public returns(int32){ - int32 result = table.remove(cast.s64ToString(id)); - emit RemoveResult(result); - return result; - } - - function select(int64 idLow, int64 idHigh) public view returns (string[] memory) - { - Limit memory limit = Limit(0, 500); - Condition[] memory cond = new Condition[](2); - cond[0] = Condition(ConditionOP.GT, "id", cast.s64ToString(idLow)); - cond[1] = Condition(ConditionOP.LE, "id", cast.s64ToString(idHigh)); - Entry[] memory entries = table.select(cond, limit); - string[] memory names = new string[](entries.length); - for(uint i = 0; i < names.length; i++) - { - names[i] = entries[i].fields[0]; - } - return names; - } - - function count(int64 idLow, int64 idHigh) public view returns (uint32) - { - Condition[] memory cond = new Condition[](2); - cond[0] = Condition(ConditionOP.GT, "id", cast.s64ToString(idLow)); - cond[1] = Condition(ConditionOP.LE, "id", cast.s64ToString(idHigh)); - return table.count(cond); - } - - function update(int64 idLow, int64 idHigh) public returns (int32) - { - UpdateField[] memory updateFields = new UpdateField[](1); - updateFields[0] = UpdateField("status", "updated"); - - Limit memory limit = Limit(0, 500); - Condition[] memory cond = new Condition[](2); - cond[0] = Condition(ConditionOP.GT, "id", cast.s64ToString(idLow)); - cond[1] = Condition(ConditionOP.LE, "id", cast.s64ToString(idHigh)); - return table.update(cond, limit, updateFields); - } - - function remove(int64 idLow, int64 idHigh) public returns (int32) - { - Limit memory limit = Limit(0, 500); - Condition[] memory cond = new Condition[](2); - cond[0] = Condition(ConditionOP.GT, "id", cast.s64ToString(idLow)); - cond[1] = Condition(ConditionOP.LE, "id", cast.s64ToString(idHigh)); - return table.remove(cond, limit); - } - - function createTable(string memory tableName, uint8 keyOrder, string memory key,string[] memory fields) public returns(int256){ - require(keyOrder == 0 || keyOrder == 1); - KeyOrder _keyOrder = KeyOrder.Lexicographic; - if (keyOrder == 1) - { - _keyOrder = KeyOrder.Numerical; - } - TableInfo memory tf = TableInfo(_keyOrder, key, fields); - int32 result = tm.createTable(tableName,tf); - emit CreateResult(result); - return result; - } - - function desc() public view returns(string memory, string[] memory){ - TableInfo memory ti = tm.descWithKeyOrder(TABLE_NAME); - return (ti.keyColumn,ti.valueColumns); - } +// SPDX-License-Identifier: Apache-2.0 +pragma solidity >=0.6.10 <0.8.20; +pragma experimental ABIEncoderV2; + +import "./Table.sol"; + +contract TableTest { + event CreateResult(int256 count); + event InsertResult(int256 count); + event UpdateResult(int256 count); + event RemoveResult(int256 count); + + TableManager constant tm = TableManager(address(0x1002)); + Table table; + string constant TABLE_NAME = "t_test"; + constructor () public{ + // create table + string[] memory columnNames = new string[](2); + columnNames[0] = "name"; + columnNames[1] = "age"; + TableInfo memory tf = TableInfo("id", columnNames); + + tm.createTable(TABLE_NAME, tf); + address t_address = tm.openTable(TABLE_NAME); + require(t_address!=address(0x0),""); + table = Table(t_address); + } + + function select(string memory id) public view returns (string memory,string memory) + { + Entry memory entry = table.select(id); + + string memory name; + string memory age; + if(entry.fields.length==2){ + name = entry.fields[0]; + age = entry.fields[1]; + } + return (name,age); + } + + function insert(string memory id,string memory name,string memory age) public returns (int32){ + string[] memory columns = new string[](2); + columns[0] = name; + columns[1] = age; + Entry memory entry = Entry(id, columns); + int32 result = table.insert(entry); + emit InsertResult(result); + return result; + } + + function update(string memory id, string memory name, string memory age) public returns (int32){ + UpdateField[] memory updateFields = new UpdateField[](2); + updateFields[0] = UpdateField("name", name); + updateFields[1] = UpdateField("age", age); + + int32 result = table.update(id, updateFields); + emit UpdateResult(result); + return result; + } + + function remove(string memory id) public returns(int32){ + int32 result = table.remove(id); + emit RemoveResult(result); + return result; + } + + function createTable(string memory tableName,string memory key,string[] memory fields) public returns(int256){ + TableInfo memory tf = TableInfo(key, fields); + int32 result = tm.createTable(tableName,tf); + emit CreateResult(result); + return result; + } + + function desc() public view returns(string memory, string[] memory){ + TableInfo memory ti = tm.desc(TABLE_NAME); + return (ti.keyColumn,ti.valueColumns); + } } \ No newline at end of file diff --git a/src/main/resources/contract/solidity/TableTestV320.sol b/src/main/resources/contract/solidity/TableTestV320.sol new file mode 100644 index 00000000..b5467e1d --- /dev/null +++ b/src/main/resources/contract/solidity/TableTestV320.sol @@ -0,0 +1,131 @@ +// SPDX-License-Identifier: Apache-2.0 +pragma solidity >=0.6.10 <0.8.20; +pragma experimental ABIEncoderV2; + +import "./TableV320.sol"; +import "./Cast.sol"; + +contract TableTest { + event CreateResult(int256 count); + event InsertResult(int256 count); + event UpdateResult(int256 count); + event RemoveResult(int256 count); + + Cast constant cast = Cast(address(0x100f)); + TableManager constant tm = TableManager(address(0x1002)); + Table table; + string constant TABLE_NAME = "t_testV320"; + constructor () public { + // create table + string[] memory columnNames = new string[](3); + columnNames[0] = "name"; + columnNames[1] = "age"; + columnNames[2] = "status"; + TableInfo memory tf = TableInfo(KeyOrder.Numerical ,"id", columnNames); + + tm.createTable(TABLE_NAME, tf); + address t_address = tm.openTable(TABLE_NAME); + require(t_address!=address(0x0),""); + table = Table(t_address); + } + + function select(int64 id) public view returns (string memory, string memory) + { + Entry memory entry = table.select(cast.s64ToString(id)); + string memory name; + string memory age; + if(entry.fields.length == 3){ + name = entry.fields[0]; + age = entry.fields[1]; + } + return (name, age); + } + + function insert(int64 id, string memory name, string memory age) public returns (int32){ + Entry memory entry = Entry(cast.s64ToString(id), new string[](3)); + entry.fields[0] = name; + entry.fields[1] = age; + entry.fields[2] = "init"; + int32 result = table.insert(entry); + emit InsertResult(result); + return result; + } + + function update(int64 id, string memory name, string memory age) public returns (int32){ + UpdateField[] memory updateFields = new UpdateField[](2); + updateFields[0] = UpdateField("name", name); + updateFields[1] = UpdateField("age", age); + + int32 result = table.update(cast.s64ToString(id), updateFields); + emit UpdateResult(result); + return result; + } + + function remove(int64 id) public returns(int32){ + int32 result = table.remove(cast.s64ToString(id)); + emit RemoveResult(result); + return result; + } + + function select(int64 idLow, int64 idHigh) public view returns (string[] memory) + { + Limit memory limit = Limit(0, 500); + Condition[] memory cond = new Condition[](2); + cond[0] = Condition(ConditionOP.GT, "id", cast.s64ToString(idLow)); + cond[1] = Condition(ConditionOP.LE, "id", cast.s64ToString(idHigh)); + Entry[] memory entries = table.select(cond, limit); + string[] memory names = new string[](entries.length); + for(uint i = 0; i < names.length; i++) + { + names[i] = entries[i].fields[0]; + } + return names; + } + + function count(int64 idLow, int64 idHigh) public view returns (uint32) + { + Condition[] memory cond = new Condition[](2); + cond[0] = Condition(ConditionOP.GT, "id", cast.s64ToString(idLow)); + cond[1] = Condition(ConditionOP.LE, "id", cast.s64ToString(idHigh)); + return table.count(cond); + } + + function update(int64 idLow, int64 idHigh) public returns (int32) + { + UpdateField[] memory updateFields = new UpdateField[](1); + updateFields[0] = UpdateField("status", "updated"); + + Limit memory limit = Limit(0, 500); + Condition[] memory cond = new Condition[](2); + cond[0] = Condition(ConditionOP.GT, "id", cast.s64ToString(idLow)); + cond[1] = Condition(ConditionOP.LE, "id", cast.s64ToString(idHigh)); + return table.update(cond, limit, updateFields); + } + + function remove(int64 idLow, int64 idHigh) public returns (int32) + { + Limit memory limit = Limit(0, 500); + Condition[] memory cond = new Condition[](2); + cond[0] = Condition(ConditionOP.GT, "id", cast.s64ToString(idLow)); + cond[1] = Condition(ConditionOP.LE, "id", cast.s64ToString(idHigh)); + return table.remove(cond, limit); + } + + function createTable(string memory tableName, uint8 keyOrder, string memory key,string[] memory fields) public returns(int256){ + require(keyOrder == 0 || keyOrder == 1); + KeyOrder _keyOrder = KeyOrder.Lexicographic; + if (keyOrder == 1) + { + _keyOrder = KeyOrder.Numerical; + } + TableInfo memory tf = TableInfo(_keyOrder, key, fields); + int32 result = tm.createTable(tableName,tf); + emit CreateResult(result); + return result; + } + + function desc() public view returns(string memory, string[] memory){ + TableInfo memory ti = tm.descWithKeyOrder(TABLE_NAME); + return (ti.keyColumn,ti.valueColumns); + } +} \ No newline at end of file diff --git a/src/main/resources/contract/solidity/TableV320.sol b/src/main/resources/contract/solidity/TableV320.sol new file mode 100644 index 00000000..6c8a70c7 --- /dev/null +++ b/src/main/resources/contract/solidity/TableV320.sol @@ -0,0 +1,87 @@ +// SPDX-License-Identifier: Apache-2.0 +// 该接口文件定义了FISCO BCOS v3.2.0及以后版本的接口,使用时需要将该文件放在合约目录下 +// 若要使用FISCO BCOS v3.1.0及以前版本的接口,请使用Table.sol,旧合约仍然能在新节点中使用 +pragma solidity >=0.6.10 <0.8.20; +pragma experimental ABIEncoderV2; +import "./EntryWrapper.sol"; + +// KeyOrder指定Key的排序规则,字典序和数字序,如果指定为数字序,key只能为数字 +enum KeyOrder {Lexicographic, Numerical} +struct TableInfo { + KeyOrder keyOrder; + string keyColumn; + string[] valueColumns; +} + +// 更新字段,用于update +struct UpdateField { + string columnName; + // 考虑工具类 + string value; +} + +// 筛选条件,大于、大于等于、小于、小于等于 +enum ConditionOP {GT, GE, LT, LE, EQ, NE, STARTS_WITH, ENDS_WITH, CONTAINS} +struct Condition { + ConditionOP op; + string field; + string value; +} + +// 数量限制 +struct Limit { + uint32 offset; + // count limit max is 500 + uint32 count; +} + +// 表管理合约,是静态Precompiled,有固定的合约地址 +abstract contract TableManager { + // 创建表,传入TableInfo + function createTable(string memory path, TableInfo memory tableInfo) public virtual returns (int32); + + // 创建KV表,传入key和value字段名 + function createKVTable(string memory tableName, string memory keyField, string memory valueField) public virtual returns (int32); + + // 只提供给Solidity合约调用时使用 + function openTable(string memory path) public view virtual returns (address); + + // 变更表字段 + // 只能新增字段,不能删除字段,新增的字段默认值为空,不能与原有字段重复 + function appendColumns(string memory path, string[] memory newColumns) public virtual returns (int32); + + // 获取表信息 + function descWithKeyOrder(string memory tableName) public view virtual returns (TableInfo memory); +} + +// 表合约,是动态Precompiled,TableManager创建时指定地址 +abstract contract Table { + // 按key查询entry + function select(string memory key) public virtual view returns (Entry memory); + + // 按条件批量查询entry,condition为空则查询所有记录 + function select(Condition[] memory conditions, Limit memory limit) public virtual view returns (Entry[] memory); + + // 按照条件查询count数据 + function count(Condition[] memory conditions) public virtual view returns (uint32); + + // 插入数据 + function insert(Entry memory entry) public virtual returns (int32); + + // 按key更新entry + function update(string memory key, UpdateField[] memory updateFields) public virtual returns (int32); + + // 按条件批量更新entry,condition为空则更新所有记录 + function update(Condition[] memory conditions, Limit memory limit, UpdateField[] memory updateFields) public virtual returns (int32); + + // 按key删除entry + function remove(string memory key) public virtual returns (int32); + // 按条件批量删除entry,condition为空则删除所有记录 + function remove(Condition[] memory conditions, Limit memory limit) public virtual returns (int32); +} + +abstract contract KVTable { + function get(string memory key) public view virtual returns (bool, string memory); + + function set(string memory key, string memory value) public virtual returns (int32); +} \ No newline at end of file diff --git a/tools/get_account.sh b/tools/get_account.sh index ff582fda..3b4a0a23 100755 --- a/tools/get_account.sh +++ b/tools/get_account.sh @@ -85,7 +85,7 @@ calculate_address_pem() else accountAddress=$(echo ${pubKey}| ${keccak_256_bin} -x -l | tr -d ' -' | tail -c 41) fi - [ ! -z "${no_print}" ] || LOG_INFO "Account Address : 0x${accountAddress}" + [ ! -z "${no_print}" ] || LOG_INFO "Account Address : 0x${accountAddress}" && LOG_INFO "Account privateHex: 0x${privKey}" && LOG_INFO "Account publicHex : 0x${pubKey}" } calculate_address_pkcs12() diff --git a/tools/get_gm_account.sh b/tools/get_gm_account.sh index 976702c7..3a6f043e 100755 --- a/tools/get_gm_account.sh +++ b/tools/get_gm_account.sh @@ -111,7 +111,7 @@ calculate_address_pem() pubKey=$(${TASSL_CMD} ec -in ${pem_file} -text -noout 2>/dev/null| sed -n '7,11p' | tr -d ": \n" | awk '{print substr($0,3);}') # echo "public key = ${pubKey}" accountAddress=$(${sm3_bin} ${pubKey}) - [ ! -z "${no_print}" ] || LOG_INFO "Account Address : 0x${accountAddress}" + [ ! -z "${no_print}" ] || LOG_INFO "Account Address : 0x${accountAddress}" && LOG_INFO "Account privateHex: 0x${privKey}" && LOG_INFO "Account publicHex : 0x${pubKey}" } calculate_address_pkcs12() From db2fd8baa7a9d4e556ac3735cc661d9e5768b925 Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Tue, 4 Apr 2023 00:45:32 +0800 Subject: [PATCH 14/18] (contracts): fix tableTest contracts bug. (#750) --- .ci/ci_check.sh | 72 +++++++++++++++---- .../java/console/common/ConsoleUtils.java | 13 +++- .../contract/solidity/KVTableTest.sol | 2 +- .../contract/solidity/TableTestV320.sol | 2 +- 4 files changed, 74 insertions(+), 15 deletions(-) diff --git a/.ci/ci_check.sh b/.ci/ci_check.sh index fe106be9..4fe38fd7 100755 --- a/.ci/ci_check.sh +++ b/.ci/ci_check.sh @@ -1,12 +1,24 @@ #!/bin/bash set -e -default_tag=v3.2.0 LOG_INFO() { local content=${1} echo -e "\033[32m ${content}\033[0m" } +download_tassl() +{ +local OPENSSL_CMD=${HOME}/.fisco/tassl-1.1.1b +if [ -f "${OPENSSL_CMD}" ];then + return +fi +local package_name="tassl-1.1.1b-linux-x86_64" +if [ "$(uname)" == "Darwin" ];then + package_name="tassl-1.1.1b-macOS-x86_64" +fi +curl -LO "https://github.com/FISCO-BCOS/LargeFiles/raw/master/tools/${package_name}.tar.gz" && tar -zxvf "${package_name}.tar.gz" && mv "${package_name}" tassl-1.1.1b && mkdir -p ~/.fisco && mv tassl-1.1.1b ~/.fisco/ +} + get_sed_cmd() { local sed_cmd="sed -i" @@ -18,13 +30,23 @@ get_sed_cmd() download_build_chain() { - tag=$(curl -sS "https://gitee.com/api/v5/repos/FISCO-BCOS/FISCO-BCOS/tags" | grep -oe "\"name\":\"v[2-9]*\.[0-9]*\.[0-9]*\"" | cut -d \" -f 4 | sort -V | tail -n 1) + local tag="${1}" + if [ -z "${tag}" ]; then + tag=$(curl -sS "https://gitee.com/api/v5/repos/FISCO-BCOS/FISCO-BCOS/tags" | grep -oe "\"name\":\"v[2-9]*\.[0-9]*\.[0-9]*\"" | cut -d \" -f 4 | sort -V | tail -n 1) + fi + LOG_INFO "--- current tag: $tag" + curl -LO "https://github.com/FISCO-BCOS/FISCO-BCOS/releases/download/${tag}/build_chain.sh" && chmod u+x build_chain.sh +} + +download_binary() +{ + local tag="${1}" LOG_INFO "--- current tag: $tag" - if [[ -z ${tag} ]]; then - LOG_INFO "tag is empty, use default tag: ${default_tag}" - tag="${default_tag}" + local package_name="fisco-bcos-linux-x86_64.tar.gz" + if [ "$(uname)" == "Darwin" ];then + package_name="fisco-bcos-macOS-x86_64.tar.gz" fi - curl -#LO "https://github.com/FISCO-BCOS/FISCO-BCOS/releases/download/${tag}/build_chain.sh" && chmod u+x build_chain.sh + curl -LO "https://github.com/FISCO-BCOS/FISCO-BCOS/releases/download/${tag}/${package_name}" && tar -zxvf "${package_name}" } prepare_environment() @@ -47,14 +69,23 @@ build_node() { local node_type="${1}" if [ "${node_type}" == "sm" ];then - bash -x build_chain.sh -l 127.0.0.1:4 -s -A + bash build_chain.sh -l 127.0.0.1:4 -s -A -e ./fisco-bcos else - bash -x build_chain.sh -l 127.0.0.1:4 -A + bash build_chain.sh -l 127.0.0.1:4 -A -e ./fisco-bcos fi ./nodes/127.0.0.1/fisco-bcos -v ./nodes/127.0.0.1/start_all.sh } +clean_node() +{ + bash nodes/127.0.0.1/stop_all.sh + rm -rf nodes + if [ "${1}" == "true" ]; then + rm -rf ./fisco-bcos* + fi +} + check_standard_node() { build_node @@ -62,6 +93,7 @@ check_standard_node() ## run integration test bash gradlew test --info bash gradlew integrationTest --info + clean_node "${1}" } check_sm_node() @@ -71,6 +103,7 @@ check_sm_node() ## run integration test bash gradlew test --info bash gradlew integrationTest --info + clean_node "${1}" } check_basic() @@ -86,10 +119,25 @@ bash gradlew integrationTest --info #cp src/integration-test/resources/config-example.toml src/integration-test/resources/config.toml #LOG_INFO "------ download_build_chain---------" -download_build_chain -#LOG_INFO "------ check_standard_node---------" -#check_standard_node +download_binary "v3.2.0" +download_build_chain "v3.2.0" +LOG_INFO "------ check_standard_node---------" +check_standard_node false LOG_INFO "------ check_sm_node---------" -check_sm_node +check_sm_node true LOG_INFO "------ check_basic---------" check_basic + +LOG_INFO "------ download_binary: v3.1.0---------" +download_binary "v3.1.0" +download_build_chain "v3.1.0" +LOG_INFO "------ check_standard_node---------" +check_standard_node +rm -rf ./bin + +LOG_INFO "------ download_binary: v3.0.0---------" +download_binary "v3.0.0" +download_build_chain "v3.0.0" +LOG_INFO "------ check_standard_node---------" +check_standard_node +rm -rf ./bin \ No newline at end of file diff --git a/src/main/java/console/common/ConsoleUtils.java b/src/main/java/console/common/ConsoleUtils.java index 1bc63ead..d8ee1533 100644 --- a/src/main/java/console/common/ConsoleUtils.java +++ b/src/main/java/console/common/ConsoleUtils.java @@ -607,7 +607,18 @@ public static void getReturnResults( resultData.append("]"); resultType.append("]"); } else if (result instanceof StructType) { - // FIXME: out put struct + resultType.append("["); + resultData.append("["); + List values = ((StructType) result).getComponentTypes(); + for (int i = 0; i < values.size(); ++i) { + getReturnResults(resultType, resultData, values.get(i)); + if (i != values.size() - 1) { + resultType.append(", "); + resultData.append(", "); + } + } + resultData.append("]"); + resultType.append("]"); throw new UnsupportedOperationException(); } else if (result instanceof Bytes) { String data = "hex://0x" + bytesToHex(((Bytes) result).getValue()); diff --git a/src/main/resources/contract/solidity/KVTableTest.sol b/src/main/resources/contract/solidity/KVTableTest.sol index 20f63c05..2f249464 100644 --- a/src/main/resources/contract/solidity/KVTableTest.sol +++ b/src/main/resources/contract/solidity/KVTableTest.sol @@ -22,7 +22,7 @@ contract KVTableTest { } function desc() public view returns(string memory, string memory){ - TableInfo memory tf = tm.descWithKeyOrder(tableName); + TableInfo memory tf = tm.desc(tableName); return (tf.keyColumn, tf.valueColumns[0]); } diff --git a/src/main/resources/contract/solidity/TableTestV320.sol b/src/main/resources/contract/solidity/TableTestV320.sol index b5467e1d..e3198c7b 100644 --- a/src/main/resources/contract/solidity/TableTestV320.sol +++ b/src/main/resources/contract/solidity/TableTestV320.sol @@ -5,7 +5,7 @@ pragma experimental ABIEncoderV2; import "./TableV320.sol"; import "./Cast.sol"; -contract TableTest { +contract TableTestV320 { event CreateResult(int256 count); event InsertResult(int256 count); event UpdateResult(int256 count); From ebd72301960ff8b4af645efe6225d6c78c904bef Mon Sep 17 00:00:00 2001 From: lucasli <410567249@qq.com> Date: Thu, 6 Apr 2023 10:37:57 +0800 Subject: [PATCH 15/18] (account): support list accounts with HSM model (#751) --- src/main/java/console/client/ConsoleClientImpl.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/console/client/ConsoleClientImpl.java b/src/main/java/console/client/ConsoleClientImpl.java index 34033ab3..17c0120f 100644 --- a/src/main/java/console/client/ConsoleClientImpl.java +++ b/src/main/java/console/client/ConsoleClientImpl.java @@ -366,7 +366,11 @@ public void listAccount(String[] params) { return; } String currentAccount = client.getCryptoSuite().getCryptoKeyPair().getAddress(); - System.out.println(currentAccount + "(current account) <="); + String currentAccountSuffix = + client.getCryptoSuite().getCryptoTypeConfig() == CryptoType.HSM_TYPE + ? "(current HSM account) <=" + : "(current account) <="; + System.out.println(currentAccount + currentAccountSuffix); for (String s : accountList) { if (!s.equals(currentAccount)) { System.out.println(s); @@ -377,7 +381,8 @@ public void listAccount(String[] params) { public static String getAccountDir(Client client) { ConfigOption configOption = client.getCryptoSuite().getConfig(); String subDir = CryptoKeyPair.ECDSA_ACCOUNT_SUBDIR; - if (client.getCryptoSuite().getCryptoTypeConfig() == CryptoType.SM_TYPE) { + if (client.getCryptoSuite().getCryptoTypeConfig() == CryptoType.SM_TYPE + || client.getCryptoSuite().getCryptoTypeConfig() == CryptoType.HSM_TYPE) { subDir = CryptoKeyPair.GM_ACCOUNT_SUBDIR; } return configOption.getAccountConfig().getKeyStoreDir() + File.separator + subDir; From 1f75281002f816ac851cb0a44425f0f9e8dae7af Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Mon, 10 Apr 2023 17:33:58 +0800 Subject: [PATCH 16/18] (auth): add switch group logic when setSysConfigProposal. (#753) --- .../java/console/PrecompiledTest.java | 28 +++++++++++++++++-- src/main/java/console/auth/AuthFace.java | 3 +- src/main/java/console/auth/AuthImpl.java | 10 ++++++- .../command/category/AuthOpCommand.java | 4 ++- .../command/category/ShardingCommand.java | 12 ++++++-- 5 files changed, 48 insertions(+), 9 deletions(-) diff --git a/src/integration-test/java/console/PrecompiledTest.java b/src/integration-test/java/console/PrecompiledTest.java index e61758e6..48a6d564 100644 --- a/src/integration-test/java/console/PrecompiledTest.java +++ b/src/integration-test/java/console/PrecompiledTest.java @@ -1,5 +1,6 @@ package console; +import org.fisco.bcos.sdk.v3.model.EnumNodeVersion; import org.junit.Assert; import org.junit.Rule; import org.junit.Test; @@ -14,63 +15,84 @@ public class PrecompiledTest extends TestBase { @Test public void crudTest() throws Exception { - String tableName = "t_demo" + Math.abs(new Random().nextInt()); + String tableName = "t_demo" + System.currentTimeMillis(); String createSql = "create table " + tableName + "(name varchar, item_id varchar, item_name varchar, primary key(name))"; + System.out.println(createSql); precompiledFace.createTable(createSql); Assert.assertTrue(log.getLog().contains("Ok")); log.clearLog(); + if (chainVersion.getVersion() >= EnumNodeVersion.BCOS_3_2_0.getVersion()) { + String tableName2 = "t_demo_" + System.currentTimeMillis(); + String createSql2 = "create table " + tableName2 + "(name varchar, item_id varchar, item_name varchar, primary key(name))"; + System.out.println(createSql2); + precompiledFace.createTable(createSql2); + Assert.assertTrue(log.getLog().contains("Ok")); + log.clearLog(); + } + + System.out.println("ls /tables"); precompiledFace.listDir(new String[]{"", "/tables"}); Assert.assertTrue(log.getLog().contains(tableName)); log.clearLog(); String insertSql = "insert into " + tableName + " (name, item_id, item_name) values (fruit, 1, apple1)"; + System.out.println(insertSql); precompiledFace.insert(insertSql); Assert.assertTrue(log.getLog().contains("OK")); log.clearLog(); // select String selectSql = "select * from " + tableName + " where name = fruit"; + System.out.println(selectSql); precompiledFace.select(selectSql); Assert.assertTrue(log.getLog().contains("fruit")); log.clearLog(); selectSql = "select name, item_id, item_name from " + tableName + " where name = fruit"; + System.out.println(selectSql); precompiledFace.select(selectSql); Assert.assertTrue(log.getLog().contains("fruit")); log.clearLog(); insertSql = "insert into " + tableName + " (name, item_id, item_name) values (fruit2, 2, orange)"; + System.out.println(insertSql); precompiledFace.insert(insertSql); Assert.assertTrue(log.getLog().contains("OK")); log.clearLog(); selectSql = "select * from " + tableName + " where name >= fruit"; + System.out.println(selectSql); precompiledFace.select(selectSql); Assert.assertTrue(log.getLog().contains("fruit2")); log.clearLog(); selectSql = "select * from " + tableName + " where name >= fruit limit 0,1"; + System.out.println(selectSql); precompiledFace.select(selectSql); Assert.assertFalse(log.getLog().contains("fruit2")); log.clearLog(); String updateSql = "update " + tableName + " set item_name = orange where name = fruit"; + System.out.println(updateSql); precompiledFace.update(updateSql); Assert.assertTrue(log.getLog().contains("1 row affected")); log.clearLog(); String removeSql = "delete from " + tableName + " where name = fruit2"; + System.out.println(removeSql); precompiledFace.remove(removeSql); Assert.assertTrue(log.getLog().contains("OK")); log.clearLog(); selectSql = "select * from " + tableName + " where name = fruit2"; + System.out.println(selectSql); precompiledFace.select(selectSql); Assert.assertTrue(log.getLog().contains("Empty")); log.clearLog(); - String newField = "comment" + Math.abs(new Random().nextInt()); + String newField = "comment" + System.currentTimeMillis(); + System.out.println(newField); String alterSql = "alter table " + tableName + " add " + newField + " varchar"; precompiledFace.alterTable(alterSql); Assert.assertTrue(log.getLog().contains("Ok")); @@ -139,7 +161,7 @@ public void bfsTest() throws Exception { @Test public void authTest() throws Exception { - if (!isAuthCheck){ + if (!isAuthCheck) { return; } String[] emptyParams = {}; diff --git a/src/main/java/console/auth/AuthFace.java b/src/main/java/console/auth/AuthFace.java index 06b3994e..bbad366b 100644 --- a/src/main/java/console/auth/AuthFace.java +++ b/src/main/java/console/auth/AuthFace.java @@ -24,7 +24,8 @@ public interface AuthFace { void createRemoveNodeProposal(String[] params) throws Exception; - void createSetSysConfigProposal(String[] params) throws Exception; + void createSetSysConfigProposal(ConsoleInitializer consoleInitializer, String[] params) + throws Exception; void createUpgradeVoteComputerProposal(String[] params) throws Exception; diff --git a/src/main/java/console/auth/AuthImpl.java b/src/main/java/console/auth/AuthImpl.java index a6bfdd9c..55768f84 100644 --- a/src/main/java/console/auth/AuthImpl.java +++ b/src/main/java/console/auth/AuthImpl.java @@ -1,5 +1,7 @@ package console.auth; +import static console.common.Common.COMPATIBILITY_VERSION; + import console.ConsoleInitializer; import console.common.ConsoleUtils; import java.math.BigInteger; @@ -217,13 +219,19 @@ public void createRemoveNodeProposal(String[] params) throws Exception { } @Override - public void createSetSysConfigProposal(String[] params) throws Exception { + public void createSetSysConfigProposal(ConsoleInitializer consoleInitializer, String[] params) + throws Exception { String key = params[1]; String value = params[2]; BigInteger proposalId = authManager.createSetSysConfigProposal(key, value); System.out.println("Set system config proposal created, ID is: " + proposalId); showProposalInfo(proposalId); + if (key.equals(COMPATIBILITY_VERSION) && proposalId.compareTo(BigInteger.ZERO) > 0) { + String[] param = new String[2]; + param[1] = consoleInitializer.getGroupID(); + consoleInitializer.switchGroup(param); + } } @Override diff --git a/src/main/java/console/command/category/AuthOpCommand.java b/src/main/java/console/command/category/AuthOpCommand.java index 7b1d193b..cd22bc49 100644 --- a/src/main/java/console/command/category/AuthOpCommand.java +++ b/src/main/java/console/command/category/AuthOpCommand.java @@ -183,7 +183,9 @@ public Map getAllCommandInfo(boolean isWasm) { "Create a proposal to committee, which attempt to set system config.", HelpInfo::setSysConfigProposalHelp, (consoleInitializer, params, pwd) -> - consoleInitializer.getAuthFace().createSetSysConfigProposal(params), + consoleInitializer + .getAuthFace() + .createSetSysConfigProposal(consoleInitializer, params), 2, 2, false, diff --git a/src/main/java/console/command/category/ShardingCommand.java b/src/main/java/console/command/category/ShardingCommand.java index 584891b0..7905bd68 100644 --- a/src/main/java/console/command/category/ShardingCommand.java +++ b/src/main/java/console/command/category/ShardingCommand.java @@ -51,7 +51,9 @@ public Map getAllCommandInfo(boolean isWasm) { (consoleInitializer, params, pwd) -> consoleInitializer.getPrecompiledFace().getContractShard(params), 1, - 1); + 1, + false, + false); public static final CommandInfo MAKE_SHARD = new CommandInfo( @@ -61,7 +63,9 @@ public Map getAllCommandInfo(boolean isWasm) { (consoleInitializer, params, pwd) -> consoleInitializer.getPrecompiledFace().makeShard(params), 1, - 1); + 1, + false, + false); public static final CommandInfo LINK_SHARD = new CommandInfo( @@ -71,7 +75,9 @@ public Map getAllCommandInfo(boolean isWasm) { (consoleInitializer, params, pwd) -> consoleInitializer.getPrecompiledFace().linkShard(params), 2, - 2); + 2, + false, + false); static { Field[] fields = ShardingCommand.class.getDeclaredFields(); From 542d0802558bcf23ae22d787b852f2d4128be20d Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Mon, 10 Apr 2023 19:05:07 +0800 Subject: [PATCH 17/18] (Precompiled): add fixBfs interface to fix BFS index data error. (#754) --- .github/workflows/workflow.yml | 2 +- .../java/console/command/category/BfsCommand.java | 13 +++++++++++++ .../java/console/contract/ConsoleContractImpl.java | 3 +-- .../java/console/precompiled/PrecompiledFace.java | 2 ++ .../java/console/precompiled/PrecompiledImpl.java | 13 ++++++++----- 5 files changed, 25 insertions(+), 8 deletions(-) diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 3d490db1..c0060581 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -14,7 +14,7 @@ jobs: strategy: fail-fast: false matrix: - os: [ubuntu-18.04, ubuntu-20.04, ubuntu-22.04, windows-2019, macos-latest] + os: [ubuntu-20.04, ubuntu-22.04, windows-2019, macos-latest] steps: - uses: actions/checkout@v2 with: diff --git a/src/main/java/console/command/category/BfsCommand.java b/src/main/java/console/command/category/BfsCommand.java index 7d357d47..c929cd1c 100644 --- a/src/main/java/console/command/category/BfsCommand.java +++ b/src/main/java/console/command/category/BfsCommand.java @@ -93,6 +93,19 @@ public Map getAllCommandInfo(boolean isWasm) { 2, 2); + public static final CommandInfo FIX_BFS = + new CommandInfo( + "fixBFS", + "Fix the bfs bug of the specified version.", + () -> { + System.out.println("Fix the bfs bug of the specified version."); + System.out.println("Fix the bfs bug of the specified version."); + }, + (consoleInitializer, params, pwd) -> + consoleInitializer.getPrecompiledFace().fixBFS(params), + 0, + 0); + public static final CommandInfo PWD = new CommandInfo( "pwd", diff --git a/src/main/java/console/contract/ConsoleContractImpl.java b/src/main/java/console/contract/ConsoleContractImpl.java index 91dfb405..181d7640 100644 --- a/src/main/java/console/contract/ConsoleContractImpl.java +++ b/src/main/java/console/contract/ConsoleContractImpl.java @@ -182,8 +182,7 @@ public void deploy(String[] params, String pwd) throws Exception { } private void deployLink(String linkPath, String address, String abiString) throws Exception { - EnumNodeVersion.Version supportedVersion = - EnumNodeVersion.valueOf((int) bfsService.getCurrentVersion()).toVersionObj(); + EnumNodeVersion.Version supportedVersion = bfsService.getCurrentVersion().toVersionObj(); final RetCode retCode; if (supportedVersion.compareTo(EnumNodeVersion.BCOS_3_1_0.toVersionObj()) >= 0) { retCode = diff --git a/src/main/java/console/precompiled/PrecompiledFace.java b/src/main/java/console/precompiled/PrecompiledFace.java index 7c9f5eca..c203db01 100644 --- a/src/main/java/console/precompiled/PrecompiledFace.java +++ b/src/main/java/console/precompiled/PrecompiledFace.java @@ -45,5 +45,7 @@ void setSystemConfigByKey(ConsoleInitializer consoleInitializer, String[] params void linkShard(String[] params) throws Exception; + void fixBFS(String[] params) throws Exception; + String getPwd(); } diff --git a/src/main/java/console/precompiled/PrecompiledImpl.java b/src/main/java/console/precompiled/PrecompiledImpl.java index edcfa36c..ee887450 100644 --- a/src/main/java/console/precompiled/PrecompiledImpl.java +++ b/src/main/java/console/precompiled/PrecompiledImpl.java @@ -466,8 +466,7 @@ public void changeDir(String[] params) throws Exception { pwd = path; return; } - EnumNodeVersion.Version supportedVersion = - EnumNodeVersion.valueOf((int) bfsService.getCurrentVersion()).toVersionObj(); + EnumNodeVersion.Version supportedVersion = bfsService.getCurrentVersion().toVersionObj(); if (supportedVersion.compareTo(EnumNodeVersion.BCOS_3_1_0.toVersionObj()) >= 0) { BFSInfo bfsInfo = bfsService.isExist(path); if (bfsInfo != null) { @@ -529,7 +528,7 @@ public void listDir(String[] params) throws Exception { do { Tuple2> fileInfoList; EnumNodeVersion.Version supportedVersion = - EnumNodeVersion.valueOf((int) bfsService.getCurrentVersion()).toVersionObj(); + bfsService.getCurrentVersion().toVersionObj(); if (supportedVersion.compareTo(EnumNodeVersion.BCOS_3_1_0.toVersionObj()) >= 0) { fileInfoList = bfsService.list(listPath, offset, Common.LS_DEFAULT_COUNT); } else { @@ -678,8 +677,7 @@ public void link(String[] params) throws Exception { } RetCode retCode; - EnumNodeVersion.Version supportedVersion = - EnumNodeVersion.valueOf((int) bfsService.getCurrentVersion()).toVersionObj(); + EnumNodeVersion.Version supportedVersion = bfsService.getCurrentVersion().toVersionObj(); if (supportedVersion.compareTo(EnumNodeVersion.BCOS_3_1_0.toVersionObj()) >= 0) { retCode = bfsService.link( @@ -744,6 +742,11 @@ public void linkShard(String[] params) throws Exception { } } + @Override + public void fixBFS(String[] params) throws Exception { + ConsoleUtils.printJson(bfsService.fixBfs().toString()); + } + @Override public String getPwd() { return pwd; From a13ff87e9eb73c67930d144a02ff3a92793c4944 Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Sat, 15 Apr 2023 00:19:40 +0800 Subject: [PATCH 18/18] (console,jline): fix console fresh bug when switch group. (#755) --- src/main/java/console/command/JlineUtils.java | 3 +++ .../console/command/completer/CurrentPathCompleter.java | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/src/main/java/console/command/JlineUtils.java b/src/main/java/console/command/JlineUtils.java index 36f941da..59665a39 100644 --- a/src/main/java/console/command/JlineUtils.java +++ b/src/main/java/console/command/JlineUtils.java @@ -55,6 +55,9 @@ public static void switchGroup(Client client) { if (accountCompleter != null) { accountCompleter.setClient(client); } + if (currentPathCompleter != null) { + currentPathCompleter.setClient(client); + } } public static void switchPwd(String pwd) { diff --git a/src/main/java/console/command/completer/CurrentPathCompleter.java b/src/main/java/console/command/completer/CurrentPathCompleter.java index 9b2010c5..f9f68059 100644 --- a/src/main/java/console/command/completer/CurrentPathCompleter.java +++ b/src/main/java/console/command/completer/CurrentPathCompleter.java @@ -93,4 +93,10 @@ public void complete(LineReader reader, ParsedLine commandLine, List logger.debug("CurrentPathCompleter exception, error: {}", e.getMessage(), e); } } + + public void setClient(Client client) { + this.client = client; + this.bfsService = new BFSService(client, client.getCryptoSuite().getCryptoKeyPair()); + this.pwd = "/apps"; + } }