kona_derive/stages/
frame_queue.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//! This module contains the [FrameQueue] stage of the derivation pipeline.

use crate::{
    errors::PipelineError,
    stages::NextFrameProvider,
    traits::{OriginAdvancer, OriginProvider, SignalReceiver},
    types::{PipelineResult, Signal},
};
use alloc::{boxed::Box, collections::VecDeque, sync::Arc};
use alloy_primitives::Bytes;
use async_trait::async_trait;
use core::fmt::Debug;
use op_alloy_genesis::RollupConfig;
use op_alloy_protocol::{BlockInfo, Frame};
use tracing::{debug, error, trace};

/// Provides data frames for the [FrameQueue] stage.
#[async_trait]
pub trait FrameQueueProvider {
    /// An item that can be converted into a byte array.
    type Item: Into<Bytes>;

    /// Retrieves the next data item from the L1 retrieval stage.
    /// If there is data, it pushes it into the next stage.
    /// If there is no data, it returns an error.
    async fn next_data(&mut self) -> PipelineResult<Self::Item>;
}

/// The [FrameQueue] stage of the derivation pipeline.
/// This stage takes the output of the [L1Retrieval] stage and parses it into frames.
///
/// [L1Retrieval]: crate::stages::L1Retrieval
#[derive(Debug)]
pub struct FrameQueue<P>
where
    P: FrameQueueProvider + OriginAdvancer + OriginProvider + SignalReceiver + Debug,
{
    /// The previous stage in the pipeline.
    pub prev: P,
    /// The current frame queue.
    queue: VecDeque<Frame>,
    /// The rollup config.
    rollup_config: Arc<RollupConfig>,
}

impl<P> FrameQueue<P>
where
    P: FrameQueueProvider + OriginAdvancer + OriginProvider + SignalReceiver + Debug,
{
    /// Create a new [FrameQueue] stage with the given previous [L1Retrieval] stage.
    ///
    /// [L1Retrieval]: crate::stages::L1Retrieval
    pub const fn new(prev: P, cfg: Arc<RollupConfig>) -> Self {
        Self { prev, queue: VecDeque::new(), rollup_config: cfg }
    }

    /// Returns if holocene is active.
    pub fn is_holocene_active(&self, origin: BlockInfo) -> bool {
        self.rollup_config.is_holocene_active(origin.timestamp)
    }

    /// Prunes frames if Holocene is active.
    pub fn prune(&mut self, origin: BlockInfo) {
        if !self.is_holocene_active(origin) {
            return;
        }

        let mut i = 0;
        while i < self.queue.len() - 1 {
            let prev_frame = &self.queue[i];
            let next_frame = &self.queue[i + 1];
            let extends_channel = prev_frame.id == next_frame.id;

            // If the frames are in the same channel, and the frame numbers are not sequential,
            // drop the next frame.
            if extends_channel && prev_frame.number + 1 != next_frame.number {
                self.queue.remove(i + 1);
                continue;
            }

            // If the frames are in the same channel, and the previous is last, drop the next frame.
            if extends_channel && prev_frame.is_last {
                self.queue.remove(i + 1);
                continue;
            }

            // If the frames are in different channels, the next frame must be first.
            if !extends_channel && next_frame.number != 0 {
                self.queue.remove(i + 1);
                continue;
            }

            // If the frames are in different channels, and the current channel is not last, walk
            // back the channel and drop all prev frames.
            if !extends_channel && !prev_frame.is_last && next_frame.number == 0 {
                // Find the index of the first frame in the queue with the same channel ID
                // as the previous frame.
                let first_frame =
                    self.queue.iter().position(|f| f.id == prev_frame.id).expect("infallible");

                // Drain all frames from the previous channel.
                let drained = self.queue.drain(first_frame..=i);
                i = i.saturating_sub(drained.len());
                continue;
            }

            i += 1;
        }
    }

    /// Loads more frames into the [FrameQueue].
    pub async fn load_frames(&mut self) -> PipelineResult<()> {
        // Skip loading frames if the queue is not empty.
        if !self.queue.is_empty() {
            return Ok(());
        }

        let data = match self.prev.next_data().await {
            Ok(data) => data,
            Err(e) => {
                debug!(target: "frame-queue", "Failed to retrieve data: {:?}", e);
                // SAFETY: Bubble up potential EOF error without wrapping.
                return Err(e);
            }
        };

        let Ok(frames) = Frame::parse_frames(&data.into()) else {
            // There may be more frames in the queue for the
            // pipeline to advance, so don't return an error here.
            error!(target: "frame-queue", "Failed to parse frames from data.");
            return Ok(());
        };

        // Optimistically extend the queue with the new frames.
        self.queue.extend(frames);

        // Prune frames if Holocene is active.
        let origin = self.origin().ok_or(PipelineError::MissingOrigin.crit())?;
        self.prune(origin);

        Ok(())
    }
}

#[async_trait]
impl<P> OriginAdvancer for FrameQueue<P>
where
    P: FrameQueueProvider + OriginAdvancer + OriginProvider + SignalReceiver + Send + Debug,
{
    async fn advance_origin(&mut self) -> PipelineResult<()> {
        self.prev.advance_origin().await
    }
}

#[async_trait]
impl<P> NextFrameProvider for FrameQueue<P>
where
    P: FrameQueueProvider + OriginAdvancer + OriginProvider + SignalReceiver + Send + Debug,
{
    async fn next_frame(&mut self) -> PipelineResult<Frame> {
        self.load_frames().await?;

        // If we did not add more frames but still have more data, retry this function.
        if self.queue.is_empty() {
            trace!(target: "frame-queue", "Queue is empty after fetching data. Retrying next_frame.");
            return Err(PipelineError::NotEnoughData.temp());
        }

        Ok(self.queue.pop_front().expect("Frame queue impossibly empty"))
    }
}

impl<P> OriginProvider for FrameQueue<P>
where
    P: FrameQueueProvider + OriginAdvancer + OriginProvider + SignalReceiver + Debug,
{
    fn origin(&self) -> Option<BlockInfo> {
        self.prev.origin()
    }
}

#[async_trait]
impl<P> SignalReceiver for FrameQueue<P>
where
    P: FrameQueueProvider + OriginAdvancer + OriginProvider + SignalReceiver + Send + Debug,
{
    async fn signal(&mut self, signal: Signal) -> PipelineResult<()> {
        self.prev.signal(signal).await?;
        self.queue = VecDeque::default();
        Ok(())
    }
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;
    use crate::{test_utils::TestFrameQueueProvider, types::ResetSignal};
    use alloc::vec;

    #[tokio::test]
    async fn test_frame_queue_reset() {
        let mock = TestFrameQueueProvider::new(vec![]);
        let mut frame_queue = FrameQueue::new(mock, Default::default());
        assert!(!frame_queue.prev.reset);
        frame_queue.signal(ResetSignal::default().signal()).await.unwrap();
        assert_eq!(frame_queue.queue.len(), 0);
        assert!(frame_queue.prev.reset);
    }

    #[tokio::test]
    async fn test_frame_queue_empty_bytes() {
        let data = vec![Ok(Bytes::from(vec![0x00]))];
        let mut mock = TestFrameQueueProvider::new(data);
        mock.set_origin(BlockInfo::default());
        let mut frame_queue = FrameQueue::new(mock, Default::default());
        assert!(!frame_queue.is_holocene_active(BlockInfo::default()));
        let err = frame_queue.next_frame().await.unwrap_err();
        assert_eq!(err, PipelineError::NotEnoughData.temp());
    }

    #[tokio::test]
    async fn test_frame_queue_no_frames_decoded() {
        let data = vec![Err(PipelineError::Eof.temp()), Ok(Bytes::default())];
        let mut mock = TestFrameQueueProvider::new(data);
        mock.set_origin(BlockInfo::default());
        let mut frame_queue = FrameQueue::new(mock, Default::default());
        assert!(!frame_queue.is_holocene_active(BlockInfo::default()));
        let err = frame_queue.next_frame().await.unwrap_err();
        assert_eq!(err, PipelineError::NotEnoughData.temp());
    }

    #[tokio::test]
    async fn test_frame_queue_wrong_derivation_version() {
        let assert = crate::test_utils::FrameQueueBuilder::new()
            .with_origin(BlockInfo::default())
            .with_raw_frames(Bytes::from(vec![0x01]))
            .with_expected_err(PipelineError::NotEnoughData.temp())
            .build();
        assert.holocene_active(false);
        assert.next_frames().await;
    }

    #[tokio::test]
    async fn test_frame_queue_frame_too_short() {
        let assert = crate::test_utils::FrameQueueBuilder::new()
            .with_origin(BlockInfo::default())
            .with_raw_frames(Bytes::from(vec![0x00, 0x01]))
            .with_expected_err(PipelineError::NotEnoughData.temp())
            .build();
        assert.holocene_active(false);
        assert.next_frames().await;
    }

    #[tokio::test]
    async fn test_frame_queue_single_frame() {
        let frames = [crate::frame!(0xFF, 0, vec![0xDD; 50], true)];
        let assert = crate::test_utils::FrameQueueBuilder::new()
            .with_expected_frames(&frames)
            .with_origin(BlockInfo::default())
            .with_frames(&frames)
            .build();
        assert.holocene_active(false);
        assert.next_frames().await;
    }

    #[tokio::test]
    async fn test_frame_queue_multiple_frames() {
        let frames = [
            crate::frame!(0xFF, 0, vec![0xDD; 50], false),
            crate::frame!(0xFF, 1, vec![0xDD; 50], false),
            crate::frame!(0xFF, 2, vec![0xDD; 50], true),
        ];
        let assert = crate::test_utils::FrameQueueBuilder::new()
            .with_expected_frames(&frames)
            .with_origin(BlockInfo::default())
            .with_frames(&frames)
            .build();
        assert.holocene_active(false);
        assert.next_frames().await;
    }

    #[tokio::test]
    async fn test_frame_queue_missing_origin() {
        let frames = [crate::frame!(0xFF, 0, vec![0xDD; 50], true)];
        let assert = crate::test_utils::FrameQueueBuilder::new()
            .with_expected_frames(&frames)
            .with_frames(&frames)
            .build();
        assert.holocene_active(false);
        assert.missing_origin().await;
    }

    #[tokio::test]
    async fn test_holocene_valid_frames() {
        let frames = [
            crate::frame!(0xFF, 0, vec![0xDD; 50], false),
            crate::frame!(0xFF, 1, vec![0xDD; 50], false),
            crate::frame!(0xFF, 2, vec![0xDD; 50], true),
        ];
        let assert = crate::test_utils::FrameQueueBuilder::new()
            .with_rollup_config(&RollupConfig { holocene_time: Some(0), ..Default::default() })
            .with_origin(BlockInfo::default())
            .with_expected_frames(&frames)
            .with_frames(&frames)
            .build();
        assert.holocene_active(true);
        assert.next_frames().await;
    }

    #[tokio::test]
    async fn test_holocene_single_frame() {
        let frames = [crate::frame!(0xFF, 1, vec![0xDD; 50], true)];
        let assert = crate::test_utils::FrameQueueBuilder::new()
            .with_rollup_config(&RollupConfig { holocene_time: Some(0), ..Default::default() })
            .with_origin(BlockInfo::default())
            .with_expected_frames(&frames)
            .with_frames(&frames)
            .build();
        assert.holocene_active(true);
        assert.next_frames().await;
    }

    #[tokio::test]
    async fn test_holocene_unordered_frames() {
        let frames = [
            // -- First Channel --
            crate::frame!(0xEE, 0, vec![0xDD; 50], false),
            crate::frame!(0xEE, 1, vec![0xDD; 50], false),
            crate::frame!(0xEE, 2, vec![0xDD; 50], true),
            crate::frame!(0xEE, 3, vec![0xDD; 50], false), // Dropped
            // -- Next Channel --
            crate::frame!(0xFF, 0, vec![0xDD; 50], false),
            crate::frame!(0xFF, 1, vec![0xDD; 50], true),
        ];
        let assert = crate::test_utils::FrameQueueBuilder::new()
            .with_rollup_config(&RollupConfig { holocene_time: Some(0), ..Default::default() })
            .with_origin(BlockInfo::default())
            .with_expected_frames(&[&frames[0..3], &frames[4..]].concat())
            .with_frames(&frames)
            .build();
        assert.holocene_active(true);
        assert.next_frames().await;
    }

    #[tokio::test]
    async fn test_holocene_non_sequential_frames() {
        let frames = [
            // -- First Channel --
            crate::frame!(0xEE, 0, vec![0xDD; 50], false),
            crate::frame!(0xEE, 1, vec![0xDD; 50], false),
            crate::frame!(0xEE, 3, vec![0xDD; 50], true), // Dropped
            crate::frame!(0xEE, 4, vec![0xDD; 50], false), // Dropped
        ];
        let assert = crate::test_utils::FrameQueueBuilder::new()
            .with_rollup_config(&RollupConfig { holocene_time: Some(0), ..Default::default() })
            .with_origin(BlockInfo::default())
            .with_expected_frames(&frames[0..2])
            .with_frames(&frames)
            .build();
        assert.holocene_active(true);
        assert.next_frames().await;
    }

    #[tokio::test]
    async fn test_holocene_unclosed_channel() {
        let frames = [
            // -- First Channel --
            crate::frame!(0xEE, 0, vec![0xDD; 50], false),
            crate::frame!(0xEE, 1, vec![0xDD; 50], false),
            crate::frame!(0xEE, 2, vec![0xDD; 50], false),
            crate::frame!(0xEE, 3, vec![0xDD; 50], false),
            // -- Next Channel --
            crate::frame!(0xFF, 0, vec![0xDD; 50], false),
            crate::frame!(0xFF, 1, vec![0xDD; 50], true),
        ];
        let assert = crate::test_utils::FrameQueueBuilder::new()
            .with_rollup_config(&RollupConfig { holocene_time: Some(0), ..Default::default() })
            .with_origin(BlockInfo::default())
            .with_expected_frames(&frames[4..])
            .with_frames(&frames)
            .build();
        assert.holocene_active(true);
        assert.next_frames().await;
    }

    #[tokio::test]
    async fn test_holocene_unstarted_channel() {
        let frames = [
            // -- First Channel --
            crate::frame!(0xDD, 0, vec![0xDD; 50], false),
            crate::frame!(0xDD, 1, vec![0xDD; 50], false),
            crate::frame!(0xDD, 2, vec![0xDD; 50], false),
            crate::frame!(0xDD, 3, vec![0xDD; 50], true),
            // -- Second Channel --
            crate::frame!(0xEE, 1, vec![0xDD; 50], false), // Dropped
            crate::frame!(0xEE, 2, vec![0xDD; 50], true),  // Dropped
            // -- Third Channel --
            crate::frame!(0xFF, 0, vec![0xDD; 50], false),
            crate::frame!(0xFF, 1, vec![0xDD; 50], true),
        ];
        let assert = crate::test_utils::FrameQueueBuilder::new()
            .with_rollup_config(&RollupConfig { holocene_time: Some(0), ..Default::default() })
            .with_origin(BlockInfo::default())
            .with_expected_frames(&[&frames[0..4], &frames[6..]].concat())
            .with_frames(&frames)
            .build();
        assert.holocene_active(true);
        assert.next_frames().await;
    }

    #[tokio::test]
    async fn test_holocene_unclosed_channel_with_invalid_start() {
        let frames = [
            // -- First Channel --
            crate::frame!(0xEE, 0, vec![0xDD; 50], false),
            crate::frame!(0xEE, 1, vec![0xDD; 50], false),
            crate::frame!(0xEE, 2, vec![0xDD; 50], false),
            crate::frame!(0xEE, 3, vec![0xDD; 50], false),
            // -- Next Channel --
            crate::frame!(0xFF, 1, vec![0xDD; 50], false), // Dropped
            crate::frame!(0xFF, 2, vec![0xDD; 50], true),  // Dropped
        ];
        let assert = crate::test_utils::FrameQueueBuilder::new()
            .with_rollup_config(&RollupConfig { holocene_time: Some(0), ..Default::default() })
            .with_origin(BlockInfo::default())
            .with_expected_frames(&frames[0..4])
            .with_frames(&frames)
            .build();
        assert.holocene_active(true);
        assert.next_frames().await;
    }

    #[tokio::test]
    async fn test_holocene_replace_channel() {
        let frames = [
            // -- First Channel - VALID & CLOSED --
            crate::frame!(0xDD, 0, vec![0xDD; 50], false),
            crate::frame!(0xDD, 1, vec![0xDD; 50], true),
            // -- Second Channel - VALID & NOT CLOSED / DROPPED --
            crate::frame!(0xEE, 0, vec![0xDD; 50], false),
            crate::frame!(0xEE, 1, vec![0xDD; 50], false),
            // -- Third Channel - VALID & CLOSED / REPLACES CHANNEL #2 --
            crate::frame!(0xFF, 0, vec![0xDD; 50], false),
            crate::frame!(0xFF, 1, vec![0xDD; 50], true),
        ];
        let assert = crate::test_utils::FrameQueueBuilder::new()
            .with_rollup_config(&RollupConfig { holocene_time: Some(0), ..Default::default() })
            .with_origin(BlockInfo::default())
            .with_expected_frames(&[&frames[0..2], &frames[4..]].concat())
            .with_frames(&frames)
            .build();
        assert.holocene_active(true);
        assert.next_frames().await;
    }

    #[tokio::test]
    async fn test_holocene_interleaved_invalid_channel() {
        let frames = [
            // -- First channel is dropped since it is replaced by the second channel --
            // -- Second channel is dropped since it isn't closed --
            crate::frame!(0x01, 0, vec![0xDD; 50], false),
            crate::frame!(0x02, 0, vec![0xDD; 50], false),
            crate::frame!(0x01, 1, vec![0xDD; 50], true),
            crate::frame!(0x02, 1, vec![0xDD; 50], false),
            // -- Third Channel - VALID & CLOSED --
            crate::frame!(0xFF, 0, vec![0xDD; 50], false),
            crate::frame!(0xFF, 1, vec![0xDD; 50], true),
        ];
        let assert = crate::test_utils::FrameQueueBuilder::new()
            .with_rollup_config(&RollupConfig { holocene_time: Some(0), ..Default::default() })
            .with_origin(BlockInfo::default())
            .with_expected_frames(&frames[4..])
            .with_frames(&frames)
            .build();
        assert.holocene_active(true);
        assert.next_frames().await;
    }

    #[tokio::test]
    async fn test_holocene_interleaved_valid_channel() {
        let frames = [
            // -- First channel is dropped since it is replaced by the second channel --
            // -- Second channel is successfully closed so it's valid --
            crate::frame!(0x01, 0, vec![0xDD; 50], false),
            crate::frame!(0x02, 0, vec![0xDD; 50], false),
            crate::frame!(0x01, 1, vec![0xDD; 50], true),
            crate::frame!(0x02, 1, vec![0xDD; 50], true),
            // -- Third Channel - VALID & CLOSED --
            crate::frame!(0xFF, 0, vec![0xDD; 50], false),
            crate::frame!(0xFF, 1, vec![0xDD; 50], true),
        ];
        let assert = crate::test_utils::FrameQueueBuilder::new()
            .with_rollup_config(&RollupConfig { holocene_time: Some(0), ..Default::default() })
            .with_origin(BlockInfo::default())
            .with_expected_frames(&[&frames[1..2], &frames[3..]].concat())
            .with_frames(&frames)
            .build();
        assert.holocene_active(true);
        assert.next_frames().await;
    }
}