hyperledger-fabrichyperledgerhyperledger-chaincodechaincode

What is the life cycle of a Hyperledger Fabric chaincode?


I'm trying to understand the life cycle of a Hyperledger Fabric chaincode and how it's different from the life cycle of a traditional application. Can someone explain the different stages that a chaincode goes through, and what happens at each stage?


Solution

  • Firstly, might be good to understand that 'peer chaincode lifecycle' commands were introduced only with Hyperledger Fabric version above 2. With version 1.x we only use commands of 'peer chaincode' family.

    With 2.x version, we now have both 'chaincode' and 'chaincode lifecycle' commands. Chaincode lifecycle are commands solely for deploying the smart contract.

    An example deploy flow would be something like:

    peer lifecycle chaincode package 
    peer lifecycle chaincode install 
    peer lifecycle chaincode queryinstalled
    peer lifecycle chaincode approveformyorg 
    peer lifecycle chaincode checkcommitreadiness 
    peer lifecycle chaincode commit
    peer lifecycle chaincode querycommitted
    

    Operations on the chaincode (after the successful deployment) are using the ("old") 'peer chaincode' commands. For example when you query or invoke chaincode.

    peer chaincode invoke
    peer chaincode query
    

    Stages of deploying the chaincode are as follows:

    1. Package and install chaincode

    The first step in the installation process is to package (.tar.gz) the chaincode, which requires installing the chaincode dependencies (please note that the steps are different if you want to install a smart contract written in Go, JavaScript, or Typescript). Then we install this package: e.g. peer lifecycle chaincode install sacc.tar.gz (The peer builds the chaincode when it is installed. The install command will return any build errors if there is a problem(s) with the code.)

    2. Approve chaincode

    After installing a chaincode package, you need to approve a chaincode definition for your organization. The definition includes important parameters such as the name, version, and the chaincode endorsement policy. The number of organizations that need to approve a chaincode before it can be activated is governed by the lifecycle endorsement policy. This policy, by default, requires that a majority of channel members need to approve a chaincode before it can be used on that channel.

    3. Commit chaincode

    Once a majority of channel members (organizations) have approved the chaincode definition, one organization can commit the definition to the channel.