cuprate_txpool/
free.rs

1//! General free functions (related to the tx-pool database).
2
3//---------------------------------------------------------------------------------------------------- Import
4use cuprate_database::{ConcreteEnv, Env, EnvInner, InitError, RuntimeError, TxRw};
5
6use crate::{config::Config, tables::OpenTables, types::TransactionBlobHash};
7
8//---------------------------------------------------------------------------------------------------- Free functions
9/// Open the txpool database using the passed [`Config`].
10///
11/// This calls [`cuprate_database::Env::open`] and prepares the
12/// database to be ready for txpool-related usage, e.g.
13/// table creation, table sort order, etc.
14///
15/// All tables found in [`crate::tables`] will be
16/// ready for usage in the returned [`ConcreteEnv`].
17///
18/// # Errors
19/// This will error if:
20/// - The database file could not be opened
21/// - A write transaction could not be opened
22/// - A table could not be created/opened
23#[cold]
24#[inline(never)] // only called once
25pub fn open(config: Config) -> Result<ConcreteEnv, InitError> {
26    // Attempt to open the database environment.
27    let env = <ConcreteEnv as Env>::open(config.db_config)?;
28
29    /// Convert runtime errors to init errors.
30    ///
31    /// INVARIANT:
32    /// [`cuprate_database`]'s functions mostly return the former
33    /// so we must convert them. We have knowledge of which errors
34    /// makes sense in this functions context so we panic on
35    /// unexpected ones.
36    fn runtime_to_init_error(runtime: RuntimeError) -> InitError {
37        match runtime {
38            RuntimeError::Io(io_error) => io_error.into(),
39
40            // These errors shouldn't be happening here.
41            RuntimeError::KeyExists
42            | RuntimeError::KeyNotFound
43            | RuntimeError::ResizeNeeded
44            | RuntimeError::TableNotFound => unreachable!(),
45        }
46    }
47
48    // INVARIANT: We must ensure that all tables are created,
49    // `cuprate_database` has no way of knowing _which_ tables
50    // we want since it is agnostic, so we are responsible for this.
51    {
52        let env_inner = env.env_inner();
53        let tx_rw = env_inner.tx_rw().map_err(runtime_to_init_error)?;
54
55        // Create all tables.
56        OpenTables::create_tables(&env_inner, &tx_rw).map_err(runtime_to_init_error)?;
57
58        TxRw::commit(tx_rw).map_err(runtime_to_init_error)?;
59    }
60
61    Ok(env)
62}
63
64/// Calculate the transaction blob hash.
65///
66/// This value is supposed to be quick to compute just based of the tx-blob without needing to parse the tx.
67///
68/// The exact way the hash is calculated is not stable and is subject to change, as such it should not be exposed
69/// as a way to interact with Cuprate externally.
70pub fn transaction_blob_hash(tx_blob: &[u8]) -> TransactionBlobHash {
71    blake3::hash(tx_blob).into()
72}