@@ -20,65 +20,101 @@ import (
20
20
"fmt"
21
21
"reflect"
22
22
23
- "github.com/golang/protobuf/proto"
24
23
"github.com/hyperledger/fabric/msp"
25
- "github.com/hyperledger/fabric/protos/common"
26
24
mspprotos "github.com/hyperledger/fabric/protos/msp"
27
25
)
28
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
+
29
37
// MSPConfigHandler
30
38
type MSPConfigHandler struct {
39
+ pendingConfig * mspConfigStore
31
40
msp.MSPManager
32
- config []* mspprotos.MSPConfig
33
- proposedMgr msp.MSPManager
34
41
}
35
42
36
43
// BeginConfig called when a config proposal is begun
37
44
func (bh * MSPConfigHandler ) BeginConfig () {
38
- if bh .config != nil || bh . proposedMgr != nil {
45
+ if bh .pendingConfig != nil {
39
46
panic ("Programming error, called BeginConfig while a proposal was in process" )
40
47
}
41
- bh .config = make ([]* mspprotos.MSPConfig , 0 )
48
+ bh .pendingConfig = & mspConfigStore {
49
+ idMap : make (map [string ]* pendingMSPConfig ),
50
+ }
42
51
}
43
52
44
53
// RollbackConfig called when a config proposal is abandoned
45
54
func (bh * MSPConfigHandler ) RollbackConfig () {
46
- bh .config = nil
47
- bh .proposedMgr = nil
55
+ bh .pendingConfig = nil
48
56
}
49
57
50
58
// CommitConfig called when a config proposal is committed
51
59
func (bh * MSPConfigHandler ) CommitConfig () {
52
- if bh .config == nil {
60
+ if bh .pendingConfig == nil {
53
61
panic ("Programming error, called CommitConfig with no proposal in process" )
54
62
}
55
63
56
- bh .MSPManager = bh .proposedMgr
57
- bh .config = nil
58
- bh .proposedMgr = nil
64
+ bh .MSPManager = bh .pendingConfig .proposedMgr
65
+ bh .pendingConfig = nil
59
66
}
60
67
61
68
// ProposeConfig called when config is added to a proposal
62
- func (bh * MSPConfigHandler ) ProposeConfig (key string , configValue * common.ConfigValue ) error {
63
- mspconfig := & mspprotos.MSPConfig {}
64
- err := proto .Unmarshal (configValue .Value , mspconfig )
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 ()
65
77
if err != nil {
66
- return fmt .Errorf ("Error unmarshalling msp config item , err %s" , err )
78
+ return nil , fmt .Errorf ("Creating the MSP manager failed , err %s" , err )
67
79
}
68
80
69
- // TODO handle deduplication more gracefully
70
- for _ , oMsp := range bh .config {
71
- if reflect .DeepEqual (oMsp , mspconfig ) {
72
- // The MSP is already in the list
73
- return nil
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
+ } else {
97
+ bh .pendingConfig .idMap [mspID ] = & pendingMSPConfig {
98
+ mspConfig : mspConfig ,
99
+ msp : mspInst ,
74
100
}
75
101
}
76
102
77
- bh .config = append (bh .config , []* mspprotos.MSPConfig {mspconfig }... )
103
+ mspList := make ([]msp.MSP , len (bh .pendingConfig .idMap ))
104
+ i := 0
105
+ for _ , pendingMSP := range bh .pendingConfig .idMap {
106
+ mspList [i ] = pendingMSP .msp
107
+ i ++
108
+ }
109
+
78
110
// the only way to make sure that I have a
79
111
// workable config is to toss the proposed
80
112
// manager, create a new one, call setup on
81
113
// it and return whatever error setup gives me
82
- bh .proposedMgr = msp .NewMSPManager ()
83
- return bh .proposedMgr .Setup (bh .config )
114
+ bh .pendingConfig .proposedMgr = msp .NewMSPManager ()
115
+ err = bh .pendingConfig .proposedMgr .Setup (mspList )
116
+ if err != nil {
117
+ return nil , err
118
+ }
119
+ return mspInst , nil
84
120
}
0 commit comments