Chaincode implementation

Chaincode is usually written in Golang or Java. Chaincode can be public (visible to all on the network), confidential, or access controlled. These code files serve as a smart contract that users can interact with via APIs. Users can call functions in the chaincode that result in a state change, and consequently updates the ledger.

There are also functions that are only used to query the ledger and do not result in any state change. Chaincode implementation is performed by first creating the chaincode shim interface in the code. Shim provides APIs for accessing state variables and transaction context of chain code. It can either be in Java or Golang code.

The following four functions are required in order to implement the chaincode:

  • Init(): This function is invoked when chaincode is deployed onto the ledger. This initializes the chaincode and results in making a state change, which accordingly updates the ledger.
  • Invoke(): This function is used when contracts are executed. It takes a function name as parameters along with an array of arguments. This function results in a state change and writes to the ledger.
  • Query(): This function is used to query the current state of a deployed chaincode. This function does not make any changes to the ledger.
  • 4(): This function is executed when a peer deploys its own copy of the chaincode. The chaincode is registered with the peer using this function.

The following diagram illustrates the general overview of Hyperledger Fabric, note that peers cluster at the top includes all types of nodes such as endorsers, committers, Orderers, and so on.

A high-level overview of Hyperledger Fabric

The preceding diagram shows that peers shown at the top middle communicate with each and each node has a copy of blockchain. On the top-right corner, the membership services are shown which validate and authenticate peers on the network by using a certificate authority (CA). At the bottom of the image, a magnified view of blockchain is shown where by existing systems can produce events for the blockchain and also can listen for the blockchain events, which then can optionally trigger an action. At the bottom right-hand side, a user's interaction is shown with the application which talks to the smart contract via the invoice() method, and smart contracts can query or update the state of the blockchain.

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

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