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

☆樱花仙子☆ 提交于 2020-01-03 06:19:32

问题


I need to calculate the difference between "the timestamp when transaction is submitted" and "the timestamp when transaction is committed". Is it possible to get the tx committed (or completed) timestamp in fabric?

I tried to use Hyperledger Explorer running on my composer channel. I can see tx timestamp inside the block. But I am not sure whether it is creation or committing timestamp. Also, can I convert explorer timestamp to ISO 8601 format?

Please help me on this.


回答1:


The timestamp you see is the endorsement time timestamp, you can get access to it during chaincode invocation by using following 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)

If you would like to compute the difference between two events, I'd suggest to leverage events hub to get direct notification of transaction being committed. You basically need to subscribe to events waiting for your transaction then you will be able to compute the difference, note you need to compute average request RTT to deduct it from the final result to get more accurate evaluation. To see how you can get subscribed to events you can see an example here (Deprecated)block_listener.go.




回答2:


I changed blockchain-explorer/main.js code to convert time format.

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({ })
}});

TO

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({ })
}});


来源:https://stackoverflow.com/questions/48369186/how-to-get-timestamp-when-transaction-is-committed-or-completed-in-hyperledger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!