mirror of
https://github.com/hinto-janai/cuprate.git
synced 2025-01-22 02:34:29 +00:00
Add tx blob hash table
This commit is contained in:
parent
b6d94cf780
commit
4ba94f4bb7
6 changed files with 36 additions and 8 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -910,6 +910,7 @@ dependencies = [
|
||||||
"monero-serai",
|
"monero-serai",
|
||||||
"rayon",
|
"rayon",
|
||||||
"serde",
|
"serde",
|
||||||
|
"sha3",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
"thiserror",
|
"thiserror",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
|
|
@ -29,6 +29,7 @@ bytemuck = { workspace = true, features = ["must_cast", "derive"
|
||||||
bitflags = { workspace = true, features = ["std", "serde", "bytemuck"] }
|
bitflags = { workspace = true, features = ["std", "serde", "bytemuck"] }
|
||||||
thiserror = { workspace = true }
|
thiserror = { workspace = true }
|
||||||
hex = { workspace = true }
|
hex = { workspace = true }
|
||||||
|
sha3 = { workspace = true, features = ["std"] }
|
||||||
|
|
||||||
tower = { workspace = true, optional = true }
|
tower = { workspace = true, optional = true }
|
||||||
rayon = { workspace = true, optional = true }
|
rayon = { workspace = true, optional = true }
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
//! General free functions (related to the tx-pool database).
|
//! General free functions (related to the tx-pool database).
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------- Import
|
//---------------------------------------------------------------------------------------------------- Import
|
||||||
|
use sha3::{Digest, Sha3_256};
|
||||||
|
|
||||||
use cuprate_database::{ConcreteEnv, Env, EnvInner, InitError, RuntimeError, TxRw};
|
use cuprate_database::{ConcreteEnv, Env, EnvInner, InitError, RuntimeError, TxRw};
|
||||||
|
|
||||||
use crate::{config::Config, tables::OpenTables};
|
use crate::{config::Config, tables::OpenTables, types::TransactionBlobHash};
|
||||||
|
|
||||||
//---------------------------------------------------------------------------------------------------- Free functions
|
//---------------------------------------------------------------------------------------------------- Free functions
|
||||||
/// Open the txpool database using the passed [`Config`].
|
/// Open the txpool database using the passed [`Config`].
|
||||||
|
@ -60,3 +62,15 @@ pub fn open(config: Config) -> Result<ConcreteEnv, InitError> {
|
||||||
|
|
||||||
Ok(env)
|
Ok(env)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Calculate the transaction blob hash.
|
||||||
|
///
|
||||||
|
/// This value is supposed to be quick to compute just based of the tx-blob without needing to parse the tx.
|
||||||
|
///
|
||||||
|
/// The exact way the hash is calculated is not stable and is subject to change, as such it should not be exposed
|
||||||
|
/// as a way to interact with Cuprate externally.
|
||||||
|
pub fn transaction_blob_hash(tx_blob: &[u8]) -> TransactionBlobHash {
|
||||||
|
let mut hasher = Sha3_256::new();
|
||||||
|
hasher.update(tx_blob);
|
||||||
|
hasher.finalize().into()
|
||||||
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ use cuprate_database::{DatabaseRw, RuntimeError, StorableVec};
|
||||||
use cuprate_types::TransactionVerificationData;
|
use cuprate_types::TransactionVerificationData;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
free::transaction_blob_hash,
|
||||||
ops::{
|
ops::{
|
||||||
key_images::{add_tx_key_images, remove_tx_key_images},
|
key_images::{add_tx_key_images, remove_tx_key_images},
|
||||||
TxPoolWriteError,
|
TxPoolWriteError,
|
||||||
|
@ -56,6 +57,10 @@ pub fn add_transaction(
|
||||||
let kis_table = tables.spent_key_images_mut();
|
let kis_table = tables.spent_key_images_mut();
|
||||||
add_tx_key_images(&tx.tx.prefix().inputs, &tx.tx_hash, kis_table)?;
|
add_tx_key_images(&tx.tx.prefix().inputs, &tx.tx_hash, kis_table)?;
|
||||||
|
|
||||||
|
// Add the blob hash to table 4.
|
||||||
|
let blob_hash = transaction_blob_hash(&tx.tx_blob);
|
||||||
|
tables.known_blob_hashes_mut().put(&blob_hash, &())?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,5 +84,9 @@ pub fn remove_transaction(
|
||||||
let kis_table = tables.spent_key_images_mut();
|
let kis_table = tables.spent_key_images_mut();
|
||||||
remove_tx_key_images(&tx.prefix().inputs, kis_table)?;
|
remove_tx_key_images(&tx.prefix().inputs, kis_table)?;
|
||||||
|
|
||||||
|
// Remove the blob hash from table 4.
|
||||||
|
let blob_hash = transaction_blob_hash(&tx_blob);
|
||||||
|
tables.known_blob_hashes_mut().delete(&blob_hash)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ pub enum TxpoolReadRequest {
|
||||||
/// A request for the [`TransactionVerificationData`] of a transaction in the tx pool.
|
/// A request for the [`TransactionVerificationData`] of a transaction in the tx pool.
|
||||||
TxVerificationData(TransactionHash),
|
TxVerificationData(TransactionHash),
|
||||||
/// A request to filter (remove) all **known** transactions from the set.
|
/// A request to filter (remove) all **known** transactions from the set.
|
||||||
///
|
///
|
||||||
/// The hash is **not** the transaction hash, it is the hash of the serialized tx-blob.
|
/// The hash is **not** the transaction hash, it is the hash of the serialized tx-blob.
|
||||||
FilterKnownTxBlobHashes(HashSet<TransactionBlobHash>),
|
FilterKnownTxBlobHashes(HashSet<TransactionBlobHash>),
|
||||||
}
|
}
|
||||||
|
@ -26,10 +26,7 @@ pub enum TxpoolReadRequest {
|
||||||
#[expect(clippy::large_enum_variant)]
|
#[expect(clippy::large_enum_variant)]
|
||||||
pub enum TxpoolReadResponse {
|
pub enum TxpoolReadResponse {
|
||||||
/// A response containing the raw bytes of a transaction.
|
/// A response containing the raw bytes of a transaction.
|
||||||
TxBlob {
|
TxBlob { tx_blob: Vec<u8>, state_stem: bool },
|
||||||
tx_blob: Vec<u8>,
|
|
||||||
state_stem: bool,
|
|
||||||
},
|
|
||||||
/// A response of [`TransactionVerificationData`].
|
/// A response of [`TransactionVerificationData`].
|
||||||
TxVerificationData(TransactionVerificationData),
|
TxVerificationData(TransactionVerificationData),
|
||||||
/// The response for [`TxpoolReadRequest::FilterKnownTxBlobHashes`].
|
/// The response for [`TxpoolReadRequest::FilterKnownTxBlobHashes`].
|
||||||
|
|
|
@ -16,7 +16,9 @@
|
||||||
//! accessing _all_ tables defined here at once.
|
//! accessing _all_ tables defined here at once.
|
||||||
use cuprate_database::{define_tables, StorableVec};
|
use cuprate_database::{define_tables, StorableVec};
|
||||||
|
|
||||||
use crate::types::{KeyImage, RawCachedVerificationState, TransactionHash, TransactionInfo};
|
use crate::types::{
|
||||||
|
KeyImage, RawCachedVerificationState, TransactionBlobHash, TransactionHash, TransactionInfo,
|
||||||
|
};
|
||||||
|
|
||||||
define_tables! {
|
define_tables! {
|
||||||
/// Serialized transaction blobs.
|
/// Serialized transaction blobs.
|
||||||
|
@ -41,5 +43,9 @@ define_tables! {
|
||||||
///
|
///
|
||||||
/// This table contains the spent key images from all transactions in the pool.
|
/// This table contains the spent key images from all transactions in the pool.
|
||||||
3 => SpentKeyImages,
|
3 => SpentKeyImages,
|
||||||
KeyImage => TransactionHash
|
KeyImage => TransactionHash,
|
||||||
|
|
||||||
|
/// Transaction blob hashes that are in the pool.
|
||||||
|
4 => KnownBlobHashes,
|
||||||
|
TransactionBlobHash => (),
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue