Skip to main content

cuprate_blockchain/service/
read.rs

1//! Database reader thread-pool definitions and logic.
2
3//---------------------------------------------------------------------------------------------------- Import
4use std::{
5    cmp::min,
6    collections::{BTreeMap, HashMap, HashSet},
7    ops::Range,
8    sync::Arc,
9    task::{Context, Poll},
10};
11
12use bytes::Bytes;
13use fjall::Readable;
14use futures::channel::oneshot;
15use indexmap::{IndexMap, IndexSet};
16use rayon::{
17    iter::{Either, IntoParallelIterator, ParallelIterator},
18    prelude::*,
19    ThreadPool,
20};
21use tapes::TapesRead;
22use tower::Service;
23
24use cuprate_helper::{
25    asynch::InfallibleOneshotReceiver,
26    cast::{u64_to_usize, usize_to_u64},
27    map::{combine_low_high_bits_to_u128, split_u128_into_low_high_bits},
28};
29use cuprate_types::{
30    blockchain::{BlockchainReadRequest, BlockchainResponse},
31    output_cache::OutputCache,
32    rpc::{
33        ChainInfo, CoinbaseTxSum, OutputDistributionData, OutputHistogramEntry,
34        OutputHistogramInput,
35    },
36    Chain, ChainId, ExtendedBlockHeader, PreRctOutputDistributionInput, TransactionBlobs,
37    TxInBlockchain, TxsInBlock,
38};
39
40use crate::{
41    error::{BlockchainError, DbResult},
42    ops::{
43        alt_block::{
44            alt_block_height, get_alt_block, get_alt_block_extended_header_from_height,
45            get_alt_block_hash, get_alt_block_information, get_alt_chain_history_ranges,
46        },
47        block::{
48            block_exists, block_height, get_block, get_block_by_hash, get_block_complete_entry,
49            get_block_complete_entry_from_height, get_block_extended_header_from_height,
50        },
51        blockchain::find_split_point,
52        output::{
53            get_num_outputs_with_amount, id_to_output_on_chain, unlocked_and_recent_instances,
54        },
55        tx::{get_split_tx_blobs, get_tx_blob_from_id},
56    },
57    service::{
58        free::{compact_history_genesis_not_included, compact_history_index_to_height_offset},
59        ResponseResult,
60    },
61    types::{
62        AltBlockHeight, AltChainInfo, Amount, AmountIndex, BlockHash, BlockHeight,
63        CompactAltBlockInfo, KeyImage, Output, PreRctOutputId, RawChainId,
64    },
65    BlockchainDatabase,
66};
67
68/// The [`tower::Service`] handle to read from the database.
69#[derive(Clone)]
70pub struct BlockchainReadHandle {
71    /// Handle to the custom `rayon` DB reader thread-pool.
72    ///
73    /// Requests are [`rayon::ThreadPool::spawn`]ed in this thread-pool,
74    /// and responses are returned via a channel we (the caller) provide.
75    pub pool: Arc<ThreadPool>,
76
77    pub blockchain: Arc<BlockchainDatabase>,
78}
79
80impl Service<BlockchainReadRequest> for BlockchainReadHandle {
81    type Response = BlockchainResponse;
82    type Error = BlockchainError;
83    type Future = InfallibleOneshotReceiver<Result<Self::Response, Self::Error>>;
84
85    fn poll_ready(&mut self, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
86        Poll::Ready(Ok(()))
87    }
88
89    fn call(&mut self, req: BlockchainReadRequest) -> Self::Future {
90        let (tx, rx) = oneshot::channel();
91
92        let db = Arc::clone(&self.blockchain);
93        self.pool.spawn(move || {
94            let res = map_request(&db, req);
95
96            let _ = tx.send(res);
97        });
98
99        InfallibleOneshotReceiver::from(rx)
100    }
101}
102
103//---------------------------------------------------------------------------------------------------- Request Mapping
104// This function maps [`Request`]s to function calls
105// executed by the rayon DB reader threadpool.
106
107/// Map [`Request`]'s to specific database handler functions.
108///
109/// This is the main entrance into all `Request` handler functions.
110/// The basic structure is:
111/// 1. `Request` is mapped to a handler function
112/// 2. Handler function is called
113/// 3. [`BlockchainResponse`] is returned
114fn map_request(
115    env: &BlockchainDatabase,       // Access to the database
116    request: BlockchainReadRequest, // The request we must fulfill
117) -> Result<BlockchainResponse, BlockchainError> {
118    use BlockchainReadRequest as R;
119
120    /* SOMEDAY: pre-request handling, run some code for each request? */
121
122    match request {
123        R::BlockCompleteEntries {
124            block_hashes,
125            prune,
126        } => block_complete_entries(env, block_hashes, prune),
127        R::BlockCompleteEntriesByHeight(heights) => block_complete_entries_by_height(env, heights),
128        R::BlockCompleteEntriesAboveSplitPoint {
129            chain,
130            start_height,
131            no_miner_tx,
132            len,
133            prune,
134        } => block_complete_entries_above_split_point(
135            env,
136            &chain,
137            start_height,
138            no_miner_tx,
139            len,
140            prune,
141        ),
142        R::BlockExtendedHeader(block) => block_extended_header(env, block),
143        R::BlockHash(block, chain) => block_hash(env, block, chain),
144        R::BlockHashInRange(blocks, chain) => block_hash_in_range(env, blocks, chain),
145        R::FindBlock(block_hash) => find_block(env, block_hash),
146        R::FilterUnknownHashes(hashes) => filter_unknown_hashes(env, hashes),
147        R::BlockExtendedHeaderInRange(range, chain) => {
148            block_extended_header_in_range(env, range, chain)
149        }
150        R::ChainHeight => chain_height(env),
151        R::GeneratedCoins(height) => generated_coins(env, height),
152        R::CumulativeRctOutsInRange(range) => cumulative_rct_outs_in_range(env, range),
153        R::Outputs {
154            outputs: map,
155            get_txid,
156        } => outputs(env, map, get_txid),
157        R::OutputsVec { outputs, get_txid } => outputs_vec(env, outputs, get_txid),
158        R::NumberOutputsWithAmount(vec) => number_outputs_with_amount(env, vec),
159        R::KeyImagesSpent(set) => key_images_spent(env, set),
160        R::KeyImagesSpentVec(set) => key_images_spent_vec(env, set),
161        R::CompactChainHistory => compact_chain_history(env),
162        R::NextChainEntry(block_hashes, amount) => next_chain_entry(env, &block_hashes, amount),
163        R::FindFirstUnknown(block_ids) => find_first_unknown(env, &block_ids),
164        R::TxsInBlock {
165            block_hash,
166            tx_indexes,
167        } => txs_in_block(env, block_hash, tx_indexes),
168        R::AltBlocksInChain(chain_id) => alt_blocks_in_chain(env, chain_id),
169        R::Block { height } => block(env, height),
170        R::BlockByHash(hash) => block_by_hash(env, hash),
171        R::TotalTxCount => Ok(total_tx_count(env)),
172        R::DatabaseSize => Ok(database_size(env)),
173        R::OutputHistogram(input) => output_histogram(env, &input),
174        R::CoinbaseTxSum { height, count } => coinbase_tx_sum(env, height, count),
175        R::AltChains => alt_chains(env),
176        R::AltChainCount => alt_chain_count(env),
177        R::Transactions { tx_hashes } => transactions(env, tx_hashes),
178        R::TotalRctOutputs => Ok(total_rct_outputs(env)),
179        R::TxOutputIndexes { tx_hash } => tx_output_indexes(env, &tx_hash),
180        R::PreRctOutputDistribution(input) => pre_rct_output_distribution(env, &input),
181    }
182
183    /* SOMEDAY: post-request handling, run some code for each request? */
184}
185
186//---------------------------------------------------------------------------------------------------- Handler functions
187// These are the actual functions that do stuff according to the incoming [`Request`].
188//
189// Each function name is a 1-1 mapping (from CamelCase -> snake_case) to
190// the enum variant name, e.g: `BlockExtendedHeader` -> `block_extended_header`.
191//
192// Each function will return the [`Response`] that we
193// should send back to the caller in [`map_request()`].
194//
195// INVARIANT:
196// These functions are called above in `tower::Service::call()`
197// using a custom threadpool which means any call to `par_*()` functions
198// will be using the custom rayon DB reader thread-pool, not the global one.
199//
200// All functions below assume that this is the case, such that
201// `par_*()` functions will not block the _global_ rayon thread-pool.
202
203// TODO: The overhead of parallelism may be too much for every request, perfomace test to find optimal
204// amount of parallelism.
205
206/// [`BlockchainReadRequest::BlockCompleteEntries`].
207fn block_complete_entries(
208    db: &BlockchainDatabase,
209    block_hashes: Vec<BlockHash>,
210    prune: bool,
211) -> ResponseResult {
212    let (tx_ro, tapes) = db.read_transactions()?;
213
214    let (missing_hashes, blocks) = block_hashes
215        .into_par_iter()
216        .map(
217            |block_hash| match get_block_complete_entry(db, &block_hash, prune, &tx_ro, &tapes) {
218                Err(BlockchainError::NotFound) => Ok(Either::Left(block_hash)),
219                res => res.map(Either::Right),
220            },
221        )
222        .collect::<DbResult<_>>()?;
223
224    let blockchain_height = crate::ops::blockchain::chain_height(db, &tapes)?;
225
226    Ok(BlockchainResponse::BlockCompleteEntries {
227        blocks,
228        missing_hashes,
229        blockchain_height,
230    })
231}
232
233/// [`BlockchainReadRequest::BlockCompleteEntriesAboveSplitPoint`].
234fn block_complete_entries_above_split_point(
235    db: &BlockchainDatabase,
236    chain: &[[u8; 32]],
237    start_height: Option<usize>,
238    no_miner_tx: bool,
239    len: usize,
240    prune: bool,
241) -> ResponseResult {
242    /// Total size of all block/tx blobs to return before stopping early.
243    ///
244    /// This is lower than monerod, as monerod packs too close to the epee size limit.
245    const MAX_TOTAL_SIZE: usize = 50 * 1024 * 1024;
246    /// Total tx count to return before stopping early.
247    ///
248    /// This is lower than monerod, as monerod packs too close to the epee size limit.
249    const MAX_TOTAL_TXS: usize = 10_000;
250
251    let (tx_ro, tapes) = db.read_transactions()?;
252
253    let blockchain_height = crate::ops::blockchain::chain_height(db, &tapes)?;
254    let top_hash = tapes
255        .read_entry(&db.block_infos, usize_to_u64(blockchain_height - 1))?
256        .ok_or(BlockchainError::NotFound)?
257        .block_hash;
258
259    // If a specific start height was requested, use it directly. Otherwise, scan to find the split point.
260    let height = if let Some(h) = start_height {
261        if h >= blockchain_height {
262            return Ok(BlockchainResponse::BlockCompleteEntriesAboveSplitPoint {
263                blocks: vec![],
264                output_indices: vec![],
265                blockchain_height,
266                start_height: h,
267                top_hash,
268            });
269        }
270        h
271    } else {
272        let split = find_split_point(db, chain, false, false, &tx_ro)?;
273
274        if split == chain.len() {
275            return Err(BlockchainError::NotFound);
276        }
277
278        block_height(db, &tx_ro, &chain[split])?.ok_or(BlockchainError::NotFound)?
279    };
280
281    if height == blockchain_height {
282        return Ok(BlockchainResponse::BlockCompleteEntriesAboveSplitPoint {
283            blocks: vec![],
284            output_indices: vec![],
285            blockchain_height,
286            start_height: height,
287            top_hash,
288        });
289    }
290
291    let mut tx_count = 0;
292    let mut total_size = 0;
293
294    let blocks: Vec<_> = (height..min(height + len, blockchain_height))
295        .map_while(|height| {
296            if total_size >= MAX_TOTAL_SIZE || tx_count >= MAX_TOTAL_TXS {
297                return None;
298            }
299
300            let block = match get_block_complete_entry_from_height(height, prune, &tapes, db) {
301                Ok(v) => v,
302                Err(e) => return Some(Err(e)),
303            };
304
305            tx_count += block.txs.len() + 1;
306
307            let tx_blobs_size = match &block.txs {
308                TransactionBlobs::None => 0,
309                TransactionBlobs::Normal(b) => b.iter().map(Bytes::len).sum(),
310                TransactionBlobs::Pruned(p) => p.iter().map(|p| p.blob.len() + 32).sum(),
311            };
312
313            total_size += block.block.len() + tx_blobs_size;
314
315            Some(Ok(block))
316        })
317        .collect::<DbResult<_>>()?;
318
319    let first_tx_idx = tapes
320        .read_entry(&db.block_infos, usize_to_u64(height))?
321        .ok_or(BlockchainError::NotFound)?
322        .mining_tx_index;
323
324    let mut output_indices = Vec::with_capacity(blocks.len());
325    output_indices.push(Vec::with_capacity(8));
326
327    let mut last_height = height;
328    let mut miner_tx = true;
329
330    for (i, tx_info) in tapes.iter_from(&db.tx_infos, first_tx_idx)?.enumerate() {
331        let tx_info = tx_info?;
332
333        if tx_info.height != last_height {
334            if tx_info.height == height + blocks.len() {
335                // We have gone past all txs in the blocks we need
336                break;
337            }
338            last_height = tx_info.height;
339            miner_tx = true;
340            output_indices.push(Vec::with_capacity(8));
341        }
342
343        // monerod replaces the miner tx's indices with an empty
344        // placeholder when `no_miner_tx` is set.
345        if no_miner_tx && miner_tx {
346            miner_tx = false;
347            output_indices.last_mut().unwrap().push(vec![]);
348            continue;
349        }
350        miner_tx = false;
351
352        let o_indexes = if tx_info.is_v1_tx() {
353            // For v1 txs we need to look up indexes.
354            let res = tx_ro
355                .get(
356                    &db.v1_tx_outputs,
357                    (first_tx_idx + usize_to_u64(i)).to_le_bytes(),
358                )?
359                .ok_or(BlockchainError::NotFound)?;
360
361            res.chunks(8)
362                .map(|chunk| u64::from_le_bytes(chunk.try_into().unwrap()))
363                .collect::<Vec<_>>()
364        } else {
365            // For v2 we can use the data in the tx_info.
366            (0..tx_info.numb_rct_outputs)
367                .map(|i| usize_to_u64(i) + tx_info.rct_output_start_idx)
368                .collect()
369        };
370
371        output_indices.last_mut().unwrap().push(o_indexes);
372    }
373
374    Ok(BlockchainResponse::BlockCompleteEntriesAboveSplitPoint {
375        blocks,
376        output_indices,
377        blockchain_height,
378        start_height: height,
379        top_hash,
380    })
381}
382
383/// [`BlockchainReadRequest::BlockCompleteEntriesByHeight`].
384fn block_complete_entries_by_height(
385    db: &BlockchainDatabase,
386    block_heights: Vec<BlockHeight>,
387) -> ResponseResult {
388    let tapes = db.linear_tapes.reader();
389
390    let blocks = block_heights
391        .into_par_iter()
392        .map(|height| get_block_complete_entry_from_height(height, false, &tapes, db))
393        .collect::<DbResult<_>>()?;
394
395    Ok(BlockchainResponse::BlockCompleteEntriesByHeight(blocks))
396}
397
398/// [`BlockchainReadRequest::BlockExtendedHeader`].
399#[inline]
400fn block_extended_header(db: &BlockchainDatabase, block_height: BlockHeight) -> ResponseResult {
401    let tapes = db.linear_tapes.reader();
402
403    Ok(BlockchainResponse::BlockExtendedHeader(
404        get_block_extended_header_from_height(block_height, &tapes, db)?,
405    ))
406}
407
408/// [`BlockchainReadRequest::BlockHash`].
409#[inline]
410fn block_hash(db: &BlockchainDatabase, block_height: BlockHeight, chain: Chain) -> ResponseResult {
411    let (tx_ro, tapes) = db.read_transactions()?;
412
413    let block_hash = match chain {
414        Chain::Main => {
415            tapes
416                .read_entry(&db.block_infos, usize_to_u64(block_height))?
417                .ok_or(BlockchainError::NotFound)?
418                .block_hash
419        }
420        Chain::Alt(chain) => get_alt_block_hash(db, &block_height, chain, &tx_ro, &tapes)?,
421    };
422
423    Ok(BlockchainResponse::BlockHash(block_hash))
424}
425
426/// [`BlockchainReadRequest::BlockHashInRange`].
427#[inline]
428fn block_hash_in_range(
429    db: &BlockchainDatabase,
430    range: Range<usize>,
431    chain: Chain,
432) -> ResponseResult {
433    let (tx_ro, tapes) = db.read_transactions()?;
434
435    if range.is_empty() {
436        return Ok(BlockchainResponse::BlockHashInRange(vec![]));
437    }
438
439    let block_hash = match chain {
440        Chain::Main => tapes
441            .iter_from(&db.block_infos, usize_to_u64(range.start))?
442            .map(|info| Ok(info?.block_hash))
443            .take(range.len())
444            .collect::<Result<_, BlockchainError>>()?,
445        Chain::Alt(chain) => range
446            .into_par_iter()
447            .map(|block_height| get_alt_block_hash(db, &block_height, chain, &tx_ro, &tapes))
448            .collect::<DbResult<Vec<_>>>()?,
449    };
450
451    Ok(BlockchainResponse::BlockHashInRange(block_hash))
452}
453
454/// [`BlockchainReadRequest::FindBlock`]
455fn find_block(db: &BlockchainDatabase, block_hash: BlockHash) -> ResponseResult {
456    let tx_ro = db.fjall.snapshot();
457
458    // Check the main chain first, then alt chains.
459    let location = if let Some(height) = block_height(db, &tx_ro, &block_hash)? {
460        Some((Chain::Main, height))
461    } else {
462        alt_block_height(db, &tx_ro, &block_hash)?
463            .map(|alt| (Chain::Alt(alt.chain_id.into()), alt.height))
464    };
465
466    Ok(BlockchainResponse::FindBlock(location))
467}
468
469/// [`BlockchainReadRequest::FilterUnknownHashes`].
470#[inline]
471fn filter_unknown_hashes(
472    db: &BlockchainDatabase,
473    mut hashes: HashSet<BlockHash>,
474) -> ResponseResult {
475    let tx_ro = db.fjall.snapshot();
476
477    let mut err = None;
478
479    hashes.retain(|block_hash| match block_exists(db, block_hash, &tx_ro) {
480        Ok(exists) => exists,
481        Err(e) => {
482            err.get_or_insert(e);
483            false
484        }
485    });
486
487    if let Some(e) = err {
488        Err(e)
489    } else {
490        Ok(BlockchainResponse::FilterUnknownHashes(hashes))
491    }
492}
493
494/// [`BlockchainReadRequest::BlockExtendedHeaderInRange`].
495#[inline]
496fn block_extended_header_in_range(
497    db: &BlockchainDatabase,
498    range: Range<BlockHeight>,
499    chain: Chain,
500) -> ResponseResult {
501    let (tx_ro, tapes) = db.read_transactions()?;
502
503    // Collect results using `rayon`.
504    let vec = match chain {
505        Chain::Main => range
506            .into_iter()
507            .map(|block_height| get_block_extended_header_from_height(block_height, &tapes, db))
508            .collect::<DbResult<Vec<ExtendedBlockHeader>>>()?,
509        Chain::Alt(chain_id) => {
510            let ranges = { get_alt_chain_history_ranges(db, range, chain_id, &tx_ro)? };
511
512            ranges
513                .iter()
514                .rev()
515                .flat_map(|(chain, range)| {
516                    range.clone().map(|height| match *chain {
517                        Chain::Main => get_block_extended_header_from_height(height, &tapes, db),
518                        Chain::Alt(chain_id) => get_alt_block_extended_header_from_height(
519                            db,
520                            &AltBlockHeight {
521                                chain_id: chain_id.into(),
522                                height,
523                            },
524                            &tx_ro,
525                        ),
526                    })
527                })
528                .collect::<DbResult<Vec<_>>>()?
529        }
530    };
531
532    Ok(BlockchainResponse::BlockExtendedHeaderInRange(vec))
533}
534
535/// [`BlockchainReadRequest::ChainHeight`].
536#[inline]
537fn chain_height(db: &BlockchainDatabase) -> ResponseResult {
538    let tapes = db.linear_tapes.reader();
539
540    let chain_height = tapes
541        .fixed_sized_tape_len(&db.block_infos)
542        .expect("Required tape not found");
543
544    if chain_height == 0 {
545        return Err(BlockchainError::NotFound);
546    }
547
548    let block_hash = tapes
549        .read_entry(&db.block_infos, chain_height - 1)?
550        .ok_or(BlockchainError::NotFound)?
551        .block_hash;
552
553    Ok(BlockchainResponse::ChainHeight(
554        chain_height.try_into().unwrap(),
555        block_hash,
556    ))
557}
558
559/// [`BlockchainReadRequest::GeneratedCoins`].
560#[inline]
561fn generated_coins(db: &BlockchainDatabase, height: usize) -> ResponseResult {
562    let tapes = db.linear_tapes.reader();
563
564    Ok(BlockchainResponse::GeneratedCoins(
565        tapes
566            .read_entry(&db.block_infos, usize_to_u64(height))?
567            .map_or(0, |info| info.cumulative_generated_coins),
568    ))
569}
570
571/// [`BlockchainReadRequest::CumulativeRctOutsInRange`].
572#[inline]
573fn cumulative_rct_outs_in_range(db: &BlockchainDatabase, range: Range<usize>) -> ResponseResult {
574    let tapes = db.linear_tapes.reader();
575
576    let cumulative_rct_outs = tapes
577        .iter_from(&db.block_infos, usize_to_u64(range.start))?
578        .take(range.len())
579        .map(|info| Ok(info?.cumulative_rct_outs))
580        .collect::<DbResult<Vec<u64>>>()?;
581
582    if cumulative_rct_outs.len() < range.len() {
583        return Err(BlockchainError::NotFound);
584    }
585
586    Ok(BlockchainResponse::CumulativeRctOutsInRange(
587        cumulative_rct_outs,
588    ))
589}
590
591/// [`BlockchainReadRequest::Outputs`].
592#[inline]
593fn outputs(
594    db: &BlockchainDatabase,
595    outputs: IndexMap<Amount, IndexSet<AmountIndex>>,
596    get_txid: bool,
597) -> ResponseResult {
598    // Prepare tx/tables in `ThreadLocal`.
599
600    let (tx_ro, tapes) = db.read_transactions()?;
601
602    let amount_of_outs = outputs
603        .par_iter()
604        .map(|(&amount, _)| {
605            if amount == 0 {
606                Ok((
607                    amount,
608                    tapes
609                        .fixed_sized_tape_len(&db.rct_outputs)
610                        .expect("Required tape not found"),
611                ))
612            } else {
613                // v1 transactions.
614                match get_num_outputs_with_amount(db, &tx_ro, amount) {
615                    Ok(count) => Ok((amount, count)),
616                    Err(e) => Err(e),
617                }
618            }
619        })
620        .collect::<Result<_, _>>()?;
621
622    // The 2nd mapping function.
623    // This is pulled out from the below `map()` for readability.
624    let inner_map = |amount, amount_index| {
625        let id = PreRctOutputId {
626            amount,
627            amount_index,
628        };
629
630        let output_on_chain = match id_to_output_on_chain(db, &id, get_txid, &tx_ro, &tapes) {
631            Ok(output) => output,
632            Err(BlockchainError::NotFound) => return Ok(Either::Right(amount_index)),
633            Err(e) => return Err(e),
634        };
635
636        Ok(Either::Left((amount_index, output_on_chain)))
637    };
638
639    let (map, wanted_outputs) = outputs
640        .into_par_iter()
641        .map(|(amount, amount_index_set)| {
642            let (left, right) = amount_index_set
643                .into_par_iter()
644                .map(|amount_index| inner_map(amount, amount_index))
645                .collect::<Result<_, _>>()?;
646
647            Ok(((amount, left), (amount, right)))
648        })
649        .collect::<DbResult<(IndexMap<_, IndexMap<_, _>>, IndexMap<_, IndexSet<_>>)>>()?;
650
651    let cache = OutputCache::new(map, amount_of_outs, wanted_outputs);
652
653    Ok(BlockchainResponse::Outputs(cache))
654}
655
656/// [`BlockchainReadRequest::OutputsVec`].
657#[inline]
658fn outputs_vec(
659    db: &BlockchainDatabase,
660    outputs: Vec<(Amount, AmountIndex)>,
661    get_txid: bool,
662) -> ResponseResult {
663    let (tx_ro, tapes) = db.read_transactions()?;
664
665    let result = outputs
666        .into_iter()
667        .map(|(amount, amount_index)| {
668            let id = PreRctOutputId {
669                amount,
670                amount_index,
671            };
672            let output = id_to_output_on_chain(db, &id, get_txid, &tx_ro, &tapes)?;
673            Ok((amount, vec![(amount_index, output)]))
674        })
675        .collect::<DbResult<Vec<_>>>()?;
676
677    Ok(BlockchainResponse::OutputsVec(result))
678}
679
680/// [`BlockchainReadRequest::NumberOutputsWithAmount`].
681#[inline]
682fn number_outputs_with_amount(db: &BlockchainDatabase, amounts: Vec<Amount>) -> ResponseResult {
683    let (tx_ro, tapes) = db.read_transactions()?;
684
685    // Cache the amount of RCT outputs once.
686    let num_rct_outputs = u64_to_usize(
687        tapes
688            .fixed_sized_tape_len(&db.rct_outputs)
689            .expect("Required tape not found"),
690    );
691
692    // Collect results using `rayon`.
693    let map = amounts
694        .into_par_iter()
695        .map(|amount| {
696            if amount == 0 {
697                // v2 transactions.
698                Ok((amount, num_rct_outputs))
699            } else {
700                // v1 transactions.
701                match get_num_outputs_with_amount(db, &tx_ro, amount) {
702                    Ok(count) => Ok((amount, u64_to_usize(count))),
703                    Err(e) => Err(e),
704                }
705            }
706        })
707        .collect::<DbResult<HashMap<Amount, usize>>>()?;
708
709    Ok(BlockchainResponse::NumberOutputsWithAmount(map))
710}
711
712/// [`BlockchainReadRequest::KeyImagesSpent`].
713#[inline]
714fn key_images_spent(db: &BlockchainDatabase, key_images: HashSet<KeyImage>) -> ResponseResult {
715    let tx_ro = db.fjall.snapshot();
716
717    // FIXME:
718    // Create/use `enum cuprate_types::Exist { Does, DoesNot }`
719    // or similar instead of `bool` for clarity.
720    // <https://github.com/Cuprate/cuprate/pull/113#discussion_r1581536526>
721    //
722    // Collect results using `rayon`.
723    match key_images
724        .into_par_iter()
725        .map(|ki| tx_ro.contains_key(&db.key_images, ki))
726        // If the result is either:
727        // `Ok(true)` => a key image was found, return early
728        // `Err` => an error was found, return early
729        //
730        // Else, `Ok(false)` will continue the iterator.
731        .find_any(|result| !matches!(result, Ok(false)))
732    {
733        None => Ok(BlockchainResponse::KeyImagesSpent(false)), // Key image was NOT found.
734        Some(Ok(true)) => Ok(BlockchainResponse::KeyImagesSpent(true)), // Key image was found.
735        Some(Err(e)) => Err(e.into()),                         // A database error occurred.
736        Some(Ok(false)) => unreachable!(),
737    }
738}
739
740/// [`BlockchainReadRequest::KeyImagesSpentVec`].
741fn key_images_spent_vec(db: &BlockchainDatabase, key_images: Vec<KeyImage>) -> ResponseResult {
742    let tx_ro = db.fjall.snapshot();
743
744    // Collect results using `rayon`.
745    Ok(BlockchainResponse::KeyImagesSpentVec(
746        key_images
747            .into_par_iter()
748            .map(|ki| tx_ro.contains_key(&db.key_images, ki))
749            .collect::<Result<_, _>>()?,
750    ))
751}
752
753/// [`BlockchainReadRequest::CompactChainHistory`]
754fn compact_chain_history(db: &BlockchainDatabase) -> ResponseResult {
755    let tapes = db.linear_tapes.reader();
756
757    let get_block_info = |height| -> Result<_, BlockchainError> {
758        tapes
759            .read_entry(&db.block_infos, height)?
760            .ok_or(BlockchainError::NotFound)
761    };
762
763    let top_block_height = tapes
764        .fixed_sized_tape_len(&db.block_infos)
765        .expect("Required tape not open")
766        - 1;
767
768    let top_block_info = get_block_info(top_block_height)?;
769    let cumulative_difficulty = combine_low_high_bits_to_u128(
770        top_block_info.cumulative_difficulty_low,
771        top_block_info.cumulative_difficulty_high,
772    );
773
774    /// The amount of top block IDs in the compact chain.
775    const INITIAL_BLOCKS: usize = 11;
776
777    // rayon is not used here because the amount of block IDs is expected to be small.
778    let mut block_ids = (0..)
779        .map(compact_history_index_to_height_offset::<INITIAL_BLOCKS>)
780        .map_while(|i| top_block_height.checked_sub(usize_to_u64(i)))
781        .map(|height| Ok(get_block_info(height)?.block_hash))
782        .collect::<DbResult<Vec<_>>>()?;
783
784    if compact_history_genesis_not_included::<INITIAL_BLOCKS>(u64_to_usize(top_block_height)) {
785        block_ids.push(get_block_info(0)?.block_hash);
786    }
787
788    Ok(BlockchainResponse::CompactChainHistory {
789        cumulative_difficulty,
790        block_ids,
791    })
792}
793
794/// [`BlockchainReadRequest::NextChainEntry`]
795///
796/// # Invariant
797/// `block_ids` must be sorted in reverse chronological block order, or else
798/// the returned result is unspecified and meaningless, as this function
799/// performs a binary search.
800fn next_chain_entry(
801    db: &BlockchainDatabase,
802    block_ids: &[BlockHash],
803    next_entry_size: usize,
804) -> ResponseResult {
805    let (tx_ro, tapes) = db.read_transactions()?;
806
807    let idx = find_split_point(db, block_ids, false, false, &tx_ro)?;
808
809    // This will happen if we have a different genesis block.
810    if idx == block_ids.len() {
811        return Ok(BlockchainResponse::NextChainEntry {
812            start_height: None,
813            chain_height: 0,
814            block_ids: vec![],
815            block_weights: vec![],
816            cumulative_difficulty: 0,
817            first_block_blob: None,
818        });
819    }
820
821    // The returned chain entry must overlap with one of the blocks  we were told about.
822    let first_known_block_hash = block_ids[idx];
823    let first_known_height =
824        block_height(db, &tx_ro, &first_known_block_hash)?.ok_or(BlockchainError::NotFound)?;
825
826    let chain_height = crate::ops::blockchain::chain_height(db, &tapes)?;
827    let last_height_in_chain_entry = min(first_known_height + next_entry_size, chain_height);
828
829    let entry_count = last_height_in_chain_entry - first_known_height;
830    let mut block_infos = vec![crate::types::BlockInfo::default(); entry_count];
831    tapes.read_entries(
832        &db.block_infos,
833        usize_to_u64(first_known_height),
834        &mut block_infos,
835    )?;
836
837    let (block_ids, block_weights) = block_infos
838        .iter()
839        .map(|info| (info.block_hash, info.weight))
840        .unzip::<_, _, Vec<_>, Vec<_>>();
841
842    let top_block_info = tapes
843        .read_entry(&db.block_infos, usize_to_u64(chain_height) - 1)?
844        .ok_or(BlockchainError::NotFound)?;
845
846    let first_block_blob = if block_ids.len() >= 2 {
847        Some(get_block(&(first_known_height + 1), None, &tapes, db)?.serialize())
848    } else {
849        None
850    };
851
852    Ok(BlockchainResponse::NextChainEntry {
853        start_height: Some(first_known_height),
854        chain_height,
855        block_ids,
856        block_weights,
857        cumulative_difficulty: combine_low_high_bits_to_u128(
858            top_block_info.cumulative_difficulty_low,
859            top_block_info.cumulative_difficulty_high,
860        ),
861        first_block_blob,
862    })
863}
864
865/// [`BlockchainReadRequest::FindFirstUnknown`]
866///
867/// # Invariant
868/// `block_ids` must be sorted in chronological block order, or else
869/// the returned result is unspecified and meaningless, as this function
870/// performs a binary search.
871fn find_first_unknown(db: &BlockchainDatabase, block_ids: &[BlockHash]) -> ResponseResult {
872    let tx_ro = db.fjall.snapshot();
873    let idx = find_split_point(db, block_ids, true, true, &tx_ro)?;
874
875    Ok(if idx == block_ids.len() {
876        BlockchainResponse::FindFirstUnknown(None)
877    } else if idx == 0 {
878        BlockchainResponse::FindFirstUnknown(Some((0, 0)))
879    } else {
880        let last_known_height = usize::from_le_bytes(
881            tx_ro
882                .get(&db.block_heights, block_ids[idx - 1])?
883                .unwrap()
884                .as_ref()
885                .try_into()
886                .unwrap(),
887        );
888
889        BlockchainResponse::FindFirstUnknown(Some((idx, last_known_height + 1)))
890    })
891}
892
893/// [`BlockchainReadRequest::TxsInBlock`]
894fn txs_in_block(
895    db: &BlockchainDatabase,
896    block_hash: BlockHash,
897    missing_txs: Vec<u64>,
898) -> ResponseResult {
899    let (tx_ro, tapes) = db.read_transactions()?;
900
901    // Check the main chain first, fall back to alt blocks if not found.
902    let (block, txs) = if let Some(block_height) = block_height(db, &tx_ro, &block_hash)? {
903        let block_info = tapes
904            .read_entry(&db.block_infos, usize_to_u64(block_height))?
905            .ok_or(BlockchainError::NotFound)?;
906
907        let block = get_block(&block_height, Some(&block_info), &tapes, db)?;
908        let first_tx_index = block_info.mining_tx_index + 1;
909
910        if block.transactions.len() < missing_txs.len()
911            || missing_txs
912                .iter()
913                .any(|&i| i >= usize_to_u64(block.transactions.len()))
914        {
915            return Ok(BlockchainResponse::TxsInBlock(None));
916        }
917
918        let txs = missing_txs
919            .into_iter()
920            .map(|index_offset| get_tx_blob_from_id(&(first_tx_index + index_offset), &tapes, db))
921            .collect::<DbResult<_>>()?;
922
923        (block, txs)
924    } else {
925        let alt_block_height =
926            alt_block_height(db, &tx_ro, &block_hash)?.ok_or(BlockchainError::NotFound)?;
927
928        let block = get_alt_block(db, &alt_block_height, &tx_ro)?;
929
930        if block.transactions.len() < missing_txs.len()
931            || missing_txs
932                .iter()
933                .any(|&i| i >= usize_to_u64(block.transactions.len()))
934        {
935            return Ok(BlockchainResponse::TxsInBlock(None));
936        }
937
938        let alt_transaction_blobs = db.alt_transaction_blobs.load();
939        let txs = missing_txs
940            .into_iter()
941            .map(|index_offset| {
942                let tx_hash = &block.transactions[u64_to_usize(index_offset)];
943
944                tx_ro
945                    .get(&**alt_transaction_blobs, tx_hash)?
946                    .map(|blob| blob.to_vec())
947                    .ok_or(BlockchainError::NotFound)
948            })
949            .collect::<DbResult<_>>()?;
950
951        (block, txs)
952    };
953
954    Ok(BlockchainResponse::TxsInBlock(Some(TxsInBlock {
955        block: block.serialize(),
956        txs,
957    })))
958}
959
960/// [`BlockchainReadRequest::AltBlocksInChain`]
961fn alt_blocks_in_chain(db: &BlockchainDatabase, chain_id: ChainId) -> ResponseResult {
962    let tx_ro = db.fjall.snapshot();
963
964    // Get the history of this alt-chain.
965    let history = { get_alt_chain_history_ranges(db, 0..usize::MAX, chain_id, &tx_ro)? };
966
967    // Get all the blocks until we join the main-chain.
968    let blocks = history
969        .iter()
970        .rev()
971        .skip(1)
972        .flat_map(|(chain_id, range)| {
973            let Chain::Alt(chain_id) = chain_id else {
974                panic!("Should not have main chain blocks here we skipped last range");
975            };
976
977            range.clone().map(|height| {
978                get_alt_block_information(
979                    db,
980                    &AltBlockHeight {
981                        chain_id: (*chain_id).into(),
982                        height,
983                    },
984                    &tx_ro,
985                )
986            })
987        })
988        .collect::<DbResult<_>>()?;
989
990    Ok(BlockchainResponse::AltBlocksInChain(blocks))
991}
992
993/// [`BlockchainReadRequest::Block`]
994fn block(db: &BlockchainDatabase, block_height: BlockHeight) -> ResponseResult {
995    let tapes = db.linear_tapes.reader();
996
997    Ok(BlockchainResponse::Block(get_block(
998        &block_height,
999        None,
1000        &tapes,
1001        db,
1002    )?))
1003}
1004
1005/// [`BlockchainReadRequest::BlockByHash`]
1006fn block_by_hash(db: &BlockchainDatabase, block_hash: BlockHash) -> ResponseResult {
1007    let (tx_ro, tapes) = db.read_transactions()?;
1008
1009    Ok(BlockchainResponse::Block(get_block_by_hash(
1010        db,
1011        &block_hash,
1012        &tx_ro,
1013        &tapes,
1014    )?))
1015}
1016
1017/// [`BlockchainReadRequest::TotalTxCount`]
1018fn total_tx_count(db: &BlockchainDatabase) -> BlockchainResponse {
1019    let tapes = db.linear_tapes.reader();
1020    let count = u64_to_usize(
1021        tapes
1022            .fixed_sized_tape_len(&db.tx_infos)
1023            .expect("tx_infos tape exists"),
1024    );
1025
1026    BlockchainResponse::TotalTxCount(count)
1027}
1028
1029/// Walk a directory recursively and sum the sizes of all files.
1030fn dir_size(path: &std::path::Path) -> u64 {
1031    let Ok(entries) = std::fs::read_dir(path) else {
1032        return 0;
1033    };
1034    entries
1035        .filter_map(Result::ok)
1036        .map(|e| match e.file_type() {
1037            Ok(ft) if ft.is_dir() => dir_size(&e.path()),
1038            Ok(ft) if ft.is_file() => e.metadata().map_or(0, |m| m.len()),
1039            _ => 0,
1040        })
1041        .sum()
1042}
1043
1044/// [`BlockchainReadRequest::DatabaseSize`]
1045fn database_size(db: &BlockchainDatabase) -> BlockchainResponse {
1046    // Sum file sizes in both data directories (blob and index).
1047    let blob_size = dir_size(&db.config.blob_dir);
1048    let index_size = if db.config.index_dir == db.config.blob_dir {
1049        0
1050    } else {
1051        dir_size(&db.config.index_dir)
1052    };
1053    let database_size = blob_size + index_size;
1054
1055    // TODO:
1056    let free_space = u64::MAX;
1057
1058    BlockchainResponse::DatabaseSize {
1059        database_size,
1060        free_space,
1061    }
1062}
1063
1064/// [`BlockchainReadRequest::OutputHistogram`]
1065fn output_histogram(db: &BlockchainDatabase, input: &OutputHistogramInput) -> ResponseResult {
1066    let (tx_ro, tapes) = db.read_transactions()?;
1067
1068    let num_rct_outputs = tapes
1069        .fixed_sized_tape_len(&db.rct_outputs)
1070        .expect("rct_outputs tape exists");
1071
1072    let amounts_and_counts: BTreeMap<Amount, u64> = if input.amounts.is_empty() {
1073        let mut result = BTreeMap::new();
1074
1075        // RCT outputs are represented as amount = 0 and live in a separate tape.
1076        if num_rct_outputs > 0 {
1077            result.insert(0_u64, num_rct_outputs);
1078        }
1079
1080        // We need to get the amount of outputs for each amount. We do this by first finding the next
1081        // `amount` value in the sorted table, then calling `get_num_outputs_with_amount`, then using
1082        // fjalls `range` to find the next amount value.
1083        let mut next = tx_ro.first_key_value(&db.pre_rct_outputs);
1084        while let Some(guard) = next {
1085            let amount = Amount::from_be_bytes(guard.key()?[..8].try_into().unwrap());
1086
1087            result.insert(amount, get_num_outputs_with_amount(db, &tx_ro, amount)?);
1088
1089            // Seek the first key of the next amount group, stopping on overflow/end.
1090            next = match amount.checked_add(1) {
1091                Some(next_amount) => tx_ro
1092                    .range(&db.pre_rct_outputs, next_amount.to_be_bytes()..)
1093                    .next(),
1094                None => None,
1095            };
1096        }
1097
1098        result
1099    } else {
1100        // Use the caller-specified amounts.
1101        input
1102            .amounts
1103            .iter()
1104            .map(|&amount| {
1105                let count = if amount == 0 {
1106                    num_rct_outputs
1107                } else {
1108                    get_num_outputs_with_amount(db, &tx_ro, amount)?
1109                };
1110                Ok((amount, count))
1111            })
1112            .collect::<DbResult<_>>()?
1113    };
1114
1115    let current_height = tapes
1116        .fixed_sized_tape_len(&db.block_infos)
1117        .expect("block_infos tape exists");
1118
1119    let histogram = amounts_and_counts
1120        .into_iter()
1121        .filter(|&(_, total_instances)| {
1122            // filter the amounts that have to many or too little outputs
1123            (input.min_count == 0 || total_instances >= input.min_count)
1124                && (input.max_count == 0 || total_instances <= input.max_count)
1125        })
1126        .map(|(amount, total_instances)| {
1127            let (unlocked_instances, recent_instances) =
1128                if input.unlocked || input.recent_cutoff > 0 {
1129                    unlocked_and_recent_instances(
1130                        db,
1131                        &tx_ro,
1132                        &tapes,
1133                        amount,
1134                        total_instances,
1135                        current_height,
1136                        input.recent_cutoff,
1137                    )?
1138                } else {
1139                    (0, 0)
1140                };
1141
1142            Ok(OutputHistogramEntry {
1143                amount,
1144                total_instances,
1145                unlocked_instances,
1146                recent_instances,
1147            })
1148        })
1149        .collect::<DbResult<_>>()?;
1150
1151    Ok(BlockchainResponse::OutputHistogram(histogram))
1152}
1153
1154/// [`BlockchainReadRequest::CoinbaseTxSum`]
1155fn coinbase_tx_sum(db: &BlockchainDatabase, height: usize, count: u64) -> ResponseResult {
1156    let tapes = db.linear_tapes.reader();
1157
1158    let start_cumulative = if height == 0 {
1159        0_u64
1160    } else {
1161        tapes
1162            .read_entry(&db.block_infos, usize_to_u64(height - 1))?
1163            .ok_or(BlockchainError::NotFound)?
1164            .cumulative_generated_coins
1165    };
1166
1167    let (emission_amount, fee_amount, _) = (height..)
1168        .zip(tapes.iter_from(&db.block_infos, usize_to_u64(height))?)
1169        .take(u64_to_usize(count))
1170        .try_fold(
1171            (0_u128, 0_u128, start_cumulative),
1172            |(emission_amount, fee_amount, prev_cumulative), (h, block_info)| {
1173                let block_info = block_info?;
1174
1175                // base_reward = newly minted coins for this block (does not include fees).
1176                let base_reward = block_info
1177                    .cumulative_generated_coins
1178                    .saturating_sub(prev_cumulative);
1179
1180                // coinbase_output = sum of miner_tx output amounts = base_reward + block_fees.
1181                let block = get_block(&h, Some(&block_info), &tapes, db)?;
1182                let coinbase_output: u64 = block
1183                    .miner_transaction()
1184                    .prefix()
1185                    .outputs
1186                    .iter()
1187                    .map(|o| o.amount.unwrap_or(0))
1188                    .sum();
1189
1190                DbResult::Ok((
1191                    emission_amount + u128::from(base_reward),
1192                    fee_amount + u128::from(coinbase_output.saturating_sub(base_reward)),
1193                    block_info.cumulative_generated_coins,
1194                ))
1195            },
1196        )?;
1197
1198    let (emission_amount, emission_amount_top64) = split_u128_into_low_high_bits(emission_amount);
1199    let (fee_amount, fee_amount_top64) = split_u128_into_low_high_bits(fee_amount);
1200
1201    Ok(BlockchainResponse::CoinbaseTxSum(CoinbaseTxSum {
1202        emission_amount,
1203        emission_amount_top64,
1204        fee_amount,
1205        fee_amount_top64,
1206    }))
1207}
1208
1209/// [`BlockchainReadRequest::AltChains`]
1210fn alt_chains(db: &BlockchainDatabase) -> ResponseResult {
1211    let (tx_ro, tapes) = db.read_transactions()?;
1212    let alt_chain_infos = db.alt_chain_infos.load();
1213    let alt_block_infos = db.alt_block_infos.load();
1214
1215    let mut chains = Vec::new();
1216
1217    for guard in alt_chain_infos.iter() {
1218        let (chain_id_bytes, chain_info_bytes) = guard.into_inner()?;
1219
1220        let chain_id = RawChainId(u64::from_le_bytes(
1221            chain_id_bytes.as_ref().try_into().unwrap(),
1222        ));
1223        let chain_info: AltChainInfo = bytemuck::pod_read_unaligned(chain_info_bytes.as_ref());
1224
1225        let tip = chain_info.chain_height - 1;
1226        let history =
1227            get_alt_chain_history_ranges(db, 0..chain_info.chain_height, chain_id.into(), &tx_ro)?;
1228
1229        let tip_height = AltBlockHeight {
1230            chain_id,
1231            height: tip,
1232        };
1233        let tip_info_bytes = tx_ro
1234            .get(&**alt_block_infos, bytemuck::bytes_of(&tip_height))?
1235            .ok_or(BlockchainError::NotFound)?;
1236        let tip_info: CompactAltBlockInfo = bytemuck::pod_read_unaligned(tip_info_bytes.as_ref());
1237
1238        // The last segment is the main-chain portion. Its range starts at 0 and ends at the
1239        // fork height, so the last main chain block = range.end - 1.
1240        let main_fork_height = history
1241            .last()
1242            .map(|(_, r)| r.end.saturating_sub(1))
1243            .ok_or(BlockchainError::NotFound)?;
1244
1245        // Build the hashes of the alt chain all the way to the main chain split point.
1246        let mut block_hashes = Vec::new();
1247        for (segment_chain, height_range) in &history {
1248            let Chain::Alt(segment_chain_id) = segment_chain else {
1249                break;
1250            };
1251            let raw_id = RawChainId::from(*segment_chain_id);
1252            for height in height_range.clone().rev() {
1253                let alt_h = AltBlockHeight {
1254                    chain_id: raw_id,
1255                    height,
1256                };
1257                let info_bytes = tx_ro
1258                    .get(&**alt_block_infos, bytemuck::bytes_of(&alt_h))?
1259                    .ok_or(BlockchainError::NotFound)?;
1260                let info: CompactAltBlockInfo = bytemuck::pod_read_unaligned(info_bytes.as_ref());
1261                block_hashes.push(info.block_hash);
1262            }
1263        }
1264
1265        // Get the main chain block hash at the fork point.
1266        let main_chain_parent_block = tapes
1267            .read_entry(&db.block_infos, usize_to_u64(main_fork_height))?
1268            .map(|info| info.block_hash)
1269            .ok_or(BlockchainError::NotFound)?;
1270
1271        let length = usize_to_u64(block_hashes.len());
1272
1273        chains.push(ChainInfo {
1274            block_hash: tip_info.block_hash,
1275            block_hashes,
1276            difficulty: tip_info.cumulative_difficulty_low,
1277            difficulty_top64: tip_info.cumulative_difficulty_high,
1278            height: usize_to_u64(tip),
1279            length,
1280            main_chain_parent_block,
1281        });
1282    }
1283
1284    Ok(BlockchainResponse::AltChains(chains))
1285}
1286
1287/// [`BlockchainReadRequest::AltChainCount`]
1288fn alt_chain_count(db: &BlockchainDatabase) -> ResponseResult {
1289    let count = db.alt_chain_infos.load().len()?;
1290    Ok(BlockchainResponse::AltChainCount(count))
1291}
1292
1293/// [`BlockchainReadRequest::Transactions`]
1294fn transactions(db: &BlockchainDatabase, tx_hashes: Vec<[u8; 32]>) -> ResponseResult {
1295    let (tx_ro, tapes) = db.read_transactions()?;
1296    let chain_height = crate::ops::blockchain::chain_height(db, &tapes)?;
1297
1298    let mut txs = Vec::with_capacity(tx_hashes.len());
1299    let mut missed_txs = Vec::new();
1300    let mut block_timestamps = HashMap::new();
1301
1302    for tx_hash in tx_hashes {
1303        let Some(tx_id) = tx_ro.get(&db.tx_ids, tx_hash)? else {
1304            missed_txs.push(tx_hash);
1305            continue;
1306        };
1307
1308        let tx_id = u64::from_le_bytes(tx_id.as_ref().try_into().unwrap());
1309        let tx_info = tapes
1310            .read_entry(&db.tx_infos, tx_id)?
1311            .ok_or(BlockchainError::NotFound)?;
1312        let block_info = tapes
1313            .read_entry(&db.block_infos, usize_to_u64(tx_info.height))?
1314            .ok_or(BlockchainError::NotFound)?;
1315        let is_miner_tx = tx_id == block_info.mining_tx_index;
1316
1317        let (pruned_blob, prunable_blob, prunable_hash) =
1318            get_split_tx_blobs(tx_id, &tx_info, is_miner_tx, &tapes, db)?;
1319
1320        let block_timestamp = if let Some(timestamp) = block_timestamps.get(&tx_info.height) {
1321            *timestamp
1322        } else {
1323            let timestamp =
1324                get_block_extended_header_from_height(tx_info.height, &tapes, db)?.timestamp;
1325            block_timestamps.insert(tx_info.height, timestamp);
1326            timestamp
1327        };
1328
1329        let output_indices = if tx_info.is_v1_tx() {
1330            let output_indices = tx_ro
1331                .get(&db.v1_tx_outputs, tx_id.to_le_bytes())?
1332                .ok_or(BlockchainError::NotFound)?;
1333
1334            output_indices
1335                .chunks(8)
1336                .map(|chunk| u64::from_le_bytes(chunk.try_into().unwrap()))
1337                .collect()
1338        } else {
1339            (0..tx_info.numb_rct_outputs)
1340                .map(|i| usize_to_u64(i) + tx_info.rct_output_start_idx)
1341                .collect()
1342        };
1343
1344        txs.push(TxInBlockchain {
1345            block_height: usize_to_u64(tx_info.height),
1346            block_timestamp,
1347            confirmations: usize_to_u64(chain_height - tx_info.height),
1348            output_indices,
1349            tx_hash,
1350            pruned_blob,
1351            prunable_blob,
1352            prunable_hash,
1353        });
1354    }
1355
1356    Ok(BlockchainResponse::Transactions { txs, missed_txs })
1357}
1358
1359/// [`BlockchainReadRequest::TotalRctOutputs`]
1360fn total_rct_outputs(db: &BlockchainDatabase) -> BlockchainResponse {
1361    let tapes = db.linear_tapes.reader();
1362
1363    let len = tapes
1364        .fixed_sized_tape_len(&db.rct_outputs)
1365        .expect("Required tape not found");
1366
1367    BlockchainResponse::TotalRctOutputs(len)
1368}
1369
1370/// [`BlockchainReadRequest::TxOutputIndexes`]
1371fn tx_output_indexes(db: &BlockchainDatabase, tx_hash: &[u8; 32]) -> ResponseResult {
1372    let (tx_ro, tapes) = db.read_transactions()?;
1373
1374    let tx_id = u64::from_le_bytes(
1375        tx_ro
1376            .get(&db.tx_ids, tx_hash)?
1377            .ok_or(BlockchainError::NotFound)?
1378            .as_ref()
1379            .try_into()
1380            .unwrap(),
1381    );
1382
1383    let tx_info = tapes
1384        .read_entry(&db.tx_infos, tx_id)?
1385        .ok_or(BlockchainError::NotFound)?;
1386
1387    let o_indexes = if tx_info.is_v1_tx() {
1388        let bytes = tx_ro
1389            .get(&db.v1_tx_outputs, tx_id.to_le_bytes())?
1390            .ok_or(BlockchainError::NotFound)?;
1391
1392        bytemuck::pod_collect_to_vec(bytes.as_ref())
1393    } else {
1394        (0..tx_info.numb_rct_outputs)
1395            .map(|i| usize_to_u64(i) + tx_info.rct_output_start_idx)
1396            .collect()
1397    };
1398
1399    Ok(BlockchainResponse::TxOutputIndexes(o_indexes))
1400}
1401
1402/// [`BlockchainReadRequest::PreRctOutputDistribution`]
1403fn pre_rct_output_distribution(
1404    db: &BlockchainDatabase,
1405    input: &PreRctOutputDistributionInput,
1406) -> ResponseResult {
1407    let (tx_ro, tapes) = db.read_transactions()?;
1408    let chain_height = u64_to_usize(
1409        tapes
1410            .fixed_sized_tape_len(&db.block_infos)
1411            .expect("block_infos tape exists"),
1412    );
1413
1414    if input.to_height.is_some_and(|h| h.get() < input.from_height) {
1415        return Err(BlockchainError::NotFound);
1416    }
1417
1418    let to_height = input.to_height.map_or(chain_height - 1, |h| {
1419        let h = h.get();
1420        u64_to_usize(h)
1421    });
1422
1423    if to_height >= chain_height {
1424        return Err(BlockchainError::NotFound);
1425    }
1426
1427    let mut result = Vec::with_capacity(input.amounts.len());
1428
1429    for &amount in &input.amounts {
1430        let amount = amount.get();
1431
1432        let start_height = u64_to_usize(input.from_height);
1433
1434        if start_height > to_height {
1435            return Err(BlockchainError::NotFound);
1436        }
1437
1438        let mut per_block: Vec<u64> = vec![0; to_height - start_height + 1];
1439        let mut below_start: u64 = 0;
1440
1441        // loop over all outputs with this amount.
1442        for guard in tx_ro.prefix(&db.pre_rct_outputs, amount.to_be_bytes()) {
1443            let output: Output = bytemuck::pod_read_unaligned(guard.value()?.as_ref());
1444            let h = output.height;
1445            // Add the output to the block it says it is in.
1446            if h < start_height {
1447                below_start += 1;
1448            } else if h <= to_height {
1449                per_block[h - start_height] += 1;
1450            } else {
1451                // The outputs are sorted by (amount, height), so we can stop
1452                // once we see a height above the range.
1453                break;
1454            }
1455        }
1456
1457        // monerod folds the below `start_height` count into the first bucket and
1458        // reports `base = 0` for pre-RCT amounts.
1459        per_block[0] += below_start;
1460
1461        let distribution = if input.cumulative {
1462            let mut cumulative = per_block;
1463            for i in 1..cumulative.len() {
1464                cumulative[i] += cumulative[i - 1];
1465            }
1466            cumulative
1467        } else {
1468            per_block
1469        };
1470
1471        result.push(OutputDistributionData {
1472            amount,
1473            distribution,
1474            start_height: usize_to_u64(start_height),
1475            base: 0,
1476        });
1477    }
1478
1479    Ok(BlockchainResponse::PreRctOutputDistribution(result))
1480}