From f4cb62fe861478b1c575e4b1975a825a3b9bf1f1 Mon Sep 17 00:00:00 2001 From: Pham Tu Date: Tue, 27 Jun 2023 16:11:09 +0700 Subject: [PATCH] add timestamp support for tx_search --- rpc/core/tx.go | 24 +++++++++++++++++------- rpc/core/types/responses.go | 13 +++++++------ 2 files changed, 24 insertions(+), 13 deletions(-) diff --git a/rpc/core/tx.go b/rpc/core/tx.go index fe24f5c17bc..e4f56789648 100644 --- a/rpc/core/tx.go +++ b/rpc/core/tx.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "sort" + "time" cmtmath "github.com/tendermint/tendermint/libs/math" cmtquery "github.com/tendermint/tendermint/libs/pubsub/query" @@ -112,22 +113,31 @@ func TxSearch( pageSize := cmtmath.MinInt(perPage, totalCount-skipCount) apiResults := make([]*ctypes.ResultTx, 0, pageSize) + + blocks := make(map[int64]*types.Block) + for i := skipCount; i < skipCount+pageSize; i++ { r := results[i] + if _, ok := blocks[r.Height]; !ok { + blocks[r.Height] = env.BlockStore.LoadBlock(r.Height) + } + + block := blocks[r.Height] + var proof types.TxProof if prove { - block := env.BlockStore.LoadBlock(r.Height) proof = block.Data.Txs.Proof(int(r.Index)) // XXX: overflow on 32-bit machines } apiResults = append(apiResults, &ctypes.ResultTx{ - Hash: types.Tx(r.Tx).Hash(), - Height: r.Height, - Index: r.Index, - TxResult: r.Result, - Tx: r.Tx, - Proof: proof, + Hash: types.Tx(r.Tx).Hash(), + Height: r.Height, + Index: r.Index, + TxResult: r.Result, + Timestamp: block.Time.Format(time.RFC3339), + Tx: r.Tx, + Proof: proof, }) } diff --git a/rpc/core/types/responses.go b/rpc/core/types/responses.go index c6ed6eb1999..d964c9154ab 100644 --- a/rpc/core/types/responses.go +++ b/rpc/core/types/responses.go @@ -191,12 +191,13 @@ type ResultCheckTx struct { // Result of querying for a tx type ResultTx struct { - Hash bytes.HexBytes `json:"hash"` - Height int64 `json:"height"` - Index uint32 `json:"index"` - TxResult abci.ResponseDeliverTx `json:"tx_result"` - Tx types.Tx `json:"tx"` - Proof types.TxProof `json:"proof,omitempty"` + Hash bytes.HexBytes `json:"hash"` + Height int64 `json:"height"` + Index uint32 `json:"index"` + TxResult abci.ResponseDeliverTx `json:"tx_result"` + Timestamp string `json:"timestamp,omitempty"` + Tx types.Tx `json:"tx"` + Proof types.TxProof `json:"proof,omitempty"` } // Result of searching for txs