Skip to content

Commit

Permalink
Add capabilities during org addition to channel (#140)
Browse files Browse the repository at this point in the history
* add capabilities during org addition to channel

Signed-off-by: Deepika Karanji - d0k03k3 <Deepika.Karanji@walmartlabs.com>

* Move common function to utils

Signed-off-by: Deepika Karanji - d0k03k3 <Deepika.Karanji@walmartlabs.com>

---------

Signed-off-by: Deepika Karanji - d0k03k3 <Deepika.Karanji@walmartlabs.com>
Co-authored-by: Deepika Karanji - d0k03k3 <Deepika.Karanji@walmartlabs.com>
  • Loading branch information
DeepikaKaranji and Deepika Karanji - d0k03k3 authored Mar 21, 2024
1 parent b93f7db commit 0019998
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ private String commitChaincode(

try {

Collection<Peer> peers = channel.getPeersForOrganization(gateway.getIdentity().getMspId());
Collection<Peer> peers = channel.getPeers();

LifecycleCommitChaincodeDefinitionRequest lifecycleCommitChaincodeDefinitionRequest =
hfClientWrapper.getHfClient().newLifecycleCommitChaincodeDefinitionRequest();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package hlf.java.rest.client.service.impl;

import static hlf.java.rest.client.util.FabricClientConstants.FABRIC_2_0;
import static hlf.java.rest.client.util.FabricClientConstants.VALUE_TAG_CAPABILITIES;

import com.google.protobuf.ByteString;
import com.google.protobuf.Timestamp;
import hlf.java.rest.client.exception.ChannelOperationException;
Expand Down Expand Up @@ -51,8 +54,6 @@ public class ChannelServiceImpl implements ChannelService {
private static final String EMPTY_MOD_POLICY = "";
private static final String GROUP_TAG_APPLICATION = "Application";
private static final String VALUE_TAG_CONSORTIUM = "Consortium";
private static final String VALUE_TAG_CAPABILITIES = "Capabilities";
private static final String FABRIC_2_0 = "V2_0";
private static final int INIT_VERSION = 1;
private static final int EMPTY_VERSION = 0;
private static final int HEADER_TYPE = 2;
Expand Down Expand Up @@ -279,7 +280,8 @@ private Configtx.ConfigGroup newChannelGroup(
}
channelGroupBuilder.setVersion(EMPTY_VERSION).setModPolicy(EMPTY_MOD_POLICY);
// add capabilities for V2_0 lifecycle chaincode support
channelGroupBuilder.putValues(VALUE_TAG_CAPABILITIES, getCapabilities(FABRIC_2_0));
channelGroupBuilder.putValues(
VALUE_TAG_CAPABILITIES, FabricChannelUtil.getCapabilitiesWithDefaultVersion(FABRIC_2_0));
return channelGroupBuilder.build();
}

Expand All @@ -296,7 +298,8 @@ private Configtx.ConfigGroup newApplicationGroup(Map<String, MSPDTO> mspMap, boo
if (isWriteSet) {
addDefaultImplicitMetaPolicy(appGroupBuilder);
appGroupBuilder.setVersion(INIT_VERSION);
appGroupBuilder.putValues(VALUE_TAG_CAPABILITIES, getCapabilities(FABRIC_2_0));
appGroupBuilder.putValues(
VALUE_TAG_CAPABILITIES, FabricChannelUtil.getCapabilitiesWithDefaultVersion(FABRIC_2_0));
}
for (Map.Entry<String, MSPDTO> entry : mspMap.entrySet()) {
if (entry.getValue() != null) {
Expand All @@ -309,23 +312,6 @@ private Configtx.ConfigGroup newApplicationGroup(Map<String, MSPDTO> mspMap, boo
return appGroupBuilder.build();
}

/**
* @param capabilities capabilities need to be added to config
* @return channel capabilities
*/
private Configtx.ConfigValue getCapabilities(String... capabilities) {
Configtx.ConfigValue.Builder valueBuilder = Configtx.ConfigValue.newBuilder();
valueBuilder.setModPolicy(DEFAULT_MOD_POLICY);
Configuration.Capabilities.Builder capabilitiesBuilder =
Configuration.Capabilities.newBuilder();
for (String capability : capabilities) {
capabilitiesBuilder.putCapabilities(
capability, Configuration.Capability.newBuilder().build());
}
valueBuilder.setValue(capabilitiesBuilder.build().toByteString());
return valueBuilder.build();
}

/**
* generate empty msp config group
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ public ResponseEntity<ClientResponseModel> addOrgToChannel(
UpdateChannelConfiguration updateChannelConfiguration = new UpdateChannelConfiguration();
updateChannelConfiguration.setUpdateChannelConfiguration(
configUpdate.toByteString().toByteArray());

selectedChannel.updateChannelConfiguration(
updateChannelConfiguration,
selectedChannel.getUpdateChannelConfigurationSignature(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package hlf.java.rest.client.service.impl;

import static hlf.java.rest.client.util.FabricClientConstants.FABRIC_2_0;
import static hlf.java.rest.client.util.FabricClientConstants.VALUE_TAG_CAPABILITIES;

import com.google.protobuf.ByteString;
import hlf.java.rest.client.exception.ServiceException;
import hlf.java.rest.client.model.AnchorPeerDTO;
Expand Down Expand Up @@ -39,6 +42,9 @@ public ConfigGroup buildWriteset(ConfigGroup readset, ChannelUpdateParamsDTO org
.setModPolicy(FabricClientConstants.CHANNEL_CONFIG_MOD_POLICY_ADMINS)
.putAllPolicies(FabricChannelUtil.setApplicationPolicies(readset))
.putGroups(newOrgMspId, setNewOrgGroup(newOrgMspId, organizationDetails))
.putValues(
VALUE_TAG_CAPABILITIES,
FabricChannelUtil.getCapabilitiesWithDefaultVersion(FABRIC_2_0))
// putAllGroups excludes new organization
.putAllGroups(existingOrganizations)
// Application group version
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/hlf/java/rest/client/util/FabricChannelUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import java.util.Map;
import lombok.experimental.UtilityClass;
import org.hyperledger.fabric.protos.common.Configtx;
import org.hyperledger.fabric.protos.common.Configuration;
import org.hyperledger.fabric.protos.common.MspPrincipal;
import org.hyperledger.fabric.protos.common.Policies;

@UtilityClass
public class FabricChannelUtil {
private final int DEFAULT_VERSION = 0;
private static final String DEFAULT_MOD_POLICY = "Admins";

/**
* get default configuration policy for organization that maps the roles. The policy type is
Expand Down Expand Up @@ -240,4 +242,22 @@ public Map<String, Configtx.ConfigPolicy> setApplicationPolicies(Configtx.Config
FabricClientConstants.CHANNEL_CONFIG_POLICY_TYPE_WRITERS, writerPolicy);
return applicationPoliciesMap;
}

/**
* @param capabilities capabilities need to be added to config
* @return channel capabilities
*/
public Configtx.ConfigValue getCapabilitiesWithDefaultVersion(String... capabilities) {
Configtx.ConfigValue.Builder valueBuilder = Configtx.ConfigValue.newBuilder();
Configuration.Capabilities.Builder capabilitiesBuilder =
Configuration.Capabilities.newBuilder();
valueBuilder.setModPolicy(DEFAULT_MOD_POLICY);
valueBuilder.setVersion(0);
for (String capability : capabilities) {
capabilitiesBuilder.putCapabilities(
capability, Configuration.Capability.newBuilder().build());
}
valueBuilder.setValue(capabilitiesBuilder.build().toByteString());
return valueBuilder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class FabricClientConstants {
public static final String ERROR_MSG = "error_msg";
public static final String PEER_LIST = "peers";

public static final String FABRIC_2_0 = "V2_0";
public static final String FABRIC_TRANSACTION_ID = "fabric_tx_id";
public static final String FABRIC_EVENT_NAME = "event_name";
public static final String FABRIC_CHANNEL_NAME = "channel_name";
Expand Down Expand Up @@ -77,4 +78,6 @@ public class FabricClientConstants {
public static final String JSON_PATH_ROOTCERTS = "$..rootCerts";
public static final String JSON_PATH_TLS_ROOT_CERTS = "$..tls_root_certs";
public static final String JSON_PATH_CERTIFICATE = "$..certificate";

public static final String VALUE_TAG_CAPABILITIES = "Capabilities";
}

0 comments on commit 0019998

Please sign in to comment.