delta_domain_sdk/
views.rs

1//! # Views
2//!
3//! Read-only [`Views`] to access domain state and data.
4
5use crate::{
6    base_layer::BaseLayer,
7    storage::{
8        Database,
9        DefaultStorage,
10    },
11    views::{
12        domain::DomainStateView,
13        finalized::FinalizedState,
14        sdls::Sdls,
15    },
16};
17use base_sdk::core::Shard;
18use domain_runtime::storage::ColumnFamilies;
19use std::num::NonZero;
20use storage::{
21    database::StorageError,
22    key_value::KeyValueStorageWithColumnFamilies,
23};
24
25/// Domain State View
26pub mod domain;
27/// Finalized State
28pub mod finalized;
29/// SDL status and data
30pub mod sdls;
31
32/// Read-only views to read state and data from the domain.
33///
34/// The views are initialized by [running][run] the domain. Be sure to do that
35/// first and wait for initialization to finish ([run_in_background][runbkg] can help
36/// for this).
37///
38/// Cloneable and can be shared across tasks.
39///
40/// [run]: crate::runner::Runner::run
41/// [runbkg]: crate::runner::Runner::run_in_background
42#[derive(Debug)]
43pub struct Views<S = DefaultStorage>
44where
45    S: KeyValueStorageWithColumnFamilies<
46            ColumnFamilyIdentifier = ColumnFamilies,
47            Error = StorageError,
48        > + Send
49        + Sync
50        + 'static,
51{
52    domain_view: DomainStateView<Database<S>>,
53    finalized: FinalizedState<Database<S>, BaseLayer>,
54    sdls: Sdls<Database<S>>,
55}
56
57impl<S> Views<S>
58where
59    S: KeyValueStorageWithColumnFamilies<
60            ColumnFamilyIdentifier = ColumnFamilies,
61            Error = StorageError,
62        > + Send
63        + Sync
64        + 'static,
65{
66    pub(crate) fn new(shard: NonZero<Shard>, db: Database<S>, fetcher: BaseLayer) -> Self {
67        Self {
68            domain_view: DomainStateView::new(db.clone()),
69            finalized: FinalizedState::new(shard, db.clone(), fetcher),
70            sdls: Sdls::new(db),
71        }
72    }
73
74    /// Access the domain state view
75    pub const fn domain_view(&self) -> &DomainStateView<Database<S>> {
76        &self.domain_view
77    }
78
79    /// Access the finalized state
80    pub const fn finalized(&self) -> &FinalizedState<Database<S>, BaseLayer> {
81        &self.finalized
82    }
83
84    /// Access the SDL status
85    pub const fn sdls(&self) -> &Sdls<Database<S>> {
86        &self.sdls
87    }
88}
89
90// Sadly, this is necessary because `S` is not necessarily `Clone` however
91// `Database<S>` always is
92impl<S> Clone for Views<S>
93where
94    S: KeyValueStorageWithColumnFamilies<
95            ColumnFamilyIdentifier = ColumnFamilies,
96            Error = StorageError,
97        > + Send
98        + Sync
99        + 'static,
100{
101    fn clone(&self) -> Self {
102        Self {
103            domain_view: self.domain_view.clone(),
104            finalized: self.finalized.clone(),
105            sdls: self.sdls.clone(),
106        }
107    }
108}