github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/common/configtx/test/helper.go (about)

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