一尘不染

特定参与者的历史学家

node.js

有什么方法可以使用节点API获取Hyperledger-composer中特定参与者的Historian?

我正在使用Node API开发基于hyperledger-
composer的应用程序。我想在其个人资料中显示特定参与者的交易历史记录。我已经为此创建了permission.acl,并且在操场上运行良好。但是,当我从节点API访问历史记录器时,它将提供网络的完整历史记录器。我不知道该如何过滤参与者。


阅读 242

收藏
2020-07-07

共1个答案

一尘不染

您可以将自v0.20以来的REST
API调用的结果返回给调用的客户端应用程序,因此可以进行如下操作(未经测试,但是您知道了)。注意:您可以直接通过REST直接使用您的参数(或您为自己的业务网络创建的任何端点-
下面的示例是trade-network)调用REST API结束(/ GET Trader
),而不是使用“只读”的示例如下所述的事务处理器端点,用于将更大的结果集返回到客户端应用程序。在文档中查看有关此内容的更多信息

使用API​​的NODE JS Client:


    const BusinessNetworkConnection = require('composer-client').BusinessNetworkConnection;

    const rp = require('request-promise');

    this.bizNetworkConnection = new BusinessNetworkConnection();
    this.cardName ='admin@mynet';
    this.businessNetworkIdentifier = 'mynet';

    this.bizNetworkConnection.connect(this.cardName)
    .then((result) => {

    //You can do ANYTHING HERE eg.

    })
    .catch((error) => {
    throw error;
    });

    // set up my read only transaction object - find the history of a particular Participant - note it could equally be an Asset instead !

    var obj = {
        "$class": "org.example.trading.MyPartHistory",
        "tradeId": "P1"
    };


    async function callPartHistory() {

    var options = {
        method: 'POST',
        uri: 'http://localhost:3000/api/MyPartHistory',
        body: obj,
        json: true 
    };

    let results = await rp(options);
    //    console.log("Return value from REST API is " + results);
    console.log(" ");
    console.log(`PARTICIPANT HISTORY for Asset ID:  ${results[0].tradeId} is: `); 
    console.log("=============================================");

    for (const part of results) {
         console.log(`${part.tradeId}             ${part.name}` );
    }
   }

   // Main

   callPartHistory();

//模型文件


@commit(false)
@returns(Trader[])
transaction MyPartHistory {
o String tradeId
}

只读交易处理器代码(在“ logic.js”中):


/**
 * Sample read-only transaction
 * @param {org.example.trading.MyPartHistory} tx
 * @returns {org.example.trading.Trader[]} All trxns  
 * @transaction
 */


async function participantHistory(tx) {

    const partId = tx.tradeid;
    const nativeSupport = tx.nativeSupport;
    // const partRegistry = await getParticipantRegistry('org.example.trading.Trader')

    const nativeKey = getNativeAPI().createCompositeKey('Asset:org.example.trading.Trader', [partId]);
    const iterator = await getNativeAPI().getHistoryForKey(nativeKey);
    let results = [];
    let res = {done : false};
    while (!res.done) {
        res = await iterator.next();

        if (res && res.value && res.value.value) {
            let val = res.value.value.toString('utf8');
            if (val.length > 0) {
               console.log("@debug val is  " + val );
               results.push(JSON.parse(val));
            }
        }
        if (res && res.done) {
            try {
                iterator.close();
            }
            catch (err) {
            }
        }
    }
    var newArray = [];
    for (const item of results) {
            newArray.push(getSerializer().fromJSON(item));
    }
    console.log("@debug the results to be returned are as follows: ");

    return newArray; // returns something to my NodeJS client (called via REST API)
}
2020-07-07