Fabric學習(三)之搭建第一個Fabric網絡詳解

時間:2019.12.26.22.20
本篇文章爲構建第一個Fabric網絡的詳解
參考來源:官方地址

密鑰生成器

使用 cryptogen 工具爲我們的網絡實體生成各種加密材料( x509 證書和簽名祕鑰)。這些證書是身份的代表,在實體之間通信和交易的時候,它們允許對身份驗證進行簽名和驗證

如何工作

Cryptogen 通過一個包含網絡拓撲的文件 crypto-config.yaml,爲所有組織和屬於這些組織的組件生成一組證書和祕鑰。每一個組織被分配一個唯一的根證書(ca-cert),它綁定該組織的特定組件(Peer 節點和排序節點)。通過爲每個組織分配一個唯一的 CA 證書,我們模擬了一個典型的網絡,網絡中的成員可以使用它自己的證書授權中心。Fabric 中的事務和通信由一個實體的私鑰(keystore)簽名,然後通過公鑰(signcerts)驗證。

crypto-config.yaml 文件gtihub地址

通過文件中 count 變量指定每個組織的 Peer 節點數量。案例裏每個組織有兩個 Peer 節點
運行 cryptogen 工具之後,生成的證書和密鑰將保存到一個名爲 crypto-config 的文件夾中。注意, crypto-config.yaml 文件在排序組織中設置了五個排序節點。cryptogen 會爲這五個排序節點生成證書、創建系統通道和 mychannel。使用 Raft 或者 Kafka 排序服務會用五個節點進行排序,而使用Solo 排序服務只會使用一個排序節點

配置交易生成器

configtxgen 工具用來創建四個配置構件:
1、排序節點的創世區塊
2、通道配置交易
3、兩個錨節點交易,分貝對應一個Peer組織

排序區塊是排序服務的創世區塊,通道配置交易在通道創建的時候廣播給排序服務。錨節點交易,指定了每個組織在此通道上的錨節點

如何工作

Configtxgen 使用一個文件——configtx.yaml,這個文件包含了一個示例網絡的定義。它擁有三個成員:一個排序組織(OrdererOrg)和兩個 Peer 組織(Org1 & Org2),這兩個 Peer 組織每個都管理和維護兩個 Peer 節點。這個文件還定義了一個聯盟——SampleConsortium,包含了我們的兩個 Peer 組織

參數 作用
TwoOrgsOrdererGenesis 以Solo 排序服務生成創世區塊
SampleMultiNodeEtcdRaft 以Raft 排序服務生成創世區塊。只有將 -o 參數指定爲 etcdraft 是纔可用
SampleDevModeKafka 以Kafka 排序服務生成創世區塊。只有將 -o 參數指定爲 kafka 是纔可用。
TwoOrgsChannel 爲通道 mychannel 生成創世區塊

configtx.yaml 文件的github地址

該文件還包含兩個值得注意的附加規範。
第一,爲每個組織指定了錨節點(peer0.org1.example.com & peer0.org2.example.com)。
第二,爲每個成員指定 MSP 文件位置,進而我們可以在排序節點的創世區塊中存儲每個組織的根證書。這是一個關鍵概念。現在每個和排序服務通信的網絡實體都可以驗證它們的數字簽名。

運行工具

configtxgen 和 cryptogen 命令來手動生成證書/密鑰和各種配置。或者,你可以嘗試使用 byfn.sh 腳本來完成你的目標

手動生成構件

你可以參考 byfn.sn 腳本中的 generateCerts 函數,這個函數包含了生成 crypto-config.yaml 中所定義的證書的必要命令,這些證書將被作爲你的網絡配置。然而,爲了方便起見,我們在這裏也提供一個參考。
進入 fabric-samples 文件運行 cryptogen 工具。
執行以下命令會下載必要的二進制文件,在當前目錄生成 bin 文件夾存放下載的二進制文件。

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

終端輸出

org1.example.com
org2.example.com

證書和祕鑰(例如 MSP 材料)將會保存在 first-network 目錄的 crypto-config 文件夾中。
接下來,我們需要告訴 configtxgen 工具去哪兒尋找它需要的 configtx.yaml 文件。告訴它在當前工作目錄:

$ export FABRIC_CFG_PATH=$PWD

調用 configtxgen 工具創建排序節點的創世區塊(默認爲Solo 排序服務–>TwoOrgsOrdererGenesis )

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

調用 configtxgen 工具創建 Raft 排序服務的創世區塊

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

調用 configtxgen 工具創建Kafka 排序服務的創世區塊

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

其中採用 solo 排序模式的類似輸出

2017-10-26 19:21:56.301 EDT [common/tools/configtxgen] main -> INFO 001 Loading configuration
2017-10-26 19:21:56.309 EDT [common/tools/configtxgen] doOutputBlock -> INFO 002 Generating genesis block
2017-10-26 19:21:56.309 EDT [common/tools/configtxgen] doOutputBlock -> INFO 003 Writing genesis block

排序通道創世區塊和其他生成的構件都保存在當前項目根目錄中的 channel-artifacts 文件夾。上邊命令中的 channelID 是系統通道的名字

創建通道配置交易

接下來,我們需要去創建通道的交易構件。請確保替換 $CHANNEL_NAME 或者 將 CHANNEL_NAME 設置爲整個說明中可以使用的環境變量

# The channel.tx artifact contains the definitions for our sample channel
$ export CHANNEL_NAME=mychannel  
$ ../bin/configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME

注意,如果你使用 Raft 或者 Kafka 排序服務,你也不需要爲通道指定特殊命令。TwoOrgsChannel 選項會使用你指定的排序服務配置爲網絡創建創世區塊。
終端類似輸出

2017-10-26 19:24:05.324 EDT [common/tools/configtxgen] main -> INFO 001 Loading configuration
2017-10-26 19:24:05.329 EDT [common/tools/configtxgen] doOutputChannelCreateTx -> INFO 002 Generating new channel configtx
2017-10-26 19:24:05.329 EDT [common/tools/configtxgen] doOutputChannelCreateTx -> INFO 003 Writing new channel tx

爲通道上的組織 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 文件關聯了我們之前下載的鏡像,然後通過我們之前生成的創世區塊 genesis.block 引導排序節點
啓動網絡

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

如果你想要實時查看你的網絡日誌,請不要加 -d 標識。如果你想要查看日誌流,你需要打開第二個終端來執行 CLI 調用

創建和加入通道

回想一下,我們在 創建通道配置交易章節中使用 configtxgen 工具創建通道配置交易。你可以使用相同的方式創建額外的通道配置交易,使用 configtx.yaml 中相同或者不同的選項傳給 configtxgen 工具。然後你可以重複在本章節中的過程在你的網絡中創建其他通道。

進入容器

$ docker exec -it cli bash
// 類似輸出
root@0d78bb69300d:/opt/gopath/src/github.com/hyperledger/fabric/peer#

要想在容器中運行 peer 命令,必須先設置環境變量,調用每個節點時都必須重新設置

# Environment variables for PEER0
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/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

把在創建通道配置交易章節中創建的通道配置交易配置channel.tx作爲創建通道請求的一部分傳遞給排序節點。
使用 -c 標誌指定通道的名稱,-f 標誌指定通道配置交易,在這個例子中它是 channel.tx,當然你也可以使用不同的名稱掛載你自己的交易配置。我們將再次在 CLI 容器中設置 CHANNEL_NAME 環境變量,這樣我們就不用顯式的傳遞這個參數。通道的名稱必須全部是消息字母,小於 250 個字符,並且匹配正則表達式 [a-z][a-z0-9.-]*

export CHANNEL_NAME=mychannel

# the channel.tx file is mounted in the channel-artifacts directory within your CLI container
# as a result, we pass the full path for the file
# we also pass the path for the orderer ca-cert in order to verify the TLS handshake
# be sure to export or replace the $CHANNEL_NAME variable appropriately

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

注意 --cafile 會作爲命令的一部分。這是排序節點的根證書的本地路徑,允許我們去驗證 TLS 握手
peer0.org1.example.com 加入通道。

# By default, this joins ``peer0.org1.example.com`` only
# the <CHANNEL_NAME.block> was returned by the previous command
# if you have not modified the channel name, you will join with mychannel.block
# if you have created a different channel name, then pass in the appropriately named block

 peer channel join -b mychannel.block

修改環境變量,將四個節點都加入通道

peer1.org1.example.com 加入通道

CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp 
CORE_PEER_ADDRESS=peer1.org1.example.com:8051 
CORE_PEER_LOCALMSPID="Org1MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer1.org1.example.com/tls/ca.crt 
peer channel join -b mychannel.block

peer0.org2.example.com 加入通道

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

peer1.org2.example.com 加入通道

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=peer1.org2.example.com:10051 
CORE_PEER_LOCALMSPID="Org2MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer1.org2.example.com/tls/ca.crt 
peer channel join -b mychannel.block

更新錨節點

更新通道定義,將 Org1 的錨節點定義爲 peer0.org1.example.com 。

CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
CORE_PEER_ADDRESS=peer0.org1.example.com:7051  // 說明現在節點端口分別爲 7050/8050/9050/10050
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

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

將 Org2 的錨節點定義爲 peer0.org2.example.com (與執行 Org2 節點的 peer channel join 命令相同,我們需要爲這個命令配置合適的環境變量。)

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

安裝和實例化鏈碼

只示範java的(安裝之前先設置環境變量-這裏不再一一設置,默認已設置好對應的環境變量)

# make note of the -l flag to indicate "java" chaincode
# for java chaincode -p takes the absolute path to the java chaincode
peer chaincode install -n mycc -v 1.0 -l java -p /opt/gopath/src/github.com/chaincode/chaincode_example02/java/

當我們在通道上實例化鏈碼之後,背書策略被設定爲需要 Org1Org2 的節點都背書。所以,我們需要在 Org2 的節點上也安裝鏈碼(最好是四個節點都安上好的鏈碼之後在實例化)

接下來,在通道上實例化鏈碼。這會在通道上初始化鏈碼,爲鏈碼指定背書策略,然後爲目標節點啓動鏈碼容器。注意 -P 這個參數。這是我們的策略,我們在此策略中指定針對要驗證的此鏈碼的交易所需的背書級別。

在下面的命令裏你將會注意到我們指定 -P "AND ('Org1MSP.peer','Org2MSP.peer')" 作爲策略。這表明我們需要屬於 Org1 和 Org2 的節點“背書” (就是說要兩個背書)。如果我們把語法改成 OR ,那我們將只需要一個背書。

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

查詢

# be sure to set the -C and -n flags appropriately

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

//輸出爲100

調用

調用之後再用上面語句查詢,顯示結果爲90

# be sure to set the -C and -n flags appropriately

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: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"]}'

關於第一個網絡的詳解到此結束

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章