About Chaincode in HyperLedger Fabric

- 3 mins

About Chaincode in HyperLedger Fabric

We will go through a Sample chaincode and understand the following:

Chaincode

Chaincode is a program (smart contract) that is written to read and update the ledger state. All the business logic is inside the chaincode.

In Hyperledger Fabric, chaincode is the ‘smart contract’ that runs on the peers and creates transactions. More broadly, it enables users to create transactions in the Hyperledger Fabric network’s shared ledger and update the world state of the assets.

Chaincode is programmable code, written in Go, and instantiated on a channel. Developers use chaincode to develop business contracts, asset definitions, and collectively-manage decentralized applications. The chaincode manages the ledger state through transactions invoked by applications. Assets are created and updated by a specific chaincode, and cannot be accessed by another chaincode.

Applications interact with the blockchain ledger through the chaincode.Therefore, the chaincode needs to be installed on every peer that will endorse a transaction and instantiated on the channel.

Chaincode Key APIs

An important interface that you can use when writing your chaincode is defined by Hyperledger Fabric —

The ChaincodeStub provides functions that allow you to interact with the underlying ledger to query, update, and delete assets.

The key APIs for chaincode include:

Overview of a Chaincode Program

When creating a chaincode, there are two methods that you will need to implement:

As a developer, one must create both an Init and an Invoke method within your chaincode. The chaincode must be installed using the peer chaincode install command, and instantiated using the peer chaincode instantiate command before the chaincode can be invoked. Then, transactions can be created using the peer chaincode invoke or peer chaincode query commands.

Chaincode Decomposed — Dependencies

package main
import (
    github.com/hyperledger/fabric/core/chaincode/shim
    github.com/hyperledger/fabric/protos/peer
)

Chaincode Decomposed — Struct

type SampleChaincode struct {

}

This might not look like much, but this is the statement that begins the definition of an object/class in Go.

SampleChaincode implements a simple chaincode to manage an asset.

Reference

comments powered by Disqus
rss facebook twitter github gitlab youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora