fabric2.4.9 running test network
The shared content is based on the official website, with some additional content such as self-understanding added. This time, it is mainly to test the network and briefly explain the chaincode operation. For specific chaincode operations and explanations, please refer to the next section.
https://hyperledger-fabric.readthedocs.io/en/release-2.4/test_network.html
The premise of operation is the environment configuration shared by the previous post
Install fabric2.4.x on ubuntu20.4
run testnetwork
· First enter the folder
cd fabric-samples/test-network
· Stop now to avoid mistakes
./network.sh down
· On script launch
./network.sh up
· Query via docker ps -a
· Create a channel
./network.sh createChannel
This is considered a success!
You can also specify the name through the -c parameter, for example, create a channel named channel1
./network.sh createChannel -c channel1`.
· Deploy the chaincode on the channel
Use the system chaincode, deployed on the channel just created
./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
-ccn: chaincode name
-ccp: Chaincode installation path
-ccl: language parameter
The first deployment will install dependencies
An error is reported here, there is no jq
just install
sudo apt-get jq
Run the above chain code again, and it will succeed after a while!
In fact, this script will automatically complete the life cycle of the entire chain code, and the life cycle and how to write the chain code will be shared later.
` Use CLI to interact with the network
- Add the node binary file to the cli path in the test-network
export PATH=${<!-- -->PWD}/../bin:$PATH
- Set fabric-cgf –path to point to the core.yaml file in fabric-samples
export FABRIC_CFG_PATH=$PWD/../config/
- Operate the environment variable cli of peer as org1:
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
The meaning of the above command is:
- Enable TLS encrypted communication between peers
- Specifies the name of the organization to which the current node belongs
- Specifies the root certificate file path of the peer
- Specify the msp configuration file path of the current node
- Specify the address and port number of the specified current node to establish a connection with the peer
- Call the InitLedger function to initialize the 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":[]}'
- peer chaincode invoke -o localhost:7050: invoke the peer chaincode invoke command and specify the output port as 7050.
- –ordererTLSHostnameOverride orderer.example.com: Set the TLS hostname override of orderer, here set the TLS hostname of orderer to orderer.example.com.
- –tls –cafile “${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem”: Enable TLS encrypted communication and specify a certificate file path.
4. -C mychannel -n basic: Specify the name of the channel where the chaincode is located as mychannel, and the namespace of the chaincode as basic.
- –peerAddresses localhost:7051: Specify the address and port number of the current node to establish a connection with the peer.
- –tlsRootCertFiles “${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt”: Specifies the root certificate file path of the current node.
–peerAddresses localhost:9051: Specify the address and port number of another peer to establish a connection with another peer.
–tlsRootCertFiles “${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt”: Specify the root certificate file path of another peer.
-c ‘{“function”:”InitLedger”, “Args”:[]}’: call the InitLedger function of chaincode, and pass an empty parameter list -c parameter is used to specify the name and input parameters of the chaincode to call . In the above example, we call the chaincode function named InitLedger with -c ‘{“function”:”InitLedger”, “Args”:[]}’ and pass an empty input argument list.
In actual use, it can be simplified as:
peer chaincode invoke -o localhost:7050 -C mychannel -n basic --tlsRootCertFiles "${<!-- -->PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example .com/tls/ca.crt" --peerAddresses localhost:7051 --tls --cafile "${<!-- -->PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/ msp/tlscacerts/tlsca.example.com-cert.pem" --peerAddresses localhost:9051 -c '{"function":"InitLedger","Args":[]}'
5. Query ledger
peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
The successful result is as follows
6. Change the owner of the asset
The transferasset function in the chaincode is called, the parameter is [“asset6”, “Christopher”], other parameters are similar to the above and not repeated.
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":"TransferAsset","Args":["asset6","Christopher"]}'
7. Set org2 as an environment variable
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
- Query the asset transfer chaincode running on org2
peer chaincode query -C mychannel -n basic -c '{"Args":["ReadAsset","asset6"]}'
9. Finally close the network
./network.sh down