Hyperledger Fabric Tutorial--Deploying Fabric Smart Contracts

Deploying Smart Contracts for Channels Hyperledger Fabric Tutorial – Deploying Fabric Smart Contracts

In Hyperledger Fabric, smart contracts are deployed in packages called chaincodes. Organizations that want to verify transactions or query the ledger need to have chaincode installed on their peer nodes. After installing the chaincode on the peer nodes joining the channel, channel members can deploy the chaincode to the channel and use the smart contracts in the chaincode to create or update assets on the channel ledger.

Chaincodes are deployed to channels using a process called the Fabric chaincode lifecycle. The Fabric chaincode lifecycle allows multiple organizations to agree on how a chaincode should operate before it becomes available. This tutorial learns how to use the peer lifecycle chaincode command to deploy the chaincode to the channel of the Fabric test network. Once you understand these commands, you can use the steps in this tutorial to deploy your own chaincode to a test network, or deploy chaincode to a production network.

(1) Start the network Execute the command
in the directory:~/github.com/hyperledger/fabric-samples/test-network

./network.sh up createChannel

insert image description here

(2) Packaging smart contract (JavaScript)

We need to package the chaincode before we can install it on our peer node. The steps are different if you want to install a smart contract written in Go, Java, or JavaScript.
Before packaging the chain code, we need to install the chain code dependencies, open another terminal and enter:

~/github.com/hyperledger/fabric-samples/chaincode/fabcar/javascript/

implement:

source /etc/profile  #这一步骤将node命令放入环境变量中

Dependencies are listed in the package.json file and can be found in the dependencies section shown below:

The package.json file imports the Fabric Contract class . fabcar.jsYou can open lib/fabcar and view the file in a text editor .

To install smart contract dependencies, run the following command from the fabcar/javascript directory:

npm install

insert image description here

If the command executes successfully, the JavaScript package will be installed in the node_modules folder.

With the dependencies, you can create the chaincode package, go back to another terminal, and execute:

export PATH=${
    
    PWD}/../bin:$PATH
export FABRIC_CFG_PATH=$PWD/../config/   #这个环境变量指定配置文件的位置
peer lifecycle chaincode package fabcar.tar.gz --path ../chaincode/fabcar/javascript/ --lang node --label fabcar_1

The -lang flag is used to specify the chaincode language, and the -path flag provides the location of the smart contract code. The --label flag is used to specify the chaincode label which will identify the chaincode after it is installed.
insert image description here

(3) Install the chain code package The
chain code needs to be installed on each peer that will endorse the transaction. Since we will be setting the endorsement policy to require both Org1 and Org2 to endorse, we will need to install chaincode on the peers run by both organizations.

First install the chaincode on the Org1 peer. Setting the following environment variables will operate the peer CLI as the Org1 admin user, and CORE_PEER_ADDRESS will be set to point to peer0.org1.example.com.

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

Issue the "peer lifecycle chaincode install" command to install the chaincode on the peer:

peer lifecycle chaincode install fabcar.tar.gz

Install chaincode on Org2 peer:

export CORE_PEER_TLS_ENABLED=true
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

Issue the "peer lifecycle chaincode install" command to install the chaincode on the peer :

peer lifecycle chaincode install fabcar.tar.gz

insert image description here
insert image description here

(4) Approve the chaincode definition
After installing the chaincode package, the organization needs to approve the chaincode definition. The definition includes chaincode parameters such as name, version, and chaincode endorsement policy.

By default, only chaincodes approved by a majority of channel members can be used on a channel. Because we only have two orgs on the channel, both Org1 and Org2 need to approve Fabcar's chaincode definition.
Query installed chaincodes:

peer lifecycle chaincode queryinstalled

insert image description here

The Package ID is a combination of the chaincode label and the hash of the chaincode binary. Each peer will generate the same Package ID, which is different for different machines Package ID.
Put the Package ID into the environment variable:

export CC_PACKAGE_ID=fabcar_1:2d275418cc7415743770ab319d81af4e0055feaa7b5a92334d53ee14df2220b0

NOTE: Here's the CC_PACKAGE_ID to you 实际ID为准!

Approve the chaincode definition:

peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --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

The --package id flag includes the package identifier in the chaincode definition. The --sequence parameter is an integer that keeps track of how many times the chaincode is defined or updated. Because the chaincode is deployed to the channel for the first time, the serial number is 1, and when the Fabcar chaincode is upgraded, the serial number will increase to 2.

Change to Org1 user:

export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_MSPCONFIGPATH=${
    
    PWD}/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp
export CORE_PEER_TLS_ROOTCERT_FILE=${
    
    PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_ADDRESS=localhost:7051
peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --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

insert image description here

(5) Submit the chaincode definition to the channel

The set of channel memberships that require chaincode approval before chaincode deployment is governed by policy. By default, this policy requires that a majority of channel members need to approve the chaincode before it can be used on the channel. Since we only have 2 orgs on the channel and a majority of 2 orgs is 2, we need to approve Fabcar's chaincode definitions as Org1 and Org2.

Use the " peer lifecycle chaincode checkcommitreadiness" command to check if the channel members have approved the same chaincode definition.

peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name fabcar --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

insert image description here

Commit the chaincode definition to the channel using the "peer lifecycle chaincode commit" command.

peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --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

insert image description here

Use the " peer lifecycle chaincode querycommitted" command to confirm that the chaincode definition has been committed to the channel.

peer lifecycle chaincode querycommitted --channelID mychannel --name fabmakeup--cafile ${
    
    PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

insert image description here

(6) Call chain code

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 fabmakeup --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":"queryMakeup","Args":[]}'
peer chaincode query -C mychannel -n fabcar -c '{"Args":["queryAllCars"]}'

insert image description here

This is the end of this Hyperledger Fabric Tutorial – Deployment of Fabric Smart Contract sharing, and it will continue to be updated later.
Friends are welcome to discuss. If you have any questions, please comment in the comment area or send a private chat message. Welcome to leave a message!

Guess you like

Origin blog.csdn.net/Myx74270512/article/details/128107797