Preparation and basic use of Hyperledger Fabric test network

Article directory

      • related installation
      • Launch test network
      • Create channels
      • Packaging chaincode
      • Install the chaincode package
      • defined by chaincode
      • The chaincode definition is submitted to the channel
      • call chaincode
      • close the network
      • problems encountered
        • 1.docker keeps starting
        • 2. Forget about testnets
        • 3. Java version is too high, recommend 1.8

Related installation

  1. npm, node, git, docker, docker-compose. docker is guaranteed to run all the time

    service docker start
    service docker status
    

2. Install Java, maven environment

https://blog.csdn.net/qq_41829594/article/details/122408706

https://www.oracle.com/java/technologies/downloads/

https://maven.apache.org/download.cgi

tar -zxvf

Configure environment variables in /etc/profile, then source file to make it effective, check whether the installation is successful

# set java development
export JAVA_HOME=/etc/opt/java/jdk1.8.0 #Java decompression path
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH

3. Install jq

apt install jq
jq --version

4. Download the .sh file in the fabric

curl -sSLO https://raw.githubusercontent.com/hyperledger/fabric/main/scripts/install-fabric.sh & amp; & amp; chmod +x install-fabric.sh

5. Run the .sh file to download docker impressions, Fabric samples and binaries

./install-fabric.sh docker samples binary
or
./install-fabric.sh d s b

Start test network

cd fabric-samples/test-network
./network.sh up

docker ps -a

Create a channel

./network.sh createChannel

Package chaincode

Download the Java contract source code written by others

cd ~fabric-samples/chaincode/

git clone https://gitee.com/kernelHP/hyperledger-fabric-contract-java-demo.git

cd ../../test-network

export PATH=${<!-- -->PWD}/../bin:$PATH //Add the binary files in the bin directory to the CLI path

export FABRIC_CFG_PATH=$PWD/../config/ //Set FABRIC_CFG_PATH to point to the core.yaml file in fabric-samples

package creates a chaincode 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

Install the chaincode package

After the chain code is packaged, we need to install the chain code on each peer node that approves the transaction. We will set the endorsement policy to require endorsements from both Org1 and Org2, we need to install the chaincode on the nodes of both organizations: peer0.org1.example.com and peer0.org2.example.com

Set environment variables to operate peer CLI 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

install node installs chaincode

peer lifecycle chaincode install hyperledger-fabric-contract-java-demo.tar.gz

The process will be very slow, because some files need to be downloaded, you can open another terminal to view the process

Screenshot of successful chaincode startup. Repeat the previous command to find that the chaincode is successfully installed.

Set the environment variable, operate the node as Org2, and repeat the steps to install the chain code

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
peer lifecycle chaincode install hyperledger-fabric-contract-java-demo.tar.gz

Definition via chaincode

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 the node, the package ID can be used to associate the node-installed chaincode with the passed chaincode definition

queryinstalled queries the package ID, which is used to associate the chaincode installed on the peer node with the passed chaincode definition, and allows organizations to use chaincode to approve transactions

peer lifecycle chaincode query installed

Set the bundle id as an environment variable

export CC_PACKAGE_ID=hyperledger-fabric-contract-java-demo_1:54a21ac334812b0505cd548f1aed0bc0a7b603eef44c98a17ef05f004737b451

approveformyorg Org2 is defined by chain code (current environment variable setting administrator is Org2)

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

Reset environment variables to make 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

Org1 is defined by chaincode

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

Chain code definition is submitted to the channel

checkcommitreadiness checks whether the channel members have approved the same chaincode definition, will generate a JSON map showing whether the channel members have approved

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

commit commits the chaincode definition to the channel

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

querycommitted confirms that the chaincode definition has been submitted to the channel, and if successful, puts back the order and version of the chaincode definition

Call 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 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" , "blue" , "big lazy cat"]}'

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" , "White" , "Super Lazy Cat"]}'

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"]}'

no cat query

create a cat

query again

Turn off the network

./network.sh down

Problems encountered

1.docker keeps starting

https://blog.csdn.net/ACkingdom/article/details/125747583

2. Forget about the test network

3. Java version is too high, recommend 1.8