delta_base_sdk/sdl.rs
1//! # State Diff List (SDL)
2//!
3//! This module provides types and utilities for working with [State Diff Lists
4//! (SDLs)](`primitives::diff::list::StateDiffList`).
5
6use crate::{
7 core::Shard,
8 crypto::HashDigest,
9};
10use proto_types::{
11 error::{
12 HashSnafu,
13 ProtoError,
14 WronglyFormattedDataSnafu,
15 },
16 sdl::SdlUpdate as WireUpdate,
17};
18use snafu::{
19 OptionExt,
20 ResultExt,
21};
22
23// Re-export of types to be exposed by the base SDK
24pub use primitives::diff::{
25 aggregator::AggregatedStateDiff,
26 list::StateDiffList,
27 StateDiff,
28};
29
30pub use primitives::proofs::sdl_proof::{
31 SDLProof,
32 VerificationKey,
33};
34
35pub use proto_types::sdl::SdlStatus;
36
37/// Update notification for a [StateDiffList] status or metadata
38///
39/// [SdlUpdate] represents a notification about a change to a SDL's status
40/// in the network. It is typically received through the events stream
41/// and allows clients to track the progress of their submitted SDLs.
42///
43/// # Example
44///
45/// ```no_run
46/// use delta_base_sdk::{
47/// events::BaseLayerEvent,
48/// rpc::BaseRpcClient,
49/// sdl::SdlUpdate,
50/// };
51/// use tokio_stream::StreamExt;
52///
53/// async fn monitor_sdl(hash_to_monitor: &str) -> Result<(), Box<dyn std::error::Error>> {
54/// let client = BaseRpcClient::new("http://localhost:50051").await?;
55/// let shard_id = 1;
56///
57/// let mut events = client.stream_base_layer_events(shard_id).await?;
58///
59/// // Monitor events for the SDL we're interested in
60/// while let Some(event_result) = events.next().await {
61/// if let Ok(BaseLayerEvent::SdlUpdate(update)) = event_result {
62/// if update.hash.to_string() == hash_to_monitor {
63/// println!("SDL status update: {:?}", update.status);
64/// }
65/// }
66/// }
67///
68/// Ok(())
69/// }
70/// ```
71#[derive(Debug, Clone, Copy)]
72pub struct SdlUpdate {
73 /// Unique hash identifier of the SDL
74 ///
75 /// This hash is computed from the SDL's contents and serves as
76 /// its unique identifier in the network.
77 pub hash: HashDigest,
78
79 /// Shard identifier that emitted this SDL
80 ///
81 /// Indicates which shard was responsible for creating and
82 /// submitting this SDL to the network.
83 pub originator: Shard,
84
85 /// Current processing status of the SDL
86 ///
87 /// Represents the SDL's status in the network's processing pipeline,
88 /// such as submitted, accepted, rejected, or proven.
89 pub status: SdlStatus,
90}
91
92/// Conversion from protocol buffer representation to native SDK type
93impl TryFrom<WireUpdate> for SdlUpdate {
94 type Error = ProtoError;
95
96 fn try_from(value: WireUpdate) -> Result<Self, Self::Error> {
97 Ok(Self {
98 hash: value.sdl_hash.try_into().context(HashSnafu)?,
99 originator: value.originator_shard,
100 status: value
101 .status
102 .try_into()
103 .ok()
104 .context(WronglyFormattedDataSnafu {
105 field: "status".to_string(),
106 })?,
107 })
108 }
109}