Hyperledger fabric test environment deployment

1. Preface

This tutorial runs under the ubuntu20.04 version. Please deploy the ubuntu20.04 environment before performing the fabric2.2 environment deployment. All deployments are carried out in accordance with the official document hyperledger fabric. I also share the problems and solutions encountered during the operation. I will answer them one by one. If you don’t know much about blockchain, please check out the related concepts of blockchain. If there is any unclear understanding or unclear expression in the article, you are welcome to criticize and correct me.

2. Introduction to hyperledger fabric

Hyperledger Fabric is one of the blockchain projects in Hyperledger and one of the classic consortium chains. It has a ledger, uses smart contracts, and a system for managing transactions by participants, but it is not completely decentralized, that is, members who want to join the alliance need to register from a trusted Membership Service Provider (MSP), as follows An introduction to some related concepts.

image

3. Test network example

3.1 Setting up a development environment

1. Install git

sudo apt-get install git

2. Install curl

sudo apt-get install curl

3. Install docker

sudo apt-get -y install docker-compose
docker --version
docker-compose --version
# Tip, for future docker-related errors, perform step 2, step 3, step 1
#1. Restart docker
sudo systemctl start docker
# Set the system to automatically start docker, optional
sudo systemctl enable docker
#2. Add the user to the docker group and ensure that the user command can be executed
sudo gpasswd -a $USER docker
#3. Update user group
newgrp docker
# docker information
docker info

4. Install go

Install and unzip:

# Download
wget https://studygolang.com/dl/golang/go1.20.9.linux-amd64.tar.gz

# Unzip
sudo tar -C /usr/local -xzf go1.20.9.linux-amd64.tar.gz
# Go environment configuration
mkdir $HOME/go
#Open ~./bashrc with vi and configure environment variables
vi ~/.bashrc
# Insert at the bottom
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
#Make the configured environment variables take effect
source ~/.bashrc
#Check if the configuration is correct
go version
# Configure goproxy environment variables to accelerate domestic downloads
go env -w GOPROXY=https://goproxy.io

3.2 Installation examples, binaries and Docker images

The cURL commands in the instructions below will set up your environment so that you can run a Fabric test network. Specifically, it performs the following steps:

  • Clone the hyperledger/fabric-samples repository
  • Download the latest Hyperledger-Fabric Docker image and mark it as latest
  • Download the Hyperledger Fabric CLI tool binaries and configuration files to the fabric-samples/bin and /config directories. These binaries will be useful for subsequent and test network interactions.

1. Obtain the installation script

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

Run the script -h to view the following operations

./install-fabric.sh -h
Usage: ./install-fabric.sh [-f|--fabric-version <arg>] [-c|--ca-version <arg>] <comp-1> [<comp-2>] ... [<comp-n>] ...
        <comp>: Component to install one or more of d[ocker]|b[inary]|s[amples]. If none specified, all will be installed
        -f, --fabric-version: FabricVersion (default: '2.5.4')
        -c, --ca-version: Fabric CA Version (default: '1.5.7')

2. Select to install specified components

./install-fabric.sh --fabric-version 2.2.0 binary

3. Precautions

Automatic download scripts are usually unable to download the relevant content directly due to network problems.

The following is a manual download solution

  • Manually clone the hyperledger/fabric/repository and obtain the automatic download script
  • Modify the automatic download part and change it to manually obtain the specified image and fabric-samples warehouse (note: the relevant component version number requirements are indicated in the script)
Clone the hyperledger/fabric-samples repository
mkdir -p $GOPATH/src/github.com/hyperledger
cd $GOPATH/src/github.com/hyperledger
# Get fabric-samples source code
git clone https://github.com/hyperledger/fabric-samples.git
Select the appropriate version tag and enter the directory switch branch
cd fabric-samples
# You can choose your own version
git checkout release-2.2
#View version
git branch
Clone the hyperledger/fabric repository
mkdir -p $GOPATH/src/github.com/hyperledger
cd $GOPATH/src/github.com/hyperledger
# Get fabric-samples source code
git clone https://github.com/hyperledger/fabric.git
Select the appropriate version tag and enter the directory switch branch
cd fabric
# You can choose your own version
git checkout release-2.2
#View version
git branch
View deployment script requirements for each component version
# if version not passed in, default to latest released version
VERSION=2.2.13
# if ca version not passed in, default to latest released version
CA_VERSION=1.5.6
Modify bootstrap.sh
# This will download the .tar.gz
download() {
    #local BINARY_FILE=$1
    #local URL=$2
    #echo "===> Downloading: " "${URL}"
    #curl -L --retry 5 --retry-delay 3 "${URL}" | tar xz || rc=$?
    #if [ -n "$rc" ]; then
    # echo "==> There was an error downloading the binary file."
    # return 22
    #else
        echo "==> Done."
    #fi
}

3.3 Start test network

You can find the script to start the network in the test-network directory of the fabric-samples code repository. Use the following command to navigate to the test network directory:

cd fabric-samples/test-network

In this directory, you can find the annotated script network.sh, which sets up a fabric network using a docker image on your local computer. You can run ./network.sh -h to print help text to the terminal:

Usage:
  network.sh <Mode> [Flags]
    Modes:
      up - bring up fabric orderer and peer nodes. No channel is created
      up createChannel - bring up fabric network with one channel
      createChannel - create and join a channel after the network is created
      deployCC - deploy the asset transfer basic chaincode on the channel or specify
      down - clear the network with docker-compose down
      restart - restart the network

    Flags:
    -ca <use CAs> - create Certificate Authorities to generate the crypto material
    -c <channel name> - channel name to use (defaults to "mychannel")
    -s <dbtype> - the database backend to use: goleveldb (default) or couchdb
    -r <max retry> - CLI times out after certain number of attempts (defaults to 5)
    -d <delay> - delay duration in seconds (defaults to 3)
    -ccn <name> - the short name of the chaincode to deploy: basic (default),ledger, private, secured
    -ccl <language> - the programming language of the chaincode to deploy: go (default), java, javascript, typescript
    -ccv <version> - chaincode version. 1.0 (default)
    -ccs <sequence> - chaincode definition sequence. Must be an integer, 1 (default), 2, 3, etc
    -ccp <path> - Optional, chaincode path. Path to the chaincode. When provided the -ccn will be used as the deployed name and not the short name of the known chaincodes.
    -cci <fcn name> - Optional, chaincode init required function to invoke. When provided this function will be invoked after deployment of the chaincode and will define the chaincode as initialization required.
    -i <imagetag> - the tag to be used to launch the network (defaults to "latest")
    -cai <ca_imagetag> - the image tag to be used for CA (defaults to "latest")
    -verbose - verbose mode
    -h - print this message

 Possible Mode and flag combinations
   up -ca -c -r -d -s -i -verbose
   up createChannel -ca -c -r -d -s -i -verbose
   createChannel -c -r -d -verbose
   deployCC -ccn -ccl -ccv -ccs -ccp -cci -r -d -verbose

 Taking all defaults:
   network.sh up

 Examples:
   network.sh up createChannel -ca -c mychannel -s couchdb -i 2.0.0
   network.sh createChannel -c channelName
   network.sh deployCC -ccn basic -ccl javascript

In the test-network directory, run the following command to delete any previously run containers or projects:

./network.sh down

Then, you can start the network by executing the following command.

./network.sh up

This command creates a fabric network consisting of two peer nodes and an ordering node. No channel is created when running ./network.sh up. We will implement this in subsequent steps. If the command is executed successfully, you will see the node log that was created.

Creating network "net_test" with the default driver
Creating volume "net_orderer.example.com" with default driver
Creating volume "net_peer0.org1.example.com" with default driver
Creating volume "net_peer0.org2.example.com" with default driver
Creating orderer.example.com ... done
Creating peer0.org2.example.com ... done
Creating peer0.org1.example.com ... done
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8d0c74b9d6af hyperledger/fabric-orderer:latest "orderer" 4 seconds ago Up Less than a second 0.0.0.0:7050->7050/tcp orderer.example.com
ea1cf82b5b99 hyperledger/fabric-peer:latest "peer node start" 4 seconds ago Up Less than a second 0.0.0.0:7051->7051/tcp peer0.org1.example.com
cd8d9b23cb56 hyperledger/fabric-peer:latest "peer node start" 4 seconds ago Up 1 second 7051/tcp, 0.0.0.0:9051->9051/tcp peer0.org2.example.com