-
Notifications
You must be signed in to change notification settings - Fork 20
/
Web3Config.java
139 lines (129 loc) · 5.02 KB
/
Web3Config.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
* Copyright 2014-2020 the original author or authors.
*
* 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 com.webank.webase.transaction.config;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import com.webank.webase.transaction.base.ConstantCode;
import com.webank.webase.transaction.base.exception.BaseException;
import java.util.concurrent.ThreadPoolExecutor.AbortPolicy;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.fisco.bcos.channel.client.Service;
import org.fisco.bcos.channel.handler.ChannelConnections;
import org.fisco.bcos.channel.handler.GroupChannelConnectionsConfig;
import org.fisco.bcos.web3j.crypto.EncryptType;
import org.fisco.bcos.web3j.protocol.Web3j;
import org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
/**
* init web3sdk.
*
*/
@Data
@Slf4j
@Configuration
@ConfigurationProperties(prefix = "sdk")
public class Web3Config {
private String orgName;
private int timeout = 10000;
private int corePoolSize = 100;
private int maxPoolSize = 500;
private int queueCapacity = 500;
private int keepAlive = 60;
private GroupChannelConnectionsConfig groupConfig;
// 0: standard, 1: guomi
private int encryptType;
/**
* init web3j.
*
* @return
*/
@Bean
@DependsOn("encryptType")
public HashMap<Integer, Web3j> web3j() throws Exception {
HashMap<Integer, Web3j> web3jMap = new HashMap<Integer, Web3j>();
Service service = new Service();
service.setOrgID(orgName);
service.setThreadPool(sdkThreadPool());
service.setAllChannelConnections(groupConfig);
List<ChannelConnections> channelConnectList = groupConfig.getAllChannelConnections();
for (ChannelConnections connect : channelConnectList) {
int groupId = connect.getGroupId();
log.info("init groupId:{}", groupId);
// set groupId
service.setGroupId(groupId);
service.run();
ChannelEthereumService channelEthereumService = new ChannelEthereumService();
channelEthereumService.setTimeout(timeout);
channelEthereumService.setChannelService(service);
Web3j web3j = Web3j.build(channelEthereumService, groupId);
// whether webase-transaction match with chain's encrypt type: guomi or standard
isMatchEncryptType(web3j);
web3j.getGroupList().send().getGroupList();
web3jMap.put(groupId, web3j);
}
return web3jMap;
}
/**
* set sdk threadPool.
*
* @return
*/
@Bean
public ThreadPoolTaskExecutor sdkThreadPool() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.setKeepAliveSeconds(keepAlive);
executor.setRejectedExecutionHandler(new AbortPolicy());
executor.setThreadNamePrefix("sdkThreadPool-");
executor.initialize();
return executor;
}
/**
* set sdk's encrypt type: 0: standard, 1: guomi sdk switch ecdsa to sm2, sha to sm3.
*/
@Bean(name = "encryptType")
public EncryptType EncryptType() {
return new EncryptType(encryptType);
}
/**
* check local sdk's encrypt type match with chain's
* @param web3j
* @throws IOException
* @throws BaseException
*/
public void isMatchEncryptType(Web3j web3j) throws IOException, BaseException {
boolean isMatch = true;
// 1: guomi, 0: standard
String clientVersion = web3j.getNodeVersion().send().getNodeVersion().getVersion();
log.info("Chain's clientVersion:{}", clientVersion);
if (clientVersion.contains("gm")) {
isMatch = EncryptType.encryptType == 1;
} else {
isMatch = EncryptType.encryptType == 0;
}
if (!isMatch) {
log.error("Chain's version not matches with local encryptType:{}", EncryptType.encryptType);
throw new BaseException(ConstantCode.SYSTEM_ERROR.getCode(), "Chain's version not matches "
+ "with local encryptType"+ EncryptType.encryptType);
}
}
}