本文介绍了特定参与者的历史学家的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there any way in which I can get Historian for a particular participant in hyperledger-composer using node API?

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

I am developing an application based on hyperledger-composer using Node APIs.I want to show the history of transaction of a particular participant in his/her profile. I have created the permission.acl for that and that is working fine in playground. But when i am accessing the historian from node API it is giving complete historian of the network. I don't know how to filter that for a participant.

推荐答案

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

you can return results from REST API calls since v0.20 to the calling client application, so something like the following would work (not tested, but you get the idea). NOTE: You could just call the REST API end (/GET Trader) direct via REST with your parameter (or whatever endpoints you create for your own business network - the example below is trade-network), rather than the example of using 'READ-ONLY' Transaction processor Endpoint described below, for returning larger result sets to your client application. See more on this in the docs

使用API​​的NODE JS客户端:

NODE JS Client using APIs:

    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"中):

READ-ONLY TRANSACTION PROCESSOR CODE (in '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)
}

这篇关于特定参与者的历史学家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 12:36