Creating a Hyperledger Fabric network from scratch — Part II Starting the network

0
41

Creating a Hyperledger Fabric network from scratch — Part II Starting the network

In the previous post, we had defined our network, the organizations that were going to be part of it and the containers we needed to start. In this new post, we will create the scripts that allow us to generate everything we need to start our network.

To be able to start the network we need the following steps:

  • Generate the cryptographic necessary material to identify organizations and allow users to connect to it.
  • Start the containers.
  • Create a channel in which organizations can communicate.
  • Join organizations to the channel.

Generate the cryptographic material

For an organization to be part of a network, we need it to be identified by a digital certificate, Hyperledger Fabric, uses the X509 type certificate and provides a tool in its binaries to be able to generate them, cryptogen. In the case of this application, as we have downloaded it within fabric-samples, we have this binary in the immediately superior path, and we can use it, with the command:

../bin/cryptogen generate — config=./supply-network/crypto-config.yaml — output=”./supply-network/crypto-config”

This command calls this binary, which is passed as a configuration file, crypto-config.yaml that we generated in the previous post, and we indicate the path to write the certificates. Once executed, we see that it generates the certificates, both from the orderer service, and from the organizations we define:

In addition to this, we need to create a genesis block with which to start the blockchain and the description of the channel to connect to. We will do this with the configtxgen tool that Hyperldeger also provides.

To generate the genesis block we need to execute:

../bin/configtxgen -configPath ./supply-network -profile SupplyOrdererGenesis -outputBlock ./supply-network/channel-artifacts/genesis.block

And to generate the channel:

Join organizations to the channel

Once we have the certificates created, we can start the containers, this will be done thanks to the docker-compose-cli.yaml file that we define and using docker compose, we will start the docker containers with:

docker-compose -f docker-compose-cli.yaml up -d

to see that they were created and are running correctly, we can see it with

docker ps -a

This shows us which containers are running, in the event that one is in “Exited” state we can see that it has caused it to not start properly, looking at its logs with

docker logs <container-name>

Create a channel in which organizations can communicate

Once we have identification for our parts, we will create a channel so that they can communicate for it, we need to use the CLI type container that we defined in the previous post, in order to connect to it, we can use the docker exec command and open a console in which to execute commands, but as we also need to pass parameters and the idea is that it can be automated, we can generate a script like the following, and make it run inside the container using the command:

docker exec -it cli createChannel.sh

In this script we first create the necessary variables and execute the command

peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx — tls $CORE_PEER_TLS_ENABLED — cafile $ORDERER_CA >&log.txt

Join organizations to the channel

This creates the channel with the channel.tx file that we created in the previous step. Now we just need to join organizations to this channel, for which we will create another script to pass as parameters the data of each organization, and call it once per organization.

When we can specify the parameters we will call it once per organization and it will execute the peer channel join command, which will cause them to join the channel.

echo "Joining Deliverer to channel..."
docker exec -it cli ./join-peer.sh peer0 deliverer DelivererMSP 10051 1.0
echo "Joining Manufacturer to channel..."
docker exec -it cli ./join-peer.sh peer0 manufacturer ManufacturerMSP 9051 1.0
echo "Joining Retailer to channel..."
docker exec -it cli ./join-peer.sh peer0 retailer RetailerMSP 11051 1.0

And with this we would have our network started, in the next post we will see how to create and install the chaincode to interact with the network.
The repository with the entire application can be found on my Github

Previous post can find at

Creating a Hyperledger Fabric network from scratch — Part I Designing the network

Part III can be found here:

Creating a Hyperledger Fabric network from scratch — Part III Chaincode


Creating a Hyperledger Fabric network from scratch — Part II Starting the network was originally published in Coinmonks on Medium, where people are continuing the conversation by highlighting and responding to this story.