How to build your own Hyperledger Fabric network using minifabric?

Ömer Faruk Boztaş
stm-blockchain
Published in
4 min readMar 2, 2021
Photo by Alina Grubnyak on Unsplash

Minifabric is a tool that can be used for hyperledger fabric network setup. Available here. We will focus on how we can customize our network with various configurations.

Firstly, we need to define a consortium for the organization and channels in the network to be established, in accordance with the structure we want.

For this, we will change the configtx.j2 file inside the minifabric\playbooks\ops\certgen\templates folder. At the bottom of the file there is a field named Profiles. In this, we should specify the channels, organizations and consortiums we want.

Example:

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

.

.

.

Profiles:

HelloWorldChannel:
Consortium: HelloWorldConsortium
<<: *ChannelDefaults
Application:
<<: *ApplicationDefaults
Organizations:
- *hello-world
- *hello-universe
Capabilities:
<<: *ApplicationCapabilities

.

.

.

OrdererGenesis:

Consortiums:

HelloWorldConsortium:

Organizations:

- *hello-world

- *hello-universe

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

After doing this, we will need to make various changes in the spec.yaml file in the minifab folder. We will define CA, peer and orderer.

Example spec.yaml:

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

fabric:

cas:

- “ca1.hello-world”

- “ca1.hello-universe”

peers:

- “peer1.hello-world”

- “peer1.hello-universe”

orderers:

- “orderer1.orderer.com”

settings:

ca:

FABRIC_LOGGING_SPEC: DEBUG

peer:

FABRIC_LOGGING_SPEC: DEBUG

orderer:

FABRIC_LOGGING_SPEC: DEBUG

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Then to create the network;

./minifab netup

Various flags can also be added at the end, eg. -e true exposes endpoint addresses, -o {org_name} allows us to specify the organization name to be used, etc. (Here for a list of flags.)

After the network is up, we will put our channel creation scripts and organizations’ participation scripts into the vars/run folder. We will mention some points to pay attention to below.

Sample channel creation script:

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

#!/bin/bash
# Script to create channel block 0 and then create channel
cp $FABRIC_CFG_PATH/core.yaml /vars/core.yaml
cd /vars
export FABRIC_CFG_PATH=/vars
configtxgen -profile HelloWorldChannel \
-outputCreateChannelTx helloworldchannel.tx -channelID HelloWorldChannel

export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_ID=cli
export CORE_PEER_ADDRESS=192.168.1.105:7003
export CORE_PEER_TLS_ROOTCERT_FILE=/vars/keyfiles/peerOrganizations/hello.world/peers/peer1.helloworld/tls/ca.crt
export CORE_PEER_LOCALMSPID=hello-world
export CORE_PEER_MSPCONFIGPATH=/vars/keyfiles/peerOrganizations/hello.world/users/Admin@helloworld/msp
export ORDERER_ADDRESS=192.168.1.105:7009
export ORDERER_TLS_CA=/vars/keyfiles/ordererOrganizations/orderer.com/orderers/orderer1.orderer.com/tls/ca.crt
peer channel create -c helloworldchannel-f helloworldchannel.tx -o $ORDERER_ADDRESS \
--cafile $ORDERER_TLS_CA --tls

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

In the above script, the channel name should be the name we specified in the profile in configtx.j2 file. The peer address and orderer address must be the same as the address in the respective docker container.

Script to join a peer to a channel:

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

#!/bin/bash
# Script to join a peer to a channel
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_ID=cli
export CORE_PEER_ADDRESS=192.168.1.105:7003
export CORE_PEER_TLS_ROOTCERT_FILE=/vars/keyfiles/peerOrganizations/hello.world/peers/peer1.helloworld/tls/ca.crt
export CORE_PEER_LOCALMSPID=helloworldchannel
export CORE_PEER_MSPCONFIGPATH=/vars/keyfiles/peerOrganizations/hello.world/users/Admin@helloworld/msp
export ORDERER_ADDRESS=192.168.1.105:7009
export ORDERER_TLS_CA=/vars/keyfiles/ordererOrganizations/orderer.com/orderers/orderer1.orderer.com/tls/ca.crt
if [ ! -f “helloworldchannel.genesis.block” ]; then
peer channel fetch oldest -o $ORDERER_ADDRESS --cafile $ORDERER_TLS_CA \

--tls -c helloworldchannel /vars/helloworldchannel.genesis.block
fi

peer channel join -b /vars/helloworldchannel.genesis.block \
-o $ORDERER_ADDRESS --cafile $ORDERER_TLS_CA --tls

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

Also in this script, channel names and addresses should be changed in accordance with your local information.

Scripts should also be given 777 privileges from chmod.

After putting these scripts in the vars /run folder, we will run them in our cli container one after the other. We need to look at the name of our cli container, it can be viewed with docker ps.
Then we can run all our scripts as docker exec {cli_container_name}/vars/ run/{script_name}

Our network is now available. We have files that we need to export in order to access the network from outside. We do this as follows:

./minifab profilegen, discover -c {desired_channel_name}

After doing this, the necessary files to be able to connect to the channel are created by the minifabric.

--

--