Skip to content

Commit

Permalink
[FAB-2339] Add simple tool write out genesis block
Browse files Browse the repository at this point in the history
https://jira.hyperledger.org/browse/FAB-2339

In order to simulate real deployment scenarios, it's necessary to have
the genesis block generated ahead of time and loaded via a file.  This
has been done only via bdd thusfar.  This CR adds a simple tool which
dumps the genesis block created by the configtx provisional genesis
package to the filesystem.

There is a doc file under docs/configtxgen.md which describes the usage.

Change-Id: I42171afdce4bb47d63806805a88aa3b93759023d
Signed-off-by: Jason Yellick <jyellick@us.ibm.com>
  • Loading branch information
Jason Yellick committed Feb 19, 2017
1 parent 4887bf4 commit 6be8f63
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 6 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ MSP_SAMPLECONFIG = $(shell git ls-files msp/sampleconfig/*.pem)
PROJECT_FILES = $(shell git ls-files)
IMAGES = peer orderer ccenv javaenv buildenv testenv zookeeper kafka

pkgmap.configtxgen := $(PKGNAME)/common/configtx/tool/configtxgen
pkgmap.peer := $(PKGNAME)/peer
pkgmap.orderer := $(PKGNAME)/orderer
pkgmap.block-listener := $(PKGNAME)/examples/events/block-listener
Expand Down Expand Up @@ -96,6 +97,9 @@ peer-docker: build/image/peer/$(DUMMY)
orderer: build/bin/orderer
orderer-docker: build/image/orderer/$(DUMMY)

.PHONY: configtxgen
configtxgen: build/bin/configtxgen

buildenv: build/image/buildenv/$(DUMMY)

build/image/testenv/$(DUMMY): build/image/buildenv/$(DUMMY)
Expand Down
56 changes: 56 additions & 0 deletions common/configtx/tool/configtxgen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
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 main

import (
"flag"
"io/ioutil"

genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig"
"github.com/hyperledger/fabric/common/configtx/tool/provisional"
"github.com/hyperledger/fabric/protos/utils"

logging "github.com/op/go-logging"
)

const (
DefaultGenesisFileLocation = "genesis.block"
)

var logger = logging.MustGetLogger("common/configtx/tool")

func main() {
var writePath, profile string

dryRun := flag.Bool("dryRun", false, "Whether to only generate but not write the genesis block file.")
flag.StringVar(&writePath, "path", DefaultGenesisFileLocation, "The path to write the genesis block to.")
flag.StringVar(&profile, "profile", genesisconfig.SampleInsecureProfile, "The profile from configtx.yaml to use for generation.")
flag.Parse()

logging.SetLevel(logging.INFO, "")

logger.Info("Loading configuration")
config := genesisconfig.Load(profile)

logger.Info("Generating genesis block")
genesisBlock := provisional.New(config).GenesisBlock()

if !*dryRun {
logger.Info("Writing genesis block")
ioutil.WriteFile(writePath, utils.MarshalOrPanic(genesisBlock), 0644)
}
}
8 changes: 3 additions & 5 deletions common/configtx/tool/localconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ const (

var logger = logging.MustGetLogger("configtx/tool/localconfig")

func init() {
logging.SetLevel(logging.ERROR, "")
}

// Prefix is the default config prefix for the orderer
const Prefix string = "CONFIGTX"

Expand Down Expand Up @@ -189,7 +185,8 @@ func Load(profile string) *Profile {
// for environment variables
config.SetEnvPrefix(Prefix)
config.AutomaticEnv()
replacer := strings.NewReplacer(".", "_")
// This replacer allows substitution within the particular profile without having to fully qualify the name
replacer := strings.NewReplacer(strings.ToUpper(fmt.Sprintf("profiles.%s.", profile)), "", ".", "_")
config.SetEnvKeyReplacer(replacer)

err := config.ReadInConfig()
Expand All @@ -208,6 +205,7 @@ func Load(profile string) *Profile {
if !ok {
logger.Panicf("Could not find profile %s", profile)
}

result.completeInitialization()

return result
Expand Down
30 changes: 30 additions & 0 deletions docs/configtxgen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Configuring using the configtxgen tool

This document describe the usage for the `configtxgen` utility for manipulating fabric channel configuration.

For now, the tool is primarily focused on generating the genesis block for bootstrapping the orderer, but it is intended to be enhanced in the future for generating new channel configurations as well as reconfiguring existing channels.

## Building the tool

Building the tool is as simple as `make configtxgen`. This will create a `configtxgen` binary at `build/bin/configtxgen` which is included in the Vagrant development environment path by default.

## Configuration Profiles

The configuration parameters supplied to the `configtxgen` tool are primarily provided by the `configtx.yaml` file. This file is located at `fabric/common/configtx/tool/configtx.yaml` in the fabric.git repository.

This configuration file is split primarily into three pieces.

1. The `Profiles` section. By default, this section includes some sample configurations which can be used for development or testing scenarios, and refer to crypto material present in the fabric.git tree. These profiles can make a good starting point for construction a real deployment profile. The `configtxgen` tool allows you to specify the profile it is operating under by passing the `-profile` flag. Profiles may explicitly declare all configuration, but usually inherit configuration from the defaults in (3) below.
2. The `Organizations` section. By default, this section includes a single reference to the sampleconfig MSP definition. For production deployments, the sample organization should be removed, and the MSP definitions of the network members should be referenced and defined instead. Each element in the `Organizations` section should be tagged with an anchor label such as `&orgName` which will allow the definition to be referenced in the `Profiles` sections.
3. The default sections. There are default sections for `Orderer` and `Application` configuration, these include attributes like `BatchTimeout` and are generally used as the base inherited values for the profiles.

This configuration file may be edited, or, individual properties may be overridden by setting environment variables, such as `CONFIGTX_ORDERER_ORDERERTYPE=kafka`. Note that the `Profiles` element and profile name do not need to be specified.

## Bootstrapping the orderer
After creating a configuration profile as desired, simply invoke
```
configtxgen -profile &lt;profile_name&gt;
```
This will produce a `genesis.block` file in the current directory. You may optionally specify another filename by passing in the `-path` parameter, or, you may skip the writing of the file by passing the `dryRun` parameter if you simply wish to test parsing of the file.

Then, to utilize this genesis block, before starting the orderer, simply specify `ORDERER_GENERAL_GENESISMETHOD=file` and `ORDERER_GENERAL_GENESISFILE=$PWD/genesis.block` or modify the `orderer.yaml` file to encode these values.
1 change: 1 addition & 0 deletions images/orderer/Dockerfile.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FROM hyperledger/fabric-baseos:_BASE_TAG_
ENV ORDERER_CFG_PATH /etc/hyperledger/fabric
ENV ORDERER_GENERAL_LOCALMSPDIR $ORDERER_CFG_PATH/msp/sampleconfig
ENV ORDERER_GENERAL_GENESISPROFILE=SampleInsecureSolo
RUN mkdir -p /var/hyperledger/production $ORDERER_CFG_PATH
COPY payload/orderer /usr/local/bin
COPY payload/configtx.yaml $ORDERER_CFG_PATH/
Expand Down
2 changes: 1 addition & 1 deletion orderer/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func generateConfigEnv(peerNum uint64, grpcPort int, peerCommPort string, certFi
envs = append(envs, fmt.Sprintf("ORDERER_CFG_PATH=%s", ordererDir))
envs = append(envs, fmt.Sprintf("ORDERER_GENERAL_LOCALMSPDIR=%s", ordererDir+"/../msp/sampleconfig"))
envs = append(envs, fmt.Sprintf("ORDERER_GENERAL_LISTENPORT=%d", grpcPort))
envs = append(envs, fmt.Sprintf("CONFIGTX_PROFILES_SAMPLEINSECURESOLO_ORDERER_ORDERERTYPE=%s", "sbft"))
envs = append(envs, fmt.Sprintf("CONFIGTX_ORDERER_ORDERERTYPE=%s", "sbft"))
envs = append(envs, fmt.Sprintf("ORDERER_GENERAL_GENESISPROFILE=%s", genesisconfig.SampleInsecureProfile))
envs = append(envs, fmt.Sprintf("ORDERER_GENESIS_DEPRECATEDBATCHTIMEOUT=%d", 1000))
envs = append(envs, fmt.Sprintf("ORDERER_GENESIS_DEPRECATED=%d", 1000000000))
Expand Down

0 comments on commit 6be8f63

Please sign in to comment.