Skip to main content

cuprate_blockchain/ops/
block.rs

1//! Block functions.
2use std::{borrow::Cow, cmp::min, collections::HashMap, io};
3
4use bytes::{Buf, Bytes};
5use fjall::Readable;
6use monero_oxide::{
7    block::{Block, BlockHeader},
8    transaction::{Pruned, Transaction},
9};
10use tapes::{BlobTape, TapesAppend, TapesRead, TapesTruncate};
11use tracing::instrument;
12
13use cuprate_constants::block::MAX_BLOCK_HEIGHT_USIZE;
14use cuprate_helper::{
15    cast::{u64_to_usize, usize_to_u64},
16    map::{combine_low_high_bits_to_u128, split_u128_into_low_high_bits},
17    tx::tx_fee,
18};
19use cuprate_pruning::{
20    PruningSeed, CRYPTONOTE_PRUNING_LOG_STRIPES, CRYPTONOTE_PRUNING_STRIPE_SIZE,
21    CRYPTONOTE_PRUNING_TIP_BLOCKS,
22};
23use cuprate_types::{
24    AltBlockInformation, BlockCompleteEntry, ChainId, ExtendedBlockHeader, HardFork,
25    PrunedTxBlobEntry, TransactionBlobs, VerifiedBlockInformation, VerifiedTransactionInformation,
26};
27
28use crate::{
29    database::{PrunableTables, CHAIN_TIP_KEY},
30    error::{BlockchainError, DbResult},
31    ops::{
32        alt_block,
33        blockchain::chain_height,
34        tx::{
35            add_tx_info_to_dynamic_tables, add_tx_info_to_tapes, get_tx_from_id,
36            remove_tx_from_dynamic_tables,
37        },
38    },
39    types::{Amount, BlockHash, BlockHeight, BlockInfo},
40    BlockchainDatabase,
41};
42
43/// Write a slice of contiguous blocks to the tapes database.
44///
45/// The blocks must follow directly from the top of the chain.
46///
47/// # Panic
48///
49/// This will panic if the given blocks cross 3 pruning stripes.
50#[instrument(skip_all, level = "info")]
51pub fn add_blocks_to_tapes(
52    blocks: &[VerifiedBlockInformation],
53    db: &BlockchainDatabase,
54    append_tx: &mut tapes::TapesAppendTransaction,
55) -> DbResult<()> {
56    /// A helper function to write the v2 prunable data, returning the index of the first v2 prunable byte.
57    fn write_v2_prunable_data(
58        append_tx: &mut tapes::TapesAppendTransaction,
59        stripe: u32,
60        prunable_tables: &PrunableTables,
61        blocks: &[VerifiedBlockInformation],
62    ) -> io::Result<u64> {
63        fn write_v2_prunable_data_inner(
64            append_tx: &mut tapes::TapesAppendTransaction,
65            tape: &impl BlobTape,
66            blocks: &[VerifiedBlockInformation],
67        ) -> io::Result<u64> {
68            let first_idx = append_tx
69                .blob_tape_len(tape)
70                .expect("Required tape not found");
71            for block in blocks {
72                for tx in &block.txs {
73                    if tx.tx.version() != 1 {
74                        append_tx.append_bytes(tape, tx.tx_prunable_blob.as_slice())?;
75                    }
76                }
77            }
78
79            Ok(first_idx)
80        }
81
82        if let Some(prunable_tape) = prunable_tables.try_get_prunable_tape(stripe) {
83            write_v2_prunable_data_inner(append_tx, prunable_tape, blocks)
84        } else {
85            let PrunableTables::Pruned {
86                prunable_tip_blobs, ..
87            } = prunable_tables
88            else {
89                unreachable!()
90            };
91            write_v2_prunable_data_inner(append_tx, prunable_tip_blobs, blocks)
92        }
93    }
94
95    /// A helper function to write the v1 prunable data, returning the index of the first v1 prunable byte.
96    fn write_v1_prunable_data(
97        append_tx: &mut tapes::TapesAppendTransaction,
98        db: &BlockchainDatabase,
99        blocks: &[VerifiedBlockInformation],
100    ) -> io::Result<u64> {
101        let first_idx = append_tx
102            .blob_tape_len(&db.v1_prunable_blobs)
103            .expect("Required tape not found");
104
105        for block in blocks {
106            for tx in &block.txs {
107                if tx.tx.version() == 1 {
108                    append_tx.append_bytes(&db.v1_prunable_blobs, &tx.tx_prunable_blob)?;
109                }
110            }
111        }
112
113        Ok(first_idx)
114    }
115
116    // First fill in the `pruned_blobs` tape.
117    let mut pruned_tape_index = append_tx.blob_tape_len(&db.pruned_blobs).unwrap_or(0);
118    for block in blocks {
119        append_tx.append_bytes(&db.pruned_blobs, &block.block_blob)?;
120
121        for tx in &block.txs {
122            append_tx.append_bytes(&db.pruned_blobs, tx.tx_pruned.as_slice())?;
123
124            let prunable_hash = if tx.tx_prunable_blob.is_empty() || tx.tx.version() == 1 {
125                [0; 32]
126            } else {
127                monero_oxide::primitives::keccak256(&tx.tx_prunable_blob)
128            };
129            append_tx.append_bytes(&db.pruned_blobs, &prunable_hash)?;
130        }
131    }
132
133    tracing::trace!("pruned_tape_index: {}", pruned_tape_index);
134
135    // Split the blocks at the point the pruning stripe changes.
136    let start_height = blocks[0].height;
137    let first_block_pruning_seed = PruningSeed::new_pruned(
138        cuprate_pruning::get_block_pruning_stripe(
139            start_height,
140            usize::MAX,
141            CRYPTONOTE_PRUNING_LOG_STRIPES,
142        )
143        .unwrap(),
144        CRYPTONOTE_PRUNING_LOG_STRIPES,
145    )
146    .unwrap();
147    let next_stripe_height = first_block_pruning_seed
148        .get_next_pruned_block(start_height, MAX_BLOCK_HEIGHT_USIZE)
149        .unwrap()
150        .unwrap();
151
152    let (first_stripe, next_stripe) =
153        blocks.split_at(min(next_stripe_height - start_height, blocks.len()));
154
155    assert!(
156        next_stripe.len() <= CRYPTONOTE_PRUNING_STRIPE_SIZE,
157        "blocks cross more than 2 pruning stripes"
158    );
159
160    tracing::debug!(
161        start_height,
162        ?first_block_pruning_seed,
163        next_stripe_height,
164        first_stripe_len = first_stripe.len(),
165        next_stripe_len = next_stripe.len()
166    );
167
168    let mut numb_rct_outs = append_tx
169        .fixed_sized_tape_len(&db.rct_outputs)
170        .expect("Required tape not found");
171
172    // Now iterate over chunks of blocks with the same stripe.
173    for blocks in [first_stripe, next_stripe] {
174        if blocks.is_empty() {
175            continue;
176        }
177
178        // If pruned then move the prunable tip forward.
179        if let PrunableTables::Pruned {
180            prunable_tip,
181            prunable_tip_blobs,
182            stripe: our_stripe,
183            ..
184        } = &db.prunable_tables
185        {
186            // As we split the batch into their stripes above the whole range we are removing here will
187            // also be in the same stripe.
188            let end_removal_height = blocks
189                .last()
190                .unwrap()
191                .height
192                .saturating_sub(CRYPTONOTE_PRUNING_TIP_BLOCKS - 1);
193
194            let end_removal_stripe = cuprate_pruning::get_block_pruning_stripe(
195                end_removal_height,
196                usize::MAX,
197                CRYPTONOTE_PRUNING_LOG_STRIPES,
198            )
199            .unwrap();
200
201            let new_start_tx_idx = append_tx
202                .read_entry(&db.block_infos, usize_to_u64(end_removal_height))?
203                .map_or(0, |b| b.mining_tx_index);
204
205            append_tx.shift_start_idx_fixed(prunable_tip, new_start_tx_idx)?;
206
207            // Only pruned blocks are stored in `prunable_tip_blobs`.
208            if end_removal_stripe != *our_stripe {
209                if let Some(new_start_bytes) =
210                    append_tx.read_entry(prunable_tip, new_start_tx_idx)?
211                {
212                    append_tx.shift_start_idx(prunable_tip_blobs, new_start_bytes)?;
213                }
214            }
215        }
216
217        // Get the stripe and write to the correct v2 prunable tape.
218        let stripe = cuprate_pruning::get_block_pruning_stripe(
219            blocks[0].height,
220            usize::MAX,
221            CRYPTONOTE_PRUNING_LOG_STRIPES,
222        )
223        .unwrap();
224
225        // If we are pruning and this is prunable `v2_prunable_index` is the index into the tip blobs.
226        let mut v2_prunable_index =
227            write_v2_prunable_data(append_tx, stripe, &db.prunable_tables, blocks)?;
228
229        // Write the v1 prunable tape for any v1 txs.
230        let mut v1_prunable_index = write_v1_prunable_data(append_tx, db, blocks)?;
231
232        tracing::debug!(
233            chunk_start = blocks[0].height,
234            stripe,
235            v1_prunable_index,
236            v2_prunable_index,
237            numb_rct_outs
238        );
239
240        // Now loop over every block in the stripe.
241        for block in blocks {
242            let block_pruned_blob_idx = pruned_tape_index;
243            let block_v1_prunable_idx = v1_prunable_index;
244            let block_v2_prunable_idx = v2_prunable_index;
245
246            let header_len = usize_to_u64(block.block.header.serialize().len());
247
248            // Add the miner tx info to the tapes, recording the index of the miner tx.
249            let mining_tx_index = {
250                let tx = block.block.miner_transaction();
251                add_tx_info_to_tapes(
252                    &tx.clone().into(),
253                    pruned_tape_index + header_len, // The miner tx will be stored after the block header.
254                    v2_prunable_index,
255                    tx.serialize().len(),
256                    0,
257                    &block.height,
258                    &mut numb_rct_outs,
259                    append_tx,
260                    db,
261                )?
262            };
263            // Move the pruned tape index forward by the block size (header, miner tx, tx hash list).
264            pruned_tape_index += usize_to_u64(block.block_blob.len());
265
266            // Loop over all the transactions in the block, adding their info to the tapes.
267            for tx in &block.txs {
268                add_tx_info_to_tapes(
269                    &tx.tx,
270                    pruned_tape_index,
271                    // Pruned part of v1 txs will be stored in a different tape.
272                    if tx.tx.version() == 1 {
273                        v1_prunable_index
274                    } else {
275                        v2_prunable_index
276                    },
277                    tx.tx_pruned.len(),
278                    tx.tx_prunable_blob.len(),
279                    &block.height,
280                    &mut numb_rct_outs,
281                    append_tx,
282                    db,
283                )?;
284
285                // Move the blob tapes forward by the transaction size(s). We add 32 for the pruned tape
286                // as we store the prunable hash in the pruned tape.
287                pruned_tape_index += usize_to_u64(tx.tx_pruned.len()) + 32;
288                if tx.tx.version() == 1 {
289                    v1_prunable_index += usize_to_u64(tx.tx_prunable_blob.len());
290                } else {
291                    v2_prunable_index += usize_to_u64(tx.tx_prunable_blob.len());
292                }
293            }
294
295            // `saturating_add` is used here as cumulative generated coins overflows due to tail emission.
296            let cumulative_generated_coins = append_tx
297                .read_entry(
298                    &db.block_infos,
299                    usize_to_u64(block.height.saturating_sub(1)),
300                )?
301                .map_or(0, |prev| prev.cumulative_generated_coins)
302                .saturating_add(block.generated_coins);
303
304            let (cumulative_difficulty_low, cumulative_difficulty_high) =
305                split_u128_into_low_high_bits(block.cumulative_difficulty);
306
307            // Add the block info to the tapes.
308            append_tx.append_entries(
309                &db.block_infos,
310                &[BlockInfo {
311                    cumulative_difficulty_low,
312                    cumulative_difficulty_high,
313                    cumulative_generated_coins,
314                    cumulative_rct_outs: numb_rct_outs,
315                    block_hash: block.block_hash,
316                    weight: block.weight,
317                    long_term_weight: block.long_term_weight,
318                    mining_tx_index,
319                    pruned_blob_idx: block_pruned_blob_idx,
320                    v1_prunable_blob_idx: block_v1_prunable_idx,
321                }],
322            )?;
323
324            tracing::debug!(
325                height = block.height,
326                block_pruned_blob_idx,
327                block_v1_prunable_idx,
328                block_v2_prunable_idx,
329                cumulative_generated_coins,
330                "added block to tapes"
331            );
332        }
333    }
334
335    Ok(())
336}
337
338/// Add a [`VerifiedBlockInformation`] to the dynamic database (fjall).
339///
340/// This extracts all the data from the input block and
341/// maps/adds them to the appropriate database tables.
342///
343/// # Panics
344/// This function will panic if the block is invalid.
345// no inline, too big.
346pub fn add_block_to_dynamic_tables<'a, I: Iterator<Item = Cow<'a, Transaction<Pruned>>>>(
347    db: &BlockchainDatabase,
348    block: &Block,
349    block_hash: &BlockHash,
350    txs: I,
351    numb_transactions: &mut u64,
352    w: &mut fjall::OwnedWriteBatch,
353    pre_rct_numb_outputs_cache: &mut HashMap<Amount, u64>,
354) -> DbResult<()> {
355    // Panic (should never happen) instead of allowing DB corruption.
356    // <https://github.com/Cuprate/cuprate/pull/102#discussion_r1560020991>
357    assert!(
358        u32::try_from(block.number()).is_ok(),
359        "block.height ({}) > u32::MAX",
360        block.number(),
361    );
362
363    // Add the miner transaction first.
364    let tx = block.miner_transaction();
365    add_tx_info_to_dynamic_tables(
366        db,
367        &tx.clone().into(),
368        *numb_transactions,
369        &tx.hash(),
370        &block.number(),
371        w,
372        pre_rct_numb_outputs_cache,
373    )?;
374    *numb_transactions += 1;
375
376    for (tx_hash, tx) in block.transactions.iter().zip(txs) {
377        #[cfg(debug_assertions)]
378        {
379            if db.pruning_seed() == PruningSeed::NotPruned {
380                // Make sure the given tx is correct.
381                let tx_full = get_tx_from_id(numb_transactions, &db.linear_tapes.reader(), db)?;
382
383                let (pruned, _) = tx_full.pruned_with_prunable();
384
385                assert_eq!(tx.as_ref(), &pruned);
386            }
387        }
388
389        add_tx_info_to_dynamic_tables(
390            db,
391            &tx,
392            *numb_transactions,
393            tx_hash,
394            &block.number(),
395            w,
396            pre_rct_numb_outputs_cache,
397        )?;
398        *numb_transactions += 1;
399    }
400
401    w.insert(&db.block_heights, block_hash, block.number().to_le_bytes());
402    w.insert(&db.chain_tip, CHAIN_TIP_KEY, block_hash);
403
404    Ok(())
405}
406
407//---------------------------------------------------------------------------------------------------- `pop_block`
408/// Remove the top/latest block from the database.
409///
410/// The removed block's data is returned along with whether it was added to the alt block tables.
411///
412/// If a [`ChainId`] is specified the popped block will be added to the alt block tables under
413/// that [`ChainId`]. Otherwise, the block will be completely removed from the DB.
414// no inline, too big
415pub fn pop_block(
416    db: &BlockchainDatabase,
417    move_to_alt_chain: Option<ChainId>,
418    tx_rw: &mut fjall::OwnedWriteBatch,
419    tapes: &mut tapes::TapesTruncateTransaction,
420) -> DbResult<(BlockHeight, BlockHash, Block, bool)> {
421    // Pop the last block info.
422    let (block_height, block_info) = tapes
423        .pop_fixed_sized_tape(&db.block_infos)?
424        .ok_or(BlockchainError::NotFound)?;
425
426    let block_height = usize::try_from(block_height).unwrap();
427
428    tx_rw.remove(&db.block_heights, block_info.block_hash);
429
430    let new_top = tapes
431        .read_entry(&db.block_infos, usize_to_u64(block_height - 1))?
432        .ok_or(BlockchainError::NotFound)?;
433    tx_rw.insert(&db.chain_tip, CHAIN_TIP_KEY, new_top.block_hash);
434
435    // Get the block from the database, so we know what txs to remove.
436    let block = get_block(&block_height, Some(&block_info), tapes, db)?;
437    //------------------------------------------------------ Transaction / Outputs / Key Images
438
439    remove_tx_from_dynamic_tables(db, &block.miner_transaction().hash(), tx_rw, tapes)?;
440
441    let mut add_to_alt_chain = move_to_alt_chain.is_some();
442    let mut txs = Vec::with_capacity(block.transactions.len());
443
444    for tx_hash in &block.transactions {
445        let (tx_id, _) = remove_tx_from_dynamic_tables(db, tx_hash, tx_rw, tapes)?;
446
447        if add_to_alt_chain {
448            // TODO: we could just get the missing data here, `remove_tx_from_dynamic_tables` returns the pruned tx
449            match get_tx_from_id(&tx_id, tapes, db) {
450                Ok(tx) => {
451                    let tx_weight = tx.weight();
452                    let tx_hash = tx.hash();
453                    let fee = tx_fee(&tx);
454                    let (tx_pruned, prunable) = tx.pruned_with_prunable();
455
456                    txs.push(VerifiedTransactionInformation {
457                        tx_weight,
458                        tx_pruned: tx_pruned.serialize(),
459                        tx_prunable_blob: prunable,
460                        tx_hash,
461                        fee,
462                        tx: tx_pruned,
463                    });
464                }
465                Err(BlockchainError::NotFound) => {
466                    add_to_alt_chain = false;
467                    txs.clear();
468                }
469                Err(e) => return Err(e),
470            }
471        }
472    }
473
474    if let Some(chain_id) = move_to_alt_chain.filter(|_| add_to_alt_chain) {
475        alt_block::add_alt_block(
476            db,
477            &AltBlockInformation {
478                block: block.clone(),
479                block_blob: block.serialize(),
480                txs,
481                block_hash: block_info.block_hash,
482                // We know the PoW is valid for this block so just set it so it will always verify as valid.
483                pow_hash: [0; 32],
484                height: block_height,
485                weight: block_info.weight,
486                long_term_weight: block_info.long_term_weight,
487                cumulative_difficulty: combine_low_high_bits_to_u128(
488                    block_info.cumulative_difficulty_low,
489                    block_info.cumulative_difficulty_high,
490                ),
491                chain_id,
492            },
493            tx_rw,
494        )?;
495    }
496
497    // Truncate the tapes.
498    tapes.truncate_blob_tape(&db.pruned_blobs, block_info.pruned_blob_idx)?;
499    tapes.truncate_blob_tape(&db.v1_prunable_blobs, block_info.v1_prunable_blob_idx)?;
500    let stripe = cuprate_pruning::get_block_pruning_stripe(
501        block_height,
502        usize::MAX,
503        CRYPTONOTE_PRUNING_LOG_STRIPES,
504    )
505    .unwrap();
506
507    match &db.prunable_tables {
508        PrunableTables::Full(prunable_blobs) => {
509            let prunable_blob_idx_end = tapes
510                .read_entry(&db.tx_infos, block_info.mining_tx_index)?
511                .unwrap()
512                .prunable_blob_idx;
513
514            tapes
515                .truncate_blob_tape(&prunable_blobs[stripe as usize - 1], prunable_blob_idx_end)?;
516        }
517        PrunableTables::Pruned {
518            prunable_tip,
519            kept_stripe,
520            stripe: our_stripe,
521            ..
522        } if *our_stripe == stripe => {
523            let prunable_blob_idx_end = tapes
524                .read_entry(&db.tx_infos, block_info.mining_tx_index)?
525                .unwrap()
526                .prunable_blob_idx;
527
528            tapes.truncate_blob_tape(kept_stripe, prunable_blob_idx_end)?;
529            tapes.truncate_fixed_sized_tape(prunable_tip, block_info.mining_tx_index)?;
530        }
531        PrunableTables::Pruned {
532            prunable_tip,
533            prunable_tip_blobs,
534            ..
535        } => {
536            let prunable_blob_idx_end =
537                match tapes.read_entry(prunable_tip, block_info.mining_tx_index)? {
538                    Some(index) => index,
539                    // The offset is gone below the rolling tip; empty the blob tape.
540                    None => tapes
541                        .blob_tape_start(prunable_tip_blobs)
542                        .expect("required tape not open"),
543                };
544
545            tapes.truncate_blob_tape(prunable_tip_blobs, prunable_blob_idx_end)?;
546            tapes.truncate_fixed_sized_tape(prunable_tip, block_info.mining_tx_index)?;
547        }
548    }
549
550    tapes.truncate_fixed_sized_tape(&db.tx_infos, block_info.mining_tx_index)?;
551
552    let cumulative_rct_outs = tapes
553        .read_entry(&db.block_infos, usize_to_u64(block_height) - 1)?
554        .map_or(0, |info| info.cumulative_rct_outs);
555
556    tapes.truncate_fixed_sized_tape(&db.rct_outputs, cumulative_rct_outs)?;
557
558    Ok((block_height, block_info.block_hash, block, add_to_alt_chain))
559}
560
561//---------------------------------------------------------------------------------------------------- `get_block_complete_entry_*`
562/// Retrieve a [`BlockCompleteEntry`] from the database.
563///
564pub fn get_block_complete_entry(
565    db: &BlockchainDatabase,
566    block_hash: &BlockHash,
567    prune: bool,
568    tx_ro: &fjall::Snapshot,
569    tapes: &tapes::TapesReadTransaction,
570) -> DbResult<BlockCompleteEntry> {
571    let block_height = tx_ro
572        .get(&db.block_heights, block_hash)?
573        .ok_or(BlockchainError::NotFound)?;
574
575    get_block_complete_entry_from_height(
576        usize::from_le_bytes(block_height.as_ref().try_into().unwrap()),
577        prune,
578        tapes,
579        db,
580    )
581}
582
583/// Retrieve a [`BlockCompleteEntry`] from the database.
584///
585pub fn get_block_complete_entry_from_height(
586    block_height: BlockHeight,
587    prune: bool,
588    tapes: &tapes::TapesReadTransaction,
589    db: &BlockchainDatabase,
590) -> DbResult<BlockCompleteEntry> {
591    /// A helper function to read a span of bytes from a tape into an owned buffer.
592    fn read_blob(
593        tapes: &tapes::TapesReadTransaction,
594        tape: &impl BlobTape,
595        start: u64,
596        len: usize,
597    ) -> DbResult<Bytes> {
598        if len == 0 {
599            return Ok(Bytes::new());
600        }
601
602        let mut buf = vec![0; len];
603        tapes.read_bytes(tape, start, &mut buf)?;
604        Ok(Bytes::from(buf))
605    }
606
607    let block_info = tapes
608        .read_entry(&db.block_infos, usize_to_u64(block_height))?
609        .ok_or(BlockchainError::NotFound)?;
610
611    // return early if we don't have this block (we pruned it)
612    let chain_height = chain_height(db, tapes)?;
613    if !prune && !db.pruning_seed().has_full_block(block_height, chain_height) {
614        return Err(BlockchainError::NotFound);
615    }
616
617    let block_blob_start_idx = block_info.pruned_blob_idx;
618    let mut block_blob_end_idx = None;
619
620    let mut txs = Vec::with_capacity(32);
621
622    for tx_info in tapes.iter_from(&db.tx_infos, block_info.mining_tx_index + 1)? {
623        let tx_info = tx_info?;
624
625        if tx_info.height != block_height {
626            break;
627        }
628
629        // Set the `block_blob_end_idx` to the first tx in the block.
630        block_blob_end_idx.get_or_insert(tx_info.pruned_blob_idx);
631
632        txs.push(tx_info);
633    }
634
635    let txs = if txs.is_empty() {
636        TransactionBlobs::None
637    } else {
638        // We do just one big read of the pruned tape to get all txs blobs as they are contiguous.
639        let first_blob_idx = txs.first().unwrap().pruned_blob_idx;
640        let mut bytes = read_blob(
641            tapes,
642            &db.pruned_blobs,
643            first_blob_idx,
644            u64_to_usize(txs.last().unwrap().pruned_blob_idx - first_blob_idx)
645                + txs.last().unwrap().pruned_size
646                + 32,
647        )?;
648
649        let mut v2_prunable_blob_idx = None;
650        let (mut v1_len, mut v2_len) = (0, 0);
651        for t in &txs {
652            if t.is_v1_tx() {
653                v1_len += t.prunable_size;
654            } else {
655                v2_prunable_blob_idx.get_or_insert(t.prunable_blob_idx);
656                v2_len += t.prunable_size;
657            }
658        }
659        let v2_prunable_blob_idx = v2_prunable_blob_idx.unwrap_or(0);
660
661        let mut v1 = read_blob(
662            tapes,
663            &db.v1_prunable_blobs,
664            block_info.v1_prunable_blob_idx,
665            v1_len,
666        )?;
667
668        if prune {
669            // v1 txs can't be pruned, so they are sent whole with the null prunable hash they are stored with,
670            // which tells the peer the blob is a full tx, as in `monerod`:
671            // <https://github.com/monero-project/monero/blob/cc73fe71162d564ffda8e549b79a350bca53c454/src/cryptonote_core/blockchain.cpp#L2547-L2563>
672            TransactionBlobs::Pruned(
673                txs.iter()
674                    .map(|tx_info| PrunedTxBlobEntry {
675                        blob: {
676                            let pruned = bytes.split_to(tx_info.pruned_size);
677                            if tx_info.is_v1_tx() {
678                                let prunable = v1.split_to(tx_info.prunable_size);
679                                Bytes::from([pruned.as_ref(), prunable.as_ref()].concat())
680                            } else {
681                                pruned
682                            }
683                        },
684                        prunable_hash: bytes.split_to(32).try_into().unwrap(),
685                    })
686                    .collect(),
687            )
688        } else {
689            let pruning_stripe = cuprate_pruning::get_block_pruning_stripe(
690                block_height,
691                usize::MAX,
692                CRYPTONOTE_PRUNING_LOG_STRIPES,
693            )
694            .unwrap();
695
696            let mut v2 = match &db.prunable_tables {
697                PrunableTables::Full(prunable_blobs) => read_blob(
698                    tapes,
699                    &prunable_blobs[usize::try_from(pruning_stripe)
700                        .expect("stripe will not exceed usize::MAX")
701                        - 1],
702                    v2_prunable_blob_idx,
703                    v2_len,
704                )?,
705                PrunableTables::Pruned {
706                    kept_stripe,
707                    stripe,
708                    ..
709                } if *stripe == pruning_stripe => {
710                    read_blob(tapes, kept_stripe, v2_prunable_blob_idx, v2_len)?
711                }
712                PrunableTables::Pruned {
713                    prunable_tip,
714                    prunable_tip_blobs,
715                    ..
716                } => {
717                    let prunable_blob_idx =
718                        tapes.read_entry(prunable_tip, block_info.mining_tx_index)?;
719
720                    let Some(prunable_blob_idx) = prunable_blob_idx else {
721                        return Err(BlockchainError::NotFound);
722                    };
723
724                    read_blob(tapes, prunable_tip_blobs, prunable_blob_idx, v2_len)?
725                }
726            };
727
728            TransactionBlobs::Normal(
729                txs.iter()
730                    .map(|tx_info| {
731                        let pruned = bytes.split_to(tx_info.pruned_size);
732                        bytes.advance(32); // skip the interleaved prunable hash
733                        let prunable = if tx_info.is_v1_tx() {
734                            v1.split_to(tx_info.prunable_size)
735                        } else {
736                            v2.split_to(tx_info.prunable_size)
737                        };
738                        Bytes::from([pruned.as_ref(), prunable.as_ref()].concat())
739                    })
740                    .collect(),
741            )
742        }
743    };
744
745    let block_blob = {
746        let block_blob_end_idx = block_blob_end_idx.map_or_else(
747            || {
748                // If the `block_blob_end_idx` has not been set then there were no txs in the block.
749                // Get the next block's start index or the end of the pruned tape for the end index.
750                let next_block_info =
751                    tapes.read_entry(&db.block_infos, usize_to_u64(block_height + 1))?;
752
753                if let Some(info) = next_block_info {
754                    return Ok::<_, BlockchainError>(info.pruned_blob_idx);
755                }
756
757                Ok(tapes
758                    .blob_tape_len(&db.pruned_blobs)
759                    .expect("Required tape not found"))
760            },
761            Ok,
762        )?;
763
764        read_blob(
765            tapes,
766            &db.pruned_blobs,
767            block_blob_start_idx,
768            u64_to_usize(block_blob_end_idx - block_blob_start_idx),
769        )?
770    };
771
772    Ok(BlockCompleteEntry {
773        block: block_blob,
774        txs,
775        pruned: prune,
776        block_weight: if prune {
777            usize_to_u64(block_info.weight)
778        } else {
779            0
780        },
781    })
782}
783
784//---------------------------------------------------------------------------------------------------- `get_block_extended_header_*`
785/// Retrieve a [`ExtendedBlockHeader`] from the database.
786///
787/// This extracts all the data from the database tables
788/// needed to create a full `ExtendedBlockHeader`.
789///
790/// # Notes
791/// This is more expensive than [`get_block_extended_header_from_height`]
792#[inline]
793pub fn get_block_extended_header(
794    db: &BlockchainDatabase,
795    block_hash: &BlockHash,
796    tx_ro: &fjall::Snapshot,
797    tapes: &tapes::TapesReadTransaction,
798) -> DbResult<ExtendedBlockHeader> {
799    let block_height = tx_ro
800        .get(&db.block_heights, block_hash)?
801        .ok_or(BlockchainError::NotFound)?;
802
803    get_block_extended_header_from_height(
804        usize::from_le_bytes(block_height.as_ref().try_into().unwrap()),
805        tapes,
806        db,
807    )
808}
809
810/// Same as [`get_block_extended_header`] but with a [`BlockHeight`].
811#[expect(
812    clippy::missing_panics_doc,
813    reason = "The panic is only possible with a corrupt DB"
814)]
815#[inline]
816pub fn get_block_extended_header_from_height(
817    block_height: BlockHeight,
818    tapes: &tapes::TapesReadTransaction,
819    db: &BlockchainDatabase,
820) -> DbResult<ExtendedBlockHeader> {
821    let block_info = tapes
822        .read_entry(&db.block_infos, usize_to_u64(block_height))?
823        .ok_or(BlockchainError::NotFound)?;
824    let miner_tx_info = tapes
825        .read_entry(&db.tx_infos, block_info.mining_tx_index)?
826        .ok_or(BlockchainError::NotFound)?;
827
828    let mut block_header_blob =
829        vec![0; u64_to_usize(miner_tx_info.pruned_blob_idx - block_info.pruned_blob_idx)];
830
831    tapes.read_bytes(
832        &db.pruned_blobs,
833        block_info.pruned_blob_idx,
834        &mut block_header_blob,
835    )?;
836
837    let block_header = BlockHeader::read(&mut block_header_blob.as_slice()).unwrap();
838
839    let cumulative_difficulty = combine_low_high_bits_to_u128(
840        block_info.cumulative_difficulty_low,
841        block_info.cumulative_difficulty_high,
842    );
843
844    Ok(ExtendedBlockHeader {
845        cumulative_difficulty,
846        version: HardFork::from_version(block_header.hardfork_version)
847            .expect("Stored block must have a valid hard-fork"),
848        vote: block_header.hardfork_signal,
849        timestamp: block_header.timestamp,
850        block_weight: block_info.weight,
851        long_term_weight: block_info.long_term_weight,
852    })
853}
854
855/// Return the top/latest [`ExtendedBlockHeader`] from the database.
856#[inline]
857pub fn get_block_extended_header_top(
858    db: &BlockchainDatabase,
859    tapes: &tapes::TapesReadTransaction,
860) -> DbResult<(ExtendedBlockHeader, BlockHeight)> {
861    let height = u64_to_usize(
862        tapes
863            .fixed_sized_tape_len(&db.block_infos)
864            .expect("Required tape not found")
865            .saturating_sub(1),
866    );
867    let header = get_block_extended_header_from_height(height, tapes, db)?;
868    Ok((header, height))
869}
870
871//---------------------------------------------------------------------------------------------------- Block
872/// Retrieve a [`Block`] via its [`BlockHeight`].
873///
874/// If the block info is given, it will not be looked up, which can be useful when the info is popped.
875#[inline]
876pub fn get_block(
877    block_height: &BlockHeight,
878    blocks_info: Option<&BlockInfo>,
879    tapes: &impl TapesRead,
880    db: &BlockchainDatabase,
881) -> DbResult<Block> {
882    let block_info = match blocks_info {
883        Some(blocks_info) => *blocks_info,
884        None => tapes
885            .read_entry(&db.block_infos, usize_to_u64(*block_height))?
886            .ok_or(BlockchainError::NotFound)?,
887    };
888
889    let pruned_end_blob_idx =
890        match tapes.read_entry(&db.tx_infos, block_info.mining_tx_index + 1)? {
891            // First check if the block has a tx then use that start as the block header end.
892            Some(tx_info) if tx_info.height == *block_height => tx_info.pruned_blob_idx,
893            // Otherwise the next tx is in a different block, so use the start of that block header.
894            Some(_) => {
895                tapes
896                    .read_entry(&db.block_infos, usize_to_u64(*block_height + 1))?
897                    .ok_or(BlockchainError::NotFound)?
898                    .pruned_blob_idx
899            }
900            // If this is the top block, and it doesn't have a tx, then use the end of the pruned tape.
901            None => tapes
902                .blob_tape_len(&db.pruned_blobs)
903                .expect("Required tape not found"),
904        };
905
906    let mut blob =
907        vec![0; usize::try_from(pruned_end_blob_idx - block_info.pruned_blob_idx).unwrap()];
908
909    tapes.read_bytes(&db.pruned_blobs, block_info.pruned_blob_idx, &mut blob)?;
910
911    Ok(Block::read(&mut blob.as_slice())?)
912}
913
914/// Retrieve a [`Block`] via its [`BlockHash`].
915#[inline]
916pub fn get_block_by_hash(
917    db: &BlockchainDatabase,
918    block_hash: &BlockHash,
919    tx_ro: &fjall::Snapshot,
920    tapes: &tapes::TapesReadTransaction,
921) -> DbResult<Block> {
922    let block_height = tx_ro
923        .get(&db.block_heights, block_hash)?
924        .ok_or(BlockchainError::NotFound)?;
925
926    get_block(
927        &usize::from_le_bytes(block_height.as_ref().try_into().unwrap()),
928        None,
929        tapes,
930        db,
931    )
932}
933
934/// Retrieve a [`BlockHeight`] via its [`BlockHash`].
935#[inline]
936pub fn get_block_height(
937    db: &BlockchainDatabase,
938    block_hash: &BlockHash,
939    tx_ro: &fjall::Snapshot,
940) -> DbResult<BlockHeight> {
941    let block_height = tx_ro
942        .get(&db.block_heights, block_hash)?
943        .ok_or(BlockchainError::NotFound)?;
944
945    Ok(usize::from_le_bytes(
946        block_height.as_ref().try_into().unwrap(),
947    ))
948}
949
950/// Check if a block exists in the database.
951///
952#[inline]
953pub fn block_exists(
954    db: &BlockchainDatabase,
955    block_hash: &BlockHash,
956    tx_ro: &fjall::Snapshot,
957) -> DbResult<bool> {
958    Ok(tx_ro.contains_key(&db.block_heights, block_hash)?)
959}
960
961/// Returns the height of a block from its hash, only if it is in the main chain.
962///
963pub(crate) fn block_height(
964    db: &BlockchainDatabase,
965    tx_ro: &fjall::Snapshot,
966    hash: &BlockHash,
967) -> DbResult<Option<usize>> {
968    let Some(block_height) = tx_ro.get(&db.block_heights, hash)? else {
969        return Ok(None);
970    };
971
972    Ok(Some(usize::from_le_bytes(
973        block_height.as_ref().try_into().unwrap(),
974    )))
975}