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.
4use monero_serai::transaction::Transaction;
5
6use cuprate_database::{DatabaseRo, DbResult};
7use cuprate_types::{TransactionVerificationData, TxVersion};
8
9use crate::{
10    tables::{Tables, TransactionInfos},
11    types::{TransactionHash, TxStateFlags},
12};
13
14/// Gets the [`TransactionVerificationData`] of a transaction in the tx-pool, leaving the tx in the pool.
15pub fn get_transaction_verification_data(
16    tx_hash: &TransactionHash,
17    tables: &impl Tables,
18) -> DbResult<TransactionVerificationData> {
19    let tx_blob = tables.transaction_blobs().get(tx_hash)?.0;
20
21    let tx_info = tables.transaction_infos().get(tx_hash)?;
22
23    let cached_verification_state = tables.cached_verification_state().get(tx_hash)?.into();
24
25    let tx =
26        Transaction::read(&mut tx_blob.as_slice()).expect("Tx in the tx-pool must be parseable");
27
28    Ok(TransactionVerificationData {
29        version: TxVersion::from_raw(tx.version()).expect("Tx in tx-pool has invalid version"),
30        tx,
31        tx_blob,
32        tx_weight: tx_info.weight,
33        fee: tx_info.fee,
34        tx_hash: *tx_hash,
35        cached_verification_state,
36    })
37}
38
39/// Returns `true` if the transaction with the given hash is in the stem pool.
40///
41/// # Errors
42/// This will return an [`Err`] if the transaction is not in the pool.
43pub fn in_stem_pool(
44    tx_hash: &TransactionHash,
45    tx_infos: &impl DatabaseRo<TransactionInfos>,
46) -> DbResult<bool> {
47    Ok(tx_infos
48        .get(tx_hash)?
49        .flags
50        .contains(TxStateFlags::STATE_STEM))
51}