Historian for a particular participant

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-18 05:26:27

问题


Is there any way in which I can get Historian for a particular participant in hyperledger-composer using node 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.


回答1:


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

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();

// MODEL FILE


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

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)
}


来源:https://stackoverflow.com/questions/51783822/historian-for-a-particular-participant

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!