Skip to content

Commit

Permalink
[FAB-2428] Move config root to configvalues
Browse files Browse the repository at this point in the history
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 <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Feb 27, 2017
1 parent 9a09ac0 commit 9379e85
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 61 deletions.
72 changes: 11 additions & 61 deletions common/configtx/initializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,17 @@ import (
"github.com/hyperledger/fabric/common/configtx/api"
configvaluesapi "github.com/hyperledger/fabric/common/configvalues"
configvalueschannel "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"
configvaluesroot "github.com/hyperledger/fabric/common/configvalues/root"
"github.com/hyperledger/fabric/common/policies"
"github.com/hyperledger/fabric/msp"
cb "github.com/hyperledger/fabric/protos/common"
)

type resources struct {
policyManager *policies.ManagerImpl
channelConfig *configvalueschannel.Config
ordererConfig *configtxorderer.ManagerImpl
applicationConfig *configtxapplication.SharedConfigImpl
mspConfigHandler *configtxmsp.MSPConfigHandler
policyManager *policies.ManagerImpl
configRoot *configvaluesroot.Root
mspConfigHandler *configtxmsp.MSPConfigHandler
}

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

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

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

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

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

ordererConfig := configtxorderer.NewManagerImpl(mspConfigHandler)
applicationConfig := configtxapplication.NewSharedConfigImpl(mspConfigHandler)

return &resources{
policyManager: policies.NewManagerImpl(RootGroupKey, policyProviderMap),
channelConfig: configvalueschannel.NewConfig(ordererConfig, applicationConfig),
ordererConfig: ordererConfig,
applicationConfig: applicationConfig,
mspConfigHandler: mspConfigHandler,
policyManager: policies.NewManagerImpl(RootGroupKey, policyProviderMap),
configRoot: configvaluesroot.NewRoot(mspConfigHandler),
mspConfigHandler: mspConfigHandler,
}
}

type valueProposerRoot struct {
channelConfig *configvalueschannel.Config
mspConfigHandler *configtxmsp.MSPConfigHandler
}

type policyProposerRoot struct {
policyManager policies.Proposer
}

// BeginValueProposals is used to start a new config proposal
func (v *valueProposerRoot) BeginValueProposals(groups []string) ([]configvaluesapi.ValueProposer, error) {
if len(groups) != 1 {
logger.Panicf("Initializer only supports having one root group")
}
logger.Debugf("Calling begin for MSP manager")
v.mspConfigHandler.BeginConfig()
return []configvaluesapi.ValueProposer{v.channelConfig}, nil
}

// RollbackConfig is used to abandon a new config proposal
func (i *valueProposerRoot) RollbackProposals() {
logger.Debugf("Calling rollback for MSP manager")
i.mspConfigHandler.RollbackProposals()
}

// PreCommit is used to verify total configuration before commit
func (i *valueProposerRoot) PreCommit() error {
logger.Debugf("Calling pre commit for MSP manager")
return i.mspConfigHandler.PreCommit()
}

// CommitConfig is used to commit a new config proposal
func (i *valueProposerRoot) CommitProposals() {
logger.Debugf("Calling commit for MSP manager")
i.mspConfigHandler.CommitProposals()
}

func (i *valueProposerRoot) ProposeValue(key string, value *cb.ConfigValue) error {
return fmt.Errorf("Programming error, this should never be invoked")
}

// BeginPolicyProposals is used to start a new config proposal
func (p *policyProposerRoot) BeginPolicyProposals(groups []string) ([]policies.Proposer, error) {
if len(groups) != 1 {
Expand All @@ -158,7 +113,6 @@ func (i *policyProposerRoot) CommitProposals() {}

type initializer struct {
*resources
vpr *valueProposerRoot
ppr *policyProposerRoot
}

Expand All @@ -167,10 +121,6 @@ func NewInitializer() api.Initializer {
resources := newResources()
return &initializer{
resources: resources,
vpr: &valueProposerRoot{
channelConfig: resources.channelConfig,
mspConfigHandler: resources.mspConfigHandler,
},
ppr: &policyProposerRoot{
policyManager: resources.policyManager,
},
Expand All @@ -182,5 +132,5 @@ func (i *initializer) PolicyProposer() policies.Proposer {
}

func (i *initializer) ValueProposer() configvaluesapi.ValueProposer {
return i.vpr
return i.resources.configRoot
}
3 changes: 3 additions & 0 deletions common/configvalues/channel/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ const (

// OrdererAddressesKey is the cb.ConfigItem type key name for the OrdererAddresses message
OrdererAddressesKey = "OrdererAddresses"

// GroupKey is the name of the channel group
GroupKey = "Channel"
)

// Hashing algorithm types
Expand Down
98 changes: 98 additions & 0 deletions common/configvalues/root/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
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 config

import (
"fmt"

configvaluesapi "github.com/hyperledger/fabric/common/configvalues"
"github.com/hyperledger/fabric/common/configvalues/channel"
"github.com/hyperledger/fabric/common/configvalues/channel/application"
"github.com/hyperledger/fabric/common/configvalues/channel/orderer"
"github.com/hyperledger/fabric/common/configvalues/msp"
cb "github.com/hyperledger/fabric/protos/common"
)

// Root acts as the object which anchors the rest of the config
// Note, yes, this is a stuttering name, but, the intent is to move
// this up one level at the end of refactoring
type Root struct {
channel *channel.Config
orderer *orderer.ManagerImpl
application *application.SharedConfigImpl
mspConfigHandler *msp.MSPConfigHandler
}

// NewRoot creates a new instance of the configvalues Root
func NewRoot(mspConfigHandler *msp.MSPConfigHandler) *Root {
ordererConfig := orderer.NewManagerImpl(mspConfigHandler)
applicationConfig := application.NewSharedConfigImpl(mspConfigHandler)

return &Root{
channel: channel.NewConfig(ordererConfig, applicationConfig),
orderer: ordererConfig,
application: applicationConfig,
mspConfigHandler: mspConfigHandler,
}
}

// BeginValueProposals is used to start a new config proposal
func (r *Root) BeginValueProposals(groups []string) ([]configvaluesapi.ValueProposer, error) {
if len(groups) != 1 {
return nil, fmt.Errorf("Root config only supports having one base group")
}
if groups[0] != channel.GroupKey {
return nil, fmt.Errorf("Root group must have channel")
}
r.mspConfigHandler.BeginConfig()
return []configvaluesapi.ValueProposer{r.channel}, nil
}

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

// PreCommit is used to verify total configuration before commit
func (r *Root) PreCommit() error {
return r.mspConfigHandler.PreCommit()
}

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

// ProposeValue should not be invoked on this object
func (r *Root) ProposeValue(key string, value *cb.ConfigValue) error {
return fmt.Errorf("Programming error, this should never be invoked")
}

// Channel returns the associated Channel level config
func (r *Root) Channel() *channel.Config {
return r.channel
}

// Orderer returns the associated Orderer level config
func (r *Root) Orderer() *orderer.ManagerImpl {
return r.orderer
}

// Application returns the associated Application level config
func (r *Root) Application() *application.SharedConfigImpl {
return r.application
}
48 changes: 48 additions & 0 deletions common/configvalues/root/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright IBM Corp. 2016 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 config

import (
"testing"

"github.com/hyperledger/fabric/common/configvalues/channel"
"github.com/hyperledger/fabric/common/configvalues/msp"

logging "github.com/op/go-logging"
"github.com/stretchr/testify/assert"
)

func init() {
logging.SetLevel(logging.DEBUG, "")
}

func TestBeginBadRoot(t *testing.T) {
r := NewRoot(&msp.MSPConfigHandler{})

_, err := r.BeginValueProposals([]string{channel.GroupKey, channel.GroupKey})
assert.Error(t, err, "Only one root element allowed")

_, err = r.BeginValueProposals([]string{"foo"})
assert.Error(t, err, "Non %s group not allowed", channel.GroupKey)
}

func TestProposeValue(t *testing.T) {
r := NewRoot(&msp.MSPConfigHandler{})

err := r.ProposeValue("foo", nil)
assert.Error(t, err, "ProposeValue should return error")
}

0 comments on commit 9379e85

Please sign in to comment.