Skip to content

Commit e3e51b4

Browse files
author
Jason Yellick
committed
Remove fields from ConfigurationEnvelope
The ConfigurationEnvelope used to contain a ChainID and a Sequence number. These fields were convenient, but ultimately inferrable from the configuration envelope contents. In an effort to make this message easier to understand and assemble, this changeset removes the sequence and chainID from the ConfigurationEnvelope. It also correspondingly fixes the configuration transaction processing code to examine the contents of the ConfigurationEnvelope to infer these values. Change-Id: I9336360071c78d163506491bccb8e51c87e8fce6 Signed-off-by: Jason Yellick <[email protected]>
1 parent 492f2ad commit e3e51b4

File tree

6 files changed

+207
-234
lines changed

6 files changed

+207
-234
lines changed

bddtests/common/configuration_pb2.py

+18-32
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

orderer/common/bootstrap/static/static.go

-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,6 @@ func (b *bootstrapper) GenesisBlock() (*cb.Block, error) {
8282
lockdownDefaultModificationPolicy := b.makeSignedConfigurationItem(configtx.DefaultModificationPolicyID, cb.ConfigurationItem_Policy, sigPolicyToPolicy(cauthdsl.RejectAllPolicy), configtx.DefaultModificationPolicyID)
8383

8484
initialConfigTX := errorlessMarshal(&cb.ConfigurationEnvelope{
85-
Sequence: 0,
86-
ChainID: b.chainID,
8785
Items: []*cb.SignedConfigurationItem{
8886
lockdownDefaultModificationPolicy,
8987
},

orderer/common/configtx/configtx.go

+63-16
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,47 @@ type configurationManager struct {
6767
handlers map[cb.ConfigurationItem_ConfigurationType]Handler
6868
}
6969

70+
// computeChainIDAndSequence returns the chain id and the sequence number for a configuration envelope
71+
// or an error if there is a problem with the configuration envelope
72+
func computeChainIDAndSequence(configtx *cb.ConfigurationEnvelope) ([]byte, uint64, error) {
73+
if len(configtx.Items) == 0 {
74+
return nil, 0, fmt.Errorf("Empty envelope unsupported")
75+
}
76+
77+
m := uint64(0) //configtx.Items[0].LastModified
78+
var chainID []byte //:= configtx.Items[0].Header.ChainID
79+
80+
for _, signedItem := range configtx.Items {
81+
item := &cb.ConfigurationItem{}
82+
err := proto.Unmarshal(signedItem.ConfigurationItem, item)
83+
if err != nil {
84+
return nil, 0, err
85+
}
86+
87+
if item.LastModified > m {
88+
m = item.LastModified
89+
}
90+
91+
if item.Header == nil {
92+
return nil, 0, fmt.Errorf("Header not set: %v", item)
93+
}
94+
95+
if item.Header.ChainID == nil {
96+
return nil, 0, fmt.Errorf("Header chainID was nil: %v", item)
97+
}
98+
99+
if chainID == nil {
100+
chainID = item.Header.ChainID
101+
} else {
102+
if !bytes.Equal(chainID, item.Header.ChainID) {
103+
return nil, 0, fmt.Errorf("Mismatched chainIDs in envelope %x != %x", chainID, item.Header.ChainID)
104+
}
105+
}
106+
}
107+
108+
return chainID, m, nil
109+
}
110+
70111
// NewConfigurationManager creates a new Manager unless an error is encountered
71112
func NewConfigurationManager(configtx *cb.ConfigurationEnvelope, pm policies.Manager, handlers map[cb.ConfigurationItem_ConfigurationType]Handler) (Manager, error) {
72113
for ctype := range cb.ConfigurationItem_ConfigurationType_name {
@@ -75,15 +116,20 @@ func NewConfigurationManager(configtx *cb.ConfigurationEnvelope, pm policies.Man
75116
}
76117
}
77118

119+
chainID, seq, err := computeChainIDAndSequence(configtx)
120+
if err != nil {
121+
return nil, err
122+
}
123+
78124
cm := &configurationManager{
79-
sequence: configtx.Sequence - 1,
80-
chainID: configtx.ChainID,
125+
sequence: seq - 1,
126+
chainID: chainID,
81127
pm: pm,
82128
handlers: handlers,
83129
configuration: makeConfigMap(),
84130
}
85131

86-
err := cm.Apply(configtx)
132+
err = cm.Apply(configtx)
87133

88134
if err != nil {
89135
return nil, err
@@ -119,14 +165,19 @@ func (cm *configurationManager) commitHandlers() {
119165
}
120166

121167
func (cm *configurationManager) processConfig(configtx *cb.ConfigurationEnvelope) (configMap map[cb.ConfigurationItem_ConfigurationType]map[string]*cb.ConfigurationItem, err error) {
168+
chainID, seq, err := computeChainIDAndSequence(configtx)
169+
if err != nil {
170+
return nil, err
171+
}
172+
122173
// Verify config is a sequential update to prevent exhausting sequence numbers
123-
if configtx.Sequence != cm.sequence+1 {
124-
return nil, fmt.Errorf("Config sequence number jumped from %d to %d", cm.sequence, configtx.Sequence)
174+
if seq != cm.sequence+1 {
175+
return nil, fmt.Errorf("Config sequence number jumped from %d to %d", cm.sequence, seq)
125176
}
126177

127178
// Verify config is intended for this globally unique chain ID
128-
if !bytes.Equal(configtx.ChainID, cm.chainID) {
129-
return nil, fmt.Errorf("Config is for the wrong chain, expected %x, got %x", cm.chainID, configtx.ChainID)
179+
if !bytes.Equal(chainID, cm.chainID) {
180+
return nil, fmt.Errorf("Config is for the wrong chain, expected %x, got %x", cm.chainID, chainID)
130181
}
131182

132183
defaultModificationPolicy, defaultPolicySet := cm.pm.GetPolicy(DefaultModificationPolicyID)
@@ -143,14 +194,10 @@ func (cm *configurationManager) processConfig(configtx *cb.ConfigurationEnvelope
143194
config := &cb.ConfigurationItem{}
144195
err = proto.Unmarshal(entry.ConfigurationItem, config)
145196
if err != nil {
197+
// Note that this is not reachable by test coverage because the unmarshal error would have already been found when computing the chainID and seqNo
146198
return nil, err
147199
}
148200

149-
// Ensure this configuration was intended for this chain
150-
if !bytes.Equal(config.Header.ChainID, cm.chainID) {
151-
return nil, fmt.Errorf("Config item %v for type %v was not meant for a different chain %x", config.Key, config.Type, config.Header.ChainID)
152-
}
153-
154201
// Get the modification policy for this config item if one was previously specified
155202
// or the default if this is a new config item
156203
var policy policies.Policy
@@ -188,16 +235,16 @@ func (cm *configurationManager) processConfig(configtx *cb.ConfigurationEnvelope
188235
// Config was modified if the LastModified or the Data contents changed
189236
isModified = (val.LastModified != config.LastModified) || !bytes.Equal(config.Value, val.Value)
190237
} else {
191-
if config.LastModified != configtx.Sequence {
238+
if config.LastModified != seq {
192239
return nil, fmt.Errorf("Key %v for type %v was new, but had an older Sequence %d set", config.Key, config.Type, config.LastModified)
193240
}
194241
isModified = true
195242
}
196243

197244
// If a config item was modified, its LastModified must be set correctly
198245
if isModified {
199-
if config.LastModified != configtx.Sequence {
200-
return nil, fmt.Errorf("Key %v for type %v was modified, but its LastModified %d does not equal current configtx Sequence %d", config.Key, config.Type, config.LastModified, configtx.Sequence)
246+
if config.LastModified != seq {
247+
return nil, fmt.Errorf("Key %v for type %v was modified, but its LastModified %d does not equal current configtx Sequence %d", config.Key, config.Type, config.LastModified, seq)
201248
}
202249
}
203250

@@ -244,7 +291,7 @@ func (cm *configurationManager) Apply(configtx *cb.ConfigurationEnvelope) error
244291
return err
245292
}
246293
cm.configuration = configMap
247-
cm.sequence = configtx.Sequence
294+
cm.sequence++
248295
cm.commitHandlers()
249296
return nil
250297
}

0 commit comments

Comments
 (0)