Compare commits

..

3 commits

Author SHA1 Message Date
hinto.janai
02a99f3bb9
add DB fn signatures 2024-09-26 20:37:32 -04:00
hinto.janai
cf3095446e
blockchain manager msg types 2024-09-26 20:14:48 -04:00
hinto.janai
a1b3bdabef
blockchain_manager msgs 2024-09-25 15:47:32 -04:00
12 changed files with 320 additions and 112 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,38 +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)
}
/// [`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(())
}
/// [`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)
}
/// [`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)
}
/// [`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))
}
/// [`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> {

View file

@ -327,7 +327,7 @@ impl<D: Database + Clone + Send + 'static> ContextTask<D> {
}
BlockChainContextRequest::HardForkInfo(_)
| BlockChainContextRequest::FeeEstimate { .. } => {
todo!()
todo!("finish https://github.com/Cuprate/cuprate/pull/297")
}
})
}

View file

@ -415,7 +415,7 @@ impl<Z: BorshNetworkZone> Service<AddressBookRequest<Z>> for AddressBook<Z> {
| AddressBookRequest::ConnectionCount
| AddressBookRequest::SetBan(_)
| AddressBookRequest::GetBan(_)
| AddressBookRequest::GetBans => todo!(),
| AddressBookRequest::GetBans => todo!("finish https://github.com/Cuprate/cuprate/pull/297"),
};
ready(response)

View file

@ -132,7 +132,7 @@ impl<N: NetworkZone> Service<AddressBookRequest<N>> for DummyAddressBook {
| AddressBookRequest::ConnectionCount
| AddressBookRequest::SetBan(_)
| AddressBookRequest::GetBan(_)
| AddressBookRequest::GetBans => todo!(),
| AddressBookRequest::GetBans => todo!("finish https://github.com/Cuprate/cuprate/pull/297"),
}))
}
}

View file

@ -1,5 +1,12 @@
//! Database reader thread-pool definitions and logic.
#![expect(
unreachable_code,
unused_variables,
clippy::unnecessary_wraps,
reason = "TODO: finish implementing the signatures from <https://github.com/Cuprate/cuprate/pull/297>"
)]
//---------------------------------------------------------------------------------------------------- Import
use std::{
collections::{HashMap, HashSet},
@ -107,12 +114,14 @@ fn map_request(
R::CompactChainHistory => compact_chain_history(env),
R::FindFirstUnknown(block_ids) => find_first_unknown(env, &block_ids),
R::AltBlocksInChain(chain_id) => alt_blocks_in_chain(env, chain_id),
R::TotalTxCount
| R::DatabaseSize
| R::Difficulty(_)
| R::OutputHistogram
| R::CoinbaseTxSum
| R::MinerData => todo!(),
R::Block(height) => block(env, height),
R::BlockByHash(hash) => block_by_hash(env, hash),
R::TotalTxCount => total_tx_count(env),
R::DatabaseSize => database_size(env),
R::Difficulty(height) => difficulty(env, height),
R::OutputHistogram => output_histogram(env),
R::CoinbaseTxSum => coinbase_tx_sum(env),
R::MinerData => miner_data(env),
}
/* SOMEDAY: post-request handling, run some code for each request? */
@ -607,3 +616,46 @@ fn alt_blocks_in_chain(env: &ConcreteEnv, chain_id: ChainId) -> ResponseResult {
Ok(BlockchainResponse::AltBlocksInChain(blocks))
}
/// [`BlockchainReadRequest::Block`]
fn block(env: &ConcreteEnv, block_height: BlockHeight) -> ResponseResult {
Ok(BlockchainResponse::Block(todo!()))
}
/// [`BlockchainReadRequest::BlockByHash`]
fn block_by_hash(env: &ConcreteEnv, block_hash: BlockHash) -> ResponseResult {
Ok(BlockchainResponse::Block(todo!()))
}
/// [`BlockchainReadRequest::TotalTxCount`]
fn total_tx_count(env: &ConcreteEnv) -> ResponseResult {
Ok(BlockchainResponse::TotalTxCount(todo!()))
}
/// [`BlockchainReadRequest::DatabaseSize`]
fn database_size(env: &ConcreteEnv) -> ResponseResult {
Ok(BlockchainResponse::DatabaseSize {
database_size: todo!(),
free_space: todo!(),
})
}
/// [`BlockchainReadRequest::Difficulty()`]
fn difficulty(env: &ConcreteEnv, block_height: BlockHeight) -> ResponseResult {
Ok(BlockchainResponse::Difficulty(todo!()))
}
/// [`BlockchainReadRequest::OutputHistogram`]
fn output_histogram(env: &ConcreteEnv) -> ResponseResult {
Ok(BlockchainResponse::OutputHistogram(todo!()))
}
/// [`BlockchainReadRequest::CoinbaseTxSum`]
fn coinbase_tx_sum(env: &ConcreteEnv) -> ResponseResult {
Ok(BlockchainResponse::CoinbaseTxSum(todo!()))
}
/// [`BlockchainReadRequest::MinerData`]
fn miner_data(env: &ConcreteEnv) -> ResponseResult {
Ok(BlockchainResponse::MinerData(todo!()))
}

View file

@ -1,3 +1,10 @@
#![expect(
unreachable_code,
unused_variables,
clippy::unnecessary_wraps,
reason = "TODO: finish implementing the signatures from <https://github.com/Cuprate/cuprate/pull/297>"
)]
use std::sync::Arc;
use rayon::ThreadPool;
@ -58,7 +65,8 @@ fn map_request(
match request {
TxpoolReadRequest::TxBlob(tx_hash) => tx_blob(env, &tx_hash),
TxpoolReadRequest::TxVerificationData(tx_hash) => tx_verification_data(env, &tx_hash),
TxpoolReadRequest::Backlog | TxpoolReadRequest::Size => todo!(),
TxpoolReadRequest::Backlog => backlog(env),
TxpoolReadRequest::Size => size(env),
}
}
@ -102,3 +110,15 @@ fn tx_verification_data(env: &ConcreteEnv, tx_hash: &TransactionHash) -> ReadRes
get_transaction_verification_data(tx_hash, &tables).map(TxpoolReadResponse::TxVerificationData)
}
/// [`TxpoolReadRequest::Backlog`].
#[inline]
fn backlog(env: &ConcreteEnv) -> ReadResponseResult {
Ok(TxpoolReadResponse::Backlog(todo!()))
}
/// [`TxpoolReadRequest::Size`].
#[inline]
fn size(env: &ConcreteEnv) -> ReadResponseResult {
Ok(TxpoolReadResponse::Size(todo!()))
}

View file

@ -8,6 +8,8 @@ use std::{
ops::Range,
};
use monero_serai::block::Block;
use crate::{
types::{Chain, ExtendedBlockHeader, OutputOnChain, VerifiedBlockInformation},
AltBlockInformation, ChainId,
@ -104,6 +106,12 @@ pub enum BlockchainReadRequest {
/// A request for all alt blocks in the chain with the given [`ChainId`].
AltBlocksInChain(ChainId),
/// TODO
Block(usize),
/// TODO
BlockByHash([u8; 32]),
/// TODO
TotalTxCount,
@ -165,6 +173,7 @@ pub enum BlockchainWriteRequest {
/// This pairs with [`BlockchainReadRequest`] and [`BlockchainWriteRequest`],
/// see those two for more info.
#[derive(Debug, Clone, PartialEq, Eq)]
#[expect(clippy::large_enum_variant)]
pub enum BlockchainResponse {
//------------------------------------------------------ Reads
/// Response to [`BlockchainReadRequest::BlockExtendedHeader`].
@ -245,6 +254,13 @@ pub enum BlockchainResponse {
/// Contains all the alt blocks in the alt-chain in chronological order.
AltBlocksInChain(Vec<AltBlockInformation>),
/// The response for:
/// - [`BlockchainReadRequest::Block`].
/// - [`BlockchainReadRequest::BlockByHash`].
///
/// TODO
Block(Block),
/// The response for [`BlockchainReadRequest::TotalTxCount`].
///
/// TODO