github.com/ewagmig/fabric@v2.1.1+incompatible/common/channelconfig/msp.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package channelconfig
     8  
     9  import (
    10  	"fmt"
    11  
    12  	"github.com/golang/protobuf/proto"
    13  	mspprotos "github.com/hyperledger/fabric-protos-go/msp"
    14  	"github.com/hyperledger/fabric/bccsp"
    15  	"github.com/hyperledger/fabric/msp"
    16  	"github.com/hyperledger/fabric/msp/cache"
    17  	"github.com/pkg/errors"
    18  )
    19  
    20  type pendingMSPConfig struct {
    21  	mspConfig *mspprotos.MSPConfig
    22  	msp       msp.MSP
    23  }
    24  
    25  // MSPConfigHandler
    26  type MSPConfigHandler struct {
    27  	version msp.MSPVersion
    28  	idMap   map[string]*pendingMSPConfig
    29  	bccsp   bccsp.BCCSP
    30  }
    31  
    32  func NewMSPConfigHandler(mspVersion msp.MSPVersion, bccsp bccsp.BCCSP) *MSPConfigHandler {
    33  	return &MSPConfigHandler{
    34  		version: mspVersion,
    35  		idMap:   make(map[string]*pendingMSPConfig),
    36  		bccsp:   bccsp,
    37  	}
    38  }
    39  
    40  // ProposeMSP called when an org defines an MSP
    41  func (bh *MSPConfigHandler) ProposeMSP(mspConfig *mspprotos.MSPConfig) (msp.MSP, error) {
    42  	var theMsp msp.MSP
    43  	var err error
    44  
    45  	switch mspConfig.Type {
    46  	case int32(msp.FABRIC):
    47  		// create the bccsp msp instance
    48  		mspInst, err := msp.New(
    49  			&msp.BCCSPNewOpts{NewBaseOpts: msp.NewBaseOpts{Version: bh.version}},
    50  			bh.bccsp,
    51  		)
    52  		if err != nil {
    53  			return nil, errors.WithMessage(err, "creating the MSP manager failed")
    54  		}
    55  
    56  		// add a cache layer on top
    57  		theMsp, err = cache.New(mspInst)
    58  		if err != nil {
    59  			return nil, errors.WithMessage(err, "creating the MSP cache failed")
    60  		}
    61  	case int32(msp.IDEMIX):
    62  		// create the idemix msp instance
    63  		theMsp, err = msp.New(
    64  			&msp.IdemixNewOpts{NewBaseOpts: msp.NewBaseOpts{Version: bh.version}},
    65  			bh.bccsp,
    66  		)
    67  		if err != nil {
    68  			return nil, errors.WithMessage(err, "creating the MSP manager failed")
    69  		}
    70  	default:
    71  		return nil, errors.New(fmt.Sprintf("Setup error: unsupported msp type %d", mspConfig.Type))
    72  	}
    73  
    74  	// set it up
    75  	err = theMsp.Setup(mspConfig)
    76  	if err != nil {
    77  		return nil, errors.WithMessage(err, "setting up the MSP manager failed")
    78  	}
    79  
    80  	// add the MSP to the map of pending MSPs
    81  	mspID, _ := theMsp.GetIdentifier()
    82  
    83  	existingPendingMSPConfig, ok := bh.idMap[mspID]
    84  	if ok && !proto.Equal(existingPendingMSPConfig.mspConfig, mspConfig) {
    85  		return nil, errors.New(fmt.Sprintf("Attempted to define two different versions of MSP: %s", mspID))
    86  	}
    87  
    88  	if !ok {
    89  		bh.idMap[mspID] = &pendingMSPConfig{
    90  			mspConfig: mspConfig,
    91  			msp:       theMsp,
    92  		}
    93  	}
    94  
    95  	return theMsp, nil
    96  }
    97  
    98  func (bh *MSPConfigHandler) CreateMSPManager() (msp.MSPManager, error) {
    99  	mspList := make([]msp.MSP, len(bh.idMap))
   100  	i := 0
   101  	for _, pendingMSP := range bh.idMap {
   102  		mspList[i] = pendingMSP.msp
   103  		i++
   104  	}
   105  
   106  	manager := msp.NewMSPManager()
   107  	err := manager.Setup(mspList)
   108  	return manager, err
   109  }