Skip to main content

cuprate_txpool/ops/
tx_read.rs

1//! Transaction read ops.
2//!
3//! This module handles reading full transaction data, like getting a transaction from the pool.
4
5use fjall::Readable;
6use monero_oxide::transaction::Transaction;
7
8use cuprate_types::{TransactionVerificationData, TxVersion};
9
10use crate::{
11    error::TxPoolError,
12    txpool::TxpoolDatabase,
13    types::{TransactionHash, TransactionInfo},
14};
15
16/// Gets the [`TransactionVerificationData`] of a transaction in the tx-pool, leaving the tx in the pool.
17pub fn get_transaction_verification_data(
18    tx_hash: &TransactionHash,
19    snapshot: &fjall::Snapshot,
20    db: &TxpoolDatabase,
21) -> Result<TransactionVerificationData, TxPoolError> {
22    let tx_blob = snapshot
23        .get(&db.tx_blobs, tx_hash)?
24        .ok_or(TxPoolError::NotFound)?
25        .to_vec();
26
27    let tx_info = snapshot
28        .get(&db.tx_infos, tx_hash)?
29        .ok_or(TxPoolError::NotFound)?;
30
31    let tx_info: TransactionInfo = bytemuck::pod_read_unaligned(tx_info.as_ref());
32
33    let tx =
34        Transaction::read(&mut tx_blob.as_slice()).expect("Tx in the tx-pool must be parseable");
35
36    Ok(TransactionVerificationData {
37        version: TxVersion::from_raw(tx.version()).expect("Tx in tx-pool has invalid version"),
38        tx,
39        tx_blob,
40        tx_weight: tx_info.weight,
41        fee: tx_info.fee,
42        tx_hash: *tx_hash,
43        cached_verification_state: tx_info.cached_verification_state.into(),
44    })
45}
46
47/// Returns `true` if the transaction with the given hash is in the stem pool.
48///
49/// # Errors
50/// This will return an [`Err`] if the transaction is not in the pool.
51pub fn in_stem_pool(
52    tx_hash: &TransactionHash,
53    snapshot: &fjall::Snapshot,
54    db: &TxpoolDatabase,
55) -> Result<bool, TxPoolError> {
56    let tx_info = snapshot
57        .get(&db.tx_infos, tx_hash)?
58        .ok_or(TxPoolError::NotFound)?;
59
60    let tx_info: TransactionInfo = bytemuck::pod_read_unaligned(tx_info.as_ref());
61
62    Ok(tx_info.flags.private())
63}