Setting up a Hyperledger Fabric Network with Multiple Chaincodes and Multiple Channels
Varun Raj, Co-founder and CTO
Hyperledger Fabric is an enterprise grade framework for building permissioned blockchain networks. It’s a project supported and managed by the Linux Foundation. If you’re getting started with HL Fabric I’d strongly recommend you to read our previous articles to understand the architecture.
Terminologies
Before getting started with this, I’ll explain what exactly are channels and chaincodes.
Channels
Channels are the state store of an blockchain network which hold the application data generated during the lifecycle of the system. It’s used to store information like user data, purchase data, order data, invoices and more. In a Fabric network there can be multiple channels to provide privacy and security to the parties involved.
For instance if there are two parties who want have some data shared only between them, they can create a channel and store the corresponding information within that channel so that none of the other participants will be able to see that data.
Chaincodes
Chaincodes are the smart contracts that validate the each transaction that execute on the peer node. In a Fabric network chaincodes are the business logic to decide on what to be done on some data. Similar to channels, chaincodes can also be multiple in a network which helps to abstract the functional aspect of the network so that only allowed organizations will be able to invoke the chaincode.
For instance in a supply chain network, all the organizations will be able to record the sale data but only one organization will be managing and monitoring the entire network like an admin. Thus we can have one chaincode for the supplychain logic and one for admin operations like adding a new organization.
Since HL Fabric is a enterprise grade blockchain framework, it provides a lot of options for imparting security on to your network, one important feature being the multi ledger and multi channel network. This article will help you understand how to setup an Fabric network that involves multiple chaincodes and multiple channels.
Prerequisites
Before proceeding to the setting up part, please make sure the following processes are performed.
Installing Docker
Since entire Hyperledger Fabric is running on Docker, it’s necessary that you’ve it installed in your development machine.
Pulling the Images
Once you’ve installed the docker, pull the Hyperledger Fabric images from the repository with the following commands.
And tag them as latest,
Finally verify by listing the docker images to check if everything is installed correctly.
Defining the Architecture
In our current example, we’ll be having the following components.
1 Orderer (orderer.example.com)
2 Organizations (org1, org2)
1 Peer per organization (peer0.org1.example.com, peer0.org2.example.com)
1 cli (fabric-tools)
This setup is mainly to make the process simpler. However, in production you’ll be having a much more complex setup for scalability. Also there will be cli container to create channels, join them and also install and instantiate chaincodes.
And we’ll be having two channels (channelone & channeltwo) and two chaincodes (firstchaincode & secondchaincode). Here the firstchaincode will be installed in channelone of Org1 & Org2 and the secondchaincode will be installed in channeltwo of Org2 alone. Thus secondchaincode and channeltwo will be a private one for Org2 alone.
Setting Up The Environment
As the first step, we need to create few basic things that is needed for running the Fabric network. Create a new folder in a suitable location.
Pulling Platform Tools
The platform tools are necessary to create the certificates and configuration transaction blocks to setup the network. It’s specific to each platform thus you need to manually download it with the following script.
This will create a bin folder in the current location, which contains the tools for the corresponding operations.
Next step is to create the following files and its content to start the network
docker-composer.yml - Which has our docker container’s configurations
crypto-config.yml - Which has the orderers and peers informations for generating the certificiates
configtx.yaml - That has the configuration information for building the genesis block and channel transaction files.
Docker Container Configuration
The containers are defined in the Docker compose file. This holds the configuration for the architecture. Copy paste the below content docker-compose.yml
Crypto Config
Configtx.yaml
As we mentioned above, we’ve create profiles for two channels ChannelOne and ChannelTwo. This will be used for generating channel transaction blocks in the following steps.
Set the Environment Variables
Before proceeding to the scripts, for easier usage I’ve used bunch of environment variables that will be used in the scripts. Export them in your terminal before proceeding to the scripts.
Generating the Certificates & Configuration Transaction Blocks
After defining the configuration, let’s get on to generating the certificates and the transactions blocks.
Generate Certificates
We’ll be using the cryptogen tool to generate the certificates for the defined organisations and users. It takes the crypto-config.yaml file as input and generates the certificates in crypto-config folder.
Generate Genesis Block
After generating the certificates, we have to create the first block of our network which is called the genesis block. We’ll be using the configtxgen tool to generate it from configtx.yaml.
Generate Channel Transactions
For each channel we will have to create a config transaction which is will be used when creating the channel.
Generate Anchor Peer Update Profile
In order to update a peer to anchor peer, we have to create a transaction block signed by the corresponding OrgMSP.
After completing these steps you can see that there will be two newly created folders in your project repo, the channel-artifacts & crypto-config. As you can see in the docker-compose file, these will be mounted in the peer and orderer volumes for the certificates to consume during the function of each of the nodes.
Starting The Network
Since we have all the certificates now, we can start all the components of the Fabric network. All we have to do is to start the docker services defined the docker-compose file.
After this command is executed successfully, just check if everything is running with the following command.
This will return all the running Docker containers and make sure you have 1 orderer, 2 peers, 2 couchdb and a cli running.
Channel Creation & Joining
We’ll be using the cli container to create and join the channels. While executing the commands from the cli container, the commands will be executed as a peer based on the environment variables set in the cli container. Since we have defined the Org1MSP as the default variable all the commands executed from the cli will be run as Org1 peers and when we need to change it to some other organization, we need to pass the corresponding environment variables during docker exec.
Creating Channel One as Org1 Peer
The channel creation is done by using the transaction tx that’s created in channel-artifacts folder.
Join the Channel One as Org1 Peer
After you create the channel, a new file named channelone.block will be created in the cli container which will be used to join the peer to the channel.
Join the Channel One as Org2 Peer
Similarly we’ll change the environment variables to make the cli act as Org2 Peers and join the channel.
Update the Anchor Peers in Channel One
After joining the channel, we’ve to update the peers of each organization that will be acting as the anchor peer. In order to do that we’ll be using the anchorpeerupdate tx file that’s generated.
Org1 Peer
Org2 Peer
Creating Channel Two as Org2 Peer
Joining Channel Two as Org2 Peer
Update the Anchor Peers in Channel Two
Installing and Instantiating the Chaincodes
As per our architecture, we have created two channels and now we have to installed the corresponding chaincodes in the corresponding channels.
In our current example, we have two chaincodes each under ./chaincode/one & ./chaincode/two in the root folder. Also we have mounted the entire folder into the cli container under,
Export Chaincode Name And Location Variables
We’ll store the chaincode name and the path in the environment variable to access it easily
Install Chaincode (firstchaincode) in Org1 Peer
For installing the chaincode we’ll use peer chaincode install command, and we have to pass the name, location and the version of the chaincode to be installed. This process will install the runtime for the chaincode.
Instantiate Chaincode (firstchaincode) in Org1 Peer
After installing the chaincode runtime, we have to instantiate to make it functional. This command needs TLS signing for the verification of the installer. The chaincode will be instantiated in the channelone of the network,
Install Chaincode (firstchaincode) in Org2 Peer
Instantiate Chaincode (firstchaincode) in Org2 Peer
Install & Instantiate (secondchaincode) in Org2 Peer
The second chaincode is the private one (Like admin chaincode). This will be installed in only a specific organisation which will have rights to share the data. Thus in our scenario, we’ll install it only in Org2 peers and in channeltwo of the network.
With this part our entire network setup is over and we’re good to test the network. If we look at the couchdb admin, it’ll have the database for the corresponding channels and the chaincodes.
CouchDB of Org1
As we can see below it has one only one chaincode and one channel (channelone_firstchaincode)
CouchDB of Org2
And the org2 has one for each chaincode and channel (channelone_firstchaincode & channeltwo_secondchaincode) Thus org2 can read and write to both the channels with corresponding chaincode.
Hope this helps you to understand how to setup an Hyperledger Fabric Network with multiple channels and multiple chaincodes. This will be really helpful when you grow your network and start thinking about separating the chaincode logic to multiple parts.