Skip to content

Add tx-granular ledger snapshot source using meta, RPC, and archives - #1657

Draft
leighmcculloch wants to merge 79 commits into
mainfrom
snapshot-source-tx
Draft

Add tx-granular ledger snapshot source using meta, RPC, and archives#1657
leighmcculloch wants to merge 79 commits into
mainfrom
snapshot-source-tx

Conversation

@leighmcculloch

@leighmcculloch leighmcculloch commented Dec 19, 2025

Copy link
Copy Markdown
Member

What

Add new crates for fetching ledger entries from multiple data sources to enable more seamless and built-in fork testing in the sdk. Add soroban-ledger-snapshot-source-tx for transaction-level snapshot sources.

sequenceDiagram
    autonumber
    actor App as 
    participant SelTxMeta as Query<br/>Ledger<br/>Tx Meta
    participant LedgerMeta as Query<br/>Ledger – 1..N<br/>Tx Meta
    participant Archive as History<br/>Archive
    participant RPC as RPC

    App->>SelTxMeta: Lookup with key
    Note over App,SelTxMeta: If found → use it.<br/>If not → continue.
    
    App->>SelTxMeta: Look in prior txs in same ledger
    Note over App,SelTxMeta: If found → use it.<br/>If not → continue.

    opt Optionally use RPC
        App->>RPC: getLedgerEntries([key])
        Note over App,RPC: Use if (lastModified < queryLedger)<br/>AND (rpcLatestSeen >= queryLedger).<br/>Otherwise continue.
    end

    App->>LedgerMeta: Look in prior ledger meta
    Note over App,LedgerMeta: If found → use it.<br/>If checkpoint ledger not reached → continue to next ledger meta<br/>If checkpoint ledger is reached → continue to archive.

    App->>Archive: Download checkpoint from history archive and search
    Note over App,Archive: If found → use it.<br/>If not → does not exist.
Loading

Why

The current fork testing experience utilising the stellar-cli has low granularity only at the boundaries of ledgers, and requires downloading full history archives and manually identifying footprints ahead of time, which is difficult to do well and a poor developer experience. This change enables the SDK to lazily fetch ledger entries on-demand from the most efficient source available, caching results locally for subsequent runs. Developers will be able to fork test against any ledger and transaction without pre-identifying the footprint.

The change uses a Ledger Meta Storage (SEP-54), an RPC, and a History Archive to collect ledger entries.

The change caches results in three layers. All raw files downloaded are cached in the system cache directory and reused across tests, across workspaces. All ledger entries found are cached in the system cache directory. All ledger entries found for the current workspace are cached in the tests-snapshot-source directory intended to be committed so that CI runs reproducibly without needing to collect entries. Note that the format of that cache is not a ledger snapshot json file because for many tests running concurrently one file per ledger entry is easier to manage.

Close #1448

Try it out

Add the following dependencies to your Cargo.toml:

[dev-dependencies]
soroban-sdk = { version = "25.1.0", features = ["testutils"] }
soroban-ledger-snapshot-source-tx = { git = "https://github.com/stellar/rs-soroban-sdk", branch = "snapshot-source-tx" }
bytes-lit = "0.0.6"

Note: Requires soroban-sdk v23.4.0 or later.

Example

use bytes_lit::bytes;
use soroban_ledger_snapshot_source_tx::{Network, TxSnapshotSource};
use soroban_sdk::{token::TokenClient, Address, Env};

#[test]
fn test_fork_at_tx() {
    // The address of the native asset (test XLM) on testnet.
    const NATIVE_ADDRESS: &str = "CDLZFC3SYJYDZT7K67VZ75HPJVIEUVNIXF47ZG2FB2RMQQVU2HHGCYSC";
    
    // Create a snapshot source at ledger 8655, just BEFORE the specified tx.
    let source = TxSnapshotSource::new(
        Network::testnet(),
        8655,
        Some(bytes!(0x580bad1826d02b45634a3742049a23bf652fb2d4bc0814c83f2f58a7a9810ac9)),
    );
    
    // Setup the environment with the source.
    let env = Env::from_ledger_snapshot(source);
    let contract = Address::from_str(&env, NATIVE_ADDRESS);
    let client = TokenClient::new(&env, &contract);

    // Lookup the balance of the following address.
    let addr = Address::from_str(&env, "CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM");
    let bal = client.balance(&addr);
    assert_eq!(bal, 47);  // Balance just before the tx executed
}

Observing State Changes

The TxSnapshotSource looks up state with transaction-level granularity when a transaction hash is provided allowing developers to debug a transaction by starting at the point just before the transaction. For example:

Before a specific tx: Pass the tx hash to get state just before that tx executed:

let source = TxSnapshotSource::new(
    Network::testnet(),
    8655,
    Some(bytes!(0x580bad1826d02b45634a3742049a23bf652fb2d4bc0814c83f2f58a7a9810ac9)),
);
// Balance will be 47 (before this tx added 10)

End of ledger (after all txs): Use None for the tx_hash to get state at the end of the ledger:

let source = TxSnapshotSource::new(Network::testnet(), 8655, None);
// Balance will be 57 (after all txs in ledger 8655)

Different ledgers/txs: Update the test to investigate the state of the balance at each of the following ledgers and transactions to see how the transactions affected the balance. Check out the links to see the operations the transactions performed and how they align with the changes in balances observed.

Ledger TX HASH Change Bal
8511 51b21c588f3b397f957d3c14685debf96224c27c5a45b81b27eb5ee0a56517bf + 1 1
8613 52e9a67fdd78105ed84f6d073f50a464e14c1c587a5e4b45842e4b3386336751 + 2 3
8622 a5dd4e98df837da3b1a7de52e9698c1a28ae15973f52651c765f1654a3033993 + 3 6
8632 7a6143351fc48aed1b34d9d0b1690bfe11404cc10cc10f5d52ec06758d0f2b5b + 6 12
8633 d5f676598fd331867f728c4f1c28ea6045c2d31a80d48bfb873d8302816a6304 + 7 19
8633 34e420d6cf03ec2ad1560545f6f98ae281bbb6ac9837d23dbc44d158b2942a65 + 8 27
8655 52ec73f485fb132fb5777c6f2be6c0358e9587c64f4a9ef1f861b56923493400 +11 38
8655 ccede7115f962f3680dc7ff45b21e52e5ea02004f6c2e5b47cf35b853a91203a + 9 47
8655 580bad1826d02b45634a3742049a23bf652fb2d4bc0814c83f2f58a7a9810ac9 +10 57

Debugging with RUST_LOG

Enable logging to see which data sources are being queried and what entries are found:

RUST_LOG=soroban_ledger_fetch=debug cargo test -- --nocapture

Example output:

2025-12-19T14:56:01.070297Z  INFO soroban_ledger_fetch: fetch, key: {"contract_data":{"contract":"CDLZFC3SYJYDZT7K67VZ75HPJVIEUVNIXF47ZG2FB2RMQQVU2HHGCYSC","durability":"persistent","key":"ledger_key_contract_instance"}}
2025-12-19T14:56:01.070932Z DEBUG soroban_ledger_fetch: fetch from meta range, count: 16, first: 8655, last: 8640
2025-12-19T14:56:01.071069Z DEBUG soroban_ledger_fetch: fetch from meta, ledger: 8655
2025-12-19T14:56:01.990395Z DEBUG soroban_ledger_fetch: fetch from rpc, ledger: 8655
2025-12-19T14:56:02.900716Z DEBUG soroban_ledger_fetch: found from rpc, last_modified: 689, usable: true
2025-12-19T14:56:02.919258Z  INFO soroban_ledger_fetch: found, entry: {"data":{"contract_data":{"contract":"CDLZFC3SYJYDZT7K67VZ75HPJVIEUVNIXF47ZG2FB2RMQQVU2HHGCYSC","durability":"persistent","ext":"v0","key":"ledger_key_contract_instance","val":{"contract_instance":{"executable":"stellar_asset","storage":[{"key":{"symbol":"METADATA"},"val":{"map":[{"key":{"symbol":"decimal"},"val":{"u32":7}},{"key":{"symbol":"name"},"val":{"string":"native"}},{"key":{"symbol":"symbol"},"val":{"string":"native"}}]}},{"key":{"vec":[{"symbol":"AssetInfo"}]},"val":{"vec":[{"symbol":"Native"}]}}]}}}},"ext":"v0","last_modified_ledger_seq":689}
2025-12-19T14:56:02.941928Z  INFO soroban_ledger_fetch: fetch, key: {"contract_data":{"contract":"CDLZFC3SYJYDZT7K67VZ75HPJVIEUVNIXF47ZG2FB2RMQQVU2HHGCYSC","durability":"persistent","key":{"vec":[{"symbol":"Balance"},{"address":"CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM"}]}}}
2025-12-19T14:56:02.942165Z DEBUG soroban_ledger_fetch: fetch from meta range, count: 16, first: 8655, last: 8640
2025-12-19T14:56:02.942205Z DEBUG soroban_ledger_fetch: fetch from meta, ledger: 8655
2025-12-19T14:56:02.950580Z  INFO soroban_ledger_fetch: found, entry: {"data":{"contract_data":{"contract":"CDLZFC3SYJYDZT7K67VZ75HPJVIEUVNIXF47ZG2FB2RMQQVU2HHGCYSC","durability":"persistent","ext":"v0","key":{"vec":[{"symbol":"Balance"},{"address":"CAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD2KM"}]},"val":{"map":[{"key":{"symbol":"amount"},"val":{"i128":"47"}},{"key":{"symbol":"authorized"},"val":{"bool":true}},{"key":{"symbol":"clawback"},"val":{"bool":false}}]}}},"ext":"v0","last_modified_ledger_seq":8655}

TODO

Thanks

Thanks @orbitlens for sharing the idea of using transaction meta as a way to collect recent state. Thanks to all the people who provided feedback to me about how they use the existing stellar snapshot create functionality.

@socket-security

socket-security Bot commented Dec 19, 2025

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedreqwest@​0.12.287910094100100
Addedthiserror@​2.0.188010093100100
Addedcargo_metadata@​0.19.210010093100100
Addeddirectories@​6.0.09710093100100
Addedflate2@​1.1.910010093100100
Addedtracing@​0.1.4410010093100100
Addedtracing-subscriber@​0.3.239910093100100
Addedzstd@​0.13.310010093100100
Addedbytes-lit@​0.0.510010093100100
Addedurl@​2.5.8100100100100100

View full report

This comment was marked as resolved.

@leighmcculloch
leighmcculloch force-pushed the snapshot-source-tx branch 2 times, most recently from 42f37c9 to 598ad4c Compare December 19, 2025 14:52
@leighmcculloch leighmcculloch changed the title Add new ledger snapshot source using meta, RPC, and archives Add tx-granular ledger snapshot source using meta, RPC, and archives Dec 19, 2025
@leighmcculloch
leighmcculloch force-pushed the snapshot-source-tx branch 3 times, most recently from 0a8ae20 to 19904da Compare December 19, 2025 15:34
@earrietadev

Copy link
Copy Markdown

is it possible to set it so the timestamp is also set after the snapshot has been loaded? Currently we need to manually set it after the env is created from the snapshot with something like e.ledger().set_timestamp(1766260837);

@willemneal

Copy link
Copy Markdown
Contributor

This seems like an interesting way of handling a simulation locally. Or at least doing a read call locally.

@orbitlens

Copy link
Copy Markdown

Looks awesome. Thanks, Leigh! ❤️

Comment thread soroban-ledger-snapshot-source-tx/src/lib.rs Outdated
@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@leighmcculloch

Copy link
Copy Markdown
Member Author

I've pushed a change to this that drastically reduces a first run. Some of the cached data wasn't made available until a second pass, but was actually useful to have on the next step in the first pass, so just added an additional level of caching there.

@github-actions

This comment has been minimized.

### What

Add a CI workflow that forks a local stellar/quickstart network, reading
state from the SEP-54 ledger meta store and the history archive that
quickstart runs alongside core and rpc, three times over: with no cache,
with only the system cache, and with the system and workspace caches.

### Why

Everything the snapshot source does has so far only been exercised
against mainnet fixtures, so a break in the meta store or history
archive lookups only surfaces against live networks and data that can't
be produced on demand; quickstart runs both stores locally, and this
also caught `Network::local()` pointing at `/meta-archive` rather than
the `/ledger-meta` path quickstart serves.

### Known limitations

The workflow uses the floating `stellar/quickstart:testing` tag because
galexie support is not in a released quickstart tag yet, and the test
asserts only native Stellar Asset Contract balances, not other entry
types.

### SemVer Change

- [ ] Major (`vX._._`) - Breaking change to the public API.
- [ ] Minor (`v_.Y._`) - Additive change to the public API.
- [x] Patch (`v_._.Z`) - No change to the public API.
@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

semver:minor — this PR adds a new published crate, soroban-ledger-snapshot-source-tx, which is an additive change to the repository's public API surface.

  • soroban-ledger-snapshot-source-tx/Cargo.toml is new and listed in the workspace, making it a published crate; adding new public items is minor per the semver rules.
  • cargo semver-checks reports "no semver update required" for all previously existing crates (soroban-sdk, soroban-ledger-snapshot, soroban-spec, soroban-spec-rust, soroban-token-sdk) — no breaking or additive changes to them.
  • tests/fork/ is not published (publish = false), so it does not affect the classification.

This classification is advisory; the author and reviewer decide the release type.

Generated by Semver Impact for #1657 · sonnet46 · 19.9 AIC · ⌖ 4.12 AIC · ⊞ 8.1K ·

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve the in SDK fork testing experience

6 participants