Skip to content

Commit eef97ad

Browse files
add documentation for snapshot source types and conversion
1 parent f7527ed commit eef97ad

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

soroban-sdk/src/testutils.rs

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,20 @@ use soroban_ledger_snapshot::LedgerSnapshot;
2525

2626
pub use crate::env::EnvTestConfig;
2727

28-
pub use crate::env::internal::{storage::SnapshotSource, HostError};
28+
/// Trait for providing ledger data to the test environment.
29+
///
30+
/// Implement this trait to create custom snapshot sources that load ledger state
31+
/// from sources other than [`LedgerSnapshot`] files, such as RPC endpoints,
32+
/// history archives, or in-memory data structures.
33+
///
34+
/// Use with [`SnapshotSourceInput`] and [`Env::from_ledger_snapshot`] to initialize
35+
/// a test environment from a custom source.
36+
pub use crate::env::internal::storage::SnapshotSource;
37+
38+
/// Error type returned by [`SnapshotSource::get`].
39+
///
40+
/// Required for implementing custom snapshot sources.
41+
pub use crate::env::internal::HostError;
2942

3043
pub trait Register {
3144
fn register<'i, I, A>(self, env: &Env, id: I, args: A) -> crate::Address
@@ -608,12 +621,67 @@ impl StellarAssetContract {
608621
}
609622
}
610623

624+
/// Input for creating an [`Env`] from a custom snapshot source.
625+
///
626+
/// This struct enables [`Env::from_ledger_snapshot`] to accept custom snapshot
627+
/// source types beyond [`LedgerSnapshot`], providing flexibility for testing
628+
/// scenarios that load ledger state from different sources such as RPC endpoints,
629+
/// history archives, or in-memory data structures.
630+
///
631+
/// # Fields
632+
///
633+
/// * `source` - A snapshot source implementing the [`SnapshotSource`] trait.
634+
/// This is used to load ledger entries on demand during test execution.
635+
///
636+
/// * `ledger_info` - Optional ledger info to initialize the environment with.
637+
/// If `None`, default test ledger info is used.
638+
///
639+
/// * `snapshot` - Optional [`LedgerSnapshot`] used as the base for capturing
640+
/// state changes. When the test completes, modified entries are written to
641+
/// this snapshot. If `None`, a new empty snapshot is created.
642+
///
643+
/// # Example
644+
///
645+
/// ```
646+
/// use soroban_sdk::testutils::{SnapshotSource, SnapshotSourceInput, HostError};
647+
/// use soroban_sdk::xdr::{LedgerEntry, LedgerKey};
648+
/// use soroban_sdk::Env;
649+
/// use std::rc::Rc;
650+
///
651+
/// struct MyCustomSource;
652+
///
653+
/// impl SnapshotSource for MyCustomSource {
654+
/// fn get(
655+
/// &self,
656+
/// key: &Rc<LedgerKey>,
657+
/// ) -> Result<Option<(Rc<LedgerEntry>, Option<u32>)>, HostError> {
658+
/// // Return None for keys not found, or Some((entry, live_until_ledger))
659+
/// Ok(None)
660+
/// }
661+
/// }
662+
///
663+
/// let input = SnapshotSourceInput {
664+
/// source: Rc::new(MyCustomSource),
665+
/// ledger_info: None,
666+
/// snapshot: None,
667+
/// };
668+
/// let env = Env::from_ledger_snapshot(input);
669+
/// ```
611670
pub struct SnapshotSourceInput {
612671
pub source: Rc<dyn SnapshotSource>,
613672
pub ledger_info: Option<LedgerInfo>,
614673
pub snapshot: Option<Rc<LedgerSnapshot>>,
615674
}
616675

676+
/// Converts a [`LedgerSnapshot`] into a [`SnapshotSourceInput`].
677+
///
678+
/// This conversion maintains backward compatibility with the existing API,
679+
/// allowing [`LedgerSnapshot`] to be used directly with [`Env::from_ledger_snapshot`].
680+
///
681+
/// The [`LedgerSnapshot`] is wrapped in an [`Rc`] and used for all three fields:
682+
/// - As the snapshot source for loading ledger entries
683+
/// - To provide the ledger info for the environment
684+
/// - As the base snapshot for capturing state changes
617685
impl From<LedgerSnapshot> for SnapshotSourceInput {
618686
fn from(s: LedgerSnapshot) -> Self {
619687
let s = Rc::new(s);

0 commit comments

Comments
 (0)