diff --git a/binaries/cuprated/src/rpc/handler.rs b/binaries/cuprated/src/rpc/handler.rs index 7c8c7eb..b021dcb 100644 --- a/binaries/cuprated/src/rpc/handler.rs +++ b/binaries/cuprated/src/rpc/handler.rs @@ -3,13 +3,11 @@ use std::task::{Context, Poll}; use anyhow::Error; -use futures::{channel::oneshot::channel, future::BoxFuture}; -use serde::{Deserialize, Serialize}; +use futures::future::BoxFuture; +use monero_serai::block::Block; use tower::Service; use cuprate_blockchain::service::{BlockchainReadHandle, BlockchainWriteHandle}; -use cuprate_helper::asynch::InfallibleOneshotReceiver; -use cuprate_json_rpc::Id; use cuprate_rpc_interface::RpcHandler; use cuprate_rpc_types::{ bin::{BinRequest, BinResponse}, @@ -20,6 +18,82 @@ use cuprate_txpool::service::{TxpoolReadHandle, TxpoolWriteHandle}; use crate::rpc::{bin, json, other}; +/// TODO: use real type when public. +#[derive(Clone)] +#[expect(clippy::large_enum_variant)] +pub enum BlockchainManagerRequest { + /// Input is the amount of blocks to pop. + PopBlocks { amount: usize }, + + /// TODO + Prune, + + /// TODO + Pruned, + + /// TODO + RelayBlock(Block), + + /// TODO + Syncing, + + /// TODO + Synced, + + /// TODO + Target, + + /// TODO + TargetHeight, +} + +/// TODO: use real type when public. +#[derive(Clone)] +pub enum BlockchainManagerResponse { + /// TODO + /// + /// Response to: + /// - [`BlockchainManagerRequest::Prune`] + /// - [`BlockchainManagerRequest::RelayBlock`] + Ok, + + /// Response to [`BlockchainManagerRequest::PopBlocks`] + /// + /// TODO + PopBlocks { new_height: usize }, + + /// Response to [`BlockchainManagerRequest::Pruned`] + /// + /// TODO + Pruned(bool), + + /// Response to [`BlockchainManagerRequest::Syncing`] + /// + /// TODO + Syncing(bool), + + /// Response to [`BlockchainManagerRequest::Synced`] + /// + /// TODO + Synced(bool), + + /// Response to [`BlockchainManagerRequest::Target`] + /// + /// TODO + Target { height: usize }, + + /// Response to [`BlockchainManagerRequest::TargetHeight`] + /// + /// TODO + TargetHeight { height: usize }, +} + +/// TODO: use real type when public. +pub type BlockchainManagerHandle = cuprate_database_service::DatabaseReadService< + BlockchainManagerRequest, + BlockchainManagerResponse, +>; + /// TODO #[derive(Clone)] pub struct CupratedRpcHandler { @@ -43,6 +117,9 @@ pub struct CupratedRpcHandlerState { /// Write handle to the blockchain database. pub blockchain_write: BlockchainWriteHandle, + /// Handle to the blockchain manager. + pub blockchain_manager: BlockchainManagerHandle, + /// Read handle to the transaction pool database. pub txpool_read: TxpoolReadHandle, diff --git a/binaries/cuprated/src/rpc/request/address_book.rs b/binaries/cuprated/src/rpc/request/address_book.rs index 78ed5fb..d939dd4 100644 --- a/binaries/cuprated/src/rpc/request/address_book.rs +++ b/binaries/cuprated/src/rpc/request/address_book.rs @@ -1,31 +1,15 @@ //! Functions for TODO: doc enum message. -use std::{ - collections::{HashMap, HashSet}, - convert::Infallible, - sync::Arc, -}; +use std::convert::Infallible; -use anyhow::{anyhow, Error}; -use futures::StreamExt; -use monero_serai::block::Block; -use tower::{Service, ServiceExt}; +use anyhow::Error; +use tower::ServiceExt; -use cuprate_helper::{ - cast::{u64_to_usize, usize_to_u64}, - map::split_u128_into_low_high_bits, -}; +use cuprate_helper::cast::usize_to_u64; use cuprate_p2p_core::{ - client::handshaker::builder::DummyAddressBook, services::{AddressBookRequest, AddressBookResponse}, - AddressBook, ClearNet, NetworkZone, + AddressBook, NetworkZone, }; -use cuprate_types::{ - blockchain::{BlockchainReadRequest, BlockchainWriteRequest}, - Chain, ExtendedBlockHeader, HardFork, OutputOnChain, VerifiedBlockInformation, -}; - -use crate::rpc::{CupratedRpcHandler, CupratedRpcHandlerState}; /// [`AddressBookRequest::PeerlistSize`] pub(super) async fn peerlist_size( diff --git a/binaries/cuprated/src/rpc/request/blockchain.rs b/binaries/cuprated/src/rpc/request/blockchain.rs index 59a443f..7bc6012 100644 --- a/binaries/cuprated/src/rpc/request/blockchain.rs +++ b/binaries/cuprated/src/rpc/request/blockchain.rs @@ -3,26 +3,18 @@ use std::{ collections::{HashMap, HashSet}, ops::Range, - sync::Arc, }; -use anyhow::{anyhow, Error}; +use anyhow::Error; use cuprate_blockchain::service::BlockchainReadHandle; -use futures::StreamExt; -use monero_serai::block::Block; use tower::{Service, ServiceExt}; -use cuprate_helper::{ - cast::{u64_to_usize, usize_to_u64}, - map::split_u128_into_low_high_bits, -}; +use cuprate_helper::cast::{u64_to_usize, usize_to_u64}; use cuprate_types::{ - blockchain::{BlockchainReadRequest, BlockchainResponse, BlockchainWriteRequest}, - Chain, ExtendedBlockHeader, HardFork, OutputOnChain, VerifiedBlockInformation, + blockchain::{BlockchainReadRequest, BlockchainResponse}, + Chain, ExtendedBlockHeader, OutputOnChain, }; -use crate::rpc::CupratedRpcHandlerState; - /// [`BlockchainReadRequest::BlockExtendedHeader`]. pub(super) async fn block_extended_header( mut blockchain_read: BlockchainReadHandle, diff --git a/binaries/cuprated/src/rpc/request/blockchain_context.rs b/binaries/cuprated/src/rpc/request/blockchain_context.rs index 1610069..1cf69d6 100644 --- a/binaries/cuprated/src/rpc/request/blockchain_context.rs +++ b/binaries/cuprated/src/rpc/request/blockchain_context.rs @@ -1,32 +1,15 @@ //! Functions for [`BlockChainContextRequest`] and [`BlockChainContextResponse`]. -use std::{ - collections::{HashMap, HashSet}, - convert::Infallible, - ops::Range, - sync::Arc, -}; +use std::convert::Infallible; -use anyhow::{anyhow, Error}; -use futures::StreamExt; -use monero_serai::block::Block; -use randomx_rs::RandomXVM; +use anyhow::Error; use tower::{Service, ServiceExt}; use cuprate_consensus::context::{ BlockChainContext, BlockChainContextRequest, BlockChainContextResponse, BlockChainContextService, }; -use cuprate_helper::{ - cast::{u64_to_usize, usize_to_u64}, - map::split_u128_into_low_high_bits, -}; -use cuprate_types::{ - blockchain::{BlockchainReadRequest, BlockchainResponse, BlockchainWriteRequest}, - Chain, ExtendedBlockHeader, HardFork, OutputOnChain, VerifiedBlockInformation, -}; - -use crate::rpc::CupratedRpcHandlerState; +use cuprate_types::HardFork; /// [`BlockChainContextRequest::Context`]. pub(super) async fn context( diff --git a/binaries/cuprated/src/rpc/request/blockchain_manager.rs b/binaries/cuprated/src/rpc/request/blockchain_manager.rs index 652855b..c254b5a 100644 --- a/binaries/cuprated/src/rpc/request/blockchain_manager.rs +++ b/binaries/cuprated/src/rpc/request/blockchain_manager.rs @@ -1,63 +1,139 @@ //! Functions for TODO: doc enum message. -use std::{ - collections::{HashMap, HashSet}, - sync::Arc, -}; - -use anyhow::{anyhow, Error}; -use futures::StreamExt; +use anyhow::Error; use monero_serai::block::Block; use tower::{Service, ServiceExt}; -use cuprate_consensus::BlockchainResponse; -use cuprate_helper::{ - cast::{u64_to_usize, usize_to_u64}, - map::split_u128_into_low_high_bits, -}; -use cuprate_types::{ - blockchain::{BlockchainReadRequest, BlockchainWriteRequest}, - Chain, ExtendedBlockHeader, HardFork, OutputOnChain, VerifiedBlockInformation, +use cuprate_helper::cast::{u64_to_usize, usize_to_u64}; + +use crate::rpc::handler::{ + BlockchainManagerHandle, BlockchainManagerRequest, BlockchainManagerResponse, }; -use crate::rpc::{CupratedRpcHandler, CupratedRpcHandlerState}; +/// [`BlockchainManagerResponse::PopBlocks`] +pub(super) async fn pop_blocks( + blockchain_manager: &mut BlockchainManagerHandle, + amount: u64, +) -> Result { + let BlockchainManagerResponse::PopBlocks { new_height } = blockchain_manager + .ready() + .await? + .call(BlockchainManagerRequest::PopBlocks { + amount: u64_to_usize(amount), + }) + .await? + else { + unreachable!(); + }; -/// TODO: doc enum message -pub(super) async fn pop_blocks() -> Result<(u64, [u8; 32]), Error> { - Ok(todo!()) + Ok(usize_to_u64(new_height)) } -/// TODO: doc enum message -pub(super) async fn prune() -> Result<(), Error> { - Ok(todo!()) +/// [`BlockchainManagerResponse::Prune`] +pub(super) async fn prune(blockchain_manager: &mut BlockchainManagerHandle) -> Result<(), Error> { + let BlockchainManagerResponse::Ok = blockchain_manager + .ready() + .await? + .call(BlockchainManagerRequest::Prune) + .await? + else { + unreachable!(); + }; + + Ok(()) } -/// TODO: doc enum message -pub(super) async fn pruned() -> Result { - Ok(todo!()) +/// [`BlockchainManagerResponse::Pruned`] +pub(super) async fn pruned( + blockchain_manager: &mut BlockchainManagerHandle, +) -> Result { + let BlockchainManagerResponse::Pruned(pruned) = blockchain_manager + .ready() + .await? + .call(BlockchainManagerRequest::Pruned) + .await? + else { + unreachable!(); + }; + + Ok(pruned) } -/// TODO: doc enum message -pub(super) async fn relay_block() -> Result { - Ok(todo!()) +/// [`BlockchainManagerResponse::RelayBlock`] +pub(super) async fn relay_block( + blockchain_manager: &mut BlockchainManagerHandle, + block: Block, +) -> Result<(), Error> { + let BlockchainManagerResponse::Ok = blockchain_manager + .ready() + .await? + .call(BlockchainManagerRequest::RelayBlock(block)) + .await? + else { + unreachable!(); + }; + + Ok(()) } -/// TODO: doc enum message -pub(super) async fn syncing() -> Result { - Ok(todo!()) +/// [`BlockchainManagerResponse::Syncing`] +pub(super) async fn syncing( + blockchain_manager: &mut BlockchainManagerHandle, +) -> Result { + let BlockchainManagerResponse::Syncing(syncing) = blockchain_manager + .ready() + .await? + .call(BlockchainManagerRequest::Syncing) + .await? + else { + unreachable!(); + }; + + Ok(syncing) } -/// TODO: doc enum message -pub(super) async fn synced() -> Result { - Ok(todo!()) +/// [`BlockchainManagerResponse::Synced`] +pub(super) async fn synced( + blockchain_manager: &mut BlockchainManagerHandle, +) -> Result { + let BlockchainManagerResponse::Synced(syncing) = blockchain_manager + .ready() + .await? + .call(BlockchainManagerRequest::Synced) + .await? + else { + unreachable!(); + }; + + Ok(syncing) } -/// TODO: doc enum message -pub(super) async fn target() -> Result { - Ok(todo!()) +/// [`BlockchainManagerResponse::Target`] +pub(super) async fn target(blockchain_manager: &mut BlockchainManagerHandle) -> Result { + let BlockchainManagerResponse::Target { height } = blockchain_manager + .ready() + .await? + .call(BlockchainManagerRequest::Target) + .await? + else { + unreachable!(); + }; + + Ok(usize_to_u64(height)) } -/// TODO: doc enum message -pub(super) async fn target_height() -> Result { - Ok(todo!()) +/// [`BlockchainManagerResponse::TargetHeight`] +pub(super) async fn target_height( + blockchain_manager: &mut BlockchainManagerHandle, +) -> Result { + let BlockchainManagerResponse::TargetHeight { height } = blockchain_manager + .ready() + .await? + .call(BlockchainManagerRequest::TargetHeight) + .await? + else { + unreachable!(); + }; + + Ok(usize_to_u64(height)) } diff --git a/binaries/cuprated/src/rpc/request/txpool.rs b/binaries/cuprated/src/rpc/request/txpool.rs index 517661e..71fa834 100644 --- a/binaries/cuprated/src/rpc/request/txpool.rs +++ b/binaries/cuprated/src/rpc/request/txpool.rs @@ -1,32 +1,15 @@ //! Functions for TODO: doc enum message. -use std::{ - collections::{HashMap, HashSet}, - convert::Infallible, - sync::Arc, -}; +use std::convert::Infallible; -use anyhow::{anyhow, Error}; -use cuprate_database_service::DatabaseReadService; -use futures::StreamExt; -use monero_serai::block::Block; +use anyhow::Error; use tower::{Service, ServiceExt}; -use cuprate_consensus::BlockchainResponse; -use cuprate_helper::{ - cast::{u64_to_usize, usize_to_u64}, - map::split_u128_into_low_high_bits, -}; +use cuprate_helper::cast::usize_to_u64; use cuprate_txpool::service::{ interface::{TxpoolReadRequest, TxpoolReadResponse}, TxpoolReadHandle, }; -use cuprate_types::{ - blockchain::{BlockchainReadRequest, BlockchainWriteRequest}, - Chain, ExtendedBlockHeader, HardFork, OutputOnChain, VerifiedBlockInformation, -}; - -use crate::rpc::{CupratedRpcHandler, CupratedRpcHandlerState}; /// [`TxpoolReadRequest::Backlog`] pub(super) async fn backlog(txpool_read: &mut TxpoolReadHandle) -> Result, Error> {