From “First Network” to “Test Network”: A New Test Network introduced in Fabric v2.0

Overview

Another newly introduced item in Hyperledger Fabric release 2.0 is Test Network. If you have followed my articles on Hyperledger Fabric, the most common network for demonstration is always the First Network. An eye-catching description in the official document is that

Some Key Findings

There are some quick findings when compared with First Network.

  • Ordering Service in Test Network is implemented as Solo, which means only one orderer is there providing the service. Bear this in mind though this does not have impact on chaincode development and testing. In contrast, five orderers are deployed as an orderer cluster using Raft in First Network.
  • Each peer organization comes with one peer in Test Network, while First Network comes with two peers for each peer organization.
  • In Test Network, crypto material can be generated either through cryptogen or by organization certificate authority. In First Network, only cryptogen is available. If organization CA is needed, it is created through material created in cryptogen.
  • We can create more than one channels with the Test Network script network.sh. In First Network we can create only one channel.
  • The sample chaincode in the Test Network script is fabcar, while that in the First Network script is abstore (was named chaincode_example02 in previous 1.x releases).
  • Test Network does not bring up a CLI container. Peer commands are issued from local host. Proper environment variables are required before we can use the CLI command.

Test Network Architecture

Components

Test Network contains three organization: one Orderer Organization and two Peer Organizations.

Channels

Test Network provides a flexible way to create channel, in a way that we can create more than one channels. It is done by restructuring the channel related activities into one single step and into one script. Once we specify the channel ID when executing the script, the configuration transactions, creation of channel genesis block and peers of member organizations joining channel will be done. As a comparison, First Network comes with one channel when the script executed. We will take a look on these channel related activities on the script session.

Sample Chaincode

While chaincode is not part of a fabric network (instead we say chaincode is deployed on a fabric network), the scripts provided by both Test Network and First Network come with sample chaincode deployment. It helps to verify that the fabric network is operating correctly with this sample chaincode.

Image for post
Image for post
The Test Network and First Network side-by-side comparison in Fabric v2.0

Review the First Network Script

As said, network.sh is a well designed script for Test Network. It comes with modes and flags to perform several tasks. Most of the scripts are inherited from the First Network, with some modification and restructuring to make the script more flexible. Therefore, it make sense to first review the flow of the script in First Network. This helps to understand the design of network.sh.

Image for post
Image for post
  • a configuration transaction for the channel specified, and
  • two anchor peer update transactions
  • four peers (belonging to two peer organizations)
  • a CLI container

Test Network: network.sh

The master script for Test Network is network.sh. As mentioned before, network.sh inherits many script modules from byfn.sh. The most important part is that network.sh makes restructuring on the function such that it provides the following options when running this script.

Image for post
Image for post

Overall Flow

This is a parallel flow for network.sh as what we compare with First Network.

  • two peers (for two peer organizations)
  • generate anchor peer update transactions for channel
  • apply the configuration transaction to the network, resulting the channel genesis block file
  • join the peers with the channel genesis block file
  • apply the anchor peer update transactions to the network

Various Modes in network.sh

With this flow, network.sh comes with modes and options that provides flexibility when one brings up the Test Network.

Image for post
Image for post
Various modes provided in network.sh.

Demonstration on Test Network

Finally here is just a demonstration on what happens when we run network.sh. We will show several scenarios.

Directory Structure

Here is the directory structure of Test Network.

Image for post
Image for post
  • docker: where all the docker compose files are kept
  • organizations: where the crypto material configuration files and generated material are kept
  • scripts: keeping the scripts for different modes
  • system-genesis-block: where the genesis block (system one, stored in orderers) is kept and mapped to the orderer

Bring Up Test Network Components

We begin with no options specified.

./network.sh updocker psdocker exec peer0.org1.example.com peer channel list
Image for post
Image for post
Image for post
Image for post
./network.sh down # tear down previous network
./network.sh up -s couchdb
docker ps
Image for post
Image for post
./network.sh down # tear down previous network
./network.sh up -ca
docker ps
Image for post
Image for post

Bring Up Test Network Components with Channel

Now we shift our focus on channel. We first bring up the network with default mychannel.

./network.sh down # tear down previous network
./network.sh up createChannel
docker psdocker exec peer0.org1.example.com peer channel list
Image for post
Image for post
./network.sh createChannel -c newchanneldocker exec peer0.org1.example.com peer channel list
docker exec peer0.org2.example.com peer channel list
Image for post
Image for post

Deploy Chaincode on a Channel

If we first tear down everything and directly deploy with this mode, it fails as nothing exist yet.

./network.sh down # tear down previous network
./network.sh deployCC
Image for post
Image for post
./network.sh down # tear down previous network
./network.sh up
./network.sh deployCC
Image for post
Image for post
./network.sh down # tear down previous network
./network.sh up createChannel
./network.sh deployCC
Image for post
Image for post

A note for Interacting with Chaincode

In the First Network we have a CLI container, and all peer commands can be issued from the container either using docker exec cli <command> or go into a shell docker exec -it cli bash. There is no CLI container defined in Test Network. Those peer command are instead executed from local host.

Image for post
Image for post
cd fabric-samples/test-networkexport PATH=${PWD}/../bin:${PWD}:$PATH
export FABRIC_CFG_PATH=$PWD/../config/
# for peer command issued to peer0.org1.example.com
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=localhost:7051
# for peer command issued to peer0.org2.example.com
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org2MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
export CORE_PEER_ADDRESS=localhost:9051
Image for post
Image for post
peer channel listpeer chaincode query -C mychannel -n fabcar -c '{"Args":["queryAllCars"]}'
Image for post
Image for post

Summary

In this article we have gone through some detail about Test Network with a reference to and comparison with First Network. Certain enhancement and modification is made (like channel creation, CA generated crypto material), while some changes are not as handy as before (CLI container, and if you wish to test Raft-based setup). Nevertheless, it is good enough for a network when you development and test your own chaincode. If you have been working with First Network for a while, there should not be difficulty to get hold of this newly introduced Test Network.

Written by

Happy to share what I learn on blockchain. Visit http://www.ledgertech.biz/kcarticles.html for my works. or reach me on https://www.linkedin.com/in/ktam1/.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store