Before deploying java chain code, ensure that java and maven environment have been installed on the machine
Reference tutorial: https://www.bilibili.com/video/BV1bu41117Lx
1 Package smart contract
1.1 Download the hyperledger-fabric-contract-java-demo contract source code to the local machine
cd ~fabric-samples/chaincode/
git clone https://gitee.com/kernelHP/hyperledger-fabric-contract-java-demo.git
1.2 Return to the directory where test-network is located
Return to the directory where the test-network is located so that the chaincode can be packaged with other network components.
cd ../../test-network
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 hyperledger-fabric-contract-java-demo.tar.gz --path ../chaincode/hyperledger-fabric-contract-java-demo/ --lang java --label hyperledger-fabric-contract-java-demo_1
Command Explanation: This command will create a package named hyperledger-fabric-contract-java-demo.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 hyperledger-fabric-contract-java-demo 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
Use the peer lifecycle chaincode install command to install the chaincode on the peer node.
That is, the installation contract
peer lifecycle chaincode install hyperledger-fabric-contract-java-demo.tar.gz
If you see the following information, the chaincode installation is successful
[The external chain image transfer failed, the source site may have an anti-leech mechanism, it is recommended to save the image and upload it directly (img-pj5QCX1Y-1652845797158)(https://doc.3hea.com/ uploads/fabric/images/m_b49ae7c94703495123e7f9eedeec9036_r.png)]
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 hyperledger-fabric-contract-java-demo.tar.gz
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.
[External link picture transfer failed, the source site may have an anti-theft link mechanism, it is recommended to save the picture and upload it directly (img-Xk66SKsU-1652845797159)(https://doc.3hea.com/uploads/fabric/images/m_8c4f69f70f0226c334b86555c861d965_r.png )]
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 After both Org1 and Org1 nodes are installed, use the following command to query the 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:
Installed chaincodes on peer:
Package ID: hyperledger-fabric-contract-java-demo_1:762e0fe3dbeee0f7b08fb6200adeb4a3a20f649a00f168c0b3c2257e53b6e506, Label: hyperledger-fabric-contract-java-demo_1
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! ! !
Copy and paste the content in the red box in the picture above to the command below
export CC_PACKAGE_ID=The content just copied
export CC_PACKAGE_ID=hyperledger-fabric-contract-java-demo_1:f61f9e5b97d2034aa864bccd148dac36d0583c13176dfd267af1c8f34e7c03ae
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 hyperledger-fabric-contract-java-demo at the Org2 organization level. Use the peer lifecycle chaincode approveformyorg command to pass the chaincode definition:
peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name hyperledger-fabric-contract-java-demo --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
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 hyperledger-fabric-contract-java-demo --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
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 hyperledger-fabric-contract-java-demo --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
}
}
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 hyperledger-fabric-contract-java-demo --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
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 hyperledger-fabric-contract-java-demo --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
If the chaincode was successfully committed to the channel, the querycommitted command will return the order and version of the chaincode definition:
Version: 1.0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true, Org2MSP: true]
5 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 hyperledger-fabric-contract-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":"createCat","Args":["cat-0" , "tom" , "3" , "蓝色" , "大懒猫"]}'
peer chaincode query -C mychannel -n hyperledger-fabric-contract-java-demo -c '{"Args":["queryCat" , "cat-0"]}'
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 hyperledger-fabric-contract-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":"updateCat","Args":["cat-0" , "tom" , "3" , "白色" , "超级大懒猫"]}'
peer chaincode query -C mychannel -n hyperledger-fabric-contract-java-demo -c '{"Args":["queryCat" , "cat-0"]}'
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 hyperledger-fabric-contract-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":"deleteCat","Args":["cat-0"]}'
peer chaincode query -C mychannel -n hyperledger-fabric-contract-java-demo -c '{"Args":["queryCat" , "cat-0"]}'
See the Chaincode invoke successful. result: status:200 message to prove that the chaincode invoke is successful:
[External link image transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the image and upload it directly (img-kzvuevTP-1652845797159) (https://doc.3hea.com/uploads/fabric/images/m_431af27faa97dfa181e7c2c359a0b5bc_r.png )]
6. Common Mistakes
Cannot run peer because cannot init crypto, specified path “/root/fabric-samples/chaincode/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp” does not exist or cannot be accessed: stat /root/fabric-samples/chaincode/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp: no such file or directory
Note: You must first enter the fabric-samples/test-network directory
before configuring environment variables and installing the chaincode
, otherwise the following error will be reported when installing the chaincode
failed to read chaincode package at ‘hyperledger-fabric-contract-java-demo.tar.gz’: open hyperledger-fabric-contract-java-demo.tar.gz: no such file or directory
It is also because it did not enter the /test-network directory when packaging the contract compression package
chaincode install failed with status: 500 - error in simulation: failed to execute transaction f84be232e353667cf9821b97af99fe344af56c11641a2c541140e71f86d6c9d7: error sending: timeout expired while executing transaction
When the peer lifecycle chaincode install hyperledger-fabric-contract-java-demo.tar.gz command is completed, this error may be reported and timed out, but don’t worry, because you need to build the maven project and download the required project dependent package,
Open a new window and enter docker ps to view all containers
docker ps
View the latest container logs
docker logs -f 09a644e8470d
The project has been built.
Execute the install chaincode command again
peer lifecycle chaincode install hyperledger-fabric-contract-java-demo.tar.gz
The chaincode already successfully installed appears
, indicating that the chaincode has been successfully installed