在脚本文件中编写事务

问题描述 投票:0回答:1

我正在制定一个计划,我有一个供应链(materialSupplier,Producer,OEM和User),每个参与者的部门之间进行交易,参与者之间的交易: - MaterialSupplier和Producer材料 - 生产者和OEM模块 - OEM和用户,以及用户线束之间

我能够在模型文件中创建所有参与者,资产和交易(我认为/希望我没有任何错误)。此外,我能够在“异步功能SetupDemo”中创建示例。

现在我尝试编写事务,但它不起作用。我已经尝试过不同的方式......现在这是我的代码:

模型文件:

namespace org.master

// BASE DEFINTIONS

concept MaterialDetails {
  --> MaterialSupplier make
  o String materialName
  o String materialColour optional
  o String batch
  o Double amount
  --> MaterialSupplier owner optional
}

concept MaterialTransferLogEntry {
  --> Material materialId
  --> MaterialSupplier buyer
  --> MaterialSupplier seller optional
  o DateTime timestamp
}

asset Material identified by materialId {
  o String materialId
  o MaterialDetails materialDetails
  o MaterialTransferLogEntry [] logEntries optional
}

concept ModuleDetails {
  --> Producer make
  o String moduleName
  o Double amount
  o String components
}

asset Module identified by moduleId {
  o String moduleId
  o ModuleDetails moduleDetails
}

concept HarnessDetails {
  --> OEM make 
  o String harnessName
  o Double amount
  o String components
}

asset Harness identified by harnessId {
  o String harnessId
  o HarnessDetails harnessDetails
}

participant MaterialSupplier identified by materialSupplierId {
  o String materialSupplierId
  o String materialSupplierName
  o Double accountBalance optional
}

participant Producer identified by producerId {
  o String producerId
  o String producerName
  o Double accountBalance optional
}

participant OEM identified by oemId {
  o String oemId
  o String oemName
  o Double accountBalance optional
}

participant User identified by userId {
  o String userId
  o String userName optional
  o Double accountBalance optional
}

transaction InternMaterialSupplier {
  --> MaterialSupplier seller
  --> MaterialSupplier buyer
  --> Material materialId
  o Double unitCount
  o DateTime arrivalDateTime
}

event InternMaterialSupplierEvent {
  --> MaterialSupplier seller
  --> MaterialSupplier buyer
  --> Material materialId
  o Double unitCount
  o DateTime arrivalDateTime
}

transaction MaterialSupplierToProducer {
  --> MaterialSupplier seller
  --> Producer buyer
  o Double unitCount
  o Double unitPrice
  o Double minQuality optional
  o Double maxQuality optional
  o Double Penalty optional
  o QualityControlMaterial [] qualityControlsMaterial
  o DateTime arrivalDateTime
}

event MaterialSupplierToProducerEvent {
  --> MaterialSupplier seller
  --> Producer buyer
  o Double unitCount
  o Double unitPrice
  o Double minQuality optional
  o Double maxQuality optional
  o Double Penalty optional
  o QualityControlMaterial [] qualityControlsMaterial
  o DateTime arrivalDateTime
}

transaction InternProducer {
  --> Producer seller
  --> Producer buyer
  --> Material materialId
  o Double unitCount
  o DateTime arrivalDateTime
}

event InternProducerEvent {
  --> Producer seller
  --> Producer buyer
  --> Material materialId
  o Double unitCount
  o DateTime arrivalDateTime
}

transaction ProducerToOEM {
  --> Producer seller
  --> OEM buyer
  o Double unitCount
  o Double unitPrice
  o Double minQuality optional
  o Double maxQuality optional
  o Double Penalty optional
  o QualityControlModule [] qualityControlsModule
  o DateTime arrivalDateTime
}

event ProducerToOEMEvent {
  --> Producer seller
  --> OEM buyer
  o Double unitCount
  o Double unitPrice
  o Double minQuality optional
  o Double maxQuality optional
  o Double Penalty optional
  o QualityControlModule [] qualityControlsModule
  o DateTime arrivalDateTime
}

transaction InternOEM {
  --> OEM seller
  --> OEM buyer
  --> Module moduleId
  o Double unitCount
  o DateTime arrivalDateTime
}

event InternOEMEvent {
  --> OEM seller
  --> OEM buyer
  --> Module moduleId
  o Double unitCount
  o DateTime arrivalDateTime
}

transaction OEMToUser {
  --> OEM seller
  --> User buyer
  o Double unitCount
  o Double unitPrice
  o Double minQuality optional
  o Double maxQuality optional
  o Double Penalty optional
  o QualityControlHarness [] qualityControlsHarness
  o DateTime arrivalDateTime
}

event OEMToUserEvent {
  --> OEM seller
  --> User buyer
  o Double unitCount
  o Double unitPrice
  o Double minQuality optional
  o Double maxQuality optional
  o Double Penalty optional
  o QualityControlHarness [] qualityControlsHarness
  o DateTime arrivalDateTime
}

transaction InternUser {
  --> OEM seller
  --> OEM buyer
  --> Harness harnessId
  o Double unitCount
  o Double unitPrice
  o DateTime arrivalDateTime
}

event InternUserEvent {
  --> OEM seller
  --> OEM buyer
  --> Harness harnessId
  o Double unitCount
  o Double unitPrice
  o DateTime arrivalDateTime
}

transaction QualityControlMaterial {
  o Double qualityDegree
}

transaction QualityControlModule {
  o Double qualityDegree
}

transaction QualityControlHarness {
  o Double qualityDegree
}

// DEMO SPECIFIC DEFINITIONS
transaction SetupDemo {
}

脚本文件:

/**
 * @param {org.master.InternMaterialSupplier} test - the InternmaterialSupplier transaction
 * @transaction
 */

async function internMaterialSupplier(internMaterialSupplier) {
  console.log('internMaterialSupplier');

  const namespace = 'org.master';
  const factory = getFactory();

  const seller = internMaterialSupplier.seller;
  const buyer = internMaterialSupplier.buyer;
  const material = internMaterialSupplier.material;

  material.owner = buyer;

  const materialTransferLogEntry = factory.newConcept(namespace, 'MaterialTransferLogEntry');
  materialTransferLogEntry.material = factory.newRelationship(namespace, 'Material', material.getIdentifier());
  materialTransferLogEntry.seller = factory.newRelationship(namespace, 'MaterialSupplier', seller.getIdentifier());
  materialTransferLogEntry.buyer = factory.newRelationship(namespace, 'MaterialSupplier', buyer.getIdentifier());
  materialTransferLogEntry.timestamp = internMaterialSupplier.timestamp;
  if(!material.LogEntries) {
    material.logEntries = [];
  }

  material.logEntries.push(materialTransferLogEntry);

  const assetRegistry = await getAssetRegistry(material.getFullyQualifiedType());
  await assetRegistry.update(material);
}

// DEMO SETUP FUNCTIONS
/**
 * Create the participants to use in the demo
 * @param {org.master.SetupDemo} setupDemo - the SetupDemo transaction
 * @transaction
 */
async function setupDemo(setupDemo) { // eslint-disable-line no-unused-vars
    console.log('setupDemo');

    const factory = getFactory();
    const namespace = 'org.master';

    let materialSuppliers;

    const materials = {
        'Logistics': {
            'Wire1': [
                {
                    'materialId': 'W1.1',
                    'materialColour': 'Yellow',
                    'batch': 'Wire 1.1',
                    'amount': 100,
                    'owner' : 'Logistics'
                }
            ],
            'Wire2': [
                {
                    'materialId': 'W2.1',
                    'materialColour': 'Blue',
                    'batch': 'Wire 2.1',
                    'amount': 100
                }
            ],
            'Wire3': [
                {
                    'materialId': 'W3.1',
                    'materialColour': 'Black',
                    'batch': 'Wire 3.1',
                    'amount': 100
                }
            ],
            'Connector1': [
                {
                    'materialId': 'C1.1',
                    'batch': 'Connector 1.1',
                    'amount': 100
                }
            ],
            'Connector2': [
                {
                    'materialId': 'C2.1',
                    'batch': 'Connector 2.1',
                    'amount': 100
                }
            ],
            'Connector3': [
                {
                    'materialId': 'C3.1',
                    'batch': 'Connector 3.1',
                    'amount': 100
                }
            ],
            'Grommet1': [
                {
                    'materialId': 'G1.1',
                    'batch': 'Grommet 1.1',
                    'amount': 100
                }
            ],
            'Grommet2': [
                {
                    'materialId': 'G2.1',
                    'batch': 'Grommet 2.1',
                    'amount': 100
                }
            ],
            'Grommet3': [
                {
                    'materialId': 'G3.1',
                    'batch': 'Grommet 3.1',
                    'amount': 100
                }
            ],
            'Seal1': [
                {
                    'materialId': 'S1.1',
                    'batch': 'Seal 1.1',
                    'amount': 100
                }
            ],
            'Seal2': [
                {
                    'materialId': 'S2.1',
                    'batch': 'Seal 2.1',
                    'amount': 100
                }
            ],
            'Seal3': [
                {
                    'materialId': 'S3.1',
                    'batch': 'Seal 3.1',
                    'amount': 100
                }
            ],
            'Part1': [
                {
                    'materialId': 'P1.1',
                    'batch': 'Part 1.1',
                    'amount': 100
                }
            ],
            'Part2': [
                {
                    'materialId': 'P2.1',
                    'batch': 'Part 2.1',
                    'amount': 100
                }
            ],
            'Part3': [
                {
                    'materialId': 'P3.1',
                    'batch': 'Part 3.1',
                    'amount': 100
                }
            ]
        },
        'Production': {
        },
        'QualityControl': {
        }
    };

    // create array of MaterialSuppliers particpant resources identified by the top level keys in materials const
    materialSuppliers = Object.keys(materials).map(function (materialSupplier) {
        const materialSupplierResource = factory.newResource(namespace, 'MaterialSupplier', materialSupplier);
        materialSupplierResource.materialSupplierName = materialSupplier;
        return materialSupplierResource;
    });

    // add the materialSupplier
    const materialSupplierRegistry = await getParticipantRegistry(namespace + '.MaterialSupplier');
    await materialSupplierRegistry.addAll(materialSuppliers);

    // add the materials
    const materialRegistry = await getAssetRegistry(namespace + '.Material');
    const materialResources = [];

    for (const materialSupplier in materials) {
        for (const model in materials[materialSupplier]) { // brauch ich die Funktion?
            const materialCharacteristicsForModel = materials[materialSupplier][model]; // brauch ich die Funktion?
            materialCharacteristicsForModel.forEach(function(materialCharacteristics) { // brauch ich die Funktion?
                const material = factory.newResource(namespace, 'Material', materialCharacteristics.materialId);
                material.materialDetails = factory.newConcept(namespace, 'MaterialDetails');
                material.materialDetails.make = factory.newResource(namespace, 'MaterialSupplier', materialSupplier);
                material.materialDetails.materialName = model;
                material.materialDetails.materialColour = materialCharacteristics.materialColour;
                material.materialDetails.batch = materialCharacteristics.batch;
                material.materialDetails.amount = materialCharacteristics.amount;

                materialResources.push(material);
            });
        }
    }

    await materialRegistry.addAll(materialResources);

    let producers;

    const modules = {
        'Logistics': {
            'Module1': [
                {
                    'moduleId': 'M1',
                    'amount': 10,
                    'components': 'W1, C1, G1, S1, P1'
                }
            ],
            'Module2': [
                {
                    'moduleId': 'M2',
                    'amount': 5,
                    'components': 'W2, W3, C3, S2, S3, P1, P2'
                }
            ],
            'Module3': [
                {
                    'moduleId': 'M3',
                    'amount': 1,
                    'components': 'W3, C1, G2, P2'
                }
            ]
        },
        'Production': {
        },
        'QualityControl': {
        }
    };

    // create array of Producers particpant resources identified by the top level keys in modules const
    producers = Object.keys(modules).map(function (producer) {
        const producerResource = factory.newResource(namespace, 'Producer', producer);
        producerResource.producerName = producer;
        return producerResource;
    });

    // add the producer
    const producerRegistry = await getParticipantRegistry(namespace + '.Producer');
    await producerRegistry.addAll(producers);

    // add the modules
    const moduleRegistry = await getAssetRegistry(namespace + '.Module');
    const moduleResources = [];

    for (const producer in modules) {
        for (const model in modules[producer]) { // brauch ich die Funktion?
            const moduleCharacteristicsForModel = modules[producer][model]; // brauch ich die Funktion?
            moduleCharacteristicsForModel.forEach(function(moduleCharacteristics) { // brauch ich die Funktion?
                const module = factory.newResource(namespace, 'Module', moduleCharacteristics.moduleId);
                module.moduleDetails = factory.newConcept(namespace, 'ModuleDetails');
                module.moduleDetails.make = factory.newResource(namespace, 'Producer', producer);
                module.moduleDetails.moduleName = model;
                module.moduleDetails.amount = moduleCharacteristics.amount;
                module.moduleDetails.components = moduleCharacteristics.components; 

                moduleResources.push(module);
            });
        }
    }

    await moduleRegistry.addAll(moduleResources);


    let oems;

    const harnesses = {
        'Logistics': {
            'Harness': [
                {
                    'harnessId': 'H1',
                    'amount': 10,
                    'components': 'M1, M3'
                }
            ],
            'Harness2': [
                {
                    'harnessId': 'H2',
                    'amount': 5,
                    'components': 'M1, M1, M2'
                }
            ],
            'Harness3': [
                {
                    'harnessId': 'H3',
                    'amount': 1,
                    'components': 'M2, M3'
                }
            ]
        },
        'Production': {
        },
        'QualityControl': {
        }
    };

    // create array of Producers particpant resources identified by the top level keys in modules const
    oems = Object.keys(harnesses).map(function (oem) {
        const oemResource = factory.newResource(namespace, 'OEM', oem);
        oemResource.oemName = oem;
        return oemResource;
    });

    // add the OEM
    const oemRegistry = await getParticipantRegistry(namespace + '.OEM');
    await oemRegistry.addAll(oems);

    // add the modules
    const harnessRegistry = await getAssetRegistry(namespace + '.Harness');
    const harnessResources = [];

    for (const oem in harnesses) {
        for (const model in harnesses[oem]) { // brauch ich die Funktion?
            const harnessCharacteristicsForModel = harnesses[oem][model]; // brauch ich die Funktion?
            harnessCharacteristicsForModel.forEach(function(harnessCharacteristics) { // brauch ich die Funktion?
                const harness = factory.newResource(namespace, 'Harness', harnessCharacteristics.harnessId);
                harness.harnessDetails = factory.newConcept(namespace, 'HarnessDetails');
                harness.harnessDetails.make = factory.newResource(namespace, 'OEM', oem);
                harness.harnessDetails.harnessName = model;
                harness.harnessDetails.amount = harnessCharacteristics.amount;
                harness.harnessDetails.components = harnessCharacteristics.components; 


                harnessResources.push(harness);
            });
        }
    }

    await harnessRegistry.addAll(harnessResources);

  // Users: die bisherigen Programmierungen umwandeln!! - wie muss ich schauen

  let users = ['Tom', 'Harold', 'Lizzy', 'Emma', 'Patrick'];

  // convert array names of users to be array of participant resources of type User with identifier of that name
  users = users.map(function (user) {
        return factory.newResource(namespace, 'User', user);
    });

  // add the users
  const userRegistry = await getParticipantRegistry(namespace + '.User');
  await userRegistry.addAll(users);
}

访问控制:

/**
 * Sample access control list.
 */
rule Default {
    description: "Allow all participants access to all resources"
    participant: "ANY"
    operation: ALL
    resource: "org.master.*"
    action: ALLOW
}

rule SystemACL {
    description:  "System ACL to permit all access"
    participant: "org.hyperledger.composer.system.Participant"
    operation: ALL
    resource: "org.hyperledger.composer.system.**"
    action: ALLOW
}

rule NetworkAdminUser {
    description: "Grant business network administrators full access to user resources"
    participant: "org.hyperledger.composer.system.NetworkAdmin"
    operation: ALL
    resource: "**"
    action: ALLOW
}

rule NetworkAdminSystem {
    description: "Grant business network administrators full access to system resources"
    participant: "org.hyperledger.composer.system.NetworkAdmin"
    operation: ALL
    resource: "org.hyperledger.composer.system.**"
    action: ALLOW
}

我不期望一个完整的解决方案,但也许一些提示/帮助我如何在脚本文件中编写事务

谢谢 ;)

transactions hyperledger-composer
1个回答
0
投票

基本上,Hyperledger Composer业务网络定义由一组模型文件和一组脚本组成。在模型文件中,您需要声明资产,参与者,事务和事件。

脚本可能包含事务处理器函数,这些函数实现业务网络定义的模型文件中定义的事务。

我建议使用以下链接在文件logic.js中编写事务逻辑

1)Transaction processor structure

2)Transaction Functions Detail

3)some basic explanation

希望它能帮到你:)

© www.soinside.com 2019 - 2024. All rights reserved.