Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/persistence/stock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,13 @@ impl Stock {
pub fn in_memory() -> Self {
Self::with(MemStash::in_memory(), MemState::in_memory(), MemIndex::in_memory())
}

/// Creates an independent in-memory snapshot without carrying over persistence providers.
///
/// Mutating or storing the returned stock cannot modify the source stock. This is useful for
/// preparing multi-step operations before atomically promoting their resulting state.
#[inline]
pub fn snapshot(&self) -> Self { self.clone_no_persistence() }
}

impl<S: StashProvider, H: StateProvider, I: IndexProvider> Stock<S, H, I> {
Expand Down Expand Up @@ -1776,6 +1783,27 @@ mod test {
}
}

#[test]
fn stock_snapshot_isolated_from_source() {
let source = Stock::in_memory();
let seal = GraphSeal::new_random_vout(Vout::from_u32(1));
let secret = seal.conceal();
let mut snapshot = source.snapshot();

snapshot.store_secret_seal(seal).unwrap();

assert!(source
.as_stash_provider()
.seal_secret(secret)
.unwrap()
.is_none());
assert!(snapshot
.as_stash_provider()
.seal_secret(secret)
.unwrap()
.is_some());
}

#[test]
fn test_transition_builder() {
let stock = Stock::in_memory();
Expand Down
Loading