Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions .ci/ci_check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ set -e
# check code format
bash gradlew verifyGoogleJavaFormat
# build
bash gradlew build -x integrationTest
bash gradlew build

# check integration-test
## start up FISCO BCOS nodes.
curl -LO https://raw.githubusercontent.com/FISCO-BCOS/FISCO-BCOS/master/tools/build_chain.sh && chmod u+x build_chain.sh
./build_chain.sh -l 127.0.0.1:4
./nodes/127.0.0.1/fisco-bcos -v
./nodes/127.0.0.1/start_all.sh
# ./build_chain.sh -l 127.0.0.1:4 -o nodes

## prepare resources for integration test
mkdir -p src/integration-test/resources/
Expand All @@ -25,6 +24,4 @@ bash gradlew integrationTest

## clean
bash nodes/127.0.0.1/stop_all.sh
bash nodes/127.0.0.1/stop_all.sh
bash nodes/127.0.0.1/stop_all.sh
rm -rf nodes
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ dependencies {
integrationTestCompile 'org.mockito:mockito-core:2.23.0'
}

check.dependsOn integrationTest
integrationTest.mustRunAfter test
// check.dependsOn integrationTest
// integrationTest.mustRunAfter test

archivesBaseName = 'java-sdk'
group = 'org.fisco-bcos'
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/fisco/bcos/sdk/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import org.fisco.bcos.sdk.client.protocol.response.TotalTransactionCount;
import org.fisco.bcos.sdk.client.protocol.response.TransactionReceiptWithProof;
import org.fisco.bcos.sdk.client.protocol.response.TransactionWithProof;
import org.fisco.bcos.sdk.service.GroupManagerService;

/**
* This is the interface of client module.
Expand All @@ -64,7 +65,7 @@ public interface Client {
* @param GroupId
* @return a client instance
*/
Client build(Channel channel, String GroupId);
Client build(GroupManagerService groupManagerService, Channel channel, String GroupId);

/**
* Build a client instance Can only call interfaces relate to group management and node
Expand All @@ -73,7 +74,7 @@ public interface Client {
* @param channel
* @return a client instance
*/
Client build(Channel channel);
Client build(GroupManagerService groupManagerService, Channel channel);

/**
* Ledger operation: send transaction
Expand Down Expand Up @@ -368,7 +369,7 @@ void getTransactionReceiptByHashWithProofAsync(
*
* @return block number
*/
BigInteger getBlockNumberCache();
BigInteger getBlockLimit();

/**
* Group operation: generate a new group
Expand Down
39 changes: 28 additions & 11 deletions src/main/java/org/fisco/bcos/sdk/client/ClientImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,37 @@
import org.fisco.bcos.sdk.client.protocol.response.TotalTransactionCount;
import org.fisco.bcos.sdk.client.protocol.response.TransactionReceiptWithProof;
import org.fisco.bcos.sdk.client.protocol.response.TransactionWithProof;
import org.fisco.bcos.sdk.service.GroupManagerService;
import org.fisco.bcos.sdk.utils.Numeric;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ClientImpl implements Client {
private static Logger logger = LoggerFactory.getLogger(ClientImpl.class);
private final JsonRpcService jsonRpcService;
private final String groupId;
private final Integer groupId;

ClientImpl(Channel channel, String groupId) {
this.jsonRpcService = new JsonRpcService(channel, groupId);
ClientImpl(GroupManagerService groupManagerService, Channel channel, Integer groupId) {
this.jsonRpcService = new JsonRpcService(groupManagerService, channel, groupId);
this.groupId = groupId;
}

/**
* Build a client instance GroupId is identified, all interfaces are available
*
* @param channel
* @param groupId
* @param groupIdStr
* @return a client instance
*/
@Override
public Client build(Channel channel, String groupId) {
return new ClientImpl(channel, groupId);
public Client build(
GroupManagerService groupManagerService, Channel channel, String groupIdStr) {
Integer groupId = Integer.valueOf(groupIdStr);
if (groupId == null) {
logger.warn("build client failed for invalid groupId, groupId: {}", groupIdStr);
return null;
}
return new ClientImpl(groupManagerService, channel, groupId);
}

/**
Expand All @@ -83,8 +93,8 @@ public Client build(Channel channel, String groupId) {
* @return a client instance
*/
@Override
public Client build(Channel channel) {
return new ClientImpl(channel, "1");
public Client build(GroupManagerService groupManagerService, Channel channel) {
return new ClientImpl(groupManagerService, channel, 1);
}

@Override
Expand Down Expand Up @@ -418,9 +428,16 @@ public void getPendingTxSizeAsync(RespCallback<PendingTxSize> callback) {
}

@Override
public BigInteger getBlockNumberCache() {
// TODO: get the cache of the latest block number of the group, and return the blockLimit
return null;
public BigInteger getBlockLimit() {
Integer groupId = Integer.valueOf(this.groupId);
if (this.jsonRpcService.getGroupManagerService().getBlockLimitByGroup(groupId)
== BigInteger.ZERO) {
BigInteger blockNumber = this.getBlockNumber().getBlockNumber();
// update the blockNumber of groupManagerService
this.jsonRpcService.getGroupManagerService().updateBlockNumber(groupId, blockNumber);
return blockNumber;
}
return this.jsonRpcService.getGroupManagerService().getBlockLimitByGroup(groupId);
}

@Override
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/org/fisco/bcos/sdk/client/JsonRpcService.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.fisco.bcos.sdk.model.Message;
import org.fisco.bcos.sdk.model.MsgType;
import org.fisco.bcos.sdk.model.Response;
import org.fisco.bcos.sdk.service.GroupManagerService;
import org.fisco.bcos.sdk.utils.ChannelUtils;
import org.fisco.bcos.sdk.utils.ObjectMapperFactory;
import org.slf4j.Logger;
Expand All @@ -31,10 +32,13 @@
public class JsonRpcService {
protected final ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
private static Logger logger = LoggerFactory.getLogger(JsonRpcService.class);
private final GroupManagerService groupManagerService;
public final Channel channel;
private final String groupId;
private final int groupId;

public JsonRpcService(Channel channel, String groupId) {
public JsonRpcService(
GroupManagerService groupManagerService, Channel channel, Integer groupId) {
this.groupManagerService = groupManagerService;
this.channel = channel;
this.groupId = groupId;
}
Expand All @@ -43,6 +47,10 @@ public Channel getChannel() {
return this.channel;
}

public GroupManagerService getGroupManagerService() {
return this.groupManagerService;
}

public <T extends JsonRpcResponse> T sendRequestToPeer(
JsonRpcRequest request, String peerIpPort, Class<T> responseType) {
return this.sendRequestToPeer(
Expand All @@ -66,7 +74,7 @@ public <T extends JsonRpcResponse> T sendRequestToGroup(
JsonRpcRequest request, MsgType messageType, Class<T> responseType) {
Message message =
encodeRequestToMessage(request, Short.valueOf((short) messageType.ordinal()));
Response response = channel.sendToGroup(message, this.groupId);
Response response = this.groupManagerService.sendMessageToGroup(this.groupId, message);
return this.parseResponseIntoJsonRpcResponse(request, response, responseType);
}

Expand Down Expand Up @@ -118,9 +126,9 @@ public <T extends JsonRpcResponse> void asyncSendRequestToGroup(
RespCallback<T> callback) {
Message message =
encodeRequestToMessage(request, Short.valueOf((short) messageType.ordinal()));
this.channel.asyncSendToGroup(
message,
this.groupManagerService.asyncSendMessageToGroup(
this.groupId,
message,
new ResponseCallback() {
@Override
public void onResponse(Response response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import com.fasterxml.jackson.databind.ObjectReader;
import java.io.IOException;
import java.util.Optional;
import org.fisco.bcos.sdk.client.protocol.model.TransactionReceipt;
import org.fisco.bcos.sdk.model.TransactionReceipt;
import org.fisco.bcos.sdk.utils.ObjectMapperFactory;

/** getTransactionReceipt. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import java.util.Objects;
import org.fisco.bcos.sdk.client.protocol.model.MerkleProofUnit;
import org.fisco.bcos.sdk.client.protocol.model.TransactionReceipt;
import org.fisco.bcos.sdk.model.MerkleProofUnit;
import org.fisco.bcos.sdk.model.TransactionReceipt;

/** getTransactionReceiptWithProof. */
public class TransactionReceiptWithProof
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.List;
import java.util.Objects;
import org.fisco.bcos.sdk.client.protocol.model.JsonTransactionResponse;
import org.fisco.bcos.sdk.client.protocol.model.MerkleProofUnit;
import org.fisco.bcos.sdk.model.MerkleProofUnit;

/** getTransactionWithProof. */
public class TransactionWithProof
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*
*/

package org.fisco.bcos.sdk.client.protocol.model;
package org.fisco.bcos.sdk.model;

import java.util.List;
import java.util.Objects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* the License.
*
*/
package org.fisco.bcos.sdk.client.protocol.model;
package org.fisco.bcos.sdk.model;

import java.util.List;
import java.util.Objects;
Expand Down
136 changes: 136 additions & 0 deletions src/main/java/org/fisco/bcos/sdk/service/GroupManagerService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/**
* Copyright 2014-2020 [fisco-dev]
*
* <p>Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of the License at
*
* <p>http://www.apache.org/licenses/LICENSE-2.0
*
* <p>Unless required by applicable law or agreed to in writing, software distributed under the
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fisco.bcos.sdk.service;

import java.math.BigInteger;
import java.util.List;
import java.util.Set;
import org.fisco.bcos.sdk.channel.PeerSelectRule;
import org.fisco.bcos.sdk.channel.ResponseCallback;
import org.fisco.bcos.sdk.model.Message;
import org.fisco.bcos.sdk.model.Response;

public interface GroupManagerService {

/**
* Update the group list information of the node
*
* @param peerIpAndPort: Node ip and port information
* @param groupList: Group list of nodes
*/
void updateGroupInfo(String peerIpAndPort, List<String> groupList);

/**
* Get the blockNumber notify message from the AMOP module, parse the package and update the
* latest block height of each group
*
* @param peerIpAndPort: Node ip and port
* @param blockNumberNotifyMessage: the blockNumber notify message
*/
void updateBlockNumberInfo(String peerIpAndPort, Message blockNumberNotifyMessage);

/**
* update the block number information for the specified group
*
* @param groupId: the specified groupId
* @param currentBlockNumber: the current blockNumber
*/
void updateBlockNumber(int groupId, BigInteger currentBlockNumber);

/**
* Get block limit of specified group
*
* @param groupId: The specified groupId
* @return: the blockLimit(needed by the transaction module)
*/
BigInteger getBlockLimitByGroup(int groupId);

/**
* Get the node list of the specified group
*
* @param groupId: The group id
* @return: The node list that started the group
*/
Set<String> getGroupNodeList(int groupId);

/**
* Get the group list of specified node
*
* @param nodeAddress: The ip and port info of the node
* @return: List of groups started by the node
*/
Set<Integer> getGroupInfoByNodeInfo(String nodeAddress);

/**
* Send a message to a node in the group and select the node with the highest block height in
* the group
*
* @param groupId: The group the message is sent to
* @param message: The message to be sent
* @return: response of the node located in the specified group
*/
Response sendMessageToGroup(int groupId, Message message);

/**
* Send messages to nodes in the group according to specified rules (If multiple nodes are
* filtered out, only select one of them to send the message)
*
* @param groupId: The group the message is sent to
* @param message: The message to be sent
* @param rule: Rule for filtering the target nodes
* @return: callback to be called after receiving response
* @param callback:
*/
Response sendMessageToGroupByRule(
int groupId, Message message, PeerSelectRule rule, ResponseCallback callback);

/**
* Send a message to a node in the group and select the node with the highest block height in
* the group
*
* @param groupId: The group the message is sent to
* @param message: The message to be sent
* @param callback: callback to be called after receiving response
*/
void asyncSendMessageToGroup(int groupId, Message message, ResponseCallback callback);

/**
* Send messages to nodes in the group according to specified rules (If multiple nodes are
* filtered out, only select one of them to send the message)
*
* @param groupId: The group the message is sent to
* @param message: The message to be sent
* @param rule: Rules for filtering the target nodes
* @param callback:: Function to be called after receiving response
*/
void asyncSendMessageToGroupByRule(
int groupId, Message message, PeerSelectRule rule, ResponseCallback callback);

/**
* Send messages to nodes in the group according to specified rules
*
* @param groupId: The group the message is sent to
* @param message: The message to be sent
* @param rule: Rules for filtering the target nodes
*/
void multicastMessageToGroup(int groupId, Message message, PeerSelectRule rule);

/**
* Broadcast messages to all the nodes of the specified group
*
* @param groupId: The group the message is sent to
* @param message: The message to be sent
*/
void broadcastMessageToGroup(int groupId, Message message);
}
Loading