Environment construction of hyperledger fabric v1.2 in blockchain

789 2021-04-02 06:25:53 阅读数:852

本文一共[544]字,预计阅读时长:1分钟~
environment construction hyperledger fabric v1.2


> 2015 year ,Linux The Foundation launched Hyperledger project , The goal is to develop cross industry blockchain Technology .
> Hyperledger The project is not just about defining a single blockchain standard , It also encourages the collaborative development of blockchain technology through the power of the open source community .Hyperledger Fabric yes Hyperledger A blockchain project in . Similar to other blockchain technologies ,Hyperledger Fabric Include a ledger , Using smart contracts and a system to manage transactions through all participants .
> Hyperledger Fabric The biggest difference from other blockchain systems lies in private ownership and licensing . It is different from the open network system that allows unknown participants to join the network without permission ( Workload proof protocol is needed to ensure effective transaction and maintain network security ),Hyperledger Fabric adopt Membership Service Provider(MSP) To register all the members . Hyperledger Fabric Several pluggable options are also available . Ledger data can be stored in a variety of formats , The consensus mechanism can be accessed or disconnected , At the same time support a variety of different MSP.

> Hyperledger Fabric Provides the establishment of channel The function of , This allows participants to create a separate ledger for the transaction . When some of the players in the network are competitors , This function has become particularly important . Because these participants don't want all the trading information —— For example, specific price information provided to some customers —— Are open to all participants in the network . Only in the same channel Participants in , Will have the channel The books in the book , And others are not here channel The participants in the project can't see the account book .


Hyperledger Fabric Is a platform that provides distributed ledger solutions .
Hyperledger Fabric Supported by modular architecture , And has excellent confidentiality 、 Scalability 、 Flexibility and scalability
Hyperledger  Fabric It is designed to support different modules and components to plug and enable directly , And be able to adapt to a variety of complex scenarios in the economic ecosystem . 
Hyperledger Fabric Provides a unique scalable 、 Extensible architecture , This is also Hyperledger  Fabric Significant differences from other blockchain solutions . If you're planning to deploy an enterprise level blockchain with a full review mechanism and open source architecture ,Hyperledger Fabric It's a good starting point for you .

Reference resources Hyperledger  Fabric Official documents in Chinese :[https://hyperledgercn.github.io/hyperledgerDocs/](https://hyperledgercn.github.io/hyperledgerDocs/)

### 1. Get ready Linux System Suggest Ubuntu 16.04 perhaps 18.04
###### 1.1. see apt source Source
```bash
vim /etc/apt/sources.list
```


```bash
// Beiyou mirror source
deb http://mirrors.byrio.org/ubuntu/ bionic main restricted
deb http://mirrors.byrio.org/ubuntu/ bionic-updates main restricted
deb http://mirrors.byrio.org/ubuntu/ bionic universe
deb http://mirrors.byrio.org/ubuntu/ bionic-updates universe
deb http://mirrors.byrio.org/ubuntu/ bionic multiverse
deb http://mirrors.byrio.org/ubuntu/ bionic-updates multiverse
deb http://mirrors.byrio.org/ubuntu/ bionic-backports main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu bionic-security main restricted
deb http://security.ubuntu.com/ubuntu bionic-security universe
deb http://security.ubuntu.com/ubuntu bionic-security multiverse

// Aliyuan
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
```
###### 1.2 Update source

```bash
apt-get update && apt-get upgrade
```
### 2. install Golang   
go Language Chinese official website :[https://studygolang.com/dl](https://studygolang.com/dl)


```bash
cd $HOME/ && wget https://storage.googleapis.com/golang/go1.8.1.linux-amd64.tar.gz

// Unzip to /usr/local
tar -C /usr/local -xzf go1.8.1.linux-amd64.tar.gz

// Modify environment variables
vim /etc/profile

// Set up go The path of
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH= P A T H : PATH: GOROOT/bin:$GOPATH/bin

// Environment variables take effect
source /etc/profile

// see go edition
go version
```

### 3. install git、cURL、 libltdl-dev

```bash
apt-get -y install git
// see git edition  
git -version

apt-get -y install curl
// see curl edition
curl -version

apt-get -y install libltdl-dev

```

### 4. install Docker

```bash
// Install the package to allow apt adopt HTTPS Using remote Libraries
apt-get -y install apt-transport-https ca-certificates curl software-properties-common

// add to Docker The official GPG secret key
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -

// Use the following command to set up a stable version of the remote library
add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu 
 $(lsb_release -cs) stable"

// Update again apt Package index
apt-get -y update

// Use apt install docker-ce
apt-get -y install docker-ce


// Verify that the installation is successful and view the version
docker version 
```
* notes : If the second command is executed, an error will be reported *
![ Insert picture description here ](https://img-blog.csdnimg.cn/2021033011005364.png)
### 5. install python-pip、docker-compose

```bash
apt-­get -y install python­-pip

// see pip edition
pip -V


apt-get -y install docker­-compose

// see docker-compose edition
docker­-compose ­-­version
```

### 6. download fabric Source code and Fabric-samples

###### 6.1 Manually create new  mkdir Folder

```bash
// obtain fabirc And switch versions
git clone https://gitee.com/arxin/fabric.git

// Get into fabric
cd /fabric

// View the current version information
git branch -a

// Switch to 1.2.0 edition
git checkout -b v1.2.0

// obtain fabric-samples And switch versions
curl -sSL https://bit.ly/2ysbOFE | bash -s -- 1.2.0
```
* notes : In case of unsuccessful execution , Choose accelerated link or code cloud *

```bash
// Acceleration address
git clone https://github.com.cnpmjs.org/wlc1216/Hyperledger-fabric.git
// Code cloud
git clone https://gitee.com/arxin/fabric.git
```

###### 6.2 View the pull image
```bash
docker images
```

###### 6.3 Network testing

```bash
// Get into first-network Folder
cd ../fabric/fabric-samples/first-network
```
![ Insert picture description here ](https://img-blog.csdnimg.cn/20210330134436274.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dsY18xMTEx,size_16,color_FFFFFF,t_70)

 - .env: Store some environment variables
 - base: Storage docker-compose Some of our public services
 - byfn.sh: Execute the script
 - configtx.yaml and crypto-config.yaml: According to the 2 A tool , Generate the corresponding configuration file , To start the network , Put it in the current directory channel-artifacts and crypto-config in
 - dockper-compose: Used to start the network
 - scripts: Store test scripts , What to do : Create channels 、 Join channel , Installation chain code , Instantiation chain code , Chain code interaction

```bash
// Generate the required certificates and images
./byfn.sh -m generate  

// Start network
./byfn.sh -m up  
```
![ Insert picture description here ](https://img-blog.csdnimg.cn/20210330150247153.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dsY18xMTEx,size_16,color_FFFFFF,t_70)
wait for .....
![ Insert picture description here ](https://img-blog.csdnimg.cn/20210330150331359.png)

good !! congratulations This page appears successful

```bash
// Turn off network
./byfn.sh -m down  
```


* notes : In the process of network startup docker-compose Report errors *![ Insert picture description here ](https://img-blog.csdnimg.cn/20210330134014803.png)
as a result of compse The reading time is too short
```bash
vi /etc/resolv.conf
```

terms of settlement : take options timeout:2 attempts:3 rotate single-request-reopen This line is preceded by a well mark, annotated, saved and exited

### 7. test favirc The Internet
In the official case ,channel The name is mychannel, The name of the chain code is mycc

Use docker exec Order to enter CLI Containers :

```bash
docker exec -it cli bash
```
stay /opt/gopath/src/github.com/hyperledger/fabric/peer Under the table of contents , Run the following command to query a The balance of the account :
```bash
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
```
![ Insert picture description here ](https://img-blog.csdnimg.cn/20210330151336369.png)
Now we can transfer money , Operation for invoke , from a turn b 20:

```bash
peer chaincode invoke -o orderer.example.com:7050  --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem  -C mychannel -n mycc -c '{"Args":["invoke","a","b","20"]}'
```

![ Insert picture description here ](https://img-blog.csdnimg.cn/20210330153258647.png)

Transfer succeeded !!!


* notes : About the successful transfer , But there's no synchronization problem *

The reason should be endorsement strategy , Two org Should be endorsed and can not be omitted

AND('Org1.member', 'Org2.member', 'Org3.member') It means that three subjects need to sign and endorse together

 OR('Org1.member', 'Org2.member') Indicates that signature endorsement of one of the two principals is required

 OR('Org1.member',AND('Org2.member', 'Org3.member')) Indicate need Org1 Signature endorsement or Org2 and Org3 Joint signature endorsement


### 8. About node.js Development
##### 1. install node.js and npm

```bash
curl ­sL https://deb.nodesource.com/setup_8.x | sudo ­-E bash ­-

// see node edition
node -­­v

// see npm edition
npm ­-v
```
###### 1.1 Move to fabcar Catalog

```bash
cd ../
cd fabcar
```
###### 1.2 install node modular

```bash
npm install
```
* notes : There may be node.js Version of the problem , Try upgrading *

npm Upgrade or update to the specified version , Operation instruction
```bash
npm install -g npm   perhaps  npm -g install npm@3.0.0
```

###### 1.3 install grpc Module with Hyperledger Fabric communicate :

```bash
sudo npm install grpc
```
###### 1.4 start-up fabcar Of Hyperledger Fabric The Internet

```bash
./startFabric.sh
```

Refer to some articles
thank @Hyperledger Working group on internationalization
[https://hyperledgercn.github.io/hyperledgerDocs/build_network_zh/](https://hyperledgercn.github.io/hyperledgerDocs/build_network_zh/)
thank @ Horse McccccL
[https://blog.csdn.net/so5418418/article/details/78355868](https://blog.csdn.net/so5418418/article/details/78355868)
thank @ Pen Pavilion
[https://my.oschina.net/u/2275217/blog/3014598](https://my.oschina.net/u/2275217/blog/3014598)
[https://www.tqwba.com/x_d/jishu/357137.html](https://www.tqwba.com/x_d/jishu/357137.html)

版权声明:本文为[789]所创,转载请带上原文链接,感谢。 https://netfreeman.com/2021/03/20210331173529508l.html