Skip to content

Commit 6be8f63

Browse files
author
Jason Yellick
committed
[FAB-2339] Add simple tool write out genesis block
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 <[email protected]>
1 parent 4887bf4 commit 6be8f63

File tree

6 files changed

+95
-6
lines changed

6 files changed

+95
-6
lines changed

Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ MSP_SAMPLECONFIG = $(shell git ls-files msp/sampleconfig/*.pem)
6666
PROJECT_FILES = $(shell git ls-files)
6767
IMAGES = peer orderer ccenv javaenv buildenv testenv zookeeper kafka
6868

69+
pkgmap.configtxgen := $(PKGNAME)/common/configtx/tool/configtxgen
6970
pkgmap.peer := $(PKGNAME)/peer
7071
pkgmap.orderer := $(PKGNAME)/orderer
7172
pkgmap.block-listener := $(PKGNAME)/examples/events/block-listener
@@ -96,6 +97,9 @@ peer-docker: build/image/peer/$(DUMMY)
9697
orderer: build/bin/orderer
9798
orderer-docker: build/image/orderer/$(DUMMY)
9899

100+
.PHONY: configtxgen
101+
configtxgen: build/bin/configtxgen
102+
99103
buildenv: build/image/buildenv/$(DUMMY)
100104

101105
build/image/testenv/$(DUMMY): build/image/buildenv/$(DUMMY)
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 main
18+
19+
import (
20+
"flag"
21+
"io/ioutil"
22+
23+
genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig"
24+
"github.com/hyperledger/fabric/common/configtx/tool/provisional"
25+
"github.com/hyperledger/fabric/protos/utils"
26+
27+
logging "github.com/op/go-logging"
28+
)
29+
30+
const (
31+
DefaultGenesisFileLocation = "genesis.block"
32+
)
33+
34+
var logger = logging.MustGetLogger("common/configtx/tool")
35+
36+
func main() {
37+
var writePath, profile string
38+
39+
dryRun := flag.Bool("dryRun", false, "Whether to only generate but not write the genesis block file.")
40+
flag.StringVar(&writePath, "path", DefaultGenesisFileLocation, "The path to write the genesis block to.")
41+
flag.StringVar(&profile, "profile", genesisconfig.SampleInsecureProfile, "The profile from configtx.yaml to use for generation.")
42+
flag.Parse()
43+
44+
logging.SetLevel(logging.INFO, "")
45+
46+
logger.Info("Loading configuration")
47+
config := genesisconfig.Load(profile)
48+
49+
logger.Info("Generating genesis block")
50+
genesisBlock := provisional.New(config).GenesisBlock()
51+
52+
if !*dryRun {
53+
logger.Info("Writing genesis block")
54+
ioutil.WriteFile(writePath, utils.MarshalOrPanic(genesisBlock), 0644)
55+
}
56+
}

common/configtx/tool/localconfig/config.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ const (
3939

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

42-
func init() {
43-
logging.SetLevel(logging.ERROR, "")
44-
}
45-
4642
// Prefix is the default config prefix for the orderer
4743
const Prefix string = "CONFIGTX"
4844

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

195192
err := config.ReadInConfig()
@@ -208,6 +205,7 @@ func Load(profile string) *Profile {
208205
if !ok {
209206
logger.Panicf("Could not find profile %s", profile)
210207
}
208+
211209
result.completeInitialization()
212210

213211
return result

docs/configtxgen.md

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Configuring using the configtxgen tool
2+
3+
This document describe the usage for the `configtxgen` utility for manipulating fabric channel configuration.
4+
5+
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.
6+
7+
## Building the tool
8+
9+
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.
10+
11+
## Configuration Profiles
12+
13+
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.
14+
15+
This configuration file is split primarily into three pieces.
16+
17+
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.
18+
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.
19+
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.
20+
21+
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.
22+
23+
## Bootstrapping the orderer
24+
After creating a configuration profile as desired, simply invoke
25+
```
26+
configtxgen -profile &lt;profile_name&gt;
27+
```
28+
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.
29+
30+
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.

images/orderer/Dockerfile.in

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
FROM hyperledger/fabric-baseos:_BASE_TAG_
22
ENV ORDERER_CFG_PATH /etc/hyperledger/fabric
33
ENV ORDERER_GENERAL_LOCALMSPDIR $ORDERER_CFG_PATH/msp/sampleconfig
4+
ENV ORDERER_GENERAL_GENESISPROFILE=SampleInsecureSolo
45
RUN mkdir -p /var/hyperledger/production $ORDERER_CFG_PATH
56
COPY payload/orderer /usr/local/bin
67
COPY payload/configtx.yaml $ORDERER_CFG_PATH/

orderer/network_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ func generateConfigEnv(peerNum uint64, grpcPort int, peerCommPort string, certFi
308308
envs = append(envs, fmt.Sprintf("ORDERER_CFG_PATH=%s", ordererDir))
309309
envs = append(envs, fmt.Sprintf("ORDERER_GENERAL_LOCALMSPDIR=%s", ordererDir+"/../msp/sampleconfig"))
310310
envs = append(envs, fmt.Sprintf("ORDERER_GENERAL_LISTENPORT=%d", grpcPort))
311-
envs = append(envs, fmt.Sprintf("CONFIGTX_PROFILES_SAMPLEINSECURESOLO_ORDERER_ORDERERTYPE=%s", "sbft"))
311+
envs = append(envs, fmt.Sprintf("CONFIGTX_ORDERER_ORDERERTYPE=%s", "sbft"))
312312
envs = append(envs, fmt.Sprintf("ORDERER_GENERAL_GENESISPROFILE=%s", genesisconfig.SampleInsecureProfile))
313313
envs = append(envs, fmt.Sprintf("ORDERER_GENESIS_DEPRECATEDBATCHTIMEOUT=%d", 1000))
314314
envs = append(envs, fmt.Sprintf("ORDERER_GENESIS_DEPRECATED=%d", 1000000000))

0 commit comments

Comments
 (0)