BYFN 笔记

https://hyperledger-fabric.readthedocs.io/en/release-1.4/build_network.html

第一步:生成加密工件(x.509 证书及相关密钥)

通过 cryptogen,基于 crypto-config.yaml 里定义的内容,为每个组织(organization)以及组织里的各个组件生成一组证书和密钥。每个组织都会有一个根证书/CA 证书(ca-cert)。Fabric 中的私钥 private key 叫 keystore,公钥 public key 叫 signcerts

必须在 fabric-samples/first-network 目录下,使用下边的 command

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

运行完上边的命令后,生成的加密工件会存储在 crypto-config 目录下。

名词整理
cryptogen:加密工件生成器工具 crypto generator,在 bin 目录下,用来生成 x.509 证书及相关的公钥私钥
crypto-config.yaml:生成加密工件基于的配置文件,里边包含了这个网络中的所有参与者及组件信息
ca-cert:CA 证书,每个组织都会有自己唯一的 CA (根)证书
keystore:私钥,用来为所有的交易及通信进行数字签名
signcerts:公钥,用来验证签名

第二步:生成初始化配置工件

通过 configtxgen ,基于 configtx.yaml 里定义的内容,生成 order 的创世块 genesis block、channel 信息以及为每个组织在当前 channel 中指定锚节点 anchor peer

先使用下边的命令定义一个 FABRIC_CFG_PATH 系统变量

export FABRIC_CFG_PATH=$PWD

生成 order 的创世块

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

这步执行完成后,会新生成一个 channel-artifacts 的文件夹,里边有个新生成的 order 的创世块 genesis.block

创建 channel 的初始化交易

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

这步执行完成后,会在 channel-artifacts 文件夹下生成一个新的 channel 交易的 channel.tx

为组织指定锚节点

组织1

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

这步执行完成后,会在 channel-artifacts 文件夹下生成一个新的组织 1 的锚节点交易 Org1MSPanchors.tx

组织2

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

这步执行完成后,会在 channel-artifacts 文件夹下生成一个新的组织 2 的锚节点交易 Org2MSPanchors.tx

启动网络

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

这步执行完成后,会创建六个容器:

  • Order
  • 组织1 的两个 peers
  • 组织2 的两个 peers
  • cli 工具
    期间遇到组织1 的两个 peers 无法启动,查看容器 log 发现以下错误
[signal SIGSEGV: segmentation violation code=0x1 addr=0x63 pc=0x7f5aa4117259]

通过在 /base/docker-compose-base.yaml 中,给每个 peer 的 environment 中添加一个 - GODEBUG=netdns=go,问题解决。参考了 https://stackoverflow.com/questions/49648529/hyperledger-fabric-simples-issue-run-byfn-sh-m-up-failed/49649678#49649678

创建和加入 channel

首先需要进入到 cli 的容器中

docker exec -it cli bash

使用下边的命令创建一个名为 mychannel 的 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

上边的步骤执行完成后,会在 cli 的容器中生成一个 mychannel.block 的创世块,之后加入 channel 的时候要用到。

将组织 1 的第一个 peer 节点加入 channel

peer channel join -b mychannel.block

将组织 2 的第一个 peer 节点加入 channel

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 peer channel join -b mychannel.block

更新锚节点信息添加到 channel 的定义中

使用下边的命令更新组织 1 的锚节点信息

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

使用下边的命令更新组织 2 的锚节点信息

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 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

安装和初始化 chain code

chain code 需要在每个要为交易背书的节点上安装,并且需要在 channel 里初始化
用下边的命令为组织 1 的 peer0 节点安装 chain code

peer chaincode install -n mycc -v 1.0 -l node -p /opt/gopath/src/github.com/chaincode/chaincode_example02/node/

用下边的命令为组织 2 的 peer0 节点安装 chain code

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
peer chaincode install -n mycc -v 1.0 -l node -p /opt/gopath/src/github.com/chaincode/chaincode_example02/node/

在 channel 中初始化 chain code,参数 -P 是这个 channel 中要执行的背书策略

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

通过下边的查询语句对 peer 节点进行第一次查询的时候,会为所在的节点生成一个 chain code 的容器。

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

你可能感兴趣的:(BYFN 笔记)