From 14e3a116aff87b62e444ffed1e0a3f660303db24 Mon Sep 17 00:00:00 2001 From: Jason Yellick Date: Wed, 8 Feb 2017 00:06:57 -0500 Subject: [PATCH] [FAB-2120] Move configtx.Filter back to orderer https://jira.hyperledger.org/browse/FAB-2120 When configtx was moved from orderer to common it brought along the configtx.Filter, which is inherently an orderer concept and violates the rule of not importing from common into orderer. Additionally, this CR moves some of the interfaces into an API subpackage, to help future refactoring. Change-Id: If522b44a83ba8d7bc3080c2ce944c927e6621d19 Signed-off-by: Jason Yellick --- common/configtx/api/api.go | 79 +++++++++++++++++++ common/configtx/manager.go | 41 ++-------- common/configtx/manager_test.go | 16 ++-- common/configtx/resources.go | 32 ++------ common/mocks/configtx/configtx.go | 8 +- common/mocks/configtx/configtx_test.go | 6 +- core/peer/peer.go | 7 +- msp/mgmt/mspconfigmgr_test.go | 4 +- .../common/configtxfilter}/filter.go | 9 ++- .../common/configtxfilter}/filter_test.go | 3 +- orderer/multichain/chainsupport.go | 6 +- orderer/multichain/manager.go | 3 +- 12 files changed, 121 insertions(+), 93 deletions(-) create mode 100644 common/configtx/api/api.go rename {common/configtx => orderer/common/configtxfilter}/filter.go (91%) rename {common/configtx => orderer/common/configtxfilter}/filter_test.go (97%) diff --git a/common/configtx/api/api.go b/common/configtx/api/api.go new file mode 100644 index 00000000000..4afa40af4c8 --- /dev/null +++ b/common/configtx/api/api.go @@ -0,0 +1,79 @@ +/* +Copyright IBM Corp. 2017 All Rights Reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package api + +import ( + "github.com/hyperledger/fabric/common/chainconfig" + "github.com/hyperledger/fabric/common/policies" + "github.com/hyperledger/fabric/msp" + cb "github.com/hyperledger/fabric/protos/common" +) + +// Handler provides a hook which allows other pieces of code to participate in config proposals +type Handler interface { + // BeginConfig called when a config proposal is begun + BeginConfig() + + // RollbackConfig called when a config proposal is abandoned + RollbackConfig() + + // CommitConfig called when a config proposal is committed + CommitConfig() + + // ProposeConfig called when config is added to a proposal + ProposeConfig(configItem *cb.ConfigItem) error +} + +// Manager provides a mechanism to query and update config +type Manager interface { + Resources + + // Apply attempts to apply a configtx to become the new config + Apply(configtx *cb.ConfigEnvelope) error + + // Validate attempts to validate a new configtx against the current config state + Validate(configtx *cb.ConfigEnvelope) error + + // ChainID retrieves the chain ID associated with this manager + ChainID() string + + // Sequence returns the current sequence number of the config + Sequence() uint64 +} + +// Resources is the common set of config resources for all chains +// Depending on whether chain is used at the orderer or at the peer, other +// config resources may be available +type Resources interface { + // PolicyManager returns the policies.Manager for the chain + PolicyManager() policies.Manager + + // ChainConfig returns the chainconfig.Descriptor for the chain + ChainConfig() chainconfig.Descriptor + + // MSPManager returns the msp.MSPManager for the chain + MSPManager() msp.MSPManager +} + +// Initializer is a structure which is only useful before a configtx.Manager +// has been instantiated for a chain, afterwards, it is of no utility, which +// is why it embeds the Resources interface +type Initializer interface { + Resources + // Handlers returns the handlers to be used when initializing the configtx.Manager + Handlers() map[cb.ConfigItem_ConfigType]Handler +} diff --git a/common/configtx/manager.go b/common/configtx/manager.go index dea7d301555..a4c52926cf7 100644 --- a/common/configtx/manager.go +++ b/common/configtx/manager.go @@ -22,6 +22,7 @@ import ( "reflect" "regexp" + "github.com/hyperledger/fabric/common/configtx/api" "github.com/hyperledger/fabric/common/policies" cb "github.com/hyperledger/fabric/protos/common" "github.com/hyperledger/fabric/protos/utils" @@ -41,38 +42,6 @@ var ( } ) -// Handler provides a hook which allows other pieces of code to participate in config proposals -type Handler interface { - // BeginConfig called when a config proposal is begun - BeginConfig() - - // RollbackConfig called when a config proposal is abandoned - RollbackConfig() - - // CommitConfig called when a config proposal is committed - CommitConfig() - - // ProposeConfig called when config is added to a proposal - ProposeConfig(configItem *cb.ConfigItem) error -} - -// Manager provides a mechanism to query and update config -type Manager interface { - Resources - - // Apply attempts to apply a configtx to become the new config - Apply(configtx *cb.ConfigEnvelope) error - - // Validate attempts to validate a new configtx against the current config state - Validate(configtx *cb.ConfigEnvelope) error - - // ChainID retrieves the chain ID associated with this manager - ChainID() string - - // Sequence returns the current sequence number of the config - Sequence() uint64 -} - // NewConfigItemPolicyKey is the ID of the policy used when no other policy can be resolved, for instance when attempting to create a new config item const NewConfigItemPolicyKey = "NewConfigItemPolicy" @@ -83,11 +52,11 @@ func (ap *acceptAllPolicy) Evaluate(signedData []*cb.SignedData) error { } type configManager struct { - Initializer + api.Initializer sequence uint64 chainID string config map[cb.ConfigItem_ConfigType]map[string]*cb.ConfigItem - callOnUpdate []func(Manager) + callOnUpdate []func(api.Manager) } // computeChainIDAndSequence returns the chain id and the sequence number for a config envelope @@ -152,7 +121,7 @@ func validateChainID(chainID string) error { return nil } -func NewManagerImplNext(configtx *cb.ConfigEnvelope, initializer Initializer, callOnUpdate []func(Manager)) (Manager, error) { +func NewManagerImplNext(configtx *cb.ConfigEnvelope, initializer api.Initializer, callOnUpdate []func(api.Manager)) (api.Manager, error) { configNext, err := UnmarshalConfigNext(configtx.Config) if err != nil { return nil, err @@ -165,7 +134,7 @@ func NewManagerImplNext(configtx *cb.ConfigEnvelope, initializer Initializer, ca // NewManagerImpl creates a new Manager unless an error is encountered, each element of the callOnUpdate slice // is invoked when a new config is committed -func NewManagerImpl(configtx *cb.ConfigEnvelope, initializer Initializer, callOnUpdate []func(Manager)) (Manager, error) { +func NewManagerImpl(configtx *cb.ConfigEnvelope, initializer api.Initializer, callOnUpdate []func(api.Manager)) (api.Manager, error) { for ctype := range cb.ConfigItem_ConfigType_name { if _, ok := initializer.Handlers()[cb.ConfigItem_ConfigType(ctype)]; !ok { return nil, errors.New("Must supply a handler for all known types") diff --git a/common/configtx/manager_test.go b/common/configtx/manager_test.go index 14105fa73d9..bebaeaa2548 100644 --- a/common/configtx/manager_test.go +++ b/common/configtx/manager_test.go @@ -14,14 +14,14 @@ See the License for the specific language governing permissions and limitations under the License. */ -package configtx_test +package configtx import ( "errors" "fmt" "testing" - . "github.com/hyperledger/fabric/common/configtx" + "github.com/hyperledger/fabric/common/configtx/api" mockconfigtx "github.com/hyperledger/fabric/common/mocks/configtx" "github.com/hyperledger/fabric/common/policies" cb "github.com/hyperledger/fabric/protos/common" @@ -30,8 +30,8 @@ import ( var defaultChain = "DefaultChainID" -func defaultHandlers() map[cb.ConfigItem_ConfigType]Handler { - handlers := make(map[cb.ConfigItem_ConfigType]Handler) +func defaultHandlers() map[cb.ConfigItem_ConfigType]api.Handler { + handlers := make(map[cb.ConfigItem_ConfigType]api.Handler) for ctype := range cb.ConfigItem_ConfigType_name { handlers[cb.ConfigItem_ConfigType(ctype)] = NewBytesHandler() } @@ -87,7 +87,7 @@ func makeMarshaledConfig(chainID string, configItems ...*cb.ConfigItem) []byte { func TestOmittedHandler(t *testing.T) { _, err := NewManagerImpl(&cb.ConfigEnvelope{ Config: makeMarshaledConfig(defaultChain, makeConfigItem("foo", "foo", 0, []byte("foo"))), - }, &mockconfigtx.Initializer{PolicyManagerVal: &mockPolicyManager{&mockPolicy{}}, HandlersVal: map[cb.ConfigItem_ConfigType]Handler{}}, nil) + }, &mockconfigtx.Initializer{PolicyManagerVal: &mockPolicyManager{&mockPolicy{}}, HandlersVal: map[cb.ConfigItem_ConfigType]api.Handler{}}, nil) if err == nil { t.Fatal("Should have failed to construct manager because handlers were missing") @@ -95,14 +95,14 @@ func TestOmittedHandler(t *testing.T) { } func TestCallback(t *testing.T) { - var calledBack Manager - callback := func(m Manager) { + var calledBack api.Manager + callback := func(m api.Manager) { calledBack = m } cm, err := NewManagerImpl(&cb.ConfigEnvelope{ Config: makeMarshaledConfig(defaultChain, makeConfigItem("foo", "foo", 0, []byte("foo"))), - }, &mockconfigtx.Initializer{PolicyManagerVal: &mockPolicyManager{&mockPolicy{}}, HandlersVal: defaultHandlers()}, []func(Manager){callback}) + }, &mockconfigtx.Initializer{PolicyManagerVal: &mockPolicyManager{&mockPolicy{}}, HandlersVal: defaultHandlers()}, []func(api.Manager){callback}) if err != nil { t.Fatalf("Error constructing config manager: %s", err) diff --git a/common/configtx/resources.go b/common/configtx/resources.go index dc5ba59ce8b..d9a6b66bbdc 100644 --- a/common/configtx/resources.go +++ b/common/configtx/resources.go @@ -19,37 +19,15 @@ package configtx import ( "github.com/hyperledger/fabric/common/cauthdsl" "github.com/hyperledger/fabric/common/chainconfig" + "github.com/hyperledger/fabric/common/configtx/api" "github.com/hyperledger/fabric/common/policies" "github.com/hyperledger/fabric/msp" mspmgmt "github.com/hyperledger/fabric/msp/mgmt" cb "github.com/hyperledger/fabric/protos/common" ) -// Resources is the common set of config resources for all chains -// Depending on whether chain is used at the orderer or at the peer, other -// config resources may be available -type Resources interface { - // PolicyManager returns the policies.Manager for the chain - PolicyManager() policies.Manager - - // ChainConfig returns the chainconfig.Descriptor for the chain - ChainConfig() chainconfig.Descriptor - - // MSPManager returns the msp.MSPManager for the chain - MSPManager() msp.MSPManager -} - -// Initializer is a structure which is only useful before a configtx.Manager -// has been instantiated for a chain, afterwards, it is of no utility, which -// is why it embeds the Resources interface -type Initializer interface { - Resources - // Handlers returns the handlers to be used when initializing the configtx.Manager - Handlers() map[cb.ConfigItem_ConfigType]Handler -} - type resources struct { - handlers map[cb.ConfigItem_ConfigType]Handler + handlers map[cb.ConfigItem_ConfigType]api.Handler policyManager policies.Manager chainConfig chainconfig.Descriptor mspConfigHandler *mspmgmt.MSPConfigHandler @@ -71,12 +49,12 @@ func (r *resources) MSPManager() msp.MSPManager { } // Handlers returns the handlers to be used when initializing the configtx.Manager -func (r *resources) Handlers() map[cb.ConfigItem_ConfigType]Handler { +func (r *resources) Handlers() map[cb.ConfigItem_ConfigType]api.Handler { return r.handlers } // NewInitializer creates a chain initializer for the basic set of common chain resources -func NewInitializer() Initializer { +func NewInitializer() api.Initializer { mspConfigHandler := &mspmgmt.MSPConfigHandler{} policyProviderMap := make(map[int32]policies.Provider) for pType := range cb.Policy_PolicyType_name { @@ -93,7 +71,7 @@ func NewInitializer() Initializer { policyManager := policies.NewManagerImpl(policyProviderMap) chainConfig := chainconfig.NewDescriptorImpl() - handlers := make(map[cb.ConfigItem_ConfigType]Handler) + handlers := make(map[cb.ConfigItem_ConfigType]api.Handler) for ctype := range cb.ConfigItem_ConfigType_name { rtype := cb.ConfigItem_ConfigType(ctype) diff --git a/common/mocks/configtx/configtx.go b/common/mocks/configtx/configtx.go index 67c2d2e307b..d302084a026 100644 --- a/common/mocks/configtx/configtx.go +++ b/common/mocks/configtx/configtx.go @@ -18,7 +18,7 @@ package configtx import ( "github.com/hyperledger/fabric/common/chainconfig" - "github.com/hyperledger/fabric/common/configtx" + configtxapi "github.com/hyperledger/fabric/common/configtx/api" "github.com/hyperledger/fabric/common/policies" "github.com/hyperledger/fabric/msp" cb "github.com/hyperledger/fabric/protos/common" @@ -26,7 +26,7 @@ import ( type Initializer struct { // HandlersVal is returned as the result of Handlers() - HandlersVal map[cb.ConfigItem_ConfigType]configtx.Handler + HandlersVal map[cb.ConfigItem_ConfigType]configtxapi.Handler // PolicyManagerVal is returned as the result of PolicyManager() PolicyManagerVal policies.Manager @@ -39,7 +39,7 @@ type Initializer struct { } // Returns the HandlersVal -func (i *Initializer) Handlers() map[cb.ConfigItem_ConfigType]configtx.Handler { +func (i *Initializer) Handlers() map[cb.ConfigItem_ConfigType]configtxapi.Handler { return i.HandlersVal } @@ -58,7 +58,7 @@ func (i *Initializer) MSPManager() msp.MSPManager { return i.MSPManagerVal } -// Manager is a mock implementation of configtx.Manager +// Manager is a mock implementation of configtxapi.Manager type Manager struct { Initializer diff --git a/common/mocks/configtx/configtx_test.go b/common/mocks/configtx/configtx_test.go index 6b009a7617b..7837a3742ba 100644 --- a/common/mocks/configtx/configtx_test.go +++ b/common/mocks/configtx/configtx_test.go @@ -19,13 +19,13 @@ package configtx import ( "testing" - "github.com/hyperledger/fabric/common/configtx" + configtxapi "github.com/hyperledger/fabric/common/configtx/api" ) func TestConfigtxInitializerInterface(t *testing.T) { - _ = configtx.Initializer(&Initializer{}) + _ = configtxapi.Initializer(&Initializer{}) } func TestConfigtxManagerInterface(t *testing.T) { - _ = configtx.Manager(&Manager{}) + _ = configtxapi.Manager(&Manager{}) } diff --git a/core/peer/peer.go b/core/peer/peer.go index d6a547495c8..ddb441c263a 100644 --- a/core/peer/peer.go +++ b/core/peer/peer.go @@ -23,6 +23,7 @@ import ( "sync" "github.com/hyperledger/fabric/common/configtx" + configtxapi "github.com/hyperledger/fabric/common/configtx/api" "github.com/hyperledger/fabric/core/comm" "github.com/hyperledger/fabric/core/committer" "github.com/hyperledger/fabric/core/committer/txvalidator" @@ -42,7 +43,7 @@ import ( var peerLogger = logging.MustGetLogger("peer") type chainSupport struct { - configtx.Manager + configtxapi.Manager sharedconfig.Descriptor ledger ledger.PeerLedger } @@ -163,7 +164,7 @@ func createChain(cid string, ledger ledger.PeerLedger, cb *common.Block) error { gossipEventer := service.GetGossipService().NewConfigEventer() - gossipCallbackWrapper := func(cm configtx.Manager) { + gossipCallbackWrapper := func(cm configtxapi.Manager) { gossipEventer.ProcessConfigUpdate(&chainSupport{ Manager: cm, Descriptor: sharedConfigHandler, @@ -175,7 +176,7 @@ func createChain(cid string, ledger ledger.PeerLedger, cb *common.Block) error { configtxManager, err := configtx.NewManagerImplNext( configEnvelope, configtxInitializer, - []func(cm configtx.Manager){gossipCallbackWrapper}, + []func(cm configtxapi.Manager){gossipCallbackWrapper}, ) if err != nil { return err diff --git a/msp/mgmt/mspconfigmgr_test.go b/msp/mgmt/mspconfigmgr_test.go index b74b921efe8..fccfba31235 100644 --- a/msp/mgmt/mspconfigmgr_test.go +++ b/msp/mgmt/mspconfigmgr_test.go @@ -20,7 +20,7 @@ import ( "testing" "github.com/golang/protobuf/proto" - "github.com/hyperledger/fabric/common/configtx" + configtxapi "github.com/hyperledger/fabric/common/configtx/api" "github.com/hyperledger/fabric/msp" . "github.com/hyperledger/fabric/msp/mgmt" "github.com/hyperledger/fabric/protos/common" @@ -39,7 +39,7 @@ func TestMSPConfigManager(t *testing.T) { // test success: // begin/propose/commit - var mspCH configtx.Handler + var mspCH configtxapi.Handler mspCH = &MSPConfigHandler{} mspCH.BeginConfig() err = mspCH.ProposeConfig(ci) diff --git a/common/configtx/filter.go b/orderer/common/configtxfilter/filter.go similarity index 91% rename from common/configtx/filter.go rename to orderer/common/configtxfilter/filter.go index e12b96460ed..a149b3a2be5 100644 --- a/common/configtx/filter.go +++ b/orderer/common/configtxfilter/filter.go @@ -14,11 +14,12 @@ See the License for the specific language governing permissions and limitations under the License. */ -package configtx +package configtxfilter import ( "fmt" + "github.com/hyperledger/fabric/common/configtx/api" "github.com/hyperledger/fabric/orderer/common/filter" cb "github.com/hyperledger/fabric/protos/common" @@ -26,18 +27,18 @@ import ( ) type configFilter struct { - configManager Manager + configManager api.Manager } // New creates a new configfilter Rule based on the given Manager -func NewFilter(manager Manager) filter.Rule { +func NewFilter(manager api.Manager) filter.Rule { return &configFilter{ configManager: manager, } } type configCommitter struct { - manager Manager + manager api.Manager configEnvelope *cb.ConfigEnvelope } diff --git a/common/configtx/filter_test.go b/orderer/common/configtxfilter/filter_test.go similarity index 97% rename from common/configtx/filter_test.go rename to orderer/common/configtxfilter/filter_test.go index 7cd8ee32a33..371cfeaf28a 100644 --- a/common/configtx/filter_test.go +++ b/orderer/common/configtxfilter/filter_test.go @@ -14,14 +14,13 @@ See the License for the specific language governing permissions and limitations under the License. */ -package configtx_test +package configtxfilter import ( "fmt" "reflect" "testing" - . "github.com/hyperledger/fabric/common/configtx" mockconfigtx "github.com/hyperledger/fabric/common/mocks/configtx" "github.com/hyperledger/fabric/orderer/common/filter" cb "github.com/hyperledger/fabric/protos/common" diff --git a/orderer/multichain/chainsupport.go b/orderer/multichain/chainsupport.go index dbc88cbf0ab..899e9e12a50 100644 --- a/orderer/multichain/chainsupport.go +++ b/orderer/multichain/chainsupport.go @@ -17,12 +17,12 @@ limitations under the License. package multichain import ( - "github.com/hyperledger/fabric/common/configtx" "github.com/hyperledger/fabric/common/crypto" "github.com/hyperledger/fabric/common/policies" "github.com/hyperledger/fabric/common/util" "github.com/hyperledger/fabric/orderer/common/blockcutter" "github.com/hyperledger/fabric/orderer/common/broadcast" + "github.com/hyperledger/fabric/orderer/common/configtxfilter" "github.com/hyperledger/fabric/orderer/common/filter" "github.com/hyperledger/fabric/orderer/common/sharedconfig" "github.com/hyperledger/fabric/orderer/common/sigfilter" @@ -143,7 +143,7 @@ func createStandardFilters(ledgerResources *ledgerResources) *filter.RuleSet { filter.EmptyRejectRule, sizefilter.MaxBytesRule(ledgerResources.SharedConfig().BatchSize().AbsoluteMaxBytes), sigfilter.New(ledgerResources.SharedConfig().IngressPolicyNames, ledgerResources.PolicyManager()), - configtx.NewFilter(ledgerResources), + configtxfilter.NewFilter(ledgerResources), filter.AcceptRule, }) @@ -156,7 +156,7 @@ func createSystemChainFilters(ml *multiLedger, ledgerResources *ledgerResources) sizefilter.MaxBytesRule(ledgerResources.SharedConfig().BatchSize().AbsoluteMaxBytes), sigfilter.New(ledgerResources.SharedConfig().IngressPolicyNames, ledgerResources.PolicyManager()), newSystemChainFilter(ml), - configtx.NewFilter(ledgerResources), + configtxfilter.NewFilter(ledgerResources), filter.AcceptRule, }) } diff --git a/orderer/multichain/manager.go b/orderer/multichain/manager.go index d332a2dd8d6..b9aaec31686 100644 --- a/orderer/multichain/manager.go +++ b/orderer/multichain/manager.go @@ -20,6 +20,7 @@ import ( "fmt" "github.com/hyperledger/fabric/common/configtx" + configtxapi "github.com/hyperledger/fabric/common/configtx/api" "github.com/hyperledger/fabric/orderer/common/sharedconfig" ordererledger "github.com/hyperledger/fabric/orderer/ledger" cb "github.com/hyperledger/fabric/protos/common" @@ -44,7 +45,7 @@ type Manager interface { } type configResources struct { - configtx.Manager + configtxapi.Manager sharedConfig sharedconfig.Manager }