6、編寫第一個應用【外部nodejs調用】

1、html

參考地址:https://hyperledger-fabric.readthedocs.io/en/latest/write_first_app.htmlnode

根據前幾節的配置c++

一、下載代碼git

git clone https://github.com/hyperledger/fabric-samples.git
cd fabric-samples/fabcar

二、在fabcar下會有以下文件github

chaincode    invoke.js       network         package.json    query.js        startFabric.sh

新版本的示例,chaincode在根目錄下docker

三、開啓網絡配置[注意docker版本17.06,在1.12.6版本沒有-e命令]npm

./startFabric.sh

四、查詢一個帳本json

 

安裝nodevim

yum install gcc-c++
npm install

查詢[注意配置query.js中的ip地址]網絡

node query.js

展現以下數據

複製代碼
Create a client and set the wallet location
Set wallet path, and associate user  PeerAdmin  with application
Check user is enrolled, and set a query URL in the network
Make query
Assigning transaction_id:  f2f45cb045d6290d199e1b2d4eb3b60b1e9cafeff8d09e2b7683dd8578492be7
returned from query
Query result count =  1
Response is  [{"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

1》初始化參數,包含了用戶ID,信道,鏈碼,網絡鏈接入口

複製代碼
var options = {
    wallet_path: path.join(__dirname, './creds'),
    user_id: 'PeerAdmin',
    channel_id: 'mychannel',
    chaincode_id: 'fabcar',
    network_url: 'grpc://localhost:7051',
};
複製代碼

2》查詢代碼

複製代碼
    var transaction_id = client.newTransactionID();

    // queryCar - requires 1 argument, ex: args: ['CAR4'],
    // queryAllCars - requires no arguments , ex: args: [''],
    const request = {
        chaincodeId: options.chaincode_id,
        txId: transaction_id,
        fcn: 'queryAllCars',
        args: ['']
    };
    return channel.queryByChaincode(request);
複製代碼

這裏設置了鏈碼ID,交易ID,以及調用的鏈碼的方法fcn,方法參數args等

3》鏈碼:/chaincode/fabcar/目錄下fabcar.go

此文件匹配上文的鏈碼ID,包含了以下方法:initLedgerqueryCar,queryAllCarscreateCar and changeCarOwner

func (s *SmartContract) queryAllCars(APIstub shim.ChaincodeStubInterface) sc.Response {
    startKey := "CAR0"
    endKey := "CAR999"

    resultsIterator, err := APIstub.GetStateByRange(startKey, endKey)

此處就是查詢範圍內的數據。

查看全部方法

六、測試

cp query.js query1.js
vim query1.js

修改內部訪問鏈碼方法

複製代碼
    const request = {
        chaincodeId: options.chaincode_id,
        txId: transaction_id,
        fcn: 'queryCar',
        args: ['CAR4']
    };
複製代碼

執行:node query1.js

複製代碼
Create a client and set the wallet location
Set wallet path, and associate user  PeerAdmin  with application
Check user is enrolled, and set a query URL in the network
Make query
Assigning transaction_id:  ca88dc3b60f4df009a709f2f5ee5ad3b54f43d03a7e0b931042e2797f70c795d
returned from query
Query result count =  1
Response is  {"colour":"black","make":"Tesla","model":"S","owner":"Adriana"}
複製代碼

七、更新帳本數據

使用fabcar目錄下的invoke.js

修改,中的 fcn,以及args等參數

複製代碼
var request = {
    targets: targets,
    chaincodeId: options.chaincode_id,
    fcn: '',
    args: [''],
    chainId: options.channel_id,
    txId: tx_id
複製代碼

以下

複製代碼
var request = {
    targets: targets,
    chaincodeId: options.chaincode_id,
    fcn: 'createCar',
    args: ['CAR10', 'Chevy', 'Volt', 'Red', 'Nick'],
    chainId: options.channel_id,
    txId: tx_id
複製代碼

執行命令

node invoke.js

成功後會有

The transaction has been committed on peer localhost:7053

執行

cp query1.js query2.js
vim query2.js

將query2.js中查詢條件參數,變爲CAR10便可

Response is  {"colour":"Red","make":"Chevy","model":"Volt","owner":"Nick"}

ok,能夠繼續調試其餘方法。

相關文章
相關標籤/搜索