@@ -19,6 +19,7 @@ package msp
19
19
import (
20
20
"fmt"
21
21
"reflect"
22
+ "sync"
22
23
23
24
"github.com/hyperledger/fabric/msp"
24
25
mspprotos "github.com/hyperledger/fabric/protos/msp"
@@ -36,37 +37,59 @@ type mspConfigStore struct {
36
37
37
38
// MSPConfigHandler
38
39
type MSPConfigHandler struct {
39
- pendingConfig * mspConfigStore
40
+ pendingConfig map [interface {}]* mspConfigStore
41
+ pendingLock sync.RWMutex
40
42
msp.MSPManager
41
43
}
42
44
45
+ func NewMSPConfigHandler () * MSPConfigHandler {
46
+ return & MSPConfigHandler {
47
+ pendingConfig : make (map [interface {}]* mspConfigStore ),
48
+ }
49
+ }
50
+
43
51
// 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" )
52
+ func (bh * MSPConfigHandler ) BeginConfig (tx interface {}) {
53
+ bh .pendingLock .Lock ()
54
+ defer bh .pendingLock .Unlock ()
55
+ _ , ok := bh .pendingConfig [tx ]
56
+ if ok {
57
+ panic ("Programming error, called BeginConfig mulitply for the same tx" )
47
58
}
48
- bh .pendingConfig = & mspConfigStore {
59
+ bh .pendingConfig [ tx ] = & mspConfigStore {
49
60
idMap : make (map [string ]* pendingMSPConfig ),
50
61
}
51
62
}
52
63
53
64
// RollbackProposals called when a config proposal is abandoned
54
- func (bh * MSPConfigHandler ) RollbackProposals () {
55
- bh .pendingConfig = nil
65
+ func (bh * MSPConfigHandler ) RollbackProposals (tx interface {}) {
66
+ bh .pendingLock .Lock ()
67
+ defer bh .pendingLock .Unlock ()
68
+ delete (bh .pendingConfig , tx )
56
69
}
57
70
58
71
// 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" )
72
+ func (bh * MSPConfigHandler ) CommitProposals (tx interface {}) {
73
+ bh .pendingLock .Lock ()
74
+ defer bh .pendingLock .Unlock ()
75
+ pendingConfig , ok := bh .pendingConfig [tx ]
76
+ if ! ok {
77
+ panic ("Programming error, called BeginConfig mulitply for the same tx" )
62
78
}
63
79
64
- bh .MSPManager = bh . pendingConfig .proposedMgr
65
- bh .pendingConfig = nil
80
+ bh .MSPManager = pendingConfig .proposedMgr
81
+ delete ( bh .pendingConfig , tx )
66
82
}
67
83
68
84
// ProposeValue called when config is added to a proposal
69
- func (bh * MSPConfigHandler ) ProposeMSP (mspConfig * mspprotos.MSPConfig ) (msp.MSP , error ) {
85
+ func (bh * MSPConfigHandler ) ProposeMSP (tx interface {}, mspConfig * mspprotos.MSPConfig ) (msp.MSP , error ) {
86
+ bh .pendingLock .RLock ()
87
+ pendingConfig , ok := bh .pendingConfig [tx ]
88
+ bh .pendingLock .RUnlock ()
89
+ if ! ok {
90
+ panic ("Programming error, called BeginConfig mulitply for the same tx" )
91
+ }
92
+
70
93
// check that the type for that MSP is supported
71
94
if mspConfig .Type != int32 (msp .FABRIC ) {
72
95
return nil , fmt .Errorf ("Setup error: unsupported msp type %d" , mspConfig .Type )
@@ -90,12 +113,12 @@ func (bh *MSPConfigHandler) ProposeMSP(mspConfig *mspprotos.MSPConfig) (msp.MSP,
90
113
return nil , fmt .Errorf ("Could not extract msp identifier, err %s" , err )
91
114
}
92
115
93
- existingPendingMSPConfig , ok := bh . pendingConfig .idMap [mspID ]
116
+ existingPendingMSPConfig , ok := pendingConfig .idMap [mspID ]
94
117
if ok && ! reflect .DeepEqual (existingPendingMSPConfig .mspConfig , mspConfig ) {
95
118
return nil , fmt .Errorf ("Attempted to define two different versions of MSP: %s" , mspID )
96
119
}
97
120
98
- bh . pendingConfig .idMap [mspID ] = & pendingMSPConfig {
121
+ pendingConfig .idMap [mspID ] = & pendingMSPConfig {
99
122
mspConfig : mspConfig ,
100
123
msp : mspInst ,
101
124
}
@@ -104,20 +127,27 @@ func (bh *MSPConfigHandler) ProposeMSP(mspConfig *mspprotos.MSPConfig) (msp.MSP,
104
127
}
105
128
106
129
// PreCommit instantiates the MSP manager
107
- func (bh * MSPConfigHandler ) PreCommit () error {
108
- if len (bh .pendingConfig .idMap ) == 0 {
130
+ func (bh * MSPConfigHandler ) PreCommit (tx interface {}) error {
131
+ bh .pendingLock .RLock ()
132
+ pendingConfig , ok := bh .pendingConfig [tx ]
133
+ bh .pendingLock .RUnlock ()
134
+ if ! ok {
135
+ panic ("Programming error, called PreCommit for tx which was not started" )
136
+ }
137
+
138
+ if len (pendingConfig .idMap ) == 0 {
109
139
// Cannot instantiate an MSP manager with no MSPs
110
140
return nil
111
141
}
112
142
113
- mspList := make ([]msp.MSP , len (bh . pendingConfig .idMap ))
143
+ mspList := make ([]msp.MSP , len (pendingConfig .idMap ))
114
144
i := 0
115
- for _ , pendingMSP := range bh . pendingConfig .idMap {
145
+ for _ , pendingMSP := range pendingConfig .idMap {
116
146
mspList [i ] = pendingMSP .msp
117
147
i ++
118
148
}
119
149
120
- bh . pendingConfig .proposedMgr = msp .NewMSPManager ()
121
- err := bh . pendingConfig .proposedMgr .Setup (mspList )
150
+ pendingConfig .proposedMgr = msp .NewMSPManager ()
151
+ err := pendingConfig .proposedMgr .Setup (mspList )
122
152
return err
123
153
}
0 commit comments