Debut Infotech
Published in

Debut Infotech

Deploying smart contract with the Hyperledger Fabric chaincode

Why smart contracts are smart?

In Hyperledger Fabric, smart contracts are deployed in packages known as chain codes. When two or more parties sign a contract and execute it, there are a lot of intermediaries that are involved in the process, making it lengthy, cumbersome, costly, and less credible.

A smart-contract that is based on Blockchain technology is faster, immutable, and has the ability to handle business models more effectively.

Deploying a smart contract

Blockchain technology emphasis on trust and changes the way the enterprises operate. Blockchain smart contracts are one of the most trustworthy way of publishing and saving contracts. This technol0gy helps to eliminate any third parties in the business operations, and increases the overall efficiency.

A Blockchain chaincode is deployed to a channel by means of the Fabric chaincode lifecycle. Let’s have a look at how smart contracts are deployed on the Hyperledger Fabric network:

1. Start the network

We will take the example of Fabric test network. Make sure that the pre-requisites and samples ae installed before the deployment process.

cd fabric-samples/test-network

./network.sh up createChannel

The createChannel command creates a channel i.e. mychannel. This channel further contains two-channel members, Org1 and Org2.

Following steps are used to deploy the chaincode to the channel:

Step One: Package the smart contract

A smart contract is packaged before it can be installed on the peers. There are diverse steps if want to install a smart-contract written in Go, JavaScript of Typescript.

Here we will look at the procedure to install the smart contract using Javascript:

Before the chaincode is packaged, there is a need to install the chaincode dependencies. Direct to the folder that contains the JavaScript version of basic chaincode:

cd fabric-samples/asset-transfer-basic/chaincode-javascript

To confirm that you can use the Peer CLI, check the version of the binaries. The binaries need to be a version of 2.0.0 or later.

peer lifecycle chaincode package basic.tar.gz --path../asset-transfer-basic/chaincode-javascript/ --lang node --label basic_1.0

The command will create a package named in the basic.tar.gz current directory.

Step two: Install the chaincode package

After we package the basic smart contract, we can install the chaincode on our peers.

Both the peers need to be installed as we are going to set the endorsement policy to require the authorizations:

Let’s install the chaincode on the Org1 peer first, set the following environment variables to operate the peer CLI as the Org1 admin user. The CORE_PEER_ADDRESS will direct to the Org1 peer, peer0.org1.example.com.

peer lifecycle chaincode install basic.tar.gz

peer0.org2.example.com

peer lifecycle chaincode install basic.tar.gz

After this command, the chaincode would be built. The install command will return any build errors from the chaincode, in case there is a problem with the smart contract code.

Step three: Approve a chaincode definition

After you have installed the chaincode package, it is important to approve the chaincode definition for your organization. The definition includes the important parameters of the chaincode governance.

peer lifecycle chaincode queryinstalled

Note: The package ID will not be the same for all the users.

Step four: Constraining the chaincode characterisation to the channel

An organization can obligate the chaincode definition to channel when a required number of organizations have approved the chaincode definition.

Step five: Invoking the chaincode

When we are done constraining the chain-code definition to a channel, the chain-code would start working on the peers that are joined to the chain-code channel.

The asset-transfer (basic) chaincode is now prepared to be appealed by consumer applications.

Remember that the invoke-command needs to target a sufficient number of peers to meet the chaincode authorization policy.

Clean Up

When you are finished using the chaincode, the following commands can be used to remove the Logspout tool.

docker stop logspout

docker rm logspout

You can then bring down the test network by issuing the following command from the test-network directory:

./network.sh down

--

--

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
Harpreet Singh

A pioneer in Mobile, Blockchain, Managed Services, Oracle, and AI/ML Development