blvm-node 0.1.54

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
//! Opinionated IBD implementation policy.
//!
//! Three layers — do not add a fourth:
//!
//! 1. **This file** — tip-hole / sole-peer / gap-persist DNA. Not TOML.
//!    KEEP constant is the default; rematch `BLVM_IBD_*` still overrides
//!    (peel contract). Values are the r29od / peel-go KEEP table (S11 240/205.6).
//! 2. **`[ibd]` TOML** — operator surface: peers, mode, chunk_size, engine opt-out,
//!    `max_blocks_in_transit_per_peer`, timeouts, dump_dir.
//! 3. **Emergency / debug env** — `BLVM_IBD_ENGINE=0`, `BLVM_IBD_DUMP_DIR`,
//!    `BLVM_IBD_JEMALLOC_DUMP`, `BLVM_SERVE_ONLY`, `BLVM_IBD_PEERS` pin.
//!
//! Production: KEEP constant is the default; rematch `BLVM_IBD_*` still overrides
//! (peel contract). `#[cfg(test)]` uses historical unset defaults so the assigner
//! suite does not flip.

#[cfg(test)]
use super::latch_env;

/// One GetData in flight to the tip owner. Dual-slot floods Mode T.
pub const TOP_PEER_IN_FLIGHT: usize = 1;
/// Second after-tip stripe while tip is covered. KEEP off.
pub const TIP_FRONTIER_DUAL: bool = false;
/// Release zombie EMPTY_TIP cover. KEEP off (unset in go.sh).
pub const SOLE_EMPTY_RELEASE: bool = false;
/// Tip-glue / sticky flight clamp. KEEP off.
pub const SOLE_TIP_PRIORITY: bool = false;
/// Second download worker per WAN peer so sticky dual-pipe can arm. KEEP on.
pub const STICKY_DUAL_WORKER: bool = true;
/// Grow tip-hole depth as bodies arrive.
pub const TIP_HOLE_GROW: bool = true;
/// Pipe headroom (slots). KEEP exports 128; historical unset was 32.
pub const TIP_HOLE_PIPE: usize = 128;
/// Cold grow cap. KEEP 32.
pub const TIP_HOLE_GROW_CAP: usize = 32;
/// Fast grow cap when getdata→body EWMA is healthy. KEEP 64; historical unset 48.
pub const TIP_HOLE_GROW_FAST_CAP: usize = 64;
/// Initial tip-hole depth under grow-on-delivery.
pub const TIP_HOLE_GROW_START: usize = 8;
/// Slots added per tip-band body.
pub const TIP_HOLE_GROW_STEP: usize = 8;
/// Carry tip-hole depth across chunks for the same peer. KEEP on; historical unset off.
pub const TIP_HOLE_STICKY: bool = true;
/// Elevate past cold cap only while getdata is fast.
pub const TIP_HOLE_GD_FAST: bool = true;
pub const TIP_HOLE_GD_FAST_MS: u64 = 150;
pub const TIP_HOLE_GD_FAST_N: u64 = 16;
pub const TIP_HOLE_GD_FAST_MIN_H: u64 = 0;
/// Clamp deepen while getdata is slow.
pub const TIP_HOLE_GD_SLOW: bool = true;
pub const TIP_HOLE_GD_SLOW_MS: u64 = 800;
pub const TIP_HOLE_GD_SLOW_N: u64 = 16;
pub const TIP_HOLE_GD_SLOW_RATCHET: bool = false;
/// Sole-peer GD_SLOW floor. KEEP unsets the env → 16.
pub const TIP_HOLE_SOLE_FLOOR: usize = 16;
pub const TIP_HOLE_SOLE_NO_FAST_CLEAR_N: u32 = 16;
pub const TIP_HOLE_SOLE_NO_FAST_MIN_HOLD_MS: u64 = 120_000;
/// Do not arm no-FAST until this height. KEEP 405000; historical unset 0.
pub const TIP_HOLE_SOLE_NO_FAST_ARM_MIN_H: u64 = 405_000;
/// 0 = always apply sole floor. KEEP unsets.
pub const TIP_HOLE_SOLE_FLOOR_MAX_H: u64 = 0;
/// Warm-peer deepen to pipe cap. KEEP off.
pub const TIP_HOLE_WARM: bool = false;
/// Hashes per getdata. KEEP unsets → 64.
pub const GETDATA_BATCH: usize = 64;
/// Gap-persist experiment family. KEEP unset / off.
pub const GAP_PERSIST_OFFLOAD: bool = false;
pub const GAP_PERSIST_TIP_SYNC: bool = false;
pub const GAP_PERSIST_DEFER_FAR: bool = false;
/// covering=0 idle requeue. KEEP unset → 0 (off).
pub const COVERING0_IDLE_REQUEUE_MS: u64 = 0;

fn test_bool(env: &str, default: bool, explicit_on: bool) -> bool {
    let Ok(raw) = std::env::var(env) else {
        return default;
    };
    let t = raw.trim();
    let off = matches!(t, "0" | "false" | "FALSE" | "off" | "OFF" | "no" | "NO");
    let on = matches!(t, "1" | "true" | "TRUE" | "on" | "ON" | "yes" | "YES");
    if explicit_on {
        if on {
            return true;
        }
        if off {
            return false;
        }
        return default;
    }
    // Default-on: only explicit off disables.
    if off { false } else { default }
}

fn test_usize(env: &str, default: usize, lo: usize, hi: usize) -> usize {
    std::env::var(env)
        .ok()
        .and_then(|s| s.parse().ok())
        .unwrap_or(default)
        .clamp(lo, hi)
}

fn test_u64(env: &str, default: u64, lo: u64, hi: u64) -> u64 {
    std::env::var(env)
        .ok()
        .and_then(|s| s.parse().ok())
        .unwrap_or(default)
        .clamp(lo, hi)
}

fn test_u32(env: &str, default: u32, lo: u32, hi: u32) -> u32 {
    std::env::var(env)
        .ok()
        .and_then(|s| s.parse().ok())
        .unwrap_or(default)
        .clamp(lo, hi)
}

macro_rules! keep_bool {
    ($keep:expr, $test_default:expr, $env:expr, $explicit_on:expr) => {{
        #[cfg(not(test))]
        {
            let _ = $test_default;
            test_bool($env, $keep, $explicit_on)
        }
        #[cfg(test)]
        {
            latch_env!(bool, { test_bool($env, $test_default, $explicit_on) })
        }
    }};
}

macro_rules! keep_usize {
    ($keep:expr, $test_default:expr, $env:expr, $lo:expr, $hi:expr) => {{
        #[cfg(not(test))]
        {
            let _ = $test_default;
            test_usize($env, $keep, $lo, $hi)
        }
        #[cfg(test)]
        {
            latch_env!(usize, { test_usize($env, $test_default, $lo, $hi) })
        }
    }};
}

macro_rules! keep_u64 {
    ($keep:expr, $test_default:expr, $env:expr, $lo:expr, $hi:expr) => {{
        #[cfg(not(test))]
        {
            let _ = $test_default;
            test_u64($env, $keep, $lo, $hi)
        }
        #[cfg(test)]
        {
            latch_env!(u64, { test_u64($env, $test_default, $lo, $hi) })
        }
    }};
}

pub(crate) fn sticky_dual_worker() -> bool {
    keep_bool!(
        STICKY_DUAL_WORKER,
        false,
        "BLVM_IBD_STICKY_DUAL_WORKER",
        true
    )
}

pub(crate) fn sole_tip_priority() -> bool {
    keep_bool!(SOLE_TIP_PRIORITY, false, "BLVM_IBD_SOLE_TIP_PRIORITY", true)
}

pub(crate) fn top_peer_in_flight() -> usize {
    keep_usize!(TOP_PEER_IN_FLIGHT, 2, "BLVM_IBD_TOP_PEER_IN_FLIGHT", 1, 4)
}

pub(crate) fn tip_hole_sticky() -> bool {
    keep_bool!(TIP_HOLE_STICKY, false, "BLVM_IBD_TIP_HOLE_STICKY", true)
}

pub(crate) fn getdata_batch() -> usize {
    keep_usize!(GETDATA_BATCH, 64, "BLVM_IBD_GETDATA_BATCH", 1, 64)
}

pub(crate) fn tip_hole_grow() -> bool {
    keep_bool!(TIP_HOLE_GROW, true, "BLVM_IBD_TIP_HOLE_GROW", false)
}

pub(crate) fn tip_hole_pipe() -> usize {
    keep_usize!(TIP_HOLE_PIPE, 32, "BLVM_IBD_TIP_HOLE_PIPE", 2, 128)
}

pub(crate) fn tip_hole_grow_cap_raw() -> usize {
    keep_usize!(TIP_HOLE_GROW_CAP, 32, "BLVM_IBD_TIP_HOLE_GROW_CAP", 2, 128)
}

pub(crate) fn tip_hole_gd_fast() -> bool {
    keep_bool!(TIP_HOLE_GD_FAST, true, "BLVM_IBD_TIP_HOLE_GD_FAST", false)
}

pub(crate) fn tip_hole_gd_fast_ms() -> u64 {
    keep_u64!(
        TIP_HOLE_GD_FAST_MS,
        150,
        "BLVM_IBD_TIP_HOLE_GD_FAST_MS",
        50,
        800
    )
}

pub(crate) fn tip_hole_gd_fast_n() -> u64 {
    keep_u64!(TIP_HOLE_GD_FAST_N, 16, "BLVM_IBD_TIP_HOLE_GD_FAST_N", 4, 64)
}

pub(crate) fn tip_hole_grow_fast_cap_raw() -> usize {
    keep_usize!(
        TIP_HOLE_GROW_FAST_CAP,
        48,
        "BLVM_IBD_TIP_HOLE_GROW_FAST_CAP",
        2,
        96
    )
}

pub(crate) fn tip_hole_gd_fast_min_h() -> u64 {
    keep_u64!(
        TIP_HOLE_GD_FAST_MIN_H,
        0,
        "BLVM_IBD_TIP_HOLE_GD_FAST_MIN_H",
        0,
        u64::MAX
    )
}

pub(crate) fn tip_hole_gd_slow() -> bool {
    keep_bool!(TIP_HOLE_GD_SLOW, true, "BLVM_IBD_TIP_HOLE_GD_SLOW", false)
}

pub(crate) fn tip_hole_gd_slow_ms() -> u64 {
    keep_u64!(
        TIP_HOLE_GD_SLOW_MS,
        800,
        "BLVM_IBD_TIP_HOLE_GD_SLOW_MS",
        200,
        5_000
    )
}

pub(crate) fn tip_hole_gd_slow_n() -> u64 {
    keep_u64!(TIP_HOLE_GD_SLOW_N, 16, "BLVM_IBD_TIP_HOLE_GD_SLOW_N", 4, 64)
}

pub(crate) fn tip_hole_slow_fill_cap_raw() -> Option<usize> {
    #[cfg(not(test))]
    {
        None
    }
    #[cfg(test)]
    {
        latch_env!(Option<usize>, {
            std::env::var("BLVM_IBD_TIP_HOLE_SLOW_FILL_CAP")
                .ok()
                .and_then(|s| s.parse().ok())
        })
    }
}

pub(crate) fn tip_hole_gd_slow_ratchet() -> bool {
    keep_bool!(
        TIP_HOLE_GD_SLOW_RATCHET,
        false,
        "BLVM_IBD_TIP_HOLE_GD_SLOW_RATCHET",
        true
    )
}

pub(crate) fn tip_hole_sole_floor() -> usize {
    keep_usize!(
        TIP_HOLE_SOLE_FLOOR,
        16,
        "BLVM_IBD_TIP_HOLE_SOLE_FLOOR",
        2,
        64
    )
}

pub(crate) fn tip_hole_sole_floor_recover_ms_raw() -> Option<u64> {
    #[cfg(not(test))]
    {
        None
    }
    #[cfg(test)]
    {
        latch_env!(Option<u64>, {
            std::env::var("BLVM_IBD_TIP_HOLE_SOLE_FLOOR_RECOVER_MS")
                .ok()
                .and_then(|s| s.parse().ok())
        })
    }
}

pub(crate) fn tip_hole_sole_no_fast_clear_n() -> u32 {
    #[cfg(not(test))]
    {
        TIP_HOLE_SOLE_NO_FAST_CLEAR_N
    }
    #[cfg(test)]
    {
        latch_env!(u32, {
            test_u32("BLVM_IBD_TIP_HOLE_SOLE_NO_FAST_CLEAR_N", 16, 4, 64)
        })
    }
}

pub(crate) fn tip_hole_sole_no_fast_min_hold_ms() -> u64 {
    keep_u64!(
        TIP_HOLE_SOLE_NO_FAST_MIN_HOLD_MS,
        120_000,
        "BLVM_IBD_TIP_HOLE_SOLE_NO_FAST_MIN_HOLD_MS",
        1_000,
        600_000
    )
}

pub(crate) fn tip_hole_sole_no_fast_arm_min_h() -> u64 {
    keep_u64!(
        TIP_HOLE_SOLE_NO_FAST_ARM_MIN_H,
        0,
        "BLVM_IBD_TIP_HOLE_SOLE_NO_FAST_ARM_MIN_H",
        0,
        u64::MAX
    )
}

pub(crate) fn tip_hole_sole_floor_max_h() -> u64 {
    keep_u64!(
        TIP_HOLE_SOLE_FLOOR_MAX_H,
        0,
        "BLVM_IBD_TIP_HOLE_SOLE_FLOOR_MAX_H",
        0,
        u64::MAX
    )
}

pub(crate) fn tip_hole_warm() -> bool {
    keep_bool!(TIP_HOLE_WARM, false, "BLVM_IBD_TIP_HOLE_WARM", true)
}

pub(crate) fn tip_hole_warm_cap_raw() -> Option<usize> {
    #[cfg(not(test))]
    {
        None
    }
    #[cfg(test)]
    {
        latch_env!(Option<usize>, {
            std::env::var("BLVM_IBD_TIP_HOLE_WARM_CAP")
                .ok()
                .and_then(|s| s.parse().ok())
        })
    }
}

pub(crate) fn tip_hole_grow_start() -> usize {
    keep_usize!(
        TIP_HOLE_GROW_START,
        8,
        "BLVM_IBD_TIP_HOLE_GROW_START",
        2,
        32
    )
}

pub(crate) fn tip_hole_grow_step() -> usize {
    keep_usize!(TIP_HOLE_GROW_STEP, 8, "BLVM_IBD_TIP_HOLE_GROW_STEP", 2, 32)
}

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

    #[test]
    fn keep_table_is_r29od_peel() {
        assert_eq!(TOP_PEER_IN_FLIGHT, 1);
        assert!(!TIP_FRONTIER_DUAL);
        assert!(!SOLE_EMPTY_RELEASE);
        assert!(!SOLE_TIP_PRIORITY);
        assert!(STICKY_DUAL_WORKER);
        assert_eq!(TIP_HOLE_PIPE, 128);
        assert_eq!(TIP_HOLE_GROW_CAP, 32);
        assert_eq!(TIP_HOLE_GROW_FAST_CAP, 64);
        assert_eq!(TIP_HOLE_GROW_START, 8);
        assert!(TIP_HOLE_STICKY);
        assert!(TIP_HOLE_GD_FAST);
        assert_eq!(TIP_HOLE_GD_FAST_MS, 150);
        assert_eq!(TIP_HOLE_GD_FAST_N, 16);
        assert_eq!(TIP_HOLE_SOLE_NO_FAST_MIN_HOLD_MS, 120_000);
        assert_eq!(TIP_HOLE_SOLE_NO_FAST_ARM_MIN_H, 405_000);
        assert!(!GAP_PERSIST_OFFLOAD);
        assert!(!GAP_PERSIST_TIP_SYNC);
        assert!(!GAP_PERSIST_DEFER_FAR);
        assert_eq!(COVERING0_IDLE_REQUEUE_MS, 0);
        assert_eq!(GETDATA_BATCH, 64);
    }
}