Skip to content

Commit

Permalink
[FAB-2169] Dynamically generate genesis material
Browse files Browse the repository at this point in the history
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 <jyellick@us.ibm.com>
Jason Yellick committed Feb 14, 2017
1 parent 9f561b8 commit 32b772c
Showing 12 changed files with 106 additions and 196 deletions.
4 changes: 1 addition & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -63,7 +63,6 @@ GOSHIM_DEPS = $(shell ./scripts/goListFiles.sh $(PKGNAME)/core/chaincode/shim |
JAVASHIM_DEPS = $(shell git ls-files core/chaincode/shim/java)
PROTOS = $(shell git ls-files *.proto | grep -v vendor)
MSP_SAMPLECONFIG = $(shell git ls-files msp/sampleconfig/*.pem)
GENESIS_SAMPLECONFIG = $(shell git ls-files common/configtx/test/*.template)
PROJECT_FILES = $(shell git ls-files)
IMAGES = peer orderer ccenv javaenv testenv zookeeper kafka

@@ -176,7 +175,7 @@ build/image/javaenv/payload: build/javashim.tar.bz2 \
build/image/peer/payload: build/docker/bin/peer \
peer/core.yaml \
build/msp-sampleconfig.tar.bz2 \
build/genesis-sampleconfig.tar.bz2
common/configtx/tool/genesis.yaml
build/image/orderer/payload: build/docker/bin/orderer \
build/msp-sampleconfig.tar.bz2 \
orderer/orderer.yaml \
@@ -222,7 +221,6 @@ build/goshim.tar.bz2: $(GOSHIM_DEPS)
build/javashim.tar.bz2: $(JAVASHIM_DEPS)
build/protos.tar.bz2: $(PROTOS)
build/msp-sampleconfig.tar.bz2: $(MSP_SAMPLECONFIG)
build/genesis-sampleconfig.tar.bz2: $(GENESIS_SAMPLECONFIG)

build/%.tar.bz2:
@echo "Creating $@"
41 changes: 41 additions & 0 deletions common/configtx/handlers/msp/config_util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright IBM Corp. 2017 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package msp

import (
cb "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/msp"
"github.com/hyperledger/fabric/protos/utils"
)

const (
MSPKey = "MSP"
)

// TemplateGroupMSP creates an MSP ConfigValue at the given configPath
func TemplateGroupMSP(configPath []string, mspConf *msp.MSPConfig) *cb.ConfigGroup {
result := cb.NewConfigGroup()
intermediate := result
for _, group := range configPath {
intermediate.Groups[group] = cb.NewConfigGroup()
intermediate = intermediate.Groups[group]
}
intermediate.Values[MSPKey] = &cb.ConfigValue{
Value: utils.MarshalOrPanic(mspConf),
}
return result
}
108 changes: 43 additions & 65 deletions common/configtx/test/helper.go
Original file line number Diff line number Diff line change
@@ -17,15 +17,18 @@ limitations under the License.
package test

import (
"io/ioutil"
"os"
"path/filepath"

"github.com/hyperledger/fabric/common/configtx"
configtxapplication "github.com/hyperledger/fabric/common/configtx/handlers/application"
configtxmsp "github.com/hyperledger/fabric/common/configtx/handlers/msp"
genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig"
"github.com/hyperledger/fabric/common/configtx/tool/provisional"
"github.com/hyperledger/fabric/common/genesis"
"github.com/hyperledger/fabric/msp"
cb "github.com/hyperledger/fabric/protos/common"
"github.com/hyperledger/fabric/protos/utils"

"github.com/golang/protobuf/proto"
logging "github.com/op/go-logging"
)

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

const (
OrdererTemplateName = "orderer.template"
MSPTemplateName = "msp.template"
PeerTemplateName = "peer.template"
)

var ordererTemplate configtx.Template
var mspTemplate configtx.Template
var peerTemplate configtx.Template

var compositeTemplate configtx.Template

var genesisFactory genesis.Factory
var sampleMSPPath string

func init() {
ordererTemplate = readTemplate(OrdererTemplateName)
mspTemplate = readTemplate(MSPTemplateName)
peerTemplate = readTemplate(PeerTemplateName)

compositeTemplate = configtx.NewCompositeTemplate(mspTemplate, ordererTemplate, peerTemplate)
genesisFactory = genesis.NewFactoryImpl(compositeTemplate)
func dirExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}

func resolveName(name string) (string, []byte) {
path := os.Getenv("GOPATH") + "/src/github.com/hyperledger/fabric/common/configtx/test/" + name
data, err := ioutil.ReadFile(path)
if err == nil {
return path, data
func init() {
mspSampleConfig := "/msp/sampleconfig"
peerPath := filepath.Join(os.Getenv("PEER_CFG_PATH"), mspSampleConfig)
ordererPath := filepath.Join(os.Getenv("ORDERER_CFG_PATH"), mspSampleConfig)
switch {
case dirExists(peerPath):
sampleMSPPath = peerPath
return
case dirExists(ordererPath):
sampleMSPPath = ordererPath
return
}

path = os.Getenv("PEER_CFG_PATH") + "/common/configtx/test/" + name
data, err = ioutil.ReadFile(path)
if err != nil {
panic(err)
gopath := os.Getenv("GOPATH")
for _, p := range filepath.SplitList(gopath) {
samplePath := filepath.Join(p, "src/github.com/hyperledger/fabric", mspSampleConfig)
if !dirExists(samplePath) {
continue
}
sampleMSPPath = samplePath
}

return path, data
}

func readTemplate(name string) configtx.Template {
_, data := resolveName(name)

templateProto := &cb.ConfigTemplate{}
err := proto.Unmarshal(data, templateProto)
if err != nil {
panic(err)
if sampleMSPPath == "" {
logger.Panicf("Could not find genesis.yaml, try setting PEER_CFG_PATH, ORDERER_CFG_PATH, or GOPATH correctly")
}

return configtx.NewSimpleTemplate(templateProto.Items...)
}

// WriteTemplate takes an output file and set of config items and writes them to that file as a marshaled ConfigTemplate
func WriteTemplate(name string, items ...*cb.ConfigItem) {
path, _ := resolveName(name)

logger.Debugf("Encoding config template")
outputData := utils.MarshalOrPanic(&cb.ConfigTemplate{
Items: items,
})

logger.Debugf("Writing config to %s", path)
ioutil.WriteFile(path, outputData, 0644)
}

// MakeGenesisBlock creates a genesis block using the test templates for the given chainID
func MakeGenesisBlock(chainID string) (*cb.Block, error) {
return genesisFactory.Block(chainID)
return genesis.NewFactoryImpl(CompositeTemplate()).Block(chainID)
}

// OrderererTemplate returns the test orderer template
func OrdererTemplate() configtx.Template {
return ordererTemplate
genConf := genesisconfig.Load()
return provisional.New(genConf).ChannelTemplate()
}

// MSPerTemplate returns the test MSP template
// MSPTemplate returns the test MSP template
func MSPTemplate() configtx.Template {
return mspTemplate
mspConf, err := msp.GetLocalMspConfig(sampleMSPPath, "SAMPLE")
if err != nil {
logger.Panicf("Could not load sample MSP config: %s", err)
}
return configtx.NewSimpleTemplateNext(configtxmsp.TemplateGroupMSP([]string{configtxapplication.GroupKey}, mspConf))
}

// MSPerTemplate returns the test peer template
func PeerTemplate() configtx.Template {
return peerTemplate
// ApplicationTemplate returns the test application template
func ApplicationTemplate() configtx.Template {
return configtx.NewSimpleTemplateNext(configtxapplication.DefaultAnchorPeers())
}

// CompositeTemplate returns the composite template of peer, orderer, and MSP
func CompositeTemplate() configtx.Template {
return compositeTemplate
return configtx.NewCompositeTemplate(MSPTemplate(), OrdererTemplate(), ApplicationTemplate())
}
55 changes: 0 additions & 55 deletions common/configtx/test/msp.template

This file was deleted.

17 changes: 0 additions & 17 deletions common/configtx/test/orderer.template

This file was deleted.

2 changes: 0 additions & 2 deletions common/configtx/test/peer.template

This file was deleted.

22 changes: 15 additions & 7 deletions common/configtx/tool/localconfig/config.go
Original file line number Diff line number Diff line change
@@ -117,21 +117,29 @@ func Load() *TopLevel {
config := viper.New()

config.SetConfigName("genesis")

var cfgPath string

// Path to look for the config file in based on ORDERER_CFG_PATH and GOPATH
searchPath := os.Getenv("ORDERER_CFG_PATH") + ":" + os.Getenv("GOPATH")
for _, p := range filepath.SplitList(searchPath) {
genesisPath := filepath.Join(p, "src/github.com/hyperledger/fabric/common/configtx/tool/")
// Path to look for the config file in based on GOPATH
searchPath := []string{
os.Getenv("ORDERER_CFG_PATH"),
os.Getenv("PEER_CFG_PATH"),
}

for _, p := range filepath.SplitList(os.Getenv("GOPATH")) {
searchPath = append(searchPath, filepath.Join(p, "src/github.com/hyperledger/fabric/common/configtx/tool/"))
}

for _, genesisPath := range searchPath {
logger.Infof("Checking for genesis.yaml at: %s", genesisPath)
if _, err := os.Stat(filepath.Join(genesisPath, "genesis.yaml")); err != nil {
// The yaml file does not exist in this component of the go src
// The yaml file does not exist in this component of the path
continue
}
cfgPath = genesisPath
}

if cfgPath == "" {
logger.Fatalf("Could not find genesis.yaml, try setting GOPATH correctly")
logger.Fatalf("Could not find genesis.yaml in paths of %s. Try setting ORDERER_CFG_PATH, PEER_CFG_PATH, or GOPATH correctly", searchPath)
}
config.AddConfigPath(cfgPath) // Path to look for the config file in

2 changes: 1 addition & 1 deletion common/localmsp/signer_test.go
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ func TestMain(m *testing.M) {
var mspMgrConfigDir string
var alternativeCfgPath = os.Getenv("ORDERER_CFG_PATH")
if alternativeCfgPath != "" {
mspMgrConfigDir = alternativeCfgPath + "/../msp/sampleconfig/"
mspMgrConfigDir = alternativeCfgPath + "/msp/sampleconfig/"
} else if _, err := os.Stat("./msp/sampleconfig/"); err == nil {
mspMgrConfigDir = "./msp/sampleconfig/"
} else {
6 changes: 3 additions & 3 deletions images/orderer/Dockerfile.in
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FROM hyperledger/fabric-baseos:_BASE_TAG_
ENV ORDERER_CFG_PATH /etc/hyperledger/fabric/orderer
RUN mkdir -p /var/hyperledger/production /etc/hyperledger/fabric/orderer
ENV ORDERER_CFG_PATH /etc/hyperledger/fabric
RUN mkdir -p /var/hyperledger/production $ORDERER_CFG_PATH
COPY payload/orderer /usr/local/bin
ADD payload/msp-sampleconfig.tar.bz2 $ORDERER_CFG_PATH/../
COPY payload/genesis.yaml $ORDERER_CFG_PATH/
COPY payload/orderer.yaml $ORDERER_CFG_PATH/
COPY payload/genesis.yaml $ORDERER_CFG_PATH/
EXPOSE 7050
2 changes: 1 addition & 1 deletion images/peer/Dockerfile.in
Original file line number Diff line number Diff line change
@@ -5,5 +5,5 @@ RUN mkdir -p /var/hyperledger/production $PEER_CFG_PATH
COPY payload/peer /usr/local/bin
COPY payload/core.yaml $PEER_CFG_PATH
ADD payload/msp-sampleconfig.tar.bz2 $PEER_CFG_PATH
ADD payload/genesis-sampleconfig.tar.bz2 $PEER_CFG_PATH
ADD payload/genesis.yaml $PEER_CFG_PATH
CMD peer node start
2 changes: 1 addition & 1 deletion images/testenv/Dockerfile.in
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@ FROM hyperledger/fabric-baseimage:_BASE_TAG_

# fabric configuration locations
ENV PEER_CFG_PATH /etc/hyperledger/fabric
ENV ORDERER_CFG_PATH /etc/hyperledger/fabric/orderer
ENV ORDERER_CFG_PATH /etc/hyperledger/fabric

# create needed directories
RUN mkdir -p \
41 changes: 0 additions & 41 deletions msp/template_test.go

This file was deleted.

0 comments on commit 32b772c

Please sign in to comment.