|
| 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 | + |
1 | 17 | package mspmgmt
|
2 | 18 |
|
3 | 19 | import (
|
| 20 | + "fmt" |
| 21 | + |
| 22 | + "github.com/golang/protobuf/proto" |
4 | 23 | "github.com/hyperledger/fabric/common/util"
|
5 | 24 | "github.com/hyperledger/fabric/msp"
|
6 | 25 | "github.com/hyperledger/fabric/protos/common"
|
@@ -60,3 +79,62 @@ func GetMSPManagerFromBlock(cid string, b *common.Block) (msp.MSPManager, error)
|
60 | 79 |
|
61 | 80 | return mgr, nil
|
62 | 81 | }
|
| 82 | + |
| 83 | +// MSPConfigHandler |
| 84 | +type MSPConfigHandler struct { |
| 85 | + config []*mspprotos.MSPConfig |
| 86 | + proposedMgr msp.MSPManager |
| 87 | + committedMgr msp.MSPManager |
| 88 | +} |
| 89 | + |
| 90 | +// BeginConfig called when a config proposal is begun |
| 91 | +func (bh *MSPConfigHandler) BeginConfig() { |
| 92 | + if bh.config != nil || bh.proposedMgr != nil { |
| 93 | + panic("Programming error, called BeginConfig while a proposal was in process") |
| 94 | + } |
| 95 | + bh.config = make([]*mspprotos.MSPConfig, 0) |
| 96 | +} |
| 97 | + |
| 98 | +// RollbackConfig called when a config proposal is abandoned |
| 99 | +func (bh *MSPConfigHandler) RollbackConfig() { |
| 100 | + bh.config = nil |
| 101 | + bh.proposedMgr = nil |
| 102 | +} |
| 103 | + |
| 104 | +// CommitConfig called when a config proposal is committed |
| 105 | +func (bh *MSPConfigHandler) CommitConfig() { |
| 106 | + if bh.config == nil { |
| 107 | + panic("Programming error, called CommitConfig with no proposal in process") |
| 108 | + } |
| 109 | + |
| 110 | + bh.committedMgr = bh.proposedMgr |
| 111 | + bh.config = nil |
| 112 | + bh.proposedMgr = nil |
| 113 | +} |
| 114 | + |
| 115 | +// ProposeConfig called when config is added to a proposal |
| 116 | +func (bh *MSPConfigHandler) ProposeConfig(configItem *common.ConfigurationItem) error { |
| 117 | + // we expect MSP type of config items |
| 118 | + if configItem.Key != msputils.MSPKey { |
| 119 | + return fmt.Errorf("Expected config item key %s, got %s", msputils.MSPKey, configItem.Key) |
| 120 | + } |
| 121 | + |
| 122 | + mspconfig := &mspprotos.MSPConfig{} |
| 123 | + err := proto.Unmarshal(configItem.Value, mspconfig) |
| 124 | + if err != nil { |
| 125 | + return fmt.Errorf("Error unmarshalling msp config item, err %s", err) |
| 126 | + } |
| 127 | + |
| 128 | + bh.config = append(bh.config, []*mspprotos.MSPConfig{mspconfig}...) |
| 129 | + // the only way to make sure that I have a |
| 130 | + // workable config is to toss the proposed |
| 131 | + // manager, create a new one, call setup on |
| 132 | + // it and return whatever error setup gives me |
| 133 | + bh.proposedMgr = msp.NewMSPManager() |
| 134 | + return bh.proposedMgr.Setup(bh.config) |
| 135 | +} |
| 136 | + |
| 137 | +// GetMSPManager returns the currently committed MSP manager |
| 138 | +func (bh *MSPConfigHandler) GetMSPManager() msp.MSPManager { |
| 139 | + return bh.committedMgr |
| 140 | +} |
0 commit comments