node.js - 特定参与者的历史学家
问题描述
有什么方法可以让我使用节点 API 为 hyperledger-composer 中的特定参与者获取 Historian?
我正在使用 Node API 开发一个基于 hyperledger-composer 的应用程序。我想在他/她的个人资料中显示特定参与者的交易历史。我为此创建了 permission.acl,并且在操场上运行良好。但是,当我从节点 API 访问历史记录时,它会提供完整的网络历史记录。我不知道如何为参与者过滤它。
解决方案
您可以将自 v0.20 起的 REST API 调用的结果返回给调用客户端应用程序,因此类似以下的内容将起作用(未经测试,但您明白了)。注意:您可以使用您的参数(或您为自己的业务网络创建的任何端点 - 下面的示例是)直接通过 REST 调用 REST API 端(/GET Trader)trade-network
,而不是使用“只读”的示例下面描述的事务处理器端点,用于将更大的结果集返回到您的客户端应用程序。在文档中查看更多信息
使用 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)
}
推荐阅读
- terraform - terraform 动态块创建 vmware 磁盘
- azure - Azure 事件网格 api 版本未定义
- asp.net - 单选按钮列表故障
- reactjs - FormBuilder react-formio 默认值
- django - Django ManyToManyField 持久性失败
- python - 如何将所有参数传递给 self 并删除 args?
- unity3d - ARCore 增强图像不在外部跟踪
- delphi - 为什么不继承本地类型声明?
- r - 如何使用 geom_smooth (ggplot2) 表示变化点趋势之前/之后
- python - PyTorch - 另一个张量中对应值的索引