Browse Source

add `tx_search` RPC endpoint

pull/835/head
Anton Kaliaev 7 years ago
parent
commit
3e577ccf4f
No known key found for this signature in database GPG Key ID: 7B6881D965918214
3 changed files with 45 additions and 0 deletions
  1. +1
    -0
      rpc/core/routes.go
  2. +40
    -0
      rpc/core/tx.go
  3. +4
    -0
      state/txindex/indexer.go

+ 1
- 0
rpc/core/routes.go View File

@ -19,6 +19,7 @@ var Routes = map[string]*rpc.RPCFunc{
"block": rpc.NewRPCFunc(Block, "height"), "block": rpc.NewRPCFunc(Block, "height"),
"commit": rpc.NewRPCFunc(Commit, "height"), "commit": rpc.NewRPCFunc(Commit, "height"),
"tx": rpc.NewRPCFunc(Tx, "hash,prove"), "tx": rpc.NewRPCFunc(Tx, "hash,prove"),
"tx_search": rpc.NewRPCFunc(Tx, "query,prove"),
"validators": rpc.NewRPCFunc(Validators, "height"), "validators": rpc.NewRPCFunc(Validators, "height"),
"dump_consensus_state": rpc.NewRPCFunc(DumpConsensusState, ""), "dump_consensus_state": rpc.NewRPCFunc(DumpConsensusState, ""),
"unconfirmed_txs": rpc.NewRPCFunc(UnconfirmedTxs, ""), "unconfirmed_txs": rpc.NewRPCFunc(UnconfirmedTxs, ""),


+ 40
- 0
rpc/core/tx.go View File

@ -6,6 +6,7 @@ import (
ctypes "github.com/tendermint/tendermint/rpc/core/types" ctypes "github.com/tendermint/tendermint/rpc/core/types"
"github.com/tendermint/tendermint/state/txindex/null" "github.com/tendermint/tendermint/state/txindex/null"
"github.com/tendermint/tendermint/types" "github.com/tendermint/tendermint/types"
tmquery "github.com/tendermint/tmlibs/pubsub/query"
) )
// Tx allows you to query the transaction results. `nil` could mean the // 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, Proof: proof,
}, nil }, 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
}

+ 4
- 0
state/txindex/indexer.go View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"github.com/tendermint/tendermint/types" "github.com/tendermint/tendermint/types"
"github.com/tendermint/tmlibs/pubsub/query"
) )
// TxIndexer interface defines methods to index and search transactions. // 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 // Get returns the transaction specified by hash or nil if the transaction is not indexed
// or stored. // or stored.
Get(hash []byte) (*types.TxResult, error) Get(hash []byte) (*types.TxResult, error)
// Search allows you to query for transactions.
Search(q *query.Query) ([]*types.TxResult, error)
} }
//---------------------------------------------------- //----------------------------------------------------


Loading…
Cancel
Save