In our previous posts, we talk about chaincode and state database but we haven't covered anything on coding chaincodes and this post will touch base on that and explain how chaincode looks like and how you can create chaincode in hyperledger fabric.
Very first thing, you may be asking, Is it easy to write and understand chaincodes in hyperledger fabric ?, answer to this question is yes, it is simple to understand and write chaincodes but you need to understand the basics first.
How many ways to write chaincode in hyperledger fabric ?
Right now, you can write chaincode in Go and nodejs. Java will be supported in upcoming version.
In this post, we will talk about chaincode in Go language. Go is very powerful language from Google.
Let's get to work and understand chaincode.
At high level, you can categories chaincode in below sections.
- Import section
- Struct section
- Init function section
- Invoke function section
- Custom functions section
- Main function section
As the name suggests, this section contains the details about import statements as shown below.
fmt - contains Println for debugging/logging
github.com/hyperledger/fabric/core/chaincode/shim - contains the definition for the chaincode interface and the chaincode stub, which you will need to interact with the ledger, as we described in the Chaincode Key APIs section
In this section, you define the Chaincode type. In below example, TestChaincode is type that will implement the chaincode functions, as shown in the following snippet.
Init function Section
In this section, Init function of Chaincode interface is implemented. Init is called during the chaincode instantiation to initialize data required by the application.
Invoke function Section
In this section, Invoke function of Chaincode is implemented. Invoke method, which gets called when a transaction is proposed by a client application.
As shown above, GetFunctionAndParameters function from shim package helps to get the function name and arguments to that function. In this example, SDK can either call setValue() function or getValue() function with required arguments.
function name is getting saved in fn and arguments in args. Later you can check the function name and based upon that invoke custom functions such as setValue or getValue.
Custom function Section
As shown above, Invoke function can further call custom defined functions. These custom functions can be defined as per the requirement.
Below is definition of setValue() function, that set the key and value.
The setValue method will modify the world state to include the key/value pair specified. If the key exists, it will override the value with the new one, using the PutState method; otherwise, a new asset will be created with the specified value.
Below is definition of getValue() function, that provide the value based upon key.
The getValue method will attempt to retrieve the value for the specified key. If key is not passed in the arguments then error is returned and if key is there then GetState function is called and that in turn query the state database and get the value for that key.
Main function Section
Last section is main function section and this is the starting point of chaincode.