翻譯自:http://hyperledger-fabric.rea...html
咱們須要實現以下三個步驟node
./byfn.sh -m down
git
進入到fabric-sample/fabcar目錄下:這是一個關於car(汽車)交易的appgithub
cd fabric-samples/fabcar && ls 顯示結果 enrollAdmin.js invoke.js package.json query.js registerUser.js startFabric.sh
docker rm -f $(docker ps -aq)
golang
docker network prune
docker rmi dev-peer0.org1.example.com-fabcar-1.0-5c906e402ed29f20260ae42283216aa75549c571e2e380f3615826365d8269ba
首先要確保fabric-ca-client和fabric-client是否已經準備好了,這兩個module是Node.js訪問超級帳本網絡的SDKdocker
npm install fabric-client
npm install fabric-ca-client
npm
./startFabric.sh
#運行golang編寫的chaincodejson
./startFabric.sh node
#運行node.js編寫的chaincodenode enrollAdmin.js
segmentfault
命令執行成功會建立一個hfc-key-store目錄,目錄中存放admin的身份標示,一對公私密鑰。promise
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-store Successfully loaded user1 from persistence Query has completed, checking results Response 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側和查詢類似,就是經過智能合約提供的接口更新數據。當前提供的功能包括建立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-store Successfully loaded user1 from persistence Assigning transaction_id: a5b684603b1f2a0296851409cecb143c3109220014182721165ef8fe5c326b2e Transaction proposal was good Successfully sent Proposal and received ProposalResponse: Status - 200, message - "OK" The transaction has been committed on peer localhost:7053 Send transaction promise and event listener promise have completed Successfully 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節點更新帳本