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