Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to upgrade Intelligent contract in Fabric2.0

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to upgrade intelligent contracts in Fabric2.0, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's learn about it!

1. View the smart contract information that needs to be upgraded

A smart contract has been deployed to the peer0.org1.example.com,cli console to enter commands:

Peer lifecycle chaincode queryinstalled

View the package_id of the mycc contract as follows:

View the contract definition of mycc in the mychannel channel, and enter the command in the cli console:

Peer lifecycle chaincode querycommitted-C mychannel

You can see the details of mycc.

| | Parameter | description in Chinese | value |-| Name | contract name | mycc | Version | contract version | 1 | Sequence | contract serial number (1 upgrade plus 1) | 1 | Endorsement Plugin | endorsement plug-in | escc | Validation Plugin | Verification plug-in | vscc |

Performing a query operation is convenient for later comparison

Peer chaincode query-C mychannel-n mycc-c'{"Args": ["query", "a"]}'

The value of an output from the console is 90

2 modify the contract code

If the contract code does not exist locally, 2.0 provides the Operations console that gets the code package from the node, enter the following command

Peer lifecycle chaincode getinstalledpackage-package-id mycc_1:00ef9e95ea103b2c27eacd5a62efd9b34863c672d236a1ce99a7d539b2f9ef7a

Packge-id: use the contract packge_id obtained by querying in step 1

The current directory shows that the mycc.tar.gz package is a contract package, decompress the code.tar in the contract package, and edit the contract code to add a method plus 10 by default.

3. Repackage the contract

The console enters the following command to package the contract:

Peer lifecycle chaincode package mycc.tar.gz-path github.com/hyperledger/fabric-samples/chaincode/abstore/go/-lang golang-label mycc_1

Package the new intelligent contract package mycc.tar.gz according to the new contract code

4. Reinstallation contract

Updating the contract code requires the contract to be reinstalled on the node

Peer lifecycle chaincode install mycc.tar.gz

The console outputs contract installation information:

At this point, we will query the node installation contract information, and enter the following in the console:

Peer lifecycle chaincode queryinstalled

Console output:

You can see that a new mycc with different package_id has been added, and the original one is still there.

5. Modify contract definition

In fact, the real upgrade operation is to enter the following command in the console of the contract definition step. Compared with the original package_id, the newly installed contract package_id,sequence should be changed to 2, because install has been installed twice.

Peer lifecycle chaincode approveformyorg-tls true-cafile / opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem-channelID mychannel-name mycc-version 1-init-required-package-id mycc_1:2f358faa3475e5c37a90be9a7c0db2f608ecb09b13f64b001f83799be9fccc77-sequence 2-waitForEvent

Check the approve status and enter in the console:

Peer lifecycle chaincode checkcommitreadiness-channelID mychannel-name mycc-version 1-sequence 2-output json-init-required

Sequence should be changed to 2, because install has been done twice, otherwise Error: query failed with status: 500-failed to invoke backing implementation of 'CheckCommitReadiness': requested sequence is 1, but new definition must be sequence 2

6. The switching node repeats the operation of 3pm, 4pm and 5.

Switch the environment variable to peer0.org2.example.com, repeat 3, 4, 4 and 5 steps until the following results occur

7. Resubmit the contract definition

Resubmit the contract definition after completing the smart contract lifecycle strategy

Peer lifecycle chaincode commit-o orderer.example.com:7050-- tls true-- cafile / opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem-- channelID mychannel-- name mycc-- peerAddresses peer0.org1.example.com:7051-- tlsRootCertFiles / opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0 Org1.example.com/tls/ca.crt-peerAddresses peer0.org2.example.com:9051-tlsRootCertFiles / opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt-version 1-sequence 2-init-required

Note the serial number sequence

The console outputs the following results:

At this point, whether the value of an is queried through the new smart contract is still 90.

8. Check the new contract code

Just added addTen this method, theoretically a should add 10 equals 100console input the following command to call addTen

Peer chaincode invoke-o orderer.example.com:7050-- tls true-- cafile / opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem-C mychannel-n mycc-- peerAddresses peer0.org1.example.com:7051-- tlsRootCertFiles / opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example Com / tls/ca.crt-- peerAddresses peer0.org2.example.com:9051-- tlsRootCertFiles / opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt-c'{"Args": ["addTen" "a"]}'

Check the result a sure enough to upgrade successfully for 100

These are all the contents of the article "how to upgrade Smart contracts in Fabric2.0". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report