Fabric 学习笔记-架构初探
本文介紹了Fabric的架構,以及通過一個簡單的Demo來熟悉整個交易流程。
Hyperledger fabric V1.0的架構
如下圖所示:
application提供各種語言的SDK接口。
membership也就是fabric-ca提供成員服務,用來管理身份,提供授權和認證。
peer負責模擬交易和記賬
- Endorser(背書)用來當peer執行一個交易以后返回yes/no。
- Committer將驗證過的區塊追加到通道上各個賬本的副本。
- Ledger就是賬本啦。
- Chaincode用來編寫業務邏輯,交易指令用來修改資產,可以理解為 fabric 網絡對外提供的一個交互接口(智能合約)。
- Event是fabric提供的一個事件框架,比如鏈代碼事件,拒絕事件,注冊事件等,在編寫代碼的時候可以訂閱這些事件來實現自己的業務邏輯。
o-service用來實現共識。
交易流程
交易過程如下圖所示:
- Application向一個或多個peer節點發送對交易的背書請求。
- Peer的endorse執行背書,但并不將結果提交到本地賬本,只是將結果返回給應用。
- 應用收集所有背書節點的結果后,將結果廣播給orderers,orderers執行共識,生成block,通過消息通道批量的將block發布給peer節點,更新lerdger。
可以看一下下面這張圖,一樣的過程,更加突出了多個peer背書的過程。
在介紹一下在交易過程中扮演重要角色的channel
channel是構建在Fabric網絡上的私有區塊鏈,實現了數據的隔離和保密。channel是由特定的peer所共享的,并且交易方必須通過該通道的正確驗證才能與賬本進行交互。
如下圖所示:
可以看到不同顏色的channel隔離了不同的peer之間的通信。
構建Fabric網絡
Fabric提供了一個first-network的demo來學習整個流程。
first-network有兩個組織,每個組織各有兩個個peer節點,以及一個排序服務。兩個peer節點無法達成共識,三個peer節點無法容錯,四個peer節點剛好完成演示。
Demo啟動以后會自動完成一筆轉賬,并且打印出整個操作過程。
啟動網絡以后可以看到非常多的日志,這里我們只追蹤關鍵步驟。
... ########################################################## ######### Generating Orderer Genesis block ############## ########################################################## 2018-02-05 15:13:08.760 CST [common/configtx/tool] main -> INFO 001 Loading configuration 2018-02-05 15:13:08.816 CST [common/configtx/tool] doOutputBlock -> INFO 002 Generating genesis block 2018-02-05 15:13:08.819 CST [common/configtx/tool] doOutputBlock -> INFO 003 Writing genesis block################################################################# ### Generating channel configuration transaction 'channel.tx' ### ################################################################# 2018-02-05 15:13:08.845 CST [common/configtx/tool] main -> INFO 001 Loading configuration 2018-02-05 15:13:08.849 CST [common/configtx/tool] doOutputChannelCreateTx -> INFO 002 Generating new channel configtx 2018-02-05 15:13:08.850 CST [common/configtx/tool] doOutputChannelCreateTx -> INFO 003 Writing new channel tx################################################################# ####### Generating anchor peer update for Org1MSP ########## ################################################################# 2018-02-05 15:13:08.876 CST [common/configtx/tool] main -> INFO 001 Loading configuration 2018-02-05 15:13:08.880 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update 2018-02-05 15:13:08.881 CST [common/configtx/tool] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update ...可以看到,在轉賬之前首先進行了fabric網絡的初始化過程,創建了創世區塊,配置了channel和生成了membership(MSP)身份服務。
Channel name : mychannel Creating channel... 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 CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key CORE_PEER_LOCALMSPID=Org1MSP CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt CORE_PEER_TLS_ENABLED=true CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp CORE_PEER_ID=cli CORE_LOGGING_LEVEL=DEBUG CORE_PEER_ADDRESS=peer0.org1.example.com:7051 2018-02-05 07:13:13.382 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP 2018-02-05 07:13:13.382 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity 2018-02-05 07:13:13.406 UTC [channelCmd] InitCmdFactory -> INFO 003 Endorser and orderer connections initialized 2018-02-05 07:13:13.409 UTC [msp] GetLocalMSP -> DEBU 004 Returning existing local MSP dd接著可以看到在轉賬前首先創建了channel,其中包括了身份認證的ca文件,key以及通信的socket信息和MSP的信息。
Attempting to Query PEER0 ...3 secs2018-02-05 07:13:55.998 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP 2018-02-05 07:13:55.998 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity 2018-02-05 07:13:55.998 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc 2018-02-05 07:13:55.998 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc 2018-02-05 07:13:55.999 UTC [msp/identity] Sign -> DEBU 005 Sign: plaintext: 0A95070A6708031A0C08B388E0D30510...6D7963631A0A0A0571756572790A0161 2018-02-05 07:13:55.999 UTC [msp/identity] Sign -> DEBU 006 Sign: digest: D6BB27BAC40E5A58ED3CF0AFB147280B60E305053D9B97A38461A398736ED7C7 Query Result: 100接著在很不起眼的地方看到了上面這些日志,差點看瞎我~重點來了~
在轉賬之前使用默認的ESCC(背書鏈碼),VSCC(驗證鏈碼),以及自身的簽名查詢了Peer0得到余額是100。
2018-02-05 07:14:14.950 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> DEBU 009 ESCC invoke result: version:1 response:<status:200 message:"OK" > payload:"\n \370\316\5...,k\363_(\n\212\027" > 2018-02-05 07:14:14.952 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 00a Chaincode invoke successful. result: status:200 2018-02-05 07:14:14.953 UTC [main] main -> INFO 00b Exiting..... ===================== Invoke transaction on PEER0 on channel 'mychannel' is successful =====================調用鏈碼進行轉賬操作,發現兩邊的狀態碼都是200表示轉賬成功,其中payload是發送轉賬的一些具體信息。
Attempting to Query PEER3 ...3 secs2018-02-05 07:14:18.566 UTC [msp] GetLocalMSP -> DEBU 001 Returning existing local MSP 2018-02-05 07:14:18.566 UTC [msp] GetDefaultSigningIdentity -> DEBU 002 Obtaining default signing identity 2018-02-05 07:14:18.566 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 003 Using default escc 2018-02-05 07:14:18.566 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 004 Using default vscc 2018-02-05 07:14:18.567 UTC [msp/identity] Sign -> DEBU 005 Sign: plaintext: 0A95070A6708031A0C08CA88E0D30510...6D7963631A0A0A0571756572790A0161 2018-02-05 07:14:18.567 UTC [msp/identity] Sign -> DEBU 006 Sign: digest: 9E93A6D0549C627788C5074633DE28C9D08F36A4763EFF55479613B2DD32CADB Query Result: 90 2018-02-05 07:14:37.014 UTC [main] main -> INFO 007 Exiting.....最后查詢Peer3發現余額只有90了,這樣就完成了一個最小區塊鏈網絡的交易示例。
https://zhuanlan.zhihu.com/p/33597118
總結
以上是生活随笔為你收集整理的Fabric 学习笔记-架构初探的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Fabric学习笔记-智能合约
- 下一篇: Algorand协议详解