這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
HyperLedger Fabcar 學習筆記(基於超級賬本編寫第一個應用)
翻譯自:http://hyperledger-fabric.rea...
編寫第一個應用程式
我們需要實現如下三個步驟
- 首先設定一個開發環境
- 學習一些簡單的我們app將會用到的智能合約的參數
- 開發一個app能夠更新查詢一個賬本
設定開發環境
- 首先確保必備的fabric image等已經安裝成功,如果沒有安裝,請參考(https://hyperledger-fabric.re...)
- 下載fabric-samples,並且安裝必備的運行工具。(參考地址:https://hyperledger-fabric.re...)
- 上述步驟如果不想看英文的同學,可以參考(搭建第一個超級賬本網路的方法)https://segmentfault.com/a/11...
- 如果按照建立超級賬本網路的教程運行,可能會啟動著一個網路,會對接下來的流程有影響,需要關閉網路。
./byfn.sh -m down
- 進入到fabric-sample/fabcar目錄下:這是一個關於car(汽車)交易的app
cd fabric-samples/fabcar && ls顯示結果enrollAdmin.js invoke.js package.json query.js registerUser.js startFabric.sh
- 殺死當前啟動並執行所有docker 鏡像服務
docker rm -f $(docker ps -aq)
- 清除所有緩衝網路
docker network prune
- 如果已經運行過此教程,還需要執行如下操作,刪除chaincode鏡像,如果第一次運行是不會有的
docker rmi dev-peer0.org1.example.com-fabcar-1.0-5c906e402ed29f20260ae42283216aa75549c571e2e380f3615826365d8269ba
安裝用戶端並登入網路
- 首先要確保fabric-ca-client和fabric-client是否已經準備好了,這兩個module是Node.js訪問超級賬本網路的SDK
npm install fabric-client
npm install fabric-ca-client
此時會在fabcar目錄下顯示node_module目錄以及一個package.json的檔案。此部分如果不理解可以學習node.js相關知識。
- 通過命令建立一個超級賬本網路
<!--運行golang編寫的chaincode-->
./startFabric.sh
說明:此指令碼會建立建立一個超級賬本網路,包括ca-server peer order等節點,安裝並執行個體化了chaincode。
<!--運行node.js編寫的chaincode-->
./startFabric.sh node
App如何使用網路
建立Admin使用者
- 首先應用的admin使用者應該向ca-server發送一個認證登記請求,接受一個對於這個user的登記認證(eCert),後續我們會根據使用這個admin註冊和認證一個新的user。命令如下:
node enrollAdmin.js
命令執行成功會建立一個hfc-key-store目錄,目錄中存放admin的身份標示,一對公私密鑰。
建立普通User使用者
- 建立一個普通使用者user1,這個使用者用來查詢和更新賬本。admin使用者身份用來建立user1使用者。執行如下命令:
node registerUser.js
查詢賬本資訊
- 執行查詢命令查詢當前汽車資訊
node query.js
[{"Key":"CAR0", "Record":{"colour":"blue","make":"Toyota","model":"Prius","owner":"Tomoko"}},{"Key":"CAR1", "Record":{"colour":"red","make":"Ford","model":"Mustang","owner":"Brad"}},{"Key":"CAR2", "Record":{"colour":"green","make":"Hyundai","model":"Tucson","owner":"Jin Soo"}},{"Key":"CAR3", "Record":{"colour":"yellow","make":"Volkswagen","model":"Passat","owner":"Max"}},{"Key":"CAR4", "Record":{"colour":"black","make":"Tesla","model":"S","owner":"Adriana"}},{"Key":"CAR5", "Record":{"colour":"purple","make":"Peugeot","model":"205","owner":"Michel"}},{"Key":"CAR6", "Record":{"colour":"white","make":"Chery","model":"S22L","owner":"Aarav"}},{"Key":"CAR7", "Record":{"colour":"violet","make":"Fiat","model":"Punto","owner":"Pari"}},{"Key":"CAR8", "Record":{"colour":"indigo","make":"Tata","model":"Nano","owner":"Valeria"}},{"Key":"CAR9", "Record":{"colour":"brown","make":"Holden","model":"Barina","owner":"Shotaro"}}]
- 我們可以嘗試修改query.js後再次運行指令碼
const request = { //targets : --- letting this default to the peers assigned to the channel chaincodeId: 'fabcar', fcn: 'queryCar', //此處修改為查詢單個CAR資訊 args: ['CAR5'] //通過修改參數查詢指定的CAR資訊};顯示結果如下Store path:/Users/ly/go/src/github.com/hyperledger/fabric-samples/fabcar/hfc-key-storeSuccessfully loaded user1 from persistenceQuery has completed, checking resultsResponse is {"colour":"purple","make":"Peugeot","model":"205","owner":"Michel"}
- 我們通過查看fabric-sample/chaincode/fabcar 查看智能合約代碼,我們的app就是調用了智能合約中支援的函數,通過rpc方式。支援函數如下:initLedger用於建立初始化的10個CAR資訊
initLedger, queryCar, queryAllCars, createCar, and changeCarOwner.
- APP執行流程圖
更新賬本資訊
- 更新賬本資訊在APP側和查詢相似,就是通過智能合約提供的介面更新資料。當前提供的功能包括建立CAR,以及修改CAR的owner屬性。我們可以通過修改invoke.js代碼實現對不同介面的調用。
var request = { //targets: let default to the peer assigned to the client chaincodeId: 'fabcar', fcn: 'createCar', args: ['CAR12', 'Honda', 'Accord', 'Black', 'Tom'], chainId: 'mychannel', txId: tx_id };執行結果:lydeiMac:fabcar ly$ node invoke.js Store path:/Users/ly/go/src/github.com/hyperledger/fabric-samples/fabcar/hfc-key-storeSuccessfully loaded user1 from persistenceAssigning transaction_id: a5b684603b1f2a0296851409cecb143c3109220014182721165ef8fe5c326b2eTransaction proposal was goodSuccessfully sent Proposal and received ProposalResponse: Status - 200, message - "OK"The transaction has been committed on peer localhost:7053Send transaction promise and event listener promise have completedSuccessfully sent transaction to the orderer.Successfully committed the change to the ledger by the peer
- 建立後查詢所有CAR資訊,可以看到增加了CAR12資訊
[{"Key":"CAR0", "Record":{"colour":"blue","make":"Toyota","model":"Prius","owner":"Tomoko"}},{"Key":"CAR1", "Record":{"colour":"red","make":"Ford","model":"Mustang","owner":"Brad"}},{"Key":"CAR12", "Record":{"colour":"Black","make":"Honda","model":"Accord","owner":"Tom"}},{"Key":"CAR2", "Record":{"colour":"green","make":"Hyundai","model":"Tucson","owner":"Jin Soo"}},{"Key":"CAR3", "Record":{"colour":"yellow","make":"Volkswagen","model":"Passat","owner":"Max"}},{"Key":"CAR4", "Record":{"colour":"black","make":"Tesla","model":"S","owner":"Adriana"}},{"Key":"CAR5", "Record":{"colour":"purple","make":"Peugeot","model":"205","owner":"Michel"}},{"Key":"CAR6", "Record":{"colour":"white","make":"Chery","model":"S22L","owner":"Aarav"}},{"Key":"CAR7", "Record":{"colour":"violet","make":"Fiat","model":"Punto","owner":"Pari"}},{"Key":"CAR8", "Record":{"colour":"indigo","make":"Tata","model":"Nano","owner":"Valeria"}},{"Key":"CAR9", "Record":{"colour":"brown","make":"Holden","model":"Barina","owner":"Shotaro"}}]
- 合約執行流程
- 先通過用戶端提交交易- 通過背書節點檢查後,用戶端得到背書節點響應- 用戶端將背書節點響應結果提交給order排序節點- order節點建立區塊後,廣播給所有的peer節點更新賬本