github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/common/configtx/initializer.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 configtx
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/hyperledger/fabric/common/cauthdsl"
    23  	"github.com/hyperledger/fabric/common/configtx/api"
    24  	configvaluesapi "github.com/hyperledger/fabric/common/configvalues"
    25  	configvalueschannel "github.com/hyperledger/fabric/common/configvalues/channel"
    26  	configtxmsp "github.com/hyperledger/fabric/common/configvalues/msp"
    27  	configvaluesroot "github.com/hyperledger/fabric/common/configvalues/root"
    28  	"github.com/hyperledger/fabric/common/policies"
    29  	"github.com/hyperledger/fabric/msp"
    30  	cb "github.com/hyperledger/fabric/protos/common"
    31  )
    32  
    33  type resources struct {
    34  	policyManager    *policies.ManagerImpl
    35  	configRoot       *configvaluesroot.Root
    36  	mspConfigHandler *configtxmsp.MSPConfigHandler
    37  }
    38  
    39  // PolicyManager returns the policies.Manager for the chain
    40  func (r *resources) PolicyManager() policies.Manager {
    41  	return r.policyManager
    42  }
    43  
    44  // ChannelConfig returns the api.ChannelConfig for the chain
    45  func (r *resources) ChannelConfig() configvalueschannel.ConfigReader {
    46  	return r.configRoot.Channel()
    47  }
    48  
    49  // OrdererConfig returns the api.OrdererConfig for the chain
    50  func (r *resources) OrdererConfig() configvaluesapi.Orderer {
    51  	return r.configRoot.Orderer()
    52  }
    53  
    54  // ApplicationConfig returns the api.ApplicationConfig for the chain
    55  func (r *resources) ApplicationConfig() configvaluesapi.Application {
    56  	return r.configRoot.Application()
    57  }
    58  
    59  // MSPManager returns the msp.MSPManager for the chain
    60  func (r *resources) MSPManager() msp.MSPManager {
    61  	return r.mspConfigHandler
    62  }
    63  
    64  func newResources() *resources {
    65  	mspConfigHandler := &configtxmsp.MSPConfigHandler{}
    66  
    67  	policyProviderMap := make(map[int32]policies.Provider)
    68  	for pType := range cb.Policy_PolicyType_name {
    69  		rtype := cb.Policy_PolicyType(pType)
    70  		switch rtype {
    71  		case cb.Policy_UNKNOWN:
    72  			// Do not register a handler
    73  		case cb.Policy_SIGNATURE:
    74  			policyProviderMap[pType] = cauthdsl.NewPolicyProvider(mspConfigHandler)
    75  		case cb.Policy_MSP:
    76  			// Add hook for MSP Handler here
    77  		}
    78  	}
    79  
    80  	return &resources{
    81  		policyManager:    policies.NewManagerImpl(RootGroupKey, policyProviderMap),
    82  		configRoot:       configvaluesroot.NewRoot(mspConfigHandler),
    83  		mspConfigHandler: mspConfigHandler,
    84  	}
    85  }
    86  
    87  type policyProposerRoot struct {
    88  	policyManager policies.Proposer
    89  }
    90  
    91  // BeginPolicyProposals is used to start a new config proposal
    92  func (p *policyProposerRoot) BeginPolicyProposals(groups []string) ([]policies.Proposer, error) {
    93  	if len(groups) != 1 {
    94  		logger.Panicf("Initializer only supports having one root group")
    95  	}
    96  	return []policies.Proposer{p.policyManager}, nil
    97  }
    98  
    99  func (i *policyProposerRoot) ProposePolicy(key string, policy *cb.ConfigPolicy) error {
   100  	return fmt.Errorf("Programming error, this should never be invoked")
   101  }
   102  
   103  // PreCommit is a no-op and returns nil
   104  func (i *policyProposerRoot) PreCommit() error {
   105  	return nil
   106  }
   107  
   108  // RollbackConfig is used to abandon a new config proposal
   109  func (i *policyProposerRoot) RollbackProposals() {}
   110  
   111  // CommitConfig is used to commit a new config proposal
   112  func (i *policyProposerRoot) CommitProposals() {}
   113  
   114  type initializer struct {
   115  	*resources
   116  	ppr *policyProposerRoot
   117  }
   118  
   119  // NewInitializer creates a chain initializer for the basic set of common chain resources
   120  func NewInitializer() api.Initializer {
   121  	resources := newResources()
   122  	return &initializer{
   123  		resources: resources,
   124  		ppr: &policyProposerRoot{
   125  			policyManager: resources.policyManager,
   126  		},
   127  	}
   128  }
   129  
   130  func (i *initializer) PolicyProposer() policies.Proposer {
   131  	return i.ppr
   132  }
   133  
   134  func (i *initializer) ValueProposer() configvaluesapi.ValueProposer {
   135  	return i.resources.configRoot
   136  }