Open In App

CLI API in Hyperledger

Last Updated : 03 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Hyperledger Fabric is an open-source permissioned blockchain platform that is designed to support the creation of scalable and secure distributed applications. It is part of the Hyperledger project, which is an umbrella project under the Linux Foundation that is focused on the development of open-source blockchain technologies. Hyperledger Fabric is designed to be modular and configurable, allowing developers to choose the specific components and features that they need to build their applications. It includes a permissioned network of peer nodes, a consensus mechanism, a system for managing the permissions of the nodes in the network, a chaincode system for implementing smart contracts, and a data storage model. Hyperledger Fabric is a powerful and flexible platform for building distributed applications that are based on blockchain technology. It provides a robust and scalable foundation for a wide range of applications, from simple contract-based applications to more complex distributed applications.

Command-line Interface (CLI) API

The CLI API is an important tool for working with Hyperledger Fabric, as it provides a simple and intuitive interface for performing many of the common tasks that are required when working with the platform. It allows developers and administrators to quickly and easily perform a wide range of tasks, without having to write custom code.

Some of the key features of the CLI API in Hyperledger Fabric include:

  •  The ability to install and instantiate chaincodes on the network, allows developers to deploy and test their chaincodes quickly and easily.
  • The ability to query the ledger, allows developers to view the current state of the ledger and the results of transactions that have been executed on the network.
  • The ability to manage the membership and permissions of the nodes in the network, allows administrators to add and remove nodes and assign different roles to them.
  • The ability to submit transactions to the network, allows developers to test their chaincodes and applications in a live environment.

How to use CLI API?

1. First, we need some environment variables, you can go to the test-network folder from fabric samples.  If you don’t know about the Hyperledger fabric test network, then refer to the article Hyperledger Fabric “Building Your First Network”

2. Then open your terminal and run these commands –

export PATH=${PWD}/../bin:${PWD}:$PATH

export FABRIC_CFG_PATH=${PWD}/configtx

export VERBOSE=false

3. Then run the following command to view the all CLI commands that are currently available:

peer –help

4. The terminal will look like this after executing the following commands:

peer --help

 

5. To know the version of the peer, use the following command:

peer version

6. The terminal will look like this after executing the following commands:

peer version

 

The peer command supports other subcommands, below are their description:

Command Description
version It displays the version of the peer node.
node start It starts the peer node.
node status It displays the current status of the peer node.
node stop It stops the peer node.
network login It is used to login into the Hyperledger Network.
network list It displays all the sets of Hyperledger Network.
chaincode deploy It is used to deploy a chaincode or smart contract to the Hyperledger Network channel.
chaincode invoke It is used to invoke and initialize the chaincode.
chaincode query It is used to make queries using any chaincode method.

Working with Chaincode using CLI

One of the most useful uses of the CLI can be working the chaincodes. One can invoke and deploy the chaincodes using the CLI.

1. The command for deploying packaged chaincode will be:

peer lifecycle chaincode install chaincode_packagename.tar.gz

2. The terminal will look like this after executing the following command –

peer lifecycle chaincode install chaincode_packagename.tar.gz

 

3. To invoke the chaincode the following command is used:

peer chaincode invoke -C $CHANNEL_NAME -n $CHAINCODE_NAME -c ‘{ “Args”: [“MyContract:methodName”, “{}”] }’

4. The actual command looks like this, for deploying a chaincode on test-network:

peer chaincode invoke -o localhost:7050 –ordererTLSHostnameOverride orderer.example.com –tls true –cafile /home/satyajit/Documents/SE/Indriya/test-network/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n INDRIYA –peerAddresses localhost:7051 –tlsRootCertFiles /home/satyajit/Documents/SE/Indriya/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt –peerAddresses localhost:9051 –tlsRootCertFiles /home/satyajit/Documents/SE/Indriya/test-network/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt –isInit -c ‘{“function”:”initLedger”,”Args”:[]}’

5. The terminal will look like this after executing the following command:

Invoke chaincode command

 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads