blvm-node 0.1.63

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
//! Incremental MuHash3072 over the `ibd_utxos` tree during parallel IBD.

use crate::storage::Storage;
use crate::storage::chainstate::ChainState;
use crate::storage::disk_utxo::key_to_outpoint;
use anyhow::{Context, Result};
use blvm_muhash::{MuHash3072, serialize_coin_for_muhash};
use blvm_protocol::types::UTXO;

pub(crate) fn load_ibd_muhash_from_chain(chain: &ChainState) -> Result<MuHash3072> {
    Ok(match chain.get_ibd_utxo_muhash_running()? {
        Some(bytes) => MuHash3072::deserialize_running_state(&bytes),
        None => MuHash3072::new(),
    })
}

/// Reset rolling MuHash bytes to the last completed engine checkpoint snapshot before gap replay.
pub(crate) fn reset_engine_resume_muhash_baseline(
    chain: &ChainState,
    export_height: u64,
) -> Result<()> {
    if export_height == 0 {
        return Ok(());
    }
    if let Some(bytes) = chain.get_engine_export_muhash()? {
        chain
            .persist_ibd_utxo_muhash_running_only(&bytes)
            .with_context(|| {
                format!("reset MuHash baseline to export snapshot at height {export_height}")
            })?;
        tracing::info!(
            "IBD engine: MuHash baseline reset to export snapshot at height {export_height}"
        );
        return Ok(());
    }
    if let Some(tip) = chain.get_engine_validation_tip()? {
        if tip > export_height {
            tracing::warn!(
                "IBD engine: export MuHash snapshot missing at height {export_height}; \
                 validation_tip={tip} is ahead — run backfill or wait for next checkpoint export"
            );
        }
    }
    Ok(())
}

/// One-shot backfill: scan the resume snapshot and persist `ibd_engine_export_muhash`.
///
/// The active ckpt wins when it has rows. Phase 3 full watermark export clears both
/// ckpt trees and leaves the snapshot in the stored canonical tree (`ibd_utxos`).
/// Safe to run offline against the data directory (node stopped). Idempotent when
/// the export snapshot already exists.
pub fn backfill_engine_export_muhash_if_missing(storage: &Storage) -> Result<bool> {
    use crate::storage::ibd_engine::ckpt_tree_for_slot;
    use crate::storage::utxo_value_codec::decode_utxo_with_codec;

    let chain = storage.chain();
    let export_height = match chain.get_engine_export_height()? {
        Some(h) if h > 0 => h,
        _ => return Ok(false),
    };
    if chain.get_engine_export_muhash()?.is_some() {
        return Ok(false);
    }

    let slot_name = ckpt_tree_for_slot(chain.get_engine_ckpt_slot()?);
    let Some((tree_name, tree)) = storage.open_engine_resume_snapshot()? else {
        anyhow::bail!(
            "cannot backfill export MuHash: {slot_name} empty at export_height={export_height}"
        );
    };

    let codec = storage.utxo_value_codec();
    let t0 = std::time::Instant::now();
    let mut mh = MuHash3072::new();
    let mut count = 0usize;
    for kv in tree.iter() {
        let (key_bytes, val_bytes) = kv?;
        if key_bytes.len() != 40 {
            continue;
        }
        let mut op_key = [0u8; 40];
        op_key.copy_from_slice(&key_bytes);
        let op = key_to_outpoint(&op_key);
        let utxo: UTXO = decode_utxo_with_codec(codec, &val_bytes)?;
        mh.insert_mut(&utxo_muhash_preimage(&op, &utxo));
        count += 1;
        if count % 5_000_000 == 0 {
            tracing::info!(
                "IBD engine MuHash backfill: scanned {} UTXOs from {} ({:.1}s)",
                count,
                tree_name,
                t0.elapsed().as_secs_f64()
            );
        }
    }

    let bytes = mh.serialize_running_state();
    chain.persist_engine_export_muhash_snapshot(&bytes)?;
    chain.persist_ibd_utxo_muhash_running_only(&bytes)?;
    storage.flush()?;

    tracing::info!(
        "IBD engine: backfilled export MuHash from {} at h={} ({} UTXOs, {:.1}s)",
        tree_name,
        export_height,
        count,
        t0.elapsed().as_secs_f64()
    );
    Ok(true)
}

#[inline]
fn utxo_muhash_preimage(op: &blvm_protocol::types::OutPoint, utxo: &UTXO) -> Vec<u8> {
    serialize_coin_for_muhash(
        &op.hash,
        op.index,
        utxo.height as u32,
        utxo.is_coinbase,
        utxo.value,
        utxo.script_pubkey.as_ref(),
    )
}

/// Fold one validated block's UTXO delta into a running MuHash (engine IBD hot path).
pub(crate) fn fold_block_utxo_delta_muhash(
    delta: &blvm_protocol::block::UtxoDelta,
    spent_inputs: &blvm_protocol::UtxoSet,
    acc: &mut MuHash3072,
) {
    for (op, utxo) in delta.additions.iter() {
        acc.insert_mut(&utxo_muhash_preimage(op, utxo.as_ref()));
    }
    for dk in delta.deletions.iter() {
        let op = blvm_protocol::utxo_overlay::utxo_deletion_key_to_outpoint(dk);
        if let Some(utxo) = spent_inputs.get(&op) {
            acc.remove_mut(&utxo_muhash_preimage(&op, utxo.as_ref()));
        }
    }
}

/// Engine IBD: fold MuHash from block outputs + spends without building overlay `UtxoDelta`.
pub(crate) fn fold_block_engine_muhash(
    block: &blvm_protocol::Block,
    tx_ids: &[[u8; 32]],
    height: u64,
    session: &crate::storage::ibd_engine::SpendSession,
    acc: &mut MuHash3072,
) {
    use crate::storage::ibd_engine::types::outpoint_to_output_key;
    use blvm_protocol::transaction::is_coinbase;

    for (ti, tx) in block.transactions.iter().enumerate() {
        if ti >= tx_ids.len() {
            break;
        }
        let tx_id = tx_ids[ti];
        let is_cb = is_coinbase(tx);
        for (oi, out) in tx.outputs.iter().enumerate() {
            let op = blvm_protocol::types::OutPoint {
                hash: tx_id,
                index: oi as u32,
            };
            let utxo = UTXO {
                value: out.value,
                script_pubkey: out.script_pubkey.clone().into(),
                height,
                is_coinbase: is_cb,
            };
            acc.insert_mut(&utxo_muhash_preimage(&op, &utxo));
        }
    }

    for tx in block.transactions.iter().skip(1) {
        for input in &tx.inputs {
            let op = input.prevout;
            let key = outpoint_to_output_key(&op);
            let utxo_ref = session
                .key_to_idx
                .get(&key)
                .map(|&idx| session.details[idx].utxo.as_ref())
                .or_else(|| session.local_spends.get(&key).map(|d| d.utxo.as_ref()));
            if let Some(u) = utxo_ref {
                acc.remove_mut(&utxo_muhash_preimage(&op, u));
            }
        }
    }
}

/// Full-tree scan vs persisted rolling MuHash (optional integrity check).
pub(crate) fn verify_ibd_utxo_muhash_startup(storage: &Storage) -> Result<()> {
    let chain = storage.chain();
    let Some(bytes) = chain.get_ibd_utxo_muhash_running()? else {
        tracing::warn!(
            "BLVM_VERIFY_IBD_UTXO_MUHASH: no ibd_utxo_muhash_running in chain_info — skipping verify \
             (legacy DB or before first MuHash checkpoint)"
        );
        return Ok(());
    };

    // Open the IBD UTXO tree from the standalone LMDB when it exists, otherwise fall back to
    // the main storage canonical tree (may be a ckpt after Phase 3 promote).
    let _standalone_db: Option<Box<dyn crate::storage::database::Database>>;
    let tree: std::sync::Arc<dyn crate::storage::database::Tree> = if let Some(root) =
        storage.data_dir()
    {
        let utxo_store_dir = root.join(crate::storage::database::IBD_UTXO_STORE_SUBDIR);
        if utxo_store_dir.exists() {
            match crate::storage::database::create_ibd_utxo_standalone_db(&utxo_store_dir) {
                Ok(db) => match db.open_tree("ibd_utxos") {
                    Ok(t) => {
                        _standalone_db = Some(db);
                        std::sync::Arc::from(t)
                    }
                    Err(e) => {
                        tracing::warn!(
                            "MuHash verify: standalone ibd_utxo open_tree failed ({e}); \
                                 using main storage canonical"
                        );
                        _standalone_db = None;
                        storage.open_ibd_utxo_tree()?
                    }
                },
                Err(e) => {
                    tracing::warn!(
                        "MuHash verify: standalone LMDB open failed ({e}); using main storage canonical"
                    );
                    _standalone_db = None;
                    storage.open_ibd_utxo_tree()?
                }
            }
        } else {
            _standalone_db = None;
            storage.open_ibd_utxo_tree()?
        }
    } else {
        _standalone_db = None;
        storage.open_ibd_utxo_tree()?
    };
    let mut scan = MuHash3072::new();

    // heed3 fast path: scan_heed3 streams (k, v) slices from mmap'd LMDB pages —
    // no Vec<u8> allocation per entry for either key or value bytes.
    // Falls through to the iter path if the tree is not a heed3 instance.
    #[cfg(feature = "heed3")]
    if let Some(heed3_tree) = tree.as_heed3_tree() {
        heed3_tree.scan_heed3(|k, v| {
            if k.len() != 40 {
                return Ok(());
            }
            let mut key = [0u8; 40];
            key.copy_from_slice(&k[..40]);
            let op = key_to_outpoint(&key);
            let pre = if let Ok(archived) = crate::storage::rkyv_codec::access_utxo(v) {
                serialize_coin_for_muhash(
                    &op.hash,
                    op.index,
                    u64::from(archived.height) as u32,
                    archived.is_coinbase,
                    archived.value.into(),
                    archived.script_pubkey.as_slice(),
                )
            } else {
                // Bincode fallback for rows written before rkyv migration.
                let utxo: UTXO = bincode::deserialize(v)
                    .with_context(|| format!("decode ibd_utxos row {:?}", &k[..8]))?;
                utxo_muhash_preimage(&op, &utxo)
            };
            scan.insert_mut(&pre);
            Ok(())
        })?;
    } else {
        // heed3 enabled but this tree is not a Heed3Tree (should not normally happen).
        for row in tree.iter() {
            let (k, v) = row?;
            if k.len() != 40 {
                continue;
            }
            let mut key = [0u8; 40];
            key.copy_from_slice(&k[..40]);
            let op = key_to_outpoint(&key);
            let pre = if let Ok(archived) = crate::storage::rkyv_codec::access_utxo(&v) {
                serialize_coin_for_muhash(
                    &op.hash,
                    op.index,
                    u64::from(archived.height) as u32,
                    archived.is_coinbase,
                    archived.value.into(),
                    archived.script_pubkey.as_slice(),
                )
            } else {
                let utxo: UTXO = bincode::deserialize(&v)
                    .with_context(|| format!("decode ibd_utxos row {:?}", &k[..8]))?;
                utxo_muhash_preimage(&op, &utxo)
            };
            scan.insert_mut(&pre);
        }
    }

    // Non-heed3 build: all rows are bincode; use the owned-bytes iter path directly.
    #[cfg(not(feature = "heed3"))]
    for row in tree.iter() {
        let (k, v) = row?;
        if k.len() != 40 {
            continue;
        }
        let mut key = [0u8; 40];
        key.copy_from_slice(&k[..40]);
        let op = key_to_outpoint(&key);
        let utxo: UTXO = bincode::deserialize(&v)
            .with_context(|| format!("decode ibd_utxos row {:?}", &k[..8]))?;
        let pre = utxo_muhash_preimage(&op, &utxo);
        scan.insert_mut(&pre);
    }

    let expected = scan.finalize();
    let got = MuHash3072::deserialize_running_state(&bytes).finalize();
    if expected != got {
        anyhow::bail!(
            "IBD UTXO MuHash verify failed: full-tree scan finalized {:02x?} != persisted running state finalized {:02x?}",
            &expected[..],
            &got[..]
        );
    }
    tracing::info!("BLVM_VERIFY_IBD_UTXO_MUHASH: ibd_utxos MuHash OK");
    Ok(())
}

#[cfg(all(test, feature = "heed3"))]
mod verify_tests {
    use super::*;
    use crate::storage::Storage;
    use crate::storage::database::DatabaseBackend;
    use crate::storage::disk_utxo::outpoint_to_key;
    use crate::storage::utxo_value_codec::{ValueCodec, encode_utxo_with_codec};
    use blvm_muhash::{MUHASH_RUNNING_STATE_BYTES, MuHash3072};
    use blvm_protocol::types::{OutPoint, UTXO};
    use tempfile::TempDir;

    #[test]
    fn verify_ibd_utxo_muhash_startup_heed3_scan() {
        let temp_dir = TempDir::new().unwrap();
        let storage = Storage::with_backend(temp_dir.path(), DatabaseBackend::Heed3).unwrap();
        let tree = storage.open_tree("ibd_utxos").unwrap();

        let mut rolling = MuHash3072::new();
        for i in 0..32u64 {
            let op = OutPoint {
                hash: [i as u8; 32],
                index: 0,
            };
            let utxo = UTXO {
                value: 10_000 + i as i64,
                script_pubkey: vec![0x51].into(),
                height: i,
                is_coinbase: i == 0,
            };
            let key = outpoint_to_key(&op);
            tree.insert(
                &key,
                &encode_utxo_with_codec(ValueCodec::Rkyv, &utxo).unwrap(),
            )
            .unwrap();
            rolling.insert_mut(&utxo_muhash_preimage(&op, &utxo));
        }

        let running: [u8; MUHASH_RUNNING_STATE_BYTES] = rolling.serialize_running_state();
        storage
            .chain()
            .persist_ibd_utxo_flush_checkpoint(31, &running)
            .unwrap();

        verify_ibd_utxo_muhash_startup(&storage).expect("heed3 scan_heed3 MuHash verify");
    }
}

#[cfg(test)]
mod backfill_tests {
    use super::*;
    use crate::storage::disk_utxo::outpoint_to_key;
    use crate::storage::utxo_value_codec::encode_utxo_with_codec;
    use blvm_protocol::types::{OutPoint, UTXO};
    use tempfile::TempDir;

    fn one_utxo_row(storage: &Storage) {
        let utxo = UTXO {
            value: 50_000,
            script_pubkey: vec![0x51].into(),
            height: 1,
            is_coinbase: true,
        };
        let op = OutPoint {
            hash: [7u8; 32],
            index: 0,
        };
        let codec = storage.utxo_value_codec();
        storage
            .open_tree("ibd_utxos")
            .unwrap()
            .insert(
                &outpoint_to_key(&op),
                &encode_utxo_with_codec(codec, &utxo).unwrap(),
            )
            .unwrap();
    }

    #[test]
    fn backfill_errors_when_ckpt_empty_and_canonical_key_unset() {
        let dir = TempDir::new().unwrap();
        let storage = Storage::new(dir.path()).unwrap();
        storage
            .chain()
            .force_set_engine_export_height(155_522)
            .unwrap();
        one_utxo_row(&storage);
        let err = backfill_engine_export_muhash_if_missing(&storage).unwrap_err();
        assert!(err.to_string().contains("ibd_utxos_ckpt_a empty"), "{err}");
        assert!(
            storage
                .chain()
                .get_engine_export_muhash()
                .unwrap()
                .is_none()
        );
    }

    #[test]
    fn backfill_scans_canonical_ibd_utxos_when_ckpt_empty() {
        let dir = TempDir::new().unwrap();
        let storage = Storage::new(dir.path()).unwrap();
        storage
            .chain()
            .force_set_engine_export_height(155_522)
            .unwrap();
        storage
            .chain()
            .set_ibd_utxo_canonical_tree("ibd_utxos")
            .unwrap();
        one_utxo_row(&storage);
        assert!(
            storage
                .open_tree("ibd_utxos_ckpt_a")
                .unwrap()
                .is_empty()
                .unwrap()
        );

        assert!(backfill_engine_export_muhash_if_missing(&storage).unwrap());
        assert!(
            storage
                .chain()
                .get_engine_export_muhash()
                .unwrap()
                .is_some()
        );
        assert!(!backfill_engine_export_muhash_if_missing(&storage).unwrap());
    }
}