交易处理器函数

Hyperledger Composer业务网络定义由一组模型文件和一组脚本组成。这些脚本可能包含交易处理器函数,这些函数可以实现业务网络定义的模型文件中定义的交易。

当使用BusinessNetworkConnection API提交交易时,交易处理函数将由运行时自动调用。

带有文档注释的装饰器在运行时处理时,使用元数据对函数进行注解。

每种交易类型都有一个关联的存储交易的库。

交易处理器结构

交易处理器函数的结构包括装饰器和元数据,后面跟着一个JavaScript函数,这两个部分都是交易处理器函数工作所必需的。

交易处理器函数之上的第一行注释包含交易处理器函数所做的人类可读描述。第二行必须包含@param标签来指示参数定义。该@param标签之后是触发交易处理器函数交易的资源名称,这需要业务网络的命名空间的格式,其次是交易的名称。资源名称后面是引用资源的参数名称,该参数必须作为参数提供给JavaScript函数。第三行必须包含@transaction标签,该标签将代码标识为交易处理器函数因而是必需的。

  1. /**
  2. * A transaction processor function description
  3. * @param {org.example.sampleTransaction} parameter-name A human description of the parameter
  4. * @transaction
  5. */

在注释之后是为交易提供价值的JavaScript函数。该函数可以具有任何名称,但必须包含在注解中定义的参数名称作为参数。

  1. function transactionProcessor(parameter-name) {
  2. //Do some things.
  3. }

完整的交易处理器函数将采用以下格式:

  1. /**
  2. * A transaction processor function description
  3. * @param {org.example.sampleTransaction} parameter-name A human description of the parameter
  4. * @transaction
  5. */
  6. function transactionProcessor(parameter-name) {
  7. //Do some things.
  8. }

编写交易处理器函数

交易处理函数是在模型文件中定义的交易的逻辑操作。例如,交易的交易处理器函数Trade可以使用JavaScript将owner资产的属性从一个参与者改变到另一个参与者。

下面是一个例子basic-sample-network,下面的SampleAsset定义包含一个名为value的属性,它被定义为一个字符串。该SampleTransaction交易需要一个到资产的关联,资产会被改变,value属性的新值由交易的属性newValue提供。

  1. asset SampleAsset identified by assetId {
  2. o String assetId
  3. --> SampleParticipant owner
  4. o String value
  5. }
  6. transaction SampleTransaction {
  7. --> SampleAsset asset
  8. o String newValue
  9. }

与交易SampleTransaction关联的交易处理器函数是使资产和资产存储库都发生变化的原因。

交易处理函数将SampleTransaction类型定义为关联交易,并将其定义为参数tx。然后,它保存要由交易更改的资产的原始值,将其替换为在提交交易期间传递的值(newValue交易定义中的属性),更新库中的资产,然后发出事件。

  1. /**
  2. * Sample transaction processor function.
  3. * @param {org.acme.sample.SampleTransaction} tx The sample transaction instance.
  4. * @transaction
  5. */
  6. function sampleTransaction(tx) {
  7. // Save the old value of the asset.
  8. var oldValue = tx.asset.value;
  9. // Update the asset with the new value.
  10. tx.asset.value = tx.newValue;
  11. // Get the asset registry for the asset.
  12. return getAssetRegistry('org.acme.sample.SampleAsset')
  13. .then(function (assetRegistry) {
  14. // Update the asset in the asset registry.
  15. return assetRegistry.update(tx.asset);
  16. })
  17. .then(function () {
  18. // Emit an event for the modified asset.
  19. var event = getFactory().newEvent('org.acme.sample', 'SampleEvent');
  20. event.asset = tx.asset;
  21. event.oldValue = oldValue;
  22. event.newValue = tx.newValue;
  23. emit(event);
  24. });
  25. }

交易处理函数中的错误处理

交易处理器函数将失败,并回滚任何已经发生错误的更改。整个交易失败,不仅仅是交易处理,交易处理器函数在错误发生之前改变的任何东西都将被回滚。

  1. /**
  2. * Sample transaction processor function.
  3. * @param {org.acme.sample.SampleTransaction} tx The sample transaction instance.
  4. * @transaction
  5. */
  6. function sampleTransaction(tx) {
  7. // Do something.
  8. throw new Error('example error');
  9. // Execution stops at this point; the transaction fails and rolls back.
  10. // Any updates made by the transaction processor function are discarded.
  11. // Transaction processor functions are atomic; all changes are committed,
  12. // or no changes are committed.
  13. }

交易所做的更改是原子操作,要么交易成功且生效所有更改,或者交易失败且所有更改无效。

解析交易中的关联

当一个交易涉及的资产、交易或参与者的某属性是一个关联时,关联会自动解析。所有关联,包括嵌套关联,会在交易处理函数运行之前解析。

以下示例包含嵌套关联,交易含有一个到资产的关联,这个资产又含有一个到参与者的关联,因为所有关联都已解析,资产的owner属性被解析为指定的参与者。

模型文件:

  1. namespace org.acme.sample
  2. participant SampleParticipant identified by participantId {
  3. o String participantId
  4. }
  5. asset SampleAsset identified by assetId {
  6. o String assetId
  7. --> SampleParticipant owner
  8. }
  9. transaction SampleTransaction {
  10. --> SampleAsset asset
  11. }

脚本文件:

  1. /**
  2. * Sample transaction processor function.
  3. * @param {org.acme.sample.SampleTransaction} tx The sample transaction instance.
  4. * @transaction
  5. */
  6. function sampleTransaction(tx) {
  7. // The relationships in the transaction are automatically resolved.
  8. // This means that the asset can be accessed in the transaction instance.
  9. var asset = tx.asset;
  10. // The relationships are fully or recursively resolved, so you can also
  11. // access nested relationships. This means that you can also access the
  12. // owner of the asset.
  13. var owner = tx.asset.owner;
  14. }

在这个例子中,不仅可以在交易中通过关联使用tx.asset引用特定的资产,还可以通过关联使用tx.asset.owner引用特定的参与者owner。在这种情况下,tx.asset.owner将被解析为引用一个特定的参与者。

交易处理器函数中的Promise返回

与关联类似,交易处理器函数将在提交交易之前等待承诺解决。如果承诺被拒绝,交易将失败。

在下面的示例代码中有几个承诺,在每个承诺都返回前交易将不会完成。

模型文件:

  1. namespace org.acme.sample
  2. transaction SampleTransaction {
  3. }

脚本文件:

  1. /**
  2. * Sample transaction processor function.
  3. * @param {org.acme.sample.SampleTransaction} tx The sample transaction instance.
  4. * @transaction
  5. */
  6. function sampleTransaction(tx) {
  7. // Transaction processor functions can return promises; Composer will wait
  8. // for the promise to be resolved before committing the transaction.
  9. // Do something that returns a promise.
  10. return Promise.resolve()
  11. .then(function () {
  12. // Do something else that returns a promise.
  13. return Promise.resolve();
  14. })
  15. .then(function () {
  16. // Do something else that returns a promise.
  17. // This transaction is complete only when this
  18. // promise is resolved.
  19. return Promise.resolve();
  20. });
  21. }

在交易处理器函数中使用API

Hyperledger Composer API可以在交易处理函数中调用,在下面的代码示例中,getAssetRegistry调用会返回一个在交易完成之前解决的承诺。

模型文件:

  1. namespace org.acme.sample
  2. asset SampleAsset identified by assetId {
  3. o String assetId
  4. o String value
  5. }
  6. transaction SampleTransaction {
  7. --> SampleAsset asset
  8. o String newValue
  9. }

脚本文件:

  1. /**
  2. * Sample transaction processor function.
  3. * @param {org.acme.sample.SampleTransaction} tx The sample transaction instance.
  4. * @transaction
  5. */
  6. function sampleTransaction(tx) {
  7. // Update the value in the asset.
  8. var asset = tx.asset;
  9. asset.value = tx.newValue;
  10. // Get the asset registry that stores the assets. Note that
  11. // getAssetRegistry() returns a promise, so we have to return
  12. // the promise so that Composer waits for it to be resolved.
  13. return getAssetRegistry('org.acme.sample.SampleAsset')
  14. .then(function (assetRegistry) {
  15. // Update the asset in the asset registry. Again, note
  16. // that update() returns a promise, so so we have to return
  17. // the promise so that Composer waits for it to be resolved.
  18. return assetRegistry.update(asset);
  19. })
  20. }

接下来是什么?

交易处理函数也可以用来:

  • 定义查询以从couchDB数据库获取有关区块链世界状态的信息。

  • 定义将事件数据发送给应用程序的事件。