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