Skip to content

Commit

Permalink
[FAB-2120] Move configtx.Filter back to orderer
Browse files Browse the repository at this point in the history
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 <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Feb 8, 2017
1 parent 3c10c46 commit 14e3a11
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 93 deletions.
79 changes: 79 additions & 0 deletions common/configtx/api/api.go
Original file line number Diff line number Diff line change
@@ -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
}
41 changes: 5 additions & 36 deletions common/configtx/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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")
Expand Down
16 changes: 8 additions & 8 deletions common/configtx/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
}
Expand Down Expand Up @@ -87,22 +87,22 @@ 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")
}
}

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)
Expand Down
32 changes: 5 additions & 27 deletions common/configtx/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions common/mocks/configtx/configtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ 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"
)

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
Expand All @@ -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
}

Expand All @@ -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

Expand Down
6 changes: 3 additions & 3 deletions common/mocks/configtx/configtx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})
}
7 changes: 4 additions & 3 deletions core/peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -42,7 +43,7 @@ import (
var peerLogger = logging.MustGetLogger("peer")

type chainSupport struct {
configtx.Manager
configtxapi.Manager
sharedconfig.Descriptor
ledger ledger.PeerLedger
}
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions msp/mgmt/mspconfigmgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 14e3a11

Please sign in to comment.