在 Hyperledger Composer 事务中以确定性方式获取时间戳

Getting timestamps in deterministic way in Hyperledger Composer transactions

是否有确定性的方法来获取事务函数中的时间戳,类似于可以在 Go 版本的 Fabric 链代码中使用的 stub.GetTxTimestamp()。

所有交易都有一个名为 timestamp 的系统 属性,因此您可以使用 myTransaction.timestamp

只是分享一个适用于 basic-sample-network 网络的示例:

在模型文件 (lib/org.acme.sample.cto) 中,我扩展了 SampleAsset 定义任何添加的新 属性 称为 timestamp 类型 DateTime:

asset SampleAsset identified by assetId {
  o String assetId
  --> SampleParticipant owner
  o String value
  o DateTime timestamp
}

在脚本文件 (lib/logic.js) 中,onSampleTransaction 函数使用当前事务的时间戳更新 SampleAsset 的 timestamp

function onSampleTransaction(sampleTransaction) {
  sampleTransaction.asset.value = sampleTransaction.newValue;
  sampleTransaction.asset.timestamp = sampleTransaction.timestamp;
  return getAssetRegistry('org.acme.sample.SampleAsset')
       .then(function (assetRegistry) {
               return assetRegistry.update(sampleTransaction.asset);
        });
}

我们不能使用供应商文件夹中的原型...

https://github.com/hyperledger-archives/fabric/issues/1832