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/// An Error returned from one of the consensus services.
41#[derive(Debug, thiserror::Error)]
42#[expect(variant_size_differences)]
43pub enum ExtendedConsensusError {
44    /// A consensus error.
45    #[error("{0}")]
46    ConErr(#[from] ConsensusError),
47    /// A database error.
48    #[error("Database error: {0}")]
49    DBErr(#[from] tower::BoxError),
50    /// The transactions passed in with this block were not the ones needed.
51    #[error("The transactions passed in with the block are incorrect.")]
52    TxsIncludedWithBlockIncorrect,
53    /// One or more statements in the batch verifier was invalid.
54    #[error("One or more statements in the batch verifier was invalid.")]
55    OneOrMoreBatchVerificationStatementsInvalid,
56    /// A request to verify a batch of blocks had no blocks in the batch.
57    #[error("A request to verify a batch of blocks had no blocks in the batch.")]
58    NoBlocksToVerify,
59}
60
61use __private::Database;
62
63pub mod __private {
64    use cuprate_types::blockchain::{BlockchainReadRequest, BlockchainResponse};
65
66    /// A type alias trait used to represent a database, so we don't have to write [`tower::Service`] bounds
67    /// everywhere.
68    ///
69    /// Automatically implemented for:
70    /// ```ignore
71    /// tower::Service<BCReadRequest, Response = BCResponse, Error = tower::BoxError>
72    /// ```
73    pub trait Database:
74        tower::Service<
75        BlockchainReadRequest,
76        Response = BlockchainResponse,
77        Error = tower::BoxError,
78        Future: Send + 'static,
79    >
80    {
81    }
82
83    impl<
84            T: tower::Service<
85                BlockchainReadRequest,
86                Response = BlockchainResponse,
87                Error = tower::BoxError,
88                Future: Send + 'static,
89            >,
90        > Database for T
91    {
92    }
93}