Hyperledger Fabric “Building Your First Network” Tutorial — Part 2

Sam LAM @ OriginBit
4 min readFeb 6, 2019

--

In part 1 of this tutorial set, I was introducing the files in BYFN. In part 2, I am going to introduce the remaining files.

Recap the File List

Let’s recap our list of files:

  1. docker-compose-cli.yaml (Part 1)
  2. base/docker-compose-base.yaml (Part 2)
  3. base/peer-base.yaml (Part 2)
  4. channel-artifacts/ (Part 2)
  5. crypto-config.yaml (Part 2)
  6. configtx.yaml (Part 2)
  7. byfn.sh (Part 2)
  8. scripts/script.sh (Part 2)
  9. scripts/utils.sh (Part 2)

We will finish all the remaining files in this part.

2.2 base/docker-compose-base.yaml

In part 1, we have introduceddocker-compose-cli.yaml, which is to define our (virtual) Fabric network. We review a part of docker-compose-cli.yaml:

Part of docker-compose-cli.yaml

Notice that there is an extends keyword. As discussed in part 1, it is to re-use another base Docker compose file. The purpose of this is to make things cleaner — by grouping the shared configurations in one base file, such that other Docker compose files can extend it. It is about good practice — reusability, but this is not mandatory.

Then, the file keyword in line 8 specifies which base file to extend, and service keyword specifies which item (service) in the base file to extend.

Now, we look at base/docker-compose-base.yaml:

The base file is similar to docker-compose-cli.yaml.

Lines 3 to 28 define a template node called orderer.example.com, and docker-compose-cli.yaml extends it and add some extra configurations based on this template.

Line 5 specifies which image this node uses. In our case, the node uses an image from Hyperledger official — hyperledger/fabric-orderer. That $IMAGE_TAG is the image version, such as 1.4.

Other keywords and configurations are similar to docker-compose-cli.yaml. The point of having a base file is just to group common configurations to a base file to make them reusable.

Notice that in line 32, there is also an extends keyword, that is, again, to extend or reuse another base file,base/peer-base.yaml, which we will quickly take a look.

2.3 base/peer-base.yaml

b

The syntax of this file is more or less the same as docker-compose-cli.yaml and base/peer-base.yaml. Again, why do we need these base files? Because of reusability, though it is not mandatory.

2.4 channel-artifacts/

Notice that this is an empty directory currently. This directory will be used to store some configuration transactions and a genesis block. More details later.

2.5 crypto-config.yaml

This is a file to define certifications and keys to be generated and used in the network.

In Hyperledger Fabric, we have a set of certifications and keys for users and nodes. For example, Peer needs to have a set of certificates and keys to perform endorsement, prove itself as a member in the Blockchain network, do signing, etc.

Line 4 OrdererOrgs is defining certificates and keys generation for organization related to Orderer nodes.

Line 22 Template > Count specifies how many nodes in Org1 (organization 1), while 29 Users > Count specifies how many users in Org1.

2.6 configtx.yaml

This is a file to define the configurations of the Blockchain network, though later they could be updated.

This is a long file, so I don’t embed into the blog post. You could refer to it here.

Let’s look at some parts of this file:

Here, we are defining an organization — Org1 in the Blockchain network, along with some policies.

Line 15 specifies Anchor Peer of Org1, that is a special role of Peer node — to act as a representative to talk with other organizations.

Line 4 specifies the location of org1 certifications and keys.

Here, we are defining configurations related to how to form a block for the Blockchain, such as MaxMessageCount, that is the number of transactions in each block.

So, overall, there are three organizations, OrdererOrg, Org1, and Org2. You may refer to here for more details of configtx.yaml.

2.7 byfn.sh

This is a convenient script file by Hyperledger Fabric to start the network.

We use it this way to bring up the network (we will run together later):

./byfn.sh up

Basically, it does the followings:

  1. Generate certificates and keys based on crypto-config.yaml
  2. Generate channel artifacts based on configtx.yaml, outputs will be stored in channel-artifacts/
  3. Bring up the (virtual) Fabric network based on docker-compose-cli.yaml
  4. Create Hyperledger Fabric channel, mychannel
  5. Join Peer nodes into the channel
  6. Install the Smart Contract (Chaincode) in Peer nodes
  7. Instantiate the Smart Contract (Chaincode) in one of the Peer nodes

Channel in Hyperledger Fabric is a private group in a Blockchain network. And each Blockchain network could contain multiple Channels, where each Channel is independent to other Channels, has its own ledger, and contains (multiple) organisation(s).

2.8 scripts/script.sh & scripts/utils.sh

  • scripts/script.sh is a script used by byfn.sh for steps 4 to 7 above.
  • scripts/utils.sh is a script used by scripts/script.sh. It contains some common utility functions, such as installing Smart Contract (Chaincode)

Starting from the next part, we will continue some practical steps.

Thanks! If you like my stories, please follow me for new updates!

Next: Part3

--

--

Sam LAM @ OriginBit

OriginBit Founder | HSUHK Lecturer | Gov Advisory Committee Member(SCSDITE) | GDG Organizer | MBuddhStud(HKU) | BEng(HKUST) | MCSE | MCP | CCNP | CEH