当前位置:网站首页>Hyperledger fabric 2.0 installation attempt

Hyperledger fabric 2.0 installation attempt

2020-12-07 10:22:52 osc_ 8eb8cps3



Preface

It mainly records the brief process of exploring blockchain technology for the first time


Tips : The following is the main body of this article , The following cases can be used for reference

One 、 Environment deployment steps

1. Install basic tools

First of all, we need to make sure that the basic environment for our operation is Ubuntu18.04 edition
The code is as follows ( Example ):

sudo apt install git  // install git
sudo apt install curl	// install curl

 install pip And update the 
sudo apt install python-pip
pip install --upgrade pip

2. install docker

The code is as follows ( Example ):

1. Installation is required first 17.06.2-ce Or later , It is recommended to install the latest version of docker.

2. install docker There was no guarantee that docker The version is too low. We choose to uninstall the previous docker, reinstall docker

sudo apt-get remove docker docker-engine docker.io

3. uninstall docker When I'm done, I start installing docker, Execute the command separately

sudo apt install docker.io
docker -v // View version 
// Restart the service 
sudo systemctl daemon-reload
sudo systemctl restart docker

3. install docker-compose

sudo apt install docker-compose
docker-compose -v
sudo chmod +x /usr/share/doc/docker-compose

4. install Go Language

First install some of the necessary dependencies :

sudo apt install libtool libltdl-dev

Go to the website https://studygolang.com/dl download Go Language installation package , I have the latest version of : go1.14.6.linux-amd64.tar.gz

Copy the compressed package to /usr/local Under the path , Then decompress :

wget https://studygolang.com/dl/golang/go1.14.6.linux-amd64.tar.gz

Download the Golang After compressing the package , Use tar Command to decompress the compressed package to the specified /usr/local/ Under the path , The order is as follows

sudo tar -xzvf go1.14.6.linux-amd64.tar.gz -C /usr/local/

3. Let's start configuring environment variables

sudo vim /etc/profile

Add the following code at the end of the file

export PATH=$PATH:/usr/local/go/bin
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin

4. Use source command , Make the configuration information you just added take effect

source /etc/profile

5. Use go version Command to verify that the installation was successful
If the version number is shown , It means the installation is successful . My version number is like this

go version
go version go1.14.6 linux/amd64

Two 、Hyperledger Fabric install

1. Pull fabric Source code

First create a folder ( By default, students /home/stack/ Under the table of contents , there stack Change to your own user name )

mkdir -p go/src/github.com/hyperledger/

Enter the folder you just created

cd go/src/github.com/hyperledger/

from github Pull up fabric Source code :

git clone https://hub.fastgit.org/hyperledger/fabric.git

modify bootstrap.sh Script

cd fabric/
cd scripts/
vim ~/go/src/github.com/hyperledger/fabric/scripts/bootstrap.sh

Modify the script
 Insert picture description here

by
 there samples We'll download it later

function bootstrap.sh Script

sudo ./bootstrap.sh

notes : This place may still be slow : You can try to change docker Source
Reference resources : Change source reference ( Close test effectively )

Pull fabric-samples
Entry directory

cd ~/go/src/github.com/hyperledger/fabric/scripts

And then you start pulling fabric-samples

git clone https://hub.fastgit.org/hyperledger/fabric-samples.git

Download the binary and unzip

cd ~/go/src/github.com/hyperledger/fabric/scripts/fabric-samples
wget https://github.91chifun.workers.dev//https://github.com/hyperledger/fabric/releases/download/v2.2.1/hyperledger-fabric-linux-amd64-2.2.1.tar.gz

Then unzip the package in the current directory

sudo tar -xzvf hyperledger-fabric-linux-amd64-2.2.1.tar.gz

But this is not the end !!!!! After decompressing fabric-samples The next one is bin Folder , There are actually two things missing from it fabric-ca The file of , This will lead to the problem that the binary file can not be found in the later run . therefore , Next, I follow the method of the official website to operate as follows :

cd   ( This command returns to the user root directory )
mkdir test ( Create a temporary folder )
cd /test

Carry out the orders given on the official website

curl -sSL https://bit.ly/2ysbOFE | bash -s

After downloading ,test There will also be one under the folder fabric-samples Folder , There is also a bin Folder , Open it to see that there are two more executable files than above fabric-ca-client ,fabric-ca-server(*** It's what we lack !***)
Put this bin Copy the entire folder to our previous fabric-samples Next

cp -r /home/stack/test/fabric-samples/bin /home/stack/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/

( It depends on your catalogue , There is a space between the two paths )
Back to our previous catalog

cd /home/stack/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network

2. start-up Fabric The Internet , Call test chain code

start-up Fabric The Internet

sudo ./network.sh up

Establish a channel

sudo ./network.sh createChannel

12 Start the chain code on the channel
( It's suggested to do it again
source /etc/profile Otherwise, there may be mistakes in the next step )

sudo ./network.sh deployCC

13 Set environment variables ( Must be in test-network Under the table of contents )

sudo vim /etc/profile 

Add the following information :

export PATH=${
   
      PWD}/../bin:${
   
      PWD}:$PATH
export FABRIC_CFG_PATH=${
   
      PWD}/../config/

Continue to add the following environment variables

# 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/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=localhost:7051

After completion
source /etc/profile( Must be in test-network Under the table of contents )

function

peer chaincode query -C mychannel -n fabcar -c '{"Args":["queryAllCars"]}'

( But there should be an error here )

Because no will fabcar Join channel !

therefore , Go back to the previous Directory

cd ..

Get into fabcar Catalog

cd /fabcar

function startFabric.sh Executable file

./startFabric.sh

16 If it runs successfully , Then go back to test-network Catalog

cd ..
cd /test-network

Now run again

peer chaincode query -C mychannel -n fabcar -c '{"Args":["queryAllCars"]}'

Get a list of cars
end !

3、 ... and 、 stay fabric-sample Deploy your own chain code in the network environment

It is suggested to enter super management account ( command :su - , Then enter the password to enter . Specific functions and how to operate Baidu , That's easy to understand ). And then don't add... In the subsequent operation sudo!!
It's mainly about personal choice , In actual development cases, user accounts are used , Instead of root Permission account , But if you use root Permission to operate will be easier to succeed , So here's to root For example, access accounts :
First of all docker Change the source of ( According to personal network environment reference )

cd /etc/docker
cat daemon.json
{
   
      
	"registry-mirrors": [
	"https://docker.mirrors.ustc.edu.cn",
	"https://kfwkfulq.mirror.aliyuncs.com",
	"https://2lqq34jg.mirror.aliyuncs.com",
	"https://pee6w651.mirror.aliyuncs.com",
	"https://registry.docker-cn.com",
	"http://hub-mirror.c.163.com"
]
}

systemctl daemon-reload
systemctl restart docker

Then you can replace your own chain code fabcar What's in it , You can also create a new folder and write your own chain code file
I've just replaced it here fabcar The content of
Get into fabric-samples/test-network Catalog

cd ~/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network

The following content refers to the blog content of big guy
https://blog.csdn.net/mutou___/article/details/110039475#comments_13973567

3.1 Return to the known initial state

Use the following command to end all activities or stale Docker Container and delete previously generated parts

sudo ./network.sh down

3.2 Start the test network

sudo ./network.sh up createChannel

The createChannel Command to create a to mychannel Two channel members Org1 and Org2 Named channels . The order will also belong to each organization peer Node join channel . If networks and channels are successfully created , You can see the following message in the log :

Channel successfully joined====

3.3、 Set up Logspout( Optional )

This step is not required , But for troubleshooting chain codes are very useful .
Fabric Example monitordocker.sh Medium commercial-paper Examples have been included for installation and configuration Logspout Script for .

Open a new terminal
Get into fabric-samples/test-network Catalog

cd ~/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network

take monitordocker.sh Script from the commercial-paper Example copied to fabric-samples/test-network In the working directory

sudo cp ../commercial-paper/organization/digibank/configuration/cli/monitordocker.sh .

start-up Logspout

sudo ./monitordocker.sh net_test

After success, you can see output similar to the following :

Starting monitoring on all containers on the network net_test
Unable to find image 'gliderlabs/logspout:latest' locally
latest: Pulling from gliderlabs/logspout
188c0c94c7c5: Already exists 
f76076d59b2f: Pull complete 
d09edbff691c: Pull complete 
Digest: sha256:2b70c1989a00a2775311e66aec8be6e2479deb0e0c0c0be603de1863f49027e8
Status: Downloaded newer image for gliderlabs/logspout:latest
1c6c61c50c02e45282d565c8177d81358945a1ab59065a822491ee88fcac1ad6

3.4、 Package smart contracts

Be careful : Here's the following Go Smart contract packaging steps written in language ,JavaScript See Section 8 for packing .

Open a new terminal
install chaincode Dependencies
Dependencies go.mod stay fabric-samples/chaincode/fabcar/go The file in the directory lists , The go.mod The file will be Fabric contract API Import into smart contract package .

cd ~/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/chaincode/fabcar/go
sudo GO111MODULE=on go mod vendor

Back to test-network In the directory , So that the chain code can be packaged with other network components .

cd ../../../test-network

take bin The binary files in the directory are added to CLI route
The chain code package of the required format can be used peer CLI establish , Use the following command to add these binaries to your CLI route .

export PATH=${
   
      PWD}/../bin:$PATH

Set up FABRIC_CFG_PATH To point to fabric-samples Medium core.yaml file

export FABRIC_CFG_PATH=$PWD/../config/

Create chain code package
Use peer lifecycle chaincode package Command to create chain code package .

sudo peer lifecycle chaincode package fabcar.tar.gz --path ../chaincode/fabcar/go/ --lang golang --label fabcar_1

Command interpretation : This command will create a directory named basic.tar.gz Software package .–lang Tags are used to specify the chain code language ,–path The tag provides the location of the smart contract code , The path must be a standard path or a path relative to the current working directory ,–label Tag is used to specify a chain code label , The label will identify the chain code after it is installed . It is recommended that your tag contain the chain code name and version .

Now? , We've created chain code packages , We can install the chain code on the peer node of the test network .

3.5、 Install the chain code package

pack Fabcar After the smart contract , We can do it in peer Install chain code on node . Need to be in each of the peer Install chain code on node . Because we're going to set up endorsements to request from Org1 and Org2 Endorsement of , So we need to be in two organizations peer Install chain code on node :peer0.org1.example.com and peer0.org2.example.com.

Org1 peer Node installation chain code
First in Org1 peer Install chain code on node .

Set the following environment variables , With Org1 Administrator's identity operation peer CLI.

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/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=localhost:7051

Use peer lifecycle chaincode install Command in peer Install chain code on node .

sudo peer lifecycle chaincode install fabcar.tar.gz

If the command is successful , be peer The node will generate and return the package identifier . next step , This package ID Will be used to pass the chain code . You should see output similar to the following :

2020-11-24 20:59:00.766 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 001 Installed remotely: response:<status:200 payload:"\nIfabcar_1:762e0fe3dbeee0f7b08fb6200adeb4a3a20f649a00f168c0b3c2257e53b6e506\022\010fabcar_1" > 
2020-11-24 20:59:00.775 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 002 Chaincode code package identifier: fabcar_1:762e0fe3dbeee0f7b08fb6200adeb4a3a20f649a00f168c0b3c2257e53b6e506

Org2 peer Node installation chain code
Set the following environment variables , With Org2 Administrator's identity operation peer CLI.

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_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/Admin@org2.example.com/msp
export CORE_PEER_ADDRESS=localhost:9051

Use peer lifecycle chaincode install Command in peer Install chain code on node .

sudo peer lifecycle chaincode install fabcar.tar.gz

Be careful : When installing the chain code , Chain code by peer Node construction . If there is a problem with the smart contract code ,install The command will return all build errors from the chain code .

3.6、 Define by chain code

After installing the chain code package , It needs to be defined by the chain code of the organization . The definition includes important parameters of chain code management , Such as name , Version and chain code approval strategy .

If the organization is already in its peer Chain code is installed on the node , Then they need to include the package in the chain code definition adopted by their organization ID. package ID Is used to peer The chain code installed on the node is associated with the chain code definition passed , And allow organizations to use chain codes to recognize transactions .

Query package ID
have access to peer lifecycle chaincode queryinstalled Order to inquire peer node , To find the package of the chain code ID.

sudo peer lifecycle chaincode queryinstalled

package ID It's a combination of the hash values of the chain code tag and the chain code binary file . Every peer Nodes will generate the same package ID. You should see output similar to the following :

Installed chaincodes on peer:
Package ID: fabcar_1:762e0fe3dbeee0f7b08fb6200adeb4a3a20f649a00f168c0b3c2257e53b6e506, Label: fabcar_1

Through the chain code , We will use the package ID, therefore , Package ID Save as environment variable . The package that will be returned ID Paste it into the command below .
Be careful : package ID It's not the same for all users , So you need to use the package returned from the command window in the previous step ID To complete this step . Instead of copying commands directly !!!

export CC_PACKAGE_ID=fabcar_1:762e0fe3dbeee0f7b08fb6200adeb4a3a20f649a00f168c0b3c2257e53b6e506

Org2 Define by chain code
Because the environment variable has been set to peer CLI As Orig2 The administrator operates , So we can use Org2 The organization level will be Fabcar Chain code is defined by . Use peer lifecycle chaincode approveformyorg Commands are defined by chain codes :

sudo peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --version 1.0 --package-id $CC_PACKAGE_ID --sequence 1 --tls --cafile ${
   
      PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

Org1 Define by chain code
Set the following environment variables to Org1 Run as Administrator :

export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_MSPCONFIGPATH=${
   
      PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_TLS_ROOTCERT_FILE=${
   
      PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_ADDRESS=localhost:7051

Org1 Define by chain code :

sudo peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --version 1.0 --package-id $CC_PACKAGE_ID --sequence 1 --tls --cafile ${
   
      PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

3.7、 Submit chain code definition to channel

Use peer lifecycle chaincode checkcommitreadiness Command to check whether channel members have approved the same chain code definition :

sudo peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name fabcar --version 1.0 --sequence 1 --tls --cafile ${
   
      PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --output json
1

This command will generate a JSON mapping , This map shows whether channel members have approved checkcommitreadiness The parameters specified in the command :

{
   
      
	"approvals": {
   
      
		"Org1MSP": true,
		"Org2MSP": true
	}
}

Because both organizations that are members of the channel agree to the same parameters , So the chain code definition is ready to be submitted to the channel . You can use peer lifecycle chaincode commit Command to submit the chain code definition to the channel .commit Commands also need to be submitted by the organization administrator .

sudo peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --version 1.0 --sequence 1 --tls --cafile ${
   
      PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --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

have access to peer lifecycle chaincode querycommitted Command to confirm that the chain code definition has been submitted to the channel .

sudo peer lifecycle chaincode querycommitted --channelID mychannel --name fabcar --cafile ${
   
      PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

If the chain code is successfully submitted to the channel , The querycommitted The command will return the order and version of the chain code definition :

Version: 1.0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true, Org2MSP: true]

This is the end of the content , You can start calling your own chain code .


版权声明
本文为[osc_ 8eb8cps3]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/202012071021432231.html