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