github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/hyperledger/fabric-config/configtx/organization.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  	"fmt"
    11  
    12  	"github.com/golang/protobuf/proto"
    13  	cb "github.com/hyperledger/fabric-protos-go/common"
    14  	mb "github.com/hyperledger/fabric-protos-go/msp"
    15  	pb "github.com/hyperledger/fabric-protos-go/peer"
    16  )
    17  
    18  // newOrgConfigGroup returns an config group for an organization.
    19  // It defines the crypto material for the organization (its MSP).
    20  // It sets the mod_policy of all elements to "Admins".
    21  func newOrgConfigGroup(org Organization) (*cb.ConfigGroup, error) {
    22  	orgGroup := newConfigGroup()
    23  	orgGroup.ModPolicy = AdminsPolicyKey
    24  
    25  	if org.ModPolicy != "" {
    26  		orgGroup.ModPolicy = org.ModPolicy
    27  	}
    28  
    29  	if err := setPolicies(orgGroup, org.Policies); err != nil {
    30  		return nil, err
    31  	}
    32  
    33  	fabricMSPConfig, err := org.MSP.toProto()
    34  	if err != nil {
    35  		return nil, fmt.Errorf("converting fabric msp config to proto: %v", err)
    36  	}
    37  
    38  	conf, err := proto.Marshal(fabricMSPConfig)
    39  	if err != nil {
    40  		return nil, fmt.Errorf("marshaling msp config: %v", err)
    41  	}
    42  
    43  	// mspConfig defaults type to FABRIC which implements an X.509 based provider
    44  	mspConfig := &mb.MSPConfig{
    45  		Config: conf,
    46  	}
    47  
    48  	err = setValue(orgGroup, mspValue(mspConfig), AdminsPolicyKey)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	return orgGroup, nil
    54  }
    55  
    56  func newOrdererOrgConfigGroup(org Organization) (*cb.ConfigGroup, error) {
    57  	orgGroup, err := newOrgConfigGroup(org)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	// OrdererEndpoints are orderer org specific and are only added when specified for orderer orgs
    63  	if len(org.OrdererEndpoints) > 0 {
    64  		err := setValue(orgGroup, endpointsValue(org.OrdererEndpoints), AdminsPolicyKey)
    65  		if err != nil {
    66  			return nil, err
    67  		}
    68  	}
    69  
    70  	return orgGroup, nil
    71  }
    72  
    73  func newApplicationOrgConfigGroup(org Organization) (*cb.ConfigGroup, error) {
    74  	orgGroup, err := newOrgConfigGroup(org)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  
    79  	// AnchorPeers are application org specific and are only added when specified for application orgs
    80  	anchorProtos := make([]*pb.AnchorPeer, len(org.AnchorPeers))
    81  	for i, anchorPeer := range org.AnchorPeers {
    82  		anchorProtos[i] = &pb.AnchorPeer{
    83  			Host: anchorPeer.Host,
    84  			Port: int32(anchorPeer.Port),
    85  		}
    86  	}
    87  
    88  	// Avoid adding an unnecessary anchor peers element when one is not required
    89  	// This helps prevent a delta from the orderer system channel when computing
    90  	// more complex channel creation transactions
    91  	if len(anchorProtos) > 0 {
    92  		err := setValue(orgGroup, anchorPeersValue(anchorProtos), AdminsPolicyKey)
    93  		if err != nil {
    94  			return nil, fmt.Errorf("failed to add anchor peers value: %v", err)
    95  		}
    96  	}
    97  
    98  	return orgGroup, nil
    99  }
   100  
   101  // getOrganization returns a basic Organization struct from org config group.
   102  func getOrganization(orgGroup *cb.ConfigGroup, orgName string) (Organization, error) {
   103  	policies, err := getPolicies(orgGroup.Policies)
   104  	if err != nil {
   105  		return Organization{}, err
   106  	}
   107  
   108  	msp, err := getMSPConfig(orgGroup)
   109  	if err != nil {
   110  		return Organization{}, err
   111  	}
   112  
   113  	var anchorPeers []Address
   114  	_, ok := orgGroup.Values[AnchorPeersKey]
   115  	if ok {
   116  		anchorProtos := &pb.AnchorPeers{}
   117  		err = unmarshalConfigValueAtKey(orgGroup, AnchorPeersKey, anchorProtos)
   118  		if err != nil {
   119  			return Organization{}, err
   120  		}
   121  
   122  		for _, anchorProto := range anchorProtos.AnchorPeers {
   123  			anchorPeers = append(anchorPeers, Address{
   124  				Host: anchorProto.Host,
   125  				Port: int(anchorProto.Port),
   126  			})
   127  		}
   128  	}
   129  
   130  	return Organization{
   131  		Name:        orgName,
   132  		Policies:    policies,
   133  		MSP:         msp,
   134  		AnchorPeers: anchorPeers,
   135  	}, nil
   136  }