Hyperledger Fabric2.0.0 安装

Hyperledger Fabric2.0.0安装

基础环境准备

Linux内核版本

Docker要求的linux内核版本最低为3.10,查看Linux内核版本

cat /proc/version

安装jdk(1.8)

  1. 官网下载jdk-8u141-linux-x64.tar.gz安装包,上传至目录/opt

  2. 进入/opt目录并解压安装包:

cd /opt
tar -zxvf jdk-8u141-linux-x64.tar.gz
  1. 配置环境变量:
export JAVA_HOME=/opt/jdk1.8.0_141
export CLASSPATH=.:${JAVA_HOME}/jre/lib/rt.jar:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar
export PATH=$PATH:${JAVA_HOME}/bin
  1. 初始化profile文件
source /etc/profile
  1. 查看java版本
[root@mid1 test-network]# java -version
java version "1.8.0_141"
Java(TM) SE Runtime Environment (build 1.8.0_141-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, mixed mode)

安装Docker

  1. 卸载原有docker
yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-selinux docker-engine-selinux docker-engine
  1. 使用存储库安装
yum install -y yum-utils
yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

安装docker引擎

yum install docker-ce docker-ce-cli containerd.io

查看docker 版本

docker --version
  1. 安装docker-compose
#下载docker-compose安装包
curl -L "https://github.com/docker/compose/releases/download/1.26.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose

#设置软连接
ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
  1. 启动docker,并设置docker开机自启
systemctl start docker
chkconfig docker on

安装Go语言环境

  1. 下载go安装包go1.14.3.linux-amd64.tar.gz

  2. 解压至/usr/local/go/

  3. 配置环境变量

#go
export PATH=$PATH:/usr/local/go/bin
export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$PATH:$HOME/go/bin
  1. 初始化profile文件
source /etc/profile
  1. 查看go版本
go version

安装nodejs

官网下载安装包

https://nodejs.org/en/download/

如node-v14.6.0-linux-x64.tar.xz,解压安装包

tar -xvf node-v14.6.0-linux-x64.tar.xz

设置软连接

mv node-v14.6.0-linux-x64 node-v14.6.0
cp -r node-v14.6.0 /usr/local/
ln -s /usr/local/node-v14.6.0/bin/node /usr/local/bin/node
ln -s /usr/local/node-v14.6.0/bin/npm /usr/local/bin/npm

fabric 2.0.0源码安装

  1. 创建放置fabric的文件夹
mkdir -p $GOPATH/src/github.com/hyperledger
  1. 下载fabric源码
cd $GOPATH/src/github.com/hyperledger
git clone https://github.com/hyperledger/fabric.git
cd fabric/scripts
  1. 查看bootstrap.sh
# if version not passed in, default to latest released version
VERSION=2.0.0
# if ca version not passed in, default to latest released version
CA_VERSION=1.4.4

能看到version为2.0.0,ca_version为1.4.4,thirdparty_image_version为0.4.18

执行bootstrap.sh下载镜像和二进制文件:

./bootstrap.sh

执行完后,在scripts目录下会多出来一个fabric-samples目录

备注:

公司网下载经常失败,可以先下载然后解压到相应目录。

下载两个文件hyperledger-fabric-linux-amd64-2.0.0.tar.gz,hyperledger-fabric-ca-linux-amd64-1.4.7.tar.gz,具体下载地址可在每次执行bootstrap.sh文件时看到。

注释掉bootstrap.sh的两行代码

download "${BINARY_FILE}" "https://github.com/hyperledger/fabric/releases/download/v${VERSION}/${BINARY_FILE}"
download "${CA_BINARY_FILE}" "https://github.com/hyperledger/fabric-ca/releases/download/v${CA_VERSION}/${CA_BINARY_FILE}"

或者将bootstrap.sh内的函数pullBinaries注释掉。

这样就不会下载文件了。

解压hyperledger-fabric-linux-amd64-2.0.0.tar.gz,生成两个目录bin、config,拷贝到fabric-samples文件夹中,解压hyperledger-fabric-ca-linux-amd64-1.4.7.tar.gz,将解压后的fabric-ca-client、fabric-ca-server拷贝到fabric-samples/bin目录下,然后继续执行bootstrap.sh下载镜像。OVER!!!

  1. 配置环境变量/etc/profile
export PATH=$PATH:$GOPATH/src/github.com/hyperledger/fabric/scripts/fabric-samples/bin

赋读写权限

chmod -R 777 $GOPATH/src/github.com/hyperledger/fabric/scripts/fabric-samples/bin
  1. 启动test-network测试网络
cd $GOPATH/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network
./network.sh up

启动后会生成三个节点,一个orderer节点和两个peer节点,以容器的形式体现

docker ps | grep fabric
60f523ed898e        hyperledger/fabric-peer:latest           "peer node start"        2 hours ago         Up 2 hours             0.0.0.0:7051->7051/tcp                                             peer0.org1.example.com
e8cc1f1adb07        hyperledger/fabric-peer:latest           "peer node start"        2 hours ago         Up 2 hours             7051/tcp, 0.0.0.0:9051->9051/tcp                                   peer0.org2.example.com
134cc16701f8        hyperledger/fabric-orderer:latest        "orderer"                2 hours ago         Up 2 hours             0.0.0.0:7050->7050/tcp

fabric测试网络test-network部署成功,停止网络使用

./network.sh down

部署fabric网络

生成网络构件

#停止以运行的fabric容器
cd fabric-samples/first-network   #进入fabric-samples/first-network目录
./byfn.sh down   #若已经进行过部署,则销毁已有网络;若第一次部署,则忽略该步骤
docker ps -a | grep fabric #查看容器是否被删除
#生成网络构件
./byfn.sh generate #主要为网络实体(Org1和Org2)证书和秘钥,生成Orderer Genesis block(创世区块)
#启动网络
./byfn.sh up -a -n -s couchdb   #参数-a启动证书颁发机构,-n不部署abstore链码(脚本默认部署该链码),-s使用couchdb数据库而不是goleveldb ,关于该脚本的详细使用帮助可通过./byfn.sh -h查看,停用网络可以运行./byfn.sh down

生成联盟通道配置组件

#使用cryptogen工具生成证书及密钥,放置于crypto-config目录下
# cryptogen generate --config=./crypto-config.yaml   #byfn.sh脚本中的generateCerts()函数调用cryptogen工具后,还调用了同一目录下的ccp-generate.sh脚本为Org1和Org2生成CCP文件(供后期SDK使用)
#使用configtxgen工具生成4个配置组件(genesis.block,channel.tx,Org1MSPanchors.tx,Org2MSPanchors.tx),放置于 channel-artifacts目录下
# export FABRIC_CFG_PATH=$PWD   #告诉configtxgen从哪里寻找configtx.yaml文件
# configtxgen -profile SampleMultiNodeEtcdRaft -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block   #生成系统通道创世区块genesis.block
# export CHANNEL_NAME=mychannel   #通过环境变量指定通道名称
# configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME   #创建 通道配置 事务channel.tx
# configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP   #创建 更新组织Org1在该通道上的锚节点 的事务Org1MSPanchors.tx
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP   #创建 更新组织Org2在该通道上的锚节点 的事务Org2MSPanchors.tx

启动网络

#创建和加入通道
export BYFN_CA1_PRIVATE_KEY=$(cd crypto-config/peerOrganizations/org1.example.com/ca && ls *_sk)   #以环境变量的形式指定每个组织的CA的私钥,CA节点的yaml配置文件中会使用该变量
export BYFN_CA2_PRIVATE_KEY=$(cd crypto-config/peerOrganizations/org2.example.com/ca && ls *_sk)
# docker-compose -f docker-compose-cli.yaml -f docker-compose-etcdraft2.yaml -f docker-compose-ca.yaml -f docker-compose-couch.yaml up -d   #参数-d用于不显示实时日志,若想查看日志流,后续命令则需要打开另一个终端来执行
# docker ps   #查看已启动容器的基本信息,包括一个cli,四个peer,四个couchdb,五个order,两个ca
# docker network inspect net_byfn   #可查看网络信息
#进入CLI容器加入peer通道
#配置环境变量,以使CLI容器作为peer0.org1.example.com节点执行命令,后续很多操作需要不断地切换这组环境变量,以作为不同的peer节点执行相关操作,节点peer0.org1.example.com的环境变量如下
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp

CORE_PEER_ADDRESS=peer0.org1.example.com:7051

CORE_PEER_LOCALMSPID="Org1MSP"

CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt

#将通道配置事务发送给order节点以创建通道
# export CHANNEL_NAME=mychannel

# peer channel create -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/channel.tx --tls --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 join -b mychannel.block

#切换peer节点的环境变量,多次执行以下命令,为每个组织(Org1和Org2)指定其在当前通道中的锚节点,每个组织至少有一个锚节点
# peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org1MSPanchors.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

安装和实例化链码

使用golang链码,生成链码安装包

# peer lifecycle chaincode package mycc.tar.gz --path github.com/hyperledger/fabric-samples/chaincode/abstore/go/ --lang golang --label mycc_1

安装链码

# peer lifecycle chaincode install mycc.tar.gz

查看已安装的链码

# peer lifecycle chaincode queryinstalled

批准Org1链码,并提交到通道

# peer lifecycle chaincode approveformyorg --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 --channelID mychannel --name mycc --version 1 --init-required --package-id mycc_1:f5cc6d6e871262e8da9788f3d463442e51c482ec8288c13e4545741ad45d86fa --sequence 2 --waitForEvent

查询已批准链码

# peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name mycc --version 1 --sequence 2 --output json --init-required

重新设置Org2的环境变量

CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer0.org2.example.com:9051
CORE_PEER_LOCALMSPID="Org2MSP"
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

批准Org2链码

# peer lifecycle chaincode approveformyorg --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 --channelID mychannel --name mycc --version 1 --init-required --package-id mycc_1:f5cc6d6e871262e8da9788f3d463442e51c482ec8288c13e4545741ad45d86fa --sequence 2 --waitForEvent

提交链码到通道

# peer lifecycle chaincode commit -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 --channelID mychannel --name 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 --version 1 --sequence 2 --init-required

查询链码定义

# peer lifecycle chaincode querycommitted --channelID mychannel --name mycc

调用链码接口测试Fabric

调用链码的查询(query)函数

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

调用链码初始化(invoke)函数

# 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":["Init","a","100","b","100"]}'

调用链码的提交(invoke)函数,如a向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 --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"]}' --waitForEvent

删除a或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 --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":["delete", "b"]}'

你可能感兴趣的:(区块链,区块链)