github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/common/configvalues/msp/config.go (about) 1 /* 2 Copyright IBM Corp. 2017 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package msp 18 19 import ( 20 "fmt" 21 "reflect" 22 23 "github.com/hyperledger/fabric/msp" 24 mspprotos "github.com/hyperledger/fabric/protos/msp" 25 ) 26 27 type pendingMSPConfig struct { 28 mspConfig *mspprotos.MSPConfig 29 msp msp.MSP 30 } 31 32 type mspConfigStore struct { 33 idMap map[string]*pendingMSPConfig 34 proposedMgr msp.MSPManager 35 } 36 37 // MSPConfigHandler 38 type MSPConfigHandler struct { 39 pendingConfig *mspConfigStore 40 msp.MSPManager 41 } 42 43 // BeginConfig called when a config proposal is begun 44 func (bh *MSPConfigHandler) BeginConfig() { 45 if bh.pendingConfig != nil { 46 panic("Programming error, called BeginValueProposals while a proposal was in process") 47 } 48 bh.pendingConfig = &mspConfigStore{ 49 idMap: make(map[string]*pendingMSPConfig), 50 } 51 } 52 53 // RollbackProposals called when a config proposal is abandoned 54 func (bh *MSPConfigHandler) RollbackProposals() { 55 bh.pendingConfig = nil 56 } 57 58 // CommitProposals called when a config proposal is committed 59 func (bh *MSPConfigHandler) CommitProposals() { 60 if bh.pendingConfig == nil { 61 panic("Programming error, called CommitProposals with no proposal in process") 62 } 63 64 bh.MSPManager = bh.pendingConfig.proposedMgr 65 bh.pendingConfig = nil 66 } 67 68 // ProposeValue called when config is added to a proposal 69 func (bh *MSPConfigHandler) ProposeMSP(mspConfig *mspprotos.MSPConfig) (msp.MSP, error) { 70 // check that the type for that MSP is supported 71 if mspConfig.Type != int32(msp.FABRIC) { 72 return nil, fmt.Errorf("Setup error: unsupported msp type %d", mspConfig.Type) 73 } 74 75 // create the msp instance 76 mspInst, err := msp.NewBccspMsp() 77 if err != nil { 78 return nil, fmt.Errorf("Creating the MSP manager failed, err %s", err) 79 } 80 81 // set it up 82 err = mspInst.Setup(mspConfig) 83 if err != nil { 84 return nil, fmt.Errorf("Setting up the MSP manager failed, err %s", err) 85 } 86 87 // add the MSP to the map of pending MSPs 88 mspID, err := mspInst.GetIdentifier() 89 if err != nil { 90 return nil, fmt.Errorf("Could not extract msp identifier, err %s", err) 91 } 92 93 existingPendingMSPConfig, ok := bh.pendingConfig.idMap[mspID] 94 if ok && !reflect.DeepEqual(existingPendingMSPConfig.mspConfig, mspConfig) { 95 return nil, fmt.Errorf("Attempted to define two different versions of MSP: %s", mspID) 96 } 97 98 bh.pendingConfig.idMap[mspID] = &pendingMSPConfig{ 99 mspConfig: mspConfig, 100 msp: mspInst, 101 } 102 103 return mspInst, nil 104 } 105 106 // PreCommit instantiates the MSP manager 107 func (bh *MSPConfigHandler) PreCommit() error { 108 if len(bh.pendingConfig.idMap) == 0 { 109 // Cannot instantiate an MSP manager with no MSPs 110 return nil 111 } 112 113 mspList := make([]msp.MSP, len(bh.pendingConfig.idMap)) 114 i := 0 115 for _, pendingMSP := range bh.pendingConfig.idMap { 116 mspList[i] = pendingMSP.msp 117 i++ 118 } 119 120 bh.pendingConfig.proposedMgr = msp.NewMSPManager() 121 err := bh.pendingConfig.proposedMgr.Setup(mspList) 122 return err 123 }