在Hyperledger Composer中实现自动资产流转批量交易的方案问询
Hyperledger Composer 循环资产流转实现方案
我来帮你梳理下在Hyperledger Composer中实现循环资产流转的最佳方案,结合你现有的配置和需求,具体如下:
现有业务网络配置
首先先整理下你当前的Composer配置:
当前状态
无特殊配置
脚本文件(logic.js)
/* global getAssetRegistry getFactory emit */ /** * Sample transaction processor function. * @param {org.example.basic.SampleTransaction} tx The sample transaction instance. * @transaction */ async function sampleTransaction(tx) { // eslint-disable-line no-unused-vars // Save the old value of the asset. const oldValue = tx.asset.value; // Update the asset with the new value. tx.asset.value = tx.newValue; // Get the asset registry for the asset. const assetRegistry = await getAssetRegistry('org.example.basic.SecretAsset'); // Update the asset in the asset registry. await assetRegistry.update(tx.asset); // Emit an event for the modified asset. let event = getFactory().newEvent('org.example.basic', 'SampleEvent'); event.asset = tx.asset; event.oldValue = oldValue; event.newValue = tx.newValue; emit(event); } /** * * @param {org.example.basic.SetupDemo} setupDemo - SetupDemo instance * @transaction */ async function setupDemo(setupDemo) { // eslint-disable-line no-unused-vars const factory = getFactory(); const NS = 'org.example.basic'; const MPC_Participant = [ factory.newResource(NS, 'MPC_Participant', 'P1'), factory.newResource(NS, 'MPC_Participant', 'P2'), factory.newResource(NS, 'MPC_Participant', 'P3'), factory.newResource(NS, 'MPC_Participant', 'P4'), ]; MPC_Participant.forEach(function(MPC_Participant) { MPC_Participant.participantId = MPC_Participant.getIdentifier(); MPC_Participant.number = String(Math.floor(Math.random() * (20 - 10 + 1)) + 10); }); const MPC_ParticipantRegistry = await getParticipantRegistry(NS + '.MPC_Participant'); await MPC_ParticipantRegistry.addAll(MPC_Participant); //add asset to each participant const SecretAsset = [ factory.newResource(NS, 'SecretAsset', 'I1'), factory.newResource(NS, 'SecretAsset', 'I2'), factory.newResource(NS, 'SecretAsset', 'I3'), factory.newResource(NS, 'SecretAsset', 'I4'), ]; SecretAsset.forEach(function(SecretAsset, index) { const par = 'P' + (index + 1); SecretAsset.assetId = SecretAsset.assetId; SecretAsset.owner = factory.newRelationship(NS, 'MPC_Participant', par); SecretAsset.value = String(Math.floor(Math.random() * (20 - 10 + 1)) + 10); }); const SecretAssetRegistry = await getAssetRegistry(NS + '.SecretAsset'); await SecretAssetRegistry.addAll(SecretAsset); }
模型文件(model.cto)
/** * Sample business network definition. */ namespace org.example.basic asset SecretAsset identified by assetId { o String assetId --> MPC_Participant owner o String value } participant MPC_Participant identified by participantId { o String participantId o String number } transaction SampleTransaction { --> SecretAsset asset o String newValue } transaction SetupDemo { } event SampleEvent { --> SecretAsset asset o String oldValue o String newValue }
需求理解
你需要实现的核心逻辑是:
- 资产在MPC_Participant之间循环流转(P1→P2→P3→P4→P1...)
- 流转过程中累加数值
- 无需手动执行每笔交易,自动完成循环
- 每笔流转交易都要有独立的日志记录
你之前在自研Java区块链中通过循环创建交易并打包进区块实现,现在我们把这个逻辑适配到Composer中。
实现方案
1. 扩展业务模型
我们需要新增两个交易类型和一个事件,来支撑自动循环和单步流转的逻辑:
更新后的model.cto:
/** * Sample business network definition. */ namespace org.example.basic asset SecretAsset identified by assetId { o String assetId --> MPC_Participant owner o String value } participant MPC_Participant identified by participantId { o String participantId o String number } transaction SampleTransaction { --> SecretAsset asset o String newValue } transaction SetupDemo { } // 单步资产转移并累加的交易 transaction TransferAndAccumulate { --> SecretAsset asset --> MPC_Participant newOwner } // 启动循环流转的触发交易 transaction StartCycleTransfer { o Integer cycleCount // 可选:循环的轮数,默认1轮 } event SampleEvent { --> SecretAsset asset o String oldValue o String newValue } // 流转事件,用于追踪每一步的转移详情 event TransferEvent { --> SecretAsset asset --> MPC_Participant oldOwner --> MPC_Participant newOwner o String accumulatedValue }
2. 编写交易处理器
接下来补充两个关键的交易处理函数到你的logic.js中:
单步流转处理函数
负责完成单步的资产转移和数值累加,每执行一次就是一笔独立的交易:
/** * 处理单步资产转移与数值累加 * @param {org.example.basic.TransferAndAccumulate} tx 转移交易实例 * @transaction */ async function transferAndAccumulate(tx) { const oldOwner = tx.asset.owner; const oldValue = parseInt(tx.asset.value); // 这里按照你的需求实现累加逻辑:资产值加上新持有者的number值 // 如果需要调整累加规则(比如加当前持有者的number),直接修改这里即可 const accumulatedValue = oldValue + parseInt(tx.newOwner.number); tx.asset.value = accumulatedValue.toString(); tx.asset.owner = tx.newOwner; // 更新资产注册表 const assetRegistry = await getAssetRegistry('org.example.basic.SecretAsset'); await assetRegistry.update(tx.asset); // 触发流转事件,方便后续监控和审计 const event = getFactory().newEvent('org.example.basic', 'TransferEvent'); event.asset = tx.asset; event.oldOwner = oldOwner; event.newOwner = tx.newOwner; event.accumulatedValue = accumulatedValue.toString(); emit(event); }
循环触发处理函数
负责自动循环创建并提交每一步的流转交易,全程无需手动干预:
/** * 启动循环流转交易 * @param {org.example.basic.StartCycleTransfer} tx 启动交易实例 * @transaction */ async function startCycleTransfer(tx) { const factory = getFactory(); const NS = 'org.example.basic'; const cycleCount = tx.cycleCount || 1; // 默认循环1轮 // 获取所有参与者并按P1-P4排序 const participantRegistry = await getParticipantRegistry(NS + '.MPC_Participant'); const participants = await participantRegistry.getAll(); const sortedParticipants = participants.sort((a, b) => { return parseInt(a.participantId.substring(1)) - parseInt(b.participantId.substring(1)); }); const participantCount = sortedParticipants.length; // 获取所有资产(匹配你SetupDemo中每个参与者对应一个资产的逻辑) const assetRegistry = await getAssetRegistry(NS + '.SecretAsset'); const assets = await assetRegistry.getAll(); // 循环执行指定轮数 for (let cycle = 0; cycle < cycleCount; cycle++) { // 遍历每个参与者,执行资产转移 for (let i = 0; i < participantCount; i++) { const currentParticipant = sortedParticipants[i]; // 找到当前参与者持有的资产 const asset = assets.find(a => a.owner.participantId === currentParticipant.participantId); if (!asset) continue; // 确定下一个参与者:最后一个回到第一个 const nextIndex = (i + 1) % participantCount; const nextParticipant = sortedParticipants[nextIndex]; // 创建单步转移交易 const transferTx = factory.newTransaction(NS, 'TransferAndAccumulate'); transferTx.asset = factory.newRelationship(NS, 'SecretAsset', asset.assetId); transferTx.newOwner = factory.newRelationship(NS, 'MPC_Participant', nextParticipant.participantId); // 提交交易:每一步都是独立的交易,会被单独记录在区块链日志中 await submitTransaction(transferTx); } } }
3. 核心优势说明
- 独立交易日志:每一次
TransferAndAccumulate交易都会被单独提交到区块链,Composer会自动记录每笔交易的ID、参与者、时间、交易内容等完整信息,完全满足你对单独日志的需求。 - 自动执行:只需提交一次
StartCycleTransfer交易,就能自动完成指定次数的循环流转,不用手动操作每一步。 - 灵活调整:可以通过
cycleCount参数控制循环轮数,也可以修改transferAndAccumulate中的累加逻辑适配你的业务需求。
4. 使用步骤
- 先执行
SetupDemo交易初始化参与者和资产。 - 在Composer Playground或通过API提交
StartCycleTransfer交易,比如设置循环2轮:
{ "$class": "org.example.basic.StartCycleTransfer", "cycleCount": 2 }
提交后,区块链上会生成4*2=8笔独立的TransferAndAccumulate交易,每笔都有单独的日志,资产会按照P1→P2→P3→P4→P1的顺序循环流转并累加数值。
内容的提问来源于stack exchange,提问作者h0ppel
相关产品推荐
相关产品推荐

