Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
620 views
in Technique[技术] by (71.8m points)

node.js - Historian for a particular participant

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...