在超级聚合器Composer中自动执行事务

时间:2018-05-25 16:14:05

标签: hyperledger-fabric hyperledger-composer

这是我目前的作曲家状态。没什么特别的......

脚本文件:

* 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);

}

型号:

/**
 * 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
}

我现在想要的是实现这样的东西:

参与者1将他的资产发送给参与者2,两者都加起来,此参与者2将此值发送给Partizipant 3 ...依此类推

实现这样的东西最好的方法是什么?有没有例子?..我不想手动添加所有这些交易...就像我的SetupDemo ...

如何可能,所有这些交易都可以显示在事务日志中......就像分别执行所有其他事务的事务一样。

用我在Java中的noobie区块链我做了类似的事情:

int sum = 0;
            for(int i = 0; i < numberParticipants; i++){
                Block block = new Block(getLastBlock().hash);
                for(int n = 0; n < numberParticipants; n++){
                    sum += participants.get(n).getValue(n);

                    if(n+1 !=numberParticipants){
                        block.addTransaction(participants.get(n).sendValue(participants.get(n+1).publicKey, sum));
                    else{
                        block.addTransaction(participants.get(n).sendValue(participants.get(0).publicKey, sum));
                    }
                addBlock(block);    
            }

任何想法,例子?

1 个答案:

答案 0 :(得分:0)

您可以使用单个事务移动多个资产,但您将在Historian注册表中拥有单个记录(所有事务的列表)。或者您可以让后端执行多个事务。但据我所知,你不能让交易创建另一个交易。执行交易是一个复杂的过程,有代言和共识以及所有这些,使其执行其他交易太多了。