Hyperledger Fabric 环境搭建

2018-04-03 20:25:18

所有操作在centos7下完成,其他系统(ubuntu/macos..)类似,修改相应指令即可。

1. 安装Golang

golang官网 获取最新版本 https://dl.google.com/go/go1.10.1.linux-amd64.tar.gz

下载最新版本

wget https://dl.google.com/go/go1.10.1.linux-amd64.tar.gz

解压

sudo tar -C /usr/local -xzf go1.10.1.linux-amd64.tar.gz

设置环境变量vi ~/.bash_profile,增加以下内容

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

重新载入环境变量

source ~/.bash_profile

至此 golang 安装成功,输入go version查看版本

2. 安装Docker

下载并安装,使用阿里的源:

curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-engine/internet | sh -

替换docker hub源的阿里源,加快镜像下载速度:

sudo tee /etc/docker/daemon.json <<-'EOF'
{
    "registry-mirrors": ["https://obou6wyb.mirror.aliyuncs.com"]
}
EOF

修改完后,重启docker,查看docker版本

systemctl daemon-reload
sudo systemctl restart docker
docker version

3. 安装docker-compose

1、安装pip

sudo get install python-pip

2、 下载docker-compose

curl -L https://get.daocloud.io/docker/compose/releases/download/1.12.0/docker-compose-`uname -s`-`uname -m` > ~/docker-compose

3、 将docker-compose移动至/usr/local/bin文件夹当中,使其作为可执行命令文件

mv ./docker-compose /usr/local/bin/docker-compose

4、修改docker-compose权限

chmod 755 /usr/local/bin/docker-compose

5、查看版本

docker-compose version

4. 下载Fabric源码

切换至go的GOROOT路径下,即第一步安装go的时候配置的路径,也可输入go env查看。

[root@VM_162_219_centos ~]# go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/root/go"
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build440227295=/tmp/go-build -gno-record-gcc-switches"

切换至/usr/local/go目录后,进入src代码目录。如果不存在github.com文件夹,则新建一个。在github.com下新建hyperledger文件夹并进入,此时所在路径为:

/usr/local/go/src/github.com/hyperledger

在当前目录下,从github拉取fabric源码

git clone https://github.com/hyperledger/fabric.git

本次使用1.0.0版本(与后面安装的docker fabric镜像版本保持一致!)

cd fabric
git checkout v1.0.0

到此为止,fabric源码下载成功。

5. 下载Docker Fabric镜像

切换到下载的fabric源码目录

cd /usr/local/go/src/github.com/hyperledger/fabric/examples/e2e_cli

利用源码中的脚本下载1.0.0版本的fabric docker镜像

source ./download-dockerimages.sh -c x86_64-1.0.0 -f x86_64-1.0.0

下载完后可以执行docker images查看镜像

[root@VM_162_219_centos e2e_cli]# docker images
REPOSITORY                               TAG                 IMAGE ID            CREATED             SIZE
dev-peer1.org2.example.com-mycc-1.0      latest              93a9797573ad        2 hours ago         173 MB
dev-peer0.org1.example.com-mycc-1.0      latest              bef7e3211370        2 hours ago         173 MB
dev-peer0.org2.example.com-mycc-1.0      latest              2884f1a84570        2 hours ago         173 MB
docker.io/hello-world                    latest              f2a91732366c        4 months ago        1.85 kB
docker.io/hyperledger/fabric-tools       x86_64-1.0.0        0403fd1c72c7        8 months ago        1.32 GB
hyperledger/fabric-tools                 latest              0403fd1c72c7        8 months ago        1.32 GB
docker.io/hyperledger/fabric-couchdb     x86_64-1.0.0        2fbdbf3ab945        8 months ago        1.48 GB
hyperledger/fabric-couchdb               latest              2fbdbf3ab945        8 months ago        1.48 GB
docker.io/hyperledger/fabric-kafka       x86_64-1.0.0        dbd3f94de4b5        8 months ago        1.3 GB
hyperledger/fabric-kafka                 latest              dbd3f94de4b5        8 months ago        1.3 GB
docker.io/hyperledger/fabric-zookeeper   x86_64-1.0.0        e545dbf1c6af        8 months ago        1.31 GB
hyperledger/fabric-zookeeper             latest              e545dbf1c6af        8 months ago        1.31 GB
docker.io/hyperledger/fabric-orderer     x86_64-1.0.0        e317ca5638ba        8 months ago        179 MB
hyperledger/fabric-orderer               latest              e317ca5638ba        8 months ago        179 MB
hyperledger/fabric-peer                  latest              6830dcd7b9b5        8 months ago        182 MB
docker.io/hyperledger/fabric-peer        x86_64-1.0.0        6830dcd7b9b5        8 months ago        182 MB
docker.io/hyperledger/fabric-javaenv     x86_64-1.0.0        8948126f0935        8 months ago        1.42 GB
hyperledger/fabric-javaenv               latest              8948126f0935        8 months ago        1.42 GB
docker.io/hyperledger/fabric-ccenv       x86_64-1.0.0        7182c260a5ca        8 months ago        1.29 GB
hyperledger/fabric-ccenv                 latest              7182c260a5ca        8 months ago        1.29 GB
docker.io/hyperledger/fabric-ca          x86_64-1.0.0        a15c59ecda5b        8 months ago        238 MB
hyperledger/fabric-ca                    latest              a15c59ecda5b        8 months ago        238 MB
docker.io/hyperledger/fabric-baseos      x86_64-0.3.1        4b0cab202084        10 months ago       157 MB

6. 启动Fabric网络

启动Fabric网络实际上是启动下载的Fabric Docker镜像,并执行一系列命令

cd /usr/local/go/src/github.com/hyperledger/fabric/examples/e2e_cli

./network_setup.sh up

最终出现以下提示时启动成功:

===================== Query on PEER3 on channel 'mychannel' is successful =====================

===================== All GOOD, End-2-End execution completed =====================


 _____   _   _   ____            _____   ____    _____
| ____| | \ | | |  _ \          | ____| |___ \  | ____|
|  _|   |  \| | | | | |  _____  |  _|     __) | |  _|
| |___  | |\  | | |_| | |_____| | |___   / __/  | |___
|_____| |_| \_| |____/          |_____| |_____| |_____|

启动成功后,可以control+c退出,fabric并不会中止,而是在后台运行在docker中。

7. 使用合约测试Fabric网络

前面下载的fabric源码中包含了一个合约示例,当启动Fabric网络之后,启动了一个名为mychannel的channel及一个名为mycc的链码。

首先连接到Fabric网络所在的docker容器:

docker exec -it cli bash

mycc的合约定义了两个账户a和b,分别查看他们的账户余额:

peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'

peer chaincode query -C mychannel -n mycc -c '{"Args":["query","b"]}'

Query Result为账户余额

root@6762dba419ff:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
2018-04-03 12:03:14.367 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2018-04-03 12:03:14.367 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2018-04-03 12:03:14.367 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc
2018-04-03 12:03:14.367 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc
2018-04-03 12:03:14.367 UTC [msp/identity] Sign -> DEBU 005 Sign: plaintext: 0A95070A6708031A0C0882DB8DD60510...6D7963631A0A0A0571756572790A0161
2018-04-03 12:03:14.367 UTC [msp/identity] Sign -> DEBU 006 Sign: digest: 51A66C3205697E626DDDD25E6995FBCC04BDBD6D602E68959D2995EC13DEC8E1
Query Result: 90
2018-04-03 12:03:14.373 UTC [main] main -> INFO 007 Exiting.....
2018-04-03 12:03:14.373 UTC [main] main -> INFO 007 Exiting.....
root@6762dba419ff:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode query -C mychannel -n mycc -c '{"Args":["query","b"]}'
2018-04-03 12:08:36.201 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2018-04-03 12:08:36.201 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2018-04-03 12:08:36.201 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc
2018-04-03 12:08:36.201 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc
2018-04-03 12:08:36.201 UTC [msp/identity] Sign -> DEBU 005 Sign: plaintext: 0A94070A6608031A0B08C4DD8DD60510...6D7963631A0A0A0571756572790A0162
2018-04-03 12:08:36.201 UTC [msp/identity] Sign -> DEBU 006 Sign: digest: 0E7915778933F605D5B7D04D52FFF7608F390E0AF2A7B37C6C3FA6027747DF56
Query Result: 210
2018-04-03 12:08:36.207 UTC [main] main -> INFO 007 Exiting.....

使用合约的invoke方法,从a账户转账20给b账户:

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"]}'

转账成功后再次查询a和b的余额,此时余额已经发生变化。转账成功。

root@6762dba419ff:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
2018-04-03 12:03:14.367 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2018-04-03 12:03:14.367 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2018-04-03 12:03:14.367 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc
2018-04-03 12:03:14.367 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc
2018-04-03 12:03:14.367 UTC [msp/identity] Sign -> DEBU 005 Sign: plaintext: 0A95070A6708031A0C0882DB8DD60510...6D7963631A0A0A0571756572790A0161
2018-04-03 12:03:14.367 UTC [msp/identity] Sign -> DEBU 006 Sign: digest: 51A66C3205697E626DDDD25E6995FBCC04BDBD6D602E68959D2995EC13DEC8E1
Query Result: 70
2018-04-03 12:03:14.373 UTC [main] main -> INFO 007 Exiting.....
root@6762dba419ff:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer chaincode query -C mychannel -n mycc -c '{"Args":["query","b"]}'
2018-04-03 12:08:36.201 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP
2018-04-03 12:08:36.201 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity
2018-04-03 12:08:36.201 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc
2018-04-03 12:08:36.201 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc
2018-04-03 12:08:36.201 UTC [msp/identity] Sign -> DEBU 005 Sign: plaintext: 0A94070A6608031A0B08C4DD8DD60510...6D7963631A0A0A0571756572790A0162
2018-04-03 12:08:36.201 UTC [msp/identity] Sign -> DEBU 006 Sign: digest: 0E7915778933F605D5B7D04D52FFF7608F390E0AF2A7B37C6C3FA6027747DF56
Query Result: 230
2018-04-03 12:08:36.207 UTC [main] main -> INFO 007 Exiting.....

至此fabric环境搭建成功,测试成功。

退出docker容器exit

停止fabric docker进程,中止fabric网络

cd /usr/local/go/src/github.com/hyperledger/fabric/examples/e2e_cli

./network_setup.sh down
搭建ETH私链并创建ERC20 Token

1、下载ETH钱包 下载 Ethereum Wallet或Mist其一即可,二者功能相同。 2、安装geth Mac下使用: brew install geth 其余平台参考安装方法 3、编辑创世区块配置文件 genesis.json:(参考示例) ludis@MacBook ~ cd Desktop/ethereum ludis@MacBook ~/Desktop/ethereum cat > genesis.json { "config": { "chainId": 33, "homesteadBlock": 0, "eip155Block": 0, "eip158Block": 0

从一道菜开始重操旧业——土豆鸡块

闲下来一看,已经四个月没写博客了。 入币圈这么久,这半年多却是最让人“忙”的。忙的记不起更博,虽然中途学了很多新技术,做了几个好菜想要记录,留了一年的辫子也剪成寸头。 正是去年年末,比特币一举达到2w多美元。从那时起,我的微信群、QQ群、小密圈等等爆发式的多了起来,每天微信和qq显示的未读消息都是999+,实际上如果软件支持的话。这个数字可能是9999+。 币圈的信息淹没了我。第一次感到精力完全不够用,每天都有数十个币种、ico在涌来。被信息淹没的后果,不是错过了很多信息,而是开始什么都不看了。是的,每个群,每个公众号都慢慢开始被封印。从一开始的生怕错过一条信息,到后面的无动于衷,中间的转换也是一种经历。 从一个吃瓜码农,到每天账户余额两位数波动。短短的一年,我竟然体验了很多的大起大落。虽然结局比较忧桑,但我却有一丝满足,因为有了这些体验,整个人的世界观价值观有了一次升级。这或许是每个参与到这种“世纪泡沫”中的人都有的体会,一切都是一个轮回,一个泡沫的萌生,高潮,退潮,再次觉醒...能参与到这种泡沫中的人都是“幸福的”