Skip to content

Commit 4887bf4

Browse files
author
Jason Yellick
committed
[FAB-2349] Change channel create to CONFIG_UPDATE
https://jira.hyperledger.org/browse/FAB-2349 Channel creation is currently being done via a CONFIG transaction but this needs to be modified to be a CONFIG_UPDATE transaction. This will enable future support of partial config channel creation. Change-Id: I7bc116120adf411f0020a827fcd6c7a36d0d8b66 Signed-off-by: Jason Yellick <[email protected]>
1 parent b78e929 commit 4887bf4

File tree

17 files changed

+252
-172
lines changed

17 files changed

+252
-172
lines changed

common/configtx/api/api.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@ type Manager interface {
2929
Resources
3030

3131
// Apply attempts to apply a configtx to become the new config
32-
Apply(configtx *cb.Envelope) error
32+
Apply(configEnv *cb.ConfigEnvelope) error
33+
34+
// Validate attempts to apply a configtx to become the new config
35+
Validate(configEnv *cb.ConfigEnvelope) error
3336

3437
// Validate attempts to validate a new configtx against the current config state
35-
Validate(configtx *cb.Envelope) error
38+
ProposeConfigUpdate(configtx *cb.Envelope) (*cb.ConfigEnvelope, error)
3639

3740
// ConfigEnvelope returns the *cb.ConfigEnvelope from the last successful Apply
3841
ConfigEnvelope() *cb.ConfigEnvelope

common/configtx/manager.go

+56-19
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package configtx
1818

1919
import (
2020
"fmt"
21+
"reflect"
2122
"regexp"
2223

2324
"github.com/hyperledger/fabric/common/configtx/api"
@@ -146,49 +147,92 @@ func (cm *configManager) commitCallbacks() {
146147
}
147148

148149
// Validate attempts to validate a new configtx against the current config state
149-
func (cm *configManager) Validate(configtx *cb.Envelope) error {
150+
// It requires an Envelope of type CONFIG_UPDATE
151+
func (cm *configManager) ProposeConfigUpdate(configtx *cb.Envelope) (*cb.ConfigEnvelope, error) {
150152
configUpdateEnv, err := envelopeToConfigUpdate(configtx)
151153
if err != nil {
152-
return err
154+
return nil, err
153155
}
154156

155157
configMap, err := cm.authorizeUpdate(configUpdateEnv)
156158
if err != nil {
157-
return err
159+
return nil, err
158160
}
159161

160162
channelGroup, err := configMapToConfig(configMap)
161163
if err != nil {
162-
return fmt.Errorf("Could not turn configMap back to channelGroup: %s", err)
164+
return nil, fmt.Errorf("Could not turn configMap back to channelGroup: %s", err)
163165
}
164166

165167
result, err := cm.processConfig(channelGroup)
166168
if err != nil {
167-
return err
169+
return nil, err
168170
}
169171

170172
result.rollback()
171-
return nil
173+
174+
return &cb.ConfigEnvelope{
175+
Config: &cb.Config{
176+
Header: &cb.ChannelHeader{
177+
ChannelId: cm.chainID,
178+
},
179+
Channel: channelGroup,
180+
},
181+
LastUpdate: configtx,
182+
}, nil
172183
}
173184

174-
// Apply attempts to apply a configtx to become the new config
175-
func (cm *configManager) Apply(configtx *cb.Envelope) error {
176-
configUpdateEnv, err := envelopeToConfigUpdate(configtx)
185+
func (cm *configManager) prepareApply(configEnv *cb.ConfigEnvelope) (map[string]comparable, *configResult, error) {
186+
if configEnv == nil {
187+
return nil, nil, fmt.Errorf("Attempted to apply config with nil envelope")
188+
}
189+
190+
configUpdateEnv, err := envelopeToConfigUpdate(configEnv.LastUpdate)
177191
if err != nil {
178-
return err
192+
return nil, nil, err
179193
}
180194

181195
configMap, err := cm.authorizeUpdate(configUpdateEnv)
182196
if err != nil {
183-
return err
197+
return nil, nil, err
184198
}
185199

186200
channelGroup, err := configMapToConfig(configMap)
187201
if err != nil {
188-
return fmt.Errorf("Could not turn configMap back to channelGroup: %s", err)
202+
return nil, nil, fmt.Errorf("Could not turn configMap back to channelGroup: %s", err)
203+
}
204+
205+
if configEnv.Config == nil {
206+
return nil, nil, fmt.Errorf("Config cannot be nil")
207+
}
208+
209+
if !reflect.DeepEqual(channelGroup, configEnv.Config.Channel) {
210+
return nil, nil, fmt.Errorf("ConfigEnvelope LastUpdate did not produce the supplied config result")
189211
}
190212

191213
result, err := cm.processConfig(channelGroup)
214+
if err != nil {
215+
return nil, nil, err
216+
}
217+
218+
return configMap, result, nil
219+
}
220+
221+
// Validate simulates applying a ConfigEnvelope to become the new config
222+
func (cm *configManager) Validate(configEnv *cb.ConfigEnvelope) error {
223+
_, result, err := cm.prepareApply(configEnv)
224+
if err != nil {
225+
return err
226+
}
227+
228+
result.rollback()
229+
230+
return nil
231+
}
232+
233+
// Apply attempts to apply a ConfigEnvelope to become the new config
234+
func (cm *configManager) Apply(configEnv *cb.ConfigEnvelope) error {
235+
configMap, result, err := cm.prepareApply(configEnv)
192236
if err != nil {
193237
return err
194238
}
@@ -199,13 +243,6 @@ func (cm *configManager) Apply(configtx *cb.Envelope) error {
199243
cm.config = configMap
200244
cm.sequence++
201245

202-
cm.configEnv = &cb.ConfigEnvelope{
203-
Config: &cb.Config{
204-
// XXX add header
205-
Channel: channelGroup,
206-
},
207-
LastUpdate: configtx,
208-
}
209246
return nil
210247
}
211248

common/configtx/manager_test.go

+32-67
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,9 @@ func TestDifferentChainID(t *testing.T) {
134134

135135
newConfig := makeConfigUpdateEnvelope("wrongChain", makeConfigPair("foo", "foo", 1, []byte("foo")))
136136

137-
err = cm.Validate(newConfig)
137+
_, err = cm.ProposeConfigUpdate(newConfig)
138138
if err == nil {
139-
t.Error("Should have errored when validating a new config set the wrong chain ID")
140-
}
141-
142-
err = cm.Apply(newConfig)
143-
if err == nil {
144-
t.Error("Should have errored when applying a new config with the wrong chain ID")
139+
t.Error("Should have errored when proposing a new config set the wrong chain ID")
145140
}
146141
}
147142

@@ -157,14 +152,9 @@ func TestOldConfigReplay(t *testing.T) {
157152

158153
newConfig := makeConfigUpdateEnvelope(defaultChain, makeConfigPair("foo", "foo", 0, []byte("foo")))
159154

160-
err = cm.Validate(newConfig)
155+
_, err = cm.ProposeConfigUpdate(newConfig)
161156
if err == nil {
162-
t.Error("Should have errored when validating a config that is not a newer sequence number")
163-
}
164-
165-
err = cm.Apply(newConfig)
166-
if err == nil {
167-
t.Error("Should have errored when applying a config that is not a newer sequence number")
157+
t.Error("Should have errored when proposing a config that is not a newer sequence number")
168158
}
169159
}
170160

@@ -180,12 +170,17 @@ func TestValidConfigChange(t *testing.T) {
180170

181171
newConfig := makeConfigUpdateEnvelope(defaultChain, makeConfigPair("foo", "foo", 1, []byte("foo")))
182172

183-
err = cm.Validate(newConfig)
173+
configEnv, err := cm.ProposeConfigUpdate(newConfig)
174+
if err != nil {
175+
t.Errorf("Should not have errored proposing config: %s", err)
176+
}
177+
178+
err = cm.Validate(configEnv)
184179
if err != nil {
185180
t.Errorf("Should not have errored validating config: %s", err)
186181
}
187182

188-
err = cm.Apply(newConfig)
183+
err = cm.Apply(configEnv)
189184
if err != nil {
190185
t.Errorf("Should not have errored applying config: %s", err)
191186
}
@@ -208,14 +203,9 @@ func TestConfigChangeRegressedSequence(t *testing.T) {
208203
makeConfigPair("bar", "bar", 2, []byte("bar")),
209204
)
210205

211-
err = cm.Validate(newConfig)
206+
_, err = cm.ProposeConfigUpdate(newConfig)
212207
if err == nil {
213-
t.Error("Should have errored validating config because foo's sequence number regressed")
214-
}
215-
216-
err = cm.Apply(newConfig)
217-
if err == nil {
218-
t.Error("Should have errored applying config because foo's sequence number regressed")
208+
t.Error("Should have errored proposing config because foo's sequence number regressed")
219209
}
220210
}
221211

@@ -236,14 +226,9 @@ func TestConfigChangeOldSequence(t *testing.T) {
236226
makeConfigPair("bar", "bar", 1, []byte("bar")),
237227
)
238228

239-
err = cm.Validate(newConfig)
240-
if err == nil {
241-
t.Error("Should have errored validating config because bar was new but its sequence number was old")
242-
}
243-
244-
err = cm.Apply(newConfig)
229+
_, err = cm.ProposeConfigUpdate(newConfig)
245230
if err == nil {
246-
t.Error("Should have errored applying config because bar was new but its sequence number was old")
231+
t.Error("Should have errored proposing config because bar was new but its sequence number was old")
247232
}
248233
}
249234

@@ -267,14 +252,9 @@ func TestConfigImplicitDelete(t *testing.T) {
267252
makeConfigPair("bar", "bar", 1, []byte("bar")),
268253
)
269254

270-
err = cm.Validate(newConfig)
255+
_, err = cm.ProposeConfigUpdate(newConfig)
271256
if err == nil {
272-
t.Error("Should have errored validating config because foo was implicitly deleted")
273-
}
274-
275-
err = cm.Apply(newConfig)
276-
if err == nil {
277-
t.Error("Should have errored applying config because foo was implicitly deleted")
257+
t.Error("Should have errored proposing config because foo was implicitly deleted")
278258
}
279259
}
280260

@@ -290,14 +270,9 @@ func TestEmptyConfigUpdate(t *testing.T) {
290270

291271
newConfig := &cb.Envelope{}
292272

293-
err = cm.Validate(newConfig)
294-
if err == nil {
295-
t.Error("Should not errored validating config because new config is empty")
296-
}
297-
298-
err = cm.Apply(newConfig)
273+
_, err = cm.ProposeConfigUpdate(newConfig)
299274
if err == nil {
300-
t.Error("Should not errored applying config because new config is empty")
275+
t.Error("Should not errored proposing config because new config is empty")
301276
}
302277
}
303278

@@ -323,14 +298,9 @@ func TestSilentConfigModification(t *testing.T) {
323298
makeConfigPair("bar", "bar", 1, []byte("bar")),
324299
)
325300

326-
err = cm.Validate(newConfig)
301+
_, err = cm.ProposeConfigUpdate(newConfig)
327302
if err == nil {
328-
t.Error("Should not errored validating config because foo was silently modified (despite modification allowed by policy)")
329-
}
330-
331-
err = cm.Apply(newConfig)
332-
if err == nil {
333-
t.Error("Should not errored applying config because foo was silently modified (despite modification allowed by policy)")
303+
t.Error("Should have errored proposing config because foo was silently modified (despite modification allowed by policy)")
334304
}
335305
}
336306

@@ -350,14 +320,9 @@ func TestConfigChangeViolatesPolicy(t *testing.T) {
350320

351321
newConfig := makeConfigUpdateEnvelope(defaultChain, makeConfigPair("foo", "foo", 1, []byte("foo")))
352322

353-
err = cm.Validate(newConfig)
354-
if err == nil {
355-
t.Error("Should have errored validating config because policy rejected modification")
356-
}
357-
358-
err = cm.Apply(newConfig)
323+
_, err = cm.ProposeConfigUpdate(newConfig)
359324
if err == nil {
360-
t.Error("Should have errored applying config because policy rejected modification")
325+
t.Error("Should have errored proposing config because policy rejected modification")
361326
}
362327
}
363328

@@ -383,12 +348,17 @@ func TestUnchangedConfigViolatesPolicy(t *testing.T) {
383348
makeConfigPair("bar", "bar", 1, []byte("foo")),
384349
)
385350

386-
err = cm.Validate(newConfig)
351+
configEnv, err := cm.ProposeConfigUpdate(newConfig)
352+
if err != nil {
353+
t.Errorf("Should not have errored proposing config, but got %s", err)
354+
}
355+
356+
err = cm.Validate(configEnv)
387357
if err != nil {
388358
t.Errorf("Should not have errored validating config, but got %s", err)
389359
}
390360

391-
err = cm.Apply(newConfig)
361+
err = cm.Apply(configEnv)
392362
if err != nil {
393363
t.Errorf("Should not have errored applying config, but got %s", err)
394364
}
@@ -410,14 +380,9 @@ func TestInvalidProposal(t *testing.T) {
410380

411381
newConfig := makeConfigUpdateEnvelope(defaultChain, makeConfigPair("foo", "foo", 1, []byte("foo")))
412382

413-
err = cm.Validate(newConfig)
414-
if err == nil {
415-
t.Error("Should have errored validating config because the handler rejected it")
416-
}
417-
418-
err = cm.Apply(newConfig)
383+
_, err = cm.ProposeConfigUpdate(newConfig)
419384
if err == nil {
420-
t.Error("Should have errored applying config because the handler rejected it")
385+
t.Error("Should have errored proposing config because the handler rejected it")
421386
}
422387
}
423388

common/configtx/template.go

+2-26
Original file line numberDiff line numberDiff line change
@@ -195,34 +195,10 @@ func MakeChainCreationTransaction(creationPolicy string, chainID string, signer
195195
return nil, err
196196
}
197197

198-
configUpdate, err := UnmarshalConfigUpdate(newConfigUpdateEnv.ConfigUpdate)
199-
if err != nil {
200-
return nil, err
201-
}
202-
203-
newConfigEnv := &cb.ConfigEnvelope{
204-
// Config is an XXX temporary workaround until the orderer generates the real configtx from the WriteSet
205-
Config: &cb.Config{
206-
Header: configUpdate.Header,
207-
Channel: configUpdate.WriteSet,
208-
},
209-
LastUpdate: &cb.Envelope{
210-
Payload: utils.MarshalOrPanic(&cb.Payload{
211-
Header: &cb.Header{
212-
ChannelHeader: &cb.ChannelHeader{
213-
ChannelId: chainID,
214-
Type: int32(cb.HeaderType_CONFIG_UPDATE),
215-
},
216-
},
217-
Data: utils.MarshalOrPanic(newConfigUpdateEnv),
218-
}),
219-
},
220-
}
221-
222-
payloadChannelHeader := utils.MakeChannelHeader(cb.HeaderType_CONFIG, msgVersion, chainID, epoch)
198+
payloadChannelHeader := utils.MakeChannelHeader(cb.HeaderType_CONFIG_UPDATE, msgVersion, chainID, epoch)
223199
payloadSignatureHeader := utils.MakeSignatureHeader(sSigner, utils.CreateNonceOrPanic())
224200
payloadHeader := utils.MakePayloadHeader(payloadChannelHeader, payloadSignatureHeader)
225-
payload := &cb.Payload{Header: payloadHeader, Data: utils.MarshalOrPanic(newConfigEnv)}
201+
payload := &cb.Payload{Header: payloadHeader, Data: utils.MarshalOrPanic(newConfigUpdateEnv)}
226202
paylBytes := utils.MarshalOrPanic(payload)
227203

228204
// sign the payload

0 commit comments

Comments
 (0)