搭建fabric网络

软硬件环境

硬件:阿里云主机4c16G1T
操作系统:centos7.6

1. 安装运行依赖

1.1. git

yum install  -y git 

1.2. docker

yum install  -y docker 
system enable docker
systemctl start docker

1.3. docker-compose

yum install  -y docker-compose 

1.4. golang

下载并解压

wget https://dl.google.com/go/go1.13.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.13.linux-amd64.tar.gz

设置path

vi 〜/ .bash_profile

将以下内容追加到bash_profile

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

保存文件,执行以下命令

source ~/.bash_profile

创建gopath目录,go get相关的库将下载到这里

mkdir -p /root/gopath

测试是否成功

go env

2. 拉取fabric源码

创建一个目录并进入

$ mkdir -p ~/go/src/github.com/hyperledger 
$ cd ~/go/src/github.com/hyperledger 

拉取代码,并切换到指定分支

$ git clone https://github.com/hyperledger/fabric.git 
$ cd fabric
$ git branch -a  
$ git checkout v1.4.4 

3. 拉取依赖(命令、镜像、fabric-samples库)

对网络有要求,镜像慢可以配置镜像加速

cd ~/go/src/github.com/hyperledger/fabric/scripts
$./bootstrap.sh -h
Usage: bootstrap.sh [version [ca_version [thirdparty_version]]] [options]
options:
-h : this help
-d : bypass docker image download
-s : bypass fabric-samples repo clone
-b : bypass download of platform-specific binaries

ca1.4.4仓库没有,无法下载,因而配置1.4.3

扫描二维码关注公众号,回复: 9811248 查看本文章
./bootstrap.sh 1.4.4 1.4.3 0.4.18
  • 如果当前目录没有 hyperledger/fabric-samples,会从 github.com 克隆 hyperledger/fabric-samples 存储库; 使用 checkout 签出对应指定的版本标签;
  • 将指定版本的 Hyperledger Fabric 平台特定的二进制文件和配置文件安装到 fabric-samples - 存储库的根目录中;
  • 下载指定版本的 Hyperledger Fabric Docker 镜像文件;将下载的 Docker 镜像文件标记为 “lastest"。

查看二进制脚本,将看到8个fabric二进制命令

$ls ~/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/bin/
 
configtxgen  configtxlator  cryptogen  discover  fabric-ca-client  idemixgen  orderer  peer

移动位置

mv fabric-samples/ ~/go/src/github.com/hyperledger/

设置path

vi 〜/ .bash_profile

修改最后一行

export PATH=$PATH:$GOROOT/bin:$GOPATH/bin:~/go/src/github.com/hyperledger/fabric-samples/bin

保存文件,执行以下命令

source ~/.bash_profile

4. 启动first-network

cd ~/go/src/github.com/hyperledger/fabric-samples/first-network/
byfn.sh generate -c mychannel
./byfn.sh up -c mychannel -i 1.4.4

5.first-network步骤解读

5.1. 创建配置文件

configtxgen -profile TwoOrgsOrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block

configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel

configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP

configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP

5.2. 创建通道

admin.org1 连接 peer0.org1 节点,向 orderer 节点传递要创建的通道名称 CHANNEL_NAME (默认为 mychannel)和通道配置交易 channel.tx。如果创建成功,则返回通道的创世区块 CHANNEL_NANE.block,该区块包含 channel.tx 指定的通道配置信息,保存在 CLI 容器中

peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls $CORE_PEER_TLS_ENABLED --cafile $ORDERER_CA >&log.txt

5.3.加入通道

先加入通道 再遍历org 再遍历peer

peer channel join -b $CHANNEL_NAME.block >&log.txt

5.4.更新锚节点

peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org1MSPanchors.tx --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

peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org2MSPanchors.tx --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

5.5.安装链码

peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/

5.6.实例化链码

peer chaincode instantiate -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 -l golang -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P 'AND ('\''Org1MSP.peer'\'','\''Org2MSP.peer'\'')'

5.7.查询

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

5.8.调用链码

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 --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["invoke","a","b","10"]}'

重新安装

docker volume prune
发布了14 篇原创文章 · 获赞 0 · 访问量 155

猜你喜欢

转载自blog.csdn.net/kk3909/article/details/104819309
今日推荐