是否可以在 composer 中订阅资产创建事件?

Is it possible to subscribe to asset creation events in composer?

我意识到我可以在链代码中发出事件并在节点 SDK 中监听这些事件。

但是,我希望能够在资产创建时收到一个事件,以便我可以对其采取行动。这可能吗?

另一种方法可能是能够指定在添加资产时调用的函数。

我能想到一个解决方法:

  1. 创建一个交易来创建带有事件的资产
  2. 在 .acl 中将资产的创建限制为一次交易

示例:

rule CreateAssetOnlyWithTx{
    description: "Allow all learners to create and read their own submissions"
    participant: "ANY"
    operation: CREATE
    resource: "org.example.blah.asset"
    transaction: "org.example.blah.tx"
    action: ALLOW
}

如果您通过交易功能添加资产,您可以在创建后立即发出事件。如果资产已成功创建,仅供参考,才会发出该事件。在这里查看更多 https://hyperledger.github.io/composer/business-network/publishing-events.html

// 将车辆添加到资产注册表并发出事件 - 未测试。

return getAssetRegistry('org.acme.Vehicle')
.then(function (vehicleAssetRegistry) {
    // Get the factory for creating new asset instances.
    var factory = getFactory();

    // Create the vehicle.

    var vehicle = factory.newResource('org.acme', 'Vehicle', 'VEHICLE_1');

    vehicle.colour = 'BLUE';

    // Add the vehicle to the vehicle asset registry.

    return vehicleAssetRegistry.add(vehicle);

})
.catch(function (error) {

// Add optional error handling here. If the transaction fails, no event is emitted please note

})
.then(function() {

     var addNotification = getFactory().newEvent('org.acme.trading', 'AddNotification');

     addNotification.vehicle = vehicle;   // the object passed into the transaction etc etc
     emit(addNotification);
})