helper.go 2.94 KB
Newer Older
1
/*
2
Copyright IBM Corp. All Rights Reserved.
3

4
SPDX-License-Identifier: Apache-2.0
5
6
7
8
9
*/

package test

import (
10
	"github.com/hyperledger/fabric/common/channelconfig"
11
	"github.com/hyperledger/fabric/common/flogging"
12
	"github.com/hyperledger/fabric/common/genesis"
13
	"github.com/hyperledger/fabric/common/tools/configtxgen/configtxgentest"
14
	"github.com/hyperledger/fabric/common/tools/configtxgen/encoder"
15
	genesisconfig "github.com/hyperledger/fabric/common/tools/configtxgen/localconfig"
16
	"github.com/hyperledger/fabric/core/ledger/util"
17
	cb "github.com/hyperledger/fabric/protos/common"
18
	mspproto "github.com/hyperledger/fabric/protos/msp"
19
	"github.com/hyperledger/fabric/protos/peer"
20
21
	pb "github.com/hyperledger/fabric/protos/peer"
	"github.com/hyperledger/fabric/protos/utils"
22
23
)

24
var logger = flogging.MustGetLogger("common/configtx/test")
25

26
27
// MakeGenesisBlock creates a genesis block using the test templates for the given chainID
func MakeGenesisBlock(chainID string) (*cb.Block, error) {
28
	profile := configtxgentest.Load(genesisconfig.SampleDevModeSoloProfile)
29
	channelGroup, err := encoder.NewChannelGroup(profile)
30
	if err != nil {
31
		logger.Panicf("Error creating channel config: %s", err)
32
	}
33

34
35
36
37
38
39
40
41
42
	gb, err := genesis.NewFactoryImpl(channelGroup).Block(chainID)
	if err != nil || gb == nil {
		return gb, err
	}

	txsFilter := util.NewTxValidationFlagsSetValue(len(gb.Data.Data), peer.TxValidationCode_VALID)
	gb.Metadata.Metadata[cb.BlockMetadataIndex_TRANSACTIONS_FILTER] = txsFilter

	return gb, nil
43
}
44

45
// MakeGenesisBlockWithMSPs creates a genesis block using the MSPs provided for the given chainID
46
47
func MakeGenesisBlockFromMSPs(chainID string, appMSPConf, ordererMSPConf *mspproto.MSPConfig, appOrgID, ordererOrgID string) (*cb.Block, error) {
	profile := configtxgentest.Load(genesisconfig.SampleDevModeSoloProfile)
48
49
	profile.Orderer.Organizations = nil
	channelGroup, err := encoder.NewChannelGroup(profile)
50
	if err != nil {
51
		logger.Panicf("Error creating channel config: %s", err)
52
	}
53

54
55
56
57
58
	ordererOrg := cb.NewConfigGroup()
	ordererOrg.ModPolicy = channelconfig.AdminsPolicyKey
	ordererOrg.Values[channelconfig.MSPKey] = &cb.ConfigValue{
		Value:     utils.MarshalOrPanic(channelconfig.MSPValue(ordererMSPConf).Value()),
		ModPolicy: channelconfig.AdminsPolicyKey,
59
	}
60

61
62
63
64
65
66
67
68
69
70
71
72
	applicationOrg := cb.NewConfigGroup()
	applicationOrg.ModPolicy = channelconfig.AdminsPolicyKey
	applicationOrg.Values[channelconfig.MSPKey] = &cb.ConfigValue{
		Value:     utils.MarshalOrPanic(channelconfig.MSPValue(appMSPConf).Value()),
		ModPolicy: channelconfig.AdminsPolicyKey,
	}
	applicationOrg.Values[channelconfig.AnchorPeersKey] = &cb.ConfigValue{
		Value:     utils.MarshalOrPanic(channelconfig.AnchorPeersValue([]*pb.AnchorPeer{}).Value()),
		ModPolicy: channelconfig.AdminsPolicyKey,
	}

	channelGroup.Groups[channelconfig.OrdererGroupKey].Groups[ordererOrgID] = ordererOrg
73
	channelGroup.Groups[channelconfig.ApplicationGroupKey].Groups[appOrgID] = applicationOrg
74
75

	return genesis.NewFactoryImpl(channelGroup).Block(chainID)
76
}