Skip to content

Commit d78c425

Browse files
Add anchor peer enhancement: Using SDK (#126)
* Add anchor peer enhancement: Using SDK Signed-off-by: n0s09by <[email protected]> * Modified tests for anchor peer Signed-off-by: n0s09by <[email protected]> * validation and test modification Signed-off-by: n0s09by <[email protected]> --------- Signed-off-by: n0s09by <[email protected]>
1 parent 4837f4b commit d78c425

File tree

3 files changed

+89
-114
lines changed

3 files changed

+89
-114
lines changed

src/main/java/hlf/java/rest/client/service/impl/NetworkStatusImpl.java

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@
88
import hlf.java.rest.client.exception.ErrorConstants;
99
import hlf.java.rest.client.exception.FabricTransactionException;
1010
import hlf.java.rest.client.exception.ServiceException;
11+
import hlf.java.rest.client.model.AnchorPeerDTO;
1112
import hlf.java.rest.client.model.ChannelUpdateParamsDTO;
1213
import hlf.java.rest.client.model.ClientResponseModel;
1314
import hlf.java.rest.client.model.CommitChannelParamsDTO;
1415
import hlf.java.rest.client.service.ChannelConfigDeserialization;
1516
import hlf.java.rest.client.service.NetworkStatus;
1617
import hlf.java.rest.client.service.UpdateChannel;
1718
import java.io.IOException;
19+
import java.util.ArrayList;
1820
import java.util.Base64;
21+
import java.util.List;
1922
import lombok.extern.slf4j.Slf4j;
2023
import org.hyperledger.fabric.gateway.Gateway;
2124
import org.hyperledger.fabric.gateway.Network;
@@ -79,7 +82,7 @@ public ResponseEntity<ClientResponseModel> getChannelFromNetwork(String channelN
7982
"One or more arguments included in the config update are invalid",
8083
e);
8184
} catch (TransactionException e) {
82-
log.warn("Error retrieving channel config: " + e.getMessage());
85+
log.warn("Error retrieving channel config: {} ", e.getMessage());
8386
throw new FabricTransactionException(
8487
ErrorCode.HYPERLEDGER_FABRIC_TRANSACTION_ERROR,
8588
ErrorCode.HYPERLEDGER_FABRIC_TRANSACTION_ERROR.name(),
@@ -91,7 +94,7 @@ public ResponseEntity<ClientResponseModel> getChannelFromNetwork(String channelN
9194
"Error while establishing connection to the gateway",
9295
e);
9396
}
94-
log.warn("Error getting channel config: Network cannot be NULL: " + "Network = " + network);
97+
log.warn("Error getting channel config: Network cannot be NULL: Network: {}", network);
9598
return new ResponseEntity<>(
9699
new ClientResponseModel(ErrorCode.NOT_FOUND.getValue(), ErrorCode.NOT_FOUND.name()),
97100
HttpStatus.OK);
@@ -110,9 +113,8 @@ public ResponseEntity<ClientResponseModel> generateConfigUpdate(
110113
HttpStatus.OK);
111114
} else {
112115
log.warn(
113-
"Error generating the Config Update: Network and User cannot be NULL: "
114-
+ "Network = "
115-
+ network);
116+
"Error generating the Config Update: Network and User cannot be NULL: Network: {}",
117+
network);
116118
return new ResponseEntity<>(
117119
new ClientResponseModel(
118120
ErrorCode.NOT_FOUND.getValue(),
@@ -124,7 +126,7 @@ public ResponseEntity<ClientResponseModel> generateConfigUpdate(
124126
private ConfigUpdate createConfigUpdate(
125127
String channelName, ChannelUpdateParamsDTO organizationDetails) {
126128
Network network = gateway.getNetwork(channelName);
127-
if (network != null) {
129+
if (network != null && organizationDetails.getMspDTO() != null) {
128130
try {
129131
Channel selectedChannel = network.getChannel();
130132
byte[] channelConfigBytes = selectedChannel.getChannelConfigurationBytes();
@@ -161,7 +163,7 @@ private ConfigUpdate createConfigUpdate(
161163
"Channel has no peer or orderers defined. Can not get configuration block",
162164
e);
163165
} catch (TransactionException e) {
164-
log.warn("Error while fetching channel config: " + e.getMessage());
166+
log.warn("Error while fetching channel config: {} ", e.getMessage());
165167
throw new FabricTransactionException(
166168
ErrorCode.HYPERLEDGER_FABRIC_TRANSACTION_ERROR,
167169
ErrorCode.HYPERLEDGER_FABRIC_TRANSACTION_ERROR.name(),
@@ -175,7 +177,9 @@ private ConfigUpdate createConfigUpdate(
175177
}
176178
} else {
177179
log.warn(
178-
"Error fetching the channel config: Network cannot be NULL: " + "Network = " + network);
180+
"Error fetching the channel config: Network and MSP details cannot be NULL: Network: {} and MSP details: {} ",
181+
network,
182+
organizationDetails.getMspDTO());
179183
return ConfigUpdate.newBuilder().build();
180184
}
181185
}
@@ -227,11 +231,9 @@ public ResponseEntity<ClientResponseModel> signChannelConfigTransaction(
227231
}
228232
} else {
229233
log.warn(
230-
"Error while signing channel config: Network and User cannot be NULL: "
231-
+ "Network = "
232-
+ network
233-
+ "and User = "
234-
+ user);
234+
"Error while signing channel config: Network and User cannot be NULL: Network: {} and User: {}",
235+
network,
236+
user);
235237
return new ResponseEntity<>(
236238
new ClientResponseModel(
237239
ErrorCode.NOT_FOUND.getValue(),
@@ -287,7 +289,7 @@ public ResponseEntity<ClientResponseModel> commitChannelConfigTransaction(
287289
"One or more arguments included in the config update are invalid",
288290
e);
289291
} catch (TransactionException e) {
290-
log.warn("Error while committing channel config: " + e.getMessage());
292+
log.warn("Error while committing channel config: {} ", e.getMessage());
291293
throw new FabricTransactionException(
292294
ErrorCode.HYPERLEDGER_FABRIC_TRANSACTION_ERROR,
293295
ErrorCode.HYPERLEDGER_FABRIC_TRANSACTION_ERROR.name(),
@@ -301,11 +303,9 @@ public ResponseEntity<ClientResponseModel> commitChannelConfigTransaction(
301303
}
302304
} else {
303305
log.warn(
304-
"Error while committing channel config: Network and User cannot be NULL: "
305-
+ "Network = "
306-
+ network
307-
+ "and User = "
308-
+ user);
306+
"Error while committing channel config: Network and User cannot be NULL: Network: {} and User:{}",
307+
network,
308+
user);
309309
return new ResponseEntity<>(
310310
new ClientResponseModel(
311311
ErrorCode.NOT_FOUND.getValue(),
@@ -318,7 +318,7 @@ public ResponseEntity<ClientResponseModel> commitChannelConfigTransaction(
318318
public ResponseEntity<ClientResponseModel> addOrgToChannel(
319319
String channelName, ChannelUpdateParamsDTO organizationDetails) {
320320
Network network = gateway.getNetwork(channelName);
321-
if (network != null && user != null) {
321+
if (network != null && user != null && organizationDetails.getMspDTO() != null) {
322322
try {
323323
Channel selectedChannel = network.getChannel();
324324
ConfigUpdate configUpdate = createConfigUpdate(channelName, organizationDetails);
@@ -339,7 +339,7 @@ public ResponseEntity<ClientResponseModel> addOrgToChannel(
339339
"One or more arguments included in the config update are invalid",
340340
e);
341341
} catch (TransactionException e) {
342-
log.warn("Error while committing channel config: " + e.getMessage());
342+
log.warn("Error while committing channel config: {} ", e.getMessage());
343343
throw new FabricTransactionException(
344344
ErrorCode.HYPERLEDGER_FABRIC_TRANSACTION_ERROR,
345345
ErrorCode.HYPERLEDGER_FABRIC_TRANSACTION_ERROR.name(),
@@ -355,32 +355,51 @@ public ResponseEntity<ClientResponseModel> addOrgToChannel(
355355
new ClientResponseModel(ErrorConstants.NO_ERROR, ErrorCode.SUCCESS.getReason()),
356356
HttpStatus.OK);
357357
} else {
358-
log.warn("Network and User cannot be NULL: " + "Network = " + network + "and User = " + user);
358+
log.warn(
359+
"Network, User and MSP details cannot be NULL: Network: {}, User: {} and MSP details: {}",
360+
network,
361+
user,
362+
organizationDetails.getMspDTO());
359363
return new ResponseEntity<>(
360364
new ClientResponseModel(
361365
ErrorCode.NOT_FOUND.getValue(),
362-
"Network and User cannot be NULL: " + "Network = " + network + "and User = " + user),
366+
"Network, User and MSP details cannot be NULL: "
367+
+ "Network = "
368+
+ network
369+
+ " User = "
370+
+ user
371+
+ " and MSP details = "
372+
+ organizationDetails.getMspDTO()),
363373
HttpStatus.OK);
364374
}
365375
}
366376

377+
private List<String> getAnchorPeersToAdd(ChannelUpdateParamsDTO channelUpdateParamsDTO) {
378+
// Anchor peers are required as Host:Port
379+
List<String> anchorPeerList = new ArrayList<>();
380+
for (AnchorPeerDTO anchorPeerDTO : channelUpdateParamsDTO.getAnchorPeerDTOs()) {
381+
anchorPeerList.add(anchorPeerDTO.getHostname() + ":" + anchorPeerDTO.getPort());
382+
}
383+
return anchorPeerList;
384+
}
385+
367386
@Override
368387
public ResponseEntity<ClientResponseModel> addAnchorPeersToChannel(
369388
String channelName, ChannelUpdateParamsDTO channelUpdateParamsDTO) {
370389
Network network = gateway.getNetwork(channelName);
371390
if (network != null && user != null) {
372391
try {
373392
Channel selectedChannel = network.getChannel();
374-
ConfigUpdate configUpdate = createConfigUpdate(channelName, channelUpdateParamsDTO);
375-
String channelConfigString = JsonFormat.printer().print(configUpdate);
376-
log.info(channelConfigDeserialization.deserializeValueFields(channelConfigString));
377-
UpdateChannelConfiguration updateChannelConfiguration = new UpdateChannelConfiguration();
378-
updateChannelConfiguration.setUpdateChannelConfiguration(
379-
configUpdate.toByteString().toByteArray());
393+
Channel.AnchorPeersConfigUpdateResult configUpdateAnchorPeers =
394+
selectedChannel.getConfigUpdateAnchorPeers(
395+
selectedChannel.getPeers().iterator().next(),
396+
user,
397+
getAnchorPeersToAdd(channelUpdateParamsDTO),
398+
null);
380399
selectedChannel.updateChannelConfiguration(
381-
updateChannelConfiguration,
400+
configUpdateAnchorPeers.getUpdateChannelConfiguration(),
382401
selectedChannel.getUpdateChannelConfigurationSignature(
383-
updateChannelConfiguration, user));
402+
configUpdateAnchorPeers.getUpdateChannelConfiguration(), user));
384403
} catch (InvalidArgumentException e) {
385404
log.warn(
386405
"Error while committing channel config: One or more arguments included in the config update are invalid");
@@ -389,23 +408,23 @@ public ResponseEntity<ClientResponseModel> addAnchorPeersToChannel(
389408
"One or more arguments included in the config update are invalid",
390409
e);
391410
} catch (TransactionException e) {
392-
log.warn("Error while committing channel config: " + e.getMessage());
411+
log.warn("Error while committing channel config: {}", e.getMessage());
393412
throw new FabricTransactionException(
394413
ErrorCode.HYPERLEDGER_FABRIC_TRANSACTION_ERROR,
395414
ErrorCode.HYPERLEDGER_FABRIC_TRANSACTION_ERROR.name(),
396415
e);
397-
} catch (IOException e) {
398-
log.warn("Error while establishing connection to the gateway");
399-
throw new ServiceException(
400-
ErrorCode.HYPERLEDGER_FABRIC_CONNECTION_ERROR,
401-
"Error while establishing connection to the gateway",
416+
} catch (Exception e) {
417+
log.warn("Error while channel configuration update: {}", e.getMessage());
418+
throw new FabricTransactionException(
419+
ErrorCode.HYPERLEDGER_FABRIC_TRANSACTION_ERROR,
420+
ErrorCode.HYPERLEDGER_FABRIC_TRANSACTION_ERROR.name(),
402421
e);
403422
}
404423
return new ResponseEntity<>(
405424
new ClientResponseModel(ErrorConstants.NO_ERROR, ErrorCode.SUCCESS.getReason()),
406425
HttpStatus.OK);
407426
} else {
408-
log.warn("Network and User cannot be NULL: " + "Network = " + network + "and User = " + user);
427+
log.warn("Network and User cannot be NULL: Network: {} and User: {}", network, user);
409428
return new ResponseEntity<>(
410429
new ClientResponseModel(
411430
ErrorCode.NOT_FOUND.getValue(),

src/main/java/hlf/java/rest/client/service/impl/UpdateChannelImpl.java

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,42 +32,21 @@ public ConfigGroup buildWriteset(ConfigGroup readset, ChannelUpdateParamsDTO org
3232
Map<String, ConfigGroup> existingOrganizations =
3333
FabricChannelUtil.getExistingOrgsFromReadset(readset);
3434

35-
ConfigGroup applicationGroup;
36-
37-
if (organizationDetails.getMspDTO() != null) {
38-
// New org addition scenario
39-
// The "Application" group
40-
applicationGroup =
41-
ConfigGroup.newBuilder()
42-
.setModPolicy(FabricClientConstants.CHANNEL_CONFIG_MOD_POLICY_ADMINS)
43-
.putAllPolicies(FabricChannelUtil.setApplicationPolicies(readset))
44-
.putGroups(newOrgMspId, setNewOrgGroup(newOrgMspId, organizationDetails))
45-
// putAllGroups excludes new organization
46-
.putAllGroups(existingOrganizations)
47-
// Application group version
48-
.setVersion(
49-
FabricChannelUtil.retrieveMSPGroupVersionFromReadset(
50-
readset, FabricClientConstants.CHANNEL_CONFIG_GROUP_APPLICATION)
51-
+ 1) // will be tied to current version + 1 for this level
52-
.build();
53-
} else {
54-
// The "Application" group
55-
applicationGroup =
56-
ConfigGroup.newBuilder()
57-
.setModPolicy(FabricClientConstants.CHANNEL_CONFIG_MOD_POLICY_ADMINS)
58-
.putAllPolicies(FabricChannelUtil.setApplicationPolicies(readset))
59-
.putGroups(
60-
newOrgMspId, setAnchorPeerInGroup(newOrgMspId, readset, organizationDetails))
61-
// putAllGroups excludes new organization
62-
.putAllGroups(existingOrganizations)
63-
// Application group version
64-
.setVersion(
65-
FabricChannelUtil.retrieveMSPGroupVersionFromReadset(
66-
readset, FabricClientConstants.CHANNEL_CONFIG_GROUP_APPLICATION)
67-
+ 1) // will be tied to current version + 1 for this level
68-
.build();
69-
}
70-
35+
// New org addition scenario
36+
// The "Application" group
37+
ConfigGroup applicationGroup =
38+
ConfigGroup.newBuilder()
39+
.setModPolicy(FabricClientConstants.CHANNEL_CONFIG_MOD_POLICY_ADMINS)
40+
.putAllPolicies(FabricChannelUtil.setApplicationPolicies(readset))
41+
.putGroups(newOrgMspId, setNewOrgGroup(newOrgMspId, organizationDetails))
42+
// putAllGroups excludes new organization
43+
.putAllGroups(existingOrganizations)
44+
// Application group version
45+
.setVersion(
46+
FabricChannelUtil.retrieveMSPGroupVersionFromReadset(
47+
readset, FabricClientConstants.CHANNEL_CONFIG_GROUP_APPLICATION)
48+
+ 1) // will be tied to current version + 1 for this level
49+
.build();
7150
// the "/Channel" group
7251
return ConfigGroup.newBuilder()
7352
.putGroups(FabricClientConstants.CHANNEL_CONFIG_GROUP_APPLICATION, applicationGroup)

0 commit comments

Comments
 (0)