Skip to main content

cuprate_blockchain/ops/alt_block/
tx.rs

1use cuprate_types::VerifiedTransactionInformation;
2
3use fjall::Readable;
4use monero_oxide::transaction::Transaction;
5
6use crate::{
7    error::{BlockchainError, DbResult},
8    types::{AltTransactionInfo, TxHash},
9    BlockchainDatabase,
10};
11
12/// Adds a [`VerifiedTransactionInformation`] from an alt-block if it is not already in the DB.
13///
14pub fn add_alt_transaction_blob(
15    db: &BlockchainDatabase,
16    tx: &VerifiedTransactionInformation,
17    tx_rw: &mut fjall::OwnedWriteBatch,
18) -> DbResult<()> {
19    tx_rw.insert(
20        &db.alt_transaction_infos.load(),
21        tx.tx_hash,
22        bytemuck::bytes_of(&AltTransactionInfo {
23            tx_weight: tx.tx_weight,
24            fee: tx.fee,
25            tx_hash: tx.tx_hash,
26        }),
27    );
28
29    tx_rw.insert(
30        &db.alt_transaction_blobs.load(),
31        tx.tx_hash,
32        [tx.tx_pruned.as_slice(), tx.tx_prunable_blob.as_slice()]
33            .concat()
34            .as_slice(),
35    );
36
37    Ok(())
38}
39
40/// Retrieve a [`VerifiedTransactionInformation`] from the database.
41///
42pub fn get_alt_transaction(
43    db: &BlockchainDatabase,
44    tx_hash: &TxHash,
45    tx_ro: &fjall::Snapshot,
46) -> DbResult<VerifiedTransactionInformation> {
47    let tx_info = tx_ro
48        .get(&**db.alt_transaction_infos.load(), tx_hash)?
49        .ok_or(BlockchainError::NotFound)?;
50
51    let tx_info: AltTransactionInfo = bytemuck::pod_read_unaligned(tx_info.as_ref());
52
53    let tx = match tx_ro.get(&**db.alt_transaction_blobs.load(), tx_hash)? {
54        Some(tx_blob) => Transaction::read(&mut tx_blob.as_ref()).unwrap(),
55        None => return Err(BlockchainError::NotFound),
56    };
57
58    let tx_weight = tx_info.tx_weight;
59    let fee = tx_info.fee;
60    let (tx, tx_prunable_blob) = tx.pruned_with_prunable();
61
62    Ok(VerifiedTransactionInformation {
63        tx_prunable_blob,
64        tx_pruned: tx.serialize(),
65        tx_weight,
66        fee,
67        tx_hash: *tx_hash,
68        tx,
69    })
70}