delta_domain_sdk/views/
sdls.rs

1use base_sdk::core::{
2    SdlId,
3    TxId,
4};
5use domain_runtime::{
6    ports::DbQueriesPort,
7    sdls::types::SdlState,
8    storage::{
9        SdlProofTxIdColumnFamily,
10        SdlStateColumnFamily,
11    },
12};
13use snafu::{
14    ResultExt,
15    Snafu,
16};
17use storage::{
18    column_family::StorageQuery,
19    database::StorageError,
20};
21
22/// Errors occurring in the [Sdls] helper
23#[derive(Debug, Snafu)]
24pub enum Error {
25    /// Error when reading from storage
26    #[snafu(display("Error while reading from storage: {source}"))]
27    Storage {
28        /// The underlying storage error
29        source: StorageError,
30    },
31}
32
33/// Helper to access the SDL state
34#[derive(Debug, Clone)]
35pub struct Sdls<Db> {
36    db: Db,
37}
38
39impl<Db> Sdls<Db> {
40    pub(crate) const fn new(db: Db) -> Self {
41        Self { db }
42    }
43}
44
45impl<Db> Sdls<Db>
46where
47    Db: DbQueriesPort,
48{
49    /// Get the current status of a State Diff List (SDL) that was processed by
50    /// this domain.
51    ///
52    /// # Parameters
53    ///
54    /// * `sdl_id` - The id of the SDL to query
55    ///
56    /// # Returns
57    ///
58    /// * `Ok(Some(state))` - The current state of the SDL if found
59    /// * `Ok(None)` - If no SDL with the given id exists
60    /// * `Err(Error)` - If there was an error reading from storage
61    pub fn get_status(&self, sdl_id: &SdlId) -> Result<Option<SdlState>, Error> {
62        StorageQuery::<SdlStateColumnFamily>::get_value(&self.db, sdl_id).context(StorageSnafu)
63    }
64
65    /// Get all State Diff List (SDL) ids with their current status
66    pub fn get_all(&self) -> Result<Vec<(SdlId, SdlState)>, Error> {
67        // TODO: This method does not scale with time as it iterates over all sdls
68        StorageQuery::<SdlStateColumnFamily>::iter(&self.db)
69            .collect::<Result<Vec<_>, _>>()
70            .context(StorageSnafu)
71    }
72
73    /// Get the [TxId] of the base layer transaction that submitted the proof of
74    /// the SDL identified by the given [SdlId].
75    ///
76    /// # Returns
77    ///
78    /// * `Ok(Some(tx_id))` - The requested [TxId] if found
79    /// * `Ok(None)` - If no [TxId] was registered for the given SDL id, e.g.
80    ///   the proof was not yet submitted
81    /// * `Err(Error)` - If there was an error reading from storage
82    pub fn get_proof_transaction_id(&self, sdl_id: &SdlId) -> Result<Option<TxId>, Error> {
83        StorageQuery::<SdlProofTxIdColumnFamily>::get_value(&self.db, sdl_id).context(StorageSnafu)
84    }
85}