如何在 Hyperledger Fabric 中提交(或完成)事务时获取时间戳

How to get timestamp when transaction is committed (or completed) in Hyperledger Fabric

我需要计算 "the timestamp when transaction is submitted" 和 "the timestamp when transaction is committed" 之间的差值。是否有可能在 fabric 中获取 tx 提交(或完成)时间戳?

我尝试在我的作曲频道上使用 Hyperledger Explorer 运行。我可以在块内看到 tx 时间戳。但是我不确定是创建还是提交时间戳。另外,我可以将资源管理器时间戳转换为 ISO 8601 格式吗?

请帮我解决这个问题。

我更改了blockchain-explorer/main.js代码来转换时间格式。

app.post("/api/tx/getinfo", function(req, res) {

let  txid = req.body.txid
if( txid != '0' ){
query.getTransactionByID(peer,ledgerMgr.getCurrChannel(),txid,org).then(response_payloads=>{

    var header = response_payloads['transactionEnvelope']['payload']['header']
    var data = response_payloads['transactionEnvelope']['payload']['data']
    var signature = response_payloads['transactionEnvelope']['signature'].toString("hex")
    res.send({
        'tx_id':header.channel_header.tx_id,
        'timestamp':header.channel_header.timestamp,
        'channel_id':header.channel_header.channel_id,
        'type':header.channel_header.type,
    })
})

}else{
    res.send({ })
}});

app.post("/api/tx/getinfo", function(req, res) {

let  txid = req.body.txid
if( txid != '0' ){
query.getTransactionByID(peer,ledgerMgr.getCurrChannel(),txid,org).then(response_payloads=>{

    var header = response_payloads['transactionEnvelope']['payload']['header']
    var data = response_payloads['transactionEnvelope']['payload']['data']
    var signature = response_payloads['transactionEnvelope']['signature'].toString("hex")
    res.send({
        'tx_id':header.channel_header.tx_id,
        'timestamp':new Date(header.channel_header.timestamp).toISOString(),
        'channel_id':header.channel_header.channel_id,
        'type':header.channel_header.type,
    })
})

}else{
    res.send({ })
}});

您看到的时间戳是背书时间戳,您可以在链码调用期间通过以下方式访问它 API:

// GetTxTimestamp returns the timestamp when the transaction was created. This
// is taken from the transaction ChannelHeader, therefore it will indicate the
// client's timestamp, and will have the same value across all endorsers.
GetTxTimestamp() (*timestamp.Timestamp, error)

如果您想计算两个事件之间的差异,我建议您利用事件中心来获取正在提交的事务的直接通知。您基本上需要订阅等待您的事务的事件,然后您将能够计算差异,注意您需要计算平均请求 RTT 以从最终结果中扣除它以获得更准确的评估。要了解如何订阅事件,您可以在此处查看示例 (Deprecated)block_listener.go