github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/msp/mgmt/mgmt.go (about)

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package mgmt
     8  
     9  import (
    10  	"sync"
    11  
    12  	"github.com/hechain20/hechain/bccsp"
    13  	"github.com/hechain20/hechain/common/flogging"
    14  	"github.com/hechain20/hechain/msp"
    15  	"github.com/hechain20/hechain/msp/cache"
    16  	"github.com/spf13/viper"
    17  )
    18  
    19  // FIXME: AS SOON AS THE CHAIN MANAGEMENT CODE IS COMPLETE,
    20  // THESE MAPS AND HELPER FUNCTIONS SHOULD DISAPPEAR BECAUSE
    21  // OWNERSHIP OF PER-CHAIN MSP MANAGERS WILL BE HANDLED BY IT;
    22  // HOWEVER IN THE INTERIM, THESE HELPER FUNCTIONS ARE REQUIRED
    23  
    24  var (
    25  	m         sync.Mutex
    26  	localMsp  msp.MSP
    27  	mspMap    = make(map[string]msp.MSPManager)
    28  	mspLogger = flogging.MustGetLogger("msp")
    29  )
    30  
    31  // TODO - this is a temporary solution to allow the peer to track whether the
    32  // MSPManager has been setup for a channel, which indicates whether the channel
    33  // exists or not
    34  type mspMgmtMgr struct {
    35  	msp.MSPManager
    36  }
    37  
    38  // GetManagerForChain returns the msp manager for the supplied
    39  // chain; if no such manager exists, one is created
    40  func GetManagerForChain(chainID string) msp.MSPManager {
    41  	m.Lock()
    42  	defer m.Unlock()
    43  
    44  	mspMgr, ok := mspMap[chainID]
    45  	if !ok {
    46  		mspLogger.Debugf("Created new msp manager for channel `%s`", chainID)
    47  		mspMgmtMgr := &mspMgmtMgr{msp.NewMSPManager()}
    48  		mspMap[chainID] = mspMgmtMgr
    49  		mspMgr = mspMgmtMgr
    50  	}
    51  	return mspMgr
    52  }
    53  
    54  // GetManagers returns all the managers registered
    55  func GetDeserializers() map[string]msp.IdentityDeserializer {
    56  	m.Lock()
    57  	defer m.Unlock()
    58  
    59  	clone := make(map[string]msp.IdentityDeserializer)
    60  
    61  	for key, mspManager := range mspMap {
    62  		clone[key] = mspManager
    63  	}
    64  
    65  	return clone
    66  }
    67  
    68  // XXXSetMSPManager is a stopgap solution to transition from the custom MSP config block
    69  // parsing to the channelconfig.Resources interface, while preserving the problematic singleton
    70  // nature of the MSP manager
    71  func XXXSetMSPManager(chainID string, manager msp.MSPManager) {
    72  	m.Lock()
    73  	defer m.Unlock()
    74  
    75  	mspMap[chainID] = &mspMgmtMgr{manager}
    76  }
    77  
    78  // GetLocalMSP returns the local msp (and creates it if it doesn't exist)
    79  func GetLocalMSP(cryptoProvider bccsp.BCCSP) msp.MSP {
    80  	m.Lock()
    81  	defer m.Unlock()
    82  
    83  	if localMsp != nil {
    84  		return localMsp
    85  	}
    86  
    87  	localMsp = loadLocalMSP(cryptoProvider)
    88  
    89  	return localMsp
    90  }
    91  
    92  func loadLocalMSP(bccsp bccsp.BCCSP) msp.MSP {
    93  	// determine the type of MSP (by default, we'll use bccspMSP)
    94  	mspType := viper.GetString("peer.localMspType")
    95  	if mspType == "" {
    96  		mspType = msp.ProviderTypeToString(msp.FABRIC)
    97  	}
    98  
    99  	newOpts, found := msp.Options[mspType]
   100  	if !found {
   101  		mspLogger.Panicf("msp type " + mspType + " unknown")
   102  	}
   103  
   104  	mspInst, err := msp.New(newOpts, bccsp)
   105  	if err != nil {
   106  		mspLogger.Fatalf("Failed to initialize local MSP, received err %+v", err)
   107  	}
   108  	switch mspType {
   109  	case msp.ProviderTypeToString(msp.FABRIC):
   110  		mspInst, err = cache.New(mspInst)
   111  		if err != nil {
   112  			mspLogger.Fatalf("Failed to initialize local MSP, received err %+v", err)
   113  		}
   114  	case msp.ProviderTypeToString(msp.IDEMIX):
   115  		// Do nothing
   116  	default:
   117  		panic("msp type " + mspType + " unknown")
   118  	}
   119  
   120  	mspLogger.Debugf("Created new local MSP")
   121  
   122  	return mspInst
   123  }
   124  
   125  // GetIdentityDeserializer returns the IdentityDeserializer for the given chain
   126  func GetIdentityDeserializer(chainID string, cryptoProvider bccsp.BCCSP) msp.IdentityDeserializer {
   127  	if chainID == "" {
   128  		return GetLocalMSP(cryptoProvider)
   129  	}
   130  
   131  	return GetManagerForChain(chainID)
   132  }