Writing the Hello World in Hyperledger Fabric v1.3 (Chaincode)

A lot of resources out there explain in detail about your first Ethereum Smart Contract, but not a lot of resources explain about your first Chaincode (CC), or Hyperledger Fabric’s Smart Contract. So, this one tries to take a step forward and provide more detail into it.

We are going to write our first Hello World program in CC. The following are the functionalities we will be including in there:

  1. Set the firstString variable to Hello World when we instantiate
  2. Set the firstString variable to a new variable provided by the user
  3. Retrieve the firstString value

So, let’s get started.

Creating your own CC Object

Every CC is different and maybe requires some of the very basic attributes for the CC to work, like a particular set of certificates. The CC Object is used to define those.

Let’s name our CC Object as OurFirstChaincode and as we don’t need any specific attributes particular to our CC, we can keep the object as empty.

Instantiate the CC

While deploying our CC on the Fabric Network, we install the CC and then we instantiate. (This is the usual step towards deploying a HLF Smart Contract. Please refer the documentation is this is something new for you.)

During the Instantiate Phase, we initiate the CC by essentially writing all the required prerequisites on the chain for our DApp to work. This can be referred to as State 0 of your CC.

Instantiate requires your function to inherit the Init function. So, let’s write that.

So, let’s understand what is happening here.

We get the reference to the chain from the stub parameter and we will use that to write our value to the chain.

What HLF does is store the data as a key:value pair. So, anything that goes onto the chain, is a byte array which enables the ability to store anything on the Fabric Blockchain.

Hence, we keep the key="firstString" and value="Hello World" to be put on the chain. Now, that we have our key and value we can use the stub.PutState function to store the data on the chain. And the first parameter is the key and the second parameter is the byte array of the value.

Once, this goes onto the chain, if there is no error, we return a type of response, shim.Success(nil). We are not passing back any response back to the application, so we keep the parameter as nil.

If there is any error, we return the shim.Error back to the application in terms of error string as err.Error() .

Invoke the CC

Now, that we have Instantiated the CC, we can invoke state changes to the chain and update our variable value. So, let’s write the function to update the value of firstString to the new value passed by the user.

So, let’s see the function now. This is similar to the Init function we just understood above.

We did exactly the same thing as Init function but edited the name of the function, accepted a new array of values supplied by the application and using those value to write the new value of the firstString.

Query the CC

Now, that we have written the value to our firstString, next task is the ability to read the value stored on chain.

So, we write the logic to read the State of firstString from the Chain.

So, here we use the GetState function to get the read the value from the chain.

Final Steps

Now that we have all the functions defined, it is time to write a parent function which calls all of the above when the chaincode is invoked.

So, let’s see how we write this.

All we do here is call the required function when we receive the function name and pass the required parameters.

As this code is written in Go, we need to include an entry point to the CC. Hence, we write a main function to allow people to call functions in CC.

Now, that we have all parts, I think you are good to go and start writing a more complex CC.

Please post all the questions or queries that you might have in the comments section below.

Last Updated: 2018–11–17

All about Distributed Systems and Information Security. #golang #Cybersecurity #distributedsystems