github.com/inklabsfoundation/inkchain@v0.17.1-0.20181025012015-c3cef8062f19/common/config/consortiums.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/inklabsfoundation/inkchain/common/config/msp" 23 ) 24 25 const ( 26 // ConsortiumsGroupKey is the group name for the consortiums config 27 ConsortiumsGroupKey = "Consortiums" 28 ) 29 30 // ConsortiumsGroup stores the set of Consortiums 31 type ConsortiumsGroup struct { 32 *Proposer 33 *ConsortiumsConfig 34 35 mspConfig *msp.MSPConfigHandler 36 } 37 38 // NewConsortiumsGroup creates a new *ConsortiumsGroup 39 func NewConsortiumsGroup(mspConfig *msp.MSPConfigHandler) *ConsortiumsGroup { 40 cg := &ConsortiumsGroup{ 41 mspConfig: mspConfig, 42 } 43 cg.Proposer = NewProposer(cg) 44 return cg 45 } 46 47 // NewGroup returns a Consortium instance 48 func (cg *ConsortiumsGroup) NewGroup(name string) (ValueProposer, error) { 49 return NewConsortiumGroup(cg.mspConfig), nil 50 } 51 52 // Allocate returns the resources for a new config proposal 53 func (cg *ConsortiumsGroup) Allocate() Values { 54 return NewConsortiumsConfig(cg) 55 } 56 57 // ConsortiumsConfig holds the consoritums configuration information 58 type ConsortiumsConfig struct { 59 *standardValues 60 consortiums map[string]Consortium 61 62 consortiumsGroup *ConsortiumsGroup 63 } 64 65 // NewConsortiumsConfig creates a new instance of the consoritums config 66 func NewConsortiumsConfig(cg *ConsortiumsGroup) *ConsortiumsConfig { 67 cc := &ConsortiumsConfig{ 68 consortiums: make(map[string]Consortium), 69 consortiumsGroup: cg, 70 } 71 var err error 72 cc.standardValues, err = NewStandardValues() 73 if err != nil { 74 logger.Panicf("Programming error: %s", err) 75 } 76 return cc 77 } 78 79 // Consortiums returns a map of the current consortiums 80 func (cc *ConsortiumsConfig) Consortiums() map[string]Consortium { 81 return cc.consortiums 82 } 83 84 // Commit commits the ConsortiumsConfig 85 func (cc *ConsortiumsConfig) Commit() { 86 cc.consortiumsGroup.ConsortiumsConfig = cc 87 } 88 89 // Validate builds the Consortiums map 90 func (cc *ConsortiumsConfig) Validate(tx interface{}, groups map[string]ValueProposer) error { 91 var ok bool 92 for key, group := range groups { 93 cc.consortiums[key], ok = group.(*ConsortiumGroup) 94 if !ok { 95 return fmt.Errorf("Unexpected group type: %T", group) 96 } 97 } 98 return nil 99 }