Skip to content

Commit 32b772c

Browse files
author
Jason Yellick
committed
[FAB-2169] Dynamically generate genesis material
https://jira.hyperledger.org/browse/FAB-2169 The peer tests used to rely on composited statically defined configuraiton templates. This CR switches that path to be dynamic and further prepares for the removal of ConfigurationItem. Change-Id: I0f3962c1725ce0fb8e8525622c330c1528ea6859 Signed-off-by: Jason Yellick <[email protected]>
1 parent 9f561b8 commit 32b772c

File tree

12 files changed

+106
-196
lines changed

12 files changed

+106
-196
lines changed

Makefile

+1-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ GOSHIM_DEPS = $(shell ./scripts/goListFiles.sh $(PKGNAME)/core/chaincode/shim |
6363
JAVASHIM_DEPS = $(shell git ls-files core/chaincode/shim/java)
6464
PROTOS = $(shell git ls-files *.proto | grep -v vendor)
6565
MSP_SAMPLECONFIG = $(shell git ls-files msp/sampleconfig/*.pem)
66-
GENESIS_SAMPLECONFIG = $(shell git ls-files common/configtx/test/*.template)
6766
PROJECT_FILES = $(shell git ls-files)
6867
IMAGES = peer orderer ccenv javaenv testenv zookeeper kafka
6968

@@ -176,7 +175,7 @@ build/image/javaenv/payload: build/javashim.tar.bz2 \
176175
build/image/peer/payload: build/docker/bin/peer \
177176
peer/core.yaml \
178177
build/msp-sampleconfig.tar.bz2 \
179-
build/genesis-sampleconfig.tar.bz2
178+
common/configtx/tool/genesis.yaml
180179
build/image/orderer/payload: build/docker/bin/orderer \
181180
build/msp-sampleconfig.tar.bz2 \
182181
orderer/orderer.yaml \
@@ -222,7 +221,6 @@ build/goshim.tar.bz2: $(GOSHIM_DEPS)
222221
build/javashim.tar.bz2: $(JAVASHIM_DEPS)
223222
build/protos.tar.bz2: $(PROTOS)
224223
build/msp-sampleconfig.tar.bz2: $(MSP_SAMPLECONFIG)
225-
build/genesis-sampleconfig.tar.bz2: $(GENESIS_SAMPLECONFIG)
226224

227225
build/%.tar.bz2:
228226
@echo "Creating $@"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 msp
18+
19+
import (
20+
cb "github.com/hyperledger/fabric/protos/common"
21+
"github.com/hyperledger/fabric/protos/msp"
22+
"github.com/hyperledger/fabric/protos/utils"
23+
)
24+
25+
const (
26+
MSPKey = "MSP"
27+
)
28+
29+
// TemplateGroupMSP creates an MSP ConfigValue at the given configPath
30+
func TemplateGroupMSP(configPath []string, mspConf *msp.MSPConfig) *cb.ConfigGroup {
31+
result := cb.NewConfigGroup()
32+
intermediate := result
33+
for _, group := range configPath {
34+
intermediate.Groups[group] = cb.NewConfigGroup()
35+
intermediate = intermediate.Groups[group]
36+
}
37+
intermediate.Values[MSPKey] = &cb.ConfigValue{
38+
Value: utils.MarshalOrPanic(mspConf),
39+
}
40+
return result
41+
}

common/configtx/test/helper.go

+43-65
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,18 @@ limitations under the License.
1717
package test
1818

1919
import (
20-
"io/ioutil"
2120
"os"
21+
"path/filepath"
2222

2323
"github.com/hyperledger/fabric/common/configtx"
24+
configtxapplication "github.com/hyperledger/fabric/common/configtx/handlers/application"
25+
configtxmsp "github.com/hyperledger/fabric/common/configtx/handlers/msp"
26+
genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig"
27+
"github.com/hyperledger/fabric/common/configtx/tool/provisional"
2428
"github.com/hyperledger/fabric/common/genesis"
29+
"github.com/hyperledger/fabric/msp"
2530
cb "github.com/hyperledger/fabric/protos/common"
26-
"github.com/hyperledger/fabric/protos/utils"
2731

28-
"github.com/golang/protobuf/proto"
2932
logging "github.com/op/go-logging"
3033
)
3134

@@ -36,91 +39,66 @@ const (
3639
AcceptAllPolicyKey = "AcceptAllPolicy"
3740
)
3841

39-
const (
40-
OrdererTemplateName = "orderer.template"
41-
MSPTemplateName = "msp.template"
42-
PeerTemplateName = "peer.template"
43-
)
44-
45-
var ordererTemplate configtx.Template
46-
var mspTemplate configtx.Template
47-
var peerTemplate configtx.Template
48-
49-
var compositeTemplate configtx.Template
50-
51-
var genesisFactory genesis.Factory
42+
var sampleMSPPath string
5243

53-
func init() {
54-
ordererTemplate = readTemplate(OrdererTemplateName)
55-
mspTemplate = readTemplate(MSPTemplateName)
56-
peerTemplate = readTemplate(PeerTemplateName)
57-
58-
compositeTemplate = configtx.NewCompositeTemplate(mspTemplate, ordererTemplate, peerTemplate)
59-
genesisFactory = genesis.NewFactoryImpl(compositeTemplate)
44+
func dirExists(path string) bool {
45+
_, err := os.Stat(path)
46+
return err == nil
6047
}
6148

62-
func resolveName(name string) (string, []byte) {
63-
path := os.Getenv("GOPATH") + "/src/github.com/hyperledger/fabric/common/configtx/test/" + name
64-
data, err := ioutil.ReadFile(path)
65-
if err == nil {
66-
return path, data
49+
func init() {
50+
mspSampleConfig := "/msp/sampleconfig"
51+
peerPath := filepath.Join(os.Getenv("PEER_CFG_PATH"), mspSampleConfig)
52+
ordererPath := filepath.Join(os.Getenv("ORDERER_CFG_PATH"), mspSampleConfig)
53+
switch {
54+
case dirExists(peerPath):
55+
sampleMSPPath = peerPath
56+
return
57+
case dirExists(ordererPath):
58+
sampleMSPPath = ordererPath
59+
return
6760
}
6861

69-
path = os.Getenv("PEER_CFG_PATH") + "/common/configtx/test/" + name
70-
data, err = ioutil.ReadFile(path)
71-
if err != nil {
72-
panic(err)
62+
gopath := os.Getenv("GOPATH")
63+
for _, p := range filepath.SplitList(gopath) {
64+
samplePath := filepath.Join(p, "src/github.com/hyperledger/fabric", mspSampleConfig)
65+
if !dirExists(samplePath) {
66+
continue
67+
}
68+
sampleMSPPath = samplePath
7369
}
7470

75-
return path, data
76-
}
77-
78-
func readTemplate(name string) configtx.Template {
79-
_, data := resolveName(name)
80-
81-
templateProto := &cb.ConfigTemplate{}
82-
err := proto.Unmarshal(data, templateProto)
83-
if err != nil {
84-
panic(err)
71+
if sampleMSPPath == "" {
72+
logger.Panicf("Could not find genesis.yaml, try setting PEER_CFG_PATH, ORDERER_CFG_PATH, or GOPATH correctly")
8573
}
86-
87-
return configtx.NewSimpleTemplate(templateProto.Items...)
88-
}
89-
90-
// WriteTemplate takes an output file and set of config items and writes them to that file as a marshaled ConfigTemplate
91-
func WriteTemplate(name string, items ...*cb.ConfigItem) {
92-
path, _ := resolveName(name)
93-
94-
logger.Debugf("Encoding config template")
95-
outputData := utils.MarshalOrPanic(&cb.ConfigTemplate{
96-
Items: items,
97-
})
98-
99-
logger.Debugf("Writing config to %s", path)
100-
ioutil.WriteFile(path, outputData, 0644)
10174
}
10275

10376
// MakeGenesisBlock creates a genesis block using the test templates for the given chainID
10477
func MakeGenesisBlock(chainID string) (*cb.Block, error) {
105-
return genesisFactory.Block(chainID)
78+
return genesis.NewFactoryImpl(CompositeTemplate()).Block(chainID)
10679
}
10780

10881
// OrderererTemplate returns the test orderer template
10982
func OrdererTemplate() configtx.Template {
110-
return ordererTemplate
83+
genConf := genesisconfig.Load()
84+
return provisional.New(genConf).ChannelTemplate()
11185
}
11286

113-
// MSPerTemplate returns the test MSP template
87+
// MSPTemplate returns the test MSP template
11488
func MSPTemplate() configtx.Template {
115-
return mspTemplate
89+
mspConf, err := msp.GetLocalMspConfig(sampleMSPPath, "SAMPLE")
90+
if err != nil {
91+
logger.Panicf("Could not load sample MSP config: %s", err)
92+
}
93+
return configtx.NewSimpleTemplateNext(configtxmsp.TemplateGroupMSP([]string{configtxapplication.GroupKey}, mspConf))
11694
}
11795

118-
// MSPerTemplate returns the test peer template
119-
func PeerTemplate() configtx.Template {
120-
return peerTemplate
96+
// ApplicationTemplate returns the test application template
97+
func ApplicationTemplate() configtx.Template {
98+
return configtx.NewSimpleTemplateNext(configtxapplication.DefaultAnchorPeers())
12199
}
122100

123101
// CompositeTemplate returns the composite template of peer, orderer, and MSP
124102
func CompositeTemplate() configtx.Template {
125-
return compositeTemplate
103+
return configtx.NewCompositeTemplate(MSPTemplate(), OrdererTemplate(), ApplicationTemplate())
126104
}

common/configtx/test/msp.template

-55
This file was deleted.

common/configtx/test/orderer.template

-17
This file was deleted.

common/configtx/test/peer.template

-2
This file was deleted.

common/configtx/tool/localconfig/config.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,29 @@ func Load() *TopLevel {
117117
config := viper.New()
118118

119119
config.SetConfigName("genesis")
120-
121120
var cfgPath string
122121

123-
// Path to look for the config file in based on ORDERER_CFG_PATH and GOPATH
124-
searchPath := os.Getenv("ORDERER_CFG_PATH") + ":" + os.Getenv("GOPATH")
125-
for _, p := range filepath.SplitList(searchPath) {
126-
genesisPath := filepath.Join(p, "src/github.com/hyperledger/fabric/common/configtx/tool/")
122+
// Path to look for the config file in based on GOPATH
123+
searchPath := []string{
124+
os.Getenv("ORDERER_CFG_PATH"),
125+
os.Getenv("PEER_CFG_PATH"),
126+
}
127+
128+
for _, p := range filepath.SplitList(os.Getenv("GOPATH")) {
129+
searchPath = append(searchPath, filepath.Join(p, "src/github.com/hyperledger/fabric/common/configtx/tool/"))
130+
}
131+
132+
for _, genesisPath := range searchPath {
133+
logger.Infof("Checking for genesis.yaml at: %s", genesisPath)
127134
if _, err := os.Stat(filepath.Join(genesisPath, "genesis.yaml")); err != nil {
128-
// The yaml file does not exist in this component of the go src
135+
// The yaml file does not exist in this component of the path
129136
continue
130137
}
131138
cfgPath = genesisPath
132139
}
140+
133141
if cfgPath == "" {
134-
logger.Fatalf("Could not find genesis.yaml, try setting GOPATH correctly")
142+
logger.Fatalf("Could not find genesis.yaml in paths of %s. Try setting ORDERER_CFG_PATH, PEER_CFG_PATH, or GOPATH correctly", searchPath)
135143
}
136144
config.AddConfigPath(cfgPath) // Path to look for the config file in
137145

common/localmsp/signer_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestMain(m *testing.M) {
3030
var mspMgrConfigDir string
3131
var alternativeCfgPath = os.Getenv("ORDERER_CFG_PATH")
3232
if alternativeCfgPath != "" {
33-
mspMgrConfigDir = alternativeCfgPath + "/../msp/sampleconfig/"
33+
mspMgrConfigDir = alternativeCfgPath + "/msp/sampleconfig/"
3434
} else if _, err := os.Stat("./msp/sampleconfig/"); err == nil {
3535
mspMgrConfigDir = "./msp/sampleconfig/"
3636
} else {

images/orderer/Dockerfile.in

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
FROM hyperledger/fabric-baseos:_BASE_TAG_
2-
ENV ORDERER_CFG_PATH /etc/hyperledger/fabric/orderer
3-
RUN mkdir -p /var/hyperledger/production /etc/hyperledger/fabric/orderer
2+
ENV ORDERER_CFG_PATH /etc/hyperledger/fabric
3+
RUN mkdir -p /var/hyperledger/production $ORDERER_CFG_PATH
44
COPY payload/orderer /usr/local/bin
5-
ADD payload/msp-sampleconfig.tar.bz2 $ORDERER_CFG_PATH/../
5+
COPY payload/genesis.yaml $ORDERER_CFG_PATH/
66
COPY payload/orderer.yaml $ORDERER_CFG_PATH/
77
COPY payload/genesis.yaml $ORDERER_CFG_PATH/
88
EXPOSE 7050

images/peer/Dockerfile.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ RUN mkdir -p /var/hyperledger/production $PEER_CFG_PATH
55
COPY payload/peer /usr/local/bin
66
COPY payload/core.yaml $PEER_CFG_PATH
77
ADD payload/msp-sampleconfig.tar.bz2 $PEER_CFG_PATH
8-
ADD payload/genesis-sampleconfig.tar.bz2 $PEER_CFG_PATH
8+
ADD payload/genesis.yaml $PEER_CFG_PATH
99
CMD peer node start

images/testenv/Dockerfile.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ FROM hyperledger/fabric-baseimage:_BASE_TAG_
22

33
# fabric configuration locations
44
ENV PEER_CFG_PATH /etc/hyperledger/fabric
5-
ENV ORDERER_CFG_PATH /etc/hyperledger/fabric/orderer
5+
ENV ORDERER_CFG_PATH /etc/hyperledger/fabric
66

77
# create needed directories
88
RUN mkdir -p \

0 commit comments

Comments
 (0)