Skip to content

Commit

Permalink
[FAB-6840] Consolidate configtxapi to configtx
Browse files Browse the repository at this point in the history
Once upon a time, the configtx package did far too much.  It exposed the
channel configuration for each component, it handled transactional state
for the policy manager, and the other transaction components, and
generally became something that too many packages linked to.  Because of
all the different packages which linked to it, the interface definitions
were factored out into their own package configtxapi.

Since then, the vast majority of this function has been factored out
into other more appropriate places, and there are no longer dependency
cycles.

This CR moves the interface definitions back into the package, and
changes the rather generic 'configtx.Manager' name to
'configtx.Validator' (as its only purpose now is to validate configtx
updates before generating a new configuration).  This renaming ripples
throughout a fair bit of the code, but should be reasonably easy to
review.

Change-Id: Icd8494fc727a2474fc28f15945da7d0548f49f23
Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Nov 6, 2017
1 parent 5d410fe commit 5761146
Show file tree
Hide file tree
Showing 30 changed files with 192 additions and 289 deletions.
6 changes: 3 additions & 3 deletions common/channelconfig/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package channelconfig
import (
"time"

configtxapi "github.com/hyperledger/fabric/common/configtx/api"
"github.com/hyperledger/fabric/common/configtx"
"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/msp"
cb "github.com/hyperledger/fabric/protos/common"
Expand Down Expand Up @@ -139,8 +139,8 @@ type OrdererCapabilities interface {
// Depending on whether chain is used at the orderer or at the peer, other
// config resources may be available
type Resources interface {
// ConfigtxManager returns the configtx.Manager for the channel
ConfigtxManager() configtxapi.Manager
// ConfigtxValidator returns the configtx.Validator for the channel
ConfigtxValidator() configtx.Validator

// PolicyManager returns the policies.Manager for the channel
PolicyManager() policies.Manager
Expand Down
25 changes: 4 additions & 21 deletions common/channelconfig/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package channelconfig
import (
"github.com/hyperledger/fabric/common/cauthdsl"
"github.com/hyperledger/fabric/common/configtx"
configtxapi "github.com/hyperledger/fabric/common/configtx/api"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/msp"
Expand All @@ -34,7 +33,7 @@ type Bundle struct {
policyManager policies.Manager
mspManager msp.MSPManager
channelConfig *ChannelConfig
configtxManager configtxapi.Manager
configtxManager configtx.Validator
}

// PolicyManager returns the policy manager constructed for this config
Expand Down Expand Up @@ -73,8 +72,8 @@ func (b *Bundle) ApplicationConfig() (Application, bool) {
return result, result != nil
}

// ConfigtxManager returns the configtx.Manager for the channel
func (b *Bundle) ConfigtxManager() configtxapi.Manager {
// ConfigtxValidator returns the configtx.Validator for the channel
func (b *Bundle) ConfigtxValidator() configtx.Validator {
return b.configtxManager
}

Expand Down Expand Up @@ -149,20 +148,6 @@ func (b *Bundle) ValidateNew(nb Resources) error {
return nil
}

type simpleProposer struct {
policyManager policies.Manager
}

// RootGroupKey returns RootGroupKey constant
func (sp simpleProposer) RootGroupKey() string {
return RootGroupKey
}

// PolicyManager() returns the policy manager for considering config changes
func (sp simpleProposer) PolicyManager() policies.Manager {
return sp.policyManager
}

// NewBundleFromEnvelope wraps the NewBundle function, extracting the needed
// information from a full configtx
func NewBundleFromEnvelope(env *cb.Envelope) (*Bundle, error) {
Expand Down Expand Up @@ -217,9 +202,7 @@ func NewBundle(channelID string, config *cb.Config) (*Bundle, error) {
return nil, errors.Wrap(err, "initializing policymanager failed")
}

configtxManager, err := configtx.NewManagerImpl(channelID, config, simpleProposer{
policyManager: policyManager,
})
configtxManager, err := configtx.NewValidatorImpl(channelID, config, RootGroupKey, policyManager)
if err != nil {
return nil, errors.Wrap(err, "initializing configtx manager failed")
}
Expand Down
10 changes: 5 additions & 5 deletions common/channelconfig/bundlesource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package channelconfig
import (
"sync/atomic"

configtxapi "github.com/hyperledger/fabric/common/configtx/api"
"github.com/hyperledger/fabric/common/configtx"
"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/msp"
)
Expand Down Expand Up @@ -83,15 +83,15 @@ func (bs *BundleSource) ConsortiumsConfig() (Consortiums, bool) {
return bs.StableBundle().ConsortiumsConfig()
}

// ApplicationConfig returns the configtxapplication.SharedConfig for the channel
// ApplicationConfig returns the Application config for the channel
// and whether the Application config exists
func (bs *BundleSource) ApplicationConfig() (Application, bool) {
return bs.StableBundle().ApplicationConfig()
}

// ConfigtxManager returns the configtx.Manager for the channel
func (bs *BundleSource) ConfigtxManager() configtxapi.Manager {
return bs.StableBundle().ConfigtxManager()
// ConfigtxValidator returns the configtx.Validator for the channel
func (bs *BundleSource) ConfigtxValidator() configtx.Validator {
return bs.StableBundle().ConfigtxValidator()
}

// ValidateNew passes through to the current bundle
Expand Down
18 changes: 4 additions & 14 deletions common/configtx/api/api.go → common/configtx/configtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

package api
package configtx

import (
"github.com/hyperledger/fabric/common/policies"
cb "github.com/hyperledger/fabric/protos/common"
)

// Manager provides a mechanism to query and update config
type Manager interface {
// Validator provides a mechanism to propose config updates, see the config update results
// and validate the results of a config update.
type Validator interface {
// Validate attempts to apply a configtx to become the new config
Validate(configEnv *cb.ConfigEnvelope) error

Expand All @@ -28,13 +28,3 @@ type Manager interface {
// Sequence returns the current sequence number of the config
Sequence() uint64
}

// Proposer contains the references necessary to appropriately unmarshal
// a cb.ConfigGroup
type Proposer interface {
// RootGroupKey is the string to use to namespace the root group
RootGroupKey() string

// PolicyManager() returns the policy manager for considering config changes
PolicyManager() policies.Manager
}
34 changes: 18 additions & 16 deletions common/configtx/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"fmt"
"regexp"

"github.com/hyperledger/fabric/common/configtx/api"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/common/policies"
cb "github.com/hyperledger/fabric/protos/common"

"github.com/golang/protobuf/proto"
Expand All @@ -37,9 +37,10 @@ type configSet struct {
configProto *cb.Config
}

type configManager struct {
initializer api.Proposer
current *configSet
type ValidatorImpl struct {
namespace string
pm policies.Manager
current *configSet
}

// validateConfigID makes sure that the config element names (ie map key of
Expand Down Expand Up @@ -98,7 +99,7 @@ func validateChannelID(channelID string) error {
return nil
}

func NewManagerImpl(channelID string, config *cb.Config, initializer api.Proposer) (api.Manager, error) {
func NewValidatorImpl(channelID string, config *cb.Config, namespace string, pm policies.Manager) (*ValidatorImpl, error) {
if config == nil {
return nil, fmt.Errorf("Nil config envelope Config")
}
Expand All @@ -111,13 +112,14 @@ func NewManagerImpl(channelID string, config *cb.Config, initializer api.Propose
return nil, fmt.Errorf("Bad channel id: %s", err)
}

configMap, err := MapConfig(config.ChannelGroup, initializer.RootGroupKey())
configMap, err := MapConfig(config.ChannelGroup, namespace)
if err != nil {
return nil, fmt.Errorf("Error converting config to map: %s", err)
}

return &configManager{
initializer: initializer,
return &ValidatorImpl{
namespace: namespace,
pm: pm,
current: &configSet{
sequence: config.Sequence,
configMap: configMap,
Expand All @@ -129,11 +131,11 @@ func NewManagerImpl(channelID string, config *cb.Config, initializer api.Propose

// ProposeConfigUpdate takes in an Envelope of type CONFIG_UPDATE and produces a
// ConfigEnvelope to be used as the Envelope Payload Data of a CONFIG message
func (cm *configManager) ProposeConfigUpdate(configtx *cb.Envelope) (*cb.ConfigEnvelope, error) {
func (cm *ValidatorImpl) ProposeConfigUpdate(configtx *cb.Envelope) (*cb.ConfigEnvelope, error) {
return cm.proposeConfigUpdate(configtx)
}

func (cm *configManager) proposeConfigUpdate(configtx *cb.Envelope) (*cb.ConfigEnvelope, error) {
func (cm *ValidatorImpl) proposeConfigUpdate(configtx *cb.Envelope) (*cb.ConfigEnvelope, error) {
configUpdateEnv, err := envelopeToConfigUpdate(configtx)
if err != nil {
return nil, fmt.Errorf("Error converting envelope to config update: %s", err)
Expand All @@ -144,7 +146,7 @@ func (cm *configManager) proposeConfigUpdate(configtx *cb.Envelope) (*cb.ConfigE
return nil, fmt.Errorf("Error authorizing update: %s", err)
}

channelGroup, err := configMapToConfig(configMap, cm.initializer.RootGroupKey())
channelGroup, err := configMapToConfig(configMap, cm.namespace)
if err != nil {
return nil, fmt.Errorf("Could not turn configMap back to channelGroup: %s", err)
}
Expand All @@ -159,7 +161,7 @@ func (cm *configManager) proposeConfigUpdate(configtx *cb.Envelope) (*cb.ConfigE
}

// Validate simulates applying a ConfigEnvelope to become the new config
func (cm *configManager) Validate(configEnv *cb.ConfigEnvelope) error {
func (cm *ValidatorImpl) Validate(configEnv *cb.ConfigEnvelope) error {
if configEnv == nil {
return fmt.Errorf("config envelope is nil")
}
Expand All @@ -182,7 +184,7 @@ func (cm *configManager) Validate(configEnv *cb.ConfigEnvelope) error {
return err
}

channelGroup, err := configMapToConfig(configMap, cm.initializer.RootGroupKey())
channelGroup, err := configMapToConfig(configMap, cm.namespace)
if err != nil {
return fmt.Errorf("Could not turn configMap back to channelGroup: %s", err)
}
Expand All @@ -196,16 +198,16 @@ func (cm *configManager) Validate(configEnv *cb.ConfigEnvelope) error {
}

// ChainID retrieves the chain ID associated with this manager
func (cm *configManager) ChainID() string {
func (cm *ValidatorImpl) ChainID() string {
return cm.current.channelID
}

// Sequence returns the current sequence number of the config
func (cm *configManager) Sequence() uint64 {
func (cm *ValidatorImpl) Sequence() uint64 {
return cm.current.sequence
}

// ConfigEnvelope returns the current config envelope
func (cm *configManager) ConfigProto() *cb.Config {
func (cm *ValidatorImpl) ConfigProto() *cb.Config {
return cm.current.configProto
}
Loading

0 comments on commit 5761146

Please sign in to comment.