github.com/darrenli6/fabric-sdk-example@v0.0.0-20220109053535-94b13b56df8c/common/configtx/test/helper.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 test
    18  
    19  import (
    20  	"github.com/hyperledger/fabric/common/config"
    21  	configtxmsp "github.com/hyperledger/fabric/common/config/msp"
    22  	"github.com/hyperledger/fabric/common/configtx"
    23  	genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig"
    24  	"github.com/hyperledger/fabric/common/configtx/tool/provisional"
    25  	"github.com/hyperledger/fabric/common/flogging"
    26  	"github.com/hyperledger/fabric/common/genesis"
    27  	cf "github.com/hyperledger/fabric/core/config"
    28  	"github.com/hyperledger/fabric/msp"
    29  	cb "github.com/hyperledger/fabric/protos/common"
    30  	mspproto "github.com/hyperledger/fabric/protos/msp"
    31  )
    32  
    33  var logger = flogging.MustGetLogger("common/configtx/test")
    34  
    35  const (
    36  	// AcceptAllPolicyKey is the key of the AcceptAllPolicy.
    37  	AcceptAllPolicyKey = "AcceptAllPolicy"
    38  )
    39  
    40  func getConfigDir() string {
    41  	mspDir, err := cf.GetDevMspDir()
    42  	if err != nil {
    43  		logger.Panicf("Could not find genesis.yaml, try setting GOPATH correctly")
    44  	}
    45  
    46  	return mspDir
    47  }
    48  
    49  // MakeGenesisBlock creates a genesis block using the test templates for the given chainID
    50  func MakeGenesisBlock(chainID string) (*cb.Block, error) {
    51  	return genesis.NewFactoryImpl(CompositeTemplate()).Block(chainID)
    52  }
    53  
    54  // MakeGenesisBlockWithMSPs creates a genesis block using the MSPs provided for the given chainID
    55  func MakeGenesisBlockFromMSPs(chainID string, appMSPConf, ordererMSPConf *mspproto.MSPConfig,
    56  	appOrgID, ordererOrgID string) (*cb.Block, error) {
    57  	appOrgTemplate := configtx.NewSimpleTemplate(configtxmsp.TemplateGroupMSP([]string{config.ApplicationGroupKey, appOrgID}, appMSPConf))
    58  	ordererOrgTemplate := configtx.NewSimpleTemplate(configtxmsp.TemplateGroupMSP([]string{config.OrdererGroupKey, ordererOrgID}, ordererMSPConf))
    59  	composite := configtx.NewCompositeTemplate(OrdererTemplate(), appOrgTemplate, ApplicationOrgTemplate(), ordererOrgTemplate)
    60  	return genesis.NewFactoryImpl(composite).Block(chainID)
    61  }
    62  
    63  // OrderererTemplate returns the test orderer template
    64  func OrdererTemplate() configtx.Template {
    65  	genConf := genesisconfig.Load(genesisconfig.SampleInsecureProfile)
    66  	return provisional.New(genConf).ChannelTemplate()
    67  }
    68  
    69  // sampleOrgID apparently _must_ be set to DEFAULT or things break
    70  // Beware when changing!
    71  const sampleOrgID = "DEFAULT"
    72  
    73  // ApplicationOrgTemplate returns the SAMPLE org with MSP template
    74  func ApplicationOrgTemplate() configtx.Template {
    75  	mspConf, err := msp.GetLocalMspConfig(getConfigDir(), nil, sampleOrgID)
    76  	if err != nil {
    77  		logger.Panicf("Could not load sample MSP config: %s", err)
    78  	}
    79  	return configtx.NewSimpleTemplate(configtxmsp.TemplateGroupMSP([]string{config.ApplicationGroupKey, sampleOrgID}, mspConf))
    80  }
    81  
    82  // OrdererOrgTemplate returns the SAMPLE org with MSP template
    83  func OrdererOrgTemplate() configtx.Template {
    84  	mspConf, err := msp.GetLocalMspConfig(getConfigDir(), nil, sampleOrgID)
    85  	if err != nil {
    86  		logger.Panicf("Could not load sample MSP config: %s", err)
    87  	}
    88  	return configtx.NewSimpleTemplate(configtxmsp.TemplateGroupMSP([]string{config.OrdererGroupKey, sampleOrgID}, mspConf))
    89  }
    90  
    91  // CompositeTemplate returns the composite template of peer, orderer, and MSP
    92  func CompositeTemplate() configtx.Template {
    93  	return configtx.NewCompositeTemplate(OrdererTemplate(), ApplicationOrgTemplate(), OrdererOrgTemplate())
    94  }