Work with Hyperledger Fabric CLI, Network and Chaincode


Work with Hyperledger Fabric CLI, Network and Chaincode

Recipe ID: hsts-r25


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.
In our previous recipes, we have successfully installed Hyperledger Fabric on an AWS EC2 virtual machine and Building Hyperledger Fabric Network for Blockchain Application. By setting up the first Hyperledger Fabric network, we learned the following:

In this recipe, we will work with Hyperledger Fabric Command Line Interface or CLI to set up network and manage chaincode. Specifically, we cover the following:

Hyperledger Fabric Network and Fabric CLI

Follow below steps to create Fabric network:

1. Use the peer CLI to set up the network. Using the peer command line within the Docker CLI container for this step, we will create the channel using channel.tx so that peers can join the channel. Please note that some commands are extremely long as we need to set up peer environment variables (note that the default is peer0.org1), as follows:

$ cd ~

cd fabric-samples/first-network

 sudo docker exec -it cli bash

export CHANNEL_NAME=mychannel

 

$ peer channel create -o orderer.example.com:7050 -c

$CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls -- cafile/opt/gopath/src/github.com/hyperledger/fabric/peer/ crypto/ordererOrganizations/example.com/orderers/ orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem $ peer channel join -b mychannel.block

for peer0.org2

 

$

CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/

fabric/peer/crypto/peerOrganizations/org2.example.com/

users/Admin@org2.example.com/msp

CORE_PEER_ADDRESS=peer0.org2.example.com:7051

CORE_PEER_LOCALMSPID="Org2MSP"

CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/

hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/

peers/peer0.org2.example.com/tls/ca.crt

$ peer channel join -b mychannel.block

// for peer1.org1

CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/

hyperledger/fabric/peer/crypto/peerOrganizations/

org1.example.com/users/Admin@org1.example.com/msp

CORE_PEER_ADDRESS=peer1.org1.example.com:7051

CORE_PEER_LOCALMSPID="Org1MSP"

CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/

hyperledger/fabric/peer/crypto/peerOrganizations/

org1.example.com/peers/peer1.org1.example.com/tls/ca.crt

peer channel join -b mychannel.block

// for peer1.org2

CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/p

eer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.

com/msp CORE_PEER_ADDRESS=peer1.org2.example.com:7051

CORE_PEER_LOCALMSPID="Org2MSP"

CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabr

ic/peer/crypto/peerOrganizations/org2.example.com/peers/peer1.org2.exam

ple.com/tls/ca.crt peer channel join -b mychannel.block

This will create a connection between all four peers:
hyperledger fabric setup

 

2. Update the anchor peer on each organization. We use the files we created in the Installing Hyperledger Fabric on AWS section (Org1MSPanchors.tx and Org2MSPanchors.tx) and apply them to Peer0 of both Org1 and Org2:

$ peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org1MSPanchors.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ ordererOrganizations/example.com/orderers/orderer.example.com/ msp/tlscacerts/tlsca.example.com-cert.pem

$ CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/ fabric/peer/crypto/peerOrganizations/org2.example.com/

 

                      users/Admin@org2.example.com/msp

CORE_PEER_ADDRESS=peer0.org2.example.com:7051

CORE_PEER_LOCALMSPID="Org2MSP"

CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/

hyperledger/fabric/peer/crypto/peerOrganizations/

org2.example.com/peers/peer0.org2.example.com/tls/ca.crt

peer channel update -o orderer.example.com:7050 -c

$CHANNEL_NAME -f ./channel-artifacts/Org2MSPanchors.tx

--tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/ crypto/ordererOrganizations/example.com/orderers/ orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

3 Using the CLI, we need to install the chaincode to peer0 Org1 and peer0 Org2. The chaincode is specified in the -p option in the command and the chaincode name is mycc. This is shown in the following code:

peer chaincode install -n mycc -v 1.0 -p

 

github.com/chaincode/chaincode_example02/go/

$ CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/

fabric/peer/crypto/peerOrganizations/org2.example.com/users/Adm

in@org2.example.com/msp

CORE_PEER_ADDRESS=peer0.org2.example.com:7051

CORE_PEER_LOCALMSPID="Org2MSP"

CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperled

ger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers

/peer0.org2.example.com/tls/ca.crt peer chaincode install -n

mycc -v 1.0 -p

github.com/chaincode/chaincode_example02/go//orderers/orderer.e

xample.com/msp/tlscacerts/tlsca.example.com-cert.pem

4. Instantiate the chaincode from peer0.org2. We will use -c to initialize this with a value of 100, and b with 200. We use -p to define the endorsement policy. This is shown in the following code:

$ CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/

fabric/peer/crypto/peerOrganizations/org2.example.com/users/Adm

in@org2.example.com/msp

CORE_PEER_ADDRESS=peer0.org2.example.com:7051

CORE_PEER_LOCALMSPID="Org2MSP"

CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperled

ger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers

/peer0.org2.example.com/tls/ca.crt peer chaincode instantiate -

o orderer.example.com:7050 --tls --cafile

/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/order
erOrganizations/example.com/orderers/orderer.example.com/msp/tl scacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a", "100", "b","200"]}' -P "AND ('Org1MSP.peer','Org2MSP.peer')"

5. Execute a query on peer0 Org1 on the a value. We should get the correct value of 100 back:

6. Using the CLI, create a transaction by invoking chaincode. In this example, we will move 10 from a to b. Install chaincode on peer1 org2, and then query from peer1 org2 for the latest value of a:
hyperledger fabric setup

$ CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/

fabric/peer/crypto/peerOrganizations/org2.example.com/users/Adm

in@org2.example.com/msp

CORE_PEER_ADDRESS=peer1.org2.example.com:7051

CORE_PEER_LOCALMSPID="Org2MSP"

CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperled

ger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers

/peer1.org2.example.com/tls/ca.crt peer chaincode install -n

mycc -v 1.0 -p github.com/chaincode/chaincode_example02/go/

$CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger

/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Ad

min@org2.example.com/msp

CORE_PEER_ADDRESS=peer1.org2.example.com:7051

CORE_PEER_LOCALMSPID="Org2MSP"

CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperled

ger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers

/peer1.org2.example.com/tls/ca.crt peer chaincode query -C

$CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'

This takes some time, but we will eventually receive the result of 90, which is correct after 10 is removed from 100:
hyperledger fabric setup

This concludes building our first Fabric network. We will look at how to make changes to the existing network and add an organization to a channel in the next recipe.

Putting Things Together

We covered the following steps to build our Fabric network:

This recipe helps you to understand the Hyperledge Fabric components and shows how we can quickly set up a Hyperledger Fabric network using sample chaincode (mycc). You should be able to modify the scripts and run other samples, such as fabcar and marble02, which are provided under the fabric-sample/chaincode directory.

Fabric provides the following commands used in the byfn.sh script. These commands can be used to operate and manage the Fabric network environment:

Now that we have set up our first network on Hyperledger Fabric, we move on to the following 3 steps or recipes:

The following recipes are excellent resources for installing other Hyperledger tools:
Blockchain Developer Guide- Overview of Hyperledger Explorer and its Development Environment
Blockchain Developer Guide- How to Install Hyperledger Indy and Indy CLI on AWS
Blockchain Hyperledger Composer Business Network Modeling and Environment Setup
Blockchain Developer Guide- How to Install Hyperledger Iroha on AWS
Blockchain Developer Guide- How to Install Hyperledger Burrow on AWS

To conclude this recipe, we like to recommend our Learn Hands-on Blockchain Ethereum Development & Get Certified in 30 Hrs and Blockchain Hyperledger Development in 30 hours courses 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 tutorial 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!