github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/common/config/consortium.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 config 18 19 import ( 20 "fmt" 21 22 "github.com/hyperledger/fabric/common/config/msp" 23 cb "github.com/hyperledger/fabric/protos/common" 24 ) 25 26 // ConsortiumProtos holds the config protos for the consortium config 27 type ConsortiumProtos struct { 28 ChannelCreationPolicy *cb.Policy 29 } 30 31 // ConsortiumGroup stores the set of Consortium 32 type ConsortiumGroup struct { 33 *Proposer 34 *ConsortiumConfig 35 36 mspConfig *msp.MSPConfigHandler 37 } 38 39 // NewConsortiumGroup creates a new *ConsortiumGroup 40 func NewConsortiumGroup(mspConfig *msp.MSPConfigHandler) *ConsortiumGroup { 41 cg := &ConsortiumGroup{ 42 mspConfig: mspConfig, 43 } 44 cg.Proposer = NewProposer(cg) 45 return cg 46 } 47 48 // NewGroup returns a Consortium instance 49 func (cg *ConsortiumGroup) NewGroup(name string) (ValueProposer, error) { 50 return NewOrganizationGroup(name, cg.mspConfig), nil 51 } 52 53 // Allocate returns the resources for a new config proposal 54 func (cg *ConsortiumGroup) Allocate() Values { 55 return NewConsortiumConfig(cg) 56 } 57 58 // BeginValueProposals calls through to Proposer after calling into the MSP config Handler 59 func (cg *ConsortiumGroup) BeginValueProposals(tx interface{}, groups []string) (ValueDeserializer, []ValueProposer, error) { 60 return cg.Proposer.BeginValueProposals(tx, groups) 61 } 62 63 // PreCommit intercepts the precommit request and commits the MSP config handler before calling the underlying proposer 64 func (cg *ConsortiumGroup) PreCommit(tx interface{}) error { 65 return cg.Proposer.PreCommit(tx) 66 } 67 68 // RollbackProposals intercepts the rollback request and commits the MSP config handler before calling the underlying proposer 69 func (cg *ConsortiumGroup) RollbackProposals(tx interface{}) { 70 cg.Proposer.RollbackProposals(tx) 71 } 72 73 // CommitProposals intercepts the commit request and commits the MSP config handler before calling the underlying proposer 74 func (cg *ConsortiumGroup) CommitProposals(tx interface{}) { 75 cg.Proposer.CommitProposals(tx) 76 } 77 78 // ConsortiumConfig holds the consoritums configuration information 79 type ConsortiumConfig struct { 80 *standardValues 81 protos *ConsortiumProtos 82 orgs map[string]*OrganizationGroup 83 84 consortiumGroup *ConsortiumGroup 85 } 86 87 // NewConsortiumConfig creates a new instance of the consoritums config 88 func NewConsortiumConfig(cg *ConsortiumGroup) *ConsortiumConfig { 89 cc := &ConsortiumConfig{ 90 protos: &ConsortiumProtos{}, 91 orgs: make(map[string]*OrganizationGroup), 92 consortiumGroup: cg, 93 } 94 var err error 95 cc.standardValues, err = NewStandardValues(cc.protos) 96 if err != nil { 97 logger.Panicf("Programming error: %s", err) 98 } 99 return cc 100 } 101 102 // Organizations returns the set of organizations in the consortium 103 func (cc *ConsortiumConfig) Organizations() map[string]*OrganizationGroup { 104 return cc.orgs 105 } 106 107 // CreationPolicy returns the policy structure used to validate 108 // the channel creation 109 func (cc *ConsortiumConfig) ChannelCreationPolicy() *cb.Policy { 110 return cc.protos.ChannelCreationPolicy 111 } 112 113 // Commit commits the ConsortiumConfig 114 func (cc *ConsortiumConfig) Commit() { 115 cc.consortiumGroup.ConsortiumConfig = cc 116 } 117 118 // Validate builds the Consortium map 119 func (cc *ConsortiumConfig) Validate(tx interface{}, groups map[string]ValueProposer) error { 120 var ok bool 121 for key, group := range groups { 122 cc.orgs[key], ok = group.(*OrganizationGroup) 123 if !ok { 124 return fmt.Errorf("Unexpected group type: %T", group) 125 } 126 } 127 return nil 128 }