3

环境准备

docker安装

Docker最低版本要求是version 17.06.2-ce
推荐在Docker官网下载Mac版本

go安装

Go最低版本要求是 1.10.x
可在使用brew或者Go官网下载Mac版本

下载示例,二进制和docker镜像

在home目录新创建一个目录,比如

makedir ~/fabric_learning
cd ~/fabric_learning

下载脚本文件https://raw.githubusercontent... 到当前目录下
执行脚本

./bootstrap.sh -s version_number

e.g.

./bootstrap.sh -s 1.2.0

然后你将得到一个 fabric-samples 文件夹
在fabric-samples/bin目录下有如下平台相关的二进制文件

  • configtxgen
  • cryptogen
  • fabric-ca-client
  • idemixgen
  • peer
  • configtxlator
  • discover
  • orderer

并下载了一系列所需的image文件

手动搭建byfn网络

如无特别说明以下的操作都在fabric-samples/first-network目录下

修改docker-compose-cli.yaml文件log级别为DEBUG

cli:
  container_name: cli
  image: hyperledger/fabric-tools:$IMAGE_TAG
  tty: true
  stdin_open: true
  environment:
    - GOPATH=/opt/gopath
    - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
    - CORE_LOGGING_LEVEL=DEBUG
    #- CORE_LOGGING_LEVEL=INFO

生成加密材料 (x509证书和签名密钥)

../bin/cryptogen generate --config=./crypto-config.yaml

命令执行完成后会看到如下输出

org1.example.com
org2.example.com

并在first-network目录下生成crypto-config文件夹,证书和密钥都在此文件夹下

证书是身份的代表,当我们的网络实体交互和交易的时候,用来签名和验证权限
cryptogen 需要配置一个文件- crypto-config.yaml, 此文件包含了网络的拓扑定义,有三个成员,分别是一个Orderer Org和两个Peer Orgs (Org1 & Org2), 每个Peer组织维护两个peer结点为组织和其中的组件生成一系列的证书和密钥。

生成orderer 创世区块 genesis block

export FABRIC_CFG_PATH=$PWD
../bin/configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block

生成channel配置交易

export CHANNEL_NAME=mychannel
../bin/configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME

定义锚节点

为Org1定义锚节点

../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP

为Org2定义锚节点

../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP

启动网络

docker-compose -f docker-compose-cli.yaml up -d

创建和加入channel

Docker exec -it cli bash

命令成功后bash显示如下

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

创建channel

export CHANNEL_NAME=mychannel
peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -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

将peer0.org1.example.com加入创建的channel

peer channel join -b mychannel.block

将peer0.org2.example.com加入创建的channel

CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp CORE_PEER_ADDRESS=peer0.org2.example.com:7051 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 peer channel join -b mychannel.block

更新锚节点

peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -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
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp CORE_PEER_ADDRESS=peer0.org2.example.com:7051 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 peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org2MSPanchors.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

安装 & 实例化chaincode

peer chaincode install -n mycc -v 1.0 -p github.com/chaincode/chaincode_example02/go/
peer chaincode instantiate -o orderer.example.com:7050 --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 -C $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a", "100", "b","200"]}' -P "AND ('Org1MSP.peer','Org2MSP.peer')"

查询

peer chaincode query -C $CHANNEL_NAME -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 $CHANNEL_NAME -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:7051 --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"]}'

AndyZhang
14 声望3 粉丝