Skip to content

Cache preflight snapshot source lookups#786

Open
mihaieremia wants to merge 2 commits into
stellar:mainfrom
mihaieremia:codex/preflight-external-storage-cache
Open

Cache preflight snapshot source lookups#786
mihaieremia wants to merge 2 commits into
stellar:mainfrom
mihaieremia:codex/preflight-external-storage-cache

Conversation

@mihaieremia

@mihaieremia mihaieremia commented Jun 14, 2026

Copy link
Copy Markdown

What

Adds a per-preflight cache around SnapshotSourceGet, including cached misses, with focused unit coverage for cache hits and misses.

Why

External read-only storage can make one simulated transaction read the same target market, governance, reserve, oracle, or contract-instance keys repeatedly. In lending-style flows, a controller may touch several supplied and borrowed assets while repeatedly checking the same market configuration or oracle state.

Preflight already routes Rust host ledger-entry requests through SnapshotSourceGet. Caching those lookups for the duration of one preflight avoids repeated Go/Rust boundary work and repeated ledger-entry getter calls for the same keys. This is especially useful for external instance reads because the host records the target contract instance entry as the footprint key.

This does not change production transaction semantics, ledger state, auth, footprint contents, or result formatting. It only avoids duplicate snapshot fetch work during simulation/preflight.

Known limitations

  • Cache lifetime is one preflight only.
  • This PR does not add the external-storage ABI to RPC by itself; it is a simulation/preflight companion optimization for the env and SDK PRs.
  • Full external-storage simulation assertions should be added once RPC consumes an env/preflight dependency that includes the new protocol-28 ABI.

Validation

  • go test ./cmd/stellar-rpc/internal/preflight -run 'TestSnapshotSourceGetCaches|TestGetPreflight'
  • gofmt -l cmd/stellar-rpc/internal/preflight
  • git diff --check

@mihaieremia mihaieremia marked this pull request as ready for review June 14, 2026 17:34
Copilot AI review requested due to automatic review settings June 14, 2026 17:34

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

Note

Copilot was unable to run its full agentic suite in this review.

This PR adds an in-memory cache to SnapshotSourceGet to avoid repeated ledger entry lookups during preflight, and introduces tests to verify both hit and miss caching behavior.

Changes:

  • Introduced a per-handle cache for ledger entry + TTL lookups in snapshotSourceHandle.
  • Refactored snapshot source handle creation into newSnapshotSourceHandle.
  • Added tests ensuring repeated lookups (including missing entries) only call the underlying getter once.

Reviewed changes

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

File Description
cmd/stellar-rpc/internal/preflight/preflight.go Adds snapshot cache used by SnapshotSourceGet and wires handle creation through a helper.
cmd/stellar-rpc/internal/preflight/preflight_test.go Adds tests and a counting getter wrapper to validate caching behavior.
Comments suppressed due to low confidence (1)

cmd/stellar-rpc/internal/preflight/preflight.go:1

  • The cgo.Handle stores and retrieves snapshotSourceHandle by value, which causes copies on retrieval and on the value-receiver call to getLedgerEntryAndTTL. Storing a *snapshotSourceHandle in the handle (and type-asserting to a pointer) avoids accidental copy hazards if fields change in the future and makes it clearer that the cache is shared/mutable state.
//nolint:lll // CGO LDFLAG definitions are long

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 122 to 125
if len(entries) > 1 {
h.logger.WithError(err).Error("SnapshotSourceGet(): GetLedgerEntries() returned more than one entry")
return C.ledger_entry_and_ttl_t{}
return cachedLedgerEntryAndTTL{}
}
Comment on lines +47 to +50
type snapshotSourceCache struct {
mu sync.Mutex
entries map[string]cachedLedgerEntryAndTTL
}
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.

2 participants