Skip to main content

bark/persist/
mod.rs

1//! Persistence abstractions for Bark wallets.
2//!
3//! This module defines the [BarkPersister] trait and related data models used by the
4//! wallet to store and retrieve state. Implementors can provide their own storage backends
5//! (e.g., SQLite, PostgreSQL, in-memory, mobile key/value stores) by implementing the
6//! [BarkPersister] trait.
7//!
8//! Design goals
9//! - Clear separation between wallet logic and storage.
10//! - Transactional semantics where appropriate (round state transitions, movement recording).
11//! - Portability across different platforms and environments.
12//!
13//! Typical usage
14//! - Applications construct a concrete persister (for example, a SQLite-backed client) and
15//!   pass it to the [crate::Wallet]. The [crate::Wallet] only depends on this trait for reads/writes.
16//! - Custom wallet implementations can reuse this trait to remain compatible with Bark
17//!   storage expectations without depending on a specific database.
18//! - A default rusqlite implementation is provided by [sqlite::SqliteClient].
19
20pub mod adaptor;
21pub mod models;
22#[cfg(feature = "sqlite")]
23pub mod sqlite;
24#[cfg(test)]
25pub(crate) mod test_suite;
26
27
28use bitcoin::{Transaction, Txid};
29use bitcoin::secp256k1::PublicKey;
30use chrono::DateTime;
31use lightning_invoice::Bolt11Invoice;
32#[cfg(feature = "onchain-bdk")]
33use bdk_wallet::ChangeSet;
34
35use ark::{Vtxo, VtxoId};
36use ark::lightning::{PaymentHash, Preimage};
37use ark::vtxo::Full;
38use bitcoin_ext::BlockDelta;
39
40use crate::WalletProperties;
41use crate::actions::{WalletActionCheckpoint, WalletActionId};
42use crate::exit::ExitTxOrigin;
43use crate::persist::models::{
44	LightningReceive, PaidInvoice, PendingBoard, RoundStateId, StoredExit, StoredRoundState,
45	Unlocked, PendingOffboard,
46};
47use crate::movement::{Movement, MovementId, MovementStatus, MovementSubsystem, PaymentMethod};
48use crate::round::RoundState;
49use crate::vtxo::{VtxoState, VtxoStateKind, WalletVtxo};
50
51/// Storage interface for Bark wallets.
52///
53/// Implement this trait to plug a custom persistence backend. The wallet uses it to:
54/// - Initialize and read wallet properties and configuration.
55/// - Record movements (spends/receives), recipients, and enforce [Vtxo] state transitions.
56/// - Manage round lifecycles (attempts, pending confirmation, confirmations/cancellations).
57/// - Persist ephemeral protocol artifacts (e.g., secret nonces) transactionally.
58/// - Track Lightning receives and preimage revelation.
59/// - Track exit-related data and associated child transactions.
60/// - Persist the last synchronized Ark block height.
61///
62/// Feature integration:
63/// - With the `onchain-bdk` feature, methods are provided to initialize and persist a BDK
64///   wallet ChangeSet in the same storage.
65///
66/// Notes for implementors:
67/// - Ensure that operations that change multiple records (e.g., registering a movement,
68///   storing round state transitions) are executed transactionally.
69/// - Enforce state integrity by verifying allowed_old_states before updating a [Vtxo] state.
70/// - If your backend is not thread-safe, prefer a short-lived connection per call or use
71///   an internal pool with checked-out connections per operation.
72/// - Return precise errors so callers can surface actionable diagnostics.
73#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
74#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
75pub trait BarkPersister: Send + Sync + 'static {
76	/// Check if the wallet is initialized.
77	///
78	/// Returns:
79	/// - `Ok(true)` if the wallet is initialized.
80	/// - `Ok(false)` if the wallet is not initialized.
81	///
82	/// Errors:
83	/// - Returns an error if the query fails.
84	async fn is_initialized(&self) -> anyhow::Result<bool> {
85		Ok(self.read_properties().await?.is_some())
86	}
87
88	/// Initialize a wallet in storage with the provided properties.
89	///
90	/// Call exactly once per wallet database. Subsequent calls should fail to prevent
91	/// accidental re-initialization.
92	///
93	/// Parameters:
94	/// - properties: WalletProperties to persist (e.g., network, descriptors, metadata).
95	///
96	/// Returns:
97	/// - `Ok(())` on success.
98	///
99	/// Errors:
100	/// - Returns an error if the wallet is already initialized or if persistence fails.
101	async fn init_wallet(&self, properties: &WalletProperties) -> anyhow::Result<()>;
102
103	/// Initialize the onchain BDK wallet and return any previously stored ChangeSet.
104	///
105	/// Must be called before storing any new BDK changesets to bootstrap the BDK state.
106	///
107	/// Feature: only available with `onchain-bdk`.
108	///
109	/// Returns:
110	/// - `Ok(ChangeSet)` containing the previously persisted BDK state (possibly empty).
111	///
112	/// Errors:
113	/// - Returns an error if the BDK state cannot be created or loaded.
114	#[cfg(feature = "onchain-bdk")]
115	async fn initialize_bdk_wallet(&self) -> anyhow::Result<ChangeSet>;
116
117	/// Persist an incremental BDK ChangeSet.
118	///
119	/// The changeset should be applied atomically. Callers typically obtain the changeset
120	/// from a BDK wallet instance after mutating wallet state (e.g., sync).
121	///
122	/// Feature: only available with `onchain-bdk`.
123	///
124	/// Parameters:
125	/// - changeset: The BDK ChangeSet to persist.
126	///
127	/// Errors:
128	/// - Returns an error if the changeset cannot be written.
129	#[cfg(feature = "onchain-bdk")]
130	async fn store_bdk_wallet_changeset(&self, changeset: &ChangeSet) -> anyhow::Result<()>;
131
132	/// Read wallet properties from storage.
133	///
134	/// Returns:
135	/// - `Ok(Some(WalletProperties))` if the wallet has been initialized.
136	/// - `Ok(None)` if no wallet exists yet.
137	///
138	/// Errors:
139	/// - Returns an error on I/O or deserialization failures.
140	async fn read_properties(&self) -> anyhow::Result<Option<WalletProperties>>;
141
142	/// Set the server public key in wallet properties.
143	///
144	/// This is used to store the server pubkey for existing wallets that were
145	/// created before server pubkey tracking was added. Once set, the wallet
146	/// will verify the server pubkey on every connection.
147	///
148	/// Parameters:
149	/// - server_pubkey: The server's public key to store.
150	///
151	/// Errors:
152	/// - Returns an error if the update fails.
153	async fn set_server_pubkey(&self, server_pubkey: PublicKey) -> anyhow::Result<()>;
154
155	/// Set the server's mailbox public key in wallet properties.
156	///
157	/// This is used to store the server mailbox pubkey for existing wallets that were
158	/// created before mailbox pubkey tracking was added. Once set, Ark addresses
159	/// can be generated offline without a live server connection.
160	///
161	/// Parameters:
162	/// - server_mailbox_pubkey: The server's mailbox public key to store.
163	///
164	/// Errors:
165	/// - Returns an error if the update fails.
166	async fn set_server_mailbox_pubkey(&self, server_mailbox_pubkey: PublicKey) -> anyhow::Result<()>;
167
168	/// Creates a new movement in the given state, ready to be updated.
169	///
170	/// Parameters:
171	/// - status: The desired status for the new movement.
172	/// - subsystem: The subsystem that created the movement.
173	/// - time: The time the movement should be marked as created.
174	///
175	/// Returns:
176	/// - `Ok(MovementId)` of the newly created movement.
177	///
178	/// Errors:
179	/// - Returns an error if the movement is unable to be created.
180	async fn create_new_movement(&self,
181		status: MovementStatus,
182		subsystem: &MovementSubsystem,
183		time: DateTime<chrono::Local>,
184	) -> anyhow::Result<MovementId>;
185
186	/// Persists the given movement state.
187	///
188	/// Parameters:
189	/// - movement: The movement and its associated data to be persisted.
190	///
191	/// Errors:
192	/// - Returns an error if updating the movement fails for any reason.
193	async fn update_movement(&self, movement: &Movement) -> anyhow::Result<()>;
194
195	/// Gets the movement with the given [MovementId].
196	///
197	/// Parameters:
198	/// - movement_id: The ID of the movement to retrieve.
199	///
200	/// Returns:
201	/// - `Ok(Movement)` if the movement exists.
202	///
203	/// Errors:
204	/// - If the movement does not exist.
205	/// - If retrieving the movement fails.
206	async fn get_movement_by_id(&self, movement_id: MovementId) -> anyhow::Result<Movement>;
207
208	/// Gets every stored movement.
209	///
210	/// Returns:
211	/// - `Ok(Vec<Movement>)` containing all movements, empty if none exist.
212	///
213	/// Errors:
214	/// - If retrieving the movements fails.
215	async fn get_all_movements(&self) -> anyhow::Result<Vec<Movement>>;
216
217	/// Get all movements for a given payment method
218	///
219	/// Parameters:
220	/// - `payment_method`: The [PaymentMethod] to look up.
221	///
222	/// Returns:
223	/// - `Ok(movements)` containing all relevant movements, empty if none exist.
224	///
225	/// Errors:
226	/// - Returns an error if the query fails.
227	async fn get_movements_by_payment_method(
228		&self,
229		payment_method: &PaymentMethod,
230	) -> anyhow::Result<Vec<Movement>>;
231
232	/// Store a pending board.
233	///
234	/// Parameters:
235	/// - vtxo: The [Vtxo] to store.
236	/// - funding_txid: The funding [Txid].
237	/// - movement_id: The [MovementId] associated with this board.
238	///
239	/// Errors:
240	/// - Returns an error if the board cannot be stored.
241	async fn store_pending_board(
242		&self,
243		vtxo: &Vtxo<Full>,
244		funding_tx: &Transaction,
245		movement_id: MovementId,
246	) -> anyhow::Result<()>;
247
248	/// Remove a pending board.
249	///
250	/// Parameters:
251	/// - vtxo_id: The [VtxoId] to remove.
252	///
253	/// Errors:
254	/// - Returns an error if the board cannot be removed.
255	async fn remove_pending_board(&self, vtxo_id: &VtxoId) -> anyhow::Result<()>;
256
257	/// Get the [VtxoId] for each pending board.
258	///
259	/// Returns:
260	/// - `Ok(Vec<VtxoId>)` possibly empty.
261	///
262	/// Errors:
263	/// - Returns an error if the query fails.
264	async fn get_all_pending_board_ids(&self) -> anyhow::Result<Vec<VtxoId>>;
265
266	/// Get the [PendingBoard] associated with the given [VtxoId].
267	///
268	/// Returns:
269	/// - `Ok(Some(PendingBoard))` if a matching board exists
270	/// - `Ok(None)` if no matching board exists
271	///
272	/// Errors:
273	/// - Returns an error if the query fails.
274	async fn get_pending_board_by_vtxo_id(&self, vtxo_id: VtxoId) -> anyhow::Result<Option<PendingBoard>>;
275
276	/// Store a new ongoing round state
277	///
278	/// The holder should ensure the input VTXOs are available and locked.
279	///
280	/// Parameters:
281	/// - `round_state`: the state to store
282	///
283	/// Returns:
284	/// - `RoundStateId`: the storaged ID of the new state
285	///
286	/// Errors:
287	/// - returns an error of the new round state could not be stored
288	async fn store_round_state(&self, round_state: &RoundState) -> anyhow::Result<RoundStateId>;
289
290	/// Update an existing stored pending round state
291	///
292	/// Parameters:
293	/// - `round_state`: the round state to update
294	///
295	/// Errors:
296	/// - returns an error of the existing round state could not be found or updated
297	async fn update_round_state(&self, round_state: &StoredRoundState) -> anyhow::Result<()>;
298
299	/// Remove a pending round state from the db
300	///
301	/// Parameters:
302	/// - `round_state`: the round state to remove
303	///
304	/// Errors:
305	/// - returns an error of the existing round state could not be found or removed
306	async fn remove_round_state(&self, round_state: &StoredRoundState) -> anyhow::Result<()>;
307
308	/// Load a single round state by its id
309	///
310	/// Returns:
311	/// - `Option<StoredRoundState>`: the stored round state if found, `None` otherwise
312	///
313	/// Errors:
314	/// - returns an error of the states could not be succesfully retrieved
315	async fn get_round_state_by_id(&self, id: RoundStateId) -> anyhow::Result<Option<StoredRoundState<Unlocked>>>;
316
317	/// Load all pending round states from the db
318	///
319	/// Returns:
320	/// - `Vec<RoundStateId>`: unordered vector with all stored round state ids
321	///
322	/// Errors:
323	/// - returns an error of the ids could not be succesfully retrieved
324	async fn get_pending_round_state_ids(&self) -> anyhow::Result<Vec<RoundStateId>>;
325
326	/// Stores VTXOs with their initial state.
327	///
328	/// This operation is idempotent: if a VTXO already exists (same `id`), the
329	/// implementation should succeed without modifying the existing VTXO or its
330	/// state. This allows safe retries during crash recovery scenarios.
331	///
332	/// # Parameters
333	/// - `vtxos`: Slice of VTXO and state pairs to store.
334	///
335	/// # Behavior
336	/// - For each VTXO that does not exist: inserts the VTXO and its initial state.
337	/// - For each VTXO that already exists: no-op for that VTXO.
338	///
339	/// # Errors
340	/// - Returns an error if the storage operation fails.
341	async fn store_vtxos(
342		&self,
343		vtxos: &[(&Vtxo<Full>, &VtxoState)],
344	) -> anyhow::Result<()>;
345
346	/// Fetch a wallet [Vtxo] with its current state by ID.
347	///
348	/// Parameters:
349	/// - id: [VtxoId] to look up.
350	///
351	/// Returns:
352	/// - `Ok(Some(WalletVtxo))` if found,
353	/// - `Ok(None)` otherwise.
354	///
355	/// Errors:
356	/// - Returns an error if the lookup fails.
357	async fn get_wallet_vtxo(&self, id: VtxoId) -> anyhow::Result<Option<WalletVtxo>>;
358
359	/// Fetch all wallet VTXOs in the database.
360	///
361	/// Returns:
362	/// - `Ok(Vec<WalletVtxo>)` possibly empty.
363	///
364	/// Errors:
365	/// - Returns an error if the query fails.
366	async fn get_all_vtxos(&self) -> anyhow::Result<Vec<WalletVtxo>>;
367
368	/// Fetch all wallet VTXOs whose state matches any of the provided kinds.
369	///
370	/// Parameters:
371	/// - state: Slice of `VtxoStateKind` filters.
372	///
373	/// Returns:
374	/// - `Ok(Vec<WalletVtxo>)` possibly empty.
375	///
376	/// Errors:
377	/// - Returns an error if the query fails.
378	async fn get_vtxos_by_state(&self, state: &[VtxoStateKind]) -> anyhow::Result<Vec<WalletVtxo>>;
379
380	/// Fetch a single VTXO in full form (including the unilateral exit chain).
381	///
382	/// Listing/balance/selection paths return [WalletVtxo] (which holds
383	/// [Vtxo<ark::vtxo::Bare>]) to keep memory bounded. Operations that
384	/// genuinely need the genesis chain — unilateral exit, server
385	/// registration, arkoor send, offboard — should call this method
386	/// (or [BarkPersister::get_full_vtxos] for batches) on demand.
387	async fn get_full_vtxo(&self, id: VtxoId) -> anyhow::Result<Option<Vtxo<Full>>>;
388
389	/// Hydrate a batch of VTXOs into their full form, preserving the order
390	/// of the input slice. Returns an error if any id is missing — callers
391	/// reach this from a selection step against the wallet's listings, so a
392	/// missing row indicates the wallet's state is inconsistent with the
393	/// caller's view.
394	async fn get_full_vtxos(&self, ids: &[VtxoId]) -> anyhow::Result<Vec<Vtxo<Full>>>;
395
396	/// Remove a [Vtxo] by ID.
397	///
398	/// Parameters:
399	/// - id: `VtxoId` to remove.
400	///
401	/// Returns:
402	/// - `Ok(Some(Vtxo))` with the removed [Vtxo] data if it existed,
403	/// - `Ok(None)` otherwise.
404	///
405	/// Errors:
406	/// - Returns an error if the delete operation fails.
407	async fn remove_vtxo(&self, id: VtxoId) -> anyhow::Result<Option<Vtxo<Full>>>;
408
409	/// Check whether a [Vtxo] is already marked spent.
410	///
411	/// Parameters:
412	/// - id: VtxoId to check.
413	///
414	/// Returns:
415	/// - `Ok(true)` if spent,
416	/// - `Ok(false)` if not found or not spent.
417	///
418	/// Errors:
419	/// - Returns an error if the lookup fails.
420	async fn has_spent_vtxo(&self, id: VtxoId) -> anyhow::Result<bool>;
421
422	/// Store a newly derived/assigned [Vtxo] public key index mapping.
423	///
424	/// Parameters:
425	/// - index: Derivation index.
426	/// - public_key: PublicKey at that index.
427	///
428	/// Errors:
429	/// - Returns an error if the mapping cannot be stored.
430	async fn store_vtxo_key(&self, index: u32, public_key: PublicKey) -> anyhow::Result<()>;
431
432	/// Get the last revealed/used [Vtxo] key index.
433	///
434	/// Returns:
435	/// - `Ok(Some(u32))` if a key was stored
436	/// - `Ok(None)` otherwise.
437	///
438	/// Errors:
439	/// - Returns an error if the query fails.
440	async fn get_last_vtxo_key_index(&self) -> anyhow::Result<Option<u32>>;
441
442	/// Retrieves the derivation index of the provided [PublicKey] from the database
443	///
444	/// Returns:
445	/// - `Ok(Some(u32))` if the key was stored.
446	/// - `Ok(None)` if the key was not stored.
447	///
448	/// Errors:
449	/// - Returns an error if the query fails.
450	async fn get_public_key_idx(&self, public_key: &PublicKey) -> anyhow::Result<Option<u32>>;
451
452	/// Retrieves the mailbox checkpoint from the database
453	///
454	/// Returns:
455	/// - `Ok(u64)` the stored checkpoint.
456	///
457	/// Errors:
458	/// - Returns an error if the query fails.
459	async fn get_mailbox_checkpoint(&self) -> anyhow::Result<u64>;
460
461	/// Update the mailbox checkpoint to the new checkpoint
462	///
463	/// Returns:
464	///
465	///
466	/// Errors:
467	/// - Returns error when the query fails
468	/// - Returns error when the provided checkpoint is smaller than the existing checkpoint
469	async fn store_mailbox_checkpoint(&self, checkpoint: u64) -> anyhow::Result<()>;
470
471	/// Persist or overwrite a wallet action checkpoint.
472	///
473	/// Parameters:
474	/// - id: stable action identifier (e.g. payment hash hex for a lightning send).
475	/// - checkpoint: the payload to persist; replaces any existing row with the same id.
476	///
477	/// Errors:
478	/// - Returns an error if the write fails.
479	async fn upsert_wallet_action_checkpoint(
480		&self,
481		id: &WalletActionId,
482		checkpoint: &WalletActionCheckpoint,
483	) -> anyhow::Result<()>;
484
485	/// Fetch a wallet action checkpoint by id.
486	///
487	/// Returns:
488	/// - `Ok(Some(_))` if a row exists, `Ok(None)` otherwise.
489	///
490	/// Errors:
491	/// - Returns an error if the lookup or deserialization fails.
492	async fn get_wallet_action_checkpoint(
493		&self,
494		id: &WalletActionId,
495	) -> anyhow::Result<Option<WalletActionCheckpoint>>;
496
497	/// Fetch every persisted wallet action checkpoint, oldest first.
498	///
499	/// Used by the periodic sync to find work to re-drive.
500	async fn get_all_wallet_action_checkpoints(
501		&self,
502	) -> anyhow::Result<Vec<WalletActionCheckpoint>>;
503
504	/// Remove a wallet action checkpoint by id. No-op if absent.
505	async fn remove_wallet_action_checkpoint(
506		&self,
507		id: &WalletActionId,
508	) -> anyhow::Result<()>;
509
510	/// Record a settled outgoing lightning send.
511	///
512	/// Idempotent: a subsequent call with the same payment_hash is a
513	/// no-op (the existing row wins). This makes retry across a crash
514	/// safe even without a multi-row transaction.
515	async fn record_paid_invoice(
516		&self,
517		payment_hash: PaymentHash,
518		preimage: Preimage,
519	) -> anyhow::Result<()>;
520
521	/// Look up an existing paid-invoice record by payment hash.
522	async fn get_paid_invoice(
523		&self,
524		payment_hash: PaymentHash,
525	) -> anyhow::Result<Option<PaidInvoice>>;
526
527	/// Store an incoming Lightning receive record.
528	///
529	/// Parameters:
530	/// - payment_hash: Unique payment hash.
531	/// - preimage: Payment preimage (kept until disclosure).
532	/// - invoice: The associated BOLT11 invoice.
533	/// - htlc_recv_cltv_delta: The CLTV delta for the HTLC VTXO.
534	///
535	/// Errors:
536	/// - Returns an error if the receive cannot be stored.
537	async fn store_lightning_receive(
538		&self,
539		payment_hash: PaymentHash,
540		preimage: Preimage,
541		invoice: &Bolt11Invoice,
542		htlc_recv_cltv_delta: BlockDelta,
543	) -> anyhow::Result<()>;
544
545	/// Returns a list of all pending lightning receives
546	///
547	/// Returns:
548	/// - `Ok(Vec<LightningReceive>)` possibly empty.
549	///
550	/// Errors:
551	/// - Returns an error if the query fails.
552	async fn get_all_pending_lightning_receives(&self) -> anyhow::Result<Vec<LightningReceive>>;
553
554	/// Mark a Lightning receive preimage as revealed (e.g., after settlement).
555	///
556	/// Parameters:
557	/// - payment_hash: The payment hash identifying the receive.
558	///
559	/// Errors:
560	/// - Returns an error if the update fails or the receive does not exist.
561	async fn set_preimage_revealed(&self, payment_hash: PaymentHash) -> anyhow::Result<()>;
562
563	/// Set the VTXO IDs and [MovementId] for a [LightningReceive].
564	///
565	/// Parameters:
566	/// - payment_hash: The payment hash identifying the receive.
567	/// - htlc_vtxo_ids: The VTXO IDs to set.
568	/// - movement_id: The movement ID associated with the invoice.
569	///
570	/// Errors:
571	/// - Returns an error if the update fails or the receive does not exist.
572	async fn update_lightning_receive(
573		&self,
574		payment_hash: PaymentHash,
575		htlc_vtxo_ids: &[VtxoId],
576		movement_id: MovementId,
577	) -> anyhow::Result<()>;
578
579	/// Fetch a Lightning receive by its payment hash.
580	///
581	/// Parameters:
582	/// - payment_hash: The payment hash to look up.
583	///
584	/// Returns:
585	/// - `Ok(Some(LightningReceive))` if found,
586	/// - `Ok(None)` otherwise.
587	///
588	/// Errors:
589	/// - Returns an error if the lookup fails.
590	async fn fetch_lightning_receive_by_payment_hash(
591		&self,
592		payment_hash: PaymentHash,
593	) -> anyhow::Result<Option<LightningReceive>>;
594
595	/// Mark a Lightning receive as finished by its payment hash.
596	///
597	/// Parameters:
598	/// - payment_hash: The payment hash of the record to mark finished
599	///
600	/// Errors:
601	/// - Returns an error if the operation fails.
602	async fn finish_pending_lightning_receive(
603		&self,
604		payment_hash: PaymentHash,
605	) -> anyhow::Result<()>;
606
607	/// Store an entry indicating a [Vtxo] is being exited.
608	///
609	/// Parameters:
610	/// - exit: StoredExit describing the exit operation.
611	///
612	/// Errors:
613	/// - Returns an error if the entry cannot be stored.
614	async fn store_exit_vtxo_entry(&self, exit: &StoredExit) -> anyhow::Result<()>;
615
616	/// Remove an exit entry for a given [Vtxo] ID.
617	///
618	/// Parameters:
619	/// - id: VtxoId to remove from exit tracking.
620	///
621	/// Errors:
622	/// - Returns an error if the removal fails.
623	async fn remove_exit_vtxo_entry(&self, id: &VtxoId) -> anyhow::Result<()>;
624
625	/// List all VTXOs currently tracked as being exited.
626	///
627	/// Returns:
628	/// - `Ok(Vec<StoredExit>)` possibly empty.
629	///
630	/// Errors:
631	/// - Returns an error if the query fails.
632	async fn get_exit_vtxo_entries(&self) -> anyhow::Result<Vec<StoredExit>>;
633
634	/// Store a child transaction related to an exit transaction.
635	///
636	/// Parameters:
637	/// - exit_txid: The parent exit transaction ID.
638	/// - child_tx: The child bitcoin Transaction to store.
639	/// - origin: Metadata describing where the child came from (ExitTxOrigin).
640	///
641	/// Errors:
642	/// - Returns an error if the transaction cannot be stored.
643	async fn store_exit_child_tx(
644		&self,
645		exit_txid: Txid,
646		child_tx: &Transaction,
647		origin: ExitTxOrigin,
648	) -> anyhow::Result<()>;
649
650	/// Retrieve a stored child transaction for a given exit transaction ID.
651	///
652	/// Parameters:
653	/// - exit_txid: The parent exit transaction ID.
654	///
655	/// Returns:
656	/// - `Ok(Some((Transaction, ExitTxOrigin)))` if found,
657	/// - `Ok(None)` otherwise.
658	///
659	/// Errors:
660	/// - Returns an error if the lookup fails.
661	async fn get_exit_child_tx(
662		&self,
663		exit_txid: Txid,
664	) -> anyhow::Result<Option<(Transaction, ExitTxOrigin)>>;
665
666	/// Updates the state of the VTXO corresponding to the given [VtxoId], provided that their
667	/// current state is one of the given `allowed_states`.
668	///
669	/// # Parameters
670	/// - `vtxo_id`: The ID of the [Vtxo] to update.
671	/// - `state`: The new state to be set for the specified [Vtxo].
672	/// - `allowed_states`: An iterable collection of allowed states ([VtxoStateKind]) that the
673	///   [Vtxo] must currently be in for their state to be updated to the new `state`.
674	///
675	/// # Returns
676	/// - `Ok(WalletVtxo)` if the state update is successful.
677	/// - `Err(anyhow::Error)` if the VTXO fails to meet the required conditions,
678	///    or if another error occurs during the operation.
679	///
680	/// # Errors
681	/// - Returns an error if the current state is not within the `allowed_states`.
682	/// - Returns an error for any other issues encountered during the operation.
683	async fn update_vtxo_state_checked(
684		&self,
685		vtxo_id: VtxoId,
686		new_state: VtxoState,
687		allowed_old_states: &[VtxoStateKind],
688	) -> anyhow::Result<WalletVtxo>;
689
690	/// Transition multiple VTXOs to `new_state` atomically: either every
691	/// vtxo's state changes or none does. A failure must not affect any
692	/// vtxo.
693	async fn update_vtxo_states_checked(
694		&self,
695		vtxo_ids: &[VtxoId],
696		new_state: VtxoState,
697		allowed_old_states: &[VtxoStateKind],
698	) -> anyhow::Result<()>;
699
700	/// Store a pending offboard record.
701	///
702	/// Parameters:
703	/// - pending: The [PendingOffboard] to store.
704	///
705	/// Errors:
706	/// - Returns an error if the record cannot be stored.
707	async fn store_pending_offboard(
708		&self,
709		pending: &PendingOffboard,
710	) -> anyhow::Result<()>;
711
712	/// Get all pending offboard records.
713	///
714	/// Returns:
715	/// - `Ok(Vec<PendingOffboard>)` possibly empty.
716	///
717	/// Errors:
718	/// - Returns an error if the query fails.
719	async fn get_pending_offboards(&self) -> anyhow::Result<Vec<PendingOffboard>>;
720
721	/// Remove a pending offboard record by its [MovementId].
722	///
723	/// Parameters:
724	/// - movement_id: The [MovementId] to remove.
725	///
726	/// Errors:
727	/// - Returns an error if the record cannot be removed.
728	async fn remove_pending_offboard(&self, movement_id: MovementId) -> anyhow::Result<()>;
729}