溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Hyperledger Fabric中如何實現Chaincode的查詢功能

發(fā)布時間:2021-12-06 14:46:10 來源:億速云 閱讀:396 作者:小新 欄目:互聯網科技

這篇文章主要介紹了Hyperledger Fabric中如何實現Chaincode的查詢功能,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

Fabric SDK提供了多種查詢功能, 具體包括Channel, Block, Transaction, Chaincode 等信息, 而這個功能, 都是通過Channel對象實現的。

因此, 首先創(chuàng)建channel對象。

// setup the fabric network
var channel = fabric_client.newChannel('mychannel');
var peer = fabric_client.newPeer('grpc://localhost:7051');
channel.addPeer(peer);

然后調用channel的方法實現相應的查詢及其其他功能。例如使用queryByChaincode來查詢Blockchain中存儲的數據。

        // queryCar chaincode function - requires 1 argument, ex: args: ['CAR4'],
        // queryAllCars chaincode function - requires no arguments , ex: args: [''],
        const request = {
                //targets : --- letting this default to the peers assigned to the channel
                chaincodeId: 'fabcar',
                fcn: 'queryAllCars',
                args: ['']
        };

        // send the query proposal to the peer
        return channel.queryByChaincode(request);
}).then((query_responses) => {
        console.log("Query has completed, checking results");
        // query_responses could have more than one  results if there multiple peers were used as targets
        if (query_responses && query_responses.length == 1) {
                if (query_responses[0] instanceof Error) {
                        console.error("error from query = ", query_responses[0]);
                } else {
                        console.log("Response is ", query_responses[0].toString());
                }
        } else {
                console.log("No payloads were returned from query");
        }

此外, Channel 還提供了幾個比較常用的用于查詢的方法:

queryBlock(blockNumber, target, useAdmin, skipDecode)

queryBlockByHash(block, target, useAdmin, skipDecode)

queryBlockByTxID(tx_id, target, useAdmin, skipDecode)

queryTransaction(tx_id, target, useAdmin, skipDecode)

queryInstantiatedChaincodes(target, useAdmin)

使用以上幾個方法, 可以幫助查看Blockchain的狀態(tài)。

感謝你能夠認真閱讀完這篇文章,希望小編分享的“Hyperledger Fabric中如何實現Chaincode的查詢功能”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業(yè)資訊頻道,更多相關知識等著你來學習!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI