Skip to content

Commit

Permalink
<feat&fix>(client,transaction): add get support configs interfaces, f…
Browse files Browse the repository at this point in the history
…ix transaction builder set extension version bug. (#922)

* <feat>(client): add get support configs interfaces.

* <fix>(transaction,contract): fix transaction builder set extension version bug.
  • Loading branch information
kyonRay authored Jun 12, 2024
1 parent 6a7b902 commit fe07802
Show file tree
Hide file tree
Showing 13 changed files with 256 additions and 36 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -92,6 +94,16 @@ public void testClient() {
System.out.println(
"New block, group: " + groupId + ", blockNumber: " + blockNumber));

Map<String, Optional<SystemConfig>> systemConfigList = client.getSystemConfigList();
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);
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/org/fisco/bcos/sdk/v3/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Optional<SystemConfig>> getSystemConfigList();

/**
* Peer operation: async get system config
*
Expand All @@ -857,6 +867,20 @@ void getTransactionReceiptAsync(
*/
void getSystemConfigByKeyAsync(String node, String key, RespCallback<SystemConfig> callback);

/**
* async get all connect nodes support keys
*
* @param callback the callback instance
*/
void getSupportSysConfigKeysAsync(RespCallback<Set<String>> callback);

/**
* Peer operation: async get system config list, witch will fetch all config
*
* @param callback the callback instance
*/
void getSystemConfigListAsync(RespCallback<Map<String, Optional<SystemConfig>>> callback);

/**
* Peer operation: get sync status
*
Expand Down
154 changes: 141 additions & 13 deletions src/main/java/org/fisco/bcos/sdk/v3/client/ClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,17 @@
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 java.util.stream.Collectors;
import org.fisco.bcos.sdk.jni.BcosSDKJniObj;
import org.fisco.bcos.sdk.jni.rpc.RpcJniObj;
import org.fisco.bcos.sdk.v3.client.exceptions.ClientException;
Expand Down Expand Up @@ -58,6 +66,7 @@
import org.fisco.bcos.sdk.v3.client.protocol.response.SystemConfig;
import org.fisco.bcos.sdk.v3.client.protocol.response.TotalTransactionCount;
import org.fisco.bcos.sdk.v3.config.ConfigOption;
import org.fisco.bcos.sdk.v3.contract.precompiled.sysconfig.SystemConfigFeature;
import org.fisco.bcos.sdk.v3.contract.precompiled.sysconfig.SystemConfigService;
import org.fisco.bcos.sdk.v3.crypto.CryptoSuite;
import org.fisco.bcos.sdk.v3.model.CryptoType;
Expand Down Expand Up @@ -1008,6 +1017,32 @@ public SystemConfig getSystemConfigByKey(String node, String key) {
SystemConfig.class);
}

@Override
public Map<String, Optional<SystemConfig>> getSystemConfigList() {
CompletableFuture<Map<String, Optional<SystemConfig>>> future = new CompletableFuture<>();
this.getSystemConfigListAsync(
new RespCallback<Map<String, Optional<SystemConfig>>>() {
@Override
public void onResponse(Map<String, Optional<SystemConfig>> 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<SystemConfig> callback) {
this.getSystemConfigByKeyAsync(nodeToSendRequest, key, callback);
Expand All @@ -1026,6 +1061,89 @@ public void getSystemConfigByKeyAsync(
callback);
}

@Override
public void getSupportSysConfigKeysAsync(RespCallback<Set<String>> callback) {
this.getGroupInfoListAsync(
new RespCallback<BcosGroupInfoList>() {
@Override
public void onResponse(BcosGroupInfoList bcosGroupInfoList) {
Optional<BcosGroupInfo.GroupInfo> group =
bcosGroupInfoList.getResult().stream()
.filter(gInfo -> gInfo.getGroupID().equals(getGroup()))
.findFirst();
Set<String> keys = new TreeSet<>();
keys.addAll(
Arrays.stream(SystemConfigFeature.Features.values())
.map(SystemConfigFeature.Features::toString)
.collect(Collectors.toList()));
keys.addAll(SystemConfigService.getConfigKeys());
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<Map<String, Optional<SystemConfig>>> callback) {

this.getSupportSysConfigKeysAsync(
new RespCallback<Set<String>>() {
@Override
public void onResponse(Set<String> keys) {
Map<String, Optional<SystemConfig>> configMap =
new ConcurrentSkipListMap<>();
if (keys.isEmpty()) {
callback.onResponse(configMap);
return;
}
keys.forEach(
key ->
getSystemConfigByKeyAsync(
key,
new RespCallback<SystemConfig>() {
@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;
Expand Down Expand Up @@ -1130,7 +1248,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) {
Expand All @@ -1146,6 +1265,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);
}
}

Expand Down Expand Up @@ -1604,41 +1729,44 @@ public static <T extends JsonRpcResponse<?>> T parseResponseIntoJsonRpcResponse(
// parse the response into JsonRPCResponse
T jsonRpcResponse =
getObjectMapper().readValue(response.getContent(), responseType);
if (jsonRpcResponse.getError() != null) {
logger.error(
"parseResponseIntoJsonRpcResponse failed for non-empty error message, method: {}, retErrorMessage: {}, retErrorCode: {}",
// error code inside json rpc response
if (jsonRpcResponse.hasError()) {
logger.info(
"parseResponseIntoJsonRpcResponse failed for non-empty error message, method: {}, msg: {}, code: {}, rawRsp: {}",
method,
jsonRpcResponse.getError().getMessage(),
jsonRpcResponse.getError().getCode());
jsonRpcResponse.getError().getCode(),
response.getContentString());
throw new ClientException(
jsonRpcResponse.getError().getCode(),
jsonRpcResponse.getError().getMessage(),
"ErrorMessage: " + jsonRpcResponse.getError().getMessage());
"msg: " + jsonRpcResponse.getError().getMessage());
}
return jsonRpcResponse;
} else {
logger.error(
"parseResponseIntoJsonRpcResponse failed, method: {}, retErrorMessage: {}, retErrorCode: {}",
logger.info(
"parseResponseIntoJsonRpcResponse failed, method: {}, msg: {}, code: {}, rawRsp: {}",
method,
response.getErrorMessage(),
response.getErrorCode());
response.getErrorCode(),
response.getContent());
throw new ClientException(
response.getErrorCode(),
response.getErrorMessage(),
"get response failed, errorCode: "
"get response failed, code: "
+ response.getErrorCode()
+ ", error message: "
+ ", msg: "
+ response.getErrorMessage());
}
} catch (ClientException e) {
logger.error(
logger.info(
"parseResponseIntoJsonRpcResponse failed for decode the message exception, response: {}, errorMessage: {}",
response,
e.getMessage(),
e);
throw e;
} catch (Exception e) {
logger.error(
logger.info(
"parseResponseIntoJsonRpcResponse failed for decode the message exception, response: {}, errorMessage: {}",
response,
e.getMessage(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public String toString() {
+ ", errorMessage='"
+ errorMessage
+ '\''
+ ", msg="
+ getMessage()
+ '}';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,7 +81,8 @@ public static class GroupNodeInfo {
private String name;
private List<ServiceInfo> serviceInfoList;
private Protocol protocol;
private List<String> featureKeys;
private List<String> featureKeys = new ArrayList<>();
private List<String> supportConfigs = new ArrayList<>();

@Override
public String toString() {
Expand Down Expand Up @@ -150,6 +152,14 @@ public void setFeatureKeys(List<String> featureKeys) {
this.featureKeys = featureKeys;
}

public List<String> getSupportConfigs() {
return supportConfigs;
}

public void setSupportConfigs(List<String> supportConfigs) {
this.supportConfigs = supportConfigs;
}

static class ServiceInfo {
private String serviceName;
private int type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class NetworkConfig {

private List<String> peers;
private List<String> tarsPeers;
private int timeout = -1;
private int timeout = 10000; // ms
private String defaultGroup;
private boolean sendRpcRequestToHighestBlockNode = true;

Expand Down
Loading

0 comments on commit fe07802

Please sign in to comment.