github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/hyperledger/fabric-config/configtx/channel.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package configtx
     8  
     9  import (
    10  	"errors"
    11  	"fmt"
    12  
    13  	cb "github.com/hyperledger/fabric-protos-go/common"
    14  )
    15  
    16  // ChannelGroup encapsulates the parts of the config that control channels.
    17  // This type implements retrieval of the various channel config values.
    18  type ChannelGroup struct {
    19  	channelGroup *cb.ConfigGroup
    20  }
    21  
    22  // Channel returns the channel group from the updated config.
    23  func (c *ConfigTx) Channel() *ChannelGroup {
    24  	return &ChannelGroup{channelGroup: c.updated.ChannelGroup}
    25  }
    26  
    27  // Configuration returns a channel configuration value from a config transaction.
    28  func (c *ChannelGroup) Configuration() (Channel, error) {
    29  	var (
    30  		config Channel
    31  		err    error
    32  	)
    33  
    34  	if _, ok := c.channelGroup.Values[ConsortiumKey]; ok {
    35  		consortiumProto := &cb.Consortium{}
    36  		err := unmarshalConfigValueAtKey(c.channelGroup, ConsortiumKey, consortiumProto)
    37  		if err != nil {
    38  			return Channel{}, err
    39  		}
    40  		config.Consortium = consortiumProto.Name
    41  	}
    42  
    43  	if applicationGroup, ok := c.channelGroup.Groups[ApplicationGroupKey]; ok {
    44  		a := &ApplicationGroup{applicationGroup: applicationGroup}
    45  		config.Application, err = a.Configuration()
    46  		if err != nil {
    47  			return Channel{}, err
    48  		}
    49  	}
    50  
    51  	if ordererGroup, ok := c.channelGroup.Groups[OrdererGroupKey]; ok {
    52  		o := &OrdererGroup{ordererGroup: ordererGroup, channelGroup: c.channelGroup}
    53  		config.Orderer, err = o.Configuration()
    54  		if err != nil {
    55  			return Channel{}, err
    56  		}
    57  	}
    58  
    59  	if consortiumsGroup, ok := c.channelGroup.Groups[ConsortiumsGroupKey]; ok {
    60  		c := &ConsortiumsGroup{consortiumsGroup: consortiumsGroup}
    61  		config.Consortiums, err = c.Configuration()
    62  		if err != nil {
    63  			return Channel{}, err
    64  		}
    65  	}
    66  
    67  	if _, ok := c.channelGroup.Values[CapabilitiesKey]; ok {
    68  		config.Capabilities, err = c.Capabilities()
    69  		if err != nil {
    70  			return Channel{}, err
    71  		}
    72  	}
    73  
    74  	config.Policies, err = c.Policies()
    75  	if err != nil {
    76  		return Channel{}, err
    77  	}
    78  
    79  	return config, nil
    80  }
    81  
    82  // Policies returns a map of policies for channel configuration.
    83  func (c *ChannelGroup) Policies() (map[string]Policy, error) {
    84  	return getPolicies(c.channelGroup.Policies)
    85  }
    86  
    87  // SetModPolicy sets the specified modification policy for the channel group.
    88  func (c *ChannelGroup) SetModPolicy(modPolicy string) error {
    89  	if modPolicy == "" {
    90  		return errors.New("non empty mod policy is required")
    91  	}
    92  
    93  	c.channelGroup.ModPolicy = modPolicy
    94  
    95  	return nil
    96  }
    97  
    98  // SetPolicy sets the specified policy in the channel group's config policy map.
    99  // If the policy already exists in current configuration, its value will be overwritten.
   100  func (c *ChannelGroup) SetPolicy(policyName string, policy Policy) error {
   101  	return setPolicy(c.channelGroup, policyName, policy)
   102  }
   103  
   104  // SetPolicies sets the specified policies in the channel group's config policy map.
   105  // If the policies already exist in current configuration, the values will be replaced with new policies.
   106  func (c *ChannelGroup) SetPolicies(policies map[string]Policy) error {
   107  	return setPolicies(c.channelGroup, policies)
   108  }
   109  
   110  // RemovePolicy removes an existing channel level policy.
   111  func (c *ChannelGroup) RemovePolicy(policyName string) error {
   112  	policies, err := c.Policies()
   113  	if err != nil {
   114  		return err
   115  	}
   116  
   117  	removePolicy(c.channelGroup, policyName, policies)
   118  	return nil
   119  }
   120  
   121  // Capabilities returns a map of enabled channel capabilities
   122  // from a config transaction's updated config.
   123  func (c *ChannelGroup) Capabilities() ([]string, error) {
   124  	capabilities, err := getCapabilities(c.channelGroup)
   125  	if err != nil {
   126  		return nil, fmt.Errorf("retrieving channel capabilities: %v", err)
   127  	}
   128  
   129  	return capabilities, nil
   130  }
   131  
   132  // AddCapability adds capability to the provided channel config.
   133  // If the provided capability already exists in current configuration, this action
   134  // will be a no-op.
   135  func (c *ChannelGroup) AddCapability(capability string) error {
   136  	capabilities, err := c.Capabilities()
   137  	if err != nil {
   138  		return err
   139  	}
   140  
   141  	err = addCapability(c.channelGroup, capabilities, AdminsPolicyKey, capability)
   142  	if err != nil {
   143  		return err
   144  	}
   145  
   146  	return nil
   147  }
   148  
   149  // RemoveCapability removes capability to the provided channel config.
   150  func (c *ChannelGroup) RemoveCapability(capability string) error {
   151  	capabilities, err := c.Capabilities()
   152  	if err != nil {
   153  		return err
   154  	}
   155  
   156  	err = removeCapability(c.channelGroup, capabilities, AdminsPolicyKey, capability)
   157  	if err != nil {
   158  		return err
   159  	}
   160  
   161  	return nil
   162  }
   163  
   164  // RemoveLegacyOrdererAddresses removes the deprecated top level orderer addresses config key and value
   165  // from the channel config.
   166  // In fabric 1.4, top level orderer addresses were migrated to the org level orderer endpoints
   167  // While top-level orderer addresses are still supported, the organization value is preferred.
   168  func (c *ChannelGroup) RemoveLegacyOrdererAddresses() {
   169  	delete(c.channelGroup.Values, OrdererAddressesKey)
   170  }