kona_derive/types/
signals.rs

1//! Signal types for the `kona-derive` pipeline.
2//!
3//! Signals are the primary method of communication in the downwards direction
4//! of the pipeline. They allow the pipeline driver to perform actions such as
5//! resetting all stages in the pipeline through message passing.
6
7use kona_genesis::SystemConfig;
8use kona_protocol::{BlockInfo, L2BlockInfo};
9
10/// A signal to send to the pipeline.
11#[derive(Debug, Clone, Copy, PartialEq, Eq)]
12#[allow(clippy::large_enum_variant)]
13pub enum Signal {
14    /// Reset the pipeline.
15    Reset(ResetSignal),
16    /// Hardfork Activation.
17    Activation(ActivationSignal),
18    /// Flush the currently active channel.
19    FlushChannel,
20    /// Provide a new L1 block to the L1 traversal stage.
21    ProvideBlock(BlockInfo),
22}
23
24impl core::fmt::Display for Signal {
25    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
26        match self {
27            Self::Reset(_) => write!(f, "reset"),
28            Self::Activation(_) => write!(f, "activation"),
29            Self::FlushChannel => write!(f, "flush_channel"),
30            Self::ProvideBlock(_) => write!(f, "provide_block"),
31        }
32    }
33}
34
35impl Signal {
36    /// Sets the [`SystemConfig`] for the signal.
37    pub const fn with_system_config(self, system_config: SystemConfig) -> Self {
38        match self {
39            Self::Reset(reset) => reset.with_system_config(system_config).signal(),
40            Self::Activation(activation) => activation.with_system_config(system_config).signal(),
41            Self::FlushChannel => Self::FlushChannel,
42            Self::ProvideBlock(block) => Self::ProvideBlock(block),
43        }
44    }
45}
46
47/// A pipeline reset signal.
48#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
49pub struct ResetSignal {
50    /// The L2 safe head to reset to.
51    pub l2_safe_head: L2BlockInfo,
52    /// The L1 origin to reset to.
53    pub l1_origin: BlockInfo,
54    /// The optional [`SystemConfig`] to reset with.
55    pub system_config: Option<SystemConfig>,
56}
57
58impl ResetSignal {
59    /// Creates a new [Signal::Reset] from the [`ResetSignal`].
60    pub const fn signal(self) -> Signal {
61        Signal::Reset(self)
62    }
63
64    /// Sets the [`SystemConfig`] for the signal.
65    pub const fn with_system_config(self, system_config: SystemConfig) -> Self {
66        Self { system_config: Some(system_config), ..self }
67    }
68}
69
70/// A pipeline hardfork activation signal.
71#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
72pub struct ActivationSignal {
73    /// The L2 safe head to reset to.
74    pub l2_safe_head: L2BlockInfo,
75    /// The L1 origin to reset to.
76    pub l1_origin: BlockInfo,
77    /// The optional [`SystemConfig`] to reset with.
78    pub system_config: Option<SystemConfig>,
79}
80
81impl ActivationSignal {
82    /// Creates a new [Signal::Activation] from the [`ActivationSignal`].
83    pub const fn signal(self) -> Signal {
84        Signal::Activation(self)
85    }
86
87    /// Sets the [`SystemConfig`] for the signal.
88    pub const fn with_system_config(self, system_config: SystemConfig) -> Self {
89        Self { system_config: Some(system_config), ..self }
90    }
91}
92
93#[cfg(test)]
94mod tests {
95    use super::*;
96
97    #[test]
98    fn test_reset_signal() {
99        let signal = ResetSignal::default();
100        assert_eq!(signal.signal(), Signal::Reset(signal));
101    }
102
103    #[test]
104    fn test_activation_signal() {
105        let signal = ActivationSignal::default();
106        assert_eq!(signal.signal(), Signal::Activation(signal));
107    }
108
109    #[test]
110    fn test_signal_with_system_config() {
111        let signal = ResetSignal::default();
112        let system_config = SystemConfig::default();
113        assert_eq!(
114            signal.with_system_config(system_config).signal(),
115            Signal::Reset(ResetSignal { system_config: Some(system_config), ..signal })
116        );
117
118        let signal = ActivationSignal::default();
119        let system_config = SystemConfig::default();
120        assert_eq!(
121            signal.with_system_config(system_config).signal(),
122            Signal::Activation(ActivationSignal { system_config: Some(system_config), ..signal })
123        );
124
125        assert_eq!(Signal::FlushChannel.with_system_config(system_config), Signal::FlushChannel);
126    }
127}