github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/common/configtx/initializer.go (about) 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 configtx 18 19 import ( 20 "fmt" 21 22 "github.com/hyperledger/fabric/common/cauthdsl" 23 "github.com/hyperledger/fabric/common/config" 24 configtxmsp "github.com/hyperledger/fabric/common/config/msp" 25 "github.com/hyperledger/fabric/common/configtx/api" 26 "github.com/hyperledger/fabric/common/policies" 27 "github.com/hyperledger/fabric/msp" 28 cb "github.com/hyperledger/fabric/protos/common" 29 30 "github.com/golang/protobuf/proto" 31 ) 32 33 type resources struct { 34 policyManager *policies.ManagerImpl 35 configRoot *config.Root 36 mspConfigHandler *configtxmsp.MSPConfigHandler 37 } 38 39 // PolicyManager returns the policies.Manager for the chain 40 func (r *resources) PolicyManager() policies.Manager { 41 return r.policyManager 42 } 43 44 // ChannelConfig returns the api.ChannelConfig for the chain 45 func (r *resources) ChannelConfig() config.Channel { 46 return r.configRoot.Channel() 47 } 48 49 // OrdererConfig returns the api.OrdererConfig for the chain 50 func (r *resources) OrdererConfig() config.Orderer { 51 return r.configRoot.Orderer() 52 } 53 54 // ApplicationConfig returns the api.ApplicationConfig for the chain 55 func (r *resources) ApplicationConfig() config.Application { 56 return r.configRoot.Application() 57 } 58 59 // ConsortiumsConfig returns the api.ConsortiumsConfig for the chain 60 func (r *resources) ConsortiumsConfig() config.Consortiums { 61 return r.configRoot.Consortiums() 62 } 63 64 // MSPManager returns the msp.MSPManager for the chain 65 func (r *resources) MSPManager() msp.MSPManager { 66 return r.mspConfigHandler 67 } 68 69 func newResources() *resources { 70 mspConfigHandler := configtxmsp.NewMSPConfigHandler() 71 72 policyProviderMap := make(map[int32]policies.Provider) 73 for pType := range cb.Policy_PolicyType_name { 74 rtype := cb.Policy_PolicyType(pType) 75 switch rtype { 76 case cb.Policy_UNKNOWN: 77 // Do not register a handler 78 case cb.Policy_SIGNATURE: 79 policyProviderMap[pType] = cauthdsl.NewPolicyProvider(mspConfigHandler) 80 case cb.Policy_MSP: 81 // Add hook for MSP Handler here 82 } 83 } 84 85 return &resources{ 86 policyManager: policies.NewManagerImpl(RootGroupKey, policyProviderMap), 87 configRoot: config.NewRoot(mspConfigHandler), 88 mspConfigHandler: mspConfigHandler, 89 } 90 } 91 92 type policyProposerRoot struct { 93 policyManager policies.Proposer 94 } 95 96 // BeginPolicyProposals is used to start a new config proposal 97 func (p *policyProposerRoot) BeginPolicyProposals(tx interface{}, groups []string) ([]policies.Proposer, error) { 98 if len(groups) != 1 { 99 logger.Panicf("Initializer only supports having one root group") 100 } 101 return []policies.Proposer{p.policyManager}, nil 102 } 103 104 func (i *policyProposerRoot) ProposePolicy(tx interface{}, key string, policy *cb.ConfigPolicy) (proto.Message, error) { 105 return nil, fmt.Errorf("Programming error, this should never be invoked") 106 } 107 108 // PreCommit is a no-op and returns nil 109 func (i *policyProposerRoot) PreCommit(tx interface{}) error { 110 return nil 111 } 112 113 // RollbackConfig is used to abandon a new config proposal 114 func (i *policyProposerRoot) RollbackProposals(tx interface{}) {} 115 116 // CommitConfig is used to commit a new config proposal 117 func (i *policyProposerRoot) CommitProposals(tx interface{}) {} 118 119 type initializer struct { 120 *resources 121 ppr *policyProposerRoot 122 } 123 124 // NewInitializer creates a chain initializer for the basic set of common chain resources 125 func NewInitializer() api.Initializer { 126 resources := newResources() 127 return &initializer{ 128 resources: resources, 129 ppr: &policyProposerRoot{ 130 policyManager: resources.policyManager, 131 }, 132 } 133 } 134 135 func (i *initializer) PolicyProposer() policies.Proposer { 136 return i.ppr 137 } 138 139 func (i *initializer) ValueProposer() config.ValueProposer { 140 return i.resources.configRoot 141 }