Skip to content

Commit 14e3a11

Browse files
author
Jason Yellick
committed
[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 <[email protected]>
1 parent 3c10c46 commit 14e3a11

File tree

12 files changed

+121
-93
lines changed

12 files changed

+121
-93
lines changed

common/configtx/api/api.go

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
Copyright IBM Corp. 2017 All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package api
18+
19+
import (
20+
"github.com/hyperledger/fabric/common/chainconfig"
21+
"github.com/hyperledger/fabric/common/policies"
22+
"github.com/hyperledger/fabric/msp"
23+
cb "github.com/hyperledger/fabric/protos/common"
24+
)
25+
26+
// Handler provides a hook which allows other pieces of code to participate in config proposals
27+
type Handler interface {
28+
// BeginConfig called when a config proposal is begun
29+
BeginConfig()
30+
31+
// RollbackConfig called when a config proposal is abandoned
32+
RollbackConfig()
33+
34+
// CommitConfig called when a config proposal is committed
35+
CommitConfig()
36+
37+
// ProposeConfig called when config is added to a proposal
38+
ProposeConfig(configItem *cb.ConfigItem) error
39+
}
40+
41+
// Manager provides a mechanism to query and update config
42+
type Manager interface {
43+
Resources
44+
45+
// Apply attempts to apply a configtx to become the new config
46+
Apply(configtx *cb.ConfigEnvelope) error
47+
48+
// Validate attempts to validate a new configtx against the current config state
49+
Validate(configtx *cb.ConfigEnvelope) error
50+
51+
// ChainID retrieves the chain ID associated with this manager
52+
ChainID() string
53+
54+
// Sequence returns the current sequence number of the config
55+
Sequence() uint64
56+
}
57+
58+
// Resources is the common set of config resources for all chains
59+
// Depending on whether chain is used at the orderer or at the peer, other
60+
// config resources may be available
61+
type Resources interface {
62+
// PolicyManager returns the policies.Manager for the chain
63+
PolicyManager() policies.Manager
64+
65+
// ChainConfig returns the chainconfig.Descriptor for the chain
66+
ChainConfig() chainconfig.Descriptor
67+
68+
// MSPManager returns the msp.MSPManager for the chain
69+
MSPManager() msp.MSPManager
70+
}
71+
72+
// Initializer is a structure which is only useful before a configtx.Manager
73+
// has been instantiated for a chain, afterwards, it is of no utility, which
74+
// is why it embeds the Resources interface
75+
type Initializer interface {
76+
Resources
77+
// Handlers returns the handlers to be used when initializing the configtx.Manager
78+
Handlers() map[cb.ConfigItem_ConfigType]Handler
79+
}

common/configtx/manager.go

+5-36
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"reflect"
2323
"regexp"
2424

25+
"github.com/hyperledger/fabric/common/configtx/api"
2526
"github.com/hyperledger/fabric/common/policies"
2627
cb "github.com/hyperledger/fabric/protos/common"
2728
"github.com/hyperledger/fabric/protos/utils"
@@ -41,38 +42,6 @@ var (
4142
}
4243
)
4344

44-
// Handler provides a hook which allows other pieces of code to participate in config proposals
45-
type Handler interface {
46-
// BeginConfig called when a config proposal is begun
47-
BeginConfig()
48-
49-
// RollbackConfig called when a config proposal is abandoned
50-
RollbackConfig()
51-
52-
// CommitConfig called when a config proposal is committed
53-
CommitConfig()
54-
55-
// ProposeConfig called when config is added to a proposal
56-
ProposeConfig(configItem *cb.ConfigItem) error
57-
}
58-
59-
// Manager provides a mechanism to query and update config
60-
type Manager interface {
61-
Resources
62-
63-
// Apply attempts to apply a configtx to become the new config
64-
Apply(configtx *cb.ConfigEnvelope) error
65-
66-
// Validate attempts to validate a new configtx against the current config state
67-
Validate(configtx *cb.ConfigEnvelope) error
68-
69-
// ChainID retrieves the chain ID associated with this manager
70-
ChainID() string
71-
72-
// Sequence returns the current sequence number of the config
73-
Sequence() uint64
74-
}
75-
7645
// 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
7746
const NewConfigItemPolicyKey = "NewConfigItemPolicy"
7847

@@ -83,11 +52,11 @@ func (ap *acceptAllPolicy) Evaluate(signedData []*cb.SignedData) error {
8352
}
8453

8554
type configManager struct {
86-
Initializer
55+
api.Initializer
8756
sequence uint64
8857
chainID string
8958
config map[cb.ConfigItem_ConfigType]map[string]*cb.ConfigItem
90-
callOnUpdate []func(Manager)
59+
callOnUpdate []func(api.Manager)
9160
}
9261

9362
// computeChainIDAndSequence returns the chain id and the sequence number for a config envelope
@@ -152,7 +121,7 @@ func validateChainID(chainID string) error {
152121
return nil
153122
}
154123

155-
func NewManagerImplNext(configtx *cb.ConfigEnvelope, initializer Initializer, callOnUpdate []func(Manager)) (Manager, error) {
124+
func NewManagerImplNext(configtx *cb.ConfigEnvelope, initializer api.Initializer, callOnUpdate []func(api.Manager)) (api.Manager, error) {
156125
configNext, err := UnmarshalConfigNext(configtx.Config)
157126
if err != nil {
158127
return nil, err
@@ -165,7 +134,7 @@ func NewManagerImplNext(configtx *cb.ConfigEnvelope, initializer Initializer, ca
165134

166135
// NewManagerImpl creates a new Manager unless an error is encountered, each element of the callOnUpdate slice
167136
// is invoked when a new config is committed
168-
func NewManagerImpl(configtx *cb.ConfigEnvelope, initializer Initializer, callOnUpdate []func(Manager)) (Manager, error) {
137+
func NewManagerImpl(configtx *cb.ConfigEnvelope, initializer api.Initializer, callOnUpdate []func(api.Manager)) (api.Manager, error) {
169138
for ctype := range cb.ConfigItem_ConfigType_name {
170139
if _, ok := initializer.Handlers()[cb.ConfigItem_ConfigType(ctype)]; !ok {
171140
return nil, errors.New("Must supply a handler for all known types")

common/configtx/manager_test.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package configtx_test
17+
package configtx
1818

1919
import (
2020
"errors"
2121
"fmt"
2222
"testing"
2323

24-
. "github.com/hyperledger/fabric/common/configtx"
24+
"github.com/hyperledger/fabric/common/configtx/api"
2525
mockconfigtx "github.com/hyperledger/fabric/common/mocks/configtx"
2626
"github.com/hyperledger/fabric/common/policies"
2727
cb "github.com/hyperledger/fabric/protos/common"
@@ -30,8 +30,8 @@ import (
3030

3131
var defaultChain = "DefaultChainID"
3232

33-
func defaultHandlers() map[cb.ConfigItem_ConfigType]Handler {
34-
handlers := make(map[cb.ConfigItem_ConfigType]Handler)
33+
func defaultHandlers() map[cb.ConfigItem_ConfigType]api.Handler {
34+
handlers := make(map[cb.ConfigItem_ConfigType]api.Handler)
3535
for ctype := range cb.ConfigItem_ConfigType_name {
3636
handlers[cb.ConfigItem_ConfigType(ctype)] = NewBytesHandler()
3737
}
@@ -87,22 +87,22 @@ func makeMarshaledConfig(chainID string, configItems ...*cb.ConfigItem) []byte {
8787
func TestOmittedHandler(t *testing.T) {
8888
_, err := NewManagerImpl(&cb.ConfigEnvelope{
8989
Config: makeMarshaledConfig(defaultChain, makeConfigItem("foo", "foo", 0, []byte("foo"))),
90-
}, &mockconfigtx.Initializer{PolicyManagerVal: &mockPolicyManager{&mockPolicy{}}, HandlersVal: map[cb.ConfigItem_ConfigType]Handler{}}, nil)
90+
}, &mockconfigtx.Initializer{PolicyManagerVal: &mockPolicyManager{&mockPolicy{}}, HandlersVal: map[cb.ConfigItem_ConfigType]api.Handler{}}, nil)
9191

9292
if err == nil {
9393
t.Fatal("Should have failed to construct manager because handlers were missing")
9494
}
9595
}
9696

9797
func TestCallback(t *testing.T) {
98-
var calledBack Manager
99-
callback := func(m Manager) {
98+
var calledBack api.Manager
99+
callback := func(m api.Manager) {
100100
calledBack = m
101101
}
102102

103103
cm, err := NewManagerImpl(&cb.ConfigEnvelope{
104104
Config: makeMarshaledConfig(defaultChain, makeConfigItem("foo", "foo", 0, []byte("foo"))),
105-
}, &mockconfigtx.Initializer{PolicyManagerVal: &mockPolicyManager{&mockPolicy{}}, HandlersVal: defaultHandlers()}, []func(Manager){callback})
105+
}, &mockconfigtx.Initializer{PolicyManagerVal: &mockPolicyManager{&mockPolicy{}}, HandlersVal: defaultHandlers()}, []func(api.Manager){callback})
106106

107107
if err != nil {
108108
t.Fatalf("Error constructing config manager: %s", err)

common/configtx/resources.go

+5-27
Original file line numberDiff line numberDiff line change
@@ -19,37 +19,15 @@ package configtx
1919
import (
2020
"github.com/hyperledger/fabric/common/cauthdsl"
2121
"github.com/hyperledger/fabric/common/chainconfig"
22+
"github.com/hyperledger/fabric/common/configtx/api"
2223
"github.com/hyperledger/fabric/common/policies"
2324
"github.com/hyperledger/fabric/msp"
2425
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
2526
cb "github.com/hyperledger/fabric/protos/common"
2627
)
2728

28-
// Resources is the common set of config resources for all chains
29-
// Depending on whether chain is used at the orderer or at the peer, other
30-
// config resources may be available
31-
type Resources interface {
32-
// PolicyManager returns the policies.Manager for the chain
33-
PolicyManager() policies.Manager
34-
35-
// ChainConfig returns the chainconfig.Descriptor for the chain
36-
ChainConfig() chainconfig.Descriptor
37-
38-
// MSPManager returns the msp.MSPManager for the chain
39-
MSPManager() msp.MSPManager
40-
}
41-
42-
// Initializer is a structure which is only useful before a configtx.Manager
43-
// has been instantiated for a chain, afterwards, it is of no utility, which
44-
// is why it embeds the Resources interface
45-
type Initializer interface {
46-
Resources
47-
// Handlers returns the handlers to be used when initializing the configtx.Manager
48-
Handlers() map[cb.ConfigItem_ConfigType]Handler
49-
}
50-
5129
type resources struct {
52-
handlers map[cb.ConfigItem_ConfigType]Handler
30+
handlers map[cb.ConfigItem_ConfigType]api.Handler
5331
policyManager policies.Manager
5432
chainConfig chainconfig.Descriptor
5533
mspConfigHandler *mspmgmt.MSPConfigHandler
@@ -71,12 +49,12 @@ func (r *resources) MSPManager() msp.MSPManager {
7149
}
7250

7351
// Handlers returns the handlers to be used when initializing the configtx.Manager
74-
func (r *resources) Handlers() map[cb.ConfigItem_ConfigType]Handler {
52+
func (r *resources) Handlers() map[cb.ConfigItem_ConfigType]api.Handler {
7553
return r.handlers
7654
}
7755

7856
// NewInitializer creates a chain initializer for the basic set of common chain resources
79-
func NewInitializer() Initializer {
57+
func NewInitializer() api.Initializer {
8058
mspConfigHandler := &mspmgmt.MSPConfigHandler{}
8159
policyProviderMap := make(map[int32]policies.Provider)
8260
for pType := range cb.Policy_PolicyType_name {
@@ -93,7 +71,7 @@ func NewInitializer() Initializer {
9371

9472
policyManager := policies.NewManagerImpl(policyProviderMap)
9573
chainConfig := chainconfig.NewDescriptorImpl()
96-
handlers := make(map[cb.ConfigItem_ConfigType]Handler)
74+
handlers := make(map[cb.ConfigItem_ConfigType]api.Handler)
9775

9876
for ctype := range cb.ConfigItem_ConfigType_name {
9977
rtype := cb.ConfigItem_ConfigType(ctype)

common/mocks/configtx/configtx.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ package configtx
1818

1919
import (
2020
"github.com/hyperledger/fabric/common/chainconfig"
21-
"github.com/hyperledger/fabric/common/configtx"
21+
configtxapi "github.com/hyperledger/fabric/common/configtx/api"
2222
"github.com/hyperledger/fabric/common/policies"
2323
"github.com/hyperledger/fabric/msp"
2424
cb "github.com/hyperledger/fabric/protos/common"
2525
)
2626

2727
type Initializer struct {
2828
// HandlersVal is returned as the result of Handlers()
29-
HandlersVal map[cb.ConfigItem_ConfigType]configtx.Handler
29+
HandlersVal map[cb.ConfigItem_ConfigType]configtxapi.Handler
3030

3131
// PolicyManagerVal is returned as the result of PolicyManager()
3232
PolicyManagerVal policies.Manager
@@ -39,7 +39,7 @@ type Initializer struct {
3939
}
4040

4141
// Returns the HandlersVal
42-
func (i *Initializer) Handlers() map[cb.ConfigItem_ConfigType]configtx.Handler {
42+
func (i *Initializer) Handlers() map[cb.ConfigItem_ConfigType]configtxapi.Handler {
4343
return i.HandlersVal
4444
}
4545

@@ -58,7 +58,7 @@ func (i *Initializer) MSPManager() msp.MSPManager {
5858
return i.MSPManagerVal
5959
}
6060

61-
// Manager is a mock implementation of configtx.Manager
61+
// Manager is a mock implementation of configtxapi.Manager
6262
type Manager struct {
6363
Initializer
6464

common/mocks/configtx/configtx_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ package configtx
1919
import (
2020
"testing"
2121

22-
"github.com/hyperledger/fabric/common/configtx"
22+
configtxapi "github.com/hyperledger/fabric/common/configtx/api"
2323
)
2424

2525
func TestConfigtxInitializerInterface(t *testing.T) {
26-
_ = configtx.Initializer(&Initializer{})
26+
_ = configtxapi.Initializer(&Initializer{})
2727
}
2828

2929
func TestConfigtxManagerInterface(t *testing.T) {
30-
_ = configtx.Manager(&Manager{})
30+
_ = configtxapi.Manager(&Manager{})
3131
}

core/peer/peer.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"sync"
2424

2525
"github.com/hyperledger/fabric/common/configtx"
26+
configtxapi "github.com/hyperledger/fabric/common/configtx/api"
2627
"github.com/hyperledger/fabric/core/comm"
2728
"github.com/hyperledger/fabric/core/committer"
2829
"github.com/hyperledger/fabric/core/committer/txvalidator"
@@ -42,7 +43,7 @@ import (
4243
var peerLogger = logging.MustGetLogger("peer")
4344

4445
type chainSupport struct {
45-
configtx.Manager
46+
configtxapi.Manager
4647
sharedconfig.Descriptor
4748
ledger ledger.PeerLedger
4849
}
@@ -163,7 +164,7 @@ func createChain(cid string, ledger ledger.PeerLedger, cb *common.Block) error {
163164

164165
gossipEventer := service.GetGossipService().NewConfigEventer()
165166

166-
gossipCallbackWrapper := func(cm configtx.Manager) {
167+
gossipCallbackWrapper := func(cm configtxapi.Manager) {
167168
gossipEventer.ProcessConfigUpdate(&chainSupport{
168169
Manager: cm,
169170
Descriptor: sharedConfigHandler,
@@ -175,7 +176,7 @@ func createChain(cid string, ledger ledger.PeerLedger, cb *common.Block) error {
175176
configtxManager, err := configtx.NewManagerImplNext(
176177
configEnvelope,
177178
configtxInitializer,
178-
[]func(cm configtx.Manager){gossipCallbackWrapper},
179+
[]func(cm configtxapi.Manager){gossipCallbackWrapper},
179180
)
180181
if err != nil {
181182
return err

msp/mgmt/mspconfigmgr_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"testing"
2121

2222
"github.com/golang/protobuf/proto"
23-
"github.com/hyperledger/fabric/common/configtx"
23+
configtxapi "github.com/hyperledger/fabric/common/configtx/api"
2424
"github.com/hyperledger/fabric/msp"
2525
. "github.com/hyperledger/fabric/msp/mgmt"
2626
"github.com/hyperledger/fabric/protos/common"
@@ -39,7 +39,7 @@ func TestMSPConfigManager(t *testing.T) {
3939
// test success:
4040

4141
// begin/propose/commit
42-
var mspCH configtx.Handler
42+
var mspCH configtxapi.Handler
4343
mspCH = &MSPConfigHandler{}
4444
mspCH.BeginConfig()
4545
err = mspCH.ProposeConfig(ci)

0 commit comments

Comments
 (0)