Skip to content

Conversation

@tomerweller
Copy link
Contributor

@tomerweller tomerweller commented Dec 20, 2025

Summary

  • Adds optional order field to getEvents pagination options
  • Supports "asc" (default) and "desc" values
  • Enables efficient querying of N most recent events without scanning entire retention window
  • Fully backwards compatible - defaults to ascending order when not specified

Dependencies

This PR depends on: stellar/go-stellar-sdk#5888

The go.mod currently uses a local replace directive pointing to the go-stellar-sdk branch. Once the go-stellar-sdk PR is merged, this will be updated to use the released version.

Changes

  • Update EventReader.GetEvents() to accept order parameter
  • Update handler to use protocol.EventOrder types from go-stellar-sdk
  • Implement descending order query in DB layer (ORDER BY id DESC)
  • Add comprehensive tests for new functionality

Usage

{
  "startLedger": 1000,
  "pagination": {
    "limit": 10,
    "order": "desc"
  },
  "filters": [...]
}

Test plan

  • Existing tests pass
  • New test: descending order returns events in reverse order
  • New test: descending order with limit returns N newest events
  • New test: invalid order parameter returns validation error
  • New test: explicitly set ascending order works correctly

Closes #575

🤖 Generated with Claude Code

This adds an optional `order` field to the getEvents pagination options,
allowing clients to retrieve events in descending order (newest first).

Changes:
- Update DB layer to accept order parameter for query direction
- Update handler to use protocol.EventOrder types
- Add tests for descending order, limit with descending, and invalid order

This feature enables efficient querying of the N most recent events
without scanning the entire retention window. The implementation is
fully backwards compatible - order defaults to "asc" when not specified.

Depends on: stellar/go-stellar-sdk#5888
Closes stellar#575

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@tomerweller tomerweller force-pushed the feature/getevents-order-parameter branch 2 times, most recently from 6082035 to d377b79 Compare December 21, 2025 20:55
For DESC order, startLedger now acts as the upper bound (where we start
scanning backwards from) and endLedger acts as the lower bound.

This makes DESC order intuitive for 'get N most recent events' use case:
- Set startLedger to the maximum ledger you want
- Results will include events up to and including startLedger
- Events are returned newest first

For ASC order (default), behavior is unchanged:
- startLedger is the lower bound (where we start scanning forwards from)
- endLedger is the upper bound
gopkg.in/yaml.v3 v3.0.1 // indirect
)

replace github.com/stellar/go-stellar-sdk => ../go-stellar-sdk
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uh what

var cursorRange protocol.CursorRange
var validationLedger uint32 // The ledger to validate against retention window

if isDescending {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not like the complexity that this "feature" introduces for to an already complicated and fragile method. Can you provide a use case where it is valuable to see events in descending order? By their very nature events are meant to be consumed in chronological order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add order parameter to getEvents for descending order support

2 participants