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