protoblock 0.1.6

Asynchronous Bitcoin block ingestion pipeline with built-in reorg handling, backpressure, and observability
Documentation
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
use super::block::PreProcessedBlock;
use std::collections::HashMap;
use tokio::sync::{Mutex, Notify};

pub const BYTES_PER_MEGABYTE: usize = 1_048_576;

struct QueueEntry<T> {
    block: PreProcessedBlock<T>,
    size_bytes: usize,
}

struct QueueState<T> {
    next_expected: u64,
    blocks: HashMap<u64, QueueEntry<T>>,
    total_bytes: usize,
}

impl<T> QueueState<T> {
    fn new(next_expected: u64) -> Self {
        Self {
            next_expected,
            blocks: HashMap::new(),
            total_bytes: 0,
        }
    }
}

/// Async queue that only releases blocks in the expected order.
///
/// Enforces strictly sequential block delivery and applies backpressure when the configured
/// byte capacity is exceeded.
pub struct OrderedBlockQueue<T> {
    state: Mutex<QueueState<T>>,
    notify: Notify,
    max_bytes: usize,
}

impl<T> OrderedBlockQueue<T> {
    /// Creates a new queue starting at height 0 with unlimited capacity.
    pub fn new() -> Self {
        Self::with_start_and_capacity(0, usize::MAX)
    }

    /// Creates a new queue starting at the specified height with unlimited capacity.
    pub fn with_start(next_expected: u64) -> Self {
        Self::with_start_and_capacity(next_expected, usize::MAX)
    }

    /// Creates a new queue starting at height 0 with the specified byte capacity.
    pub fn with_capacity(max_bytes: usize) -> Self {
        Self::with_start_and_capacity(0, max_bytes)
    }

    /// Creates a new queue with the specified starting height and byte capacity.
    ///
    /// # Panics
    ///
    /// Panics if `max_bytes` is zero.
    pub fn with_start_and_capacity(next_expected: u64, max_bytes: usize) -> Self {
        assert!(max_bytes > 0, "max_bytes must be greater than zero");
        Self {
            state: Mutex::new(QueueState::new(next_expected)),
            notify: Notify::new(),
            max_bytes,
        }
    }

    /// Enqueues a block, blocking if the queue is at capacity.
    ///
    /// The block with height equal to `next_expected` bypasses the capacity limit to prevent deadlock.
    pub async fn push(&self, block: PreProcessedBlock<T>, size_bytes: usize) {
        let mut pending_block = Some(block);
        loop {
            let notified = self.notify.notified();
            let mut state = self.state.lock().await;
            let height = pending_block
                .as_ref()
                .expect("pending block should exist before enqueue")
                .height();
            let prospective_bytes = state.total_bytes.saturating_add(size_bytes);
            let queue_empty = state.blocks.is_empty();
            let is_next_expected = height == state.next_expected;
            if prospective_bytes <= self.max_bytes || queue_empty || is_next_expected {
                let block = pending_block
                    .take()
                    .expect("block should only be enqueued once");
                state
                    .blocks
                    .insert(height, QueueEntry { block, size_bytes });
                state.total_bytes = prospective_bytes;
                drop(state);
                self.notify.notify_waiters();
                return;
            }
            drop(state);
            notified.await;
        }
    }

    /// Waits for and returns the next expected block in sequence.
    ///
    /// Blocks until the expected block is available.
    pub async fn pop_next(&self) -> PreProcessedBlock<T> {
        loop {
            if let Some(block) = self.try_pop_next().await {
                self.notify.notify_waiters();
                return block;
            }
            #[cfg(test)]
            {
                // Provide a queue-specific identifier so gap probes only pause targeted queues.
                let queue_id = self as *const _ as usize;
                test_hooks::pause_in_gap(queue_id).await;
            }
            let notified = self.notify.notified();
            if let Some(block) = self.try_pop_next().await {
                self.notify.notify_waiters();
                return block;
            }
            notified.await;
        }
    }

    /// Attempts to pop the next expected block without blocking.
    ///
    /// Returns `None` if the expected block is not yet available.
    pub async fn try_pop_next(&self) -> Option<PreProcessedBlock<T>> {
        let mut state = self.state.lock().await;
        let expected = state.next_expected;
        let block = state.blocks.remove(&expected);
        if let Some(entry) = block {
            state.next_expected += 1;
            state.total_bytes = state.total_bytes.saturating_sub(entry.size_bytes);
            Some(entry.block)
        } else {
            None
        }
    }

    /// Removes all blocks from the queue and resets the byte counter.
    pub async fn clear(&self) {
        let mut state = self.state.lock().await;
        state.blocks.clear();
        state.total_bytes = 0;
        drop(state);
        self.notify.notify_waiters();
    }

    /// Resets the expected height and discards incompatible blocks.
    ///
    /// When rewinding (new height < current), all blocks are dropped.
    /// When fast-forwarding (new height > current), blocks below the new height are dropped.
    pub async fn reset_expected(&self, height: u64) {
        let mut state = self.state.lock().await;
        let previous_expected = state.next_expected;
        state.next_expected = height;

        if height < previous_expected {
            // Rewinding to an earlier height: drop any future blocks that were derived from a
            // now-stale branch so callers can safely replay from `height`.
            let mut removed_bytes = 0usize;
            state.blocks.retain(|&existing_height, entry| {
                let keep = existing_height < height;
                if !keep {
                    removed_bytes = removed_bytes.saturating_add(entry.size_bytes);
                }
                keep
            });
            state.total_bytes = state.total_bytes.saturating_sub(removed_bytes);
        } else {
            // Fast-forwarding: discard already-obsolete entries below the new starting point so we
            // only keep blocks that are at or beyond the requested height.
            let mut removed_bytes = 0usize;
            state.blocks.retain(|&existing_height, entry| {
                let keep = existing_height >= height;
                if !keep {
                    removed_bytes = removed_bytes.saturating_add(entry.size_bytes);
                }
                keep
            });
            state.total_bytes = state.total_bytes.saturating_sub(removed_bytes);
        }

        drop(state);
        self.notify.notify_waiters();
    }

    /// Returns the number of blocks currently in the queue.
    pub async fn len(&self) -> usize {
        self.state.lock().await.blocks.len()
    }

    /// Returns the total number of bytes currently held in the queue.
    pub async fn bytes(&self) -> usize {
        self.state.lock().await.total_bytes
    }

    /// Returns `true` if the queue contains no blocks.
    pub async fn is_empty(&self) -> bool {
        self.state.lock().await.blocks.is_empty()
    }

    /// Returns `true` if the next expected block is ready to be popped.
    pub async fn has_ready_block(&self) -> bool {
        let state = self.state.lock().await;
        state.blocks.contains_key(&state.next_expected)
    }

    /// Returns the height of the next expected block.
    pub async fn next_expected(&self) -> u64 {
        self.state.lock().await.next_expected
    }
}

impl<T> Default for OrderedBlockQueue<T> {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
pub(super) mod test_hooks {
    use once_cell::sync::Lazy;
    use std::sync::{Arc, Mutex};
    use tokio::sync::{oneshot, Notify};

    #[derive(Clone)]
    pub struct GapProbe {
        pub target_queue: usize,
        pub entered_signal: Arc<Mutex<Option<oneshot::Sender<()>>>>,
        pub resume: Arc<Notify>,
    }

    static GAP_PROBE: Lazy<Mutex<Option<GapProbe>>> = Lazy::new(|| Mutex::new(None));

    pub struct GapProbeGuard;

    impl Drop for GapProbeGuard {
        fn drop(&mut self) {
            GAP_PROBE.lock().unwrap().take();
        }
    }

    pub fn install_gap_probe(probe: GapProbe) -> GapProbeGuard {
        *GAP_PROBE.lock().unwrap() = Some(probe);
        GapProbeGuard
    }

    pub async fn pause_in_gap(queue_id: usize) {
        let probe = { GAP_PROBE.lock().unwrap().clone() };

        if let Some(probe) = probe {
            if probe.target_queue != queue_id {
                return;
            }

            if let Some(sender) = probe.entered_signal.lock().unwrap().take() {
                let _ = sender.send(());
            }
            probe.resume.notified().await;

            // Ensure the probe only pauses a single gap so other tests are not impacted.
            let mut guard = GAP_PROBE.lock().unwrap();
            let same_probe = guard
                .as_ref()
                .map(|current| {
                    Arc::ptr_eq(&current.entered_signal, &probe.entered_signal)
                        && Arc::ptr_eq(&current.resume, &probe.resume)
                })
                .unwrap_or(false);

            if same_probe {
                guard.take();
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::preprocessors::block::PreProcessedBlock;
    use bitcoin::hashes::Hash;
    use bitcoin::BlockHash;
    use std::sync::{Arc, Mutex};
    use tokio::sync::oneshot;
    use tokio::time::{sleep, timeout, Duration};

    fn dummy_hash(seed: u8) -> BlockHash {
        let mut bytes = [0u8; 32];
        bytes[0] = seed;
        BlockHash::from_slice(&bytes).expect("valid hash")
    }

    fn make_block(height: u64) -> PreProcessedBlock<String> {
        PreProcessedBlock::new(
            height,
            dummy_hash(height as u8),
            dummy_hash((height + 1) as u8),
            format!("data-{height}"),
        )
    }

    const TEST_BLOCK_BYTES: usize = 64;

    #[tokio::test]
    async fn pop_next_returns_in_order() {
        let queue = OrderedBlockQueue::new();
        queue.reset_expected(10).await;

        queue.push(make_block(12), TEST_BLOCK_BYTES).await;
        queue.push(make_block(11), TEST_BLOCK_BYTES).await;
        queue.push(make_block(10), TEST_BLOCK_BYTES).await;

        assert_eq!(queue.pop_next().await.height(), 10);
        assert_eq!(queue.pop_next().await.height(), 11);
        assert_eq!(queue.pop_next().await.height(), 12);
    }

    #[tokio::test]
    async fn try_pop_next_non_blocking() {
        let queue = OrderedBlockQueue::with_start(5);
        assert!(queue.try_pop_next().await.is_none());

        queue.push(make_block(5), TEST_BLOCK_BYTES).await;
        assert!(queue.try_pop_next().await.is_some());
        assert!(queue.try_pop_next().await.is_none());
    }

    #[tokio::test]
    async fn pop_next_blocks_until_ready() {
        let queue = Arc::new(OrderedBlockQueue::new());
        let cloned = queue.clone();

        let pop_future = tokio::spawn(async move { cloned.pop_next().await.height() });

        sleep(Duration::from_millis(25)).await;
        assert!(!pop_future.is_finished());

        queue.push(make_block(0), TEST_BLOCK_BYTES).await;

        let height = timeout(Duration::from_millis(250), pop_future)
            .await
            .expect("pop should finish")
            .expect("task should not fail");

        assert_eq!(height, 0);
    }

    #[tokio::test]
    async fn reset_expected_drops_future_blocks_when_rewinding() {
        let queue = OrderedBlockQueue::with_start(5);
        queue.push(make_block(5), TEST_BLOCK_BYTES).await;
        queue.push(make_block(6), TEST_BLOCK_BYTES).await;
        queue.push(make_block(10), TEST_BLOCK_BYTES).await;

        queue.reset_expected(3).await;

        assert_eq!(queue.len().await, 0, "future entries should be dropped");
        assert_eq!(queue.bytes().await, 0, "bytes should drop after rewind");
        assert!(
            queue.try_pop_next().await.is_none(),
            "queue should be empty"
        );
    }

    #[tokio::test]
    async fn reset_expected_keeps_future_blocks_when_fast_forwarding() {
        let queue = OrderedBlockQueue::with_start(0);
        queue.push(make_block(2), TEST_BLOCK_BYTES).await;
        queue.push(make_block(6), TEST_BLOCK_BYTES).await;

        queue.reset_expected(6).await;

        assert_eq!(
            queue.bytes().await,
            TEST_BLOCK_BYTES,
            "byte usage should match retained block"
        );
        let next = queue
            .try_pop_next()
            .await
            .expect("block at height 6 should remain");
        assert_eq!(next.height(), 6);
        assert_eq!(
            queue.len().await,
            0,
            "older entries should have been dropped"
        );
        assert_eq!(
            queue.bytes().await,
            0,
            "bytes should drop after draining retained block"
        );
    }

    #[tokio::test]
    async fn bytes_reflect_pending_payload() {
        let queue = OrderedBlockQueue::with_start(0);
        queue
            .push(make_block(0), TEST_BLOCK_BYTES.saturating_mul(2))
            .await;
        queue
            .push(make_block(1), TEST_BLOCK_BYTES.saturating_mul(3))
            .await;

        assert_eq!(queue.bytes().await, TEST_BLOCK_BYTES * 5);

        let _ = queue.pop_next().await;
        assert_eq!(queue.bytes().await, TEST_BLOCK_BYTES * 3);
    }

    #[tokio::test]
    async fn push_waits_when_queue_is_full() {
        let queue = Arc::new(OrderedBlockQueue::with_capacity(BYTES_PER_MEGABYTE));
        queue.reset_expected(0).await;

        queue.push(make_block(0), BYTES_PER_MEGABYTE).await;

        let cloned = queue.clone();
        let push_future = tokio::spawn(async move {
            cloned.push(make_block(1), BYTES_PER_MEGABYTE).await;
        });

        sleep(Duration::from_millis(25)).await;
        assert!(
            !push_future.is_finished(),
            "producer should wait while the queue is full"
        );

        assert_eq!(queue.pop_next().await.height(), 0);
        push_future.await.expect("push task should not panic");
        assert_eq!(queue.pop_next().await.height(), 1);
    }

    #[tokio::test]
    async fn next_expected_block_bypasses_capacity_limit() {
        let capacity = TEST_BLOCK_BYTES * 2;
        let queue = Arc::new(OrderedBlockQueue::with_capacity(capacity));
        queue.reset_expected(0).await;

        queue.push(make_block(1), TEST_BLOCK_BYTES).await;
        queue.push(make_block(2), TEST_BLOCK_BYTES).await;
        assert_eq!(queue.bytes().await, capacity);

        let cloned = queue.clone();
        let push_future = tokio::spawn(async move {
            cloned.push(make_block(0), TEST_BLOCK_BYTES).await;
        });

        timeout(Duration::from_millis(250), push_future)
            .await
            .expect("next-expected push should bypass byte cap")
            .expect("push task should not panic");

        assert_eq!(queue.bytes().await, capacity + TEST_BLOCK_BYTES);
        assert_eq!(queue.pop_next().await.height(), 0);
        assert_eq!(queue.bytes().await, capacity);
    }

    #[tokio::test]
    async fn queue_budget_tracks_preprocessed_payload() {
        let capacity = 1_024usize;
        let queue = Arc::new(OrderedBlockQueue::with_capacity(capacity));
        queue.reset_expected(0).await;

        let oversized_data = "x".repeat(capacity.saturating_mul(2));
        let block = PreProcessedBlock::new(0, dummy_hash(1), dummy_hash(2), oversized_data.clone());
        let block_bytes = block.queue_bytes();
        assert!(
            block_bytes > capacity,
            "pre-processed payload should exceed queue capacity"
        );
        queue.push(block, block_bytes).await;

        let cloned = queue.clone();
        let push_future = tokio::spawn(async move {
            let next = PreProcessedBlock::new(1, dummy_hash(3), dummy_hash(4), oversized_data);
            let next_bytes = next.queue_bytes();
            cloned.push(next, next_bytes).await;
        });

        sleep(Duration::from_millis(25)).await;
        assert!(
            !push_future.is_finished(),
            "queue should exert backpressure while full"
        );

        assert_eq!(queue.pop_next().await.height(), 0);
        push_future
            .await
            .expect("push task should resume once capacity frees");
        assert_eq!(queue.pop_next().await.height(), 1);
    }

    #[tokio::test]
    async fn oversized_block_still_fits_when_queue_is_empty() {
        let queue = OrderedBlockQueue::with_capacity(BYTES_PER_MEGABYTE);
        queue.reset_expected(0).await;

        let oversized = BYTES_PER_MEGABYTE.saturating_mul(2);
        queue.push(make_block(0), oversized).await;
        assert_eq!(queue.bytes().await, oversized);

        let block = queue.pop_next().await;
        assert_eq!(block.height(), 0);
        assert_eq!(queue.bytes().await, 0);
    }

    #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
    async fn pop_next_rechecks_after_registering_waiter() {
        let queue = Arc::new(OrderedBlockQueue::new());
        queue.reset_expected(0).await;

        let resume = Arc::new(Notify::new());
        let (entered_tx, entered_rx) = oneshot::channel();
        let target_queue = Arc::as_ptr(&queue) as usize;
        let _probe_guard = super::test_hooks::install_gap_probe(super::test_hooks::GapProbe {
            target_queue,
            entered_signal: Arc::new(Mutex::new(Some(entered_tx))),
            resume: resume.clone(),
        });

        let cloned = queue.clone();
        let pop_future = tokio::spawn(async move { cloned.pop_next().await.height() });

        entered_rx
            .await
            .expect("gap probe should signal waiter registration");
        queue.push(make_block(0), TEST_BLOCK_BYTES).await;
        resume.notify_waiters();

        let height = timeout(Duration::from_millis(250), pop_future)
            .await
            .expect("pop should finish")
            .expect("task should not fail");
        assert_eq!(height, 0);
    }
}