github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/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/config"
    24  	configtxmsp "github.com/hyperledger/fabric/common/config/msp"
    25  	"github.com/hyperledger/fabric/common/configtx/api"
    26  	"github.com/hyperledger/fabric/common/policies"
    27  	"github.com/hyperledger/fabric/msp"
    28  	cb "github.com/hyperledger/fabric/protos/common"
    29  
    30  	"github.com/golang/protobuf/proto"
    31  )
    32  
    33  type resources struct {
    34  	policyManager    *policies.ManagerImpl
    35  	configRoot       *config.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() config.Channel {
    46  	return r.configRoot.Channel()
    47  }
    48  
    49  // OrdererConfig returns the api.OrdererConfig for the chain
    50  func (r *resources) OrdererConfig() (config.Orderer, bool) {
    51  	result := r.configRoot.Orderer()
    52  	if result == nil {
    53  		return nil, false
    54  	}
    55  	return result, true
    56  }
    57  
    58  // ApplicationConfig returns the api.ApplicationConfig for the chain
    59  func (r *resources) ApplicationConfig() (config.Application, bool) {
    60  	result := r.configRoot.Application()
    61  	if result == nil {
    62  		return nil, false
    63  	}
    64  	return result, true
    65  }
    66  
    67  // ConsortiumsConfig returns the api.ConsortiumsConfig for the chain and whether or not
    68  // this channel contains consortiums config
    69  func (r *resources) ConsortiumsConfig() (config.Consortiums, bool) {
    70  	result := r.configRoot.Consortiums()
    71  	if result == nil {
    72  		return nil, false
    73  	}
    74  
    75  	return result, true
    76  }
    77  
    78  // MSPManager returns the msp.MSPManager for the chain
    79  func (r *resources) MSPManager() msp.MSPManager {
    80  	return r.mspConfigHandler
    81  }
    82  
    83  func newResources() *resources {
    84  	mspConfigHandler := configtxmsp.NewMSPConfigHandler()
    85  
    86  	policyProviderMap := make(map[int32]policies.Provider)
    87  	for pType := range cb.Policy_PolicyType_name {
    88  		rtype := cb.Policy_PolicyType(pType)
    89  		switch rtype {
    90  		case cb.Policy_UNKNOWN:
    91  			// Do not register a handler
    92  		case cb.Policy_SIGNATURE:
    93  			policyProviderMap[pType] = cauthdsl.NewPolicyProvider(mspConfigHandler)
    94  		case cb.Policy_MSP:
    95  			// Add hook for MSP Handler here
    96  		}
    97  	}
    98  
    99  	return &resources{
   100  		policyManager:    policies.NewManagerImpl(RootGroupKey, policyProviderMap),
   101  		configRoot:       config.NewRoot(mspConfigHandler),
   102  		mspConfigHandler: mspConfigHandler,
   103  	}
   104  }
   105  
   106  type policyProposerRoot struct {
   107  	policyManager policies.Proposer
   108  }
   109  
   110  // BeginPolicyProposals is used to start a new config proposal
   111  func (p *policyProposerRoot) BeginPolicyProposals(tx interface{}, groups []string) ([]policies.Proposer, error) {
   112  	if len(groups) != 1 {
   113  		logger.Panicf("Initializer only supports having one root group")
   114  	}
   115  	return []policies.Proposer{p.policyManager}, nil
   116  }
   117  
   118  func (i *policyProposerRoot) ProposePolicy(tx interface{}, key string, policy *cb.ConfigPolicy) (proto.Message, error) {
   119  	return nil, fmt.Errorf("Programming error, this should never be invoked")
   120  }
   121  
   122  // PreCommit is a no-op and returns nil
   123  func (i *policyProposerRoot) PreCommit(tx interface{}) error {
   124  	return nil
   125  }
   126  
   127  // RollbackConfig is used to abandon a new config proposal
   128  func (i *policyProposerRoot) RollbackProposals(tx interface{}) {}
   129  
   130  // CommitConfig is used to commit a new config proposal
   131  func (i *policyProposerRoot) CommitProposals(tx interface{}) {}
   132  
   133  type initializer struct {
   134  	*resources
   135  	ppr *policyProposerRoot
   136  }
   137  
   138  // NewInitializer creates a chain initializer for the basic set of common chain resources
   139  func NewInitializer() api.Initializer {
   140  	resources := newResources()
   141  	return &initializer{
   142  		resources: resources,
   143  		ppr: &policyProposerRoot{
   144  			policyManager: resources.policyManager,
   145  		},
   146  	}
   147  }
   148  
   149  func (i *initializer) PolicyProposer() policies.Proposer {
   150  	return i.ppr
   151  }
   152  
   153  func (i *initializer) ValueProposer() config.ValueProposer {
   154  	return i.resources.configRoot
   155  }