Hyperledger Fabric 2.0 is introducing a new way of installing and instantiating chaincode between organizations. It is called Chaincode Lifecycle Management. Previous versions of Hyperledger Fabric are handling the chaincode lifecycle by using peer chaincode install on each organization in the network and then peer chaincode instantiate by an organization to run the smartcontract. These "old-fashioned" commands are still possible and will be supported (For now).
To use the new chaincode lifecycle commands, you have to make sure that you either create a channel that has the newest version or update your channel application capabilities to a supported version. In this article, we will explain each step in Chaincode Lifecycle Management and use it on an example smartcontract from the official fabric-samples directory.
The new Chaincode Lifecycle Management starts with two already known steps. The first step is to package your chaincode and then install it at the second step to all relevant organizations. In step three, each organization that is willing to use the chaincode must approve it. An additional requirement for using the chaincode is that per default the majority of organizations within a channel are approving the installed chaincode package. The last step is to commit (it is not called instantiate anymore) the smartcontract as current business logic to the channel. Every organization, who gave his approval in a previous step will be able to invoke the functions of the chaincode. We will deep dive into each step to make it clearer.
Part 1 of this tutorial will focus on starting the network with three organizations and package the chaincode. The second part will install the chaincode on each organization and only two out of three will approve the chaincode definition so we can prove the approval mechanism works as expected.
Demonstration of Packaging the chaincode in Hyperledger Fabric 2.0
Let us first start our network as fast as possible and then package an example chaincode for investigating the created tar.gz package. For creating this package, we will use the fabric binaries (You could also use any third-party application).
Note: Please always make sure to install the prerequisites before you start.
Open the terminal and move to the fabric-samples/test-network folder and bring the network up and running with an example channel.
./network.sh up createChannel
This official bash script will only add two organizations. So, we will have to add the third organization with an additional script which is in the subfolder.
cd addOrg3
./addOrg3.sh up
cd ..
For using the packaging commands, we must connect our local running terminal with the fabric binaries.
export PATH=${PWD}/../bin:${PWD}:$PATH
export FABRIC_CFG_PATH=$PWD/../config/
Now we can directly package any chaincode for the folder and choose the asset-transfer-basic chaincode. The peer lifecycle chaincode package command expects the name of the creating package, the path where it gets the source code from, the programming language of the source code, and a descriptive label.
peer lifecycle chaincode package basic.tar.gz --path ../asset-transfer-basic/chaincode-javascript/ --lang node --label basic_1.0
Based on the name we have given a basic.tar.gz file is packaged into your test-network folder. Hyperledger Fabric 2.x allows packaging your chaincode in tar.gz files, which will have two files inside. The source code packaged as code.tar.gz and the metadata.json file which was created by the command flags we set.
The screenshot is showing the content and the including metadata file to clarifying the attributes of the package. The attribute path defines were the source code is saved and will be packaged. Type is the programming language of the smartcontract. It can differ between golang and node. The last attribute is labeling the chaincode and will be defined by the label flag.
After packaging the chaincode we must install the basic.tar.gz on each peer. This will be part of the next blog entry. Did you get any trouble so far? Please leave a comment in the box below or just leave feedback. I appreciate that a lot.
Comments