-
Notifications
You must be signed in to change notification settings - Fork 885
feat(indexer): push result cap and ordering into KV tx search (PLT-748) #3708
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
Changes from all commits
be4680a
00f2454
e93d170
275cbac
759d1b3
c449d5c
bce4f91
8b8bd8a
7926505
ce16702
8a1e43c
066b1bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,6 @@ import ( | |
| "context" | ||
| "errors" | ||
| "fmt" | ||
| "sort" | ||
|
|
||
| tmquery "github.com/sei-protocol/sei-chain/sei-tendermint/internal/pubsub/query" | ||
| "github.com/sei-protocol/sei-chain/sei-tendermint/internal/state/indexer" | ||
|
|
@@ -65,40 +64,39 @@ func (env *Environment) TxSearch(ctx context.Context, req *coretypes.RequestTxSe | |
| return nil, err | ||
| } | ||
|
|
||
| // Validate order_by up front so we can push the ordering (and the result | ||
| // cap) down into the indexer; a broad query is then bounded at the scan | ||
| // path rather than after materializing and sorting the full match set. | ||
| var orderDesc bool | ||
| switch req.OrderBy { | ||
| case DescendingOrder, "": | ||
| orderDesc = true | ||
|
|
||
| case AscendingOrder: | ||
| orderDesc = false | ||
|
|
||
| default: | ||
| return nil, fmt.Errorf("expected order_by to be either `asc` or `desc` or empty: %w", coretypes.ErrInvalidRequest) | ||
| } | ||
|
|
||
| for _, sink := range env.EventSinks { | ||
| if sink.Type() == indexer.KV { | ||
| // TODO(PLT-748): the kv tx indexer currently ignores these opts and | ||
| // the cap below still applies after sort (PLT-700). Once the tx | ||
| // scan path is bounded like the block indexer, this pushes the cap | ||
| // and ordering down to the scan. | ||
| results, err := sink.SearchTxEvents(ctx, q, indexer.SearchOptions{ | ||
| Limit: env.Config.MaxTxSearchResults, | ||
| OrderDesc: req.OrderBy != AscendingOrder, | ||
| OrderDesc: orderDesc, | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| // sort results (must be done before cap and pagination) | ||
| switch req.OrderBy { | ||
| case DescendingOrder, "": | ||
| 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 AscendingOrder: | ||
| 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, fmt.Errorf("expected order_by to be either `asc` or `desc` or empty: %w", coretypes.ErrInvalidRequest) | ||
| } | ||
| // Results already arrive ordered by (height, index) per orderDesc: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [blocker] Removing the RPC-layer sort breaks the existing (unchanged) |
||
| // the kv indexer either sorts the materialized set or scans its | ||
| // order-preserving secondary index in order_by order, so no re-sort | ||
| // is needed here. | ||
|
|
||
| // Safety net: the kv indexer already bounds to MaxTxSearchResults, | ||
| // but keep the cap so the response stays bounded for any sink that | ||
| // ignores the limit. | ||
|
claude[bot] marked this conversation as resolved.
|
||
| if max := env.Config.MaxTxSearchResults; max > 0 && len(results) > max { | ||
| results = results[:max] | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[blocker] Removing the RPC-layer re-sort breaks the existing TestTxSearchCapAppliedAfterSort (rpc/core/tx_test.go). That test's mock EventSink returns results in ascending height order regardless of SearchOptions and relied on TxSearch to sort before capping. With the sort gone, an OrderBy=desc request now caps the unsorted prefix ([1..5]) instead of the top-N ([20,19,18,17,16]), so the test's assertion fails and CI breaks. Update the test's mock to honor OrderDesc (or move the top-N assertion to the indexer-level tests) as part of this PR.