diff --git a/build.gradle b/build.gradle index 9e6b5201..4e0d5d92 100644 --- a/build.gradle +++ b/build.gradle @@ -42,7 +42,7 @@ dependencies { //implementation 'org.fisco-bcos:solcJ:0.5.2.1' implementation 'org.fisco-bcos:solcJ:1.0.0-SNAPSHOT' - implementation ('org.fisco-bcos.java-sdk:fisco-bcos-java-sdk:3.7.0') { + implementation ('org.fisco-bcos.java-sdk:fisco-bcos-java-sdk:3.8.0-SNAPSHOT') { exclude group: "org.slf4j" } diff --git a/src/main/java/console/client/ConsoleClientFace.java b/src/main/java/console/client/ConsoleClientFace.java index 836a84fa..3eb2dfb3 100644 --- a/src/main/java/console/client/ConsoleClientFace.java +++ b/src/main/java/console/client/ConsoleClientFace.java @@ -28,6 +28,8 @@ public interface ConsoleClientFace { void getBlockByNumber(String[] params) throws IOException; + void getLatestBlock(String[] params) throws IOException; + void getBlockHeaderByHash(String[] params) throws IOException; void getBlockHashByNumber(String[] params) throws IOException; @@ -50,6 +52,8 @@ public interface ConsoleClientFace { void getSystemConfigByKey(String[] params) throws Exception; + void listConfigs(String[] params) throws Exception; + void newAccount(String[] params); void listAccount(String[] params); diff --git a/src/main/java/console/client/ConsoleClientImpl.java b/src/main/java/console/client/ConsoleClientImpl.java index bca4e97b..f567d732 100644 --- a/src/main/java/console/client/ConsoleClientImpl.java +++ b/src/main/java/console/client/ConsoleClientImpl.java @@ -11,8 +11,12 @@ import java.math.BigInteger; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.fisco.bcos.sdk.v3.client.Client; import org.fisco.bcos.sdk.v3.client.exceptions.ClientException; import org.fisco.bcos.sdk.v3.client.protocol.model.JsonTransactionResponse; @@ -148,10 +152,29 @@ public void getBlockByNumber(String[] params) throws IOException { if (blockByNumber.getBlock() == null) { System.out.println("Block not found, please check number: " + blockNumber); } else { - ConsoleUtils.printJson( - client.getBlockByNumber(BigInteger.valueOf(blockNumber), false, flag) - .getBlock() - .toString()); + ConsoleUtils.printJson(blockByNumber.getBlock().toString()); + } + } + + @Override + public void getLatestBlock(String[] params) throws IOException { + boolean flag = false; + if (params.length == 2) { + if ("true".equals(params[1])) { + flag = true; + } else if ("false".equals(params[1])) { + flag = false; + } else { + System.out.println("Please provide true or false for the second parameter."); + return; + } + } + BigInteger blockNumber = client.getBlockNumber().getBlockNumber(); + BcosBlock block = client.getBlockByNumber(blockNumber, false, flag); + if (block.getBlock() == null) { + System.out.println("Block not found, please check number: " + blockNumber); + } else { + ConsoleUtils.printJson(block.getBlock().toString()); } } @@ -326,6 +349,39 @@ public void getSystemConfigByKey(String[] params) throws Exception { } } + @Override + public void listConfigs(String[] params) throws Exception { + Map> systemConfigList = client.getSystemConfigList(); + int longestKeySize = 0; + for (String key : systemConfigList.keySet()) { + if (key.length() > longestKeySize) { + longestKeySize = key.length(); + } + } + String leftAlignFormat = "| %-" + longestKeySize + "s | %-14s | %-12s |%n"; + String separatorLine = + Stream.generate(() -> "-").limit(longestKeySize + 2).collect(Collectors.joining()); + String adaptableTitle = + "| Config" + + Stream.generate(() -> " ") + .limit(longestKeySize - "Config".length()) + .collect(Collectors.joining()); + System.out.println("+" + separatorLine + "+----------------+--------------+"); + System.out.println(adaptableTitle + " | Value | Enable Block |"); + System.out.println("+" + separatorLine + "+----------------+--------------+"); + systemConfigList.forEach( + (key, value) -> { + String configValue = "null"; + long blockNumber = 0; + if (value.isPresent()) { + configValue = value.get().getSystemConfig().getValue(); + blockNumber = value.get().getSystemConfig().getBlockNumber(); + } + System.out.format(leftAlignFormat, key, configValue, blockNumber); + }); + System.out.println("+" + separatorLine + "+----------------+--------------+"); + } + @Override public void newAccount(String[] params) { String accountFormat = "pem"; diff --git a/src/main/java/console/command/category/StatusQueryCommand.java b/src/main/java/console/command/category/StatusQueryCommand.java index 103d4a16..2803c37b 100644 --- a/src/main/java/console/command/category/StatusQueryCommand.java +++ b/src/main/java/console/command/category/StatusQueryCommand.java @@ -67,6 +67,16 @@ public Map getAllCommandInfo(boolean isWasm) { (consoleInitializer, params, pwd) -> consoleInitializer.getConsoleClientFace().getBlockNumber(params)); + public static final CommandInfo GET_LATEST_BLOCK = + new CommandInfo( + "getLatestBlock", + "Query the latest block", + HelpInfo::getLatestBlockHelp, + (consoleInitializer, params, pwd) -> + consoleInitializer.getConsoleClientFace().getLatestBlock(params), + 0, + 1); + public static final CommandInfo GET_BLOCK_HASH_BY_NUMBER = new CommandInfo( "getBlockHashByNumber", @@ -203,6 +213,16 @@ public Map getAllCommandInfo(boolean isWasm) { 1, 1); + public static final CommandInfo LIST_CONFIGS = + new CommandInfo( + "listSystemConfigs", + "List all support system configs", + HelpInfo::listSystemConfigsHelp, + (consoleInitializer, params, pwd) -> + consoleInitializer.getConsoleClientFace().listConfigs(params), + 0, + 0); + static { Field[] fields = StatusQueryCommand.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 6ec3d7e7..9d306e22 100644 --- a/src/main/java/console/command/model/HelpInfo.java +++ b/src/main/java/console/command/model/HelpInfo.java @@ -156,6 +156,13 @@ public static void getBlockByNumberHelp() { "* boolean -- (optional) If true it returns only the hashes of the transactions, if false then return the full transaction objects."); } + public static void getLatestBlockHelp() { + System.out.println("Query the latest block."); + System.out.println("Usage: \ngetLatestBlock [boolean]"); + System.out.println( + "* boolean -- (optional) If true it returns only the hashes of the transactions, if false then return the full transaction objects."); + } + public static void getBlockHeaderByHashHelp() { System.out.println("Query information about a block header by hash."); System.out.println("Usage: \ngetBlockHeaderByHash blockHash [boolean]"); @@ -419,6 +426,11 @@ public static void getSystemConfigByKeyHelp() { " -- supported keys: " + String.join(",", Common.SUPPORTED_SYSTEM_KEYS)); } + public static void listSystemConfigsHelp() { + System.out.println("List all support system configs."); + System.out.println("Usage: \nlistSystemConfigs "); + } + public static void operateGroupHelp(String command, String operator) { System.out.println("Usage: \n" + command + " endPoint groupId"); System.out.println( diff --git a/src/main/java/console/common/ConsoleVersion.java b/src/main/java/console/common/ConsoleVersion.java index d741ed94..e60eae92 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.7.0"; + public static final String Version = "3.8.0"; public static void main(String[] args) { System.out.println("console version: " + Version); diff --git a/src/main/java/console/precompiled/PrecompiledImpl.java b/src/main/java/console/precompiled/PrecompiledImpl.java index 827f315b..237a277a 100644 --- a/src/main/java/console/precompiled/PrecompiledImpl.java +++ b/src/main/java/console/precompiled/PrecompiledImpl.java @@ -79,8 +79,12 @@ public void addSealer(String[] params) throws Exception { if (nodeId.length() != 128) { ConsoleUtils.printJson(PrecompiledRetCode.CODE_INVALID_NODEID.toString()); } else { - ConsoleUtils.printJson( - this.consensusService.addSealer(nodeId, BigInteger.valueOf(weight)).toString()); + RetCode retCode = this.consensusService.addSealer(nodeId, BigInteger.valueOf(weight)); + ConsoleUtils.printJson(retCode.toString()); + if (retCode.getCode() == PrecompiledRetCode.CODE_NO_AUTHORIZED.getCode()) { + System.out.println( + "Maybe you should use 'addSealerProposal' command to change system config."); + } } } @@ -90,7 +94,12 @@ public void addObserver(String[] params) throws Exception { if (nodeId.length() != 128) { ConsoleUtils.printJson(PrecompiledRetCode.CODE_INVALID_NODEID.toString()); } else { - ConsoleUtils.printJson(this.consensusService.addObserver(nodeId).toString()); + RetCode retCode = this.consensusService.addObserver(nodeId); + ConsoleUtils.printJson(retCode.toString()); + if (retCode.getCode() == PrecompiledRetCode.CODE_NO_AUTHORIZED.getCode()) { + System.out.println( + "Maybe you should use 'addObserverProposal' command to change system config."); + } } } @@ -100,7 +109,12 @@ public void removeNode(String[] params) throws Exception { if (nodeId.length() != 128) { ConsoleUtils.printJson(PrecompiledRetCode.CODE_INVALID_NODEID.toString()); } else { - ConsoleUtils.printJson(this.consensusService.removeNode(nodeId).toString()); + RetCode retCode = this.consensusService.removeNode(nodeId); + ConsoleUtils.printJson(retCode.toString()); + if (retCode.getCode() == PrecompiledRetCode.CODE_NO_AUTHORIZED.getCode()) { + System.out.println( + "Maybe you should use 'removeNodeProposal' command to change system config."); + } } } @@ -111,8 +125,12 @@ public void setConsensusNodeWeight(String[] params) throws Exception { if (nodeId.length() != 128) { ConsoleUtils.printJson(PrecompiledRetCode.CODE_INVALID_NODEID.toString()); } else { - ConsoleUtils.printJson( - this.consensusService.setWeight(nodeId, BigInteger.valueOf(weight)).toString()); + RetCode retCode = this.consensusService.setWeight(nodeId, BigInteger.valueOf(weight)); + ConsoleUtils.printJson(retCode.toString()); + if (retCode.getCode() == PrecompiledRetCode.CODE_NO_AUTHORIZED.getCode()) { + System.out.println( + "Maybe you should use 'setConsensusNodeWeightProposal' command to change system config."); + } } } @@ -128,6 +146,10 @@ public void setSystemConfigByKey(ConsoleInitializer consoleInitializer, String[] } RetCode retCode = this.systemConfigService.setValueByKey(key, value); ConsoleUtils.printJson(retCode.toString()); + if (retCode.getCode() == PrecompiledRetCode.CODE_NO_AUTHORIZED.getCode()) { + System.out.println( + "Maybe you should use 'setSysConfigProposal' command to change system config."); + } if (key.equals(SystemConfigService.COMPATIBILITY_VERSION) && retCode.code == PrecompiledRetCode.CODE_SUCCESS.code) { String[] param = new String[2];