Skip to main content

cuprate_consensus/
lib.rs

1//! Cuprate Consensus
2//!
3//! This crate contains Monero [`block`] and [`transactions`] verification functionality.
4//!
5//! This crate is generic over the database which is implemented as a [`tower::Service`]. To
6//! implement a database you need to have a service which accepts [`BlockchainReadRequest`] and responds
7//! with [`BlockchainResponse`].
8//!
9
10cfg_if::cfg_if! {
11    // Used in external `tests/`.
12    if #[cfg(test)] {
13        use cuprate_test_utils as _;
14        use curve25519_dalek as _;
15        use hex_literal as _;
16        use futures as _;
17    }
18}
19
20use cuprate_consensus_rules::ConsensusError;
21
22pub mod batch_verifier;
23pub mod block;
24#[cfg(test)]
25mod tests;
26pub mod transactions;
27
28pub use cuprate_consensus_context::{
29    initialize_blockchain_context, BlockChainContextRequest, BlockChainContextResponse,
30    BlockchainContext, BlockchainContextService, ContextConfig,
31};
32
33// re-export.
34pub use cuprate_consensus_rules::genesis::generate_genesis_block;
35pub use cuprate_types::{
36    blockchain::{BlockchainReadRequest, BlockchainResponse},
37    HardFork,
38};
39
40/// The verification context to verify a block or transaction with.
41#[expect(clippy::large_enum_variant)]
42pub enum VerificationContext<D> {
43    /// A full database
44    Database(D),
45    /// A batch prepared cache.
46    ///
47    /// This cache is only valid for the set of blocks it was created with, it should not be used for other blocks.
48    /// You must pass blocks in sequentially.
49    BatchPrepareCache(block::BatchPrepareCache),
50}
51
52/// An Error returned from one of the consensus services.
53#[derive(Debug, thiserror::Error)]
54pub enum ExtendedConsensusError {
55    /// A consensus error.
56    #[error("{0}")]
57    ConsensusError(#[from] ConsensusError),
58    /// A service error that we cannot recover from.
59    ///
60    /// If this happens, no more consensus verification should be done with the given inner services.
61    #[error("Fatal error: {0}")]
62    FatalError(#[from] tower::BoxError),
63}
64
65use __private::Database;
66
67pub mod __private {
68    use cuprate_types::blockchain::{BlockchainReadRequest, BlockchainResponse};
69
70    /// A type alias trait used to represent a database, so we don't have to write [`tower::Service`] bounds
71    /// everywhere.
72    ///
73    /// Automatically implemented for:
74    /// ```ignore
75    /// tower::Service<BCReadRequest, Response = BCResponse, Error = tower::BoxError>
76    /// ```
77    pub trait Database:
78        tower::Service<
79        BlockchainReadRequest,
80        Response = BlockchainResponse,
81        Error = tower::BoxError,
82        Future: Send + 'static,
83    >
84    {
85    }
86
87    impl<
88            T: tower::Service<
89                BlockchainReadRequest,
90                Response = BlockchainResponse,
91                Error = tower::BoxError,
92                Future: Send + 'static,
93            >,
94        > Database for T
95    {
96    }
97}