From 9cb825cfcdb54c8fdee4fe7ffb5ad3fb95cc575c Mon Sep 17 00:00:00 2001 From: kyonRay Date: Tue, 11 Jun 2024 16:31:00 +0800 Subject: [PATCH] (client): add get support configs interfaces. --- build.gradle | 2 +- .../fisco/bcos/sdk/v3/test/BcosSDKTest.java | 13 ++ .../org/fisco/bcos/sdk/v3/client/Client.java | 24 ++++ .../fisco/bcos/sdk/v3/client/ClientImpl.java | 116 +++++++++++++++++- .../protocol/response/BcosGroupNodeInfo.java | 12 +- 5 files changed, 164 insertions(+), 3 deletions(-) diff --git a/build.gradle b/build.gradle index 1d1cb98fe..fcdd0bf78 100644 --- a/build.gradle +++ b/build.gradle @@ -35,7 +35,7 @@ ext { // integrationTest.mustRunAfter test allprojects { group = 'org.fisco-bcos.java-sdk' - version = '3.7.0' + version = '3.8.0-SNAPSHOT' apply plugin: 'maven-publish' apply plugin: 'idea' diff --git a/src/integration-test/java/org/fisco/bcos/sdk/v3/test/BcosSDKTest.java b/src/integration-test/java/org/fisco/bcos/sdk/v3/test/BcosSDKTest.java index e3f4f713a..5070c76e3 100644 --- a/src/integration-test/java/org/fisco/bcos/sdk/v3/test/BcosSDKTest.java +++ b/src/integration-test/java/org/fisco/bcos/sdk/v3/test/BcosSDKTest.java @@ -21,6 +21,8 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Map; +import java.util.Optional; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; @@ -92,6 +94,17 @@ public void testClient() { System.out.println( "New block, group: " + groupId + ", blockNumber: " + blockNumber)); + Map> systemConfigList = client.getSystemConfigList(); + Assert.assertFalse(systemConfigList.isEmpty()); + systemConfigList.forEach( + (key, value) -> + System.out.println( + key + + " : " + + (value.isPresent() + ? value.get().getSystemConfig() + : "null"))); + // test getBlockNumber BlockNumber blockNumber = client.getBlockNumber(); Assert.assertTrue(blockNumber.getBlockNumber().compareTo(BigInteger.ZERO) >= 0); diff --git a/src/main/java/org/fisco/bcos/sdk/v3/client/Client.java b/src/main/java/org/fisco/bcos/sdk/v3/client/Client.java index b33167847..51f212822 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/client/Client.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/client/Client.java @@ -16,6 +16,9 @@ package org.fisco.bcos.sdk.v3.client; import java.math.BigInteger; +import java.util.Map; +import java.util.Optional; +import java.util.Set; import org.fisco.bcos.sdk.jni.BcosSDKJniObj; import org.fisco.bcos.sdk.v3.client.protocol.request.Transaction; import org.fisco.bcos.sdk.v3.client.protocol.response.Abi; @@ -840,6 +843,13 @@ void getTransactionReceiptAsync( */ SystemConfig getSystemConfigByKey(String node, String key); + /** + * Peer operation: get system config list, witch will fetch all config + * + * @return all config value + */ + Map> getSystemConfigList(); + /** * Peer operation: async get system config * @@ -857,6 +867,20 @@ void getTransactionReceiptAsync( */ void getSystemConfigByKeyAsync(String node, String key, RespCallback callback); + /** + * async get all connect nodes support keys + * + * @param callback the callback instance + */ + void getSupportSysConfigKeysAsync(RespCallback> callback); + + /** + * Peer operation: async get system config list, witch will fetch all config + * + * @param callback the callback instance + */ + void getSystemConfigListAsync(RespCallback>> callback); + /** * Peer operation: get sync status * diff --git a/src/main/java/org/fisco/bcos/sdk/v3/client/ClientImpl.java b/src/main/java/org/fisco/bcos/sdk/v3/client/ClientImpl.java index cddf58a2c..fd9b0c710 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/client/ClientImpl.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/client/ClientImpl.java @@ -21,9 +21,16 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.TreeSet; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentSkipListMap; import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import org.fisco.bcos.sdk.jni.BcosSDKJniObj; import org.fisco.bcos.sdk.jni.rpc.RpcJniObj; import org.fisco.bcos.sdk.v3.client.exceptions.ClientException; @@ -1008,6 +1015,32 @@ public SystemConfig getSystemConfigByKey(String node, String key) { SystemConfig.class); } + @Override + public Map> getSystemConfigList() { + CompletableFuture>> future = new CompletableFuture<>(); + this.getSystemConfigListAsync( + new RespCallback>>() { + @Override + public void onResponse(Map> configMap) { + future.complete(configMap); + } + + @Override + public void onError(Response errorResponse) { + future.completeExceptionally( + new ClientException( + "getSystemConfigList failed, error: " + + errorResponse.getErrorMessage())); + } + }); + try { + return future.get(configOption.getNetworkConfig().getTimeout(), TimeUnit.MILLISECONDS); + } catch (Exception e) { + logger.warn("getSystemConfigList failed, error: {}", e.getMessage(), e); + throw new ClientException("getSystemConfigList failed, error: " + e.getMessage(), e); + } + } + @Override public void getSystemConfigByKeyAsync(String key, RespCallback callback) { this.getSystemConfigByKeyAsync(nodeToSendRequest, key, callback); @@ -1026,6 +1059,80 @@ public void getSystemConfigByKeyAsync( callback); } + @Override + public void getSupportSysConfigKeysAsync(RespCallback> callback) { + this.getGroupInfoListAsync( + new RespCallback() { + @Override + public void onResponse(BcosGroupInfoList bcosGroupInfoList) { + Optional group = + bcosGroupInfoList.getResult().stream() + .filter(gInfo -> gInfo.getGroupID().equals(getGroup())) + .findFirst(); + Set keys = new TreeSet<>(); + if (group.isPresent() && !group.get().getNodeList().isEmpty()) { + group.get() + .getNodeList() + .forEach( + groupNodeInfo -> { + keys.addAll(groupNodeInfo.getFeatureKeys()); + keys.addAll(groupNodeInfo.getSupportConfigs()); + }); + } + callback.onResponse(keys); + } + + @Override + public void onError(Response errorResponse) { + callback.onError(errorResponse); + } + }); + } + + @Override + public void getSystemConfigListAsync( + RespCallback>> callback) { + + this.getSupportSysConfigKeysAsync( + new RespCallback>() { + @Override + public void onResponse(Set keys) { + Map> configMap = + new ConcurrentSkipListMap<>(); + keys.forEach( + key -> + getSystemConfigByKeyAsync( + key, + new RespCallback() { + @Override + public void onResponse( + SystemConfig systemConfig) { + configMap.put( + key, + Optional.ofNullable(systemConfig)); + if (configMap.size() == keys.size()) { + callback.onResponse(configMap); + } + } + + @Override + public void onError(Response errorResponse) { + // maybe not exist + configMap.put(key, Optional.empty()); + if (configMap.size() == keys.size()) { + callback.onResponse(configMap); + } + } + })); + } + + @Override + public void onError(Response errorResponse) { + callback.onError(errorResponse); + } + }); + } + @Override public SyncStatus getSyncStatus(String node) { node = Objects.isNull(node) ? "" : node; @@ -1130,7 +1237,8 @@ public BcosGroupInfo getGroupInfo() { future.complete(response); }); - Response response = future.get(); + Response response = + future.get(configOption.getNetworkConfig().getTimeout(), TimeUnit.MILLISECONDS); return ClientImpl.parseResponseIntoJsonRpcResponse( JsonRpcMethods.GET_GROUP_INFO, response, BcosGroupInfo.class); } catch (ClientException e) { @@ -1146,6 +1254,12 @@ public BcosGroupInfo getGroupInfo() { "getGroupInfo failed for decode the message exception, error message:" + e.getMessage(), e); + } catch (TimeoutException e) { + logger.error("e: ", e); + throw new ClientException( + "getGroupInfo failed for get group info timeout, error message:" + + e.getMessage(), + e); } } diff --git a/src/main/java/org/fisco/bcos/sdk/v3/client/protocol/response/BcosGroupNodeInfo.java b/src/main/java/org/fisco/bcos/sdk/v3/client/protocol/response/BcosGroupNodeInfo.java index cf484812e..07aa85466 100644 --- a/src/main/java/org/fisco/bcos/sdk/v3/client/protocol/response/BcosGroupNodeInfo.java +++ b/src/main/java/org/fisco/bcos/sdk/v3/client/protocol/response/BcosGroupNodeInfo.java @@ -12,6 +12,7 @@ import com.fasterxml.jackson.databind.type.TypeFactory; import com.fasterxml.jackson.databind.util.Converter; import java.io.IOException; +import java.util.ArrayList; import java.util.List; import org.fisco.bcos.sdk.v3.client.protocol.model.GroupNodeIniInfo; import org.fisco.bcos.sdk.v3.model.JsonRpcResponse; @@ -80,7 +81,8 @@ public static class GroupNodeInfo { private String name; private List serviceInfoList; private Protocol protocol; - private List featureKeys; + private List featureKeys = new ArrayList<>(); + private List supportConfigs = new ArrayList<>(); @Override public String toString() { @@ -150,6 +152,14 @@ public void setFeatureKeys(List featureKeys) { this.featureKeys = featureKeys; } + public List getSupportConfigs() { + return supportConfigs; + } + + public void setSupportConfigs(List supportConfigs) { + this.supportConfigs = supportConfigs; + } + static class ServiceInfo { private String serviceName; private int type;