fabric2.4.9 chain code life cycle and deployment

fabric2.4.9 chain code life cycle and deployment

The premise is to complete the content of the first two chapters. The content of this chapter is based on the official website. If you have more detailed requirements, please visit the fabric official website.
Environment build
test network

1. Shut down and restart the network

./network.sh down
./network.sh up createChannel

A channel named mychannel is created through the createchannel command, which includes two channel members org1 and org2. This command also adds peers belonging to each organization to the channel. If successful, you will see the following content

2. Set logspout

This step is not necessary but you can monitor the log of the smart contract. Create a new window and enter the following command. After configuration, it will be reflected in subsequent operations.

cd fabric-samples/test-network
cp ../commercial-paper/organization/digibank/configuration/cli/monitordocker.sh
./monitordocker.sh fabric_test

3. Packaging chaincode

As the name implies, the written chain code is packaged into a compressed file.

  • Download the dependencies required by the chaincode
cd fabric-samples/asset-transfer-basic/chaincode-go
GO111MODULE=on go mod vendor //Install the dependencies in go.mod
  • Add binaries to cli path
export PATH=${<!-- -->PWD}/../bin:$PATH
  • Use the peer version command to test whether it is successful
  • Create chaincode package
peer lifecycle chaincode package basic.tar.gz --path ../asset-transfer-basic/chaincode-go/ --lang golang --label basic_1.0

You can see the red package

You can see the red package
parameter:

  • –lang specifies the chaincode language as go
  • –path specifies the chaincode location
  • – label specifies the chaincode label, which will be identified after the chaincode is installed

4. Install chaincode package

The chain code needs to be installed on each peer, we need to install the chain code in the two nodes given by the system

  • Set org1 as an environment variable
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
  • Install chain code on peer
peer lifecycle chaincode install basic.tar.gz

If successful, as follows

  • switch to 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
  • Install chaincode
peer lifecycle chaincode install basic.tar.gz

5. Approve the chain horse definition

After installation, it needs to be approved according to the policy, and the packageid needs to be queried using the following command

peer lifecycle chaincode query installed

  • Paste the ID you just queried into the following command, which means that the package is added to the environment variable
export CC_PACKAGE_ID=basic_1.0:d29562b85321d821cc02fddff4366e54b9bdbfa5d51647b5613141dd4d947b9e
  • approval package
peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name basic --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"
peer lifecycle chaincode approveformyorg -o localhost:7050: Starts the chaincode lifecycle manager on the peer node.
--ordererTLSHostnameOverride orderer.example.com: Specify the hostname of the orderer node to communicate with a specific orderer node.
--channelID mychannel: Specifies the channel ID to use.
--name basic --version 1.0 --package-id $CC_PACKAGE_ID --sequence 1: Specify the name, version, package ID and sequence number of the chaincode.
--tls: Enable TLS encrypted connections.
--cafile "${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem": Specifies the path to the trust anchor file, which Contains trusted CA certificates.

  • switch to org1
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
  • Approve chaincode
peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name basic --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"

  • Checks if channel members have approved the same chaincode definition
peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name basic --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

generate a json map

  • Commit the chaincode definition to the channel
peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name basic --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"

peer lifecycle chaincode commit -o localhost:7050: Starts the chaincode lifecycle manager on the peer node.
–ordererTLSHostnameOverride orderer.example.com: Specify the hostname of the orderer node to communicate with a specific orderer node.
–channelID mychannel: Specifies the channel ID to use.
–name basic –version 1.0 –sequence 1: Specify the name, version and sequence number of the chaincode.
–tls: Enable TLS encrypted connections.
–cafile “{PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem”: Specifies the path to the trust anchor file that contains the The letter’s CA certificate.
–peerAddresses localhost:7051 –tlsRootCertFiles “${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt”: specify the address of the peer node and the tls root certificate The path to the file.

  • The command confirms that the chaincode has been committed to the channel
peer lifecycle chaincode querycommitted --channelID mychannel --name basic --cafile "${<!-- -->PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts /tlsca.example.com-cert.pem"

6. Call chaincode

  • First call the initialization chaincode
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":[]}'

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

* delete logspout tool

docker stop logspout
docker rm logspout
  • close the network
./network.sh down

Done! Finally, summarize

The chaincode life cycle review is as follows:

Package the chain code -> install on different organizations -> approve each -> submit the approval to the channel -> can call the chain code.

There is also an upgrade chaincode function later, please move to the official website document to watch it yourself.
fabric-documentation