Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: txs_by_height rpc api #151

Merged
merged 3 commits into from
Dec 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lite2/rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,10 @@ func (c *Client) TxSearch(query string, prove bool, page, perPage int, orderBy s
return c.next.TxSearch(query, prove, page, perPage, orderBy)
}

func (c *Client) TxsByHeight(height int64, prove bool, orderBy string) (*ctypes.ResultTxSearch, error) {
return c.next.TxsByHeight(height, prove, orderBy)
}

// Validators fetches and verifies validators.
//
// WARNING: only full validator sets are verified (when length of validators is
Expand Down
14 changes: 14 additions & 0 deletions rpc/client/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,20 @@ func (c *baseRPCClient) TxSearch(query string, prove bool, page, perPage int, or
return result, nil
}

func (c *baseRPCClient) TxsByHeight(height int64, prove bool, orderBy string) (*ctypes.ResultTxSearch, error) {
result := new(ctypes.ResultTxSearch)
params := map[string]interface{}{
"height": height,
"prove": prove,
"order_by": orderBy,
}
_, err := c.caller.Call("txs_by_height", params, result)
if err != nil {
return nil, errors.Wrap(err, "TxsByHeight")
}
return result, nil
}

func (c *baseRPCClient) Validators(height *int64, page, perPage int) (*ctypes.ResultValidators, error) {
result := new(ctypes.ResultValidators)
_, err := c.caller.Call("validators", map[string]interface{}{
Expand Down
1 change: 1 addition & 0 deletions rpc/client/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type SignClient interface {
Validators(height *int64, page, perPage int) (*ctypes.ResultValidators, error)
Tx(hash []byte, prove bool) (*ctypes.ResultTx, error)
TxSearch(query string, prove bool, page, perPage int, orderBy string) (*ctypes.ResultTxSearch, error)
TxsByHeight(height int64, prove bool, orderBy string) (*ctypes.ResultTxSearch, error)
}

// HistoryClient provides access to data from genesis to now in large chunks.
Expand Down
4 changes: 4 additions & 0 deletions rpc/client/local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,10 @@ func (c *Local) TxSearch(query string, prove bool, page, perPage int, orderBy st
return core.TxSearch(c.ctx, query, prove, page, perPage, orderBy)
}

func (c *Local) TxsByHeight(height int64, prove bool, orderBy string) (*ctypes.ResultTxSearch, error) {
return core.TxsByHeight(c.ctx, height, prove, orderBy)
}

func (c *Local) BroadcastEvidence(ev types.Evidence) (*ctypes.ResultBroadcastEvidence, error) {
return core.BroadcastEvidence(c.ctx, ev)
}
Expand Down
30 changes: 30 additions & 0 deletions rpc/client/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,36 @@ func TestTxSearch(t *testing.T) {
}
}

func TestTxsByHeight(t *testing.T) {
c := getHTTPClient()

heights := make([]int64, 10)

// first we broadcast a few txs
for i := 0; i < 10; i++ {
_, _, tx := MakeTxKV()
result, err := c.BroadcastTxCommit(tx)
require.NoError(t, err)
heights[i] = result.Height
}

// not prove and orderBy asc
for _, height := range heights {
res, err := c.TxsByHeight(height, false, "asc")
require.NoError(t, err)
require.Equal(t, 1, res.TotalCount)
require.Equal(t, 1, len(res.Txs))
}

// prove and orderBy desc
for _, height := range heights {
res, err := c.TxsByHeight(height, true, "desc")
require.NoError(t, err)
require.Equal(t, 1, res.TotalCount)
require.Equal(t, 1, len(res.Txs))
}
}

func deepcpVote(vote *types.Vote) (res *types.Vote) {
res = &types.Vote{
ValidatorAddress: make([]byte, len(vote.ValidatorAddress)),
Expand Down
1 change: 1 addition & 0 deletions rpc/core/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var Routes = map[string]*rpc.RPCFunc{
"commit": rpc.NewRPCFunc(Commit, "height"),
"tx": rpc.NewRPCFunc(Tx, "hash,prove"),
"tx_search": rpc.NewRPCFunc(TxSearch, "query,prove,page,per_page,order_by"),
"txs_by_height": rpc.NewRPCFunc(TxsByHeight, "height,prove,order_by"),
"validators": rpc.NewRPCFunc(Validators, "height,page,per_page"),
"dump_consensus_state": rpc.NewRPCFunc(DumpConsensusState, ""),
"consensus_state": rpc.NewRPCFunc(ConsensusState, ""),
Expand Down
61 changes: 61 additions & 0 deletions rpc/core/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,64 @@ func TxSearch(ctx *rpctypes.Context, query string, prove bool, page, perPage int

return &ctypes.ResultTxSearch{Txs: apiResults, TotalCount: totalCount}, nil
}

func TxsByHeight(ctx *rpctypes.Context, height int64, prove bool, orderBy string) (*ctypes.ResultTxSearch, error) {
// if index is disabled, return error
if _, ok := env.TxIndexer.(*null.TxIndex); ok {
return nil, errors.New("transaction indexing is disabled")
}

q, err := tmquery.New(fmt.Sprintf("tx.height=%d", height))
if err != nil {
return nil, err
}

results, err := env.TxIndexer.Search(ctx.Context(), q)
if err != nil {
return nil, err
}

// sort results
switch orderBy {
case "desc":
sort.Slice(results, func(i, j int) bool {
return results[i].Index > results[j].Index
})
case "asc", "":
sort.Slice(results, func(i, j int) bool {
return results[i].Index < results[j].Index
})
default:
return nil, errors.New("expected order_by to be either `asc` or `desc` or empty")
}

totalCount := len(results)

var block *types.Block
if prove {
block = env.BlockStore.LoadBlock(height)
}

apiResults := make([]*ctypes.ResultTx, 0, totalCount)
for _, r := range results {

var proof types.TxProof
if prove {
if block == nil {
panic("block must not be nil")
}
proof = block.Data.Txs.Proof(int(r.Index)) // XXX: overflow on 32-bit machines
}

apiResults = append(apiResults, &ctypes.ResultTx{
Hash: r.Tx.Hash(),
Height: r.Height,
Index: r.Index,
TxResult: r.Result,
Tx: r.Tx,
Proof: proof,
})
}

return &ctypes.ResultTxSearch{Txs: apiResults, TotalCount: totalCount}, nil
}