@@ -17,7 +17,6 @@ limitations under the License.
17
17
package configtx
18
18
19
19
import (
20
- "errors"
21
20
"fmt"
22
21
"regexp"
23
22
@@ -77,32 +76,6 @@ func computeSequence(configGroup *cb.ConfigGroup) uint64 {
77
76
return max
78
77
}
79
78
80
- // computeChannelIdAndSequence returns the chain id and the sequence number for a config envelope
81
- // or an error if there is a problem with the config envelope
82
- func computeChannelIdAndSequence (config * cb.ConfigUpdate ) (string , uint64 , error ) {
83
- if config .WriteSet == nil {
84
- return "" , 0 , errors .New ("Empty envelope unsupported" )
85
- }
86
-
87
- if config .Header == nil {
88
- return "" , 0 , fmt .Errorf ("Header not set" )
89
- }
90
-
91
- if config .Header .ChannelId == "" {
92
- return "" , 0 , fmt .Errorf ("Header chainID was not set" )
93
- }
94
-
95
- chainID := config .Header .ChannelId
96
-
97
- if err := validateChainID (chainID ); err != nil {
98
- return "" , 0 , err
99
- }
100
-
101
- m := computeSequence (config .WriteSet )
102
-
103
- return chainID , m , nil
104
- }
105
-
106
79
// validateChainID makes sure that proposed chain IDs (i.e. channel names)
107
80
// comply with the following restrictions:
108
81
// 1. Contain only ASCII alphanumerics, dots '.', dashes '-'
@@ -133,36 +106,43 @@ func validateChainID(chainID string) error {
133
106
return nil
134
107
}
135
108
136
- func NewManagerImpl (configtx * cb.ConfigEnvelope , initializer api.Initializer , callOnUpdate []func (api.Manager )) (api.Manager , error ) {
137
- // XXX as a temporary hack to get the new protos working, we assume entire config is always in the ConfigUpdate.WriteSet
109
+ func NewManagerImpl (configEnv * cb.ConfigEnvelope , initializer api.Initializer , callOnUpdate []func (api.Manager )) (api.Manager , error ) {
110
+ if configEnv == nil {
111
+ return nil , fmt .Errorf ("Nil config envelope" )
112
+ }
138
113
139
- if configtx . LastUpdate == nil {
140
- return nil , fmt .Errorf ("Must have ConfigEnvelope.LastUpdate set " )
114
+ if configEnv . Config == nil {
115
+ return nil , fmt .Errorf ("Nil config envelope Config " )
141
116
}
142
117
143
- config , err := UnmarshalConfigUpdate (configtx .LastUpdate .ConfigUpdate )
144
- if err != nil {
145
- return nil , err
118
+ if configEnv .Config .Header == nil {
119
+ return nil , fmt .Errorf ("Nil config envelop Config Header" )
146
120
}
147
121
148
- chainID , seq , err := computeChannelIdAndSequence (config )
122
+ if err := validateChainID (configEnv .Config .Header .ChannelId ); err != nil {
123
+ return nil , fmt .Errorf ("Bad channel id: %s" , err )
124
+ }
125
+
126
+ configMap , err := mapConfig (configEnv .Config .Channel )
149
127
if err != nil {
150
- return nil , fmt .Errorf ("Error computing chain ID and sequence : %s" , err )
128
+ return nil , fmt .Errorf ("Error converting config to map : %s" , err )
151
129
}
152
130
153
131
cm := & configManager {
154
132
Resources : initializer ,
155
133
initializer : initializer ,
156
- sequence : seq - 1 ,
157
- chainID : chainID ,
158
- config : make ( map [ string ] comparable ) ,
134
+ sequence : computeSequence ( configEnv . Config . Channel ) ,
135
+ chainID : configEnv . Config . Header . ChannelId ,
136
+ config : configMap ,
159
137
callOnUpdate : callOnUpdate ,
160
138
}
161
139
162
- err = cm .Apply (configtx .LastUpdate )
163
- if err != nil {
164
- return nil , fmt .Errorf ("Error applying config transaction: %s" , err )
140
+ cm .beginHandlers ()
141
+ if err := cm .proposeConfig (configMap ); err != nil {
142
+ cm .rollbackHandlers ()
143
+ return nil , err
165
144
}
145
+ cm .commitHandlers ()
166
146
167
147
return cm , nil
168
148
}
@@ -213,8 +193,6 @@ func (cm *configManager) proposeConfig(config map[string]comparable) error {
213
193
// authorizeUpdate validates that all modified config has the corresponding modification policies satisfied by the signature set
214
194
// it returns a map of the modified config
215
195
func (cm * configManager ) authorizeUpdate (configUpdateEnv * cb.ConfigUpdateEnvelope ) (map [string ]comparable , error ) {
216
- // XXX as a temporary hack to get the new protos working, we assume entire config is always in the ConfigUpdate.WriteSet
217
-
218
196
if configUpdateEnv == nil {
219
197
return nil , fmt .Errorf ("Cannot process nil ConfigUpdateEnvelope" )
220
198
}
@@ -224,7 +202,11 @@ func (cm *configManager) authorizeUpdate(configUpdateEnv *cb.ConfigUpdateEnvelop
224
202
return nil , err
225
203
}
226
204
227
- chainID , seq , err := computeChannelIdAndSequence (config )
205
+ if config .Header == nil {
206
+ return nil , fmt .Errorf ("Must have header set" )
207
+ }
208
+
209
+ seq := computeSequence (config .WriteSet )
228
210
if err != nil {
229
211
return nil , err
230
212
}
@@ -240,15 +222,8 @@ func (cm *configManager) authorizeUpdate(configUpdateEnv *cb.ConfigUpdateEnvelop
240
222
}
241
223
242
224
// Verify config is intended for this globally unique chain ID
243
- if chainID != cm .chainID {
244
- return nil , fmt .Errorf ("Config is for the wrong chain, expected %s, got %s" , cm .chainID , chainID )
245
- }
246
-
247
- defaultModificationPolicy , defaultPolicySet := cm .PolicyManager ().GetPolicy (NewConfigItemPolicyKey )
248
-
249
- // If the default modification policy is not set, it indicates this is an uninitialized chain, so be permissive of modification
250
- if ! defaultPolicySet {
251
- defaultModificationPolicy = & acceptAllPolicy {}
225
+ if config .Header .ChannelId != cm .chainID {
226
+ return nil , fmt .Errorf ("Config is for the wrong chain, expected %s, got %s" , cm .chainID , config .Header .ChannelId )
252
227
}
253
228
254
229
configMap , err := mapConfig (config .WriteSet )
@@ -277,25 +252,23 @@ func (cm *configManager) authorizeUpdate(configUpdateEnv *cb.ConfigUpdateEnvelop
277
252
278
253
// If a config item was modified, its Version must be set correctly, and it must satisfy the modification policy
279
254
if isModified {
280
- logger .Debugf ("Proposed config item %s on channel %s has been modified" , key , chainID )
255
+ logger .Debugf ("Proposed config item %s on channel %s has been modified" , key , cm . chainID )
281
256
282
257
if value .version () != seq {
283
258
return nil , fmt .Errorf ("Key %s was modified, but its Version %d does not equal current configtx Sequence %d" , key , value .version (), seq )
284
259
}
285
260
286
261
// Get the modification policy for this config item if one was previously specified
287
- // or the default if this is a new config item
262
+ // or accept it if it is new, as the group policy will be evaluated for its inclusion
288
263
var policy policies.Policy
289
264
if ok {
290
265
policy , _ = cm .PolicyManager ().GetPolicy (oldValue .modPolicy ())
291
- } else {
292
- policy = defaultModificationPolicy
266
+ // Ensure the policy is satisfied
267
+ if err = policy .Evaluate (signedData ); err != nil {
268
+ return nil , err
269
+ }
293
270
}
294
271
295
- // Ensure the policy is satisfied
296
- if err = policy .Evaluate (signedData ); err != nil {
297
- return nil , err
298
- }
299
272
}
300
273
}
301
274
0 commit comments