Understanding Chaincode

Lets look at our first chaincode application and deploy it to our basic-network.

We'll cover the following

Lets start with a simple chaincode to ensure we understand the basic concepts.

We will use a simple Land Record(landrec) chaincode which will do CRUD operations on basic land records.

We will be using javascript(node) as the language for our chaincode. GO is also supported though.

We will use the fabric-contract-api node package to write our chaincode.

How chaincode works #

Chaincode application is simple. It stores and retrieves data from underlying world-state databases and can hold any logic. Since couch db is a key-value based db, it is quite simple. Based on the data it changes in the world-state the fabric peer itself generates the transaction with “changed” data.

The fabric-contract-api requires our chaincode class to extend the Contract class.

  • The async initLedger(ctx) method is called whenever a chaincode is initialized.
  • We can add more methods depending on what functionality we want our chaincode to expose. There are a couple of important methods passed in context object that allow us to store and retrieve data from the key value store.

Look at the sample code below. There are two functions supported by this chaincode. CreateLand and QueryLand. Once this chaincode is deployed, any application can call these methods given that it calls it signed with a valid user’s private key whose certificate is issued by our CA. Important lines are highlighted

Get hands-on with 1200+ tech skills courses.