blvm-node 0.1.51

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
//! Main node run loop: block processing, network message poll, health and disk checks.

use anyhow::Result;
use std::sync::Arc;
use tracing::{debug, info, warn};

use crate::utils::{HANDSHAKE_POLL_SLEEP, log_error, with_custom_timeout};

/// Main node run loop. Called from `Node::start()` after components are started.
pub(crate) async fn run(node: &mut super::Node) -> Result<()> {
    // Subscribe before any slow startup work so SIGTERM is not missed.
    let mut shutdown_rx = crate::utils::create_shutdown_receiver();
    if *shutdown_rx.borrow() {
        info!("Shutdown already requested before run loop started");
        return Ok(());
    }

    info!("Node running - main loop started");

    // `current_height` is the height of the next block to connect (chain tip + 1).
    let chain_tip = node.storage.chain().get_height()?.unwrap_or(0);
    let mut current_height = chain_tip.saturating_add(1);
    // Full UTXO load can take minutes at mainnet scale and blocks shutdown; P2P relay
    // validation reloads from disk as needed. Catch-up sync uses parallel IBD + disk store.
    let mut utxo_set = if chain_tip == 0 {
        node.storage.utxos().get_all_utxos().unwrap_or_default()
    } else {
        Default::default()
    };
    node.network.replace_utxo_snapshot(utxo_set.clone()).await;

    // Catch up immediately if peers are ahead (do not wait for the periodic check).
    if let Err(e) = node.try_catch_up_ibd(&mut utxo_set).await {
        warn!("Catch-up sync check failed: {}", e);
    }
    if let Ok(Some(h)) = node.storage.chain().get_height() {
        current_height = h.saturating_add(1);
    }

    // Main node loop - coordinates between all components and handles shutdown signals
    let mut loop_counter: u64 = 0;
    loop {
        // Check for shutdown signal (non-blocking)
        if *shutdown_rx.borrow() {
            info!("Shutdown signal received, stopping node gracefully...");
            break;
        }
        // Process any received blocks (non-blocking)
        while let Some(block_data) = node.network.try_recv_block() {
            if let Ok(Some(h)) = node.storage.chain().get_height() {
                current_height = h.saturating_add(1);
            }
            info!("Processing block from network");
            let blocks_arc = node.storage.blocks();

            // Parse block to get hash for event publishing
            use crate::node::block_processor::parse_block_from_wire;
            let block_hash_for_validation =
                if let Ok((block, _)) = parse_block_from_wire(&block_data) {
                    use crate::storage::blockstore::BlockStore;
                    blocks_arc.get_block_hash(&block)
                } else {
                    [0u8; 32]
                };

            // Publish block validation started event
            if let Some(event_publisher) = node
                .module_subsystem
                .as_ref()
                .and_then(|s| s.event_publisher.as_ref())
            {
                event_publisher
                    .publish_block_validation_started(&block_hash_for_validation, current_height)
                    .await;
            }

            let validation_start_time = std::time::Instant::now();
            let prev_tip_header_bits = node
                .storage
                .chain()
                .get_tip_header()
                .ok()
                .flatten()
                .map(|h| h.bits)
                .unwrap_or(0);
            let prev_tip = node.storage.chain().get_tip_hash_and_height().ok();
            match node.sync_coordinator.process_block(
                &blocks_arc,
                &node.protocol,
                Some(&node.storage),
                &block_data,
                current_height,
                &mut utxo_set,
                Some(Arc::clone(&node.metrics)),
                Some(Arc::clone(&node.profiler)),
            ) {
                Ok(true) => {
                    info!("Block accepted at height {}", current_height);

                    let validation_time_ms = validation_start_time.elapsed().as_millis() as u64;
                    let tip_changed = node
                        .storage
                        .chain()
                        .get_tip_hash_and_height()
                        .ok()
                        .zip(prev_tip.as_ref())
                        .is_some_and(|((new_tip, new_h), (old_tip, old_h))| {
                            new_tip != *old_tip || new_h != *old_h
                        });

                    // Publish block validation completed event (success)
                    if let Some(event_publisher) = node
                        .module_subsystem
                        .as_ref()
                        .and_then(|s| s.event_publisher.as_ref())
                    {
                        event_publisher
                            .publish_block_validation_completed(
                                &block_hash_for_validation,
                                current_height,
                                true,
                                validation_time_ms,
                                None,
                            )
                            .await;
                    }

                    if tip_changed {
                        let blocks_arc = node.storage.blocks();
                        let (block_hash, tip_height) = node
                            .storage
                            .chain()
                            .get_tip_hash_and_height()
                            .unwrap_or(([0u8; 32], current_height));

                        if let Ok(Some(block)) = blocks_arc.get_block(&block_hash) {
                            if block.header.bits != prev_tip_header_bits {
                                if let Some(ep) = node
                                    .module_subsystem
                                    .as_ref()
                                    .and_then(|s| s.event_publisher.as_ref())
                                {
                                    ep.publish_mining_difficulty_changed(
                                        prev_tip_header_bits as u32,
                                        block.header.bits as u32,
                                        tip_height,
                                    )
                                    .await;
                                }
                            }

                            let transaction_count =
                                node.storage.transaction_count().unwrap_or(0) as u64;
                            log_error(
                                || {
                                    node.storage.chain().update_utxo_stats_cache(
                                        &block_hash,
                                        tip_height,
                                        &utxo_set,
                                        transaction_count,
                                    )
                                },
                                "Failed to update UTXO stats cache",
                            );

                            log_error(
                                || {
                                    node.storage.chain().calculate_and_cache_network_hashrate(
                                        tip_height,
                                        &blocks_arc,
                                    )
                                },
                                "Failed to update network hashrate cache",
                            );

                            if let Some(event_publisher) = node
                                .module_subsystem
                                .as_ref()
                                .and_then(|s| s.event_publisher.as_ref())
                            {
                                event_publisher
                                    .publish_new_block(&block, &block_hash, tip_height)
                                    .await;
                            }

                            if let Err(e) = node.storage.utxos().store_utxo_set(&utxo_set) {
                                warn!(
                                    "Failed to persist UTXO set after block {}: {}",
                                    tip_height, e
                                );
                            }
                            node.network.replace_utxo_snapshot(utxo_set.clone()).await;

                            #[cfg(feature = "utxo-commitments")]
                            {
                                if let Some(pruning_manager) = node.storage.pruning() {
                                    if let (Some(commitment_store), Some(_utxostore)) = (
                                        pruning_manager.commitment_store(),
                                        pruning_manager.utxostore(),
                                    ) {
                                        if let Err(e) = pruning_manager
                                            .generate_commitment_from_current_state(
                                                &block_hash,
                                                tip_height,
                                                &utxo_set,
                                                &commitment_store,
                                            )
                                        {
                                            warn!(
                                                "Failed to generate commitment for block {}: {}",
                                                tip_height, e
                                            );
                                        } else {
                                            debug!(
                                                "Generated UTXO commitment for block {}",
                                                tip_height
                                            );
                                        }
                                    }
                                }
                            }

                            current_height = tip_height + 1;
                        } else {
                            warn!("Failed to load block at active tip height {tip_height}");
                        }
                    }

                    // Check for incremental pruning during IBD
                    // Consider IBD if we're still syncing (height < tip or no recent blocks)
                    let is_ibd = current_height < 1000; // Simple heuristic: consider IBD if < 1000 blocks
                    if let Some(pruning_manager) = node.storage.pruning() {
                        if let Ok(Some(prune_stats)) =
                            pruning_manager.incremental_prune_during_ibd(current_height, is_ibd)
                        {
                            info!(
                                "Incremental pruning during IBD: {} blocks pruned, {} bytes freed",
                                prune_stats.blocks_pruned, prune_stats.storage_freed
                            );
                            // Flush storage to persist pruning changes
                            if let Err(e) = node.storage.flush() {
                                warn!("Failed to flush storage after incremental pruning: {}", e);
                            }
                        }
                    }

                    // Check for automatic pruning after block acceptance
                    if let Some(pruning_manager) = node.storage.pruning() {
                        let stats = pruning_manager.get_stats();
                        let should_prune = pruning_manager
                            .should_auto_prune(current_height, stats.last_prune_height);

                        if should_prune {
                            info!("Automatic pruning triggered at height {}", current_height);

                            // Calculate prune height based on configuration
                            let prune_height = match &pruning_manager.config.mode {
                                crate::config::PruningMode::Disabled => None,
                                crate::config::PruningMode::Normal {
                                    keep_from_height, ..
                                } => {
                                    // Prune to keep_from_height, but ensure we keep min_blocks
                                    let min_keep = pruning_manager.config.min_blocks_to_keep;
                                    let effective_keep = (*keep_from_height)
                                        .max(current_height.saturating_sub(min_keep));
                                    Some(effective_keep)
                                }
                                #[cfg(feature = "utxo-commitments")]
                                crate::config::PruningMode::Aggressive {
                                    keep_from_height,
                                    min_blocks,
                                    ..
                                } => {
                                    // Prune to keep_from_height, respecting min_blocks
                                    let sub = current_height.saturating_sub(*min_blocks);
                                    let effective_keep = (*keep_from_height).max(sub);
                                    Some(effective_keep)
                                }
                                #[cfg(not(feature = "utxo-commitments"))]
                                crate::config::PruningMode::Aggressive { .. } => {
                                    // Aggressive pruning requires utxo-commitments feature
                                    // Fall back to no pruning if feature is disabled
                                    None
                                }
                                crate::config::PruningMode::Custom {
                                    keep_bodies_from_height,
                                    ..
                                } => {
                                    // Prune to keep_bodies_from_height, respecting min_blocks
                                    let min_keep = pruning_manager.config.min_blocks_to_keep;
                                    let effective_keep = (*keep_bodies_from_height)
                                        .max(current_height.saturating_sub(min_keep));
                                    Some(effective_keep)
                                }
                            };

                            if let Some(prune_to_height) = prune_height {
                                if prune_to_height < current_height {
                                    match pruning_manager.prune_to_height(
                                        prune_to_height,
                                        current_height,
                                        false,
                                    ) {
                                        Ok(prune_stats) => {
                                            info!(
                                                "Automatic pruning completed: {} blocks pruned, {} blocks kept",
                                                prune_stats.blocks_pruned, prune_stats.blocks_kept
                                            );
                                            // Flush storage to persist pruning changes
                                            use crate::utils::log_error;
                                            log_error(
                                                || node.storage.flush(),
                                                "Failed to flush storage after automatic pruning",
                                            );
                                        }
                                        Err(e) => {
                                            warn!("Automatic pruning failed: {}", e);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                Ok(false) => {
                    warn!("Block rejected at height {}", current_height);
                    if let Err(e) = node.try_catch_up_ibd(&mut utxo_set).await {
                        warn!("Catch-up after block rejection failed: {}", e);
                    }
                    if let Ok(Some(h)) = node.storage.chain().get_height() {
                        current_height = h.saturating_add(1);
                    }
                    let validation_time_ms = validation_start_time.elapsed().as_millis() as u64;

                    // Publish block validation completed event (failure)
                    if let Some(event_publisher) = node
                        .module_subsystem
                        .as_ref()
                        .and_then(|s| s.event_publisher.as_ref())
                    {
                        event_publisher
                            .publish_block_validation_completed(
                                &block_hash_for_validation,
                                current_height,
                                false,
                                validation_time_ms,
                                Some("Block validation failed"),
                            )
                            .await;
                    }
                }
                Err(e) => {
                    warn!("Error processing block: {}", e);
                    let validation_time_ms = validation_start_time.elapsed().as_millis() as u64;

                    // Publish block validation completed event (error)
                    if let Some(event_publisher) = node
                        .module_subsystem
                        .as_ref()
                        .and_then(|s| s.event_publisher.as_ref())
                    {
                        event_publisher
                            .publish_block_validation_completed(
                                &block_hash_for_validation,
                                current_height,
                                false,
                                validation_time_ms,
                                Some(&format!("Block processing error: {e}")),
                            )
                            .await;
                    }
                }
            }
        }

        // Peer traffic is drained by the background task spawned in start_components
        // (`NetworkManager::process_messages`). Do not call it here: it never returns until
        // the channel closes, which would starve this loop and leave `pending_blocks` undrained.

        tokio::select! {
            biased;
            Ok(()) = shutdown_rx.changed() => {
                if *shutdown_rx.borrow() {
                    info!("Shutdown signal received, stopping node gracefully...");
                    break;
                }
            }
            _ = tokio::time::sleep(HANDSHAKE_POLL_SLEEP) => {}
        }

        loop_counter = loop_counter.saturating_add(1);

        // Peers advance the tip while we idle; re-check every ~5s and resume parallel IBD when behind.
        if loop_counter % 50 == 0 {
            if let Err(e) = node.try_catch_up_ibd(&mut utxo_set).await {
                warn!("Catch-up sync check failed: {}", e);
            }
            if let Ok(Some(h)) = node.storage.chain().get_height() {
                current_height = h.saturating_add(1);
            }
        }

        // Check node health periodically
        node.check_health().await?;

        // Check disk space periodically (every 10 iterations = ~1 second)
        // Use timeout to prevent hanging on slow disk operations
        let counter = node
            .disk_check_counter
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        if counter % 10 == 0 {
            let timeout_dur = node.storage_timeout();
            use crate::utils::with_custom_timeout;
            match with_custom_timeout(async { node.check_disk_space().await }, timeout_dur).await {
                Ok(Ok(())) => {
                    // Disk check succeeded
                }
                Ok(Err(e)) => {
                    warn!("Disk space check failed: {}", e);
                    // Continue - disk errors don't stop the node
                }
                Err(_) => {
                    warn!("Disk space check timed out");
                    // Continue - timeout doesn't stop the node
                }
            }
        }
    }

    // Graceful shutdown - stop all components
    info!("Initiating graceful shutdown...");
    node.stop().await?;
    Ok(())
}