cuprate_txpool/ops/
tx_write.rs1use bytemuck::TransparentWrapper;
5use monero_serai::transaction::{NotPruned, Transaction};
6
7use cuprate_database::{DatabaseRw, DbResult, StorableVec};
8use cuprate_types::TransactionVerificationData;
9
10use crate::{
11 free::transaction_blob_hash,
12 ops::{
13 key_images::{add_tx_key_images, remove_tx_key_images},
14 TxPoolWriteError,
15 },
16 tables::TablesMut,
17 types::{TransactionHash, TransactionInfo, TxStateFlags},
18};
19
20pub fn add_transaction(
27 tx: &TransactionVerificationData,
28 state_stem: bool,
29 tables: &mut impl TablesMut,
30) -> Result<(), TxPoolWriteError> {
31 tables
33 .transaction_blobs_mut()
34 .put(&tx.tx_hash, StorableVec::wrap_ref(&tx.tx_blob))?;
35
36 let mut flags = TxStateFlags::empty();
37 flags.set(TxStateFlags::STATE_STEM, state_stem);
38
39 tables.transaction_infos_mut().put(
41 &tx.tx_hash,
42 &TransactionInfo {
43 fee: tx.fee,
44 weight: tx.tx_weight,
45 flags,
46 _padding: [0; 7],
47 },
48 )?;
49
50 let cached_verification_state = tx.cached_verification_state.into();
52 tables
53 .cached_verification_state_mut()
54 .put(&tx.tx_hash, &cached_verification_state)?;
55
56 let kis_table = tables.spent_key_images_mut();
58 add_tx_key_images(&tx.tx.prefix().inputs, &tx.tx_hash, kis_table)?;
59
60 let blob_hash = transaction_blob_hash(&tx.tx_blob);
62 tables
63 .known_blob_hashes_mut()
64 .put(&blob_hash, &tx.tx_hash)?;
65
66 Ok(())
67}
68
69pub fn remove_transaction(tx_hash: &TransactionHash, tables: &mut impl TablesMut) -> DbResult<()> {
71 let tx_blob = tables.transaction_blobs_mut().take(tx_hash)?.0;
73
74 tables.transaction_infos_mut().delete(tx_hash)?;
76
77 tables.cached_verification_state_mut().delete(tx_hash)?;
79
80 let tx = Transaction::<NotPruned>::read(&mut tx_blob.as_slice())
82 .expect("Tx in the tx-pool must be parseable");
83 let kis_table = tables.spent_key_images_mut();
84 remove_tx_key_images(&tx.prefix().inputs, kis_table)?;
85
86 let blob_hash = transaction_blob_hash(&tx_blob);
88 tables.known_blob_hashes_mut().delete(&blob_hash)?;
89
90 Ok(())
91}