Skip to content

Commit 2a134d8

Browse files
authored
Support pulling cryptoType from node (FISCO-BCOS#54)
1 parent ce68360 commit 2a134d8

File tree

13 files changed

+250
-63
lines changed

13 files changed

+250
-63
lines changed

src/integration-test/java/org/fisco/bcos/sdk/BcosSDKTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ public void testSendTransactions() throws ConfigException, ContractException {
189189
//BigInteger blockLimit = sdk.getGroupManagerService().getBlockLimitByGroup(groupId);
190190
BigInteger blockNumber = client.getBlockNumber().getBlockNumber();
191191
// deploy the HelloWorld contract
192-
HelloWorld helloWorld = HelloWorld.deploy(client, sdk.getCryptoInterface());
192+
HelloWorld helloWorld = HelloWorld.deploy(client, client.getCryptoInterface());
193193
checkReceipt(helloWorld, client, blockNumber.add(BigInteger.ONE), helloWorld.getDeployReceipt(), false);
194194

195195
// check the blockLimit has been modified
@@ -208,7 +208,7 @@ public void testSendTransactions() throws ConfigException, ContractException {
208208
Assert.assertTrue(getValue.equals(settedString));
209209

210210
// load contract from the contract adddress
211-
HelloWorld helloWorld2 = HelloWorld.load(helloWorld.getContractAddress(), client, sdk.getCryptoInterface());
211+
HelloWorld helloWorld2 = HelloWorld.load(helloWorld.getContractAddress(), client, client.getCryptoInterface());
212212
Assert.assertTrue(helloWorld2.getContractAddress().equals(helloWorld.getContractAddress()));
213213
settedString = "Hello, Fisco2";
214214
TransactionReceipt receipt2 = helloWorld2.set(settedString);

src/integration-test/java/org/fisco/bcos/sdk/precompiled/PrecompiledTest.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public void test1ConsensusService() throws ConfigException, ContractException {
5656
try {
5757
BcosSDK sdk = new BcosSDK(configFile);
5858
Client client = sdk.getClient(Integer.valueOf(1));
59-
ConsensusService consensusService = new ConsensusService(client, sdk.getCryptoInterface());
59+
ConsensusService consensusService = new ConsensusService(client, client.getCryptoInterface());
6060
// get the current sealerList
6161
List<String> sealerList = client.getSealerList().getResult();
6262

@@ -118,11 +118,11 @@ public void test2CnsService() throws ConfigException {
118118
try {
119119
BcosSDK sdk = new BcosSDK(configFile);
120120
Client client = sdk.getClient(Integer.valueOf(1));
121-
HelloWorld helloWorld = HelloWorld.deploy(client, sdk.getCryptoInterface());
121+
HelloWorld helloWorld = HelloWorld.deploy(client, client.getCryptoInterface());
122122
String contractAddress = helloWorld.getContractAddress();
123123
String contractName = "HelloWorld";
124124
String contractVersion = "1.0";
125-
CnsService cnsService = new CnsService(client, sdk.getCryptoInterface());
125+
CnsService cnsService = new CnsService(client, client.getCryptoInterface());
126126
RetCode retCode = cnsService.registerCNS(contractName, contractVersion, contractAddress, "");
127127
// query the cns information
128128
List<CnsInfo> cnsInfos = cnsService.selectByName(contractName);
@@ -161,7 +161,7 @@ public void test2CnsService() throws ConfigException {
161161
Assert.assertTrue(cnsService.getContractAddress(contractName, contractVersion2).equals(contractAddress));
162162
}
163163
// insert anther cns for other contract
164-
HelloWorld helloWorld2 = HelloWorld.deploy(client, sdk.getCryptoInterface());
164+
HelloWorld helloWorld2 = HelloWorld.deploy(client, client.getCryptoInterface());
165165
String contractAddress2 = helloWorld2.getContractAddress();
166166
String contractName2 = "hello";
167167
retCode = cnsService.registerCNS(contractName2, contractVersion, contractAddress2, "");
@@ -183,7 +183,7 @@ public void test3SystemConfigService() throws ConfigException, ContractException
183183
{
184184
BcosSDK sdk = new BcosSDK(configFile);
185185
Client client = sdk.getClient(Integer.valueOf(1));
186-
SystemConfigService systemConfigService = new SystemConfigService(client, sdk.getCryptoInterface());
186+
SystemConfigService systemConfigService = new SystemConfigService(client, client.getCryptoInterface());
187187
testSystemConfigService(client, systemConfigService, "tx_count_limit");
188188
testSystemConfigService(client, systemConfigService,"tx_gas_limit");
189189
}
@@ -209,7 +209,7 @@ public void test5CRUDService() throws ConfigException, ContractException {
209209
try {
210210
BcosSDK sdk = new BcosSDK(configFile);
211211
Client client = sdk.getClient(Integer.valueOf(1));
212-
TableCRUDService tableCRUDService = new TableCRUDService(client, sdk.getCryptoInterface());
212+
TableCRUDService tableCRUDService = new TableCRUDService(client, client.getCryptoInterface());
213213
// create a user table
214214
String tableName = "test";
215215
String key = "key";
@@ -256,14 +256,14 @@ public void test6PermissionService() throws ConfigException, ContractException {
256256
try {
257257
BcosSDK sdk = new BcosSDK(configFile);
258258
Client client = sdk.getClient(Integer.valueOf(1));
259-
CryptoInterface cryptoInterface = sdk.getCryptoInterface();
259+
CryptoInterface cryptoInterface = client.getCryptoInterface();
260260
PermissionService permissionService = new PermissionService(client, cryptoInterface);
261261

262262
String tableName = "test";
263263
permissionService.grantPermission(tableName, cryptoInterface.getCryptoKeyPair().getAddress());
264264

265265
// insert data to the table with the account without permission
266-
CryptoInterface invalidCryptoInterface = new CryptoInterface(sdk.getCryptoInterface().getCryptoTypeConfig());
266+
CryptoInterface invalidCryptoInterface = new CryptoInterface(client.getCryptoInterface().getCryptoTypeConfig());
267267
TableCRUDService tableCRUDService = new TableCRUDService(client, invalidCryptoInterface);
268268
String key = "key2";
269269
Map<String, String> value = new HashMap<>(5);
@@ -295,7 +295,7 @@ public void test7ContractLifeCycleService() throws ConfigException {
295295
try {
296296
BcosSDK sdk = new BcosSDK(configFile);
297297
Client client = sdk.getClient(Integer.valueOf(1));
298-
CryptoInterface cryptoInterface = sdk.getCryptoInterface();
298+
CryptoInterface cryptoInterface = client.getCryptoInterface();
299299
ContractLifeCycleService contractLifeCycleService = new ContractLifeCycleService(client, cryptoInterface);
300300
// deploy a helloWorld
301301
HelloWorld helloWorld = HelloWorld.deploy(client, cryptoInterface);
@@ -318,7 +318,7 @@ public void test7ContractLifeCycleService() throws ConfigException {
318318
value = helloWorld.get();
319319
Assert.assertTrue("Hello, Fisco1".equals(value));
320320
// grant Manager
321-
CryptoInterface cryptoInterface1 = new CryptoInterface(sdk.getCryptoInterface().getCryptoTypeConfig());
321+
CryptoInterface cryptoInterface1 = new CryptoInterface(client.getCryptoInterface().getCryptoTypeConfig());
322322
ContractLifeCycleService contractLifeCycleService1 = new ContractLifeCycleService(client, cryptoInterface1);
323323
// freeze contract without grant manager
324324
RetCode retCode = contractLifeCycleService1.freeze(helloWorld.getContractAddress());
@@ -345,7 +345,7 @@ public void test8GovernanceService() throws ConfigException {
345345
try {
346346
BcosSDK sdk = new BcosSDK(configFile);
347347
Client client = sdk.getClient(Integer.valueOf(1));
348-
CryptoInterface cryptoInterface = sdk.getCryptoInterface();
348+
CryptoInterface cryptoInterface = client.getCryptoInterface();
349349
ChainGovernanceService chainGovernanceService = new ChainGovernanceService(client, cryptoInterface);
350350

351351
List<PermissionInfo> orgPermissionInfos = chainGovernanceService.listCommitteeMembers();
@@ -360,12 +360,12 @@ public void test8GovernanceService() throws ConfigException {
360360

361361
// create a new account and grantOperator
362362
int orgOperatorSize = chainGovernanceService.listOperators().size();
363-
CryptoInterface cryptoInterface1 = new CryptoInterface(sdk.getCryptoInterface().getCryptoTypeConfig());
363+
CryptoInterface cryptoInterface1 = new CryptoInterface(client.getCryptoInterface().getCryptoTypeConfig());
364364
chainGovernanceService.grantOperator(cryptoInterface1.getCryptoKeyPair().getAddress());
365365
Assert.assertTrue(chainGovernanceService.listOperators().size() == orgOperatorSize + 1);
366366

367367
// only the committeeMember can freeze account
368-
CryptoInterface cryptoInterface2 = new CryptoInterface(sdk.getCryptoInterface().getCryptoTypeConfig());
368+
CryptoInterface cryptoInterface2 = new CryptoInterface(client.getCryptoInterface().getCryptoTypeConfig());
369369
chainGovernanceService.grantOperator(cryptoInterface2.getCryptoKeyPair().getAddress());
370370
// create the account
371371
HelloWorld helloWorld = HelloWorld.deploy(client, cryptoInterface2);

src/main/java/org/fisco/bcos/sdk/BcosSDK.java

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.fisco.bcos.sdk.config.Config;
2020
import org.fisco.bcos.sdk.config.ConfigException;
2121
import org.fisco.bcos.sdk.config.ConfigOption;
22-
import org.fisco.bcos.sdk.crypto.CryptoInterface;
2322
import org.fisco.bcos.sdk.service.GroupManagerService;
2423
import org.fisco.bcos.sdk.service.GroupManagerServiceImpl;
2524
import org.slf4j.Logger;
@@ -33,7 +32,6 @@ public class BcosSDK {
3332
private final ConfigOption config;
3433
private final Channel channel;
3534
private final GroupManagerService groupManagerService;
36-
private final CryptoInterface cryptoInterface;
3735
private ConcurrentHashMap<Integer, Client> groupToClient = new ConcurrentHashMap<>();
3836
private long maxWaitEstablishConnectionTime = 30000;
3937

@@ -42,27 +40,10 @@ public BcosSDK(String configPath) throws ConfigException {
4240
// load configuration file
4341
this.config = Config.load(configPath);
4442
logger.info("create BcosSDK, load configPath: {} succ", configPath);
45-
46-
// get cryptoInterface according to config
47-
if (this.config.cryptoMaterial.get("algorithm").compareToIgnoreCase(ECDSA_TYPE_STR) == 0) {
48-
this.cryptoInterface = new CryptoInterface(CryptoInterface.ECDSA_TYPE);
49-
} else if (this.config.cryptoMaterial.get("algorithm").compareToIgnoreCase(SM_TYPE_STR)
50-
== 0) {
51-
this.cryptoInterface = new CryptoInterface(CryptoInterface.SM_TYPE);
52-
} else {
53-
throw new BcosSDKException(
54-
"create cryptoInterface failed for unsupported crypto type: "
55-
+ this.config.cryptoMaterial.get("algorithm"));
56-
}
57-
logger.info(
58-
"create BcosSDK, creat cryptoInterface, type: {}",
59-
this.config.cryptoMaterial.get("algorithm"));
60-
6143
// create channel
6244
this.channel = Channel.build(this.config);
6345
this.channel.start();
6446
logger.info("create BcosSDK, start channel succ");
65-
6647
if (!waitForEstablishConnection()) {
6748
logger.error("create BcosSDK failed for the number of available peers is 0");
6849
throw new BcosSDKException(
@@ -94,8 +75,11 @@ public Client getClient(Integer groupId) {
9475
if (!groupToClient.contains(groupId)) {
9576
// create a new client for the specified group
9677
Client client = Client.build(this.groupManagerService, this.channel, groupId);
78+
if (client == null) {
79+
throw new BcosSDKException("create client for group " + groupId + " failed!");
80+
}
9781
groupToClient.put(groupId, client);
98-
logger.debug("create SDK for group: {}", groupId);
82+
logger.info("create client for group {} success", groupId);
9983
}
10084
return groupToClient.get(groupId);
10185
}
@@ -111,8 +95,4 @@ public GroupManagerService getGroupManagerService() {
11195
public ConfigOption getConfig() {
11296
return this.config;
11397
}
114-
115-
public CryptoInterface getCryptoInterface() {
116-
return this.cryptoInterface;
117-
}
11898
}

src/main/java/org/fisco/bcos/sdk/channel/Channel.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ static Channel build(ConfigOption config) {
6363
*/
6464
void addConnectHandler(MsgHandler handler);
6565

66+
/**
67+
* Add a establish handler, when the SDK establishes a connection with the node, call the
68+
* handler
69+
*
70+
* @param handler
71+
*/
72+
void addEstablishHandler(MsgHandler handler);
73+
6674
/**
6775
* Add a disconnect handler, when one connection disconnect,
6876
* handler.onDisconnect(ChannleHandlerContext ctx) is called

src/main/java/org/fisco/bcos/sdk/channel/ChannelImp.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,11 @@ public void addConnectHandler(MsgHandler handler) {
142142
msgHandler.addConnectHandler(handler);
143143
}
144144

145+
@Override
146+
public void addEstablishHandler(MsgHandler handler) {
147+
msgHandler.addEstablishHandler(handler);
148+
}
149+
145150
@Override
146151
public void addMessageHandler(MsgType type, MsgHandler handler) {
147152
msgHandler.addMessageHandler(type, handler);

src/main/java/org/fisco/bcos/sdk/channel/ChannelMsgHandler.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public class ChannelMsgHandler implements MsgHandler {
5555
private List<MsgHandler> msgConnectHandlerList = new ArrayList<>();
5656
private List<MsgHandler> msgDisconnectHandleList = new ArrayList<>();
5757
private Map<Integer, MsgHandler> msgHandlers = new ConcurrentHashMap<>();
58+
private List<MsgHandler> msgEstablishHandlerList = new ArrayList<>();
59+
5860
private Map<String, ResponseCallback> seq2Callback = new ConcurrentHashMap<>();
5961
private Map<String, ChannelHandlerContext> availablePeer = new ConcurrentHashMap<>();
6062

@@ -66,6 +68,10 @@ public void addConnectHandler(MsgHandler handler) {
6668
msgConnectHandlerList.add(handler);
6769
}
6870

71+
public void addEstablishHandler(MsgHandler handler) {
72+
msgEstablishHandlerList.add(handler);
73+
}
74+
6975
public void addMessageHandler(MsgType type, MsgHandler handler) {
7076
msgHandlers.put(type.getType(), handler);
7177
}
@@ -84,6 +90,9 @@ public void removeSeq(String seq) {
8490

8591
private void addAvailablePeer(String host, ChannelHandlerContext ctx) {
8692
availablePeer.put(host, ctx);
93+
for (MsgHandler handle : msgEstablishHandlerList) {
94+
handle.onConnect(ctx);
95+
}
8796
}
8897

8998
private ResponseCallback getAndRemoveSeq(String seq) {

src/main/java/org/fisco/bcos/sdk/client/Client.java

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.fisco.bcos.sdk.client.protocol.response.TotalTransactionCount;
5151
import org.fisco.bcos.sdk.client.protocol.response.TransactionReceiptWithProof;
5252
import org.fisco.bcos.sdk.client.protocol.response.TransactionWithProof;
53+
import org.fisco.bcos.sdk.crypto.CryptoInterface;
5354
import org.fisco.bcos.sdk.eventsub.EventSubscribe;
5455
import org.fisco.bcos.sdk.model.NodeVersion;
5556
import org.fisco.bcos.sdk.model.TransactionReceipt;
@@ -74,18 +75,42 @@ public interface Client {
7475
*/
7576
static Client build(GroupManagerService groupManagerService, Channel channel, Integer groupId) {
7677
// check the groupList
77-
Set<String> groupList = groupManagerService.getGroupNodeList(groupId);
78-
if (groupList == null || groupList.size() == 0) {
78+
Set<String> nodeList = groupManagerService.getGroupNodeList(groupId);
79+
if (nodeList == null || nodeList.size() == 0) {
7980
logger.warn("build client failed for no peers setup the group {}", groupId);
8081
return null;
8182
}
82-
return new ClientImpl(groupManagerService, channel, groupId);
83+
// get cryptoType
84+
Integer cryptoType = null;
85+
NodeVersion nodeVersion = null;
86+
for (String node : nodeList) {
87+
cryptoType = groupManagerService.getCryptoType(node);
88+
if (cryptoType != null) {
89+
nodeVersion = groupManagerService.getNodeVersion(node);
90+
break;
91+
}
92+
}
93+
if (cryptoType == null || nodeVersion == null) {
94+
logger.warn(
95+
"build client failed for get crypto type or nodeVersion failed, groupId: {}",
96+
groupId);
97+
return null;
98+
}
99+
CryptoInterface cryptoInterface = new CryptoInterface(cryptoType);
100+
logger.info("build client success for group {}", groupId);
101+
return new ClientImpl(groupManagerService, channel, groupId, cryptoInterface, nodeVersion);
83102
}
84103

85104
static Client build(Channel channel) {
86105
return new ClientImpl(channel);
87106
}
88107

108+
CryptoInterface getCryptoInterface();
109+
110+
NodeVersion getClientNodeVersion();
111+
112+
Integer getCryptoType();
113+
89114
EventSubscribe getEventSubscribe();
90115
/**
91116
* get groupId of the client
@@ -673,6 +698,7 @@ void queryGroupStatusAsync(
673698
*/
674699
void getPbftViewAsync(RespCallback<PbftView> callback);
675700

701+
NodeVersion getNodeVersion(String ipAndPort);
676702
/**
677703
* Peer operation: get node version
678704
*

src/main/java/org/fisco/bcos/sdk/client/ClientImpl.java

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import org.fisco.bcos.sdk.client.protocol.response.TotalTransactionCount;
5656
import org.fisco.bcos.sdk.client.protocol.response.TransactionReceiptWithProof;
5757
import org.fisco.bcos.sdk.client.protocol.response.TransactionWithProof;
58+
import org.fisco.bcos.sdk.crypto.CryptoInterface;
5859
import org.fisco.bcos.sdk.eventsub.EventSubscribe;
5960
import org.fisco.bcos.sdk.model.NodeVersion;
6061
import org.fisco.bcos.sdk.model.TransactionReceipt;
@@ -67,12 +68,20 @@ public class ClientImpl implements Client {
6768
private final Integer groupId;
6869
private final Integer DefaultGroupId = Integer.valueOf(1);
6970
private final EventSubscribe eventSubscribe;
71+
private final CryptoInterface cryptoInterface;
72+
private final NodeVersion nodeVersion;
7073

7174
protected ClientImpl(
72-
GroupManagerService groupManagerService, Channel channel, Integer groupId) {
75+
GroupManagerService groupManagerService,
76+
Channel channel,
77+
Integer groupId,
78+
CryptoInterface cryptoInterface,
79+
NodeVersion nodeVersion) {
7380
this.jsonRpcService = new JsonRpcService(groupManagerService, channel, groupId);
7481
this.groupId = groupId;
7582
this.eventSubscribe = EventSubscribe.build(groupManagerService, groupId);
83+
this.cryptoInterface = cryptoInterface;
84+
this.nodeVersion = nodeVersion;
7685
// send request to the group, and get the blockNumber information
7786
getBlockLimit();
7887
}
@@ -81,6 +90,23 @@ protected ClientImpl(Channel channel) {
8190
this.jsonRpcService = new JsonRpcService(null, channel, null);
8291
this.groupId = null;
8392
this.eventSubscribe = null;
93+
this.cryptoInterface = null;
94+
this.nodeVersion = null;
95+
}
96+
97+
@Override
98+
public CryptoInterface getCryptoInterface() {
99+
return this.cryptoInterface;
100+
}
101+
102+
@Override
103+
public NodeVersion getClientNodeVersion() {
104+
return this.nodeVersion;
105+
}
106+
107+
@Override
108+
public Integer getCryptoType() {
109+
return this.cryptoInterface.getCryptoTypeConfig();
84110
}
85111

86112
@Override
@@ -790,6 +816,14 @@ public NodeVersion getNodeVersion() {
790816
NodeVersion.class);
791817
}
792818

819+
@Override
820+
public NodeVersion getNodeVersion(String ipAndPort) {
821+
return this.jsonRpcService.sendRequestToPeer(
822+
new JsonRpcRequest(JsonRpcMethods.GET_NODE_VERSION, Arrays.asList()),
823+
ipAndPort,
824+
NodeVersion.class);
825+
}
826+
793827
@Override
794828
public void getNodeVersion(RespCallback<NodeVersion> callback) {
795829
this.jsonRpcService.asyncSendRequestToGroup(

0 commit comments

Comments
 (0)