blvm-node 0.1.2

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
//! 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::{log_error, with_custom_timeout, HANDSHAKE_POLL_SLEEP};

/// Main node run loop. Called from `Node::start()` after components are started.
pub(crate) async fn run(node: &mut super::Node) -> Result<()> {
    info!("Node running - main loop started");

    // Set up graceful shutdown signal handling
    let shutdown_rx = crate::utils::create_shutdown_receiver();

    // Get initial state for block processing
    let mut current_height = node.storage.chain().get_height()?.unwrap_or(0);
    let mut utxo_set = blvm_protocol::UtxoSet::default();

    // Main node loop - coordinates between all components and handles shutdown signals
    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() {
            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();
            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;

                    // 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;
                    }

                    // Parse block for governance webhook (need block object, not just block_data)
                    // We'll get it from storage after it's stored
                    let blocks_arc = node.storage.blocks();
                    let block_hash =
                        if let Ok(Some(hash)) = blocks_arc.get_hash_by_height(current_height) {
                            hash
                        } else {
                            warn!("Failed to get block hash for height {}", current_height);
                            [0u8; 32]
                        };

                    // Update chain tip (for chainwork, etc.)
                    if let Ok(Some(block)) = blocks_arc.get_block(&block_hash) {
                        // Capture old tip bits before update (for MiningDifficultyChanged)
                        let old_bits = node
                            .storage
                            .chain()
                            .get_tip_header()
                            .ok()
                            .flatten()
                            .map(|h| h.bits)
                            .unwrap_or(0);

                        log_error(
                            || {
                                node.storage.chain().update_tip(
                                    &block_hash,
                                    &block.header,
                                    current_height,
                                )
                            },
                            "Failed to update chain tip",
                        );

                        // Publish MiningDifficultyChanged when bits (difficulty target) changes
                        if block.header.bits != old_bits {
                            if let Some(ep) = node
                                .module_subsystem
                                .as_ref()
                                .and_then(|s| s.event_publisher.as_ref())
                            {
                                ep.publish_mining_difficulty_changed(
                                    old_bits as u32,
                                    block.header.bits as u32,
                                    current_height,
                                )
                                .await;
                            }
                        }

                        // Update UTXO stats cache (for fast gettxoutsetinfo RPC)
                        let transaction_count =
                            node.storage.transaction_count().unwrap_or(0) as u64;
                        log_error(
                            || {
                                node.storage.chain().update_utxo_stats_cache(
                                    &block_hash,
                                    current_height,
                                    &utxo_set,
                                    transaction_count,
                                )
                            },
                            "Failed to update UTXO stats cache",
                        );

                        // Update network hashrate cache (for fast getmininginfo RPC)
                        log_error(
                            || {
                                node.storage.chain().calculate_and_cache_network_hashrate(
                                    current_height,
                                    &blocks_arc,
                                )
                            },
                            "Failed to update network hashrate cache",
                        );

                        // Publish NewBlock event to modules
                        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, current_height)
                                .await;
                        }

                        // Governance module subscribes to NewBlock events and handles notifications
                        // No direct webhook call needed - handled via event system
                    }

                    // Persist UTXO set to storage after block validation
                    // This is critical for commitment generation and incremental pruning
                    if let Err(e) = node.storage.utxos().store_utxo_set(&utxo_set) {
                        warn!(
                            "Failed to persist UTXO set after block {}: {}",
                            current_height, e
                        );
                    }

                    // Generate UTXO commitment from current state (if enabled)
                    // Use current_height (the block that was just validated) before incrementing
                    #[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(),
                            ) {
                                // Get block hash from storage (block was just stored at current_height)
                                let blocks_arc = node.storage.blocks();
                                if let Ok(Some(block_hash)) =
                                    blocks_arc.get_hash_by_height(current_height)
                                {
                                    // Generate commitment from current UTXO set state
                                    if let Err(e) = pruning_manager
                                        .generate_commitment_from_current_state(
                                            &block_hash,
                                            current_height,
                                            &utxo_set,
                                            &commitment_store,
                                        )
                                    {
                                        warn!(
                                            "Failed to generate commitment for block {}: {}",
                                            current_height, e
                                        );
                                    } else {
                                        debug!(
                                            "Generated UTXO commitment for block {}",
                                            current_height
                                        );
                                    }
                                } else {
                                    warn!("Could not find block hash for height {} to generate commitment", current_height);
                                }
                            }
                        }
                    }

                    // Increment height after processing
                    current_height += 1;

                    // 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);
                    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::time::sleep(HANDSHAKE_POLL_SLEEP).await;

        // 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(())
}