Creating and Deploying a Custom Chain Code for Hyper ledger Fabric

Golam mohammad Shovon
Coinmonks
7 min readJan 22, 2020

--

In this tutorial we are going to learn to create a custom chain code (smart contract). The chain code is based on the fabcar chain code in the hyper ledger official tutorial. I have reverse engineered it to work as a primitive food regulation chain code.

First we will take a look at the chain code. We will do it part by part.

This part consists of necessary imports for the go chain code.

Then we make a structure for the chain code.

We made a struct called stock which contains the necessary data set needed for the smart contract.

Now we take about the instantiation and invoke part. Chain code needs to be instantiated to work in the peers.

In the invoke part we call the various functions of the chain code to work.

Now for the rest of the chain code.

This portion of the code deals with functions in the chain code. The first function is queryStock, it takes one argument key of the stock, and it gives you details. The next function is initLedger , it populates the ledger with some data to query and do other stuffs with it. QueryAllStocks name is definition enough. assignStock is used to assign a stock to a different retailer. The main function is for unit testing only.

Now the final code:

Now we must compile it first. For compiling go chain code. We must keep the file in the go path. Go path setting can be learned from this link:

The we will build the code using the command.

We will have a compiled go file now.

The compiled go file and the go file then be placed in the path specefied in the compose-cli file.

The file is also given here for better understanding. If you want to know what is docker-compose-cli and what is it necessary for it can be found here.

I have used another network in this tutorial but the concept is same.

In this network we have 3 organization and each organization have one peer. The volume part where we specified for the chain code is not absolute. It’s a relative path , it relative to the gopath.

We will install the chain code in the peers following this procedure. After starting the network and joining the peers to a channel. More data for this can be found here:

Again the network is different but the concept is same.Remember all the command must be issued while you are inside the network folder.

We will enter each following this procedure:

The general command structure for this is:

For Customer Organization peer 0 the command is:

After entering the peer we will first import certificate file.

The channel name here is: channelshovon

Now we will install chain in peer 0 of customer org. The chain code is inside the food code folder in the chain folder.

The generic command for this is:

Now we can check the list of installed chain codes using this commadn.

Output:

We can see now the chain code is installed.Now we must instantiate it for further use.

Command:

The output will be like this:

Now we can use functions in the chain code. First we will initLedger for further use. Then we will use queryAllStocks,createStock,queryStock,assignStock and see their uses and output.

The outputs can be seen in the images.

The chain creates a shared ledgers between connected peers.

This is way a chain code can be deployed in a hyper ledger fabric network.

Get Best Software Deals Directly In Your Inbox

--

--

Golam mohammad Shovon
Coinmonks

A noob software engineer. Always interested to learn new things.