https://hyperledger-fabric.readthedocs.io/en/release-1.4/build_network.html
第一步:生成加密工件(x.509 證書及相關(guān)密鑰)
通過 cryptogen,基于 crypto-config.yaml 里定義的內(nèi)容,為每個組織(organization)以及組織里的各個組件生成一組證書和密鑰。每個組織都會有一個根證書/CA 證書(ca-cert)。Fabric 中的私鑰 private key 叫 keystore,公鑰 public key 叫 signcerts。
必須在 fabric-samples/first-network 目錄下,使用下邊的 command
../bin/cryptogen generate --config=./crypto-config.yaml
運(yùn)行完上邊的命令后,生成的加密工件會存儲在 crypto-config 目錄下。
名詞整理
cryptogen:加密工件生成器工具 crypto generator,在 bin 目錄下,用來生成 x.509 證書及相關(guān)的公鑰私鑰
crypto-config.yaml:生成加密工件基于的配置文件,里邊包含了這個網(wǎng)絡(luò)中的所有參與者及組件信息
ca-cert:CA 證書,每個組織都會有自己唯一的 CA (根)證書
keystore:私鑰,用來為所有的交易及通信進(jìn)行數(shù)字簽名
signcerts:公鑰,用來驗(yàn)證簽名
第二步:生成初始化配置工件
通過 configtxgen ,基于 configtx.yaml 里定義的內(nèi)容,生成 order 的創(chuàng)世塊 genesis block、channel 信息以及為每個組織在當(dāng)前 channel 中指定錨節(jié)點(diǎn) anchor peer。
先使用下邊的命令定義一個 FABRIC_CFG_PATH 系統(tǒng)變量
export FABRIC_CFG_PATH=$PWD
生成 order 的創(chuàng)世塊
../bin/configtxgen -profile TwoOrgsOrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block
這步執(zhí)行完成后,會新生成一個 channel-artifacts 的文件夾,里邊有個新生成的 order 的創(chuàng)世塊 genesis.block。
創(chuàng)建 channel 的初始化交易
export CHANNEL_NAME=mychannel && ../bin/configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME
這步執(zhí)行完成后,會在 channel-artifacts 文件夾下生成一個新的 channel 交易的 channel.tx。
為組織指定錨節(jié)點(diǎn)
組織1
../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP
這步執(zhí)行完成后,會在 channel-artifacts 文件夾下生成一個新的組織 1 的錨節(jié)點(diǎn)交易 Org1MSPanchors.tx。
組織2
../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP
這步執(zhí)行完成后,會在 channel-artifacts 文件夾下生成一個新的組織 2 的錨節(jié)點(diǎn)交易 Org2MSPanchors.tx。
啟動網(wǎng)絡(luò)
docker-compose -f docker-compose-cli.yaml up -d
這步執(zhí)行完成后,會創(chuàng)建六個容器:
- Order
- 組織1 的兩個 peers
- 組織2 的兩個 peers
- cli 工具
期間遇到組織1 的兩個 peers 無法啟動,查看容器 log 發(fā)現(xiàn)以下錯誤
[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
創(chuàng)建和加入 channel
首先需要進(jìn)入到 cli 的容器中
docker exec -it cli bash
使用下邊的命令創(chuàng)建一個名為 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
上邊的步驟執(zhí)行完成后,會在 cli 的容器中生成一個 mychannel.block 的創(chuàng)世塊,之后加入 channel 的時候要用到。
將組織 1 的第一個 peer 節(jié)點(diǎn)加入 channel
peer channel join -b mychannel.block
將組織 2 的第一個 peer 節(jié)點(diǎn)加入 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: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
更新錨節(jié)點(diǎn)信息添加到 channel 的定義中
使用下邊的命令更新組織 1 的錨節(jié)點(diǎn)信息
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 的錨節(jié)點(diǎn)信息
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: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 需要在每個要為交易背書的節(jié)點(diǎn)上安裝,并且需要在 channel 里初始化
用下邊的命令為組織 1 的 peer0 節(jié)點(diǎn)安裝 chain code
peer chaincode install -n mycc -v 1.0 -l node -p /opt/gopath/src/github.com/chaincode/chaincode_example02/node/
用下邊的命令為組織 2 的 peer0 節(jié)點(diǎn)安裝 chain code
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: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,參數(shù) -P 是這個 channel 中要執(zhí)行的背書策略
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 節(jié)點(diǎn)進(jìn)行第一次查詢的時候,會為所在的節(jié)點(diǎn)生成一個 chain code 的容器。
peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'