github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+incompatible/common/configtx/test/helper.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package test
     8  
     9  import (
    10  	cb "github.com/hyperledger/fabric-protos-go/common"
    11  	mspproto "github.com/hyperledger/fabric-protos-go/msp"
    12  	"github.com/hyperledger/fabric-protos-go/peer"
    13  	pb "github.com/hyperledger/fabric-protos-go/peer"
    14  	"github.com/hyperledger/fabric/common/channelconfig"
    15  	"github.com/hyperledger/fabric/common/flogging"
    16  	"github.com/hyperledger/fabric/common/genesis"
    17  	"github.com/hyperledger/fabric/core/config/configtest"
    18  	"github.com/hyperledger/fabric/core/ledger/util"
    19  	"github.com/hyperledger/fabric/internal/configtxgen/encoder"
    20  	"github.com/hyperledger/fabric/internal/configtxgen/genesisconfig"
    21  	"github.com/hyperledger/fabric/protoutil"
    22  )
    23  
    24  var logger = flogging.MustGetLogger("common.configtx.test")
    25  
    26  // MakeGenesisBlock creates a genesis block using the test templates for the given chainID
    27  func MakeGenesisBlock(chainID string) (*cb.Block, error) {
    28  	profile := genesisconfig.Load(genesisconfig.SampleDevModeSoloProfile, configtest.GetDevConfigDir())
    29  	channelGroup, err := encoder.NewChannelGroup(profile)
    30  	if err != nil {
    31  		logger.Panicf("Error creating channel config: %s", err)
    32  	}
    33  
    34  	gb := genesis.NewFactoryImpl(channelGroup).Block(chainID)
    35  	if gb == nil {
    36  		return gb, nil
    37  	}
    38  
    39  	txsFilter := util.NewTxValidationFlagsSetValue(len(gb.Data.Data), peer.TxValidationCode_VALID)
    40  	gb.Metadata.Metadata[cb.BlockMetadataIndex_TRANSACTIONS_FILTER] = txsFilter
    41  
    42  	return gb, nil
    43  }
    44  
    45  // MakeGenesisBlockWithMSPs creates a genesis block using the MSPs provided for the given chainID
    46  func MakeGenesisBlockFromMSPs(chainID string, appMSPConf, ordererMSPConf *mspproto.MSPConfig, appOrgID, ordererOrgID string) (*cb.Block, error) {
    47  	profile := genesisconfig.Load(genesisconfig.SampleDevModeSoloProfile, configtest.GetDevConfigDir())
    48  	profile.Orderer.Organizations = nil
    49  	channelGroup, err := encoder.NewChannelGroup(profile)
    50  	if err != nil {
    51  		logger.Panicf("Error creating channel config: %s", err)
    52  	}
    53  
    54  	ordererOrg := protoutil.NewConfigGroup()
    55  	ordererOrg.ModPolicy = channelconfig.AdminsPolicyKey
    56  	ordererOrg.Values[channelconfig.MSPKey] = &cb.ConfigValue{
    57  		Value:     protoutil.MarshalOrPanic(channelconfig.MSPValue(ordererMSPConf).Value()),
    58  		ModPolicy: channelconfig.AdminsPolicyKey,
    59  	}
    60  
    61  	applicationOrg := protoutil.NewConfigGroup()
    62  	applicationOrg.ModPolicy = channelconfig.AdminsPolicyKey
    63  	applicationOrg.Values[channelconfig.MSPKey] = &cb.ConfigValue{
    64  		Value:     protoutil.MarshalOrPanic(channelconfig.MSPValue(appMSPConf).Value()),
    65  		ModPolicy: channelconfig.AdminsPolicyKey,
    66  	}
    67  	applicationOrg.Values[channelconfig.AnchorPeersKey] = &cb.ConfigValue{
    68  		Value:     protoutil.MarshalOrPanic(channelconfig.AnchorPeersValue([]*pb.AnchorPeer{}).Value()),
    69  		ModPolicy: channelconfig.AdminsPolicyKey,
    70  	}
    71  
    72  	channelGroup.Groups[channelconfig.OrdererGroupKey].Groups[ordererOrgID] = ordererOrg
    73  	channelGroup.Groups[channelconfig.ApplicationGroupKey].Groups[appOrgID] = applicationOrg
    74  
    75  	return genesis.NewFactoryImpl(channelGroup).Block(chainID), nil
    76  }