Skip to content

Commit

Permalink
[FAB-2312] configtx value handlers to own package
Browse files Browse the repository at this point in the history
https://jira.hyperledger.org/browse/FAB-2312

There used to be a single type for configuration, 'ConfigItem', but that
has bifurcated into 'ConfigValue', 'ConfigGroup', and 'ConfigPolicy'.
In order to handle the 'ConfigValue' and the 'ConfigPolicy' in similar
ways, the 'ConfigValue' handling needs to be pushed out into its own
package just like policy handling is.  This CR accomplishes that.

This is in preparation for treating the PolicyHandler as more of a first
class object and to eliminate the need for hacky importing.

Change-Id: I690a33e09843eb4c29611319f2e3942baae2dfdb
Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Feb 17, 2017
1 parent 3b9cc55 commit fee7c6c
Show file tree
Hide file tree
Showing 48 changed files with 419 additions and 380 deletions.
2 changes: 1 addition & 1 deletion common/cauthdsl/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func addPolicy(manager *policies.ManagerImpl, id string, policy *cb.Policy) {
if err != nil {
panic(err)
}
manager.CommitConfig()
manager.CommitProposals()
}

func providerMap() map[int32]policies.Provider {
Expand Down
100 changes: 15 additions & 85 deletions common/configtx/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,87 +17,12 @@ limitations under the License.
package api

import (
"time"

configvalues "github.com/hyperledger/fabric/common/configvalues/api"
"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/msp"
cb "github.com/hyperledger/fabric/protos/common"
ab "github.com/hyperledger/fabric/protos/orderer"
pb "github.com/hyperledger/fabric/protos/peer"
)

// ChannelConfig stores the common channel config
type ChannelConfig interface {
// HashingAlgorithm returns the default algorithm to be used when hashing
// such as computing block hashes, and CreationPolicy digests
HashingAlgorithm() func(input []byte) []byte

// BlockDataHashingStructureWidth returns the width to use when constructing the
// Merkle tree to compute the BlockData hash
BlockDataHashingStructureWidth() uint32

// OrdererAddresses returns the list of valid orderer addresses to connect to to invoke Broadcast/Deliver
OrdererAddresses() []string
}

type OrgConfig interface {
// Name returns the name this org is referred to in config
Name() string

// MSPID returns the MSP ID associated with this org
MSPID() string
}

// ApplicationOrgConfig stores the per org application config
type ApplicationOrgConfig interface {
OrgConfig

// AnchorPeers returns the list of gossip anchor peers
AnchorPeers() []*pb.AnchorPeer
}

// ApplicationConfig stores the common shared application config
type ApplicationConfig interface {
// Organizations returns a map of org ID to ApplicationOrgConfig
Organizations() map[string]ApplicationOrgConfig
}

// OrdererConfig stores the common shared orderer config
type OrdererConfig interface {
// ConsensusType returns the configured consensus type
ConsensusType() string

// BatchSize returns the maximum number of messages to include in a block
BatchSize() *ab.BatchSize

// BatchTimeout returns the amount of time to wait before creating a batch
BatchTimeout() time.Duration

// ChainCreationPolicyNames returns the policy names which are allowed for chain creation
// This field is only set for the system ordering chain
ChainCreationPolicyNames() []string

// KafkaBrokers returns the addresses (IP:port notation) of a set of "bootstrap"
// Kafka brokers, i.e. this is not necessarily the entire set of Kafka brokers
// used for ordering
KafkaBrokers() []string

// IngressPolicyNames returns the name of the policy to validate incoming broadcast messages against
IngressPolicyNames() []string

// EgressPolicyNames returns the name of the policy to validate incoming broadcast messages against
EgressPolicyNames() []string
}

// Handler provides a hook which allows other pieces of code to participate in config proposals
// TODO, this should probably be renamed to ValueHandler
type Handler interface {
Transactional

// ProposeConfig called when config is added to a proposal
ProposeConfig(key string, configValue *cb.ConfigValue) error
}

// Manager provides a mechanism to query and update config
type Manager interface {
Resources
Expand Down Expand Up @@ -126,41 +51,46 @@ type Resources interface {
PolicyManager() policies.Manager

// ChannelConfig returns the ChannelConfig for the chain
ChannelConfig() ChannelConfig
ChannelConfig() configvalues.Channel

// OrdererConfig returns the configtxorderer.SharedConfig for the channel
OrdererConfig() OrdererConfig
OrdererConfig() configvalues.Orderer

// ApplicationConfig returns the configtxapplication.SharedConfig for the channel
ApplicationConfig() ApplicationConfig
ApplicationConfig() configvalues.Application

// MSPManager returns the msp.MSPManager for the chain
MSPManager() msp.MSPManager
}

// Transactional is an interface which allows for an update to be proposed and rolled back
type Transactional interface {
// BeginConfig called when a config proposal is begun
BeginConfig(groups []string) ([]Handler, error)

// RollbackConfig called when a config proposal is abandoned
RollbackConfig()
RollbackProposals()

// CommitConfig called when a config proposal is committed
CommitConfig()
CommitProposals()
}

// PolicyHandler is used for config updates to policy
type PolicyHandler interface {
Transactional

BeginConfig(groups []string) ([]PolicyHandler, error)

ProposePolicy(key string, path []string, policy *cb.ConfigPolicy) error
}

// Initializer is used as indirection between Manager and Handler to allow
// for single Handlers to handle multiple paths
type Initializer interface {
Handler
// ProposeValue is used for propsing group values
ProposeValue(key string, configValue *cb.ConfigValue) error

// BeginValueProposals is called when a config proposal is begun
BeginValueProposals(groups []string) ([]configvalues.ValueProposer, error)

Transactional

Resources

Expand Down
13 changes: 7 additions & 6 deletions common/configtx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"

"github.com/hyperledger/fabric/common/configtx/api"
configvaluesapi "github.com/hyperledger/fabric/common/configvalues/api"
cb "github.com/hyperledger/fabric/protos/common"
)

Expand All @@ -32,21 +33,21 @@ func (cr *configResult) commit() {
for _, subResult := range cr.subResults {
subResult.commit()
}
cr.handler.CommitConfig()
cr.handler.CommitProposals()
}

func (cr *configResult) rollback() {
for _, subResult := range cr.subResults {
subResult.rollback()
}
cr.handler.RollbackConfig()
cr.handler.RollbackProposals()
}

// proposeGroup proposes a group configuration with a given handler
// it will in turn recursively call itself until all groups have been exhausted
// at each call, it returns the handler that was passed in, plus any handlers returned
// by recursive calls into proposeGroup
func (cm *configManager) proposeGroup(name string, group *cb.ConfigGroup, handler api.Handler) (*configResult, error) {
func (cm *configManager) proposeGroup(name string, group *cb.ConfigGroup, handler configvaluesapi.ValueProposer) (*configResult, error) {
subGroups := make([]string, len(group.Groups))
i := 0
for subGroup := range group.Groups {
Expand All @@ -55,7 +56,7 @@ func (cm *configManager) proposeGroup(name string, group *cb.ConfigGroup, handle
}

logger.Debugf("Beginning new config for channel %s and group %s", cm.chainID, name)
subHandlers, err := handler.BeginConfig(subGroups)
subHandlers, err := handler.BeginValueProposals(subGroups)
if err != nil {
return nil, err
}
Expand All @@ -79,7 +80,7 @@ func (cm *configManager) proposeGroup(name string, group *cb.ConfigGroup, handle
}

for key, value := range group.Values {
if err := handler.ProposeConfig(key, value); err != nil {
if err := handler.ProposeValue(key, value); err != nil {
result.rollback()
return nil, err
}
Expand All @@ -94,7 +95,7 @@ func (cm *configManager) proposePolicies(rootGroup *cb.ConfigGroup) (*configResu
for key, policy := range rootGroup.Policies {
logger.Debugf("Proposing policy: %s", key)
if err := cm.initializer.PolicyHandler().ProposePolicy(key, []string{RootGroupKey}, policy); err != nil {
cm.initializer.PolicyHandler().RollbackConfig()
cm.initializer.PolicyHandler().RollbackProposals()
return nil, err
}
}
Expand Down
35 changes: 18 additions & 17 deletions common/configtx/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ import (

"github.com/hyperledger/fabric/common/cauthdsl"
"github.com/hyperledger/fabric/common/configtx/api"
configtxapplication "github.com/hyperledger/fabric/common/configtx/handlers/application"
configtxchannel "github.com/hyperledger/fabric/common/configtx/handlers/channel"
configtxmsp "github.com/hyperledger/fabric/common/configtx/handlers/msp"
configtxorderer "github.com/hyperledger/fabric/common/configtx/handlers/orderer"
configvaluesapi "github.com/hyperledger/fabric/common/configvalues/api"
configtxchannel "github.com/hyperledger/fabric/common/configvalues/channel"
configtxapplication "github.com/hyperledger/fabric/common/configvalues/channel/application"
configtxorderer "github.com/hyperledger/fabric/common/configvalues/channel/orderer"
configtxmsp "github.com/hyperledger/fabric/common/configvalues/msp"
"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/msp"
cb "github.com/hyperledger/fabric/protos/common"
Expand All @@ -44,17 +45,17 @@ func (r *resources) PolicyManager() policies.Manager {
}

// ChannelConfig returns the api.ChannelConfig for the chain
func (r *resources) ChannelConfig() api.ChannelConfig {
func (r *resources) ChannelConfig() configvaluesapi.Channel {
return r.channelConfig
}

// OrdererConfig returns the api.OrdererConfig for the chain
func (r *resources) OrdererConfig() api.OrdererConfig {
func (r *resources) OrdererConfig() configvaluesapi.Orderer {
return r.ordererConfig
}

// ApplicationConfig returns the api.ApplicationConfig for the chain
func (r *resources) ApplicationConfig() api.ApplicationConfig {
func (r *resources) ApplicationConfig() configvaluesapi.Application {
return r.applicationConfig
}

Expand Down Expand Up @@ -104,35 +105,35 @@ func NewInitializer() api.Initializer {
}
}

// BeginConfig is used to start a new config proposal
func (i *initializer) BeginConfig(groups []string) ([]api.Handler, error) {
// BeginValueProposals is used to start a new config proposal
func (i *initializer) BeginValueProposals(groups []string) ([]configvaluesapi.ValueProposer, error) {
if len(groups) != 1 {
logger.Panicf("Initializer only supports having one root group")
}
i.mspConfigHandler.BeginConfig()
return []api.Handler{i.channelConfig}, nil
return []configvaluesapi.ValueProposer{i.channelConfig}, nil
}

// RollbackConfig is used to abandon a new config proposal
func (i *initializer) RollbackConfig() {
i.mspConfigHandler.RollbackConfig()
func (i *initializer) RollbackProposals() {
i.mspConfigHandler.RollbackProposals()
}

// CommitConfig is used to commit a new config proposal
func (i *initializer) CommitConfig() {
i.mspConfigHandler.CommitConfig()
func (i *initializer) CommitProposals() {
i.mspConfigHandler.CommitProposals()
}

type importHack struct {
*policies.ManagerImpl
}

func (ih importHack) BeginConfig(groups []string) ([]api.Handler, error) {
func (ih importHack) BeginConfig(groups []string) ([]api.PolicyHandler, error) {
policyManagers, err := ih.ManagerImpl.BeginConfig(groups)
if err != nil {
return nil, err
}
handlers := make([]api.Handler, len(policyManagers))
handlers := make([]api.PolicyHandler, len(policyManagers))
for i, policyManager := range policyManagers {
handlers[i] = &importHack{ManagerImpl: policyManager}
}
Expand All @@ -147,6 +148,6 @@ func (i *initializer) PolicyHandler() api.PolicyHandler {
return importHack{ManagerImpl: i.policyManager}
}

func (i *initializer) ProposeConfig(key string, value *cb.ConfigValue) error {
func (i *initializer) ProposeValue(key string, value *cb.ConfigValue) error {
return fmt.Errorf("Programming error, this should never be invoked")
}
2 changes: 1 addition & 1 deletion common/configtx/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package configtx

import (
configtxorderer "github.com/hyperledger/fabric/common/configtx/handlers/orderer"
configtxorderer "github.com/hyperledger/fabric/common/configvalues/channel/orderer"
"github.com/hyperledger/fabric/common/util"
cb "github.com/hyperledger/fabric/protos/common"
ab "github.com/hyperledger/fabric/protos/orderer"
Expand Down
2 changes: 1 addition & 1 deletion common/configtx/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"testing"

"github.com/golang/protobuf/proto"
configtxorderer "github.com/hyperledger/fabric/common/configtx/handlers/orderer"
configtxorderer "github.com/hyperledger/fabric/common/configvalues/channel/orderer"
cb "github.com/hyperledger/fabric/protos/common"
ab "github.com/hyperledger/fabric/protos/orderer"

Expand Down
6 changes: 3 additions & 3 deletions common/configtx/test/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
"path/filepath"

"github.com/hyperledger/fabric/common/configtx"
configtxapplication "github.com/hyperledger/fabric/common/configtx/handlers/application"
configtxmsp "github.com/hyperledger/fabric/common/configtx/handlers/msp"
configtxorderer "github.com/hyperledger/fabric/common/configtx/handlers/orderer"
genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig"
"github.com/hyperledger/fabric/common/configtx/tool/provisional"
configtxapplication "github.com/hyperledger/fabric/common/configvalues/channel/application"
configtxorderer "github.com/hyperledger/fabric/common/configvalues/channel/orderer"
configtxmsp "github.com/hyperledger/fabric/common/configvalues/msp"
"github.com/hyperledger/fabric/common/genesis"
"github.com/hyperledger/fabric/msp"
cb "github.com/hyperledger/fabric/protos/common"
Expand Down
4 changes: 2 additions & 2 deletions common/configtx/tool/provisional/provisional.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (

"github.com/hyperledger/fabric/common/cauthdsl"
"github.com/hyperledger/fabric/common/configtx"
configtxchannel "github.com/hyperledger/fabric/common/configtx/handlers/channel"
configtxorderer "github.com/hyperledger/fabric/common/configtx/handlers/orderer"
genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig"
configtxchannel "github.com/hyperledger/fabric/common/configvalues/channel"
configtxorderer "github.com/hyperledger/fabric/common/configvalues/channel/orderer"
"github.com/hyperledger/fabric/common/genesis"
"github.com/hyperledger/fabric/orderer/common/bootstrap"
cb "github.com/hyperledger/fabric/protos/common"
Expand Down
Loading

0 comments on commit fee7c6c

Please sign in to comment.