|
| 1 | +use signet_extract::Extractable; |
| 2 | +use std::sync::Arc; |
| 3 | + |
| 4 | +/// A notification from the host chain, bundling a chain event with |
| 5 | +/// point-in-time block tag data. The safe/finalized block numbers are |
| 6 | +/// intentionally snapshotted at notification creation time rather than |
| 7 | +/// fetched live, because rollup safe/finalized tags are only updated |
| 8 | +/// after block processing completes. |
| 9 | +/// |
| 10 | +/// # Examples |
| 11 | +/// |
| 12 | +/// ``` |
| 13 | +/// # use std::sync::Arc; |
| 14 | +/// # use signet_node_types::{HostNotification, HostNotificationKind}; |
| 15 | +/// # fn example<C: signet_extract::Extractable>(chain: Arc<C>) { |
| 16 | +/// let notification = HostNotification { |
| 17 | +/// kind: HostNotificationKind::ChainCommitted { new: chain }, |
| 18 | +/// safe_block_number: Some(100), |
| 19 | +/// finalized_block_number: Some(90), |
| 20 | +/// }; |
| 21 | +/// |
| 22 | +/// // Access the committed chain via the shortcut method. |
| 23 | +/// assert!(notification.committed_chain().is_some()); |
| 24 | +/// assert!(notification.reverted_chain().is_none()); |
| 25 | +/// # } |
| 26 | +/// ``` |
| 27 | +#[derive(Debug, Clone)] |
| 28 | +pub struct HostNotification<C> { |
| 29 | + /// The chain event (commit, revert, or reorg). |
| 30 | + pub kind: HostNotificationKind<C>, |
| 31 | + /// The host chain "safe" block number at the time of this notification. |
| 32 | + pub safe_block_number: Option<u64>, |
| 33 | + /// The host chain "finalized" block number at the time of this |
| 34 | + /// notification. |
| 35 | + pub finalized_block_number: Option<u64>, |
| 36 | +} |
| 37 | + |
| 38 | +impl<C: Extractable> HostNotification<C> { |
| 39 | + /// Returns the committed chain, if any. Shortcut for |
| 40 | + /// `self.kind.committed_chain()`. |
| 41 | + pub const fn committed_chain(&self) -> Option<&Arc<C>> { |
| 42 | + self.kind.committed_chain() |
| 43 | + } |
| 44 | + |
| 45 | + /// Returns the reverted chain, if any. Shortcut for |
| 46 | + /// `self.kind.reverted_chain()`. |
| 47 | + pub const fn reverted_chain(&self) -> Option<&Arc<C>> { |
| 48 | + self.kind.reverted_chain() |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/// The kind of chain event in a [`HostNotification`]. |
| 53 | +/// |
| 54 | +/// # Examples |
| 55 | +/// |
| 56 | +/// ``` |
| 57 | +/// # use std::sync::Arc; |
| 58 | +/// # use signet_node_types::HostNotificationKind; |
| 59 | +/// # fn example<C: signet_extract::Extractable>(old: Arc<C>, new: Arc<C>) { |
| 60 | +/// let kind = HostNotificationKind::ChainReorged { |
| 61 | +/// old: old.clone(), |
| 62 | +/// new: new.clone(), |
| 63 | +/// }; |
| 64 | +/// # } |
| 65 | +/// ``` |
| 66 | +#[derive(Debug, Clone)] |
| 67 | +pub enum HostNotificationKind<C> { |
| 68 | + /// A new chain segment was committed. |
| 69 | + ChainCommitted { |
| 70 | + /// The newly committed chain segment. |
| 71 | + new: Arc<C>, |
| 72 | + }, |
| 73 | + /// A chain segment was reverted. |
| 74 | + ChainReverted { |
| 75 | + /// The reverted chain segment. |
| 76 | + old: Arc<C>, |
| 77 | + }, |
| 78 | + /// A chain reorg occurred: one segment was reverted and replaced by |
| 79 | + /// another. |
| 80 | + ChainReorged { |
| 81 | + /// The reverted chain segment. |
| 82 | + old: Arc<C>, |
| 83 | + /// The newly committed chain segment. |
| 84 | + new: Arc<C>, |
| 85 | + }, |
| 86 | +} |
| 87 | + |
| 88 | +impl<C: Extractable> HostNotificationKind<C> { |
| 89 | + /// Returns the committed chain, if any. |
| 90 | + /// |
| 91 | + /// Returns `Some` for [`ChainCommitted`] and [`ChainReorged`], `None` |
| 92 | + /// for [`ChainReverted`]. |
| 93 | + /// |
| 94 | + /// [`ChainCommitted`]: HostNotificationKind::ChainCommitted |
| 95 | + /// [`ChainReorged`]: HostNotificationKind::ChainReorged |
| 96 | + /// [`ChainReverted`]: HostNotificationKind::ChainReverted |
| 97 | + pub const fn committed_chain(&self) -> Option<&Arc<C>> { |
| 98 | + match self { |
| 99 | + Self::ChainCommitted { new } | Self::ChainReorged { new, .. } => Some(new), |
| 100 | + Self::ChainReverted { .. } => None, |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + /// Returns the reverted chain, if any. |
| 105 | + /// |
| 106 | + /// Returns `Some` for [`ChainReverted`] and [`ChainReorged`], `None` |
| 107 | + /// for [`ChainCommitted`]. |
| 108 | + /// |
| 109 | + /// [`ChainReverted`]: HostNotificationKind::ChainReverted |
| 110 | + /// [`ChainReorged`]: HostNotificationKind::ChainReorged |
| 111 | + /// [`ChainCommitted`]: HostNotificationKind::ChainCommitted |
| 112 | + pub const fn reverted_chain(&self) -> Option<&Arc<C>> { |
| 113 | + match self { |
| 114 | + Self::ChainReverted { old } | Self::ChainReorged { old, .. } => Some(old), |
| 115 | + Self::ChainCommitted { .. } => None, |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments