Skip to content

Commit

Permalink
[FAB-5648] Extract Initializer from configtx
Browse files Browse the repository at this point in the history
The configtx code depends upon an initializer, which provides resources
to the configtx code and is embedded to supply those resources to other
components.

At the time it was written, there was only one non-mock initializer in
the system, so it was stored within the configtx package.  However,
preparing to support other configtx processing, this code which is
dependent on the channel config must be removed from the configtx
package.

This CR simply moves the initializer code from configtx to
config/channel.

Change-Id: I03c91e68f39285bcd461eefec86d8943026de397
Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Aug 11, 2017
1 parent 58ddd21 commit edf43f7
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package configtx
package config

import (
"fmt"

"github.com/hyperledger/fabric/common/cauthdsl"
"github.com/hyperledger/fabric/common/config"
channelconfig "github.com/hyperledger/fabric/common/config/channel"
configtxmsp "github.com/hyperledger/fabric/common/config/channel/msp"
"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"

"github.com/golang/protobuf/proto"
)

const RootGroupKey = "Channel"

type resources struct {
policyManager *policies.ManagerImpl
configRoot *channelconfig.Root
configRoot *Root
mspConfigHandler *configtxmsp.MSPConfigHandler
}

Expand All @@ -43,12 +43,12 @@ func (r *resources) PolicyManager() policies.Manager {
}

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

// OrdererConfig returns the api.OrdererConfig for the chain
func (r *resources) OrdererConfig() (channelconfig.Orderer, bool) {
func (r *resources) OrdererConfig() (Orderer, bool) {
result := r.configRoot.Orderer()
if result == nil {
return nil, false
Expand All @@ -57,7 +57,7 @@ func (r *resources) OrdererConfig() (channelconfig.Orderer, bool) {
}

// ApplicationConfig returns the api.ApplicationConfig for the chain
func (r *resources) ApplicationConfig() (channelconfig.Application, bool) {
func (r *resources) ApplicationConfig() (Application, bool) {
result := r.configRoot.Application()
if result == nil {
return nil, false
Expand All @@ -67,7 +67,7 @@ func (r *resources) ApplicationConfig() (channelconfig.Application, bool) {

// ConsortiumsConfig returns the api.ConsortiumsConfig for the chain and whether or not
// this channel contains consortiums config
func (r *resources) ConsortiumsConfig() (channelconfig.Consortiums, bool) {
func (r *resources) ConsortiumsConfig() (Consortiums, bool) {
result := r.configRoot.Consortiums()
if result == nil {
return nil, false
Expand Down Expand Up @@ -99,7 +99,7 @@ func newResources() *resources {

return &resources{
policyManager: policies.NewManagerImpl(RootGroupKey, policyProviderMap),
configRoot: channelconfig.NewRoot(mspConfigHandler),
configRoot: NewRoot(mspConfigHandler),
mspConfigHandler: mspConfigHandler,
}
}
Expand Down Expand Up @@ -131,26 +131,30 @@ func (i *policyProposerRoot) RollbackProposals(tx interface{}) {}
// CommitConfig is used to commit a new config proposal
func (i *policyProposerRoot) CommitProposals(tx interface{}) {}

type initializer struct {
type Initializer struct {
*resources
ppr *policyProposerRoot
}

// NewInitializer creates a chain initializer for the basic set of common chain resources
func NewInitializer() api.Initializer {
// NewInitializer creates a chain Initializer for the basic set of common chain resources
func NewInitializer() *Initializer {
resources := newResources()
return &initializer{
return &Initializer{
resources: resources,
ppr: &policyProposerRoot{
policyManager: resources.policyManager,
},
}
}

func (i *initializer) PolicyProposer() policies.Proposer {
func (i *Initializer) RootGroupKey() string {
return RootGroupKey
}

func (i *Initializer) PolicyProposer() policies.Proposer {
return i.ppr
}

func (i *initializer) ValueProposer() config.ValueProposer {
func (i *Initializer) ValueProposer() config.ValueProposer {
return i.resources.configRoot
}
3 changes: 3 additions & 0 deletions common/configtx/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ type Proposer interface {

// PolicyProposer return the root policy proposer
PolicyProposer() policies.Proposer

// RootGroupKey is the string to use to namespace the root group
RootGroupKey() string
}

// Initializer is used as indirection between Manager and Handler to allow
Expand Down
2 changes: 1 addition & 1 deletion common/configtx/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func proposeGroup(result *configResult) error {

func processConfig(channelGroup *cb.ConfigGroup, proposer api.Proposer) (*configResult, error) {
helperGroup := cb.NewConfigGroup()
helperGroup.Groups[RootGroupKey] = channelGroup
helperGroup.Groups[proposer.RootGroupKey()] = channelGroup

configResult := &configResult{
group: helperGroup,
Expand Down
10 changes: 4 additions & 6 deletions common/configtx/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ import (
)

const (
RootGroupKey = "Channel"

GroupPrefix = "[Groups] "
ValuePrefix = "[Values] "
PolicyPrefix = "[Policy] " // The plurarility doesn't match, but, it makes the logs much easier being the same length as "Groups" and "Values"
Expand All @@ -37,10 +35,10 @@ const (

// MapConfig is intended to be called outside this file
// it takes a ConfigGroup and generates a map of fqPath to comparables (or error on invalid keys)
func MapConfig(channelGroup *cb.ConfigGroup) (map[string]comparable, error) {
func MapConfig(channelGroup *cb.ConfigGroup, rootGroupKey string) (map[string]comparable, error) {
result := make(map[string]comparable)
if channelGroup != nil {
err := recurseConfig(result, []string{RootGroupKey}, channelGroup)
err := recurseConfig(result, []string{rootGroupKey}, channelGroup)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -110,8 +108,8 @@ func recurseConfig(result map[string]comparable, path []string, group *cb.Config

// configMapToConfig is intended to be called from outside this file
// It takes a configMap and converts it back into a *cb.ConfigGroup structure
func configMapToConfig(configMap map[string]comparable) (*cb.ConfigGroup, error) {
rootPath := PathSeparator + RootGroupKey
func configMapToConfig(configMap map[string]comparable, rootGroupKey string) (*cb.ConfigGroup, error) {
rootPath := PathSeparator + rootGroupKey
return recurseConfigMap(rootPath, configMap)
}

Expand Down
8 changes: 4 additions & 4 deletions common/configtx/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestConfigMapMultiGroup(t *testing.T) {
config.Groups["0"].Groups["1"].Groups["2.2"] = cb.NewConfigGroup()
config.Groups["0"].Groups["1"].Groups["2.2"].Values["Value"] = &cb.ConfigValue{}

confMap, err := MapConfig(config)
confMap, err := MapConfig(config, "Channel")
assert.NoError(t, err)
assert.Equal(t, []string{"Channel", "0", "1", "2.1"}, confMap["[Values] /Channel/0/1/2.1/Value"].path)
assert.Equal(t, []string{"Channel", "0", "1", "2.2"}, confMap["[Values] /Channel/0/1/2.2/Value"].path)
Expand All @@ -58,7 +58,7 @@ func TestConfigMap(t *testing.T) {
config.Groups["0DeepGroup"].Groups["1DeepGroup"] = cb.NewConfigGroup()
config.Groups["0DeepGroup"].Groups["1DeepGroup"].Values["2DeepValue"] = &cb.ConfigValue{}

confMap, err := MapConfig(config)
confMap, err := MapConfig(config, "Channel")
assert.NoError(t, err, "Should not have errored building map")

assert.Len(t, confMap, 7, "There should be 7 entries in the config map")
Expand Down Expand Up @@ -88,10 +88,10 @@ func TestMapConfigBack(t *testing.T) {
config.Groups["0DeepGroup"].Groups["1DeepGroup"] = cb.NewConfigGroup()
config.Groups["0DeepGroup"].Groups["1DeepGroup"].Values["2DeepValue"] = &cb.ConfigValue{}

confMap, err := MapConfig(config)
confMap, err := MapConfig(config, "Channel")
assert.NoError(t, err, "Should not have errored building map")

newConfig, err := configMapToConfig(confMap)
newConfig, err := configMapToConfig(confMap, "Channel")
assert.NoError(t, err, "Should not have errored building config")

assert.Equal(t, config, newConfig, "Should have transformed config map back from confMap")
Expand Down
8 changes: 4 additions & 4 deletions common/configtx/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func NewManagerImpl(envConfig *cb.Envelope, initializer api.Initializer, callOnU
return nil, fmt.Errorf("Bad channel id: %s", err)
}

configMap, err := MapConfig(configEnv.Config.ChannelGroup)
configMap, err := MapConfig(configEnv.Config.ChannelGroup, initializer.RootGroupKey())
if err != nil {
return nil, fmt.Errorf("Error converting config to map: %s", err)
}
Expand Down Expand Up @@ -184,7 +184,7 @@ func (cm *configManager) proposeConfigUpdate(configtx *cb.Envelope) (*cb.ConfigE
return nil, fmt.Errorf("Error authorizing update: %s", err)
}

channelGroup, err := configMapToConfig(configMap)
channelGroup, err := configMapToConfig(configMap, cm.initializer.RootGroupKey())
if err != nil {
return nil, fmt.Errorf("Could not turn configMap back to channelGroup: %s", err)
}
Expand Down Expand Up @@ -228,7 +228,7 @@ func (cm *configManager) prepareApply(configEnv *cb.ConfigEnvelope) (*configResu
return nil, err
}

channelGroup, err := configMapToConfig(configMap)
channelGroup, err := configMapToConfig(configMap, cm.initializer.RootGroupKey())
if err != nil {
return nil, fmt.Errorf("Could not turn configMap back to channelGroup: %s", err)
}
Expand Down Expand Up @@ -265,7 +265,7 @@ func (cm *configManager) Apply(configEnv *cb.ConfigEnvelope) error {
// elements from a config graph perspective. Therefore, it is not safe to use
// as the config map after application. Instead, we compute the config map
// just like we would at startup.
configMap, err := MapConfig(configEnv.Config.ChannelGroup)
configMap, err := MapConfig(configEnv.Config.ChannelGroup, cm.initializer.RootGroupKey())
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions common/configtx/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func defaultInitializer() *mockconfigtx.Initializer {
ValueProposerVal: &mockconfigtx.ValueProposer{
Transactional: mockconfigtx.Transactional{},
},
RootGroupKeyVal: "foo",
}
}

Expand Down
4 changes: 2 additions & 2 deletions common/configtx/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func (cm *configManager) authorizeUpdate(configUpdateEnv *cb.ConfigUpdateEnvelop
return nil, fmt.Errorf("Update not for correct channel: %s for %s", configUpdate.ChannelId, cm.current.channelID)
}

readSet, err := MapConfig(configUpdate.ReadSet)
readSet, err := MapConfig(configUpdate.ReadSet, cm.initializer.RootGroupKey())
if err != nil {
return nil, fmt.Errorf("Error mapping ReadSet: %s", err)
}
Expand All @@ -145,7 +145,7 @@ func (cm *configManager) authorizeUpdate(configUpdateEnv *cb.ConfigUpdateEnvelop
return nil, fmt.Errorf("Error validating ReadSet: %s", err)
}

writeSet, err := MapConfig(configUpdate.WriteSet)
writeSet, err := MapConfig(configUpdate.WriteSet, cm.initializer.RootGroupKey())
if err != nil {
return nil, fmt.Errorf("Error mapping WriteSet: %s", err)
}
Expand Down
8 changes: 8 additions & 0 deletions common/mocks/configtx/configtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ type Initializer struct {

// ValueProposerVal is returned by ValueProposers
ValueProposerVal *ValueProposer

// RootGroupKeyVal is returned by RootGroupKey
RootGroupKeyVal string
}

// RootGroupKeyreturns RootGroupKeyVal
func (i *Initializer) RootGroupKey() string {
return i.RootGroupKeyVal
}

// PolicyProposers returns PolicyProposerVal
Expand Down
3 changes: 2 additions & 1 deletion common/tools/configtxlator/sanitycheck/sanitycheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package sanitycheck
import (
"fmt"

channelconfig "github.com/hyperledger/fabric/common/config/channel"
"github.com/hyperledger/fabric/common/configtx"
cb "github.com/hyperledger/fabric/protos/common"
mspprotos "github.com/hyperledger/fabric/protos/msp"
Expand Down Expand Up @@ -46,7 +47,7 @@ func Check(config *cb.Config) (*Messages, error) {

result := &Messages{}

cm, err := configtx.NewManagerImpl(envConfig, configtx.NewInitializer(), nil)
cm, err := configtx.NewManagerImpl(envConfig, channelconfig.NewInitializer(), nil)
if err != nil {
result.GeneralErrors = []string{err.Error()}
return result, nil
Expand Down
2 changes: 1 addition & 1 deletion core/peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func createChain(cid string, ledger ledger.PeerLedger, cb *common.Block) error {
return err
}

configtxInitializer := configtx.NewInitializer()
configtxInitializer := config.NewInitializer()

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

Expand Down
2 changes: 1 addition & 1 deletion orderer/common/msgprocessor/systemchannel.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (dt *DefaultTemplator) NewChannelConfig(envConfigUpdate *cb.Envelope) (conf
},
}, msgVersion, epoch)

initializer := configtx.NewInitializer()
initializer := config.NewInitializer()

// This is a very hacky way to disable the sanity check logging in the policy manager
// for the template configuration, but it is the least invasive near a release
Expand Down
2 changes: 1 addition & 1 deletion orderer/common/msgprocessor/systemchannel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (mdts *mockDefaultTemplatorSupport) Signer() crypto.LocalSigner {

func TestNewChannelConfig(t *testing.T) {
singleMSPGenesisBlock := provisional.New(genesisconfig.Load(genesisconfig.SampleSingleMSPSoloProfile)).GenesisBlock()
ctxm, err := configtx.NewManagerImpl(utils.ExtractEnvelopeOrPanic(singleMSPGenesisBlock, 0), configtx.NewInitializer(), nil)
ctxm, err := configtx.NewManagerImpl(utils.ExtractEnvelopeOrPanic(singleMSPGenesisBlock, 0), config.NewInitializer(), nil)
assert.Nil(t, err)

templator := NewDefaultTemplator(&mockDefaultTemplatorSupport{
Expand Down
2 changes: 1 addition & 1 deletion orderer/common/msgprocessor/systemchannelfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (scf *SystemChainFilter) authorizeAndInspect(configTx *cb.Envelope) error {
return err
}

initializer := configtx.NewInitializer()
initializer := config.NewInitializer()
configManager, err := configtx.NewManagerImpl(configTx, initializer, nil)
if err != nil {
return fmt.Errorf("failed to create config manager and handlers: %s", err)
Expand Down
2 changes: 1 addition & 1 deletion orderer/common/multichannel/registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (r *Registrar) GetChain(chainID string) (*ChainSupport, bool) {
}

func (r *Registrar) newLedgerResources(configTx *cb.Envelope) *ledgerResources {
initializer := configtx.NewInitializer()
initializer := config.NewInitializer()
configManager, err := configtx.NewManagerImpl(configTx, initializer, nil)
if err != nil {
logger.Panicf("Error creating configtx manager and handlers: %s", err)
Expand Down
3 changes: 2 additions & 1 deletion peer/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"os"

"github.com/hyperledger/fabric/bccsp/factory"
channelconfig "github.com/hyperledger/fabric/common/config/channel"
"github.com/hyperledger/fabric/common/configtx"
configtxapi "github.com/hyperledger/fabric/common/configtx/api"
"github.com/hyperledger/fabric/common/errors"
Expand Down Expand Up @@ -191,7 +192,7 @@ func GetOrdererEndpointOfChain(chainID string, signer msp.SigningIdentity, endor
if err != nil {
return nil, fmt.Errorf("Error extracting config block envelope: %s", err)
}
configtxInitializer := configtx.NewInitializer()
configtxInitializer := channelconfig.NewInitializer()
configtxManager, err := configtx.NewManagerImpl(
envelopeConfig,
configtxInitializer,
Expand Down

0 comments on commit edf43f7

Please sign in to comment.