github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/common/configtx/config.go (about)

     1  /*
     2  Copyright IBM Corp. 2016-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/configtx/api"
    23  	configvaluesapi "github.com/hyperledger/fabric/common/configvalues"
    24  	"github.com/hyperledger/fabric/common/policies"
    25  	cb "github.com/hyperledger/fabric/protos/common"
    26  )
    27  
    28  type configResult struct {
    29  	handler       api.Transactional
    30  	policyHandler api.Transactional
    31  	subResults    []*configResult
    32  }
    33  
    34  func (cr *configResult) preCommit() error {
    35  	for _, subResult := range cr.subResults {
    36  		err := subResult.preCommit()
    37  		if err != nil {
    38  			return err
    39  		}
    40  	}
    41  	return cr.handler.PreCommit()
    42  }
    43  
    44  func (cr *configResult) commit() {
    45  	for _, subResult := range cr.subResults {
    46  		subResult.commit()
    47  	}
    48  	cr.handler.CommitProposals()
    49  	cr.policyHandler.CommitProposals()
    50  }
    51  
    52  func (cr *configResult) rollback() {
    53  	for _, subResult := range cr.subResults {
    54  		subResult.rollback()
    55  	}
    56  	cr.handler.RollbackProposals()
    57  	cr.policyHandler.RollbackProposals()
    58  }
    59  
    60  // proposeGroup proposes a group configuration with a given handler
    61  // it will in turn recursively call itself until all groups have been exhausted
    62  // at each call, it returns the handler that was passed in, plus any handlers returned
    63  // by recursive calls into proposeGroup
    64  func (cm *configManager) proposeGroup(name string, group *cb.ConfigGroup, handler configvaluesapi.ValueProposer, policyHandler policies.Proposer) (*configResult, error) {
    65  	subGroups := make([]string, len(group.Groups))
    66  	i := 0
    67  	for subGroup := range group.Groups {
    68  		subGroups[i] = subGroup
    69  		i++
    70  	}
    71  
    72  	logger.Debugf("Beginning new config for channel %s and group %s", cm.chainID, name)
    73  	subHandlers, err := handler.BeginValueProposals(subGroups)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	subPolicyHandlers, err := policyHandler.BeginPolicyProposals(subGroups)
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	if len(subHandlers) != len(subGroups) || len(subPolicyHandlers) != len(subGroups) {
    84  		return nil, fmt.Errorf("Programming error, did not return as many handlers as groups %d vs %d vs %d", len(subHandlers), len(subGroups), len(subPolicyHandlers))
    85  	}
    86  
    87  	result := &configResult{
    88  		handler:       handler,
    89  		policyHandler: policyHandler,
    90  		subResults:    make([]*configResult, 0, len(subGroups)),
    91  	}
    92  
    93  	for i, subGroup := range subGroups {
    94  		subResult, err := cm.proposeGroup(name+"/"+subGroup, group.Groups[subGroup], subHandlers[i], subPolicyHandlers[i])
    95  		if err != nil {
    96  			result.rollback()
    97  			return nil, err
    98  		}
    99  		result.subResults = append(result.subResults, subResult)
   100  	}
   101  
   102  	for key, value := range group.Values {
   103  		if err := handler.ProposeValue(key, value); err != nil {
   104  			result.rollback()
   105  			return nil, err
   106  		}
   107  	}
   108  
   109  	for key, policy := range group.Policies {
   110  		if err := policyHandler.ProposePolicy(key, policy); err != nil {
   111  			result.rollback()
   112  			return nil, err
   113  		}
   114  	}
   115  
   116  	err = result.preCommit()
   117  	if err != nil {
   118  		result.rollback()
   119  		return nil, err
   120  	}
   121  
   122  	return result, nil
   123  }
   124  
   125  func (cm *configManager) processConfig(channelGroup *cb.ConfigGroup) (*configResult, error) {
   126  	helperGroup := cb.NewConfigGroup()
   127  	helperGroup.Groups[RootGroupKey] = channelGroup
   128  	groupResult, err := cm.proposeGroup("", helperGroup, cm.initializer.ValueProposer(), cm.initializer.PolicyProposer())
   129  	if err != nil {
   130  		return nil, err
   131  	}
   132  
   133  	return groupResult, nil
   134  }