title: ubuntu下搭建区块链 (Hyperledger Fabric)
date: 2020-01-04 19:50:36
tags: 区块链
category: 教程
博主环境:华为云Ubuntu 16.04.6 LTS (GNU/Linux 4.4.0-165-generic x86_64)
**使用root用户登录 **
下载
wget https://studygolang.com/dl/golang/go1.9.linux-amd64.tar.gz
安装
tar -C /usr/local -xzf go1.9.linux-amd64.tar.gz
打开配置文件
vim ~/.bashrc
修改文件(在下面加入)
GOROOT=/usr/local/go
GOPATH=~/go
PATH=$GOPATH/bin:$PATH
如果配置过其他的环境,java什么的,PATH加到后面就行
PATH= J A V A H O M E / b i n : JAVA_HOME/bin: JAVAHOME/bin:ZK_HOME/bin: G O P A T H / b i n : GOPATH/bin: GOPATH/bin:PATH
保存退出(esc,输入:wq)
source .bashrc
新建 go的目录GOPATH
mkdir go
apt-get update
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
apt-get -y update
apt-get -y install docker-ce
docker version
mkdir -p /etc/docker
修改文件
vim /etc/docker/daemon.json
加入
"registry-mirrors": ["https://obou6wyb.mirror.aliyuncs.com"]
保存退出,重启docker
systemctl daemon-reload
systemctl restart docker
sudo apt-get install python-pip
curl -L https://get.daocloud.io/docker/compose/releases/download/1.12.0/docker-compose-`uname -s`-`uname -m` > ~/docker-compose
sudo mv ~/docker-compose /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
mkdir -p ~/go/src/github.com/hyperledger
cd ~/go/src/github.com/hyperledger
git clone https://github.com/hyperledger/fabric.git
cd ~/go/src/github.com/hyperledger/fabric
git checkout v1.0.0
cd ~/go/src/github.com/hyperledger/fabric/examples/e2e_cli/
source download-dockerimages.sh -c x86_64-1.0.0 -f x86_64-1.0.0
执行命令:docker images
apt install libtool libltdl3-dev
cd ~/go/src/github.com/hyperledger/fabric
make cryptogen
build/bin/cryptogen
CGO_CFLAGS=" " GOBIN=/home/studyzy/go/src/github.com/hyperledger/fabric/build/bin go install -tags "" -ldflags "-X github.com/hyperledger/fabric/common/tools/cryptogen/metadata.Version=1.0.0" github.com/hyperledger/fabric/common/tools/cryptogen
Binary available as build/bin/cryptogen
cd ~/go/src/github.com/hyperledger/fabric/examples/e2e_cli
# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
# DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
127.0.0.1
# options timeout:2 attempts:3 rotate single-request-reopen
执行启动命令,它会启动一个 mychannel 的channel
./network_setup.sh up mychannel
进入到docker 的shell 中
docker exec -it cli bash
查看a 用户有多少余额
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
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"]}'
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
exit
./network_setup.sh down myChannel