Hyperledger Fabric uses a testnet

Table of Contents

  • 1. Prerequisites
    • Install git, curl, wget
    • Install the go language
    • Install JDK
    • install docker
    • Install docker-compose
  • 2. Use the test network
    • 2.1 Download fabric source code
    • 2.2 Start the network
    • 2.3 Interacting with the network

1. Prerequisites

Install git, curl, wget

//ubuntu
sudo apt-get install git curl wget

Install the go language

Get offline package wget -c https://golang.google.cn/dl/go1.16.6.linux-amd64.tar.gz
Unzip to the specified directory tar -zxvf go1.16.6.linux-amd64.tar -C /usr/local
Configure system variables echo 'export PATH=$PATH:/usr/local/go/bin'>>/etc/profile
Create GOPATH variable mkdir -p /opt/gopath ; echo 'export GOPATH=/opt/gopath'>>/etc/profile
Make environment variables effective source /etc/profile
View version go version

Install JDK

Get offline package: manually download http://www.oracle.com/technetwork/java/javase/downloads/index.html, upload to /tmp
Or use wget -c https://download.oracle.com/java/20/latest/jdk-20_linux-x64_bin.tar.gz to get the offline package
Unzip to the specified directory sudo tar -zxvf jdk-8u162-linux-x64.tar.gz -C /usr/local/
Modify the environment variable sudo vim ~/.profile
    #set oracle jdk environment
    export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_162 ## Note here that the directory should be replaced with the jdk directory you decompressed yourself
    export JRE_HOME=${<!-- -->JAVA_HOME}/jre
    export CLASSPATH=.:${<!-- -->JAVA_HOME}/lib:${<!-- -->JRE_HOME}/lib
    export PATH=${<!-- -->JAVA_HOME}/bin:$PATH
Make environment variables effective source /etc/profile

Install docker

sudo apt-get remove docker docker-engine docker.io containerd runc ###Uninstall the old version
sudo apt-get update ###Update apt package index
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common ###Install dependent packages
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - ###Add Docker's official GPG key https://mirrors.aliyun.com/docker-ce/linux /ubuntu/gpg
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" ###Add official software source https://mirrors.aliyun .com/docker-ce/linux/ubuntu
sudo apt update ###Update apt package index
sudo apt install docker-ce
sudo docker run hello-world
sudo usermod -aG docker $USER ### Execute Docker as a non-root user

Install docker-compose

To install a different version of Compose, replace 1.29.2 with the version of Compose you want to use
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/ local/bin/docker-compose
For executable permissions sudo chmod +x /usr/local/bin/docker-compose
Verify version docker-compose --version

2. Use the test network

2.1 Download fabric source code

Enter $GOPATH, download fabric source code
git clone https://github.com/hyperledger/fabric.git
Enter the directory cd fabric/scripts
Execute ./bootstrap.sh
    Function:
        -b: With this parameter, the binary file will not be downloaded
        -d: With this parameter, the docker image will not be pulled
        -s: Do not download fabric-samples with this parameter

2.2 Start the network

Start the network
    cd ~/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network
    ./network.sh up start the network
    ./network.sh down close the network
        Error Peer binary and configuration files not found. Solution: copy the bin and config directories to fabric-samples


Components of the test network
    docker ps -a
    The network has two federation members, Org1 and Org2. Also included is an ordering organization that maintains a network ordering service.

create a channel
    Create a channel with the default name "mychannel" ./network.sh createChannel
    Create a channel called channel1 (the channel flag allows creating multiple channels with multiple different names): ./network.sh createChannel -c channel1
    Create a network and create a channel in one step ./network.sh up createChannel


2.3 Interacting with the network

Start a chaincode on a channel
    The deployCC subcommand will install the asset-transfer (basic) chaincode on peer0.org1.example.com and peer0.org2.example.com. Then deploys the specified channel's chaincode on the channel using the channel flag (or mychannel if no channel is specified). (You can use the language sticky flag -l to install the Java or javascript version of the chaincode)
    ./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
        Error: Error: failed to normalize chaincode path: 'go list' failed with: go: github.com/golang/[email protected]: Get "https://proxy.golang.org/github.com/ golang/protobuf/@v/v1.3.2.mod": dial tcp 172.217.160.113:443: i/o timeout: exit status 1
        Solution (visit golang's official website):
            go env -w GO111MODULE=on
            go env -w GOPROXY=https://goproxy.cn,direct

interact with the network
    Add the binaries to your CLI path: export PATH=${<!-- -->PWD}/../bin:$PATH
    Set FABRIC_CFG_PATH in the fabric-samples codebase to point to the core.yaml file in it: export FABRIC_CFG_PATH=$PWD/../config/
    Set environment variables to allow you to operate the peer CLI as Org1:
        # Environment variables for 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

    Initialize the ledger with some assets:
        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":[]}'

    Use the CLI tool to query the ledger: peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'
    Call the asset-transfer (basic) chaincode to change the asset owner on the ledger:
        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"]}'

    Set environment variables to allow you to operate the peer CLI as Org2:
        # Environment variables for Org2
        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 run on peer0.org2.example.com asset-transfer (basic) chaincode:
        peer chaincode query -C mychannel -n basic -c '{"Args":["ReadAsset","asset6"]}'