add tx_search RPC endpoint

This commit is contained in:
Anton Kaliaev 2017-11-28 14:12:04 -06:00
parent ea0b205455
commit 3e577ccf4f
No known key found for this signature in database
GPG Key ID: 7B6881D965918214
3 changed files with 45 additions and 0 deletions

View File

@ -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, ""),

View File

@ -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
}

View File

@ -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)
}
//----------------------------------------------------