Skip to content

Commit

Permalink
Remove fields from ConfigurationEnvelope
Browse files Browse the repository at this point in the history
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 <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Nov 10, 2016
1 parent 492f2ad commit e3e51b4
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 234 deletions.
50 changes: 18 additions & 32 deletions bddtests/common/configuration_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions orderer/common/bootstrap/static/static.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ func (b *bootstrapper) GenesisBlock() (*cb.Block, error) {
lockdownDefaultModificationPolicy := b.makeSignedConfigurationItem(configtx.DefaultModificationPolicyID, cb.ConfigurationItem_Policy, sigPolicyToPolicy(cauthdsl.RejectAllPolicy), configtx.DefaultModificationPolicyID)

initialConfigTX := errorlessMarshal(&cb.ConfigurationEnvelope{
Sequence: 0,
ChainID: b.chainID,
Items: []*cb.SignedConfigurationItem{
lockdownDefaultModificationPolicy,
},
Expand Down
79 changes: 63 additions & 16 deletions orderer/common/configtx/configtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,47 @@ type configurationManager struct {
handlers map[cb.ConfigurationItem_ConfigurationType]Handler
}

// computeChainIDAndSequence returns the chain id and the sequence number for a configuration envelope
// or an error if there is a problem with the configuration envelope
func computeChainIDAndSequence(configtx *cb.ConfigurationEnvelope) ([]byte, uint64, error) {
if len(configtx.Items) == 0 {
return nil, 0, fmt.Errorf("Empty envelope unsupported")
}

m := uint64(0) //configtx.Items[0].LastModified
var chainID []byte //:= configtx.Items[0].Header.ChainID

for _, signedItem := range configtx.Items {
item := &cb.ConfigurationItem{}
err := proto.Unmarshal(signedItem.ConfigurationItem, item)
if err != nil {
return nil, 0, err
}

if item.LastModified > m {
m = item.LastModified
}

if item.Header == nil {
return nil, 0, fmt.Errorf("Header not set: %v", item)
}

if item.Header.ChainID == nil {
return nil, 0, fmt.Errorf("Header chainID was nil: %v", item)
}

if chainID == nil {
chainID = item.Header.ChainID
} else {
if !bytes.Equal(chainID, item.Header.ChainID) {
return nil, 0, fmt.Errorf("Mismatched chainIDs in envelope %x != %x", chainID, item.Header.ChainID)
}
}
}

return chainID, m, nil
}

// NewConfigurationManager creates a new Manager unless an error is encountered
func NewConfigurationManager(configtx *cb.ConfigurationEnvelope, pm policies.Manager, handlers map[cb.ConfigurationItem_ConfigurationType]Handler) (Manager, error) {
for ctype := range cb.ConfigurationItem_ConfigurationType_name {
Expand All @@ -75,15 +116,20 @@ func NewConfigurationManager(configtx *cb.ConfigurationEnvelope, pm policies.Man
}
}

chainID, seq, err := computeChainIDAndSequence(configtx)
if err != nil {
return nil, err
}

cm := &configurationManager{
sequence: configtx.Sequence - 1,
chainID: configtx.ChainID,
sequence: seq - 1,
chainID: chainID,
pm: pm,
handlers: handlers,
configuration: makeConfigMap(),
}

err := cm.Apply(configtx)
err = cm.Apply(configtx)

if err != nil {
return nil, err
Expand Down Expand Up @@ -119,14 +165,19 @@ func (cm *configurationManager) commitHandlers() {
}

func (cm *configurationManager) processConfig(configtx *cb.ConfigurationEnvelope) (configMap map[cb.ConfigurationItem_ConfigurationType]map[string]*cb.ConfigurationItem, err error) {
chainID, seq, err := computeChainIDAndSequence(configtx)
if err != nil {
return nil, err
}

// Verify config is a sequential update to prevent exhausting sequence numbers
if configtx.Sequence != cm.sequence+1 {
return nil, fmt.Errorf("Config sequence number jumped from %d to %d", cm.sequence, configtx.Sequence)
if seq != cm.sequence+1 {
return nil, fmt.Errorf("Config sequence number jumped from %d to %d", cm.sequence, seq)
}

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

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

// Ensure this configuration was intended for this chain
if !bytes.Equal(config.Header.ChainID, cm.chainID) {
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)
}

// Get the modification policy for this config item if one was previously specified
// or the default if this is a new config item
var policy policies.Policy
Expand Down Expand Up @@ -188,16 +235,16 @@ func (cm *configurationManager) processConfig(configtx *cb.ConfigurationEnvelope
// Config was modified if the LastModified or the Data contents changed
isModified = (val.LastModified != config.LastModified) || !bytes.Equal(config.Value, val.Value)
} else {
if config.LastModified != configtx.Sequence {
if config.LastModified != seq {
return nil, fmt.Errorf("Key %v for type %v was new, but had an older Sequence %d set", config.Key, config.Type, config.LastModified)
}
isModified = true
}

// If a config item was modified, its LastModified must be set correctly
if isModified {
if config.LastModified != configtx.Sequence {
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)
if config.LastModified != seq {
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)
}
}

Expand Down Expand Up @@ -244,7 +291,7 @@ func (cm *configurationManager) Apply(configtx *cb.ConfigurationEnvelope) error
return err
}
cm.configuration = configMap
cm.sequence = configtx.Sequence
cm.sequence++
cm.commitHandlers()
return nil
}

0 comments on commit e3e51b4

Please sign in to comment.