区块链技术研究

Hyperledger Fabric v1.0alpha mac

2017-05-09  本文已影响0人  wydnpu

参考

老的文档:

整了半天才发现,上面的都是老版本:目前最新 1.0 alpha 在如下,而且更新贼啦快,下面基本是翻译,不过是macOS上的部署过程。

准备和安装

ln -s /Applications/Docker.app/Contents/Resources/etc/docker.zsh-completion /usr/local/share/zsh-completions/_docker
ln -s /Applications/Docker.app/Contents/Resources/etc/docker-machine.zsh-completion /usr/local/share/zsh-completions/_docker-machine
ln -s /Applications/Docker.app/Contents/Resources/etc/docker-compose.zsh-completion /usr/local/share/zsh-completions/_docker-compose

检查安装:

docker version
docker-compose version
go version

Curl 二进制文件

mkdir fabric-sample
cd fabric-sample
curl -L https://logs.hyperledger.org/sandbox/vex-yul-hyp-jenkins-2/fabric-binaries/release.tar.gz -o release.tar.gz 2> /dev/null;  tar -xvf release.tar.gz 

安装docker镜像

安装docker镜像, 写本文时候还是alpha : hyperledger/fabric-peer:x86_64-1.0.0-alpha

注意:darwin-amd64 是mac的,如果是其他系统,请转到相应目录下。官方文档在下一节讲了这个,对大部分有经验的开发人员应该不用再查一遍自己的系统了: https://hyperledger-fabric.readthedocs.io/en/latest/getting_started.html#using-the-cryptogen-tool

cd release
cd darwin-amd64/install
./get-docker-images.sh

稍等,国内网络等的还蛮久的,我下的时候中间还中断了多次。安装完检查下,一共如下7个image

⇒  docker images
REPOSITORY                     TAG                  IMAGE ID            CREATED             SIZE
hyperledger/fabric-couchdb     x86_64-1.0.0-alpha   f3ce31e25872        7 weeks ago         1.51 GB
hyperledger/fabric-kafka       x86_64-1.0.0-alpha   589dad0b93fc        7 weeks ago         1.3 GB
hyperledger/fabric-zookeeper   x86_64-1.0.0-alpha   9a51f5be29c1        7 weeks ago         1.31 GB
hyperledger/fabric-orderer     x86_64-1.0.0-alpha   5685fd77ab7c        7 weeks ago         182 MB
hyperledger/fabric-peer        x86_64-1.0.0-alpha   784c5d41ac1d        7 weeks ago         184 MB
hyperledger/fabric-javaenv     x86_64-1.0.0-alpha   a08f85d8f0a9        7 weeks ago         1.42 GB
hyperledger/fabric-ccenv       x86_64-1.0.0-alpha   91792014b61f        7 weeks ago         1.29 GB

使用脚本

切换到 release/samples/e2e 目录,脚本在此目录下。

使用脚本非常方便,官方文章中大量步骤其实就是脚本中一步步手动执行;我这里将其顺序换了下。
启动

./network_setup.sh up <channel_name>

停止

./network_setup.sh down

重启

./network_setup.sh restart

后面开始就是手动执行,其实就是对脚本的解释。

使用 cryptogen 工具

切换到 release/samples/e2e 目录, crypto-config.yaml 文件包含了网络的定义和结构、需生成证书的组件列表
执行命令:
../../darwin-amd64/bin/cryptogen generate --config=./crypto-config.yaml

目录下生成 crypto-config 目录,生成的证书就在此目录下。

使用 configtxgen 工具

使用configtxgen 工具生成orderer创世块和通道配置;此工具文档在此

生成orderer创世块

# 配置系统配置
export FABRIC_CFG_PATH=$PWD
../../darwin-amd64/bin/configtxgen -profile TwoOrgs -outputBlock orderer.block

生成 orderer.block

生成 channel 配置

我这里 channel名称设定为 testing

../../darwin-amd64/bin/configtxgen -profile TwoOrgs -outputCreateChannelTx channel.tx -channelID testing

启动网络(No TLS)

将 docker-compose-no-tls.yaml 文件中 cli 配置下的 command 注释掉,不让其自动启动

./scripts/script.sh 就是cli启动网络的脚步,因此将其注释掉

working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer
#command: /bin/bash -c './scripts/script.sh ${CHANNEL_NAME}; '
volumes:

设置网络需要的系统变量

# this sets our OS; 如果你是其他系统,请另外设置;
# export ARCH_TAG=$(uname -m)
export ARCH_TAG=darwin-amd64
# this starts the network in "detached" mode; enter the appropriate value for the CHANNEL_NAME parm; 
# 移除 -d 可以看组件详细日志
CHANNEL_NAME=<CHANNEL_NAME> docker-compose -f docker-compose-no-tls.yaml up -d

创建和加入 channle

启动 cli docker镜像

docker exec -it cli bash

现在正常应该可以进入cli镜像命令提示行:

root@bb5e894d9668:/opt/gopath/src/github.com/hyperledger/fabric/peer#

创建channle

设置如下环境变量:

CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com
CORE_PEER_LOCALMSPID="OrdererMSP"
CHANNEL_NAME=<YOUR_CHANNEL_NAME>

创建channle的语法如下:

peer channel create -o <ORDERER_NAME>:7050 -c <CHANNEL_NAME> -f channel.tx

因此,完整的命令操作如下:

CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com CORE_PEER_LOCALMSPID="OrdererMSP" peer channel create -o orderer.example.com:7050 -c mychannel -f channel.tx

这个命令返回一个创世块 mychannle.block 用于加入channel使用

环境变量

可以在 scripts/script.sh 脚本中看到所有命令的使用

要使下面cli对PEER0的命令生效,必须先设置如下四个环境变量:

# Environment variables for PEER0
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
CORE_PEER_LOCALMSPID="Org0MSP"
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/cacerts/org1.example.com-cert.pem

每个节点的环境变量配置在docker-compose文件中,可以在其中详细看到

加入channle

语法如下:

peer channel join -b <CHANNEL_NAME>.block

因为需要传入4个全局环境变量,因此完整命令如下:

CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com CORE_PEER_ADDRESS=peer0.org1.example.com:7051 CORE_PEER_LOCALMSPID="Org0MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/cacerts/org1.example.com-cert.pem peer channel join -b mychannel.block

安装 chaincode

安装 chaincode源码到peer的文件系统中,语法如下:

peer chaincode install -n <CHAINCODE_NAME> -v <CHAINCODE_VERSION> -p <CHAINCODE_PATH>

完整命令如下:

CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com CORE_PEER_ADDRESS=peer0.org1.example.com:7051 CORE_PEER_LOCALMSPID="Org0MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/cacerts/org1.example.com-cert.pem peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 >&log.txt

实例 instantiate

现在我们开始启动 chaincode 容器,初始化 key-value对。语法如下:

peer chaincode instantiate -o <ORDERER_NAME>:7050 -C <CHANNEL_NAME> -n <CHAINCODE_NAME> -v <VERSION> -c '{"Args":["init","key","value"]}' -P "OR/AND (CHAINCODE_POLICY)"

注意参数 -P, 表明我们的策略需要背书的组织。-P "OR ('Org0MSP.member','Org1MSP.member')" 意思是需要 Org0 OR Org1 的背书。如果改成 AND,表示同时需要两者。

完整命令:

# we instantiate with the following key value pairs: "a","100","b","200"
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com CORE_PEER_ADDRESS=peer0.org1.example.com:7051 CORE_PEER_LOCALMSPID="Org0MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/cacerts/org1.example.com-cert.pem peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n mycc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR ('Org0MSP.member','Org1MSP.member')"

检索 query

检索下值:"a",验证下chaincode初始化成功,状态数据库已经传播出去。query语法如下:

peer chaincode query -C <CHANNEL_NAME> -n <CHAINCODE_NAME> -c '{"Args":["query","key"]}'

完整命令:

CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com CORE_PEER_ADDRESS=peer0.org1.example.com:7051 CORE_PEER_LOCALMSPID="Org0MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/cacerts/org1.example.com-cert.pem peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'

Invoke

我们将 10 从 a 移动到 b,该交易将创建新的块,变更状态数据库。语法如下:

peer chaincode invoke -o <ORDERER_NAME>:7050 -C <CHANNEL_NAME> -n <CHAINCODE_NAME> -c '{"Args":["invoke","key","key","value"]}'

完整命令:

CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com CORE_PEER_ADDRESS=peer0.org1.example.com:7051 CORE_PEER_LOCALMSPID="Org0MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/cacerts/org1.example.com-cert.pem peer chaincode invoke -o orderer.example.com:7050 -C mychannel -n mycc -c '{"Args":["invoke","a","b","10"]}'

验证 Query

验证下执行结果:

CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com CORE_PEER_ADDRESS=peer0.org1.example.com:7051 CORE_PEER_LOCALMSPID="Org0MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/cacerts/org1.example.com-cert.pem peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'

官方文档下面还有使用CouchDB、故障排除方面的内容,有兴趣的自己看下,很好理解的。

上一篇下一篇

猜你喜欢

热点阅读