From 3e577ccf4f9125bfecdd78f7cf82e12d1af3a8ec Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Tue, 28 Nov 2017 14:12:04 -0600 Subject: [PATCH] add `tx_search` RPC endpoint --- rpc/core/routes.go | 1 + rpc/core/tx.go | 40 ++++++++++++++++++++++++++++++++++++++++ state/txindex/indexer.go | 4 ++++ 3 files changed, 45 insertions(+) diff --git a/rpc/core/routes.go b/rpc/core/routes.go index a4328f1d2..2ae352c1a 100644 --- a/rpc/core/routes.go +++ b/rpc/core/routes.go @@ -19,6 +19,7 @@ var Routes = map[string]*rpc.RPCFunc{ "block": rpc.NewRPCFunc(Block, "height"), "commit": rpc.NewRPCFunc(Commit, "height"), "tx": rpc.NewRPCFunc(Tx, "hash,prove"), + "tx_search": rpc.NewRPCFunc(Tx, "query,prove"), "validators": rpc.NewRPCFunc(Validators, "height"), "dump_consensus_state": rpc.NewRPCFunc(DumpConsensusState, ""), "unconfirmed_txs": rpc.NewRPCFunc(UnconfirmedTxs, ""), diff --git a/rpc/core/tx.go b/rpc/core/tx.go index 80d1cb32e..3609c05d9 100644 --- a/rpc/core/tx.go +++ b/rpc/core/tx.go @@ -6,6 +6,7 @@ import ( ctypes "github.com/tendermint/tendermint/rpc/core/types" "github.com/tendermint/tendermint/state/txindex/null" "github.com/tendermint/tendermint/types" + tmquery "github.com/tendermint/tmlibs/pubsub/query" ) // Tx allows you to query the transaction results. `nil` could mean the @@ -99,3 +100,42 @@ func Tx(hash []byte, prove bool) (*ctypes.ResultTx, error) { Proof: proof, }, nil } + +func TxSearch(query string, prove bool) ([]*ctypes.ResultTx, error) { + // if index is disabled, return error + if _, ok := txIndexer.(*null.TxIndex); ok { + return nil, fmt.Errorf("Transaction indexing is disabled.") + } + + q, err := tmquery.New(query) + if err != nil { + return []*ctypes.ResultTx{}, err + } + + results, err := txIndexer.Search(q) + if err != nil { + return []*ctypes.ResultTx{}, err + } + + apiResults := make([]*ctypes.ResultTx, len(results)) + for i, r := range results { + height := r.Height + index := r.Index + + var proof types.TxProof + if prove { + block := blockStore.LoadBlock(int(height)) + proof = block.Data.Txs.Proof(int(index)) + } + + apiResults[i] = &ctypes.ResultTx{ + Height: height, + Index: index, + TxResult: r.Result, + Tx: r.Tx, + Proof: proof, + } + } + + return apiResults, nil +} diff --git a/state/txindex/indexer.go b/state/txindex/indexer.go index f9908f326..07a544bd9 100644 --- a/state/txindex/indexer.go +++ b/state/txindex/indexer.go @@ -4,6 +4,7 @@ import ( "errors" "github.com/tendermint/tendermint/types" + "github.com/tendermint/tmlibs/pubsub/query" ) // TxIndexer interface defines methods to index and search transactions. @@ -18,6 +19,9 @@ type TxIndexer interface { // Get returns the transaction specified by hash or nil if the transaction is not indexed // or stored. Get(hash []byte) (*types.TxResult, error) + + // Search allows you to query for transactions. + Search(q *query.Query) ([]*types.TxResult, error) } //----------------------------------------------------