cuprate/consensus/src/lib.rs

171 lines
4.4 KiB
Rust
Raw Normal View History

use std::{
collections::{HashMap, HashSet},
future::Future,
sync::Arc,
};
2023-10-20 00:04:26 +00:00
use monero_consensus::{transactions::OutputOnChain, ConsensusError, HardFork};
2023-12-03 00:29:12 +00:00
//mod batch_verifier;
mod batch_verifier;
pub mod block;
pub mod context;
mod helper;
pub mod randomx;
#[cfg(feature = "binaries")]
2023-09-03 22:50:38 +00:00
pub mod rpc;
#[cfg(test)]
mod test_utils;
2023-10-20 00:04:26 +00:00
pub mod transactions;
2023-11-11 01:55:15 +00:00
pub use block::{
PrePreparedBlock, VerifiedBlockInformation, VerifyBlockRequest, VerifyBlockResponse,
};
pub use context::{
initialize_blockchain_context, BlockChainContext, BlockChainContextRequest,
BlockChainContextResponse, ContextConfig,
};
2023-10-23 18:14:40 +00:00
pub use transactions::{VerifyTxRequest, VerifyTxResponse};
#[derive(Debug, thiserror::Error)]
pub enum ExtendedConsensusError {
#[error("{0}")]
ConErr(#[from] monero_consensus::ConsensusError),
#[error("Database error: {0}")]
DBErr(#[from] tower::BoxError),
#[error("Needed transaction is not in pool")]
TxPErr(#[from] TxNotInPool),
}
// TODO: instead of (ab)using generic returns return the acc type
pub async fn initialize_verifier<D, TxP, Ctx>(
database: D,
tx_pool: TxP,
ctx_svc: Ctx,
) -> Result<
(
2023-10-23 18:14:40 +00:00
impl tower::Service<
2023-11-11 01:55:15 +00:00
VerifyBlockRequest,
Response = VerifyBlockResponse,
Error = ExtendedConsensusError,
Future = impl Future<Output = Result<VerifyBlockResponse, ExtendedConsensusError>>
2023-11-11 01:55:15 +00:00
+ Send
+ 'static,
> + Clone
+ Send
+ 'static,
impl tower::Service<
VerifyTxRequest,
Response = VerifyTxResponse,
Error = ExtendedConsensusError,
Future = impl Future<Output = Result<VerifyTxResponse, ExtendedConsensusError>>
+ Send
+ 'static,
> + Clone
+ Send
+ 'static,
),
ConsensusError,
>
where
D: Database + Clone + Send + Sync + 'static,
D::Future: Send + 'static,
TxP: tower::Service<TxPoolRequest, Response = TxPoolResponse, Error = TxNotInPool>
+ Clone
+ Send
+ Sync
+ 'static,
TxP::Future: Send + 'static,
Ctx: tower::Service<
BlockChainContextRequest,
Response = BlockChainContextResponse,
Error = tower::BoxError,
> + Clone
+ Send
+ Sync
+ 'static,
Ctx::Future: Send + 'static,
{
let tx_svc = transactions::TxVerifierService::new(database);
let block_svc = block::BlockVerifierService::new(ctx_svc, tx_svc.clone(), tx_pool);
Ok((block_svc, tx_svc))
}
2023-09-03 22:50:38 +00:00
pub trait Database:
tower::Service<DatabaseRequest, Response = DatabaseResponse, Error = tower::BoxError>
{
}
impl<T: tower::Service<DatabaseRequest, Response = DatabaseResponse, Error = tower::BoxError>>
Database for T
{
}
#[derive(Debug, Copy, Clone)]
pub struct ExtendedBlockHeader {
pub version: HardFork,
pub vote: HardFork,
pub timestamp: u64,
pub cumulative_difficulty: u128,
pub block_weight: usize,
pub long_term_weight: usize,
}
#[derive(Debug, Clone)]
2023-09-03 22:50:38 +00:00
pub enum DatabaseRequest {
BlockExtendedHeader(cuprate_common::BlockID),
2023-10-05 16:54:19 +00:00
BlockHash(u64),
BlockExtendedHeaderInRange(std::ops::Range<u64>),
2023-09-03 22:50:38 +00:00
ChainHeight,
2023-10-23 18:14:40 +00:00
GeneratedCoins,
2023-10-20 00:04:26 +00:00
Outputs(HashMap<u64, HashSet<u64>>),
NumberOutputsWithAmount(Vec<u64>),
CheckKIsNotSpent(HashSet<[u8; 32]>),
2023-10-20 00:04:26 +00:00
#[cfg(feature = "binaries")]
BlockBatchInRange(std::ops::Range<u64>),
2023-09-03 22:50:38 +00:00
}
#[derive(Debug)]
pub enum DatabaseResponse {
BlockExtendedHeader(ExtendedBlockHeader),
2023-10-05 16:54:19 +00:00
BlockHash([u8; 32]),
BlockExtendedHeaderInRange(Vec<ExtendedBlockHeader>),
ChainHeight(u64, [u8; 32]),
2023-10-23 18:14:40 +00:00
GeneratedCoins(u64),
Outputs(HashMap<u64, HashMap<u64, OutputOnChain>>),
NumberOutputsWithAmount(HashMap<u64, usize>),
2023-10-20 00:04:26 +00:00
/// returns true if key images are spent
CheckKIsNotSpent(bool),
#[cfg(feature = "binaries")]
2023-10-05 16:54:19 +00:00
BlockBatchInRange(
Vec<(
monero_serai::block::Block,
Vec<monero_serai::transaction::Transaction>,
)>,
),
2023-09-03 22:50:38 +00:00
}
#[derive(Debug, Copy, Clone, thiserror::Error)]
#[error("The transaction requested was not in the transaction pool")]
pub struct TxNotInPool;
pub enum TxPoolRequest {
Transactions(Vec<[u8; 32]>),
}
pub enum TxPoolResponse {
Transactions(Vec<Arc<transactions::TransactionVerificationData>>),
}