In this series of blog posts, I will take you through the steps involved in the design, architecture, and implementation of a fully functional Enterprise Blockchain Application. I will do so by sharing content from various stages of developing a sample application for Loan Origination and Payment on Blockchain. If you follow through the end of this series, you will have a better understanding of the internal working of the blockchain application.
We are building this application in IBM Blockchain Platform 2.0 Beta. Please check out my previous post — 1. Design and Architecture of a Blockchain Application for better continuity.
Chaincode is the “smart contract” of Hyperledger Fabric. They encapsulate business logic and can be written in Go, Node.js or Java. Yes, Fabric is polyglot and can provide a runtime sandbox for the chaincode to run in the above-said programming languages.
A chaincode is typically used to read, write and update the ledger of the given channel. The fabric chaincode library provides useful APIs (shim) for chaincodes to access and update its states, get access to transaction context and also call other chaincodes. I will walk you through these with pseudocode of the chaincode we built for our sample application
A Chaincode that stores all loan requests
In our sample application, loanrequest.go is a simple chiancode which stores the loan applications of all applicants by applicant id and provides functions to manage the lifecycle of those loan request. The state stored in the ledger is actually a key-value pair with the key being the combination of applicant-id and loan request id, the value will be the details of the specific loan request. Let me breakdown the chaincode into smaller units for easy understanding
pay attention to “github.com/hyperledger/fabric/core/chaincode/shim” you will need this to do pretty much anything with the ledger
All chaincodes must implement “Chaincode” interface. This interface provides two key functions Init and Invoke.
Init will be called once when you instantiate the chaincode or upgrade a chaincode. Use this to initialize or migrate a state.
Invoke will be called to update or query the ledger — Invoke is the entry point to your chaincode from fabric client SDK
ChaincodeStubInterface provides useful functions to query and update the states in the ledger and to invoke other chaincodes. In our case, we will use PutState to store the new loan request onto the ledger in the default channel (by installing and instantiating this chaincode in default channel) and hence making it available for all the parties in the network.
As you can see the ledger stores a simple key-value pair with the key being request id mapped to a JSON representing the loan request
Pay attention to the stub.SetEvent this is an instruction for the chaincode to emit an event when the transaction (PutState) is committed. This is the event to which our Event hub will listen and react to.
Make sure you have only one event per transaction.
Use GetQueryResult to query your ledger. You can use “selector” to query the underlying CouchDB ledger like in the sample below.
In conclusion, you can write a simple chaincode using the shim interfaces Chaincode and ChaincodeStubInterface.
Hope you found this short post useful. The next post in the series will be on the Java Fabric client SDK showing how the chaincode functions can be invoked, integrating your enterprise applications to the blockchain behind the scenes. Hang tight you are only a few steps away from fully understanding the internals of a fully functional blockchain application.