Skip to content

Commit 9379e85

Browse files
author
Jason Yellick
committed
[FAB-2428] Move config root to configvalues
https://jira.hyperledger.org/browse/FAB-2428 Presently, there is a bit of a hacky mechanism of creating a config root inside of the configtx.Initializer. This code more appropriately belongs with the configvalues package and is needed there as part of a larger refactor. Change-Id: I1df53ad3af0bff5a1ac3cce9f5ab5d22a0c7a308 Signed-off-by: Jason Yellick <[email protected]>
1 parent 9a09ac0 commit 9379e85

File tree

4 files changed

+160
-61
lines changed

4 files changed

+160
-61
lines changed

common/configtx/initializer.go

+11-61
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,17 @@ import (
2323
"github.com/hyperledger/fabric/common/configtx/api"
2424
configvaluesapi "github.com/hyperledger/fabric/common/configvalues"
2525
configvalueschannel "github.com/hyperledger/fabric/common/configvalues/channel"
26-
configtxapplication "github.com/hyperledger/fabric/common/configvalues/channel/application"
27-
configtxorderer "github.com/hyperledger/fabric/common/configvalues/channel/orderer"
2826
configtxmsp "github.com/hyperledger/fabric/common/configvalues/msp"
27+
configvaluesroot "github.com/hyperledger/fabric/common/configvalues/root"
2928
"github.com/hyperledger/fabric/common/policies"
3029
"github.com/hyperledger/fabric/msp"
3130
cb "github.com/hyperledger/fabric/protos/common"
3231
)
3332

3433
type resources struct {
35-
policyManager *policies.ManagerImpl
36-
channelConfig *configvalueschannel.Config
37-
ordererConfig *configtxorderer.ManagerImpl
38-
applicationConfig *configtxapplication.SharedConfigImpl
39-
mspConfigHandler *configtxmsp.MSPConfigHandler
34+
policyManager *policies.ManagerImpl
35+
configRoot *configvaluesroot.Root
36+
mspConfigHandler *configtxmsp.MSPConfigHandler
4037
}
4138

4239
// PolicyManager returns the policies.Manager for the chain
@@ -46,17 +43,17 @@ func (r *resources) PolicyManager() policies.Manager {
4643

4744
// ChannelConfig returns the api.ChannelConfig for the chain
4845
func (r *resources) ChannelConfig() configvalueschannel.ConfigReader {
49-
return r.channelConfig
46+
return r.configRoot.Channel()
5047
}
5148

5249
// OrdererConfig returns the api.OrdererConfig for the chain
5350
func (r *resources) OrdererConfig() configvaluesapi.Orderer {
54-
return r.ordererConfig
51+
return r.configRoot.Orderer()
5552
}
5653

5754
// ApplicationConfig returns the api.ApplicationConfig for the chain
5855
func (r *resources) ApplicationConfig() configvaluesapi.Application {
59-
return r.applicationConfig
56+
return r.configRoot.Application()
6057
}
6158

6259
// MSPManager returns the msp.MSPManager for the chain
@@ -80,59 +77,17 @@ func newResources() *resources {
8077
}
8178
}
8279

83-
ordererConfig := configtxorderer.NewManagerImpl(mspConfigHandler)
84-
applicationConfig := configtxapplication.NewSharedConfigImpl(mspConfigHandler)
85-
8680
return &resources{
87-
policyManager: policies.NewManagerImpl(RootGroupKey, policyProviderMap),
88-
channelConfig: configvalueschannel.NewConfig(ordererConfig, applicationConfig),
89-
ordererConfig: ordererConfig,
90-
applicationConfig: applicationConfig,
91-
mspConfigHandler: mspConfigHandler,
81+
policyManager: policies.NewManagerImpl(RootGroupKey, policyProviderMap),
82+
configRoot: configvaluesroot.NewRoot(mspConfigHandler),
83+
mspConfigHandler: mspConfigHandler,
9284
}
9385
}
9486

95-
type valueProposerRoot struct {
96-
channelConfig *configvalueschannel.Config
97-
mspConfigHandler *configtxmsp.MSPConfigHandler
98-
}
99-
10087
type policyProposerRoot struct {
10188
policyManager policies.Proposer
10289
}
10390

104-
// BeginValueProposals is used to start a new config proposal
105-
func (v *valueProposerRoot) BeginValueProposals(groups []string) ([]configvaluesapi.ValueProposer, error) {
106-
if len(groups) != 1 {
107-
logger.Panicf("Initializer only supports having one root group")
108-
}
109-
logger.Debugf("Calling begin for MSP manager")
110-
v.mspConfigHandler.BeginConfig()
111-
return []configvaluesapi.ValueProposer{v.channelConfig}, nil
112-
}
113-
114-
// RollbackConfig is used to abandon a new config proposal
115-
func (i *valueProposerRoot) RollbackProposals() {
116-
logger.Debugf("Calling rollback for MSP manager")
117-
i.mspConfigHandler.RollbackProposals()
118-
}
119-
120-
// PreCommit is used to verify total configuration before commit
121-
func (i *valueProposerRoot) PreCommit() error {
122-
logger.Debugf("Calling pre commit for MSP manager")
123-
return i.mspConfigHandler.PreCommit()
124-
}
125-
126-
// CommitConfig is used to commit a new config proposal
127-
func (i *valueProposerRoot) CommitProposals() {
128-
logger.Debugf("Calling commit for MSP manager")
129-
i.mspConfigHandler.CommitProposals()
130-
}
131-
132-
func (i *valueProposerRoot) ProposeValue(key string, value *cb.ConfigValue) error {
133-
return fmt.Errorf("Programming error, this should never be invoked")
134-
}
135-
13691
// BeginPolicyProposals is used to start a new config proposal
13792
func (p *policyProposerRoot) BeginPolicyProposals(groups []string) ([]policies.Proposer, error) {
13893
if len(groups) != 1 {
@@ -158,7 +113,6 @@ func (i *policyProposerRoot) CommitProposals() {}
158113

159114
type initializer struct {
160115
*resources
161-
vpr *valueProposerRoot
162116
ppr *policyProposerRoot
163117
}
164118

@@ -167,10 +121,6 @@ func NewInitializer() api.Initializer {
167121
resources := newResources()
168122
return &initializer{
169123
resources: resources,
170-
vpr: &valueProposerRoot{
171-
channelConfig: resources.channelConfig,
172-
mspConfigHandler: resources.mspConfigHandler,
173-
},
174124
ppr: &policyProposerRoot{
175125
policyManager: resources.policyManager,
176126
},
@@ -182,5 +132,5 @@ func (i *initializer) PolicyProposer() policies.Proposer {
182132
}
183133

184134
func (i *initializer) ValueProposer() configvaluesapi.ValueProposer {
185-
return i.vpr
135+
return i.resources.configRoot
186136
}

common/configvalues/channel/config.go

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ const (
5555

5656
// OrdererAddressesKey is the cb.ConfigItem type key name for the OrdererAddresses message
5757
OrdererAddressesKey = "OrdererAddresses"
58+
59+
// GroupKey is the name of the channel group
60+
GroupKey = "Channel"
5861
)
5962

6063
// Hashing algorithm types

common/configvalues/root/root.go

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 config
18+
19+
import (
20+
"fmt"
21+
22+
configvaluesapi "github.com/hyperledger/fabric/common/configvalues"
23+
"github.com/hyperledger/fabric/common/configvalues/channel"
24+
"github.com/hyperledger/fabric/common/configvalues/channel/application"
25+
"github.com/hyperledger/fabric/common/configvalues/channel/orderer"
26+
"github.com/hyperledger/fabric/common/configvalues/msp"
27+
cb "github.com/hyperledger/fabric/protos/common"
28+
)
29+
30+
// Root acts as the object which anchors the rest of the config
31+
// Note, yes, this is a stuttering name, but, the intent is to move
32+
// this up one level at the end of refactoring
33+
type Root struct {
34+
channel *channel.Config
35+
orderer *orderer.ManagerImpl
36+
application *application.SharedConfigImpl
37+
mspConfigHandler *msp.MSPConfigHandler
38+
}
39+
40+
// NewRoot creates a new instance of the configvalues Root
41+
func NewRoot(mspConfigHandler *msp.MSPConfigHandler) *Root {
42+
ordererConfig := orderer.NewManagerImpl(mspConfigHandler)
43+
applicationConfig := application.NewSharedConfigImpl(mspConfigHandler)
44+
45+
return &Root{
46+
channel: channel.NewConfig(ordererConfig, applicationConfig),
47+
orderer: ordererConfig,
48+
application: applicationConfig,
49+
mspConfigHandler: mspConfigHandler,
50+
}
51+
}
52+
53+
// BeginValueProposals is used to start a new config proposal
54+
func (r *Root) BeginValueProposals(groups []string) ([]configvaluesapi.ValueProposer, error) {
55+
if len(groups) != 1 {
56+
return nil, fmt.Errorf("Root config only supports having one base group")
57+
}
58+
if groups[0] != channel.GroupKey {
59+
return nil, fmt.Errorf("Root group must have channel")
60+
}
61+
r.mspConfigHandler.BeginConfig()
62+
return []configvaluesapi.ValueProposer{r.channel}, nil
63+
}
64+
65+
// RollbackConfig is used to abandon a new config proposal
66+
func (r *Root) RollbackProposals() {
67+
r.mspConfigHandler.RollbackProposals()
68+
}
69+
70+
// PreCommit is used to verify total configuration before commit
71+
func (r *Root) PreCommit() error {
72+
return r.mspConfigHandler.PreCommit()
73+
}
74+
75+
// CommitConfig is used to commit a new config proposal
76+
func (r *Root) CommitProposals() {
77+
r.mspConfigHandler.CommitProposals()
78+
}
79+
80+
// ProposeValue should not be invoked on this object
81+
func (r *Root) ProposeValue(key string, value *cb.ConfigValue) error {
82+
return fmt.Errorf("Programming error, this should never be invoked")
83+
}
84+
85+
// Channel returns the associated Channel level config
86+
func (r *Root) Channel() *channel.Config {
87+
return r.channel
88+
}
89+
90+
// Orderer returns the associated Orderer level config
91+
func (r *Root) Orderer() *orderer.ManagerImpl {
92+
return r.orderer
93+
}
94+
95+
// Application returns the associated Application level config
96+
func (r *Root) Application() *application.SharedConfigImpl {
97+
return r.application
98+
}

common/configvalues/root/root_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Copyright IBM Corp. 2016 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 config
18+
19+
import (
20+
"testing"
21+
22+
"github.com/hyperledger/fabric/common/configvalues/channel"
23+
"github.com/hyperledger/fabric/common/configvalues/msp"
24+
25+
logging "github.com/op/go-logging"
26+
"github.com/stretchr/testify/assert"
27+
)
28+
29+
func init() {
30+
logging.SetLevel(logging.DEBUG, "")
31+
}
32+
33+
func TestBeginBadRoot(t *testing.T) {
34+
r := NewRoot(&msp.MSPConfigHandler{})
35+
36+
_, err := r.BeginValueProposals([]string{channel.GroupKey, channel.GroupKey})
37+
assert.Error(t, err, "Only one root element allowed")
38+
39+
_, err = r.BeginValueProposals([]string{"foo"})
40+
assert.Error(t, err, "Non %s group not allowed", channel.GroupKey)
41+
}
42+
43+
func TestProposeValue(t *testing.T) {
44+
r := NewRoot(&msp.MSPConfigHandler{})
45+
46+
err := r.ProposeValue("foo", nil)
47+
assert.Error(t, err, "ProposeValue should return error")
48+
}

0 commit comments

Comments
 (0)