zeldhash-parser 0.5.1

High-performance Bitcoin blockchain parser implementing the ZeldHash protocol
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
use std::path::Path;

use anyhow::{anyhow, Context, Result};
use rollblock::{
    orchestrator::DurabilityMode, BlockStoreFacade, RemoteServerSettings, StoreConfig,
};

use crate::cli::{
    RollblockDurabilityKind, RollblockMode, RollblockOptions,
    DEFAULT_ROLLBLOCK_ASYNC_MAX_PENDING_BLOCKS, DEFAULT_ROLLBLOCK_ASYNC_RELAXED_SYNC_EVERY,
    DEFAULT_ROLLBLOCK_COMPRESS_JOURNAL, DEFAULT_ROLLBLOCK_INITIAL_CAPACITY,
    DEFAULT_ROLLBLOCK_REMOTE_PASSWORD, DEFAULT_ROLLBLOCK_REMOTE_PORT,
    DEFAULT_ROLLBLOCK_REMOTE_USER, DEFAULT_ROLLBLOCK_SHARDS_COUNT,
    DEFAULT_ROLLBLOCK_SYNCHRONOUS_RELAXED_SYNC_EVERY, DEFAULT_ROLLBLOCK_THREAD_COUNT,
};

use super::overlay::Overlay;

#[derive(Debug, Clone)]
pub struct RollblockSettings {
    pub mode: RollblockMode,
    pub store_config: StoreConfig,
}

impl RollblockOptions {
    pub fn build(self, data_dir: &Path) -> Result<RollblockSettings> {
        let rollblock_data_dir = data_dir.join("utxodb");
        let mode = if rollblock_data_dir.exists() {
            RollblockMode::Existing
        } else {
            RollblockMode::New
        };

        let mut config = match mode {
            RollblockMode::New => {
                let shards = self.shards_count.unwrap_or(DEFAULT_ROLLBLOCK_SHARDS_COUNT);
                let capacity = self
                    .initial_capacity
                    .unwrap_or(DEFAULT_ROLLBLOCK_INITIAL_CAPACITY);
                let thread_count = self.thread_count.unwrap_or(DEFAULT_ROLLBLOCK_THREAD_COUNT);
                let compress = self
                    .compress_journal
                    .unwrap_or(DEFAULT_ROLLBLOCK_COMPRESS_JOURNAL);
                StoreConfig::new(
                    &rollblock_data_dir,
                    shards,
                    capacity,
                    thread_count,
                    compress,
                )
                .context("failed to create rollblock StoreConfig")?
            }
            RollblockMode::Existing => {
                let mut cfg = StoreConfig::existing(&rollblock_data_dir);
                if let Some(shards) = self.shards_count {
                    let capacity = self.initial_capacity.ok_or_else(|| {
                        anyhow!(
                            "rollblock_initial_capacity is required when overriding shard layout"
                        )
                    })?;
                    cfg = cfg
                        .with_shard_layout(shards, capacity)
                        .context("invalid shard layout override")?;
                }
                if let Some(thread_count) = self.thread_count {
                    cfg.thread_count = thread_count;
                }
                if let Some(compress) = self.compress_journal {
                    cfg = cfg.with_journal_compression(compress);
                }
                cfg
            }
        };

        if let Some(interval) = self.snapshot_interval {
            config = config.with_snapshot_interval(interval);
        }
        if let Some(interval) = self.max_snapshot_interval {
            config = config.with_max_snapshot_interval(interval);
        }
        if let Some(level) = self.journal_compression_level {
            config = config.with_journal_compression_level(level);
        }
        if let Some(bytes) = self.journal_chunk_size_bytes {
            config = config.with_journal_chunk_size(bytes);
        }
        if let Some(size) = self.lmdb_map_size {
            config = config.with_lmdb_map_size(size);
        }
        if let Some(window) = self.min_rollback_window {
            config = config
                .with_min_rollback_window(window)
                .context("invalid rollblock_min_rollback_window")?;
        }
        if let Some(interval) = self.prune_interval {
            config = config
                .with_prune_interval(interval)
                .context("invalid rollblock_prune_interval")?;
        }
        if let Some(profile) = self.bootstrap_block_profile {
            config = config
                .with_bootstrap_block_profile(profile)
                .context("invalid rollblock_bootstrap_block_profile")?;
        }

        let async_pending = self
            .async_max_pending_blocks
            .unwrap_or(DEFAULT_ROLLBLOCK_ASYNC_MAX_PENDING_BLOCKS);
        let async_relaxed_sync_every = self
            .async_relaxed_sync_every
            .unwrap_or(DEFAULT_ROLLBLOCK_ASYNC_RELAXED_SYNC_EVERY);
        let synchronous_relaxed_sync_every = self
            .synchronous_relaxed_sync_every
            .unwrap_or(DEFAULT_ROLLBLOCK_SYNCHRONOUS_RELAXED_SYNC_EVERY);

        let mode_kind = self
            .durability_mode
            .unwrap_or(RollblockDurabilityKind::AsyncRelaxed);

        config = match mode_kind {
            RollblockDurabilityKind::Async => config.with_async_max_pending(async_pending),
            RollblockDurabilityKind::AsyncRelaxed => {
                config.with_async_relaxed(async_pending, async_relaxed_sync_every)
            }
            RollblockDurabilityKind::Synchronous => {
                config.with_durability_mode(DurabilityMode::Synchronous)
            }
            RollblockDurabilityKind::SynchronousRelaxed => {
                config.with_durability_mode(DurabilityMode::SynchronousRelaxed {
                    sync_every_n_blocks: synchronous_relaxed_sync_every.max(1),
                })
            }
        };

        let remote_username = self
            .user
            .unwrap_or_else(|| DEFAULT_ROLLBLOCK_REMOTE_USER.to_string());
        let remote_password = self
            .password
            .unwrap_or_else(|| DEFAULT_ROLLBLOCK_REMOTE_PASSWORD.to_string());
        let remote_port = self.port.unwrap_or(DEFAULT_ROLLBLOCK_REMOTE_PORT);

        let mut remote_settings =
            RemoteServerSettings::default().with_basic_auth(remote_username, remote_password);
        remote_settings.bind_address.set_port(remote_port);

        config = config.with_remote_server(remote_settings);
        config = config
            .enable_remote_server()
            .context("failed to enable rollblock remote server")?;

        Ok(RollblockSettings {
            mode,
            store_config: config,
        })
    }
}

impl RollblockSettings {
    pub fn determine_start_height(&self) -> Result<u64> {
        if matches!(self.mode, RollblockMode::New) {
            return Ok(0);
        }

        self.read_start_height_from_store()
    }

    fn read_start_height_from_store(&self) -> Result<u64> {
        let mut peek_config = self.store_config.clone();
        peek_config.enable_server = false;
        peek_config.remote_server = None;

        let store = BlockStoreFacade::new(peek_config)
            .context("failed to open rollblock store to determine start height")?;
        let current_block = store
            .current_block()
            .context("failed to read rollblock current block height")?;
        store
            .close()
            .context("failed to close rollblock store after determining start height")?;

        current_block.checked_add(1).context(
            "rollblock current block reached u64::MAX; cannot derive protoblock start height",
        )
    }
}

impl Overlay for RollblockOptions {
    fn overlay(self, overrides: Self) -> Self {
        Self {
            user: overrides.user.or(self.user),
            password: overrides.password.or(self.password),
            port: overrides.port.or(self.port),
            shards_count: overrides.shards_count.or(self.shards_count),
            initial_capacity: overrides.initial_capacity.or(self.initial_capacity),
            thread_count: overrides.thread_count.or(self.thread_count),
            compress_journal: overrides.compress_journal.or(self.compress_journal),
            snapshot_interval: overrides.snapshot_interval.or(self.snapshot_interval),
            max_snapshot_interval: overrides
                .max_snapshot_interval
                .or(self.max_snapshot_interval),
            journal_compression_level: overrides
                .journal_compression_level
                .or(self.journal_compression_level),
            journal_chunk_size_bytes: overrides
                .journal_chunk_size_bytes
                .or(self.journal_chunk_size_bytes),
            lmdb_map_size: overrides.lmdb_map_size.or(self.lmdb_map_size),
            min_rollback_window: overrides.min_rollback_window.or(self.min_rollback_window),
            prune_interval: overrides.prune_interval.or(self.prune_interval),
            bootstrap_block_profile: overrides
                .bootstrap_block_profile
                .or(self.bootstrap_block_profile),
            durability_mode: overrides.durability_mode.or(self.durability_mode),
            async_max_pending_blocks: overrides
                .async_max_pending_blocks
                .or(self.async_max_pending_blocks),
            async_relaxed_sync_every: overrides
                .async_relaxed_sync_every
                .or(self.async_relaxed_sync_every),
            synchronous_relaxed_sync_every: overrides
                .synchronous_relaxed_sync_every
                .or(self.synchronous_relaxed_sync_every),
        }
    }
}

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

    #[test]
    fn determine_start_height_is_zero_for_new_store() {
        let tmp = tempdir().expect("tempdir should be created");
        let options = RollblockOptions::default();
        let settings = options.build(tmp.path()).expect("rollblock settings build");

        assert!(matches!(settings.mode, RollblockMode::New));

        let start_height = settings
            .determine_start_height()
            .expect("start height should default to 0 for new stores");
        assert_eq!(start_height, 0);
    }

    #[test]
    fn overlay_prefers_override_and_keeps_base_when_missing() {
        let base = RollblockOptions {
            thread_count: Some(2),
            compress_journal: Some(false),
            async_max_pending_blocks: Some(10),
            user: Some("base-user".to_string()),
            password: Some("base-pass".to_string()),
            port: Some(DEFAULT_ROLLBLOCK_REMOTE_PORT),
            ..Default::default()
        };
        let overrides = RollblockOptions {
            thread_count: Some(8),
            compress_journal: None,
            async_max_pending_blocks: Some(20),
            user: None,
            password: None,
            port: None,
            ..Default::default()
        };

        let merged = base.overlay(overrides);

        assert_eq!(merged.thread_count, Some(8));
        assert_eq!(
            merged.compress_journal,
            Some(false),
            "should retain base when override missing"
        );
        assert_eq!(merged.async_max_pending_blocks, Some(20));
        assert_eq!(merged.user.as_deref(), Some("base-user"));
        assert_eq!(merged.password.as_deref(), Some("base-pass"));
        assert_eq!(merged.port, Some(DEFAULT_ROLLBLOCK_REMOTE_PORT));
    }

    #[test]
    fn build_new_store_applies_remote_defaults() {
        let temp = tempdir().expect("tempdir should be created");
        let settings = RollblockOptions::default()
            .build(temp.path())
            .expect("rollblock settings build");

        let remote = settings
            .store_config
            .remote_server
            .as_ref()
            .expect("remote server should be configured");

        assert_eq!(remote.auth.username, DEFAULT_ROLLBLOCK_REMOTE_USER);
        assert_eq!(remote.auth.password, DEFAULT_ROLLBLOCK_REMOTE_PASSWORD);
        assert_eq!(remote.bind_address.port(), DEFAULT_ROLLBLOCK_REMOTE_PORT);
    }

    #[test]
    fn build_overrides_remote_settings_from_options() {
        let temp = tempdir().expect("tempdir should be created");
        let options = RollblockOptions {
            user: Some("alice".to_string()),
            password: Some("wonderland".to_string()),
            port: Some(9555),
            ..Default::default()
        };
        let settings = options
            .build(temp.path())
            .expect("rollblock settings build");
        let remote = settings
            .store_config
            .remote_server
            .as_ref()
            .expect("remote server should be configured");

        assert_eq!(remote.auth.username, "alice");
        assert_eq!(remote.auth.password, "wonderland");
        assert_eq!(remote.bind_address.port(), 9555);
    }

    #[test]
    fn build_existing_store_requires_capacity_when_overriding_shards() {
        let temp = tempdir().expect("tempdir should be created");
        let utxodb_path = temp.path().join("utxodb");
        std::fs::create_dir_all(&utxodb_path).expect("create utxodb dir");

        let options = RollblockOptions {
            shards_count: Some(8),
            // Missing initial_capacity should trigger the validation error.
            initial_capacity: None,
            ..Default::default()
        };

        let err = options
            .build(temp.path())
            .expect_err("should fail without capacity override");
        assert!(
            err.to_string()
                .contains("rollblock_initial_capacity is required"),
            "error should mention missing capacity, got: {err}"
        );
    }

    #[test]
    fn build_new_store_applies_defaults() {
        let temp = tempdir().expect("tempdir should be created");
        let options = RollblockOptions::default();
        let settings = options.build(temp.path()).expect("build settings");

        assert!(matches!(settings.mode, RollblockMode::New));
        // Just verify it built successfully with a new store
        assert!(settings.store_config.enable_server);
        let remote = settings
            .store_config
            .remote_server
            .as_ref()
            .expect("remote server should be configured");
        assert_eq!(remote.bind_address.port(), DEFAULT_ROLLBLOCK_REMOTE_PORT);
    }

    #[test]
    fn build_applies_durability_mode_async() {
        let temp = tempdir().expect("tempdir should be created");
        let options = RollblockOptions {
            durability_mode: Some(RollblockDurabilityKind::Async),
            ..Default::default()
        };
        let settings = options.build(temp.path()).expect("build settings");

        assert!(matches!(settings.mode, RollblockMode::New));
        // Just verify it builds successfully with this mode
        assert!(settings.store_config.enable_server);
    }

    #[test]
    fn build_applies_durability_mode_synchronous() {
        let temp = tempdir().expect("tempdir should be created");
        let options = RollblockOptions {
            durability_mode: Some(RollblockDurabilityKind::Synchronous),
            ..Default::default()
        };
        let settings = options.build(temp.path()).expect("build settings");

        assert!(matches!(settings.mode, RollblockMode::New));
    }

    #[test]
    fn build_applies_durability_mode_synchronous_relaxed() {
        let temp = tempdir().expect("tempdir should be created");
        let options = RollblockOptions {
            durability_mode: Some(RollblockDurabilityKind::SynchronousRelaxed),
            synchronous_relaxed_sync_every: Some(50),
            ..Default::default()
        };
        let settings = options.build(temp.path()).expect("build settings");

        assert!(matches!(settings.mode, RollblockMode::New));
    }

    #[test]
    fn build_applies_optional_snapshot_interval() {
        use std::time::Duration;

        let temp = tempdir().expect("tempdir should be created");
        let options = RollblockOptions {
            snapshot_interval: Some(Duration::from_secs(300)),
            ..Default::default()
        };
        let settings = options.build(temp.path()).expect("build settings");

        assert!(matches!(settings.mode, RollblockMode::New));
    }

    #[test]
    fn build_applies_optional_max_snapshot_interval() {
        use std::time::Duration;

        let temp = tempdir().expect("tempdir should be created");
        let options = RollblockOptions {
            max_snapshot_interval: Some(Duration::from_secs(600)),
            ..Default::default()
        };
        let settings = options.build(temp.path()).expect("build settings");

        assert!(matches!(settings.mode, RollblockMode::New));
    }

    #[test]
    fn build_applies_optional_journal_compression_level() {
        let temp = tempdir().expect("tempdir should be created");
        let options = RollblockOptions {
            journal_compression_level: Some(3),
            ..Default::default()
        };
        let settings = options.build(temp.path()).expect("build settings");

        assert!(matches!(settings.mode, RollblockMode::New));
    }

    #[test]
    fn build_applies_optional_journal_chunk_size_bytes() {
        let temp = tempdir().expect("tempdir should be created");
        let options = RollblockOptions {
            journal_chunk_size_bytes: Some(1024 * 1024),
            ..Default::default()
        };
        let settings = options.build(temp.path()).expect("build settings");

        assert!(matches!(settings.mode, RollblockMode::New));
    }

    #[test]
    fn build_applies_optional_lmdb_map_size() {
        let temp = tempdir().expect("tempdir should be created");
        let options = RollblockOptions {
            lmdb_map_size: Some(1024 * 1024 * 1024),
            ..Default::default()
        };
        let settings = options.build(temp.path()).expect("build settings");

        assert!(matches!(settings.mode, RollblockMode::New));
    }

    #[test]
    fn build_applies_optional_min_rollback_window() {
        let temp = tempdir().expect("tempdir should be created");
        let options = RollblockOptions {
            min_rollback_window: Some(100),
            ..Default::default()
        };
        let settings = options.build(temp.path()).expect("build settings");

        assert!(matches!(settings.mode, RollblockMode::New));
    }

    #[test]
    fn build_applies_optional_prune_interval() {
        use std::time::Duration;

        let temp = tempdir().expect("tempdir should be created");
        let options = RollblockOptions {
            prune_interval: Some(Duration::from_secs(3600)),
            ..Default::default()
        };
        let settings = options.build(temp.path()).expect("build settings");

        assert!(matches!(settings.mode, RollblockMode::New));
    }

    #[test]
    fn build_applies_optional_bootstrap_block_profile() {
        let temp = tempdir().expect("tempdir should be created");
        let options = RollblockOptions {
            bootstrap_block_profile: Some(1000),
            ..Default::default()
        };
        let settings = options.build(temp.path()).expect("build settings");

        assert!(matches!(settings.mode, RollblockMode::New));
    }

    #[test]
    fn rollblock_mode_default_is_existing() {
        let mode = RollblockMode::default();
        assert!(matches!(mode, RollblockMode::Existing));
    }

    #[test]
    fn overlay_handles_all_optional_fields() {
        use std::time::Duration;

        let base = RollblockOptions {
            shards_count: Some(8),
            initial_capacity: Some(1_000_000),
            thread_count: Some(4),
            compress_journal: Some(true),
            snapshot_interval: Some(Duration::from_secs(60)),
            max_snapshot_interval: Some(Duration::from_secs(120)),
            journal_compression_level: Some(3),
            journal_chunk_size_bytes: Some(1024),
            lmdb_map_size: Some(1024 * 1024),
            min_rollback_window: Some(50),
            prune_interval: Some(Duration::from_secs(300)),
            bootstrap_block_profile: Some(500),
            durability_mode: Some(RollblockDurabilityKind::Async),
            async_max_pending_blocks: Some(100),
            async_relaxed_sync_every: Some(10),
            synchronous_relaxed_sync_every: Some(5),
            user: Some("base-user".to_string()),
            password: Some("base-pass".to_string()),
            port: Some(DEFAULT_ROLLBLOCK_REMOTE_PORT),
        };

        let overrides = RollblockOptions {
            shards_count: Some(16),
            // Leave others as None to test preservation
            ..Default::default()
        };

        let merged = base.overlay(overrides);

        assert_eq!(merged.shards_count, Some(16));
        assert_eq!(merged.initial_capacity, Some(1_000_000));
        assert_eq!(merged.thread_count, Some(4));
        assert_eq!(merged.compress_journal, Some(true));
        assert_eq!(merged.snapshot_interval, Some(Duration::from_secs(60)));
        assert_eq!(merged.max_snapshot_interval, Some(Duration::from_secs(120)));
        assert_eq!(merged.journal_compression_level, Some(3));
        assert_eq!(merged.journal_chunk_size_bytes, Some(1024));
        assert_eq!(merged.lmdb_map_size, Some(1024 * 1024));
        assert_eq!(merged.min_rollback_window, Some(50));
        assert_eq!(merged.prune_interval, Some(Duration::from_secs(300)));
        assert_eq!(merged.bootstrap_block_profile, Some(500));
        assert!(matches!(
            merged.durability_mode,
            Some(RollblockDurabilityKind::Async)
        ));
        assert_eq!(merged.async_max_pending_blocks, Some(100));
        assert_eq!(merged.async_relaxed_sync_every, Some(10));
        assert_eq!(merged.synchronous_relaxed_sync_every, Some(5));
        assert_eq!(merged.user.as_deref(), Some("base-user"));
        assert_eq!(merged.password.as_deref(), Some("base-pass"));
        assert_eq!(merged.port, Some(DEFAULT_ROLLBLOCK_REMOTE_PORT));
    }
}