top of page

Introducing the new Chaincode Lifecycle Management of Hyperledger Fabric 2.x (Part 2/2)

In the first part of this tutorial, we learned how to set up the environment and package the chaincode within a tar.gz file. The following figure describes the final goal of this tutorial and how we want to proceed through it.

We see in the upcoming figure that Org1, Org2, and Org3 will have installed the chaincode (as a usual behavior for chaincode management) but only Org1 and Org2 are going to approve the chaincode definition which is newly possible in Hyperledger Fabric 2.x. The approval is needed by default from at least half of all network participants to get the majority and be able to use it within the channel. In our case, it must be two out of three so it can be committed (the new word in the Chaincode Lifecycle Management for instantiating) to the channel and is activated for all organizations, who approved the chaincode.



We will start with the first organization to install and approve the chaincode. Therefore, we need to set the environment variables for Org1 (Please be aware that the current URL in your terminal is at fabric-samples/test-network).

export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=localhost:7051
 
peer lifecycle chaincode install basic.tar.gz

After installation, we will receive a unique package ID, which is generated with the label and hash of the chaincode binaries. Note: The ID has to be the same for each organization because you approve the ID and not the code itself. In our case, we will export it to CC_PACKAGE_ID as the official documentation did it and approve it.


export CC_PACKAGE_ID=basic_1.0:3e6efb14d2337620b1a651cf59a421157d354148b96434fd6d0ee582388fa3d6
 
peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name basic --version 1.0 --package-id $CC_PACKAGE_ID --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

We installed and approved the smart contract as it is defined in the Chaincode Lifecycle Management. The same commands will be done for Org2. So, we will set the environment variables, install the chaincode, and approve it to get a majority in the channel.

export CORE_PEER_LOCALMSPID="Org2MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
export CORE_PEER_ADDRESS=localhost:9051
 
peer lifecycle chaincode install basic.tar.gz
 
peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name basic --version 1.0 --package-id $CC_PACKAGE_ID --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

We are done with Org1 and Org2 and approved the chaincode. Org3 will not approve it so that the chaincode will only be installed.

export CORE_PEER_LOCALMSPID="Org3MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/tls/ca.crt
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org3.example.com/users/Admin@org3.example.com/msp
export CORE_PEER_ADDRESS=localhost:11051
 
peer lifecycle chaincode install basic.tar.gz

We prepared now all organization. For being 100% sure that everything is set up as expected the following command will show who approved the chaincode package and who don’t. You should see a JSON response with the MSP of each organization to verify the previous steps.


peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name basic --version 1.0 --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --output json

The last step is to commit (instantiate) the chaincode now to our channel and fulfill our figure from the beginning of this article. Therefore, we must connect to Org1 again and do the commit command. Let’s try.

export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=localhost:7051
 
peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name basic --version 1.0 --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt

The chaincode is committed now to the channel and is active. You can see in docker with command docker ps that there are two instances of the smartcontract are running.


For your homework, it would be awesome if you do a small test and check if you can invoke the smartcontract by each organization. You know now how to connect your terminal to any organization, and you should be able to figure out how to invoke the smartcontract. Org3 should throw you an error because it doesn’t give his approval. Let me know your results in the comments.

Conclusion


In my opinion, the new chaincode lifecycle management is a useful feature in Hyperledger Fabric and allows us to agree or disagree on the chaincode parameter within a channel and increases flexibility. For example, the endorsement policy modification or a private data collection are benefits that the Chaincode Lifecycle Management is bringing on the table.


Additionally, it is a huge benefit in upgrading your network much faster. This should help you on your way to developing a more complex platform where a lot of participants agree on some business logic and others don’t.


Additional tweaks will be provided in another article in the future. Please leave feedback and give me some of your thoughts.

5 Ansichten0 Kommentare
bottom of page