Hyperledger Fabric—Jar way to install contracts

1 Package smart contract

1.1 upload jar

Enter the /fabric-samples/chaincode/ directory, and upload the contract jar

cd /root/fabric-samples/chaincode

1.2 Return to the directory where the test-network is located and start the test network

Enter the /fabric-samples/test-network/ directory and start the test network

cd /root/fabric-samples/test-network
# 启动测试网络
./network.sh up
# 创建一个通道, 默认通道名称是: mychannel
./network.sh createChannel

1.3 Add the binary files in the bin directory to the CLI path

Chaincode packages in the desired format can be created using the peer CLI, add these binaries to your CLI path using the following commands.

export PATH=${
    
    PWD}/../bin:$PATH

1.4 Set FABRIC_CFG_PATH to point to the core.yaml file in fabric-samples

export FABRIC_CFG_PATH=$PWD/../config/

1.5 Create chain code package

peer lifecycle chaincode package instructions

peer lifecycle chaincode package Cases8xAccident.tar.gz --path ../chaincode/Cases8xAccident --lang java --label Cases8xAccident

Command Explanation: This command will create a package named Cases8xAccident.tar.gz in the current directory.
The –lang tag is used to specify the chaincode language, the –path tag provides the location of the smart contract code, the path must be a standard path or a path relative to the current working directory, the –label tag is used to
specify a chaincode label, which will be in Identify the chaincode after it is installed. It is recommended that your tags contain the chaincode name and version.

Now that we have created the chaincode package, we can install the chaincode on the peer nodes of the test network.

2 Install the chaincode package

After packaging the Cases8xAccident smart contract, we can install the chaincode on the peer node. Chaincode needs to be installed on every peer that will approve transactions. Because we will set the endorsement policy to require endorsements from both Org1 and Org2, we need to install the chaincode on the peer nodes of both organizations: peer0.org1.example.com and peer0.org2.example.com

2.1 Org1 peer node installation chain code

Set the following environment variables to operate the peer CLI as an Org1 administrator.

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

peer lifecycle chaincode install command
Use the peer lifecycle chaincode install command to install a chaincode on a peer node.

peer lifecycle chaincode install Cases8xAccident.tar.gz

See the following information to indicate that the chain code is installed successfully
insert image description here
HyperledgerFabric uses the jar package to install the chain code principle

2.2 Org2 peer node installation chain code

Set the following environment variables to operate the peer CLI as an Org2 administrator.

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_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

Use the peer lifecycle chaincode install command to install the chaincode on the peer node.

peer lifecycle chaincode install Cases8xAccident.tar.gz

insert image description here

Note: When installing the chain code, the chain code is built by the peer node. If there is a problem with the smart contract code, the install command will return all build errors from the chaincode. Because the process of installing java chaincode needs to go through the process of maven building and downloading dependent packages, this process may be slow, so the install command may return a timeout error:. But in fact, the build task is still being executed in the docker container of the chaincode at this time and has not been completed. When the build is successful, the chaincode package will be installed successfully.

3 Defined by chain code

After installing the chaincode package, you need to pass the organization's chaincode definition. This definition includes important parameters for chaincode management, such as name, version, and chaincode approval policy.

If the organization has chaincode installed on their peer nodes, they need to include the package ID in the chaincode definition passed by their organization. The package ID is used to associate the chaincode installed on the peer with the passed chaincode definition and allows organizations to use the chaincode to endorse transactions.

3.1 Query package ID

peer lifecycle chaincode queryinstalled

The bundle ID is a combination of the chaincode label and the hash of the chaincode binary. Each peer node will generate the same packet ID. You should see output similar to the following:
insert image description here
We will use the bundle ID when going through the chaincode, so save the bundle ID as an environment variable. Paste the returned bundle ID into the command below.


NOTE: Bundle IDs are different for all users, so this step needs to be done using the bundle ID returned from the command window in the previous step. Instead of copying the command directly! ! !

export CC_PACKAGE_ID=Cases8xAccident:442624a81dbb67eb594ef50b0bed8a6662ddcc8b848382f5d9d69c1471f5b695

3.2 Org2 defined by chain code

Because the environment variable has been set to peer CLI to operate as an Orig2 administrator, we can pass the chaincode definition of Cases8xAccident at the Org2 organization level. Use the peer lifecycle chaincode approveformyorg command to define through the chain code:
peer lifecycle chaincode approveformyorg command analysis

peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name Cases8xAccident --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
Pay attention to the figure below, the command may need to be modified according to your own chain code
  1. mychannel : channel name
  2. Cases8xAccident : The name of the chaincode package queried by the peer lifecycle chaincode queryinstalled command
  3. version 1.0 : version number (the version number must be changed when upgrading the chaincode)
  4. sequence 1 : serial number (the serial number must be changed when upgrading the chain code)

insert image description here

3.2 Org1 defined by chain code

Set the following environment variables to run as Org1 administrator:

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

Through the chain code definition with the peer lifecycle chaincode approveformyorg command

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

4 Commit the chaincode definition to the channel

Use the peer lifecycle chaincode checkcommitreadiness command to check that channel members have approved the same chaincode definition:

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

This command will generate a JSON map showing whether channel members approved the parameters specified in the checkcommitreadiness command:

{
	"approvals": {
		"Org1MSP": true,
		"Org2MSP": true
	}
}

insert image description here


Since both organizations that are members of the channel have agreed on the same parameters, the chaincode definition is ready to be committed to the channel. You can commit a chaincode definition to a channel using the peer lifecycle chaincode commit command. The commit command also needs to be committed by an organization administrator.

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

The peer lifecycle chaincode querycommitted command can be used to confirm that the chaincode definition has been committed to the channel.

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

insert image description here


At this point, the chain code installation is complete, and the chain code test is performed below

5 call chain code

First enter the test-network directory

cd /root/fabric-samples/test-network

General query command

peer chaincode query -C channel name -n contract label name -c '{"Args":[" function name in own contract ", " parameter 1 "]}'

peer chaincode query -C mychannel -n Cases8xAccident   -c '{"Args":["query" , "1"]}'

General add, update, delete commands

Detailed analysis of Fabric2.3 peer chaincode invoke

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 channel name -n contract label name --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" :" Function name in own contract ", "Args":["parameter 1", "parameter 2", "parameter 3"]}'

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 Cases8xAccident -java-demo --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":"create","Args":["cat-0"]}'

View the running log command of the contract

docker ps -a

docker logs -f dev-peer0.org1 CONTAINER ID of the node container

docker logs -f 28e6700307d0

Guess you like

Origin blog.csdn.net/qq_44154912/article/details/126221673