Compile and Deploy Hyperledger Fabric Chaincode


Compile and Deploy Hyperledger Fabric Chaincode

Recipe ID: hsts-r29


Self-paced training

We offer blockchain introduction, Hyperledger for system admin, Ethereum, Solidity, Corda R3, Hyperledger for developers, blockchain cybersecurity and more classes in self-paced video format starting at $60. Click here to learn more and register. For complete self-paced blockchain training, visit our Complete Blockchain Development Training page.


Recipe Overview

Hyperledger Fabric is an open source enterprise-grade platform that leverages a highly-modular and configurable architecture. Hyperledger Fabric is optimized for a broad range of industry use cases, including the finance, banking, healthcare, insurance, and public sectors, as well as supply chains and digital asset management.
For those who are not familiar with Hyperledger project Intro to Hyperledger Family and Hyperledger Blockchain Ecosystem, Hyperledger Design Philosophy and Framework Architecture, The Survey of Hyperledger Fabric Architecture and Components for Blockchain Developers and Overview of Building Blockchain Smart Contracts in Hyperledger articles are strongly recommended.
Hyperledger Fabric supports Smart Contact development in general-purpose programming languages, such as JavaScriptJava, Go, and Node.js. Hyperledger Fabric is also operating under a governance model to build trust between participants on a shared network.
We have written two sets of tutorials to explore Hyperledger Fabric in depth. First set covered the following six recipes:
It started with installing Hyperledger Fabric on an AWS EC2 virtual machine,  setting up the first Hyperledger Fabric network and working with Hyperledger Fabric Command Line Interface or CLI. We learned the following:

We moved on to show you how to Add New Network to a Channel, Use CouchDB as a State Database for Hyperledger Fabric, and Create a Smart Contract and then Deploy it into the Blockchain.
In short, in the previous recipes, we learned about how to set up and configure Hyperledger Fabric. We explored its key components, including channels, Membership Service Providers (MSPs), the ordering service, and Fabric Certificate Authority (CA).
The second set, we will show you how to build a simple device asset management DApp. It consists of 6 recipes as follows:

In summary, in the second set of recipes, we are going to build a simple device asset management DApp. We will exploit this example by writing chaincode implemented by various programming languages and we'll also build, test, and deploy our DApp.

IMPORTANT: Understanding and completing the first set of recipes are required prior to working on second set of recipes.

We have now successfully written our asset management chaincode using the Go language. It is now time to build and deploy our assetmgr chaincode to Hyperledger Fabric.

Compile Go Codes

Let's first get the fabric library in our environment. Navigate to the assetmgr directory, run the get chaincode library command, and then start build:

cd $GOPATH/src/assetmgr

go get -u github.com/hyperledger/fabric/core/chaincode/shim go build

This will load the chaincode library and compile the Go code. Next, we will deploy the chaincode using the dev mode. Normally, we need to define our own channel, peer, and configuration Docker container to run our chaincode. Hyperledger, however, provides a sample dev network with a pre-generated orderer and channel artifact. This allows the user to start using chaincode for quick development and testing. You should have already set up the Fabric runtime environment with the fabric-samples project. If you haven't already done so, check out the previous recipes.

At the time of this writing, 1.4 is the latest version of fabric-samples. In our example project, we use the build in the fabric-samples project and set this same project as the default user home directory, as follows:

Hyperledger Fabric with Golang

Let's now open three Terminals and navigate to the chaincode-docker-devmode
directory of fabric-samples:

$ cd chaincode-docker-devmode

We will start a sample Fabric network to provide the Fabric runtime environment, and then package and build our Composer. Finally, we will deploy it to the network.

Starting Sample Fabric Network
Open Terminal one. This Terminal will start the sample Fabric network. Issue the following command:

docker-compose -f docker-compose-simple.yaml up

This will bring up a network with the SingleSampleMSPSolo orderer profile. It also launches peer nodes, cli, and chaincode containers.

Building and Deploying Chaincode

1. Open Terminal two. This Terminal will build and deploy the chaincode. Since we write and build the chaincode from our local Unix system, the chaincode is not yet in the Docker containers. Run the following command in the chaincode-docker-devmode folder:

docker exec -it chaincode bash

2. The output will be a list of folders:

abac chaincode_example02 fabcar marbles02 marbles02_private sacc

3. Let's create an assetmgr folder:

mkdir assetmgr

This will create an assetmgr folder in the Fabric container. First, type exit. This will exit the container and return to the chaincode-docker-devmode folder. Check the Fabric chaincode container ID by typing docker ps. You will get a similar result to the following:

hyperledger fabric and chaincode

In our example, the chaincode container ID is dbf9a0a1da76. The peer port is 7051.

4. With the container ID, we can copy the local chaincode to the chaincode container. Run the following command:

docker cp ~/itasset/src/assetmgr/assetmgr.go dbf9a0a1da76:/opt/gopath/src/chaincode/assetmgr

5. Launch the chaincode container again:

docker exec -it chaincode bash

6. Navigate to the assetmgr folder and execute the go build command. This will compile our assermgr.go in the chaincode container, as shown in the following screenshot:

hyperledger fabric and golang

The assetmgr chaincode can be found at the following path: /opt/gopath/src/chaincode/assetmgr.

7. Run the chaincode by providing the peer address and chaincode ID name. The command is as follows:

CORE_PEER_ADDRESS=peer:7052 CORE_CHAINCODE_ID_NAME=mycc:0 ./assetmgr

8. This command will deploy the chaincode to the peer node at 7052. If you don't see any errors, the chaincode will start with peer. The log indicates that assetmgr.go successfully registered with peer:

Hyperledger fabric development

Put Things Together

Here, we deployed the chaincode to the peer node. Let's now take a look at how chaincode-docker-devmode defines the blockchain configuration. chaincode-docker-devmode has some predefined configuration files and scripts. Here are the files in chaincode-docker-devmode:

hyperleder fabric

Let's take a closer look at the docker-compose-simple.yaml file:

 

Here is the screenshot we see after we bring up the Docker containers. We can see the previously mentioned four containers running:

hyperledger fabric chaincode

The script file in chaincode-docker-devmode only contains the following two commands:

peer channel create -c myc -f myc.tx -o orderer:7050 peer channel join -b myc.block

The first command creates the myc channel using the specified configuration file in the myc.tx file. The myc.tx file is generated by the configtxgen tool. This tool also generates orderer.block.

The second command joins the created channel with myc.block to the cli container. With these four containers, we can deploy our chaincode to the Fabric in the development environment.

Let's now carry out some tests from the CLI container in our next Recipe.

The following recipes are excellent resources for installing other Hyperledger tools:

Blockchain Developer Guide- How to Install and work with Hyperledger Sawtooth
Blockchain Hyperledger Composer Business Network Modeling and Environment Setup
Blockchain Developer Guide- How to Install Hyperledger Burrow on AWS
Blockchain Developer Guide- How to Install Hyperledger Iroha on AWS
Blockchain Developer Guide- How to Install Hyperledger Indy and Indy CLI on AWS
Blockchain Developer Guide- How to Install Hyperledger Seth and Docker on AWS

To conclude this recipe, we like to recommend our Blockchain Hyperledger Development in 30 hours course to those interested in pursuing a blockchain development career. Indeed, as of this writing, Hyperledger Foundation offers the following two Hyperledger certifications: The Certified Hyperledger Fabric Administrator (CHFA) and The Certified Hyperledger Sawtooth Administrator (CHSA), both of which are highly regarded in the industry. Hyperledger Foundation is in the process of creating Hyperledger Developer certification program, which may be released in early or middle of 2020. In short, by taking our hands-on online Hyperledger class, you would be able to obtain CHFA certification.

This recipe is written by Brian Wu who is our senior Hyperledger instructor in Washington DC. His Hyperledger Cookbook with 40+ hands-on recipes is highly recommended.

Related Training Courses

Hands-on Node.JS, MongoDB and Express.js Training
Advance JavaScript, jQuery Using JSON and Ajax
Learn Hands-on Blockchain Ethereum Development & Get Certified in 30 Hrs
Learn Blockchain Hyperledger Development & Get Certified in 30 Hrs
Become Blockchain Certified Security Architect in 30 hours
Blockchain Certified Solution Architect in 30 hours
Introduction to Python Programming
Object Oriented Programming with UML


Private and Custom Coding Classes

We offer private coding classes for beginners online and offline (at our Virginia site) with custom curriculum for most of our classes for $59 per hour online or $95 per hour in virginia. Give us a call or submit our Private Coding Classes for Beginners form to discuss your needs.


View Other Classes!