最近在做区块链项目,经团队几位大佬研究,终于把本地环境搭建起来了,在此记录一下。
基础环境准备 安装 Docker CE 卸载 docker 程序及相关依赖(可选)
使用 Docker 仓库进行安装
1 yum -y install gcc gcc-c++ yum-utils device-mapper-persistent-data lvm2
设置仓库
1 yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
安装 Docker-ce,安装完默认未启动,已经创建好 docker 用户组,但该组下没有用户。
1 yum install docker-ce docker-ce-cli containerd.io
查看 Docker 版本并确认安装成功
测试验证 Docker
安装 Docker-compose 下载对应的 Docker-compose 版本
1 wget https://github.com/docker/compose/releases/tag/1.25.0-rc4
将 docker-compose-Linux-x86_64 改名 docker-compose
1 mv docker-compose-Linux-x86_64 docker-compose
设置可执行权限
1 chmod +x /usr/local/bin/docker-compose
查看 Docker-compose 版本
1 docker-compose --version
安装 Go 从官网下载对应的Go版本包
1 wget https://dl.google.com/go/go1.10.3.linux-amd64.tar.gz
将源码包解压至 /usr/local/ 目录
1 tar -C /usr/local -zxvf go1.10.3.linux-amd64.tar.gz
修改环境变量 /etc/profile 文件
在环境变量末尾添加
1 export GOROOT=/usr/local/go # export PATH=$PATH:$GOROOT/bin
使修改后的环境变量生效
查看版本号
安装 Git 安装 git 依赖
1 yum install -y curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-ExtUtils-MakeMaker
下载 git 软件包
1 cd /opt/soft/ && wget https://github.com/git/git/archive/v2.22.0.tar.gz
将软件包解压至 /usr/local/git/ 目录
1 mkdir -p /usr/local/git && tar -C /usr/local/git/ zxvf /opt/soft/v2.22.0.tar.gz
进入源码目录,安装 git
1 make prefix=/usr/local/git all && make prefix=/usr/local/git install
配置环境变量,在 /etc/profile 尾部添加
1 export PATH=/usr/local/git/bin:$PATH
使环境变量生效
查看是否安装成功
Fabric 相关 fabric 代码 将 fabric-samples 下载到 $GOPATH/src/github.com/hyperledger/ 目录
1 mkdir -p $GOPATH/src/github.com/hyperledger
1 cd $GOPATH/src/github.com/hyperledger
克隆 fabric-samples 项目并切换到 v1.4 tag
1 git clone https://github.com/hyperledger/fabric-samples.git
1 git checkout -b sample v1.4.0
安装 Fabric Binaries 和 Fabric 相关的 Docker 镜像,将指定版本的 Hyperledger Fabric 特定于平台的二进制文件和配置文件安装到 fabric-samples 的 bin 和 config 目录中
1 wget https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric/hyperledger-fabric/linux-amd64-1.4.0/hyperledger-fabric-linux-amd64-1.4.0.tar.gz
1 wget https://nexus.hyperledger.org/content/repositories/releases/org/hyperledger/fabric-ca/hyperledger-fabric-ca/linux-amd64-1.4.0/hyperledger-fabric-ca-linux-amd64-1.4.0.tar.gz
1 tar xzvf hyperledger-fabric-linux-amd64-1.4.0.tar.gz -C $GOPATH/src/github.com/hyperledger/fabric-samples/
1 tar xzvf hyperledger-fabric-ca-linux-amd64-1.4.0.tar.gz -C $GOPATH/src/github.com/hyperledger/fabric-samples/
向 /etc/profile 中写入环境变量
1 sudo echo 'export PATH=$GOPATH/src/github.com/hyperledger/fabric-samples/bin:$PATH' >> /etc/profile
使环境变量生效
fabric 镜像 下载 Fabric 相关镜像
1 docker pull hyperledger/fabric-peer:1.4.0
1 docker tag hyperledger/fabric-peer:1.4.0 hyperledger/fabric-peer:latest
1 docker pull hyperledger/fabric-orderer:1.4.0
1 docker tag hyperledger/fabric-orderer:1.4.0 hyperledger/fabric-orderer:latest
1 docker pull hyperledger/fabric-ccenv:1.4.0
1 docker tag hyperledger/fabric-ccenv:1.4.0 hyperledger/fabric-ccenv:latest
1 docker pull hyperledger/fabric-tools
1 docker tag hyperledger/fabric-tools hyperledger/fabric-tools:latest
下载 Fabric 第三方镜像
1 docker pull hyperledger/fabric-couchdb:0.4.14
1 docker tag hyperledger/fabric-couchdb:0.4.14 hyperledger/fabric-couchdb:latest
1 docker pull hyperledger/fabric-kafka:0.4.14
1 docker tag hyperledger/fabric-kafka:0.4.14 hyperledger/fabric-kafka:latest
1 docker pull hyperledger/fabric-zookeeper:0.4.14
1 docker tag hyperledger/fabric-zookeeper:0.4.14 hyperledger/fabric-zookeeper:latest
下载 Fabric CA 镜像
1 docker pull hyperledger/fabric-ca:1.4.0
1 docker tag hyperledger/fabric-ca:1.4.0 hyperledger/fabric-ca:latest
启动
1 cd $GOPATH/src/github.com/hyperledger/fabric-samples/first-network
编译通过 Golang 开发的 chainCode 并启动相关的容器\
证书和签名 生成证书
1 ../../bin/cryptogen generate --config=./org3-crypto.yaml
生成 org3 的 json 字符串
1 export FABRIC_CFG_PATH=$PWD && ../../bin/configtxgen -printOrg Org3MSP > ../channel-artifacts/org3.json
拷贝 order 证书到 org3 目录下
1 cp -r crypto-config/ordererOrganizations org3-artifacts/crypto-config/
1 docker exec -it cli bash
1 export ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem && export CHANNEL_NAME=mychannel
获取 mychannel 的配置区块
1 peer channel fetch config config_block.pb -o orderer.example.com:7050 -c $CHANNEL_NAME --tls --cafile $ORDERER_CA
转为 json
1 configtxlator proto_decode --input config_block.pb --type common.Block | jq .data.data[0].payload.data.config > config.json
将 org3 加入到此 json 中
1 jq -s '.[0] * {"channel_group":{"groups":{"Application":{"groups": {"Org3MSP":.[1]}}}}}' config.json ./channel-artifacts/org3.json > modified_config.json
转为 pb
1 configtxlator proto_encode --input config.json --type common.Config --output config.pb
转为 pb
1 configtxlator proto_encode --input modified_config.json --type common.Config --output modified_config.pb
计算 pb 之间的增量
1 configtxlator compute_update --channel_id $CHANNEL_NAME --original config.pb --updated modified_config.pb --output org3_update.pb
转为 json
1 configtxlator proto_decode --input org3_update.pb --type common.ConfigUpdate | jq . > org3_update.json
加入 header 信息
1 echo '{"payload":{"header":{"channel_header":{"channel_id":"mychannel", "type":2}},"data":{"config_update":'$(cat org3_update.json)'}}}' | jq . > org3_update_in_envelope.json
转为 pb
1 configtxlator proto_encode --input org3_update_in_envelope.json --type common.Envelope --output org3_update_in_envelope.pb
org1 签名
1 peer channel signconfigtx -f org3_update_in_envelope.pb
切换到 org2 签名
1 export CORE_PEER_LOCALMSPID="Org2MSP"
1 export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
1 export CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
1 export CORE_PEER_ADDRESS=peer0.org2.example.com:7051
上传新配置
1 peer channel update -f org3_update_in_envelope.pb -c $CHANNEL_NAME -o orderer.example.com:7050 --tls --cafile $ORDERER_CA
编写 docker-compose 文件
1 docker-compose -f docker-compose-org3.yaml up -d
测试验证 进入容器
1 docker exec -it Org3cli bash
1 export ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem && export CHANNEL_NAME=mychannel
获取第 0 个区块
1 peer channel fetch 0 mychannel.block -o orderer.example.com:7050 -c $CHANNEL_NAME --tls --cafile $ORDERER_CA
打印信息如下:
1 2 2019-12-13 17:55:08.137 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized 2019-12-13 17:55:08.149 UTC [cli.common] readBlock -> INFO 002 Received block: 0
加入到 channel 里边
1 peer channel join -b mychannel.block
切换到另一个 peer
1 export CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org3.example.com/peers/peer1.org3.example.com/tls/ca.crt && export CORE_PEER_ADDRESS=peer1.org3.example.com:7051
加入到 channel 里边
1 peer channel join -b mychannel.block
至此,初步环境搭建完成,未完待续……
参考资料
hyperledger 白话区块链 用 Hyperledger Fabric开发一个Demo