Skip to content

Commit fdd62b0

Browse files
Jason YellickSrinivasan Muralidharan
Jason Yellick
authored and
Srinivasan Muralidharan
committed
[FAB-2468] configtx ChannelHeader to ChannelId
https://jira.hyperledger.org/browse/FAB-2468 After an audit with Elli, it's been concluded that the ChannelHeader embedded in the configtx.proto Config and ConfigUpdate messages are unnecessary, that the only piece of information needed is the ChannelId. By removing these, the messages will be simpler to construct and understand. Change-Id: I45273ee71ad981fdfa2f76e7a1717b316a5e5584 Signed-off-by: Jason Yellick <[email protected]>
1 parent 29d7fc0 commit fdd62b0

File tree

15 files changed

+154
-195
lines changed

15 files changed

+154
-195
lines changed

common/configtx/manager.go

+19-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright IBM Corp. 2016 All Rights Reserved.
2+
Copyright IBM Corp. 2017 All Rights Reserved.
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/hyperledger/fabric/common/configtx/api"
2525
cb "github.com/hyperledger/fabric/protos/common"
26+
"github.com/hyperledger/fabric/protos/utils"
2627

2728
logging "github.com/op/go-logging"
2829
)
@@ -99,42 +100,40 @@ func validateChainID(chainID string) error {
99100
return nil
100101
}
101102

102-
func NewManagerImpl(configEnv *cb.ConfigEnvelope, initializer api.Initializer, callOnUpdate []func(api.Manager)) (api.Manager, error) {
103-
if configEnv == nil {
104-
return nil, fmt.Errorf("Nil config envelope")
105-
}
106-
107-
if configEnv.Config == nil {
108-
return nil, fmt.Errorf("Nil config envelope Config")
103+
func NewManagerImpl(envConfig *cb.Envelope, initializer api.Initializer, callOnUpdate []func(api.Manager)) (api.Manager, error) {
104+
if envConfig == nil {
105+
return nil, fmt.Errorf("Nil envelope")
109106
}
110107

111-
if configEnv.Config.Channel == nil {
112-
return nil, fmt.Errorf("Nil config envelope Config.Channel")
108+
configEnv := &cb.ConfigEnvelope{}
109+
header, err := utils.UnmarshalEnvelopeOfType(envConfig, cb.HeaderType_CONFIG, configEnv)
110+
if err != nil {
111+
return nil, fmt.Errorf("Bad envelope: %s", err)
113112
}
114113

115-
if configEnv.Config.Header == nil {
116-
return nil, fmt.Errorf("Nil config envelop Config Header")
114+
if configEnv.Config == nil {
115+
return nil, fmt.Errorf("Nil config envelope Config")
117116
}
118117

119-
if err := validateChainID(configEnv.Config.Header.ChannelId); err != nil {
118+
if err := validateChainID(header.ChannelId); err != nil {
120119
return nil, fmt.Errorf("Bad channel id: %s", err)
121120
}
122121

123-
configMap, err := mapConfig(configEnv.Config.Channel)
122+
configMap, err := mapConfig(configEnv.Config.ChannelGroup)
124123
if err != nil {
125124
return nil, fmt.Errorf("Error converting config to map: %s", err)
126125
}
127126

128127
cm := &configManager{
129128
Resources: initializer,
130129
initializer: initializer,
131-
sequence: computeSequence(configEnv.Config.Channel),
132-
chainID: configEnv.Config.Header.ChannelId,
130+
sequence: configEnv.Config.Sequence,
131+
chainID: header.ChannelId,
133132
config: configMap,
134133
callOnUpdate: callOnUpdate,
135134
}
136135

137-
result, err := cm.processConfig(configEnv.Config.Channel)
136+
result, err := cm.processConfig(configEnv.Config.ChannelGroup)
138137
if err != nil {
139138
return nil, err
140139
}
@@ -177,10 +176,8 @@ func (cm *configManager) ProposeConfigUpdate(configtx *cb.Envelope) (*cb.ConfigE
177176

178177
return &cb.ConfigEnvelope{
179178
Config: &cb.Config{
180-
Header: &cb.ChannelHeader{
181-
ChannelId: cm.chainID,
182-
},
183-
Channel: channelGroup,
179+
Sequence: cm.sequence,
180+
ChannelGroup: channelGroup,
184181
},
185182
LastUpdate: configtx,
186183
}, nil
@@ -210,7 +207,7 @@ func (cm *configManager) prepareApply(configEnv *cb.ConfigEnvelope) (map[string]
210207
return nil, nil, fmt.Errorf("Config cannot be nil")
211208
}
212209

213-
if !reflect.DeepEqual(channelGroup, configEnv.Config.Channel) {
210+
if !reflect.DeepEqual(channelGroup, configEnv.Config.ChannelGroup) {
214211
return nil, nil, fmt.Errorf("ConfigEnvelope LastUpdate did not produce the supplied config result")
215212
}
216213

common/configtx/manager_test.go

+31-22
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,28 @@ func makeConfigPair(id, modificationPolicy string, lastModified uint64, data []b
6161
}
6262
}
6363

64-
func makeConfigEnvelope(chainID string, configPairs ...*configPair) *cb.ConfigEnvelope {
64+
func makeEnvelopeConfig(channelID string, configPairs ...*configPair) *cb.Envelope {
6565
values := make(map[string]*cb.ConfigValue)
6666
for _, pair := range configPairs {
6767
values[pair.key] = pair.value
6868
}
6969

70-
return &cb.ConfigEnvelope{
71-
Config: &cb.Config{
72-
Header: &cb.ChannelHeader{ChannelId: chainID},
73-
Channel: &cb.ConfigGroup{
74-
Values: values,
70+
return &cb.Envelope{
71+
Payload: utils.MarshalOrPanic(&cb.Payload{
72+
Header: &cb.Header{
73+
ChannelHeader: utils.MarshalOrPanic(&cb.ChannelHeader{
74+
Type: int32(cb.HeaderType_CONFIG),
75+
ChannelId: channelID,
76+
}),
7577
},
76-
},
78+
Data: utils.MarshalOrPanic(&cb.ConfigEnvelope{
79+
Config: &cb.Config{
80+
ChannelGroup: &cb.ConfigGroup{
81+
Values: values,
82+
},
83+
},
84+
}),
85+
}),
7786
}
7887
}
7988

@@ -84,7 +93,7 @@ func makeConfigUpdateEnvelope(chainID string, configPairs ...*configPair) *cb.En
8493
}
8594

8695
config := &cb.ConfigUpdate{
87-
Header: &cb.ChannelHeader{ChannelId: chainID},
96+
ChannelId: chainID,
8897
WriteSet: &cb.ConfigGroup{
8998
Values: values,
9099
},
@@ -110,7 +119,7 @@ func TestCallback(t *testing.T) {
110119
}
111120

112121
cm, err := NewManagerImpl(
113-
makeConfigEnvelope(defaultChain, makeConfigPair("foo", "foo", 0, []byte("foo"))),
122+
makeEnvelopeConfig(defaultChain, makeConfigPair("foo", "foo", 0, []byte("foo"))),
114123
defaultInitializer(), []func(api.Manager){callback})
115124

116125
if err != nil {
@@ -125,7 +134,7 @@ func TestCallback(t *testing.T) {
125134
// TestDifferentChainID tests that a config update for a different chain ID fails
126135
func TestDifferentChainID(t *testing.T) {
127136
cm, err := NewManagerImpl(
128-
makeConfigEnvelope(defaultChain, makeConfigPair("foo", "foo", 0, []byte("foo"))),
137+
makeEnvelopeConfig(defaultChain, makeConfigPair("foo", "foo", 0, []byte("foo"))),
129138
defaultInitializer(), nil)
130139

131140
if err != nil {
@@ -143,7 +152,7 @@ func TestDifferentChainID(t *testing.T) {
143152
// TestOldConfigReplay tests that resubmitting a config for a sequence number which is not newer is ignored
144153
func TestOldConfigReplay(t *testing.T) {
145154
cm, err := NewManagerImpl(
146-
makeConfigEnvelope(defaultChain, makeConfigPair("foo", "foo", 0, []byte("foo"))),
155+
makeEnvelopeConfig(defaultChain, makeConfigPair("foo", "foo", 0, []byte("foo"))),
147156
defaultInitializer(), nil)
148157

149158
if err != nil {
@@ -161,7 +170,7 @@ func TestOldConfigReplay(t *testing.T) {
161170
// TestValidConfigChange tests the happy path of updating a config value with no defaultModificationPolicy
162171
func TestValidConfigChange(t *testing.T) {
163172
cm, err := NewManagerImpl(
164-
makeConfigEnvelope(defaultChain, makeConfigPair("foo", "foo", 0, []byte("foo"))),
173+
makeEnvelopeConfig(defaultChain, makeConfigPair("foo", "foo", 0, []byte("foo"))),
165174
defaultInitializer(), nil)
166175

167176
if err != nil {
@@ -190,7 +199,7 @@ func TestValidConfigChange(t *testing.T) {
190199
// config values while advancing another
191200
func TestConfigChangeRegressedSequence(t *testing.T) {
192201
cm, err := NewManagerImpl(
193-
makeConfigEnvelope(defaultChain, makeConfigPair("foo", "foo", 1, []byte("foo"))),
202+
makeEnvelopeConfig(defaultChain, makeConfigPair("foo", "foo", 1, []byte("foo"))),
194203
defaultInitializer(), nil)
195204

196205
if err != nil {
@@ -213,7 +222,7 @@ func TestConfigChangeRegressedSequence(t *testing.T) {
213222
// config values while advancing another
214223
func TestConfigChangeOldSequence(t *testing.T) {
215224
cm, err := NewManagerImpl(
216-
makeConfigEnvelope(defaultChain, makeConfigPair("foo", "foo", 1, []byte("foo"))),
225+
makeEnvelopeConfig(defaultChain, makeConfigPair("foo", "foo", 1, []byte("foo"))),
217226
defaultInitializer(), nil)
218227

219228
if err != nil {
@@ -236,7 +245,7 @@ func TestConfigChangeOldSequence(t *testing.T) {
236245
// by omitting them in the new config
237246
func TestConfigImplicitDelete(t *testing.T) {
238247
cm, err := NewManagerImpl(
239-
makeConfigEnvelope(
248+
makeEnvelopeConfig(
240249
defaultChain,
241250
makeConfigPair("foo", "foo", 0, []byte("foo")),
242251
makeConfigPair("bar", "bar", 0, []byte("bar")),
@@ -261,7 +270,7 @@ func TestConfigImplicitDelete(t *testing.T) {
261270
// TestEmptyConfigUpdate tests to make sure that an empty config is rejected as an update
262271
func TestEmptyConfigUpdate(t *testing.T) {
263272
cm, err := NewManagerImpl(
264-
makeConfigEnvelope(defaultChain, makeConfigPair("foo", "foo", 0, []byte("foo"))),
273+
makeEnvelopeConfig(defaultChain, makeConfigPair("foo", "foo", 0, []byte("foo"))),
265274
defaultInitializer(), nil)
266275

267276
if err != nil {
@@ -281,7 +290,7 @@ func TestEmptyConfigUpdate(t *testing.T) {
281290
// increasing the config item's LastModified
282291
func TestSilentConfigModification(t *testing.T) {
283292
cm, err := NewManagerImpl(
284-
makeConfigEnvelope(
293+
makeEnvelopeConfig(
285294
defaultChain,
286295
makeConfigPair("foo", "foo", 0, []byte("foo")),
287296
makeConfigPair("bar", "bar", 0, []byte("bar")),
@@ -309,7 +318,7 @@ func TestSilentConfigModification(t *testing.T) {
309318
func TestConfigChangeViolatesPolicy(t *testing.T) {
310319
initializer := defaultInitializer()
311320
cm, err := NewManagerImpl(
312-
makeConfigEnvelope(defaultChain, makeConfigPair("foo", "foo", 0, []byte("foo"))),
321+
makeEnvelopeConfig(defaultChain, makeConfigPair("foo", "foo", 0, []byte("foo"))),
313322
initializer, nil)
314323

315324
if err != nil {
@@ -331,7 +340,7 @@ func TestConfigChangeViolatesPolicy(t *testing.T) {
331340
func TestUnchangedConfigViolatesPolicy(t *testing.T) {
332341
initializer := defaultInitializer()
333342
cm, err := NewManagerImpl(
334-
makeConfigEnvelope(defaultChain, makeConfigPair("foo", "foo", 0, []byte("foo"))),
343+
makeEnvelopeConfig(defaultChain, makeConfigPair("foo", "foo", 0, []byte("foo"))),
335344
initializer, nil)
336345

337346
if err != nil {
@@ -369,7 +378,7 @@ func TestUnchangedConfigViolatesPolicy(t *testing.T) {
369378
func TestInvalidProposal(t *testing.T) {
370379
initializer := defaultInitializer()
371380
cm, err := NewManagerImpl(
372-
makeConfigEnvelope(defaultChain, makeConfigPair("foo", "foo", 0, []byte("foo"))),
381+
makeEnvelopeConfig(defaultChain, makeConfigPair("foo", "foo", 0, []byte("foo"))),
373382
initializer, nil)
374383

375384
if err != nil {
@@ -391,7 +400,7 @@ func TestMissingHeader(t *testing.T) {
391400
group := cb.NewConfigGroup()
392401
group.Values["foo"] = &cb.ConfigValue{}
393402
_, err := NewManagerImpl(
394-
&cb.ConfigEnvelope{Config: &cb.Config{Channel: group}},
403+
&cb.Envelope{Payload: utils.MarshalOrPanic(&cb.Payload{Data: utils.MarshalOrPanic(&cb.ConfigEnvelope{Config: &cb.Config{ChannelGroup: group}})})},
395404
defaultInitializer(), nil)
396405

397406
if err == nil {
@@ -402,7 +411,7 @@ func TestMissingHeader(t *testing.T) {
402411
// TestMissingChainID checks that a config item with a missing chainID causes the config to be rejected
403412
func TestMissingChainID(t *testing.T) {
404413
_, err := NewManagerImpl(
405-
makeConfigEnvelope("", makeConfigPair("foo", "foo", 0, []byte("foo"))),
414+
makeEnvelopeConfig("", makeConfigPair("foo", "foo", 0, []byte("foo"))),
406415
defaultInitializer(), nil)
407416

408417
if err == nil {

common/configtx/template.go

+4-10
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,8 @@ func NewSimpleTemplate(configGroups ...*cb.ConfigGroup) Template {
7070
// Envelope returns a ConfigUpdateEnvelope for the given chainID
7171
func (st *simpleTemplate) Envelope(chainID string) (*cb.ConfigUpdateEnvelope, error) {
7272
config, err := proto.Marshal(&cb.ConfigUpdate{
73-
Header: &cb.ChannelHeader{
74-
ChannelId: chainID,
75-
Type: int32(cb.HeaderType_CONFIG),
76-
},
77-
WriteSet: st.configGroup,
73+
ChannelId: chainID,
74+
WriteSet: st.configGroup,
7875
})
7976

8077
if err != nil {
@@ -146,11 +143,8 @@ func (ct *compositeTemplate) Envelope(chainID string) (*cb.ConfigUpdateEnvelope,
146143
}
147144

148145
marshaledConfig, err := proto.Marshal(&cb.ConfigUpdate{
149-
Header: &cb.ChannelHeader{
150-
ChannelId: chainID,
151-
Type: int32(cb.HeaderType_CONFIG),
152-
},
153-
WriteSet: channel,
146+
ChannelId: chainID,
147+
WriteSet: channel,
154148
})
155149
if err != nil {
156150
return nil, err

common/configtx/update.go

+3-8
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ func (cm *configManager) authorizeUpdate(configUpdateEnv *cb.ConfigUpdateEnvelop
3636
return nil, err
3737
}
3838

39-
if config.Header == nil {
40-
return nil, fmt.Errorf("Must have header set")
39+
if config.ChannelId != cm.chainID {
40+
return nil, fmt.Errorf("Update not for correct channel: %s for %s", config.ChannelId, cm.chainID)
4141
}
4242

4343
seq := computeSequence(config.WriteSet)
@@ -55,11 +55,6 @@ func (cm *configManager) authorizeUpdate(configUpdateEnv *cb.ConfigUpdateEnvelop
5555
return nil, fmt.Errorf("Config sequence number jumped from %d to %d", cm.sequence, seq)
5656
}
5757

58-
// Verify config is intended for this globally unique chain ID
59-
if config.Header.ChannelId != cm.chainID {
60-
return nil, fmt.Errorf("Config is for the wrong chain, expected %s, got %s", cm.chainID, config.Header.ChannelId)
61-
}
62-
6358
configMap, err := mapConfig(config.WriteSet)
6459
if err != nil {
6560
return nil, err
@@ -149,7 +144,7 @@ func envelopeToConfigUpdate(configtx *cb.Envelope) (*cb.ConfigUpdateEnvelope, er
149144
return nil, err
150145
}
151146

152-
if payload.Header == nil /* || payload.Header.ChannelHeader == nil */ {
147+
if payload.Header == nil {
153148
return nil, fmt.Errorf("Envelope must have a Header")
154149
}
155150

common/configtx/util.go

-22
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ limitations under the License.
1717
package configtx
1818

1919
import (
20-
"fmt"
21-
2220
cb "github.com/hyperledger/fabric/protos/common"
23-
"github.com/hyperledger/fabric/protos/utils"
2421

2522
"github.com/golang/protobuf/proto"
2623
)
@@ -100,22 +97,3 @@ func UnmarshalConfigEnvelopeOrPanic(data []byte) *cb.ConfigEnvelope {
10097
}
10198
return result
10299
}
103-
104-
// ConfigEnvelopeFromBlock extract the config envelope from a config block
105-
func ConfigEnvelopeFromBlock(block *cb.Block) (*cb.ConfigEnvelope, error) {
106-
if block.Data == nil || len(block.Data.Data) != 1 {
107-
return nil, fmt.Errorf("Not a config block, must contain exactly one tx")
108-
}
109-
110-
envelope, err := utils.UnmarshalEnvelope(block.Data.Data[0])
111-
if err != nil {
112-
return nil, err
113-
}
114-
115-
payload, err := utils.UnmarshalPayload(envelope.Payload)
116-
if err != nil {
117-
return nil, err
118-
}
119-
120-
return UnmarshalConfigEnvelope(payload.Data)
121-
}

common/genesis/genesis.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (f *factory) Block(chainID string) (*cb.Block, error) {
5858
payloadChannelHeader := utils.MakeChannelHeader(cb.HeaderType_CONFIG, msgVersion, chainID, epoch)
5959
payloadSignatureHeader := utils.MakeSignatureHeader(nil, utils.CreateNonceOrPanic())
6060
payloadHeader := utils.MakePayloadHeader(payloadChannelHeader, payloadSignatureHeader)
61-
payload := &cb.Payload{Header: payloadHeader, Data: utils.MarshalOrPanic(&cb.ConfigEnvelope{Config: &cb.Config{Header: configUpdate.Header, Channel: configUpdate.WriteSet}})}
61+
payload := &cb.Payload{Header: payloadHeader, Data: utils.MarshalOrPanic(&cb.ConfigEnvelope{Config: &cb.Config{ChannelGroup: configUpdate.WriteSet}})}
6262
envelope := &cb.Envelope{Payload: utils.MarshalOrPanic(payload), Signature: nil}
6363

6464
block := cb.NewBlock(0, nil)

core/peer/peer.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func getCurrConfigBlockFromLedger(ledger ledger.PeerLedger) (*common.Block, erro
164164
// createChain creates a new chain object and insert it into the chains
165165
func createChain(cid string, ledger ledger.PeerLedger, cb *common.Block) error {
166166

167-
configEnvelope, err := configtx.ConfigEnvelopeFromBlock(cb)
167+
envelopeConfig, err := utils.ExtractEnvelope(cb, 0)
168168
if err != nil {
169169
return err
170170
}
@@ -181,7 +181,7 @@ func createChain(cid string, ledger ledger.PeerLedger, cb *common.Block) error {
181181
}
182182

183183
configtxManager, err := configtx.NewManagerImpl(
184-
configEnvelope,
184+
envelopeConfig,
185185
configtxInitializer,
186186
[]func(cm configtxapi.Manager){gossipCallbackWrapper},
187187
)

orderer/configupdate/configupdate.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,7 @@ func createInitialConfig(envConfigUpdate *cb.Envelope) (*cb.ConfigEnvelope, erro
136136

137137
return &cb.ConfigEnvelope{
138138
Config: &cb.Config{
139-
Header: configUpdate.Header,
140-
Channel: configUpdate.WriteSet,
139+
ChannelGroup: configUpdate.WriteSet,
141140
},
142141

143142
LastUpdate: envConfigUpdate,

0 commit comments

Comments
 (0)