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

What is the chain code operation process in Fabric 2.0?

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

Share

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

This article is to share with you about the chain code operation process in Fabric 2.0. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Chain code operation: Fabric 1.4 vs Fabric 2.0

We will start with a quick introduction to the whole chain code operation process in HF 1.4 and HF 2.0.

Chain code operation refers to the operation of deploying chain code on the Hyperledger fabric network channel, so that applications outside the block chain can call or query chain code methods. After the chain code is developed and tested, you first need to install the Fabric chain code to the specified peer node. The chain code cannot be used at this stage until the chain code is submitted to the channel (term in Fabric 2.0) or instantiated on the channel (term in Fabric 1.4) so that the chain code can be accessed by authorized users.

The following is a comparison of the chain code operation flow between the two versions of Hyperledger Fabric:

In Hyperledger Fabric 1.4, the chain code operation process includes the following steps: packaging, installation, and instantiation. If the chain code belongs to multiple parties, then you need to package this link. If there is no multi-owner problem, then you can install the chain code directly (which implies the packaging link). When installing the Fabric chain code, you need to specify the target node to install the chain code.

At this stage, the installed Fabric chain code cannot be used because it has not yet been instantiated on the channel. When the Fabric chain code package is installed on the specified node, we can instantiate the chain code to make the chain code available on the channel. Technically, chain code instantiation is actually a call to the chain code method of the LSCC system to initialize a chain code on the channel.

After the Fabric chain code is instantiated, it can be used to accept calls or query requests on the channel.

Let's take a look at the differences in chain code procedures in Hyperledger Fabric 2.0.

Broadly speaking, chain code operations in Fabric 2.0 basically follow the same process, but some adjustments have been made in commands and some background processing. The overall process can be divided into four steps: packaging, installation, agency approval, chain code submission. It can be roughly assumed that the first two links correspond to chain code installation in Fabric 1.4, and the latter two links correspond to chain code instantiation in Fabric 1.4, but the word instantiation is no longer used.

The step of chain code packaging is to create a package file (in tar format) that contains the Fabric chain code and some metadata. Although different agencies can be packaged separately, it is more common for one agency to package and distribute it to other agencies to ensure that all agencies use the same chain code.

The installation step is to install the packaged Fabric chain code file on the specified peer node. As in previous versions, only nodes that need to accept chain code calls need to install chain codes. At this node, the Fabric chain code is not available because it has not been submitted to the channel. The result of the chain code installation is a package identifier in the format..

Agency approval is an added step in Hyperledger Fabric 2.0. In previous versions, we could have an organization instantiate the chain code. In Fabric 2. 0, the agency is required to explicitly approve the chain code. How much agency approval is required is determined by the lifecycle endorsement policy, which is set to require most agencies (more than half) by default. If there are two institutions in the Fabric network, then both agencies are required to approve. The participation of the sorting node is required during the approval process, because a new block is generated for each approval, which means that all peer nodes know the status of the approval.

When the approval process is complete, we need to specify which channel to deploy the chain code on. This requires some information to be submitted, such as the endorsement policy, whether the Init code needs to be executed, and so on. There are also some differences here from Fabric 1.4: in Fabric 1.4, the Init method of the chain code is automatically called when the chain code is instantiated, whereas in Fabric 2.0, the Init method needs to be explicitly called after the chain code is submitted.

After the approving authority reaches the specified number, the chain code can be submitted. We are now moving on to the final step: chain code submission.

Chain code submission can be initiated by any organization. The process first requires the endorsement of the approval agency, then the transaction is submitted to the sorting service and a new block is generated, and finally all peer nodes submit the block in the ledger.

Now the chain code can be used.

2. Brief introduction of First Network and SACC chain codes

For completeness, here is some information about First Network and SACC chain codes, all from the fabric-samples repository.

First Network is a dual mechanism setup, with two peer nodes in each mechanism. After the channel mychannel is created, all four peer nodes are added. The deployment of First Network is fully implemented in byfn.sh, with some optional parameters. In the following demonstration, instead of using the default chain code (chaincode_example02 in Fabric 1.4 and abstore in Fabric 2.0), we use SACC chain codes.

An abbreviation for SACC-style Simple Asset ChainCode, which represents a simple asset chain code. It simulates a key / value store in the ledger. After the initial deployment, an initial key / value pair is required. SACC chain codes define two methods: Set () and Get (), which are used to set or read the value of a key, respectively.

Okay, now we can demonstrate the difference between chain code operations in Fabric 1.4 and Fabric 2.0.

3. Fabric 1.4.4 chain code operation demonstration

We first start First Network in a chainless manner (using the-n option), and then we load the SACC chain code to focus on the life cycle of the chain code.

Here are the demonstration steps:

Start First Network without chain code

Install the SACC chain code on the specified peer node

Instantiate SACC chain codes on the mychannel channel and query the results

Call set () to set the new value and query the results from another peer node

STEP 1: start First Network first:

Cd fabric-samples/first-network./byfn.sh up-n

The above command starts First Network, creates a channel mychannel, and adds all peer nodes to the channel. Note that in Fabric 1.4.4, First Network is implemented using the solo sorting service and has only one sort node. In addition, we see four peer nodes running and a CLI for chain code operations.

Now we can start the chain code deployment operation.

STEP 2: install the chain code on the specified peer node

Here we skip the packaging and install the chain code directly on the target nodes peer.org1 and peer0.org2, because in this demonstration we only need these two nodes to make chain code calls and queries.

# peer0.org1docker exec cli peer chaincode install-n mycc-v 1\-p github.com/chaincode/sacc # peer0.org2 docker exec\-e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp\-e CORE_PEER_ADDRESS=peer0.org2.example.com:9051-e CORE_PEER_LOCALMSPID= "Org2MSP"\-e CORE_PEER_TLS_ROOTCERT_ FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt\ cli peer chaincode install-n mycc-v 1-p github.com/chaincode/sacc

STEP 3: instantiate chain codes and query on channel mychannel

Notice that there is an Init () code in the sacc chain code. When we instantiate the chain code, we need to provide the parameters required by Init ():

Docker exec cli peer chaincode instantiate-o orderer.example.com:7050-- tls\-- 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-v 1-c'{"Args": ["name", "kc"]}'\-P "AND ('Org1MSP.peer','Org2MSP.peer')"

If we look at the log of the peer node now, we can see that there is a new block # 3.

After the chain code is instantiated, we can query:

Docker exec cli peer chaincode query-C mychannel-n mycc-c'{"Args": ["get", "name"]}'

STEP 4: call set () to set the new value and query from another node

For demonstration purposes, we call set () on peer0.org1 and then get () on peer0.org2 to show whether the chain code works properly.

# peer0.org1docker exec cli peer chaincode invoke-o orderer.example.com:7050-- tls\-- cafile / opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem\-- 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 mychannel-n mycc-c'{"Args": ["set" "name" "Peter"]}'# peer0.org2docker exec-e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp\-e CORE_PEER_ADDRESS=peer0.org2.example.com:9051\-e CORE_PEER_LOCALMSPID= "Org2MSP"\-e CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ PeerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt\ cli peer chaincode query-C mychannel-n mycc-c'{"Args": ["get" "name"]}'

4. Fabric 2.0 chain code operation demonstration

Similarly, in Fairc 2.0.0, we start First Network without a chain code, and then start the SACC chain code to focus on the chain code's life cycle.

Here are the demonstration steps:

Start First Network without chain code

Package SACC chain code

Install the SACC chain code package on the specified peer node

Chain code of agency examination and approval

Submit chain codes on mychannel channel

Call the Init method of SACC chain code

Call the set method of the SACC chain code and query the result from another peer node

STEP 1: start First Network without chain code:

Cd fabric-samples/first-network./byfn.sh up-n

Now the First Network has been started, the channel mychannel has been created, and all peer nodes have joined the mychannel channel. Note that in Fabric 2.0.0, First Network uses Raft as the sorting service, and all five sorting nodes are running. In this demonstration, we only use orderer.example.com, but the same is true with other sort nodes.

STEP 2: package SACC chain code

First of all, we deal with the problem of dependency:

Cd fabric-sample/chaincode/saccGO111MODULE=on go mod vendorcd fabric-sample/first-network

Then pack the chain code:

Docker exec cli peer lifecycle chaincode package sacc.tar.gz\-path github.com/hyperledger/fabric-samples/chaincode/sacc/\-label sacc_1

You can see in the CLI container that a new file sacc.tar.gz is generated:

STEP 3: install the SACC chain code package on the specified peer node

Now let's install SACC Horse News on peer0.org1 and peer0.org2, because in this demonstration we only need to use these two nodes for chain code calls and queries.

# peer0.org1docker exec cli peer lifecycle chaincode install sacc.tar.gz# peer0.org2docker exec\-e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp\-e CORE_PEER_ADDRESS=peer0.org2.example.com:9051\-e CORE_PEER_LOCALMSPID= "Org2MSP"\-e CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com / hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt\ cli peer lifecycle chaincode install sacc.tar.gz

We will receive the identifier of the chain code package, which will be used in the chain code approval phase below. The chain code identifier we received is: sacc_1:bf57 … 6399 .

We can use the peer lifecycle chaincode queryinstalled command to check the chain code installation on the node at any time, which is useful if we need to find out the identification ID of the chain code package.

# peer0.org1docker exec cli peer lifecycle chaincode queryinstalled# peer0.org2docker exec\-e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp\-e CORE_PEER_ADDRESS=peer0.org2.example.com:9051\-e CORE_PEER_LOCALMSPID= "Org2MSP"\-e CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/ Peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt\ cli peer lifecycle chaincode queryinstalled

STEP 4: approval chain code

According to the default policy, more than half of the organizations need to approve the chain code before submitting the chain code to the channel. For more information, please refer to the Application/Policies/LifecycleEndorsement section of configtx.yaml. The current setup includes two agencies, so both agencies are required to approve the chain code at the same time.

The first is the chain code approved by Org1:

Docker exec cli peer lifecycle chaincode approveformyorg\-tls\-- 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-- sequence 1-- waitForEvent-- package-id ${PACKAGE_ID}

If we look at the log of the peer node now, we can see the new block # 3.

Similarly, we asked Org2 to approve the chain code:

Docker exec\-e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp\-e CORE_PEER_ADDRESS=peer0.org2.example.com:9051\-e CORE_PEER_LOCALMSPID= "Org2MSP"\-e CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers / peer0.org2.example.com/tls/ca.crt\ cli peer lifecycle chaincode approveformyorg\-- tls\-- 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\-sequence 1-- waitForEvent-- package-id ${PACKAGE_ID}

Not surprisingly, you can see a new block block#4:

Notice that we specify init-related parameters in the approval command to pass the required parameters to the Init method of the SACC chain code.

You can check the submission status of the chain code at any time using the following command:

Docker exec cli peer lifecycle chaincode checkcommitreadiness\-channelID mychannel-name mycc-version 1-sequence 1-output json

Both agencies have approved the chain code and can now submit it.

STEP 5: submit chain codes to channel mychannel

Chain code submission can be done on a peer node:

Docker exec cli peer lifecycle chaincode commit-o orderer.example.com:7050\-- tls\-- cafile / opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem\-- 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\-channelID mychannel-name mycc-version 1-sequence 1-init-required

You can see the new block # 5:

Notice that we have included the parameters required by init in the commit command.

Similarly, we can use the querycommited command to view the submission status of the chain code:

Docker exec cli peer lifecycle chaincode querycommitted-channelID mychannel-name mycc

After the chain code is submitted to the channel, the life cycle of the chain code is complete and the chain code is accessible. Now let's go back to the chain code invocation and query, which is consistent with the previous version.

STEP 6: call the chain code's Init method

The Init method of the SACC chain code needs to be called first.

Docker exec cli peer chaincode invoke-o orderer.example.com:7050\-- tls\-- cafile / opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem\-- 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 mychannel-n mycc-- isInit-c'{"Args": ["name" "kc"]}'

You can now query the chain code:

Docker exec cli peer chaincode query-C mychannel-n mycc-c'{"Args": ["get", "name"]}'

STEP 7: call the chain code's set () method and query from another peer node

As before, we call the chain code's set () method on peer0.org1 to query on peer0.org2:

# peer0.org1docker exec cli peer chaincode invoke\-o orderer.example.com:7050\-tls\-cafile / opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem\-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 mychannel-n mycc-c'{"Args": ["set" "name" "Peter"]}'# peer0.org2docker exec\-e CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp\-e CORE_PEER_ADDRESS=peer0.org2.example.com:9051\-e CORE_PEER_LOCALMSPID= "Org2MSP"\-e CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/ Crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt\ cli peer chaincode query-C mychannel-n mycc-c'{"Args": ["get" "name"]}'

Everything's fine.

Thank you for reading! This is the end of this article on "what the chain code operation process is like in Fabric 2.0". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

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