Recently I am learning Hyperledger Fabric, which is an open source blockchain distributed ledger initiated and created by the Linux Foundation.
Hyperledger Fabric is an open source blockchain implementation. The development environment is built on the VirtualBox virtual machine. The deployment environment can be built on its own network or directly deployed on BlueMix. The deployment method can be traditionally Dockerized, consensus reached algorithm plug-in, and support Go and JavaScript develop smart contracts, especially with enterprise-level security mechanisms and membership mechanisms.
Let’s talk about how to build a Hyperledger Fabric based on Ubuntu Server today.
It is recommended to use a virtual machine to install an Ubuntu Server version, do not install a graphical interface, that is too stuck. When installing the Ubuntu Server version, note that if you choose the English version, you will encounter errors when installing the simplified Chinese version.
After installation, remember to change the source to Alibaba Cloud. When changing here, because the virtual machine does not support copy and paste, you can only enter it manually:
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
After changing the source, install the following software:
Note that most of them only need sudo apt-install to install. Baidu like go and docker will know how to install it, so I won't go into it. Remember to upgrade to the latest version of nodejs.
At present, most of the fabric tutorials on the Internet are based on version 1.4. Today, we will demonstrate how to build a 2.1 version of the Fabric environment for learning.
Go to the GO directory to create a hyperledger folder:
mkdir -p $GOPATH/src/github.com/hyperledger
cd $GOPATH/src/github.com/hyperledger/
Then download the source code of fabric, pay attention to the link of Code Cloud Gitee here, GitHub is too slow to go next year:
git clone https://gitee.com/mirrors/fabric.git
I don't know why my gitee is so slow. . .
Switch fabric to 2.1 version:
cd fabric/
git branch -a
git checkout release-2.1
Remember to add a sudo, you can't do anything without administrator rights.
cd scripts/
sudo ./bootstrap.sh
Then entered a long wait. The fabric-samples is downloaded from GitHub, which may be slow. After the download is complete, all downloaded docker images will be listed.
After the above work is completed, there is an additional fabric-samples folder in the current directory. We go into the test-network directory under this directory to test whether the environment built under the directory is successful:
cd fabric-samples/
cd test-network/
Then start our test network:
sudo ./network.sh up
Remember to add sudo to keep it safe. After starting a bunch of code:
That the creation was successful.
fabcar is a small demo, let's run it here and test it. We go back to the fabric-samples directory, and then enter the /fabcar directory, first clean up the network, and then start:
sudo ./networkDown.sh
sudo ./startFabric.sh
Of course, you may encounter problems, such as not being able to find the go command. This is because the sudo command resets the current environment variables, causing the settings to go not to be found.
Error: failed to normalize chaincode path: failed to determine module root: exec:"go": executable file not found in $PATH
!!!!!!!!!!!!!!! Chaincode packaging on peer0.org1 has failed !!!!!!!!!!!!!!!!
According to the online method to solve the sudo environment variable problem, set the following in your shell configuration file
vim ~/.bashrc add as follows
alias sudo='sudo env PATH=$PATH LD_LIBRARY_PATH=$LD_LIBRARY_PATH'
Then refresh to make the configuration take effect:
source ~/.bashrc
When installing the golang fabric api dependency package, there was no response for a long time, and finally the following error was reported:
go: github.com/hyperledger/[email protected]: Get https://proxy.golang.org/github.com/hyperledger/fabric-contract-api-go/@v/v1.0.0.mod: dial tcp 172.217.27.145:443: i/o timeout ~/fabric-samples/test-network Finished vendoring Go dependencies ++ peer lifecycle chaincode package fabcar.tar.gz --path ../chaincode/fabcar/go/--lang golang --label fabcar_1 ++ res=1++set+x Error: failed to normalize chaincode path:'go list' failed with: go: github.com/hyperledger/[email protected]: Get https://proxy.golang.org/github.com/hyperledger/fabric-contract-api-go/@v/v1.0.0.mod: dial tcp 172.217.27.145:443: i/o timeout: exit status 1!!!!!!!!!!!!!!! Chaincode packaging on peer0.org1 has failed !!!!!!!!!!!!!!!!
ERROR !!! Deploying chaincode failed
Solution:
golang1.13.x can be executed directly:
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct
Finally, it succeeded:
After the previous step is executed successfully, the SDK interaction examples in each language environment will be output, for example, JavaScript is like this:
JavaScript:
Start by changing into the "javascript" directory:
cd javascript
Next, install all required packages:
npm install
Then run the following applications to enroll the admin user, and register a newuser
called appUser which will be used by the other applications to interact with the deployed
FabCar contract:
node enrollAdmin
node registerUser
You can run the invoke application as follows. By default, the invoke application will
create a newcar, but you can update the application to submit other transactions:
node invoke
You can run the query application as follows. By default, the query application will
return all cars, but you can update the application to evaluate other transactions:
node query
You can choose javascript, typescript, javago language to interact with the network, here is a try with javascript. Enter the javascript directory in fabcar, here you have to switch to the root user, I don’t know why sudo still has permission problems, execute:
cd javascript/
su
npm install
Install related dependencies. After completion, we start in sequence according to the contract process:
Register an administrator account:
sudo node enrollAdmin.js
registered user:
sudo node registerUser.js
We next execute a transaction
sudo node invoke.js
Check the status after the transaction:
sudo node query.js
It's done! ! ! At this point, the Fabric environment has been set up. Everyone can learn happily on it.
The latest and ultra-detailed Hyperledger Fabric2.2 environment to build and deploy
https://blog.csdn.net/shengsikandan/article/details/107656060
fabric2.1.0 package chaincode error report
https://blog.csdn.net/qq_32247229/article/details/108860823
go prompt failed to normalize chaincode path
https://www.jason-z.com/post/165
Build fabric 1.4.3 environment under ubuntu16.04
https://blog.csdn.net/Sun_Hui_/article/details/100928155
Recommended Posts