Wefantasy /FabricLearn: This project virtualized and progressively implemented a studio alliance chain requirement, aiming to provide an easy-to-understand, reproducible Fabric learning project in which each part of the project deployment step is clearly visible, and all experiments are packaged as scripts that can be quickly replicated on any host (github.com)

preface

We have implemented the writing and deployment of Ethereum smart contracts based on Truffle framework, but the way they work means that they can only be used in a limited number of business scenarios. In contrast, Fabric, based on super ledgers, is highly scalable and customizable and can be used in more complex business scenarios, but Fabric technology involves many new concepts, and the source code is fast and incompatible between versions, making it unfriendly to beginners. In order to quickly master Fabric, this paper built a blockchain operating environment based on its latest version 2.4, deployed the official chaincode and conducted interactive debugging on it, and finally the whole environment and sample code can run normally and get the expected results.

Environment set up

Almost all Fabric tutorials on the web are based on Ubuntu rather than Windows. This is mainly due to the poor performance of the Docker environment that Fabric requires to run on Windows, and the fact that many official Fabric documentation is based on Ubuntu. Running on Windows can cause unpredictable bugs. In order to facilitate the later deployment to the public network server, I wanted to build an environment on CentOS. However, due to the suspension of CentOS8 maintenance and poor operating experience of CentOS Stream, I finally chose Debian system. The system and software versions of this environment are as follows:

System, software version
VMware Pro 16.0.0
Debian Debian – 11.2.0 – amd64 – DVD – 1. Iso
git 2.30.2
curl 7.74.0
docker 20.10
golang go1.17.8
jq Jq – 1.6 –
fabric 2.4.0
fabric-ca 1.5.2
fabric-samples v2.3.0

Each Docker image version of this environment is as follows:

The mirror version
hyperledger/fabric-tools 2.4
hyperledger/fabric-peer 2.4
hyperledger/fabric-orderer 2.4
hyperledger/fabric-ccenv 2.4
hyperledger/fabric-baseos 2.4
hyperledger/fabric-ca 1.5

Warning: It is recommended that all Fabric experiments be performed under root, otherwise there will be many problems with environment variables during the sudo switch.

Miscellaneous installation

  1. Install the latest versionGit
    apt install git
    Copy the code
  2. Install the latest versioncURL
    apt install curl
    Copy the code
  3. The installationGolang
  4. The installationjq
    apt install jq
    Copy the code

Install the Fabric

Official script Installation

Bootstrap. sh: bootstrap.sh: bootstrap.sh: bootstrap.sh: bootstrap.sh: bootstrap.sh: bootstrap.sh

wget https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh
chmod +x bootstrap.sh
./bootstrap.sh
Copy the code

Not surprisingly, you should see a smooth environment installation of the script:

Manual installation

Of course, if you use the official script and it doesn’t go wrong (for network reasons), you can manually install all the environments you need.

  1. Install the fabric samples

Fabric-samples is the official Demo collection for Fabric, which contains multiple internal samples, each with chain implementations of Golang, JavaScript, typescript, Java, and can be deployed directly to the corresponding fabric, which is helpful for beginners. The fabric – samples installation is very simple, use the git clone [email protected]: hyperledger/fabric – samples. Git to cloning project source code to the local, If it fails all the time, you can directly download the corresponding version of the compressed package in release. Fabric is the core development tool of alliance chain, which contains all the commands during our development, compilation, and deployment. 3. Download Fabric 2.4.0 and decompress it

The mkdir wget https://github.com/hyperledger/fabric/releases/download/v2.4.0/hyperledger-fabric-linux-amd64-2.4.0.tar.gz /usr/local/fabric tar -xzvf Hyperledger-fabric-linux-amd64-2.1.2.tar. gz -c /usr/local/fabricCopy the code
  1. Download fabric-CA 1.5.2 and unzip it
Wget HTTP: / / https://github.com/hyperledger/fabric-ca/releases/download/v1.5.2/hyperledger-fabric-ca-linux-amd64-1.5.2.tar.gz Tar -xzvf Hyperledger -fabric-ca-linux-amd64-1.5.2.tar.gz mv bin/* /usr/local/fabric/binCopy the code
  1. Set the environment variable in/etc/profileAt the end of the add
#Fabric
export FABRIC=/usr/local/fabric
export PATH=$PATH:$FABRIC/bin
Copy the code
  1. Updating environment variablessource /etc/profile

Install the Docker

  1. Remove the old version if it exists
apt remove docker docker-engine docker.io containerd runc
Copy the code
  1. updateaptIndex the package and allow it to be usedHTTPSThe installation
apt update
apt install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg \
    lsb-release
Copy the code
  1. addDockerThe officialGPGThe key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Copy the code
  1. addDockerwarehouse
echo \
"deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null
Copy the code
  1. The installationDockerengine
apt update
apt install docker-ce docker-ce-cli containerd.io
Copy the code
  1. The installationdocker-compose
apt install docker-compose
Copy the code

Install Docker image dependencies

Fabric-related images can be downloaded from the official image website of DockerHub, and the installation method can be obtained if you search the images you need. All images used in this experiment are as follows:

Docker pull Hyperledger/Fabric-tools :2.4 Docker pull Hyperledger/Fabric-peer :2.4 Docker pull Hyperledger/fabric-Orderer :2.4 Docker pull Hyperledger/Fabric-CCENv :2.4 Docker pull Hyperledger/Fabric-Baseos :2.4 docker Pull hyperledger/fabric - ca: 1.5Copy the code

Use the docker images command to view the images after installation:

Hyperledger /fabric-tools 2.4 625237d887db 4 weeks ago 473MB hyperledger/fabric-peer 2.4 ee643d889779 4 weeks ago 62.3MB Hyperledger/Fabric-Orderer 2.4 DF64446AC2df 4 weeks ago 37.3MB Hyperledger/Fabric-CCENV 2.4 DA4F00CB576a 4 weeks ago 517MB hyperledger/fabric-baseos 2.4 0287ebf8aaf3 4 weeks ago 6.94MB hyperledger/fabric-ca 1.5 4ea287b75c63 6 months ago 69.8 MBCopy the code

The image tag used in the sample code is “latest”. However, if you select “latest” directly during pull, an error may be reported. Therefore, we manually label the image with “latest” after the above image is pulled:

#Docker Tag IMAGEID: REPOSITORY
docker tag 625237d887db hyperledger/fabric-tools:latest
docker tag ee643d889779 hyperledger/fabric-peer:latest
docker tag df64446ac2df hyperledger/fabric-orderer:latest
docker tag da4f00cb576a hyperledger/fabric-ccenv:latest
docker tag 0287ebf8aaf3 hyperledger/fabric-baseos:latest
docker tag 4ea287b75c63 hyperledger/fabric-ca:latest
Copy the code

The final image is:

Run the test

Starting a Fabric Network

  1. Go to the test-network directory of fabric-sample
cd fabric-samples/test-network
Copy the code
  1. run./network.sh up Start the network
Creating network "fabric_test" with the default driver Creating volume "docker_orderer.example.com" with default driver Creating volume "docker_peer0.org1.example.com" with default driver Creating volume "docker_peer0.org2.example.com" with  default driver Creating peer0.org1.example.com ... done Creating orderer.example.com ... done Creating peer0.org2.example.com ... done Creating cli ... done CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7738c1e84751 hyperledger/fabric-tools:latest "/bin/bash" Less  than a second ago Up Less than a second cli 1f24de2c6cd5 hyperledger/fabric-peer:latest "peer node start" 2 seconds ago Up Less than a second 0.0.0.0:9051->9051/ TCP, ::9051->9051/ TCP, 0.0.0.0:19051->19051/ TCP, :::19051->19051/tcp peer0.org2.example.com bfc48b20360c hyperledger/fabric-orderer:latest "orderer" 2 seconds ago Up Less than a second 0.0.0.0:7050->7050/ TCP, :::7050->7050/ TCP, 0.0.0.0:7053->7053/ TCP, :::7053->7053/ TCP, :::7053->7053/ TCP, 0.0.0.0:17050 - > 17050 / TCP. :::17050->17050/tcp orderer.example.com b9a61fdaf47a hyperledger/fabric-peer:latest "peer node start" 2 seconds ago Up Less than a second 0.0.0.0:7051->7051/ TCP, ::7051->7051/ TCP, 0.0.0.0:17051->17051/ TCP, :::17051->17051/tcp peer0.org1.example.comCopy the code

If the preceding logs are displayed, the network is successfully started. Each Node and User that joins the Fabric network must belong to an organization. The network contains two parallel organizations ————peer0.org1.example.com and peer0.org2.example.com. It also includes an orderer.example.com that maintains the network as the ordering Service.

Create a channel

In the previous section, having run the peer and orderer nodes on the machine, you can now use network.sh to create a channel between Org1 and Org2. A channel is a private channel between specific network members that can only be used by the organization that belongs to the channel and is not visible to other network members. Each channel has a separate blockchain ledger, and organizations belonging to the channel can have their peers join the channel so that the peer can store the ledger on the channel and verify the transactions on the ledger. Create a custom channel testChannel using the following command:

./network.sh createChannel -c testchannel
Copy the code

Deploy chaincode

It is recommended that all deployment operations be performed as the root account. Otherwise, unknown errors may occur. The following describes the problems encountered by the author when the user is not the root user.

Once the channel is created, you can start interacting with the channel ledger using smart contracts. Smart contracts contain the business logic that manages the assets on the blockchain ledger, where a network of applications run by members can invoke smart contracts to create, change, and transfer those assets. Smart contract deployment can be done with the./network.sh deployCC command, but this process can be fraught with problems. Deploy ChainCode using the following command:

./network.sh deployCC -c testchannel -ccn basic -ccp .. /asset-transfer-basic/chaincode-go -ccl goCopy the code

Scripts/deploycc. sh: line 114: log. TXT: Permission denied scripts/ deploycc. sh: line 114: log. TXT: Permission denied

./network.sh deployCC -c testchannel -ccn basic -ccp .. /asset-transfer-basic/chaincode-go -ccl goCopy the code

New error added with sudo:deployCC.sh: line 59: go: command not found. Checking this UsergoCommand available, checkrootThe usergoCommand available, onlysudoIt can’t be used after. And when I looked it up, IT was becauselinuxThe system is restricted to use for security purposessudoWill empty the custom environment variables, the simplest solution is in/etc/sudoersThe limit is commented out directly in the file2:Retry the previous command with a comment and a new error occurs:

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.81:443: I/O timeoutCopy the code

Proxy.golang.org is obviously inaccessible to the local network. At the command line, enter the go env – w GO111MODULE = on && go env – w GOPROXY = https://goproxy.cn, direct command to configure the domestic agent again after 3. Is the unexpected error unchanged, setting the proxy did not take effect? Manually using the go get github.com/golang/protobuf manually download and install the run-time error or unchanged again, this time to check the local GOPATH directory for github.com/golang/protobuf package, why not to recognize? Sudo command will use the go environment variable of root, and the previously set proxy and downloaded package can only take effect for the local user. So the ultimate solution to this problem is to simply switch to root and reconfigure the Go agent and run it. After a successful run, you can see the following results:

2021-08-15 00:45:54.064 PDT [chaincodeCmd] ClientWait -> INFO 001 TXID [ebeb8df6904f45b81fb30714f7eecb30b4bbfd32f4acc809f34f7c660e396eb8] committed with status (VALID) at localhost:7051 2021-08-15 00:45:54.144 PDT [chaincodeCmd] ClientWait -> INFO 002 TXID [ebeb8df6904f45b81fb30714f7eecb30b4bbfd32f4acc809f34f7c660e396eb8] committed with status (VALID) at localhost:9051 Chaincode definition committed on channel 'testchannel' Using organization 1 Querying chaincode definition on peer0.org1  on channel 'testchannel'... Attempting to Query committed status on peer0.org1, Retry after 3 seconds. + peer lifecycle chaincode querycommitted --channelID testchannel --name basic + res=0 Committed Definition for Chaincode 'basic' on channel' testChannel ': Version: 1.0, Sequence: 1, Calm Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true, Org2MSP: true] Query chaincode definition successful on peer0.org1 on channel 'testchannel' Using organization 2 Querying chaincode definition on peer0.org2 on channel 'testchannel'... Attempting to Query committed status on peer0.org2, Retry after 3 seconds. + peer lifecycle chaincode querycommitted --channelID testchannel --name basic + res=0 Committed Definition for Chaincode 'basic' on channel' testChannel ': Version: 1.0, Sequence: 1, Calm Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true, Org2MSP: true] Query chaincode definition successful on peer0.org2 on channel 'testchannel' Chaincode initialization is not requiredCopy the code

Contract interaction

Context During the Fabric installation, we have set environment variables for the Fabric executable. Ensure that the peer command can be used in the test-network directory successfully.

  1. Set the FABRIC_CFG_PATH variable to include the core.yaml file
export FABRIC_CFG_PATH=$PWD/.. /config/# export FABRIC_CFG_PATH=/usr/local/fabric/config/
Copy the code
  1. Set the otherOrg1Variable dependencies of the organization
# Environment variables for Org1
#The CORE_PEER_TLS_ROOTCERT_FILE and CORE_PEER_MSPCONFIGPATH environment variables point to the identity certificate in the Organizations folder of 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:7051Copy the code
  1. Initialize 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 testchannel -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":[]}'Copy the code

4. Query the account asset list

peer chaincode query -C testchannel -n basic -c '{"Args":["GetAllAssets"]}'
Copy the code

5. Modify the book 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 testchannel -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"]}'Copy the code

6. Shut down the network

./network.sh down
Copy the code

This command will stop and delete nodes and codecontainers, organize cryptographic material, delete previously running channel items and Docker volumes, and remove codeimages from the Docker Registry.

Because the endorsement strategy of asset-Transfer (BASIC) chain code requires the transaction to be signed by Org1 and Org2 at the same time, So the chain call instructions need to use the –peerAddresses tag to point to peer0.org1.example.com and peer0.org2.example.com; Since TLS is enabled for the network, the directive also needs to point to the TLS certificate of each peer with the –tlsRootCertFiles tag.

reference


  1. Qq_JWang_03215367. Failed to find command. Longed for lesson. [2018-07-31] ↩
  2. Failed to normalize Chaincode path: ‘Go list’ failed with: go. Blog Park. [2020-11-27]↩