Skip to content

Commit

Permalink
<feat>(command): add listSystemConfigs command, getLatestBlock comman…
Browse files Browse the repository at this point in the history
…d. (#848)
  • Loading branch information
kyonRay authored Jun 18, 2024
1 parent d4ebca8 commit cd6ea4b
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 12 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/console/client/ConsoleClientFace.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
64 changes: 60 additions & 4 deletions src/main/java/console/client/ConsoleClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

Expand Down Expand Up @@ -326,6 +349,39 @@ public void getSystemConfigByKey(String[] params) throws Exception {
}
}

@Override
public void listConfigs(String[] params) throws Exception {
Map<String, Optional<SystemConfig>> 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";
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/console/command/category/StatusQueryCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ public Map<String, CommandInfo> 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",
Expand Down Expand Up @@ -203,6 +213,16 @@ public Map<String, CommandInfo> 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) {
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/console/command/model/HelpInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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]");
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/console/common/ConsoleVersion.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
34 changes: 28 additions & 6 deletions src/main/java/console/precompiled/PrecompiledImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
}
}
}

Expand All @@ -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.");
}
}
}

Expand All @@ -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.");
}
}
}

Expand All @@ -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.");
}
}
}

Expand All @@ -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];
Expand Down

0 comments on commit cd6ea4b

Please sign in to comment.