Skip to content

Commit 415438c

Browse files
authored
Merge pull request #54 from AntChainOpenLabs/fix/chainmaker_sdk_reuse
[fix][chainmaker][0.1.0] fix the bug about chainmaker sdk reuse and add some logs
2 parents cf03137 + f530b00 commit 415438c

File tree

1 file changed

+74
-40
lines changed

1 file changed

+74
-40
lines changed

pluginset/chainmaker/offchain-plugin/src/main/java/com/alipay/antchain/bridge/plugins/chainmaker/ChainMakerBBCService.java

Lines changed: 74 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,18 @@
3535
import org.chainmaker.pb.common.*;
3636
import org.chainmaker.pb.config.ChainConfigOuterClass;
3737
import org.chainmaker.sdk.*;
38-
import org.chainmaker.sdk.config.SdkConfig;
38+
import org.chainmaker.sdk.config.*;
3939
import org.chainmaker.sdk.crypto.ChainMakerCryptoSuiteException;
40-
import org.chainmaker.sdk.utils.CryptoUtils;
41-
import org.chainmaker.sdk.utils.SdkUtils;
40+
import org.chainmaker.sdk.utils.*;
4241
import org.chainmaker.sdk.utils.Utils;
43-
import org.chainmaker.sdk.utils.UtilsException;
4442
import org.web3j.abi.*;
4543
import org.web3j.abi.datatypes.*;
4644
import org.web3j.abi.datatypes.generated.Bytes32;
4745
import org.web3j.abi.datatypes.generated.Uint32;
4846
import org.web3j.protocol.core.methods.response.Log;
4947

5048
import java.io.IOException;
49+
import java.lang.reflect.Field;
5150
import java.math.BigInteger;
5251
import java.security.NoSuchAlgorithmException;
5352
import java.security.spec.InvalidKeySpecException;
@@ -124,50 +123,66 @@ public void startup(AbstractBBCContext abstractBBCContext) {
124123
getBBCLogger().info("[startup] ChainMaker startup with context: {}",
125124
new String(abstractBBCContext.getConfForBlockchainClient()));
126125

127-
if (ObjectUtil.isNull(abstractBBCContext)) {
128-
throw new RuntimeException("null bbc context");
129-
}
130-
if (ObjectUtil.isEmpty(abstractBBCContext.getConfForBlockchainClient())) {
131-
throw new RuntimeException("empty blockchain client conf");
132-
}
133-
134-
// 1. Obtain the configuration information
135126
try {
136-
config = ChainMakerConfig.fromJsonString(new String(abstractBBCContext.getConfForBlockchainClient()));
137-
} catch (IOException e) {
138-
throw new RuntimeException(e);
139-
}
140-
if (StrUtil.isEmpty(config.getSdkConfig())) {
141-
throw new RuntimeException("SdkConfig is empty");
142-
}
143-
if (config.getAdminTlsKeyPaths().isEmpty()) {
144-
throw new RuntimeException("AdminTlsKeyPath is empty");
145-
}
146-
if (config.getAdminTlsKeyPaths().size() != config.getAdminTlsCertPaths().size()) {
147-
throw new RuntimeException("AdminTlsCertPaths size not match");
148-
}
149-
if (config.getAdminKeyPaths().isEmpty()) {
150-
throw new RuntimeException("AdminKeyPath is empty");
151-
}
152-
if (config.getAdminKeyPaths().size() != config.getAdminKeyPaths().size()) {
153-
throw new RuntimeException("AdminKeyPaths size not match");
154-
}
155-
if (config.getOrgIds().isEmpty()) {
156-
throw new RuntimeException("OrgId is empty");
127+
if (ObjectUtil.isNull(abstractBBCContext)) {
128+
throw new RuntimeException("null bbc context");
129+
}
130+
if (ObjectUtil.isEmpty(abstractBBCContext.getConfForBlockchainClient())) {
131+
throw new RuntimeException("empty blockchain client conf");
132+
}
133+
134+
// 1. Obtain the configuration information
135+
try {
136+
config = ChainMakerConfig.fromJsonString(new String(abstractBBCContext.getConfForBlockchainClient()));
137+
} catch (IOException e) {
138+
throw new RuntimeException(e);
139+
}
140+
if (StrUtil.isEmpty(config.getSdkConfig())) {
141+
throw new RuntimeException("SdkConfig is empty");
142+
}
143+
if (config.getAdminTlsKeyPaths().isEmpty()) {
144+
throw new RuntimeException("AdminTlsKeyPath is empty");
145+
}
146+
if (config.getAdminTlsKeyPaths().size() != config.getAdminTlsCertPaths().size()) {
147+
throw new RuntimeException("AdminTlsCertPaths size not match");
148+
}
149+
if (config.getAdminKeyPaths().isEmpty()) {
150+
throw new RuntimeException("AdminKeyPath is empty");
151+
}
152+
if (config.getAdminKeyPaths().size() != config.getAdminKeyPaths().size()) {
153+
throw new RuntimeException("AdminKeyPaths size not match");
154+
}
155+
if (config.getOrgIds().isEmpty()) {
156+
throw new RuntimeException("OrgId is empty");
157+
}
158+
} catch (Exception e) {
159+
getBBCLogger().error("[startup] Obtain the configuration information exception", e);
160+
throw e;
157161
}
158162

159163
// 2. Connect to the chainmaker network
160164
Gson gson = new Gson();
161165
SdkConfig sdkConfig = gson.fromJson(config.getSdkConfig(), SdkConfig.class);
162166
try {
163167
chainManager = ChainManager.getInstance();
164-
chainClient = chainManager.getChainClient(sdkConfig.getChainClient().getChainId());
165-
if (chainClient == null) {
166-
chainClient = chainManager.createChainClient(sdkConfig);
168+
169+
// 移除chainManager中缓存的链sdk
170+
for (Field field : ChainManager.class.getDeclaredFields()) {
171+
if (StrUtil.equals(field.getName(), "chains")) {
172+
field.setAccessible(true);
173+
Map map = (Map) field.get(chainManager);
174+
map.remove(sdkConfig.getChainClient().getChainId());
175+
getBBCLogger().info("[startup] chainManager remove chain, id: {}",
176+
sdkConfig.getChainClient().getChainId());
177+
break;
178+
}
167179
}
168-
} catch (ChainClientException | RpcServiceClientException | UtilsException |
169-
ChainMakerCryptoSuiteException ex) {
170-
throw new RuntimeException(ex);
180+
181+
chainClient = chainManager.createChainClient(sdkConfig);
182+
} catch (ChainClientException | RpcServiceClientException | UtilsException | ChainMakerCryptoSuiteException |
183+
IllegalAccessException e) {
184+
getBBCLogger().error("[startup] Connect to the chainmaker network exception", e);
185+
throw new RuntimeException(e);
171186
}
172187

173188
// 3. get client address of chainClient
@@ -176,6 +191,7 @@ public void startup(AbstractBBCContext abstractBBCContext) {
176191
chainClient.getClientUser().getCertificate(),
177192
ChainConfigOuterClass.AddrType.ETHEREUM);
178193
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
194+
getBBCLogger().error("[startup] fail to get client address", e);
179195
throw new RuntimeException("fail to get client address", e);
180196
}
181197

@@ -191,13 +207,17 @@ public void startup(AbstractBBCContext abstractBBCContext) {
191207
config.getAdminTlsKeyPaths().get(i),
192208
config.getAdminTlsCertPaths().get(i)));
193209
} catch (ChainMakerCryptoSuiteException e) {
210+
getBBCLogger().error("[startup] fail to create admin user for endorsement", e);
194211
throw new RuntimeException("fail to create admin user for endorsement", e);
195212
}
196213
}
197214

198215
// 3. set context
199216
this.bbcContext = new ChainMakerContext(abstractBBCContext);
200217

218+
this.bbcContext.setAmContractName(this.config.getAmContractName());
219+
this.bbcContext.setSdpContractName(this.config.getSdpContractName());
220+
201221
// 4. set the pre-deployed contracts into context
202222
if (ObjectUtil.isNull(abstractBBCContext.getAuthMessageContract())
203223
&& StrUtil.isNotEmpty(this.config.getAmContractAddressDeployed())) {
@@ -215,7 +235,17 @@ public void startup(AbstractBBCContext abstractBBCContext) {
215235
this.bbcContext.setSdpContract(sdpContract);
216236
}
217237

218-
getBBCLogger().info("ChainMaker startup success");
238+
getBBCLogger().info("ChainMaker startup success, (" +
239+
"amAddr: {}, amStatus: {}, " +
240+
"sdpAddr: {}, sdpStatus: {}," +
241+
"amName: {}, sdpName: {})",
242+
this.bbcContext.getAuthMessageContract() != null ? this.bbcContext.getAuthMessageContract().getContractAddress() : "",
243+
this.bbcContext.getAuthMessageContract() != null ? this.bbcContext.getAuthMessageContract().getStatus() : "",
244+
this.bbcContext.getSdpContract() != null ? this.bbcContext.getSdpContract().getContractAddress() : "",
245+
this.bbcContext.getSdpContract() != null ? this.bbcContext.getSdpContract().getStatus() : "",
246+
this.bbcContext.getAmContractName() != null ? this.bbcContext.getAmContractName() : "",
247+
this.bbcContext.getSdpContractName() != null ? this.bbcContext.getSdpContractName() : ""
248+
);
219249
}
220250

221251
@Override
@@ -282,6 +312,8 @@ public void setupAuthMessageContract() {
282312
authMessageContract.setStatus(ContractStatusEnum.CONTRACT_DEPLOYED);
283313
bbcContext.setAuthMessageContract(authMessageContract);
284314
bbcContext.setAmContractName(contract.getName());
315+
config.setAmContractName(contract.getName());
316+
bbcContext.setConfForBlockchainClient(config.toJsonString().getBytes());
285317
getBBCLogger().info("setup am contract successful: {}-{}",
286318
contract.getName(),
287319
contract.getAddress());
@@ -312,6 +344,8 @@ public void setupSDPMessageContract() {
312344
sdpContract.setStatus(ContractStatusEnum.CONTRACT_DEPLOYED);
313345
bbcContext.setSdpContract(sdpContract);
314346
bbcContext.setSdpContractName(contract.getName());
347+
config.setSdpContractName(contract.getName());
348+
bbcContext.setConfForBlockchainClient(config.toJsonString().getBytes());
315349
getBBCLogger().info("setup sdp contract successful: {}-{}",
316350
contract.getName(),
317351
contract.getAddress());

0 commit comments

Comments
 (0)