Hyperledger Composer 中的多个事务
Multiple Transactions in Hyperledger Composer
我需要进行多次交易,速度快
我正在 Hyperledger Composer 上开发一个 Node.JS 应用程序。我每秒发送大量交易 (11,500),但速度相当慢。我正在使用 Hyperledger Composer 教程中的代码和网络上的一些示例,但似乎我无法让它变得更快。
我的模型很简单,如下:
participant Client identified by id {
o String id
}
asset Wallet identified by id {
o String id
o Double balance
--> Client owner
}
我的script.js如下
function transferFunds(transfer){
//Transfer the funds
transfer.from.balance -= transfer.amount;
transfer.to.balance += transfer.amount;
return getAssetRegistry('org.acme.Wallet')
.then(function(assetRegistry){
//persist the state of the Wallet
assetRegistry.update(transfer.from);
assetRegistry.update(transfer.to);
});
}
我应该在他们之间转账,所以我的 Node.js 交互代码具有以下逻辑:
- 连接到 Hyperledger Fabric
- 获取网络定义
- 加载正在转账的钱包
- 加载正在收款的钱包
- 转账
- 执行交易
我觉得我的代码 做得很糟糕 ,它相当慢,而且通常会使我的 Hyperledger Fabric 实例不堪重负。这是 NodeJS 代码:
const makeTransaction = (fromID, toID, funds) => {
let walletRegistry;
let from;
let to;
businessNetworkConnection.connect(connectionProfile, businessNetworkIdentifier, participantId, participantPwd)
.then((result) => {
businessNetworkDefinition = result;
return businessNetworkConnection.getAssetRegistry('org.acme.Wallet')
.then(function (vr) {
walletRegistry = vr;
return walletRegistry.get(fromID);
})
.then(function (v) {
from = v;
return walletRegistry.get(toID);
})
.then(function (v) {
to = v;
})
.then(function () {
let serializer = businessNetworkDefinition.getSerializer();
let resource = serializer.fromJSON({
"$class": "org.acme.Transfer",
"amount": funds,
"from": {
"$class": "org.acme.Wallet",
"id": from.getIdentifier(),
"balance": from.balance,
"owner": "resource:org.acme.Client#" + from.owner.getIdentifier()
},
"to": {
"$class": "org.acme.Wallet",
"id": to.getIdentifier(),
"balance": to.balance,
"owner": "resource:org.acme.Client#" + to.owner.getIdentifier()
}
});
return businessNetworkConnection.submitTransaction(resource);
});
}).then((result) => {
console.log(chalk.blue(' ------ All done! ------'));
console.log('\n');
return businessNetworkConnection.disconnect();
}).catch(function (error) {
businessNetworkConnection.disconnect();
throw error;
});
}
基本上,我想知道:
- 我如何改进它以允许多次交易而无需多次连接?
- 能否优化我的模型以减少交易规模或不必预加载两个钱包?
非常感谢!
我没有看到为您的 transfer
交易建模的内容。
assetRegistry.update() 完成两次,也许可以使用 updateAll API 调用? -> https://hyperledger.github.io/composer/jsdoc/module-composer-client.AssetRegistry.html 可能会有所帮助。
我能理解为什么您的 Node.js 应用会连接到业务网络并进行交互;但为什么不独立创建 JSON 交易并使用 REST 提交交易呢?例如,使用 Node-Red https://hyperledger.github.io/composer/integrating/node-red.html 从交易文件中集成/加载你的 JSON(如果这是你想要做的,从钱包转移和向钱包转移)。因此,要安装 Node Red,请使用 npm install node-red -g
然后启动它 node-red
并选择(从调色板中)安装选项卡下的 Node red 节点“node-red-contrib-fs”(从文件系统)。在 'file pattern' 中使用模式字符串 *.json
。
回答问题 2。是的,您的模型可以更好地建模。这可能有用 -> When to call getAssetRegistry to update assets (and the participants equivalent)
我需要进行多次交易,速度快
我正在 Hyperledger Composer 上开发一个 Node.JS 应用程序。我每秒发送大量交易 (11,500),但速度相当慢。我正在使用 Hyperledger Composer 教程中的代码和网络上的一些示例,但似乎我无法让它变得更快。
我的模型很简单,如下:
participant Client identified by id {
o String id
}
asset Wallet identified by id {
o String id
o Double balance
--> Client owner
}
我的script.js如下
function transferFunds(transfer){
//Transfer the funds
transfer.from.balance -= transfer.amount;
transfer.to.balance += transfer.amount;
return getAssetRegistry('org.acme.Wallet')
.then(function(assetRegistry){
//persist the state of the Wallet
assetRegistry.update(transfer.from);
assetRegistry.update(transfer.to);
});
}
我应该在他们之间转账,所以我的 Node.js 交互代码具有以下逻辑:
- 连接到 Hyperledger Fabric
- 获取网络定义
- 加载正在转账的钱包
- 加载正在收款的钱包
- 转账
- 执行交易
我觉得我的代码 做得很糟糕 ,它相当慢,而且通常会使我的 Hyperledger Fabric 实例不堪重负。这是 NodeJS 代码:
const makeTransaction = (fromID, toID, funds) => {
let walletRegistry;
let from;
let to;
businessNetworkConnection.connect(connectionProfile, businessNetworkIdentifier, participantId, participantPwd)
.then((result) => {
businessNetworkDefinition = result;
return businessNetworkConnection.getAssetRegistry('org.acme.Wallet')
.then(function (vr) {
walletRegistry = vr;
return walletRegistry.get(fromID);
})
.then(function (v) {
from = v;
return walletRegistry.get(toID);
})
.then(function (v) {
to = v;
})
.then(function () {
let serializer = businessNetworkDefinition.getSerializer();
let resource = serializer.fromJSON({
"$class": "org.acme.Transfer",
"amount": funds,
"from": {
"$class": "org.acme.Wallet",
"id": from.getIdentifier(),
"balance": from.balance,
"owner": "resource:org.acme.Client#" + from.owner.getIdentifier()
},
"to": {
"$class": "org.acme.Wallet",
"id": to.getIdentifier(),
"balance": to.balance,
"owner": "resource:org.acme.Client#" + to.owner.getIdentifier()
}
});
return businessNetworkConnection.submitTransaction(resource);
});
}).then((result) => {
console.log(chalk.blue(' ------ All done! ------'));
console.log('\n');
return businessNetworkConnection.disconnect();
}).catch(function (error) {
businessNetworkConnection.disconnect();
throw error;
});
}
基本上,我想知道:
- 我如何改进它以允许多次交易而无需多次连接?
- 能否优化我的模型以减少交易规模或不必预加载两个钱包?
非常感谢!
我没有看到为您的 transfer
交易建模的内容。
assetRegistry.update() 完成两次,也许可以使用 updateAll API 调用? -> https://hyperledger.github.io/composer/jsdoc/module-composer-client.AssetRegistry.html 可能会有所帮助。
我能理解为什么您的 Node.js 应用会连接到业务网络并进行交互;但为什么不独立创建 JSON 交易并使用 REST 提交交易呢?例如,使用 Node-Red https://hyperledger.github.io/composer/integrating/node-red.html 从交易文件中集成/加载你的 JSON(如果这是你想要做的,从钱包转移和向钱包转移)。因此,要安装 Node Red,请使用 npm install node-red -g
然后启动它 node-red
并选择(从调色板中)安装选项卡下的 Node red 节点“node-red-contrib-fs”(从文件系统)。在 'file pattern' 中使用模式字符串 *.json
。
回答问题 2。是的,您的模型可以更好地建模。这可能有用 -> When to call getAssetRegistry to update assets (and the participants equivalent)