Hyperledger fabric deploys chain code (5) Initialization and chain code upgrade

After the chaincode definition is submitted to channel, it will be started on the peer node of the channel where the chaincode is installed. The asset-transfer (basic) chaincode can now be called directly by client applications via the command line. Use the following command to create an initial set of assets (initialization) on ledger. Please note that calling the command needs to be against a sufficient number of peers (including multiple organizations) to satisfy the chaincode endorsement policy. (Note that the CLI does not have access to Fabric Gateway peers, so each endorsing peer must be specified).

Chaincode initialization

peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/ tlscacerts/tlsca.example.com-cert.pem" -C mychannel -n basic --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" -c '{"function":"InitLedger","Args":[]}'

If the command executes successfully, you will see a response similar to the following in the terminal:

2020-02-12 18:22:20.576 EST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200

Chain code query

We can use the query method in the deployment chaincode to read the assets created by the chaincode:

peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'

The query result is

[{"Key":"asset1","Record":{"ID":"asset1","color":"blue"," size":5,"owner":"Tomoko","appraisedValue":300}},
{"Key":"asset2","Record":{"ID":"asset2","color":"red","size": 5,"owner":"Brad","appraisedValue":400}},
{"Key":"asset3","Record":{"ID":"asset3","color":"green","size": 10,"owner":"Jin Soo","appraisedValue":500}},
{"Key":"asset4","Record":{"ID":"asset4","color":"yellow","size": 10,"owner":"Max","appraisedValue":600}},
{"Key":"asset5","Record":{"ID":"asset5","color":"black","size": 15,"owner":"Adriana","appraisedValue":700}},
{"Key":"asset6","Record":{"ID":"asset6","color":"white","size": 15,"owner":"Michel","appraisedValue":800}}]

Chaincode update

For the chaincode update process, we can deploy a new version of the chaincode using roughly the same command as chaincode deployment:

  • Channel members upgrade chaincode by installing a new chaincode package, approving the chaincode definition with a new chaincode package ID, a new chaincode package version, and a serial number incremented by one. (New chaincode definitions can be used after they are submitted to the channel. This process allows channel members to coordinate when the chaincode is upgraded and ensures that there are a policy number of channel members ready to approve the new chaincode before it is deployed to the channel. code)
    Since the previous article describes how to deploy the chain code, the commands in the chapter of updating the chain code are simplified and will not be introduced in detail.
1. Configure environment variables
export PATH=${PWD}/../bin:$PATH
export FABRIC_CFG_PATH=$PWD/../config/
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp
2. Compile the chaincode package and assign a version number tag
peer lifecycle chaincode package basic_2.tar.gz --path ../asset-transfer-ledger-queries/chaincode-go/ --lang golang --label basic_2.0
3. Peer CIL configuration of org1
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/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:7051
4. Install the new version of chaincode based on org1
peer lifecycle chaincode install basic_2.tar.gz
5. Query the installed chain code

Through this step, query the PackageID of the new version of the chain code and the hash value of the corresponding chain code file (the terminal return value in step 4 of b also has relevant information)

peer lifecycle chaincode queryinstalled
6. Reference the new version of the chain code as an environment variable

(The package and hash will be updated later according to the actual situation)

export NEW_CC_PACKAGE_ID=basic_2.0:e28cb715087273c5f8972aa579bdcca6e8a683f6053b9a9a63ba43bd1dbdb9d5
7. org1 agrees to the update of the new version of the chain code
peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name basic --version 2.0 --package-id $NEW_CC_PACKAGE_ID --sequence 2 --tls --cafile \ "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem"
8. Peer CIL configuration of org2
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/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:9051
9. Install new version of chain code based on org2
peer lifecycle chaincode install basic_2.tar.gz
10. org2 agrees to the new version update
peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name basic --version 2.0 --package-id $NEW_CC_PACKAGE_ID --sequence 2 --tls --cafile \ "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem"
11. Check the command to see if enough organizations agree to update
peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name basic --version 2.0 --sequence 2 --tls --cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example. com/msp/tlscacerts/tlsca.example.com-cert.pem" --output json
12. Upgrade chain code command

After the new chaincode definition is committed, the chaincode on the channel will be upgraded. Until then, the previous chaincode will continue to run on peers in both organizations. Org2 can use the following command to upgrade chaincode (any organization can):

peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name basic --version 2.0 --sequence 2 --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"
13. View chain code upgrade results

You can check the container command through docker to see whether the version has been successfully updated.

docker ps -a
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7bf2f1bf792b dev-peer0.org1.example.com-basic_2.0-572cafd6a972a9b6aa3fa4f6a944efb6648d363c0ba4602f56bc8b3f9e66f46c-69c9e3e44ed18cafd1e58de37a70e2 ec54cd49c7da0cd461fbd5e333de32879b "docker-entrypoint.s…" 2 minutes ago Up 2 minutes dev-peer0.org1.example.com-basic_2.0-572cafd6a972a9b6aa3fa4f6a944efb6648d363c0ba4602f56bc8b 3f9e66f46c
985e0967c27a dev-peer0.org2.example.com-basic_2.0-572cafd6a972a9b6aa3fa4f6a944efb6648d363c0ba4602f56bc8b3f9e66f46c-158e9c6a4cb51dea043461fc4d35 80e7df4c74a52b41e69a25705ce85405d760 "docker-entrypoint.s…" 2 minutes ago Up 2 minutes dev-peer0.org2.example.com-basic_2.0-572cafd6a972a9b6aa3fa4f6a944efb6648d363c0ba4602f56 bc8b3f9e66f46c
31fdd19c3be7 hyperledger/fabric-peer:latest "peer node start" About an hour ago Up About an hour 0.0.0.0:7051->7051/tcp peer0.org1.example.com
1b17ff866fe0 hyperledger/fabric-peer:latest "peer node start" About an hour ago Up About an hour 7051/tcp, 0.0.0.0:9051->9051/tcp peer0.org2.example.com
4cf170c7ae9b hyperledger/fabric-orderer:latest
14. Remarks

The above chain code upgrade command does not include re-initialization of the chain code. If there is a large-scale transformation of the original data structure of the old version of the chain code, you can try to add the re-initialization parameter - in the process of upgrading the chain code. -init-required

  • Initialization parameters need to be added to the chain code update command agreed by each organization (the org2 command is taken as an example)
peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name basic --version 2.0 --package-id $NEW_CC_PACKAGE_ID --sequence 2 --tls --cafile \ "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem" --init-required
  • You need to agree to add this parameter to the consent command of each organization, and also add this parameter to the final chain code upgrade command.
peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name basic --version 2.0 --sequence 2 --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" --init-required

This allows the chain code to automatically execute the initialization function during the upgrade process.