In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to achieve chain code management in Fabric2.0, the article introduces in great detail, has a certain reference value, interested friends must read it!
4.1 installation chain code
The Fabric-sample project comes with many sample chain codes, which are located in the fabric-samples/chaincode directory. This article tests and installs the go version of the abstore project to org1.peer0 and org2.peer0 nodes.
After the chain code is installed successfully, you can call the chain code to complete the transaction operation.
To install the chain code, you need to first enter the cli container and execute the following command:
Docker exec-it cli bash4.1.1 chain code package
Chain code packaging executes the following command:
Peer lifecycle chaincode package abs.tar.gz-path github.com/hyperledger/fabric-samples/chaincode/abstore/go/-lang golang-label abs_1
Abs.tar.gz: package contract package file name
-- path: smart contract path
-- lang: intelligent contract language that supports golang, Java and node
-- label: smart contract tag, which acts as a description
After successful execution, the file abs.tar.gz will be generated in the current directory.
4.1.2 chain code installation
This section installs the above packaged code on both the org1.peer0 and org2.peer0 nodes, so you need to switch environment variables.
Use the command to see the environment that is currently using org1.peer0.
By executing the chain code installation command, the above packaged contract code can be installed on the org1.peer0 node.
Peer lifecycle chaincode install abs.tar.gz
The implementation results are as follows:
View the installation results and execute the command:
Peer lifecycle chaincode queryinstalled
Next, install the smart contract code for org2.peer0 and execute the following command to switch the environment variables:
Source scripts/utils.shsetGlobals 0 2
View the switching result, as shown in the following figure:
By executing the chain code installation command, the above packaged contract code can be installed on the org2.peer0 node.
Peer lifecycle chaincode install abs.tar.gz4.1.3 chain code authentication
After the chain code is installed, it needs to be certified by the organization, and the chain code can be submitted and run after the certification is passed. This process can also be called contract definition, which defines the name, version and serial number of the contract. The subsequent contract upgrade is to modify the version or serial number and redefine the contract.
Execute the following code to complete the authentication of the above installation chain code by the organization where the current node belongs.
Peer lifecycle chaincode approveformyorg-tls true-cafile $ORDERER_CA-channelID mychannel-name abs-version 1-init-required-package-id abs_1:90786e8a73dbee43adfebeb316407387173f598a5f7e3cef7ddc754fe25e2ad6-sequence 1-waitForEvent
With reference to the method in the previous section, the environment is switched to realize the authentication of the above installation chain code by the org2 organization.
Finally, check the chain code authentication result and execute the following command:
Peer lifecycle chaincode checkcommitreadiness-channelID mychannel-name abs-version 1-sequence 1-output json-init-required
The result of the above figure shows that Org1 and Org2, the two organizations in the channel mychannel, have passed the certification of the intelligent contract chain code abs.
4.1.4 chain code submission
Only chain codes certified by the organization can be submitted. Execute the following command to submit the chain code:
Peer lifecycle chaincode commit-o orderer.example.com:7050-tls true-- cafile $ORDERER_CA-- channelID mychannel-- name abs-- 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 1-init-required
View the submission result and execute the following command:
Peer lifecycle chaincode querycommitted-channelID mychannel-name abs
4.2 call chain code
Chain code call mainly includes two operations: invoke and query. This section will demonstrate intelligent chain code data initialization, data query and data update.
4.2.1 chain code initialization
Execute the following command to invoke the chain code initialization method and specify initialization parameters and values.
Peer chaincode invoke-o orderer.example.com:7050-- tls true-- cafile $ORDERER_CA-C mychannel-n abs-- 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-- isInit-c'{"Args": ["Init" "a", "100", "b", "100"]} '4.2.2 data query
Execute the following command to query the value of parameter a.
Peer chaincode query-C mychannel-n abs-c'{"Args": ["query", "a"]}'
4.2.3 data updates
Execute the following command to call the invoke method of the contract, realizing that the value of parameter a decreases by 10 and the value of parameter b increases by 10.
Peer chaincode invoke-o orderer.example.com:7050-- tls true-- cafile $ORDERER_CA-C mychannel-n abs-- 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": ["invoke" "a", "b", "10"]}'
The execution result is shown in the following figure:
4.3 upgrade chain code
The key to chain code (contract) upgrade lies in the contract definition, that is, chain code authentication in Section 4.1.3. After upgrading the chain code, the contract will be redefined and the version number or serial number of the contract will be changed.
4.3.1 get the contract code
One can't make bricks without straw. The first step in upgrading the chain code should be to obtain the contract code. There are two ways to obtain the contract code:
Package command parameters according to section 4.1.1 chain code-get the code according to the path specified by path
Get the installed contract code package through the peer script and execute the following command:
# check the installed contract code package and obtain the package-id value peer lifecycle chaincode queryinstalled of the corresponding package
The execution result is shown in the following figure:
# download the specified contract code package peer lifecycle chaincode getinstalledpackage-package-id abs_1:90786e8a73dbee43adfebeb316407387173f598a5f7e3cef7ddc754fe25e2ad6
The execution result is shown in the following figure:
In the figure above, we can see that there is an additional zip file named with the package-id value in the current directory, rename the file to abs.tar.gz, and check that the zip file contains a json file and a zip package through the tar command. The Json file is the description of the compressed package, and code.tar.gz is the source code of the contract, which is the target of our modification this time. After decompressing the package, we will continue with the next operation.
4.3.2 modify the contract code
Suppose you have unzipped the downloaded contract source code package, or found the path of the sample with its own source code, and then start to modify the contract code.
This time, modify the target file as: abstore.go, add an Add function, the specific content is as follows:
Func (t * ABstore) Add (ctx contractapi.TransactionContextInterface, A string, X int) error {var err error / / Get the state from the leger Avalbytes Err: = ctx.GetStub () .GetState (A) if err! = nil {return fmt.Errorf ("Failed to get state for" + A)} if Avalbytes = = nil {return fmt.Errorf ("Nil amount for" + A)} var Aval int Aval _ = strconv.Atoi (string (Avalbytes)) Aval = Aval + X / / Write the state back to the ledger err = ctx.GetStub (). PutState (A, [] byte (strconv.Itoa (Aval) if err! = nil {return err} return nil}
The contents of the modified file are shown in the following figure:
4.3.3 repackage the contract
After the code modification is complete, repackage and execute the following command:
Peer lifecycle chaincode package abs.tar.gz-path github.com/hyperledger/fabric-samples/chaincode/abstore/go/-lang golang-label abs_1
Please check whether the current directory generates a code package after the execution is completed.
4.3.4 reinstallation contract # installation chain code peer lifecycle chaincode install abs.tar.gz# query chain code installation result peer lifecycle chaincode queryinstalled
The execution result is shown in the following figure:
The result of the above figure shows that Org1 and Org2, two organizations in the channel mychannel, have certified the newly packaged intelligent contract.
4.3.6 resubmit the contract
Resubmit the contract uses the same command as section 4.1.4 chain code submission, except that the sequence parameter value is 2.
Peer lifecycle chaincode commit-o orderer.example.com:7050-tls true-- cafile $ORDERER_CA-- channelID mychannel-- name abs-- 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 1-init-required
View the submission result, as shown in the following figure:
From the execution result of the above figure, the original value of an is 90. Call the add method to increase the value of a by 30, and query the value of an again to 120, indicating that the contract has been updated successfully and the new method has been successfully called.
4.4 additional installation chain codes for other nodes
The previous operation only installed and upgraded the chain code on the org1.peer0 and org2.peer0 nodes, while the org1.peer1 and org2.peer1 nodes did not install the chain code. When running the Java code written based on Fabric SDK, the call to the query method failed and the error "abs chain code is not installed". The client should send the request to the two nodes without chain code. For this reason, supplement this section.
The steps to supplement the installation chain code are as follows:
(1) obtain the installation package from the installed peer node
# download chain code package peer lifecycle chaincode getinstalledpackage-- package-id abs_1:013a79f868db17429c3195acacc08ae376994c5a9fb32d2302bef7f49f0354f7# rename chain code package mv abs_1\: 013a79f868db17429c3195acacc08ae376994c5a9fb32d2302bef7f49f0354f7.tar.gz abs.tar.gz
(2) install chain codes on org1.peer1 and org2.peer1 nodes
# switch environment to org1.peer1source scripts/utils.shsetGablobs 1 "execute installation command peer lifecycle chaincode queryinstalledsetGablobs 1" and execute installation command peer lifecycle chaincode queryinstalled
(3) obtain the certification of org1 and org2.
The most important thing in this process is to pay attention to the parameter value of sequence, which must be incremented by 1 to the latest value of sequence. Query the latest sequence value of the chain code, and execute the following command:
Peer lifecycle chaincode querycommitted-channelID mychannel-name abs
If the latest sequence value of chain code abs is 2, the command to obtain organization authentication is as follows:
Peer lifecycle chaincode approveformyorg-tls true-cafile $ORDERER_CA-channelID mychannel-name abs-version 1-init-required-package-id abs_1:013a79f868db17429c3195acacc08ae376994c5a9fb32d2302bef7f49f0354f7-sequence 3-waitForEvent
Switch to any node of another organization and execute the same command as above.
Finally, to check the certification results, both organizations must pass the certification.
Peer lifecycle chaincode checkcommitreadiness-channelID mychannel-name abs-version 1-sequence 3-output json-init-required
(4) submit chain code
To submit the code, you also need to modify the sequence parameter. The command is as follows:
Peer lifecycle chaincode commit-o orderer.example.com:7050-tls true-- cafile $ORDERER_CA-- channelID mychannel-- name abs-- 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 3-init-required
When the chain code is successfully installed on the org1.peer1 and org2.peer1 nodes, the sample code for ContractAPI will run successfully, and if there is still a problem, you need to check the certificate file.
These are all the contents of the article "how to implement chain Code Management in Fabric2.0". Thank you for reading! Hope to share the content to help you, more related 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.