blvm-node 0.1.91

Bitcoin Commons BLVM: Minimal Bitcoin node implementation using blvm-protocol and blvm-consensus
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
//! Storage configuration: database backend, pruning, indexing, cache.

use serde::{Deserialize, Serialize};

use super::{default_false, default_true};

/// Pruning mode configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "lowercase")]
pub enum PruningMode {
    /// No pruning (keep all blocks)
    Disabled,

    /// Normal pruning (keep recent blocks for verification)
    Normal {
        #[serde(default = "default_zero")]
        keep_from_height: u64,
        #[serde(default = "default_min_recent_blocks")]
        min_recent_blocks: u64,
    },

    /// Aggressive pruning with UTXO commitments
    Aggressive {
        #[serde(default = "default_zero")]
        keep_from_height: u64,
        #[serde(default = "default_true")]
        keep_commitments: bool,
        #[serde(default = "default_false")]
        keep_filtered_blocks: bool,
        #[serde(default = "default_min_blocks")]
        min_blocks: u64,
    },

    /// Custom pruning configuration
    Custom {
        #[serde(default = "default_true")]
        keep_headers: bool,
        #[serde(default = "default_zero")]
        keep_bodies_from_height: u64,
        #[serde(default = "default_false")]
        keep_commitments: bool,
        #[serde(default = "default_false")]
        keep_filters: bool,
        #[serde(default = "default_false")]
        keep_filtered_blocks: bool,
        #[serde(default = "default_false")]
        keep_witnesses: bool,
        #[serde(default = "default_false")]
        keep_tx_index: bool,
    },
}

fn default_zero() -> u64 {
    0
}

fn default_min_recent_blocks() -> u64 {
    288
}

fn default_min_blocks() -> u64 {
    288
}

/// Pruning configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PruningConfig {
    #[serde(default = "default_pruning_mode")]
    pub mode: PruningMode,

    #[serde(default = "default_false")]
    pub auto_prune: bool,

    #[serde(default = "default_auto_prune_interval")]
    pub auto_prune_interval: u64,

    #[serde(default = "default_min_blocks_to_keep")]
    pub min_blocks_to_keep: u64,

    #[serde(default = "default_false")]
    pub prune_on_startup: bool,

    #[serde(default = "default_false")]
    pub incremental_prune_during_ibd: bool,

    #[serde(default = "default_prune_window_size")]
    pub prune_window_size: u64,

    #[serde(default = "default_min_blocks_for_incremental_prune")]
    pub min_blocks_for_incremental_prune: u64,
}

fn default_pruning_mode() -> PruningMode {
    PruningMode::Aggressive {
        keep_from_height: 0,
        keep_commitments: true,
        keep_filtered_blocks: false,
        min_blocks: default_min_blocks(),
    }
}

fn default_auto_prune_interval() -> u64 {
    144
}

fn default_min_blocks_to_keep() -> u64 {
    288
}

fn default_prune_window_size() -> u64 {
    144
}

fn default_min_blocks_for_incremental_prune() -> u64 {
    288
}

impl Default for PruningConfig {
    fn default() -> Self {
        Self {
            mode: PruningMode::Aggressive {
                keep_from_height: 0,
                keep_commitments: true,
                keep_filtered_blocks: false,
                min_blocks: default_min_blocks(),
            },
            auto_prune: true,
            auto_prune_interval: default_auto_prune_interval(),
            min_blocks_to_keep: default_min_blocks_to_keep(),
            prune_on_startup: false,
            incremental_prune_during_ibd: true,
            prune_window_size: 144,
            min_blocks_for_incremental_prune: 288,
        }
    }
}

impl PruningConfig {
    pub fn validate(&self) -> anyhow::Result<()> {
        if let PruningMode::Aggressive { .. } = self.mode {
            #[cfg(not(feature = "utxo-commitments"))]
            {
                return Err(anyhow::anyhow!(
                    "Aggressive pruning mode requires the 'utxo-commitments' feature to be enabled. \
                    Please enable it in Cargo.toml or use Normal pruning mode instead."
                ));
            }
        }

        if self.min_blocks_to_keep == 0 {
            return Err(anyhow::anyhow!(
                "min_blocks_to_keep must be greater than 0 for safety"
            ));
        }

        if self.auto_prune && self.auto_prune_interval == 0 {
            return Err(anyhow::anyhow!(
                "auto_prune_interval must be greater than 0 when auto_prune is enabled"
            ));
        }

        match &self.mode {
            PruningMode::Normal {
                min_recent_blocks, ..
            } => {
                if *min_recent_blocks == 0 {
                    return Err(anyhow::anyhow!(
                        "min_recent_blocks must be greater than 0 in Normal pruning mode"
                    ));
                }
            }
            PruningMode::Aggressive { min_blocks, .. } => {
                if *min_blocks == 0 {
                    return Err(anyhow::anyhow!(
                        "min_blocks must be greater than 0 in Aggressive pruning mode"
                    ));
                }
            }
            PruningMode::Custom { keep_headers, .. } => {
                if !keep_headers {
                    return Err(anyhow::anyhow!(
                        "keep_headers must be true in Custom pruning mode (required for PoW verification)"
                    ));
                }
            }
            PruningMode::Disabled => {}
        }

        Ok(())
    }
}

/// Database backend configuration
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DatabaseBackendConfig {
    Sled,
    Redb,
    Rocksdb,
    Heed3,
    Auto,
}

fn default_database_backend() -> DatabaseBackendConfig {
    DatabaseBackendConfig::Auto
}

fn default_storage_path() -> String {
    "data".to_string()
}

fn default_dbcache_mb() -> usize {
    450
}

/// RocksDB tuning. ENV overrides the file; the file overrides the RAM-tier default.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RocksDBConfig {
    /// Parallelism (default: nproc or 4)
    #[serde(default)]
    pub parallelism: Option<i32>,

    /// Max background compactions (default: 2 on <=16GB RAM, 4 on 32GB+)
    #[serde(default)]
    pub max_background_compactions: Option<i32>,

    /// Max background flushes (default: 2 on <=16GB RAM, 4 on 32GB+)
    #[serde(default)]
    pub max_background_flushes: Option<i32>,

    /// Level-0 compaction trigger (default: 8)
    #[serde(default = "default_rocksdb_level0_trigger")]
    pub level0_compaction_trigger: i32,

    /// Write buffer size in MB (default: 128–256 by RAM)
    #[serde(default)]
    pub write_buffer_mb: Option<usize>,
}

fn default_rocksdb_level0_trigger() -> i32 {
    8
}

/// heed3 / LMDB tuning. ENV overrides the file; the file overrides auto-tune.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct Heed3Config {
    /// LMDB map size in megabytes. When unset, auto-tuned from free disk (70%, clamped
    /// 48 GiB–4 TiB) plus `data.mdb + 128 GiB` headroom on resume. Override via this
    /// field or `BLVM_HEED3_MAP_SIZE_MB` only when operator policy requires a fixed cap.
    #[serde(default)]
    pub map_size_mb: Option<usize>,

    /// Maximum concurrent read transactions (default: 512).
    #[serde(default)]
    pub max_readers: Option<u32>,

    /// Maximum named sub-databases / trees (default: KNOWN_TREE_NAMES + 8).
    #[serde(default)]
    pub max_dbs: Option<u32>,
}

impl Default for RocksDBConfig {
    fn default() -> Self {
        Self {
            parallelism: None,
            max_background_compactions: None,
            max_background_flushes: None,
            level0_compaction_trigger: 8,
            write_buffer_mb: None,
        }
    }
}

/// Storage configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StorageConfig {
    #[serde(default = "default_database_backend")]
    pub database_backend: DatabaseBackendConfig,

    #[serde(default = "default_storage_path")]
    pub data_dir: String,

    #[serde(default = "default_dbcache_mb")]
    pub dbcache_mb: usize,

    /// RocksDB tuning (ENV > file > RAM-tier default)
    #[serde(default)]
    pub rocksdb: Option<RocksDBConfig>,

    /// heed3 / LMDB options when `database_backend = "heed3"`.
    #[serde(default)]
    pub heed3: Option<Heed3Config>,

    pub pruning: Option<PruningConfig>,
    pub cache: Option<StorageCacheConfig>,
    #[serde(default)]
    pub indexing: Option<IndexingConfig>,
    #[cfg(feature = "compression")]
    pub compression: Option<CompressionConfig>,

    /// Auto-migrate Bitcoin Core datadir into BLVM native store on startup (default: true).
    /// Disabled by `--no-auto-migrate` or `BLVM_NO_AUTO_MIGRATE_CORE=1`.
    #[serde(default = "default_auto_migrate_core")]
    pub auto_migrate_core: bool,

    /// Override BLVM store path when migrating from Core (default: `<datadir>/blvm/`).
    #[serde(default)]
    pub core_migrate_destination: Option<String>,

    /// Keep Core `blocks/` in place during migrate; BLVM reads blk*.dat via fallback reader (saves disk).
    /// Default **true**: migrating from a full Core datadir does not copy ~700 GB of block files.
    /// Set **`false`** or **`BLVM_REUSE_CORE_BLOCK_FILES=0`** to copy block bodies into the BLVM store.
    #[serde(default = "default_reuse_core_block_files")]
    pub reuse_core_block_files: bool,
}

fn default_auto_migrate_core() -> bool {
    true
}

fn default_reuse_core_block_files() -> bool {
    true
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StorageCacheConfig {
    #[serde(default = "default_block_cache_mb")]
    pub block_cache_mb: usize,
    #[serde(default = "default_utxo_cache_mb")]
    pub utxo_cache_mb: usize,
    #[serde(default = "default_header_cache_mb")]
    pub header_cache_mb: usize,
}

fn default_block_cache_mb() -> usize {
    100
}

fn default_utxo_cache_mb() -> usize {
    50
}

fn default_header_cache_mb() -> usize {
    10
}

impl Default for StorageCacheConfig {
    fn default() -> Self {
        Self {
            block_cache_mb: 100,
            utxo_cache_mb: 50,
            header_cache_mb: 10,
        }
    }
}

impl Default for StorageConfig {
    fn default() -> Self {
        Self {
            database_backend: DatabaseBackendConfig::Auto,
            data_dir: "data".to_string(),
            dbcache_mb: 450,
            rocksdb: None,
            heed3: None,
            pruning: None,
            cache: None,
            indexing: None,
            #[cfg(feature = "compression")]
            compression: None,
            auto_migrate_core: true,
            core_migrate_destination: None,
            reuse_core_block_files: true,
        }
    }
}

impl StorageConfig {
    /// Whether startup should auto-migrate a Core datadir (ENV overrides config).
    pub fn auto_migrate_core_effective(&self) -> bool {
        if crate::utils::env_bool("BLVM_NO_AUTO_MIGRATE_CORE") {
            return false;
        }
        if std::env::var("BLVM_AUTO_MIGRATE_CORE").is_ok() {
            return crate::utils::env_bool("BLVM_AUTO_MIGRATE_CORE");
        }
        self.auto_migrate_core
    }

    /// Resolved BLVM store directory when migrating from Core inside `data_dir`.
    pub fn resolve_core_migrate_destination(
        &self,
        data_dir: &std::path::Path,
    ) -> std::path::PathBuf {
        let dest = std::env::var("BLVM_CORE_MIGRATE_DESTINATION")
            .ok()
            .map(std::path::PathBuf::from)
            .or_else(|| {
                self.core_migrate_destination
                    .as_ref()
                    .map(std::path::PathBuf::from)
            })
            .unwrap_or_else(|| data_dir.join("blvm"));
        if crate::config::path_is_strictly_within(data_dir, &dest) {
            return dest;
        }
        let fallback = data_dir.join("blvm");
        tracing::warn!(
            "core migrate destination {} is outside data_dir {} or equal to it; using {}",
            dest.display(),
            data_dir.display(),
            fallback.display()
        );
        fallback
    }

    /// Whether migrate should skip copying block bodies and read Core `blocks/` in place.
    pub fn reuse_core_block_files_effective(&self) -> bool {
        if std::env::var("BLVM_REUSE_CORE_BLOCK_FILES").is_ok() {
            return crate::utils::env_bool("BLVM_REUSE_CORE_BLOCK_FILES");
        }
        self.reuse_core_block_files
    }
}

#[cfg(feature = "compression")]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompressionConfig {
    #[serde(default = "default_true")]
    pub block_compression_enabled: bool,
    #[serde(default = "default_block_compression_level")]
    pub block_compression_level: u32,
    #[serde(default = "default_true")]
    pub utxo_compression_enabled: bool,
    #[serde(default = "default_utxo_compression_level")]
    pub utxo_compression_level: u32,
    #[serde(default = "default_true")]
    pub witness_compression_enabled: bool,
    #[serde(default = "default_witness_compression_level")]
    pub witness_compression_level: u32,
}

#[cfg(feature = "compression")]
fn default_block_compression_level() -> u32 {
    3
}

#[cfg(feature = "compression")]
fn default_utxo_compression_level() -> u32 {
    1
}

#[cfg(feature = "compression")]
fn default_witness_compression_level() -> u32 {
    2
}

#[cfg(feature = "compression")]
impl Default for CompressionConfig {
    fn default() -> Self {
        Self {
            block_compression_enabled: true,
            block_compression_level: 3,
            utxo_compression_enabled: true,
            utxo_compression_level: 1,
            witness_compression_enabled: true,
            witness_compression_level: 2,
        }
    }
}

/// Indexing strategy
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum IndexingStrategy {
    Eager,
    Lazy,
}

fn default_indexing_strategy() -> IndexingStrategy {
    IndexingStrategy::Eager
}

fn default_max_indexed_addresses() -> usize {
    0
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexingConfig {
    #[serde(default)]
    pub enable_address_index: bool,
    #[serde(default)]
    pub enable_value_index: bool,
    #[serde(default = "default_indexing_strategy")]
    pub strategy: IndexingStrategy,
    #[serde(default = "default_max_indexed_addresses")]
    pub max_indexed_addresses: usize,
    #[serde(default)]
    pub enable_compression: bool,
    #[serde(default)]
    pub background_indexing: bool,
}

impl Default for IndexingConfig {
    fn default() -> Self {
        Self {
            enable_address_index: false,
            enable_value_index: false,
            strategy: IndexingStrategy::Eager,
            max_indexed_addresses: 0,
            enable_compression: false,
            background_indexing: false,
        }
    }
}

#[cfg(test)]
mod prune_keep_floor {
    use super::{PruningConfig, PruningMode};

    #[test]
    fn keep_floors_are_288_and_prune_windows_stay_144() {
        let config = PruningConfig::default();
        let PruningMode::Aggressive { min_blocks, .. } = config.mode else {
            panic!("default pruning mode is aggressive");
        };
        assert_eq!(min_blocks, 288);
        assert_eq!(config.min_blocks_to_keep, 288);
        assert_eq!(config.auto_prune_interval, 144);
        assert_eq!(config.prune_window_size, 144);
        assert_eq!(config.min_blocks_for_incremental_prune, 288);

        let omitted: PruningConfig = toml::from_str("auto_prune = false").unwrap();
        let PruningMode::Aggressive {
            min_blocks: omitted_blocks,
            ..
        } = omitted.mode
        else {
            panic!("omitted pruning mode is aggressive");
        };
        assert_eq!(omitted_blocks, 288);
        assert_eq!(omitted.min_blocks_to_keep, 288);
        assert_eq!(omitted.auto_prune_interval, 144);
        assert_eq!(omitted.prune_window_size, 144);

        let normal: PruningMode = toml::from_str("type = \"normal\"").unwrap();
        let PruningMode::Normal {
            min_recent_blocks, ..
        } = normal
        else {
            panic!("normal mode");
        };
        assert_eq!(min_recent_blocks, 288);
    }
}