blockchain manager msg types

This commit is contained in:
hinto.janai 2024-09-26 20:14:48 -04:00
parent a1b3bdabef
commit cf3095446e
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
6 changed files with 212 additions and 117 deletions

View file

@ -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,

View file

@ -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<Z: NetworkZone>(

View file

@ -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,

View file

@ -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(

View file

@ -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<u64, Error> {
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<bool, Error> {
Ok(todo!())
/// [`BlockchainManagerResponse::Pruned`]
pub(super) async fn pruned(
blockchain_manager: &mut BlockchainManagerHandle,
) -> Result<bool, Error> {
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<bool, Error> {
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<bool, Error> {
Ok(todo!())
/// [`BlockchainManagerResponse::Syncing`]
pub(super) async fn syncing(
blockchain_manager: &mut BlockchainManagerHandle,
) -> Result<bool, Error> {
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<bool, Error> {
Ok(todo!())
/// [`BlockchainManagerResponse::Synced`]
pub(super) async fn synced(
blockchain_manager: &mut BlockchainManagerHandle,
) -> Result<bool, Error> {
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<bool, Error> {
Ok(todo!())
/// [`BlockchainManagerResponse::Target`]
pub(super) async fn target(blockchain_manager: &mut BlockchainManagerHandle) -> Result<u64, Error> {
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<bool, Error> {
Ok(todo!())
/// [`BlockchainManagerResponse::TargetHeight`]
pub(super) async fn target_height(
blockchain_manager: &mut BlockchainManagerHandle,
) -> Result<u64, Error> {
let BlockchainManagerResponse::TargetHeight { height } = blockchain_manager
.ready()
.await?
.call(BlockchainManagerRequest::TargetHeight)
.await?
else {
unreachable!();
};
Ok(usize_to_u64(height))
}

View file

@ -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<Vec<Infallible>, Error> {