Skip to content

Commit

Permalink
initial attempt at pagination prior to fetching all txs
Browse files Browse the repository at this point in the history
  • Loading branch information
czarcas7ic committed Apr 20, 2024
1 parent 634d970 commit 38ce829
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 108 deletions.
50 changes: 15 additions & 35 deletions rpc/core/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package core
import (
"errors"
"fmt"
"sort"

cmtmath "github.com/cometbft/cometbft/libs/math"
cmtquery "github.com/cometbft/cometbft/libs/pubsub/query"
ctypes "github.com/cometbft/cometbft/rpc/core/types"
rpctypes "github.com/cometbft/cometbft/rpc/jsonrpc/types"
Expand Down Expand Up @@ -70,47 +68,29 @@ func TxSearch(
return nil, err
}

results, err := env.TxIndexer.Search(ctx.Context(), q)
if err != nil {
return nil, err
// Validate number of results per page
perPage := validatePerPage(perPagePtr)
if pagePtr == nil {
// Default to page 1 if not specified
pagePtr = new(int)
*pagePtr = 1
}

// sort results (must be done before pagination)
switch orderBy {
case "desc":
sort.Slice(results, func(i, j int) bool {
if results[i].Height == results[j].Height {
return results[i].Index > results[j].Index
}
return results[i].Height > results[j].Height
})
case "asc", "":
sort.Slice(results, func(i, j int) bool {
if results[i].Height == results[j].Height {
return results[i].Index < results[j].Index
}
return results[i].Height < results[j].Height
})
default:
return nil, errors.New("expected order_by to be either `asc` or `desc` or empty")
// Adjusted call to Search to include pagination parameters
results, totalCount, err := env.TxIndexer.Search(ctx.Context(), q, *pagePtr, perPage, orderBy)
if err != nil {
return nil, err
}

// paginate results
totalCount := len(results)
perPage := validatePerPage(perPagePtr)

page, err := validatePage(pagePtr, perPage, totalCount)
// Now that we know the total number of results, validate that the page
// requested is within bounds
_, err = validatePage(pagePtr, perPage, totalCount)
if err != nil {
return nil, err
}

skipCount := validateSkipCount(page, perPage)
pageSize := cmtmath.MinInt(perPage, totalCount-skipCount)

apiResults := make([]*ctypes.ResultTx, 0, pageSize)
for i := skipCount; i < skipCount+pageSize; i++ {
r := results[i]

apiResults := make([]*ctypes.ResultTx, 0, len(results))
for _, r := range results {
var proof types.TxProof
if prove {
block := env.BlockStore.LoadBlock(r.Height)
Expand Down
4 changes: 2 additions & 2 deletions state/indexer/sink/psql/backport.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ func (BackportTxIndexer) Get([]byte) (*abci.TxResult, error) {

// Search is implemented to satisfy the TxIndexer interface, but it is not
// supported by the psql event sink and reports an error for all inputs.
func (BackportTxIndexer) Search(context.Context, *query.Query) ([]*abci.TxResult, error) {
return nil, errors.New("the TxIndexer.Search method is not supported")
func (BackportTxIndexer) Search(ctx context.Context, q *query.Query, page, perPage int, orderBy string) ([]*abci.TxResult, int, error) {
return nil, 0, errors.New("the TxIndexer.Search method is not supported")
}

// BlockIndexer returns a bridge that implements the CometBFT v0.34 block
Expand Down
2 changes: 1 addition & 1 deletion state/txindex/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type TxIndexer interface {
Get(hash []byte) (*abci.TxResult, error)

// Search allows you to query for transactions.
Search(ctx context.Context, q *query.Query) ([]*abci.TxResult, error)
Search(ctx context.Context, q *query.Query, page, perPage int, orderBy string) ([]*abci.TxResult, int, error)
}

// Batch groups together multiple Index operations to be performed at the same time.
Expand Down
Loading

0 comments on commit 38ce829

Please sign in to comment.