blvm-node 0.1.35

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
//! Disk-backed UTXO set with bounded in-memory cache
//!
//! Solves the OOM problem during IBD by keeping only a bounded subset of UTXOs
//! in memory and storing the complete set on disk (redb).
//!
//! ## Architecture
//!
//! ```text
//! ┌───────────────────────┐
//! │  In-Memory Cache      │  ← Bounded (e.g., 5M entries ≈ 2.5GB)
//! │  HashMap<OutPoint, U> │
//! └──────────┬────────────┘
//!            │ cache miss → load from disk
//! ┌──────────▼────────────┐
//! │  Disk Store (redb)    │  ← ALL UTXOs, unbounded
//! │  Tree: "ibd_utxos"    │
//! └───────────────────────┘
//! ```
//!
//! ## Performance optimizations (unified path)
//!
//! - **Incremental flush from block 1**: Always sync block changes to pending_writes;
//!   flush to disk when threshold reached. No bulk flush. No mode switch.
//! - **Flush without cache drop**: Cache stays warm; only pending_writes drains to disk.
//! - **Unified path**: Prefetch → validate → sync → evict runs every block.
//!   Early blocks: prefetch/sync/evict are fast (cache hit, small pending, no eviction).
//! - **Pending flush log**: append + sort/dedupe at flush (last write wins per key)
//! - **Fixed-size keys**: `[u8; 40]` avoids heap allocation per outpoint
//! - **Batch eviction**: Only evict when 10% over limit, clear 15% headroom
//! - **Cache size**: Auto-tuned by MemoryGuard from system RAM.

use crate::storage::database::Tree;
use anyhow::Result;

/// Historical name (TidesDB had `TDB_MAX_TXN_OPS=100000`). On RocksDB this is a safety cap
/// that splits a `flush_batch_to_disk` into multiple `WriteBatch::commit()`s when the retire
/// hot path emits a very large pending package. Each `commit()` has a fixed cost (atomic
/// sequence + memtable lock + WAL fsync if enabled) so larger chunks = fewer RocksDB write
/// barriers. Aligned with `TIDESDB_MAX_TXN_OPS` (200k) so a 200k-op flush is one batch.
pub(crate) const MAX_BATCH_OPS: usize = 200_000;

/// Don't evict outputs created in the last N blocks (likely to be spent soon).
const EVICT_MIN_AGE_BLOCKS: u64 = 100;
/// Prefer evicting outputs older than this (creation height < current - N).
const EVICT_VERY_OLD_BLOCKS: u64 = 10_000;
/// Dust threshold (satoshis) — eviction sort prefers lowest value first (dust).
#[allow(dead_code)]
const EVICT_DUST_THRESHOLD: i64 = 546;
use blvm_protocol::transaction::is_coinbase;
use blvm_protocol::types::{Block, Hash, OutPoint, UTXO};
use rustc_hash::{FxHashMap, FxHashSet};
use std::sync::Arc;
use tracing::debug;

/// Fixed-size outpoint key: 32 bytes txid + 8 bytes index (big-endian)
pub type OutPointKey = [u8; 40];

/// Pending/flushing value: UTXO kept in memory; serialized only when flushing to disk.
/// Some(arc)=insert (Arc avoids clone on get_pending), None=delete. Serialize deferred to flush.
type PendingValue = Option<Arc<UTXO>>;

/// Serialize an OutPoint to a fixed-size storage key.
/// Zero-allocation: returns a stack-allocated array instead of Vec.
#[inline]
pub fn outpoint_to_key(outpoint: &OutPoint) -> OutPointKey {
    let mut key = [0u8; 40];
    key[..32].copy_from_slice(&outpoint.hash);
    key[32..40].copy_from_slice(&(outpoint.index as u64).to_be_bytes());
    key
}

/// Convert storage key back to OutPoint for cache removal.
#[inline]
pub fn key_to_outpoint(key: &OutPointKey) -> OutPoint {
    let mut hash = [0u8; 32];
    hash.copy_from_slice(&key[..32]);
    let index = u64::from_be_bytes(key[32..40].try_into().unwrap()) as u32;
    OutPoint { hash, index }
}

/// Load UTXOs for given keys from disk. Used by prefetch overlap (spawn_blocking).
///
/// Uses `Tree::get_many_no_cache`: on RocksDB this calls `multi_get_cf_opt` with
/// `fill_cache = false`. Prefetch reads each UTXO exactly once; not caching the SST data
/// blocks prevents cold blocks from evicting hot recently-written UTXO SST blocks in the
/// dedicated 128 MB UTXO block cache.
///
/// Fallback: sequential get. No par_iter (was causing 500+ concurrent get = lock contention).
///
/// Returns `(map, keys_sorted)` so callers can scan the same key set (e.g. in-flight UTXO
/// fallback) without cloning the request list.
pub(crate) fn load_keys_from_disk(
    disk: Arc<dyn Tree>,
    mut keys: Vec<OutPointKey>,
    codec: crate::storage::utxo_value_codec::ValueCodec,
) -> Result<(FxHashMap<OutPointKey, UTXO>, Vec<OutPointKey>)> {
    if keys.is_empty() {
        return Ok((FxHashMap::default(), Vec::new()));
    }
    keys.sort_unstable();
    let mut key_refs: Vec<&[u8]> = Vec::with_capacity(keys.len());
    for k in &keys {
        key_refs.push(k.as_slice());
    }

    // heed3 zero-copy fast path: read mmap'd LMDB pages directly — no Vec<u8> per value.
    // Guard: only valid when all rows were written with the rkyv codec.
    #[cfg(feature = "heed3")]
    if codec == crate::storage::utxo_value_codec::ValueCodec::Rkyv {
        if let Some(heed3_tree) = disk.as_heed3_tree() {
            let rtxn = heed3_tree.env().read_txn()?;
            let slices = heed3_tree.get_many_heed3(&key_refs, &rtxn)?;
            let mut result = FxHashMap::with_capacity_and_hasher(keys.len(), Default::default());
            for (key, opt_bytes) in keys.iter().zip(slices) {
                if let Some(bytes) = opt_bytes {
                    if let Ok(archived) = crate::storage::rkyv_codec::access_utxo(bytes) {
                        result.insert(
                            *key,
                            crate::storage::rkyv_codec::utxo_from_archived(archived),
                        );
                    }
                }
            }
            return Ok((result, keys));
        }
    }

    let values = disk.get_many_no_cache(&key_refs)?;
    let mut result = FxHashMap::with_capacity_and_hasher(keys.len(), Default::default());
    // Serial deserialize: par_iter here was harmful in the IBD hot path. With N validation
    // workers each calling supplement_utxo_map_with_buf concurrently, every worker dispatched
    // its disk-load deserialization onto the same global rayon pool. 8 par_iters competing
    // for 11 rayon threads thrashed the pool with split/join overhead while the validation
    // workers themselves blocked on the rayon barrier. Typical cache-miss batches are 10–500
    // UTXOs, deserializing in <500µs serially — well under par_iter's coordination overhead.
    // Keeping this serial frees the rayon pool for genuinely block-level parallel work and
    // lets validation workers achieve true N-way parallelism.
    for (key, value) in keys.iter().zip(values.into_iter()) {
        if let Some(data) = value {
            if let Ok(utxo) = crate::storage::utxo_value_codec::decode_utxo_with_codec(codec, &data)
            {
                result.insert(*key, utxo);
            }
        }
    }
    Ok((result, keys))
}

/// Reuses buffer for block input keys. Avoids per-block alloc in IBD v2 validation hot path.
#[inline]
pub fn block_input_keys_into(block: &Block, keys_out: &mut Vec<OutPointKey>) {
    let est: usize = block
        .transactions
        .iter()
        .filter(|tx| !is_coinbase(tx))
        .map(|tx| tx.inputs.len())
        .sum();
    keys_out.clear();
    keys_out.reserve(est);
    for tx in block.transactions.iter() {
        if is_coinbase(tx) {
            continue;
        }
        for input in tx.inputs.iter() {
            keys_out.push(outpoint_to_key(&input.prevout));
        }
    }
}

/// Collect and deduplicate outpoint keys from multiple blocks (for batched lookahead prefetch).
/// Reduces TidesDB round-trips by loading UTXOs for several blocks in one disk read batch.
pub(crate) fn block_input_keys_batch(blocks: &[&Block]) -> Vec<OutPointKey> {
    let est: usize = blocks
        .iter()
        .map(|b| {
            b.transactions
                .iter()
                .filter(|tx| !is_coinbase(tx))
                .map(|tx| tx.inputs.len())
                .sum::<usize>()
        })
        .sum();
    let mut seen = FxHashSet::with_capacity_and_hasher(est, Default::default());
    let mut keys = Vec::with_capacity(est);
    for block in blocks {
        for tx in block.transactions.iter() {
            if is_coinbase(tx) {
                continue;
            }
            for input in tx.inputs.iter() {
                let key = outpoint_to_key(&input.prevout);
                if seen.insert(key) {
                    keys.push(key);
                }
            }
        }
    }
    keys
}

/// Same as `block_input_keys_batch` but reuses buffers. Avoids per-block allocations in hot path.
/// Caller provides cleared buffers; this clears and refills keys_out, reuses seen for dedup.
pub(crate) fn block_input_keys_batch_into(
    blocks: &[&Block],
    keys_out: &mut Vec<OutPointKey>,
    seen: &mut FxHashSet<OutPointKey>,
) {
    let est: usize = blocks
        .iter()
        .map(|b| {
            b.transactions
                .iter()
                .filter(|tx| !is_coinbase(tx))
                .map(|tx| tx.inputs.len())
                .sum::<usize>()
        })
        .sum();
    keys_out.clear();
    keys_out.reserve(est);
    seen.clear();
    for block in blocks {
        for tx in block.transactions.iter() {
            if is_coinbase(tx) {
                continue;
            }
            for input in tx.inputs.iter() {
                let key = outpoint_to_key(&input.prevout);
                if seen.insert(key) {
                    keys_out.push(key);
                }
            }
        }
    }
}

/// Same as `block_input_keys_batch_into` but takes `Arc<Block>`. Avoids holding refs into
/// ready_buffer (fixes borrow conflicts with insert/remove_entry in validation loop).
pub(crate) fn block_input_keys_batch_into_arc(
    blocks: &[Arc<Block>],
    keys_out: &mut Vec<OutPointKey>,
    seen: &mut FxHashSet<OutPointKey>,
) {
    let est: usize = blocks
        .iter()
        .map(|b| {
            b.transactions
                .iter()
                .filter(|tx| !is_coinbase(tx))
                .map(|tx| tx.inputs.len())
                .sum::<usize>()
        })
        .sum();
    keys_out.clear();
    keys_out.reserve(est);
    seen.clear();
    for block in blocks {
        for tx in block.transactions.iter() {
            if is_coinbase(tx) {
                continue;
            }
            for input in tx.inputs.iter() {
                let key = outpoint_to_key(&input.prevout);
                if seen.insert(key) {
                    keys_out.push(key);
                }
            }
        }
    }
}

/// Like `block_input_keys_into` but filters out intra-block spends.
///
/// Only skips prefetch when the input spends an output of a **non-coinbase** transaction that
/// appears **earlier in this block** (`tx_ids[j] == prevout.hash` for some `j` with `1 <= j < idx`).
/// Those UTXOs are not on disk yet; `connect_block_ibd`'s overlay supplies them after earlier txs.
///
/// Prevouts matching **coinbase** (`j == 0`) are never treated as prefetch-elidable here: BIP30
/// chain UTXOs can share a txid with this block's coinbase and must still load from disk.
///
/// Returns the number of keys filtered out (informational; log at tracing::debug level if needed).
/// Filter input keys using precomputed `tx_ids` (same length as `block.transactions`).
pub fn block_input_keys_into_filtered_with_tx_ids(
    block: &Block,
    tx_ids: &[Hash],
    keys_out: &mut Vec<OutPointKey>,
) -> usize {
    let est: usize = block
        .transactions
        .iter()
        .filter(|tx| !is_coinbase(tx))
        .map(|tx| tx.inputs.len())
        .sum();
    keys_out.clear();
    keys_out.reserve(est);

    let mut filtered = 0usize;
    for (spending_idx, tx) in block.transactions.iter().enumerate() {
        if is_coinbase(tx) {
            continue;
        }
        for input in tx.inputs.iter() {
            let h = input.prevout.hash;
            let funded_by_prior_non_cb = (1..spending_idx).any(|j| tx_ids[j] == h);
            if funded_by_prior_non_cb {
                filtered += 1;
            } else {
                keys_out.push(outpoint_to_key(&input.prevout));
            }
        }
    }
    filtered
}

/// One `compute_block_tx_ids` + filtered keys (reuses `tx_ids_buf`).
pub fn block_input_keys_and_tx_ids_filtered(
    block: &Block,
    tx_ids_buf: &mut Vec<Hash>,
    keys_out: &mut Vec<OutPointKey>,
) -> usize {
    use blvm_protocol::block::compute_block_tx_ids_into;
    compute_block_tx_ids_into(block, tx_ids_buf);
    block_input_keys_into_filtered_with_tx_ids(block, tx_ids_buf, keys_out)
}

/// Engine-mode variant: only compute tx_ids (keys are not needed when the age-tiered engine
/// owns UTXO resolution). Avoids iterating all inputs a second time for key extraction.
pub fn compute_tx_ids_only(block: &Block, tx_ids_buf: &mut Vec<Hash>) {
    use blvm_protocol::block::compute_block_tx_ids_into;
    compute_block_tx_ids_into(block, tx_ids_buf);
}

pub fn block_input_keys_into_filtered(block: &Block, keys_out: &mut Vec<OutPointKey>) -> usize {
    use blvm_protocol::block::compute_block_tx_ids;
    let tx_ids = compute_block_tx_ids(block);
    block_input_keys_into_filtered_with_tx_ids(block, &tx_ids, keys_out)
}

/// Pre-computed sync batch for disk persistence. Applied by IbdUtxoStore::apply_sync_batch.
/// Inserts hold Arc<UTXO> to avoid clone in IBD v2 apply_sync_batch hot path.
pub struct SyncBatch {
    pub deletes: Vec<OutPointKey>,
    pub inserts: Vec<(OutPointKey, Arc<UTXO>)>,
    pub total_delta: isize,
}

/// Flush a batch of UTXO operations to disk. Splits into chunks of MAX_BATCH_OPS to stay
/// under TidesDB's TDB_MAX_TXN_OPS (100k). Used by IbdUtxoStore.
pub fn flush_batch_to_disk(
    batch: &[(OutPointKey, PendingValue)],
    disk: &dyn Tree,
    codec: crate::storage::utxo_value_codec::ValueCodec,
) -> Result<usize> {
    if batch.is_empty() {
        return Ok(0);
    }
    let mut total_flushed = 0;
    for chunk in batch.chunks(MAX_BATCH_OPS) {
        let mut b = disk.batch()?;
        for (key, value_opt) in chunk {
            match value_opt {
                Some(arc) => {
                    let ser_buf = crate::storage::utxo_value_codec::encode_utxo_with_codec(
                        codec,
                        arc.as_ref(),
                    )?;
                    b.put(key.as_slice(), ser_buf.as_slice());
                }
                None => b.delete(key.as_slice()),
            }
        }
        b.commit()?;
        total_flushed += chunk.len();
    }
    debug!(
        "flush_batch_to_disk: flushed {} operations to disk",
        total_flushed
    );
    Ok(total_flushed)
}

#[cfg(all(test, feature = "heed3"))]
mod heed3_load_tests {
    use super::*;
    use crate::storage::database::{create_database, Database, DatabaseBackend, Tree};
    use crate::storage::rkyv_codec::{access_utxo, utxo_from_archived};
    use crate::storage::utxo_value_codec::{encode_utxo_with_codec, ValueCodec};
    use blvm_protocol::types::{OutPoint, UTXO};
    use std::sync::Arc;
    use tempfile::TempDir;

    #[test]
    fn load_keys_from_disk_heed3_zero_copy_matches_owned_path() {
        let temp_dir = TempDir::new().unwrap();
        let db: Arc<dyn Database> =
            Arc::from(create_database(temp_dir.path(), DatabaseBackend::Heed3, None).unwrap());
        let tree: Arc<dyn Tree> = Arc::from(db.open_tree("ibd_utxos").unwrap());

        let mut keys = Vec::new();
        for i in 0..64u64 {
            let op = OutPoint {
                hash: [i as u8; 32],
                index: 1,
            };
            let key = outpoint_to_key(&op);
            let utxo = UTXO {
                value: (i as i64) * 500,
                script_pubkey: vec![0x51, (i & 0xff) as u8].into(),
                height: i,
                is_coinbase: false,
            };
            tree.insert(
                &key,
                &encode_utxo_with_codec(ValueCodec::Rkyv, &utxo).unwrap(),
            )
            .unwrap();
            keys.push(key);
        }

        let (zc_map, _) =
            load_keys_from_disk(Arc::clone(&tree), keys.clone(), ValueCodec::Rkyv).unwrap();
        assert_eq!(zc_map.len(), 64);

        let owned = tree
            .get_many_no_cache(&keys.iter().map(|k| k.as_slice()).collect::<Vec<_>>())
            .unwrap();
        for (key, opt) in keys.iter().zip(owned) {
            let zc = zc_map.get(key).expect("zero-copy map missing key");
            let data = opt.expect("owned get missing key");
            let archived = access_utxo(&data).unwrap();
            let from_owned = utxo_from_archived(archived);
            assert_eq!(*zc, from_owned);
        }
    }
}