特定参与者的历史学家

时间:2018-08-10 09:53:41

标签: node.js hyperledger-fabric hyperledger hyperledger-composer

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

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

1 个答案:

答案 0 :(得分:2)

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

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


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