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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
use crate::rpc::options::DEFAULT_HTTP_BODY_LIMIT_BYTES;
use crate::rpc::payload::{estimate_request_body_bytes, single_block_response_body_bytes};
use crate::runtime::telemetry;
use anyhow::{bail, Context, Result};
use std::time::Duration;

const DEFAULT_RPC_TIMEOUT_SECS: u64 = 10;
const DEFAULT_QUEUE_MAX_SIZE_MB: usize = 200;
const DEFAULT_TIP_IDLE_BACKOFF_SECS: u64 = 10;
const DEFAULT_TIP_REFRESH_INTERVAL_SECS: u64 = 10;
const BITCOIN_MAX_BLOCK_BYTES: usize = 4 * 1024 * 1024;

/// Runtime configuration for the block fetcher pipeline.
///
/// All instances must be constructed via [`FetcherConfig::builder`] or [`FetcherConfig::new`]
/// so invariants are validated before any consumer observes the values.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FetcherConfig {
    rpc_url: String,
    rpc_user: String,
    rpc_password: String,
    thread_count: usize,
    max_batch_size_mb: usize,
    reorg_window_size: usize,
    start_height: u64,
    rpc_timeout: Duration,
    metrics_interval: Duration,
    queue_max_size_mb: usize,
    tip_idle_backoff: Duration,
    tip_refresh_interval: Duration,
    rpc_max_request_body_bytes: usize,
    rpc_max_response_body_bytes: usize,
}

pub struct FetcherConfigParams {
    pub rpc_url: String,
    pub rpc_user: String,
    pub rpc_password: String,
    pub thread_count: usize,
    pub max_batch_size_mb: usize,
    pub reorg_window_size: usize,
    pub start_height: u64,
    pub rpc_timeout: Duration,
    pub metrics_interval: Duration,
    pub queue_max_size_mb: usize,
    pub tip_idle_backoff: Duration,
    pub tip_refresh_interval: Duration,
    pub rpc_max_request_body_bytes: usize,
    pub rpc_max_response_body_bytes: usize,
}

impl FetcherConfig {
    /// Returns a builder to incrementally construct and validate a configuration.
    pub fn builder() -> FetcherConfigBuilder {
        FetcherConfigBuilder::default()
    }

    /// Constructs a configuration directly from the provided values.
    ///
    /// Prefer [`FetcherConfig::builder`] for ergonomics when many values use defaults.
    /// Callers that already have concrete runtime parameters can use this method to enforce
    /// validation without going through the builder.
    pub fn new(params: FetcherConfigParams) -> Result<Self> {
        let FetcherConfigParams {
            rpc_url,
            rpc_user,
            rpc_password,
            thread_count,
            max_batch_size_mb,
            reorg_window_size,
            start_height,
            rpc_timeout,
            metrics_interval,
            queue_max_size_mb,
            tip_idle_backoff,
            tip_refresh_interval,
            rpc_max_request_body_bytes,
            rpc_max_response_body_bytes,
        } = params;

        let config = Self {
            rpc_url: trimmed_string(rpc_url),
            rpc_user: trimmed_string(rpc_user),
            rpc_password: trimmed_string(rpc_password),
            thread_count,
            max_batch_size_mb,
            reorg_window_size,
            start_height,
            rpc_timeout,
            metrics_interval,
            queue_max_size_mb,
            tip_idle_backoff,
            tip_refresh_interval,
            rpc_max_request_body_bytes,
            rpc_max_response_body_bytes,
        };

        config.validate()?;
        Ok(config)
    }

    /// Full RPC URL (including scheme) configured for the fetcher.
    pub fn rpc_url(&self) -> &str {
        &self.rpc_url
    }

    /// RPC username.
    pub fn rpc_user(&self) -> &str {
        &self.rpc_user
    }

    /// RPC password.
    pub fn rpc_password(&self) -> &str {
        &self.rpc_password
    }

    /// Number of worker threads configured for the fetcher.
    pub fn thread_count(&self) -> usize {
        self.thread_count
    }

    /// Maximum megabytes per batch that workers target.
    pub fn max_batch_size_mb(&self) -> usize {
        self.max_batch_size_mb
    }

    /// Number of blocks tracked in the reorg window.
    pub fn reorg_window_size(&self) -> usize {
        self.reorg_window_size
    }

    /// Starting height requested when the pipeline boots.
    pub fn start_height(&self) -> u64 {
        self.start_height
    }

    /// Per-RPC timeout applied to the JSON-RPC client.
    pub fn rpc_timeout(&self) -> Duration {
        self.rpc_timeout
    }

    /// Interval used by the telemetry reporter.
    pub fn metrics_interval(&self) -> Duration {
        self.metrics_interval
    }

    /// Maximum megabytes of queued block data allowed in the ordered queue.
    pub fn queue_max_size_mb(&self) -> usize {
        self.queue_max_size_mb
    }

    /// Idle backoff used when workers poll near the blockchain tip.
    pub fn tip_idle_backoff(&self) -> Duration {
        self.tip_idle_backoff
    }

    /// Interval between background tip refresh RPC calls.
    pub fn tip_refresh_interval(&self) -> Duration {
        self.tip_refresh_interval
    }

    /// Maximum allowed HTTP request body bytes for RPC calls.
    pub fn rpc_max_request_body_bytes(&self) -> usize {
        self.rpc_max_request_body_bytes
    }

    /// Maximum allowed HTTP response body bytes for RPC calls.
    pub fn rpc_max_response_body_bytes(&self) -> usize {
        self.rpc_max_response_body_bytes
    }

    /// Performs validation on an existing configuration instance.
    pub fn validate(&self) -> Result<()> {
        validate_url(&self.rpc_url)?;
        ensure_not_empty(&self.rpc_user, "rpc_user")?;
        ensure_not_empty(&self.rpc_password, "rpc_password")?;

        if self.thread_count == 0 {
            bail!("thread_count must be greater than 0");
        }

        if self.max_batch_size_mb == 0 {
            bail!("max_batch_size_mb must be greater than 0");
        }

        if self.reorg_window_size == 0 {
            bail!("reorg_window_size must be greater than 0");
        }

        if self.rpc_timeout.is_zero() {
            bail!("rpc_timeout must be greater than 0");
        }

        if self.metrics_interval.is_zero() {
            bail!("metrics_interval must be greater than 0");
        }

        if self.queue_max_size_mb == 0 {
            bail!("queue_max_size_mb must be greater than 0");
        }

        if self.tip_idle_backoff.is_zero() {
            bail!("tip_idle_backoff must be greater than 0");
        }

        if self.tip_refresh_interval.is_zero() {
            bail!("tip_refresh_interval must be greater than 0");
        }

        if self.rpc_max_request_body_bytes == 0 {
            bail!("rpc_max_request_body_bytes must be greater than 0");
        }

        if self.rpc_max_response_body_bytes == 0 {
            bail!("rpc_max_response_body_bytes must be greater than 0");
        }

        let min_request_bytes = estimate_request_body_bytes(1);
        if self.rpc_max_request_body_bytes < min_request_bytes {
            bail!(
                "rpc_max_request_body_bytes ({}) must be at least {} bytes to fit one getblock \
                 request; increase PROTOBLOCK_MAX_REQUEST_MB",
                self.rpc_max_request_body_bytes,
                min_request_bytes,
            );
        }

        let max_block_hex_bytes = BITCOIN_MAX_BLOCK_BYTES.saturating_mul(2);
        let min_response_bytes = single_block_response_body_bytes(max_block_hex_bytes);
        if self.rpc_max_response_body_bytes < min_response_bytes {
            let min_response_mib = min_response_bytes as f64 / (1024.0 * 1024.0);
            bail!(
                "rpc_max_response_body_bytes ({}) must be at least {} bytes (~{:.2} MiB) to fit a \
                 full Bitcoin block; increase PROTOBLOCK_MAX_RESPONSE_MB",
                self.rpc_max_response_body_bytes,
                min_response_bytes,
                min_response_mib,
            );
        }

        Ok(())
    }
}

#[derive(Debug, Default, Clone)]
pub struct FetcherConfigBuilder {
    rpc_url: Option<String>,
    rpc_user: Option<String>,
    rpc_password: Option<String>,
    thread_count: Option<usize>,
    max_batch_size_mb: Option<usize>,
    reorg_window_size: Option<usize>,
    start_height: Option<u64>,
    rpc_timeout: Option<Duration>,
    metrics_interval: Option<Duration>,
    queue_max_size_mb: Option<usize>,
    tip_idle_backoff: Option<Duration>,
    tip_refresh_interval: Option<Duration>,
    rpc_max_request_body_bytes: Option<usize>,
    rpc_max_response_body_bytes: Option<usize>,
}

impl FetcherConfigBuilder {
    pub fn rpc_url(mut self, url: impl Into<String>) -> Self {
        self.rpc_url = Some(url.into());
        self
    }

    pub fn rpc_user(mut self, user: impl Into<String>) -> Self {
        self.rpc_user = Some(user.into());
        self
    }

    pub fn rpc_password(mut self, password: impl Into<String>) -> Self {
        self.rpc_password = Some(password.into());
        self
    }

    pub fn thread_count(mut self, count: usize) -> Self {
        self.thread_count = Some(count);
        self
    }

    pub fn max_batch_size_mb(mut self, max_mb: usize) -> Self {
        self.max_batch_size_mb = Some(max_mb);
        self
    }

    pub fn reorg_window_size(mut self, window: usize) -> Self {
        self.reorg_window_size = Some(window);
        self
    }

    pub fn start_height(mut self, height: u64) -> Self {
        self.start_height = Some(height);
        self
    }

    pub fn rpc_timeout(mut self, timeout: Duration) -> Self {
        self.rpc_timeout = Some(timeout);
        self
    }

    pub fn metrics_interval(mut self, interval: Duration) -> Self {
        self.metrics_interval = Some(interval);
        self
    }

    pub fn queue_max_size_mb(mut self, megabytes: usize) -> Self {
        self.queue_max_size_mb = Some(megabytes);
        self
    }

    pub fn tip_idle_backoff(mut self, backoff: Duration) -> Self {
        self.tip_idle_backoff = Some(backoff);
        self
    }

    pub fn tip_refresh_interval(mut self, interval: Duration) -> Self {
        self.tip_refresh_interval = Some(interval);
        self
    }

    pub fn rpc_max_request_body_bytes(mut self, bytes: usize) -> Self {
        self.rpc_max_request_body_bytes = Some(bytes);
        self
    }

    pub fn rpc_max_response_body_bytes(mut self, bytes: usize) -> Self {
        self.rpc_max_response_body_bytes = Some(bytes);
        self
    }

    pub fn build(self) -> Result<FetcherConfig> {
        let params = FetcherConfigParams {
            rpc_url: self.rpc_url.context("rpc_url is required")?,
            rpc_user: self.rpc_user.context("rpc_user is required")?,
            rpc_password: self.rpc_password.context("rpc_password is required")?,
            thread_count: self.thread_count.context("thread_count is required")?,
            max_batch_size_mb: self
                .max_batch_size_mb
                .context("max_batch_size_mb is required")?,
            reorg_window_size: self
                .reorg_window_size
                .context("reorg_window_size is required")?,
            start_height: self.start_height.context("start_height is required")?,
            rpc_timeout: self
                .rpc_timeout
                .unwrap_or_else(|| Duration::from_secs(DEFAULT_RPC_TIMEOUT_SECS)),
            metrics_interval: self
                .metrics_interval
                .unwrap_or(telemetry::DEFAULT_METRICS_INTERVAL),
            queue_max_size_mb: self.queue_max_size_mb.unwrap_or(DEFAULT_QUEUE_MAX_SIZE_MB),
            tip_idle_backoff: self
                .tip_idle_backoff
                .unwrap_or_else(|| Duration::from_secs(DEFAULT_TIP_IDLE_BACKOFF_SECS)),
            tip_refresh_interval: self
                .tip_refresh_interval
                .unwrap_or_else(|| Duration::from_secs(DEFAULT_TIP_REFRESH_INTERVAL_SECS)),
            rpc_max_request_body_bytes: self
                .rpc_max_request_body_bytes
                .unwrap_or(DEFAULT_HTTP_BODY_LIMIT_BYTES),
            rpc_max_response_body_bytes: self
                .rpc_max_response_body_bytes
                .unwrap_or(DEFAULT_HTTP_BODY_LIMIT_BYTES),
        };

        FetcherConfig::new(params)
    }
}

fn trimmed_string(value: String) -> String {
    value.trim().to_owned()
}

fn ensure_not_empty(value: &str, field: &str) -> Result<()> {
    if value.trim().is_empty() {
        bail!("{field} cannot be empty");
    }
    Ok(())
}

fn validate_url(url: &str) -> Result<()> {
    let url = url.trim();
    if !(url.starts_with("http://") || url.starts_with("https://")) {
        bail!("rpc_url must start with http:// or https://");
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::runtime::telemetry;
    use std::time::Duration;

    fn base_builder() -> FetcherConfigBuilder {
        FetcherConfig::builder()
            .rpc_url("http://localhost:8332")
            .rpc_user("user")
            .rpc_password("pass")
            .thread_count(4)
            .max_batch_size_mb(4)
            .reorg_window_size(7)
    }

    #[test]
    fn builder_produces_valid_config() {
        let config = base_builder().start_height(0).build().unwrap();
        assert_eq!(config.thread_count(), 4);
        assert_eq!(config.start_height(), 0);
        assert_eq!(config.queue_max_size_mb(), DEFAULT_QUEUE_MAX_SIZE_MB);
        assert_eq!(
            config.rpc_timeout(),
            Duration::from_secs(DEFAULT_RPC_TIMEOUT_SECS)
        );
        assert_eq!(
            config.metrics_interval(),
            telemetry::DEFAULT_METRICS_INTERVAL
        );
        assert_eq!(
            config.tip_idle_backoff(),
            Duration::from_secs(DEFAULT_TIP_IDLE_BACKOFF_SECS)
        );
        assert_eq!(
            config.tip_refresh_interval(),
            Duration::from_secs(DEFAULT_TIP_REFRESH_INTERVAL_SECS)
        );
        assert_eq!(
            config.rpc_max_request_body_bytes(),
            DEFAULT_HTTP_BODY_LIMIT_BYTES
        );
        assert_eq!(
            config.rpc_max_response_body_bytes(),
            DEFAULT_HTTP_BODY_LIMIT_BYTES
        );
    }

    #[test]
    fn metrics_interval_can_be_overridden() {
        let interval = Duration::from_secs(30);
        let queue_max_size_mb = 1024;
        let request_override = estimate_request_body_bytes(8);
        let max_block_hex_bytes = BITCOIN_MAX_BLOCK_BYTES * 2;
        let min_response_bytes = single_block_response_body_bytes(max_block_hex_bytes);
        let response_override = min_response_bytes.saturating_add(1_048_576);
        let config = base_builder()
            .metrics_interval(interval)
            .queue_max_size_mb(queue_max_size_mb)
            .rpc_max_request_body_bytes(request_override)
            .rpc_max_response_body_bytes(response_override)
            .start_height(0)
            .build()
            .expect("config should build");
        assert_eq!(config.metrics_interval(), interval);
        assert_eq!(config.queue_max_size_mb(), queue_max_size_mb);
        assert_eq!(config.rpc_max_request_body_bytes(), request_override);
        assert_eq!(config.rpc_max_response_body_bytes(), response_override);
    }

    #[test]
    fn tip_idle_backoff_can_be_overridden() {
        let backoff = Duration::from_secs(2);
        let config = base_builder()
            .tip_idle_backoff(backoff)
            .start_height(0)
            .build()
            .expect("config should build");
        assert_eq!(config.tip_idle_backoff(), backoff);
    }

    #[test]
    fn tip_refresh_interval_can_be_overridden() {
        let interval = Duration::from_secs(3);
        let config = base_builder()
            .tip_refresh_interval(interval)
            .start_height(0)
            .build()
            .expect("config should build");
        assert_eq!(config.tip_refresh_interval(), interval);
    }

    #[test]
    fn missing_required_fields_error() {
        let err = FetcherConfig::builder()
            .rpc_user("user")
            .rpc_password("pass")
            .thread_count(1)
            .max_batch_size_mb(1)
            .reorg_window_size(7)
            .start_height(0)
            .build()
            .unwrap_err();

        assert!(
            format!("{err}").contains("rpc_url"),
            "error should mention missing rpc_url"
        );
    }

    #[test]
    fn start_height_is_required() {
        let err = base_builder().build().unwrap_err();
        assert!(
            format!("{err}").contains("start_height"),
            "error should mention missing start_height"
        );
    }

    #[test]
    fn validation_catches_invalid_values() {
        let err = base_builder()
            .rpc_url("ftp://invalid")
            .start_height(10)
            .build()
            .unwrap_err();

        assert!(
            format!("{err}").contains("http:// or https://"),
            "error should mention URL scheme"
        );

        let err = base_builder()
            .thread_count(0)
            .start_height(10)
            .build()
            .unwrap_err();
        assert!(
            format!("{err}").contains("thread_count"),
            "error should mention thread count"
        );

        let err = base_builder()
            .rpc_timeout(Duration::from_secs(0))
            .start_height(10)
            .build()
            .unwrap_err();
        assert!(
            format!("{err}").contains("rpc_timeout"),
            "error should mention rpc_timeout"
        );

        let err = base_builder()
            .metrics_interval(Duration::from_secs(0))
            .start_height(10)
            .build()
            .unwrap_err();
        assert!(
            format!("{err}").contains("metrics_interval"),
            "error should mention metrics_interval"
        );

        let err = base_builder()
            .queue_max_size_mb(0)
            .start_height(10)
            .build()
            .unwrap_err();
        assert!(
            format!("{err}").contains("queue_max_size_mb"),
            "error should mention queue_max_size_mb"
        );

        let err = base_builder()
            .tip_idle_backoff(Duration::from_secs(0))
            .start_height(10)
            .build()
            .unwrap_err();
        assert!(
            format!("{err}").contains("tip_idle_backoff"),
            "error should mention tip_idle_backoff"
        );

        let err = base_builder()
            .tip_refresh_interval(Duration::from_secs(0))
            .start_height(10)
            .build()
            .unwrap_err();
        assert!(
            format!("{err}").contains("tip_refresh_interval"),
            "error should mention tip_refresh_interval"
        );

        let err = base_builder()
            .rpc_max_request_body_bytes(0)
            .start_height(10)
            .build()
            .unwrap_err();
        assert!(
            format!("{err}").contains("rpc_max_request_body_bytes"),
            "error should mention rpc_max_request_body_bytes"
        );

        let err = base_builder()
            .rpc_max_response_body_bytes(0)
            .start_height(10)
            .build()
            .unwrap_err();
        assert!(
            format!("{err}").contains("rpc_max_response_body_bytes"),
            "error should mention rpc_max_response_body_bytes"
        );
    }

    #[test]
    fn rpc_payload_limits_must_fit_single_block() {
        let min_request_bytes = estimate_request_body_bytes(1);
        let max_block_hex_bytes = BITCOIN_MAX_BLOCK_BYTES * 2;
        let min_response_bytes = single_block_response_body_bytes(max_block_hex_bytes);

        let err = base_builder()
            .rpc_max_request_body_bytes(min_request_bytes.saturating_sub(1))
            .rpc_max_response_body_bytes(min_response_bytes)
            .start_height(10)
            .build()
            .unwrap_err();
        assert!(
            format!("{err}").contains("PROTOBLOCK_MAX_REQUEST_MB"),
            "error should mention request env var"
        );

        let err = base_builder()
            .rpc_max_request_body_bytes(min_request_bytes)
            .rpc_max_response_body_bytes(min_response_bytes.saturating_sub(1))
            .start_height(10)
            .build()
            .unwrap_err();
        assert!(
            format!("{err}").contains("PROTOBLOCK_MAX_RESPONSE_MB"),
            "error should mention response env var"
        );
    }

    #[test]
    fn direct_constructor_runs_validation() {
        let err = FetcherConfig::new(FetcherConfigParams {
            rpc_url: "http://localhost:8332".into(),
            rpc_user: "user".into(),
            rpc_password: "pass".into(),
            thread_count: 0,
            max_batch_size_mb: 4,
            reorg_window_size: 7,
            start_height: 0,
            rpc_timeout: Duration::from_secs(DEFAULT_RPC_TIMEOUT_SECS),
            metrics_interval: telemetry::DEFAULT_METRICS_INTERVAL,
            queue_max_size_mb: DEFAULT_QUEUE_MAX_SIZE_MB,
            tip_idle_backoff: Duration::from_secs(DEFAULT_TIP_IDLE_BACKOFF_SECS),
            tip_refresh_interval: Duration::from_secs(DEFAULT_TIP_REFRESH_INTERVAL_SECS),
            rpc_max_request_body_bytes: DEFAULT_HTTP_BODY_LIMIT_BYTES,
            rpc_max_response_body_bytes: DEFAULT_HTTP_BODY_LIMIT_BYTES,
        })
        .unwrap_err();

        assert!(
            format!("{err}").contains("thread_count"),
            "error should mention invalid thread_count"
        );
    }
}