Cache preflight snapshot source lookups#786
Open
mihaieremia wants to merge 2 commits into
Open
Conversation
There was a problem hiding this comment.
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.Handlestores and retrievessnapshotSourceHandleby value, which causes copies on retrieval and on the value-receiver call togetLedgerEntryAndTTL. Storing a*snapshotSourceHandlein 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 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Validation
go test ./cmd/stellar-rpc/internal/preflight -run 'TestSnapshotSourceGetCaches|TestGetPreflight'gofmt -l cmd/stellar-rpc/internal/preflightgit diff --check