cuprate_txpool/ops/
tx_write.rsuse bytemuck::TransparentWrapper;
use monero_serai::transaction::{NotPruned, Transaction};
use cuprate_database::{DatabaseRw, RuntimeError, StorableVec};
use cuprate_types::TransactionVerificationData;
use crate::{
free::transaction_blob_hash,
ops::{
key_images::{add_tx_key_images, remove_tx_key_images},
TxPoolWriteError,
},
tables::TablesMut,
types::{TransactionHash, TransactionInfo, TxStateFlags},
};
pub fn add_transaction(
tx: &TransactionVerificationData,
state_stem: bool,
tables: &mut impl TablesMut,
) -> Result<(), TxPoolWriteError> {
tables
.transaction_blobs_mut()
.put(&tx.tx_hash, StorableVec::wrap_ref(&tx.tx_blob))?;
let mut flags = TxStateFlags::empty();
flags.set(TxStateFlags::STATE_STEM, state_stem);
tables.transaction_infos_mut().put(
&tx.tx_hash,
&TransactionInfo {
fee: tx.fee,
weight: tx.tx_weight,
flags,
_padding: [0; 7],
},
)?;
let cached_verification_state = (*tx.cached_verification_state.lock().unwrap()).into();
tables
.cached_verification_state_mut()
.put(&tx.tx_hash, &cached_verification_state)?;
let kis_table = tables.spent_key_images_mut();
add_tx_key_images(&tx.tx.prefix().inputs, &tx.tx_hash, kis_table)?;
let blob_hash = transaction_blob_hash(&tx.tx_blob);
tables
.known_blob_hashes_mut()
.put(&blob_hash, &tx.tx_hash)?;
Ok(())
}
pub fn remove_transaction(
tx_hash: &TransactionHash,
tables: &mut impl TablesMut,
) -> Result<(), RuntimeError> {
let tx_blob = tables.transaction_blobs_mut().take(tx_hash)?.0;
tables.transaction_infos_mut().delete(tx_hash)?;
tables.cached_verification_state_mut().delete(tx_hash)?;
let tx = Transaction::<NotPruned>::read(&mut tx_blob.as_slice())
.expect("Tx in the tx-pool must be parseable");
let kis_table = tables.spent_key_images_mut();
remove_tx_key_images(&tx.prefix().inputs, kis_table)?;
let blob_hash = transaction_blob_hash(&tx_blob);
tables.known_blob_hashes_mut().delete(&blob_hash)?;
Ok(())
}