Chaincode

In Chapter 1, Exploring Blockchain and BaaS, we learned about ledgers. There are different ledgers, for example, sales ledgers to store financial transactions, purchase ledgers to record spending and purchasing, while general ledgers record accounts, liabilities, expenses, income, and suchlike. Similarly, in HLF, transaction logs are the ledgers that record activities. These activities are also termed transactions. A ledger's world state tracks the transactions, which changes the world state of the ledger. The world state is a key-value pair, which is versioned, and, along with the transaction logs, it constitutes the ledger, which is available on all the participating nodes on the blockchain network. Now, the question is: who proposes changes to a ledger's world state? Well, the answer is dApps. Client applications issue transactions to the blockchain network via the execution of business logic in the chaincode, which are comprised of smart contract(s).

All the nodes are coordinated in terms of the latest copy of the smart contract.

Now, refer back to the diagram in the Ledger section earlier in this chapter. In that PiggyBank sample, the account balance of PiggyBank and Daddy, Mommy, and Noah's account shows the world view of the asset transfer (also known as transactions) that happened between various parties and the PiggyBank account. When Mommy and Noah take from PiggyBank, it is represented in block B2, as transactions T2 and T3. Here, the PiggyBank Version changes from 0 to 1, as it represents a change in value for the key, PiggyBank. Whenever there is a change in value, a new version is created. All those transactions are executed against the chaincode, which results in state changes in the work state. So, what is chaincode?

Chaincode (and its smart contract): This is a distributed computer program that is available across the HLF blockchain network. In the PiggyBank sample, it's the chaincode that enables the movement of assets (dollars) between accounts without the involvement of a third party or an intermediary. Daddy, Mommy, and Noah were able to perform transactions without a third party's involvement.

Chaincode, a computer program, is compiled and deployed across the blockchain network. Upon approval, chaincode is deployed to the distributed blockchain network. It runs in isolation from ordering services or peer processes, inside a Docker container. dApps logic is implemented in chaincode, where chaincode is used to initialize the ledger state and then manage it, which is handled by the SDKs. Chaincode can be invoked by client applications. Hence, two types of transactions are performed for chaincode—deploy transactions and invoke transactions.

  • Chaincode deploy transaction: Users, via participating nodes, author a chaincode (in Golang and similar) and use HLF SDK to issue a deploy transaction to the blockchain network. The permission to deploy chaincode is defined as access control and an ECert is issued by the HLF CA. Chaincode runs in a secure Docker container.
  • Chaincode invoke transaction: The dApps/client application will use SDK to propose a change to the world state of the ledger by issuing a transaction that results in an invoke to chaincode. Chaincode's invoke function is called by the application by passing the function name and transaction parameters defined in the chaincode function. Using the command line or via the SDK, query transactions can be executed against the invoke function of the chaincode. However, in this invoke function call, there is no proposal to change the world state of the ledger.

Peers execute the invoke transactions, and each committing peer commits a transaction by updating the local copy of the world state. We will get into a detailed analysis of chaincode, the registry, and suchlike in subsequent chapters. Hence, this chapter is limited to an introduction to chaincode.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset