Skip to content

Use GetLedgerRaw in getLatestLedger endpoint - #941

Merged
cjonas9 merged 11 commits into
mainfrom
optimize-GetLatestLedger
Aug 22, 2026
Merged

Use GetLedgerRaw in getLatestLedger endpoint#941
cjonas9 merged 11 commits into
mainfrom
optimize-GetLatestLedger

Conversation

@cjonas9

@cjonas9 cjonas9 commented Aug 21, 2026

Copy link
Copy Markdown
Contributor

What

Uses the new raw-bytes patterning used in getLedger in getLatestLedger:

  • Add GetLedgerRaw(ctx, sequence) ([]byte, xdr.LedgerHeaderHistoryEntry, bool, error) to LedgerReader
    • Returns raw bytes of ledger meta, the header, and whether it was found/if the request error'd
  • Updates the getLatestLedger handler to call GetLedgerRaw
  • Updates mocks/tests with GetLedgerRaw

Why

getLatestLedger currently wastes time calling GetLedgers to get a single LedgerCloseMeta, which causes an Unmarshal to XDR. The handler then marshals this XDR to binary. On each call, we pay for a ~2.8Mb Unmarshal and Marshal that can be completely bypassed.

Known limitations

N/A

Copilot AI balanced review requested due to automatic review settings August 21, 2026 19:18

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Optimizes getLatestLedger by returning stored ledger metadata bytes directly, avoiding unnecessary XDR round-trips.

Changes:

  • Adds GetLedgerRaw to ledger readers and mocks.
  • Reuses raw header parsing in database reads.
  • Updates the endpoint and tests for raw metadata.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
cmd/stellar-rpc/internal/db/ledger.go Adds raw ledger retrieval and header parsing.
cmd/stellar-rpc/internal/db/mocks.go Implements raw retrieval in the database mock.
cmd/stellar-rpc/internal/methods/get_latest_ledger.go Builds responses from raw metadata and parsed headers.
cmd/stellar-rpc/internal/methods/get_latest_ledger_test.go Updates test reader behavior for raw metadata.
cmd/stellar-rpc/internal/methods/mocks.go Extends the methods mock with GetLedgerRaw.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread cmd/stellar-rpc/internal/db/ledger.go Outdated
Comment thread cmd/stellar-rpc/internal/db/ledger.go Outdated
Comment thread cmd/stellar-rpc/internal/db/ledger.go Outdated
Comment thread cmd/stellar-rpc/internal/db/ledger.go
Comment thread cmd/stellar-rpc/internal/db/ledger.go Outdated
Comment thread cmd/stellar-rpc/internal/db/ledger.go Outdated
Comment thread cmd/stellar-rpc/internal/db/ledger.go Outdated
Comment thread cmd/stellar-rpc/internal/db/ledger.go Outdated
Comment thread cmd/stellar-rpc/internal/db/ledger.go
@cjonas9
cjonas9 merged commit 58cdd33 into main Aug 22, 2026
17 checks passed
@cjonas9
cjonas9 deleted the optimize-GetLatestLedger branch August 22, 2026 00:08
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.

getLatestLedger: apply the same raw-bytes optimization that getLedgers already uses

3 participants