Skip to content

Add anchor peer enhancement: Using SDK #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 23, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
import hlf.java.rest.client.exception.ErrorConstants;
import hlf.java.rest.client.exception.FabricTransactionException;
import hlf.java.rest.client.exception.ServiceException;
import hlf.java.rest.client.model.AnchorPeerDTO;
import hlf.java.rest.client.model.ChannelUpdateParamsDTO;
import hlf.java.rest.client.model.ClientResponseModel;
import hlf.java.rest.client.model.CommitChannelParamsDTO;
import hlf.java.rest.client.service.ChannelConfigDeserialization;
import hlf.java.rest.client.service.NetworkStatus;
import hlf.java.rest.client.service.UpdateChannel;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.hyperledger.fabric.gateway.Gateway;
import org.hyperledger.fabric.gateway.Network;
Expand Down Expand Up @@ -79,7 +82,7 @@ public ResponseEntity<ClientResponseModel> getChannelFromNetwork(String channelN
"One or more arguments included in the config update are invalid",
e);
} catch (TransactionException e) {
log.warn("Error retrieving channel config: " + e.getMessage());
log.warn("Error retrieving channel config: {} ", e.getMessage());
throw new FabricTransactionException(
ErrorCode.HYPERLEDGER_FABRIC_TRANSACTION_ERROR,
ErrorCode.HYPERLEDGER_FABRIC_TRANSACTION_ERROR.name(),
Expand All @@ -91,7 +94,7 @@ public ResponseEntity<ClientResponseModel> getChannelFromNetwork(String channelN
"Error while establishing connection to the gateway",
e);
}
log.warn("Error getting channel config: Network cannot be NULL: " + "Network = " + network);
log.warn("Error getting channel config: Network cannot be NULL: Network: {}", network);
return new ResponseEntity<>(
new ClientResponseModel(ErrorCode.NOT_FOUND.getValue(), ErrorCode.NOT_FOUND.name()),
HttpStatus.OK);
Expand All @@ -110,9 +113,8 @@ public ResponseEntity<ClientResponseModel> generateConfigUpdate(
HttpStatus.OK);
} else {
log.warn(
"Error generating the Config Update: Network and User cannot be NULL: "
+ "Network = "
+ network);
"Error generating the Config Update: Network and User cannot be NULL: Network: {}",
network);
return new ResponseEntity<>(
new ClientResponseModel(
ErrorCode.NOT_FOUND.getValue(),
Expand All @@ -124,7 +126,7 @@ public ResponseEntity<ClientResponseModel> generateConfigUpdate(
private ConfigUpdate createConfigUpdate(
String channelName, ChannelUpdateParamsDTO organizationDetails) {
Network network = gateway.getNetwork(channelName);
if (network != null) {
if (network != null && organizationDetails.getMspDTO() != null) {
try {
Channel selectedChannel = network.getChannel();
byte[] channelConfigBytes = selectedChannel.getChannelConfigurationBytes();
Expand Down Expand Up @@ -161,7 +163,7 @@ private ConfigUpdate createConfigUpdate(
"Channel has no peer or orderers defined. Can not get configuration block",
e);
} catch (TransactionException e) {
log.warn("Error while fetching channel config: " + e.getMessage());
log.warn("Error while fetching channel config: {} ", e.getMessage());
throw new FabricTransactionException(
ErrorCode.HYPERLEDGER_FABRIC_TRANSACTION_ERROR,
ErrorCode.HYPERLEDGER_FABRIC_TRANSACTION_ERROR.name(),
Expand All @@ -175,7 +177,9 @@ private ConfigUpdate createConfigUpdate(
}
} else {
log.warn(
"Error fetching the channel config: Network cannot be NULL: " + "Network = " + network);
"Error fetching the channel config: Network and MSP details cannot be NULL: Network: {} and MSP details: {} ",
network,
organizationDetails.getMspDTO());
return ConfigUpdate.newBuilder().build();
}
}
Expand Down Expand Up @@ -227,11 +231,9 @@ public ResponseEntity<ClientResponseModel> signChannelConfigTransaction(
}
} else {
log.warn(
"Error while signing channel config: Network and User cannot be NULL: "
+ "Network = "
+ network
+ "and User = "
+ user);
"Error while signing channel config: Network and User cannot be NULL: Network: {} and User: {}",
network,
user);
return new ResponseEntity<>(
new ClientResponseModel(
ErrorCode.NOT_FOUND.getValue(),
Expand Down Expand Up @@ -287,7 +289,7 @@ public ResponseEntity<ClientResponseModel> commitChannelConfigTransaction(
"One or more arguments included in the config update are invalid",
e);
} catch (TransactionException e) {
log.warn("Error while committing channel config: " + e.getMessage());
log.warn("Error while committing channel config: {} ", e.getMessage());
throw new FabricTransactionException(
ErrorCode.HYPERLEDGER_FABRIC_TRANSACTION_ERROR,
ErrorCode.HYPERLEDGER_FABRIC_TRANSACTION_ERROR.name(),
Expand All @@ -301,11 +303,9 @@ public ResponseEntity<ClientResponseModel> commitChannelConfigTransaction(
}
} else {
log.warn(
"Error while committing channel config: Network and User cannot be NULL: "
+ "Network = "
+ network
+ "and User = "
+ user);
"Error while committing channel config: Network and User cannot be NULL: Network: {} and User:{}",
network,
user);
return new ResponseEntity<>(
new ClientResponseModel(
ErrorCode.NOT_FOUND.getValue(),
Expand All @@ -318,7 +318,7 @@ public ResponseEntity<ClientResponseModel> commitChannelConfigTransaction(
public ResponseEntity<ClientResponseModel> addOrgToChannel(
String channelName, ChannelUpdateParamsDTO organizationDetails) {
Network network = gateway.getNetwork(channelName);
if (network != null && user != null) {
if (network != null && user != null && organizationDetails.getMspDTO() != null) {
try {
Channel selectedChannel = network.getChannel();
ConfigUpdate configUpdate = createConfigUpdate(channelName, organizationDetails);
Expand All @@ -339,7 +339,7 @@ public ResponseEntity<ClientResponseModel> addOrgToChannel(
"One or more arguments included in the config update are invalid",
e);
} catch (TransactionException e) {
log.warn("Error while committing channel config: " + e.getMessage());
log.warn("Error while committing channel config: {} ", e.getMessage());
throw new FabricTransactionException(
ErrorCode.HYPERLEDGER_FABRIC_TRANSACTION_ERROR,
ErrorCode.HYPERLEDGER_FABRIC_TRANSACTION_ERROR.name(),
Expand All @@ -355,32 +355,51 @@ public ResponseEntity<ClientResponseModel> addOrgToChannel(
new ClientResponseModel(ErrorConstants.NO_ERROR, ErrorCode.SUCCESS.getReason()),
HttpStatus.OK);
} else {
log.warn("Network and User cannot be NULL: " + "Network = " + network + "and User = " + user);
log.warn(
"Network, User and MSP details cannot be NULL: Network: {}, User: {} and MSP details: {}",
network,
user,
organizationDetails.getMspDTO());
return new ResponseEntity<>(
new ClientResponseModel(
ErrorCode.NOT_FOUND.getValue(),
"Network and User cannot be NULL: " + "Network = " + network + "and User = " + user),
"Network, User and MSP details cannot be NULL: "
+ "Network = "
+ network
+ " User = "
+ user
+ " and MSP details = "
+ organizationDetails.getMspDTO()),
HttpStatus.OK);
}
}

private List<String> getAnchorPeersToAdd(ChannelUpdateParamsDTO channelUpdateParamsDTO) {
// Anchor peers are required as Host:Port
List<String> anchorPeerList = new ArrayList<>();
for (AnchorPeerDTO anchorPeerDTO : channelUpdateParamsDTO.getAnchorPeerDTOs()) {
anchorPeerList.add(anchorPeerDTO.getHostname() + ":" + anchorPeerDTO.getPort());
}
return anchorPeerList;
}

@Override
public ResponseEntity<ClientResponseModel> addAnchorPeersToChannel(
String channelName, ChannelUpdateParamsDTO channelUpdateParamsDTO) {
Network network = gateway.getNetwork(channelName);
if (network != null && user != null) {
try {
Channel selectedChannel = network.getChannel();
ConfigUpdate configUpdate = createConfigUpdate(channelName, channelUpdateParamsDTO);
String channelConfigString = JsonFormat.printer().print(configUpdate);
log.info(channelConfigDeserialization.deserializeValueFields(channelConfigString));
UpdateChannelConfiguration updateChannelConfiguration = new UpdateChannelConfiguration();
updateChannelConfiguration.setUpdateChannelConfiguration(
configUpdate.toByteString().toByteArray());
Channel.AnchorPeersConfigUpdateResult configUpdateAnchorPeers =
selectedChannel.getConfigUpdateAnchorPeers(
selectedChannel.getPeers().iterator().next(),
user,
getAnchorPeersToAdd(channelUpdateParamsDTO),
null);
selectedChannel.updateChannelConfiguration(
updateChannelConfiguration,
configUpdateAnchorPeers.getUpdateChannelConfiguration(),
selectedChannel.getUpdateChannelConfigurationSignature(
updateChannelConfiguration, user));
configUpdateAnchorPeers.getUpdateChannelConfiguration(), user));
} catch (InvalidArgumentException e) {
log.warn(
"Error while committing channel config: One or more arguments included in the config update are invalid");
Expand All @@ -389,23 +408,23 @@ public ResponseEntity<ClientResponseModel> addAnchorPeersToChannel(
"One or more arguments included in the config update are invalid",
e);
} catch (TransactionException e) {
log.warn("Error while committing channel config: " + e.getMessage());
log.warn("Error while committing channel config: {}", e.getMessage());
throw new FabricTransactionException(
ErrorCode.HYPERLEDGER_FABRIC_TRANSACTION_ERROR,
ErrorCode.HYPERLEDGER_FABRIC_TRANSACTION_ERROR.name(),
e);
} catch (IOException e) {
log.warn("Error while establishing connection to the gateway");
throw new ServiceException(
ErrorCode.HYPERLEDGER_FABRIC_CONNECTION_ERROR,
"Error while establishing connection to the gateway",
} catch (Exception e) {
log.warn("Error while channel configuration update: {}", e.getMessage());
throw new FabricTransactionException(
ErrorCode.HYPERLEDGER_FABRIC_TRANSACTION_ERROR,
ErrorCode.HYPERLEDGER_FABRIC_TRANSACTION_ERROR.name(),
e);
}
return new ResponseEntity<>(
new ClientResponseModel(ErrorConstants.NO_ERROR, ErrorCode.SUCCESS.getReason()),
HttpStatus.OK);
} else {
log.warn("Network and User cannot be NULL: " + "Network = " + network + "and User = " + user);
log.warn("Network and User cannot be NULL: Network: {} and User: {}", network, user);
return new ResponseEntity<>(
new ClientResponseModel(
ErrorCode.NOT_FOUND.getValue(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,42 +32,21 @@ public ConfigGroup buildWriteset(ConfigGroup readset, ChannelUpdateParamsDTO org
Map<String, ConfigGroup> existingOrganizations =
FabricChannelUtil.getExistingOrgsFromReadset(readset);

ConfigGroup applicationGroup;

if (organizationDetails.getMspDTO() != null) {
// New org addition scenario
// The "Application" group
applicationGroup =
ConfigGroup.newBuilder()
.setModPolicy(FabricClientConstants.CHANNEL_CONFIG_MOD_POLICY_ADMINS)
.putAllPolicies(FabricChannelUtil.setApplicationPolicies(readset))
.putGroups(newOrgMspId, setNewOrgGroup(newOrgMspId, organizationDetails))
// putAllGroups excludes new organization
.putAllGroups(existingOrganizations)
// Application group version
.setVersion(
FabricChannelUtil.retrieveMSPGroupVersionFromReadset(
readset, FabricClientConstants.CHANNEL_CONFIG_GROUP_APPLICATION)
+ 1) // will be tied to current version + 1 for this level
.build();
} else {
// The "Application" group
applicationGroup =
ConfigGroup.newBuilder()
.setModPolicy(FabricClientConstants.CHANNEL_CONFIG_MOD_POLICY_ADMINS)
.putAllPolicies(FabricChannelUtil.setApplicationPolicies(readset))
.putGroups(
newOrgMspId, setAnchorPeerInGroup(newOrgMspId, readset, organizationDetails))
// putAllGroups excludes new organization
.putAllGroups(existingOrganizations)
// Application group version
.setVersion(
FabricChannelUtil.retrieveMSPGroupVersionFromReadset(
readset, FabricClientConstants.CHANNEL_CONFIG_GROUP_APPLICATION)
+ 1) // will be tied to current version + 1 for this level
.build();
}

// New org addition scenario
// The "Application" group
ConfigGroup applicationGroup =
ConfigGroup.newBuilder()
.setModPolicy(FabricClientConstants.CHANNEL_CONFIG_MOD_POLICY_ADMINS)
.putAllPolicies(FabricChannelUtil.setApplicationPolicies(readset))
.putGroups(newOrgMspId, setNewOrgGroup(newOrgMspId, organizationDetails))
// putAllGroups excludes new organization
.putAllGroups(existingOrganizations)
// Application group version
.setVersion(
FabricChannelUtil.retrieveMSPGroupVersionFromReadset(
readset, FabricClientConstants.CHANNEL_CONFIG_GROUP_APPLICATION)
+ 1) // will be tied to current version + 1 for this level
.build();
// the "/Channel" group
return ConfigGroup.newBuilder()
.putGroups(FabricClientConstants.CHANNEL_CONFIG_GROUP_APPLICATION, applicationGroup)
Expand Down
Loading