kona_derive/types/
signals.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//! Signal types for the `kona-derive` pipeline.
//!
//! Signals are the primary method of communication in the downwards direction
//! of the pipeline. They allow the pipeline driver to perform actions such as
//! resetting all stages in the pipeline through message passing.

use op_alloy_genesis::SystemConfig;
use op_alloy_protocol::{BlockInfo, L2BlockInfo};

/// A signal to send to the pipeline.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(clippy::large_enum_variant)]
pub enum Signal {
    /// Reset the pipeline.
    Reset(ResetSignal),
    /// Hardfork Activation.
    Activation(ActivationSignal),
    /// Flush the currently active channel.
    FlushChannel,
}

impl Signal {
    /// Sets the [SystemConfig] for the signal.
    pub const fn with_system_config(self, system_config: SystemConfig) -> Self {
        match self {
            Self::Reset(reset) => reset.with_system_config(system_config).signal(),
            Self::Activation(activation) => activation.with_system_config(system_config).signal(),
            Self::FlushChannel => Self::FlushChannel,
        }
    }
}

/// A pipeline reset signal.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct ResetSignal {
    /// The L2 safe head to reset to.
    pub l2_safe_head: L2BlockInfo,
    /// The L1 origin to reset to.
    pub l1_origin: BlockInfo,
    /// The optional [SystemConfig] to reset with.
    pub system_config: Option<SystemConfig>,
}

impl ResetSignal {
    /// Creates a new [Signal::Reset] from the [ResetSignal].
    pub const fn signal(self) -> Signal {
        Signal::Reset(self)
    }

    /// Sets the [SystemConfig] for the signal.
    pub const fn with_system_config(self, system_config: SystemConfig) -> Self {
        Self { system_config: Some(system_config), ..self }
    }
}

/// A pipeline hardfork activation signal.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct ActivationSignal {
    /// The L2 safe head to reset to.
    pub l2_safe_head: L2BlockInfo,
    /// The L1 origin to reset to.
    pub l1_origin: BlockInfo,
    /// The optional [SystemConfig] to reset with.
    pub system_config: Option<SystemConfig>,
}

impl ActivationSignal {
    /// Creates a new [Signal::Activation] from the [ActivationSignal].
    pub const fn signal(self) -> Signal {
        Signal::Activation(self)
    }

    /// Sets the [SystemConfig] for the signal.
    pub const fn with_system_config(self, system_config: SystemConfig) -> Self {
        Self { system_config: Some(system_config), ..self }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_reset_signal() {
        let signal = ResetSignal::default();
        assert_eq!(signal.signal(), Signal::Reset(signal));
    }

    #[test]
    fn test_activation_signal() {
        let signal = ActivationSignal::default();
        assert_eq!(signal.signal(), Signal::Activation(signal));
    }

    #[test]
    fn test_signal_with_system_config() {
        let signal = ResetSignal::default();
        let system_config = SystemConfig::default();
        assert_eq!(
            signal.with_system_config(system_config).signal(),
            Signal::Reset(ResetSignal { system_config: Some(system_config), ..signal })
        );

        let signal = ActivationSignal::default();
        let system_config = SystemConfig::default();
        assert_eq!(
            signal.with_system_config(system_config).signal(),
            Signal::Activation(ActivationSignal { system_config: Some(system_config), ..signal })
        );

        assert_eq!(Signal::FlushChannel.with_system_config(system_config), Signal::FlushChannel);
    }
}