add blockchain msgs

This commit is contained in:
hinto.janai 2024-09-24 20:39:08 -04:00
parent dfbdec3157
commit e8cb951615
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
4 changed files with 384 additions and 359 deletions

View file

@ -7,6 +7,7 @@ use std::{
}; };
use anyhow::{anyhow, Error}; use anyhow::{anyhow, Error};
use cuprate_blockchain::service::BlockchainReadHandle;
use futures::StreamExt; use futures::StreamExt;
use monero_serai::block::Block; use monero_serai::block::Block;
use tower::{Service, ServiceExt}; use tower::{Service, ServiceExt};
@ -22,14 +23,12 @@ use cuprate_types::{
use crate::rpc::CupratedRpcHandlerState; use crate::rpc::CupratedRpcHandlerState;
impl CupratedRpcHandlerState {
/// [`BlockchainReadRequest::BlockExtendedHeader`]. /// [`BlockchainReadRequest::BlockExtendedHeader`].
pub(super) async fn block_extended_header( pub(super) async fn block_extended_header(
&mut self, mut blockchain_read: BlockchainReadHandle,
height: u64, height: u64,
) -> Result<ExtendedBlockHeader, Error> { ) -> Result<ExtendedBlockHeader, Error> {
let BlockchainResponse::BlockExtendedHeader(header) = self let BlockchainResponse::BlockExtendedHeader(header) = blockchain_read
.blockchain_read
.ready() .ready()
.await? .await?
.call(BlockchainReadRequest::BlockExtendedHeader(u64_to_usize( .call(BlockchainReadRequest::BlockExtendedHeader(u64_to_usize(
@ -45,12 +44,11 @@ impl CupratedRpcHandlerState {
/// [`BlockchainReadRequest::BlockHash`]. /// [`BlockchainReadRequest::BlockHash`].
pub(super) async fn block_hash( pub(super) async fn block_hash(
&mut self, mut blockchain_read: BlockchainReadHandle,
height: u64, height: u64,
chain: Chain, chain: Chain,
) -> Result<[u8; 32], Error> { ) -> Result<[u8; 32], Error> {
let BlockchainResponse::BlockHash(hash) = self let BlockchainResponse::BlockHash(hash) = blockchain_read
.blockchain_read
.ready() .ready()
.await? .await?
.call(BlockchainReadRequest::BlockHash( .call(BlockchainReadRequest::BlockHash(
@ -67,11 +65,10 @@ impl CupratedRpcHandlerState {
/// [`BlockchainReadRequest::FindBlock`]. /// [`BlockchainReadRequest::FindBlock`].
pub(super) async fn find_block( pub(super) async fn find_block(
&mut self, mut blockchain_read: BlockchainReadHandle,
block_hash: [u8; 32], block_hash: [u8; 32],
) -> Result<Option<(Chain, usize)>, Error> { ) -> Result<Option<(Chain, usize)>, Error> {
let BlockchainResponse::FindBlock(option) = self let BlockchainResponse::FindBlock(option) = blockchain_read
.blockchain_read
.ready() .ready()
.await? .await?
.call(BlockchainReadRequest::FindBlock(block_hash)) .call(BlockchainReadRequest::FindBlock(block_hash))
@ -85,11 +82,10 @@ impl CupratedRpcHandlerState {
/// [`BlockchainReadRequest::FilterUnknownHashes`]. /// [`BlockchainReadRequest::FilterUnknownHashes`].
pub(super) async fn filter_unknown_hashes( pub(super) async fn filter_unknown_hashes(
&mut self, mut blockchain_read: BlockchainReadHandle,
block_hashes: HashSet<[u8; 32]>, block_hashes: HashSet<[u8; 32]>,
) -> Result<HashSet<[u8; 32]>, Error> { ) -> Result<HashSet<[u8; 32]>, Error> {
let BlockchainResponse::FilterUnknownHashes(output) = self let BlockchainResponse::FilterUnknownHashes(output) = blockchain_read
.blockchain_read
.ready() .ready()
.await? .await?
.call(BlockchainReadRequest::FilterUnknownHashes(block_hashes)) .call(BlockchainReadRequest::FilterUnknownHashes(block_hashes))
@ -103,12 +99,11 @@ impl CupratedRpcHandlerState {
/// [`BlockchainReadRequest::BlockExtendedHeaderInRange`] /// [`BlockchainReadRequest::BlockExtendedHeaderInRange`]
pub(super) async fn block_extended_header_in_range( pub(super) async fn block_extended_header_in_range(
&mut self, mut blockchain_read: BlockchainReadHandle,
range: Range<usize>, range: Range<usize>,
chain: Chain, chain: Chain,
) -> Result<Vec<ExtendedBlockHeader>, Error> { ) -> Result<Vec<ExtendedBlockHeader>, Error> {
let BlockchainResponse::BlockExtendedHeaderInRange(output) = self let BlockchainResponse::BlockExtendedHeaderInRange(output) = blockchain_read
.blockchain_read
.ready() .ready()
.await? .await?
.call(BlockchainReadRequest::BlockExtendedHeaderInRange( .call(BlockchainReadRequest::BlockExtendedHeaderInRange(
@ -123,9 +118,10 @@ impl CupratedRpcHandlerState {
} }
/// [`BlockchainReadRequest::ChainHeight`]. /// [`BlockchainReadRequest::ChainHeight`].
pub(super) async fn chain_height(&mut self) -> Result<(u64, [u8; 32]), Error> { pub(super) async fn chain_height(
let BlockchainResponse::ChainHeight(height, hash) = self mut blockchain_read: BlockchainReadHandle,
.blockchain_read ) -> Result<(u64, [u8; 32]), Error> {
let BlockchainResponse::ChainHeight(height, hash) = blockchain_read
.ready() .ready()
.await? .await?
.call(BlockchainReadRequest::ChainHeight) .call(BlockchainReadRequest::ChainHeight)
@ -138,9 +134,11 @@ impl CupratedRpcHandlerState {
} }
/// [`BlockchainReadRequest::GeneratedCoins`]. /// [`BlockchainReadRequest::GeneratedCoins`].
pub(super) async fn generated_coins(&mut self, block_height: u64) -> Result<u64, Error> { pub(super) async fn generated_coins(
let BlockchainResponse::GeneratedCoins(generated_coins) = self mut blockchain_read: BlockchainReadHandle,
.blockchain_read block_height: u64,
) -> Result<u64, Error> {
let BlockchainResponse::GeneratedCoins(generated_coins) = blockchain_read
.ready() .ready()
.await? .await?
.call(BlockchainReadRequest::GeneratedCoins(u64_to_usize( .call(BlockchainReadRequest::GeneratedCoins(u64_to_usize(
@ -156,11 +154,10 @@ impl CupratedRpcHandlerState {
/// [`BlockchainReadRequest::Outputs`] /// [`BlockchainReadRequest::Outputs`]
pub(super) async fn outputs( pub(super) async fn outputs(
&mut self, mut blockchain_read: BlockchainReadHandle,
outputs: HashMap<u64, HashSet<u64>>, outputs: HashMap<u64, HashSet<u64>>,
) -> Result<HashMap<u64, HashMap<u64, OutputOnChain>>, Error> { ) -> Result<HashMap<u64, HashMap<u64, OutputOnChain>>, Error> {
let BlockchainResponse::Outputs(outputs) = self let BlockchainResponse::Outputs(outputs) = blockchain_read
.blockchain_read
.ready() .ready()
.await? .await?
.call(BlockchainReadRequest::Outputs(outputs)) .call(BlockchainReadRequest::Outputs(outputs))
@ -174,11 +171,10 @@ impl CupratedRpcHandlerState {
/// [`BlockchainReadRequest::NumberOutputsWithAmount`] /// [`BlockchainReadRequest::NumberOutputsWithAmount`]
pub(super) async fn number_outputs_with_amount( pub(super) async fn number_outputs_with_amount(
&mut self, mut blockchain_read: BlockchainReadHandle,
output_amounts: Vec<u64>, output_amounts: Vec<u64>,
) -> Result<HashMap<u64, usize>, Error> { ) -> Result<HashMap<u64, usize>, Error> {
let BlockchainResponse::NumberOutputsWithAmount(map) = self let BlockchainResponse::NumberOutputsWithAmount(map) = blockchain_read
.blockchain_read
.ready() .ready()
.await? .await?
.call(BlockchainReadRequest::NumberOutputsWithAmount( .call(BlockchainReadRequest::NumberOutputsWithAmount(
@ -194,11 +190,10 @@ impl CupratedRpcHandlerState {
/// [`BlockchainReadRequest::KeyImagesSpent`] /// [`BlockchainReadRequest::KeyImagesSpent`]
pub(super) async fn key_images_spent( pub(super) async fn key_images_spent(
&mut self, mut blockchain_read: BlockchainReadHandle,
key_images: HashSet<[u8; 32]>, key_images: HashSet<[u8; 32]>,
) -> Result<bool, Error> { ) -> Result<bool, Error> {
let BlockchainResponse::KeyImagesSpent(is_spent) = self let BlockchainResponse::KeyImagesSpent(is_spent) = blockchain_read
.blockchain_read
.ready() .ready()
.await? .await?
.call(BlockchainReadRequest::KeyImagesSpent(key_images)) .call(BlockchainReadRequest::KeyImagesSpent(key_images))
@ -211,12 +206,13 @@ impl CupratedRpcHandlerState {
} }
/// [`BlockchainReadRequest::CompactChainHistory`] /// [`BlockchainReadRequest::CompactChainHistory`]
pub(super) async fn compact_chain_history(&mut self) -> Result<(Vec<[u8; 32]>, u128), Error> { pub(super) async fn compact_chain_history(
mut blockchain_read: BlockchainReadHandle,
) -> Result<(Vec<[u8; 32]>, u128), Error> {
let BlockchainResponse::CompactChainHistory { let BlockchainResponse::CompactChainHistory {
block_ids, block_ids,
cumulative_difficulty, cumulative_difficulty,
} = self } = blockchain_read
.blockchain_read
.ready() .ready()
.await? .await?
.call(BlockchainReadRequest::CompactChainHistory) .call(BlockchainReadRequest::CompactChainHistory)
@ -230,11 +226,10 @@ impl CupratedRpcHandlerState {
/// [`BlockchainReadRequest::FindFirstUnknown`] /// [`BlockchainReadRequest::FindFirstUnknown`]
pub(super) async fn find_first_unknown( pub(super) async fn find_first_unknown(
&mut self, mut blockchain_read: BlockchainReadHandle,
hashes: Vec<[u8; 32]>, hashes: Vec<[u8; 32]>,
) -> Result<Option<(usize, u64)>, Error> { ) -> Result<Option<(usize, u64)>, Error> {
let BlockchainResponse::FindFirstUnknown(resp) = self let BlockchainResponse::FindFirstUnknown(resp) = blockchain_read
.blockchain_read
.ready() .ready()
.await? .await?
.call(BlockchainReadRequest::FindFirstUnknown(hashes)) .call(BlockchainReadRequest::FindFirstUnknown(hashes))
@ -246,120 +241,102 @@ impl CupratedRpcHandlerState {
Ok(resp.map(|(index, height)| (index, usize_to_u64(height)))) Ok(resp.map(|(index, height)| (index, usize_to_u64(height))))
} }
//------------------------------------------------------------------------------------------ new /// [`BlockchainReadRequest::TotalTxCount`]
pub(super) async fn total_tx_count(
mut blockchain_read: BlockchainReadHandle,
) -> Result<u64, Error> {
let BlockchainResponse::TotalTxCount(tx_count) = blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::TotalTxCount)
.await?
else {
unreachable!();
};
// /// [`BlockchainReadRequest::Block`]. Ok(usize_to_u64(tx_count))
// pub(super) async fn block(&mut self, height: u64) -> Result<Block, Error> { }
// let BlockchainResponse::Block(block) = self
// /// [`BlockchainReadRequest::DatabaseSize`]
// .blockchain_read pub(super) async fn database_size(
// .ready() mut blockchain_read: BlockchainReadHandle,
// .await? ) -> Result<(u64, u64), Error> {
// .call(BlockchainReadRequest::Block(u64_to_usize(height))) let BlockchainResponse::DatabaseSize {
// .await? database_size,
// else { free_space,
// unreachable!(); } = blockchain_read
// }; .ready()
.await?
// Ok(block) .call(BlockchainReadRequest::DatabaseSize)
// } .await?
else {
// /// [`BlockchainReadRequest::BlockByHash`]. unreachable!();
// pub(super) async fn block_by_hash(&mut self, hash: [u8; 32]) -> Result<Block, Error> { };
// let BlockchainResponse::BlockByHash(block) = self
// Ok((database_size, free_space))
// .blockchain_read }
// .ready()
// .await? /// [`BlockchainReadRequest::Difficulty`]
// .call(BlockchainReadRequest::BlockByHash(hash)) pub(super) async fn difficulty(
// .await? mut blockchain_read: BlockchainReadHandle,
// else { block_height: u64,
// unreachable!(); ) -> Result<u128, Error> {
// }; let BlockchainResponse::Difficulty(difficulty) = blockchain_read
.ready()
// Ok(block) .await?
// } .call(BlockchainReadRequest::Difficulty(u64_to_usize(
block_height,
// /// [`BlockchainReadRequest::BlockExtendedHeaderByHash`]. )))
// pub(super) async fn block_extended_header_by_hash( .await?
// &mut self, else {
// hash: [u8; 32], unreachable!();
// ) -> Result<ExtendedBlockHeader, Error> { };
// let BlockchainResponse::BlockExtendedHeaderByHash(header) = self
// Ok(difficulty)
// .blockchain_read }
// .ready()
// .await? /// [`BlockchainReadRequest::OutputHistogram`]
// .call(BlockchainReadRequest::BlockExtendedHeaderByHash(hash)) pub(super) async fn output_histogram(
// .await? mut blockchain_read: BlockchainReadHandle,
// else { ) -> Result<(), Error> {
// unreachable!(); let BlockchainResponse::OutputHistogram(_) = blockchain_read
// }; .ready()
.await?
// Ok(header) .call(BlockchainReadRequest::OutputHistogram)
// } .await?
else {
// /// [`BlockchainReadRequest::TopBlockFull`]. unreachable!();
// pub(super) async fn top_block_full(&mut self) -> Result<(Block, ExtendedBlockHeader), Error> { };
// let BlockchainResponse::TopBlockFull(block, header) = self
// Ok(todo!())
// .blockchain_read }
// .ready()
// .await? /// [`BlockchainReadRequest::CoinbaseTxSum`]
// .call(BlockchainReadRequest::TopBlockFull) pub(super) async fn coinbase_tx_sum(
// .await? mut blockchain_read: BlockchainReadHandle,
// else { ) -> Result<(), Error> {
// unreachable!(); let BlockchainResponse::CoinbaseTxSum(_) = blockchain_read
// }; .ready()
.await?
// Ok((block, header)) .call(BlockchainReadRequest::CoinbaseTxSum)
// } .await?
else {
// /// [`BlockchainReadRequest::CurrentHardFork`] unreachable!();
// pub(super) async fn current_hard_fork(&mut self) -> Result<HardFork, Error> { };
// let BlockchainResponse::CurrentHardFork(hard_fork) = self
// Ok(todo!())
// .blockchain_read }
// .ready()
// .await? /// [`BlockchainReadRequest::MinerData`]
// .call(BlockchainReadRequest::CurrentHardFork) pub(super) async fn miner_data(mut blockchain_read: BlockchainReadHandle) -> Result<(), Error> {
// .await? let BlockchainResponse::MinerData(_) = blockchain_read
// else { .ready()
// unreachable!(); .await?
// }; .call(BlockchainReadRequest::MinerData)
.await?
// Ok(hard_fork) else {
// } unreachable!();
};
// /// [`BlockchainReadRequest::PopBlocks`]
// pub(super) async fn pop_blocks(&mut self, nblocks: u64) -> Result<u64, Error> { Ok(todo!())
// let BlockchainResponse::PopBlocks(height) = self
//
// .blockchain_write
// .ready()
// .await?
// .call(BlockchainWriteRequest::PopBlocks(nblocks))
// .await?
// else {
// unreachable!();
// };
// Ok(usize_to_u64(height))
// }
// /// [`BlockchainReadRequest::CumulativeBlockWeightLimit`]
// pub(super) async fn cumulative_block_weight_limit(&mut self) -> Result<usize, Error> {
// let BlockchainResponse::CumulativeBlockWeightLimit(limit) = self
//
// .blockchain_read
// .ready()
// .await?
// .call(BlockchainReadRequest::CumulativeBlockWeightLimit)
// .await?
// else {
// unreachable!();
// };
// Ok(limit)
// }
} }

View file

@ -22,24 +22,17 @@ use cuprate_types::{
use crate::rpc::{CupratedRpcHandler, CupratedRpcHandlerState}; use crate::rpc::{CupratedRpcHandler, CupratedRpcHandlerState};
#[expect(
unreachable_code,
clippy::needless_pass_by_ref_mut,
reason = "TODO: remove after impl"
)]
impl CupratedRpcHandlerState {
/// TODO: doc enum message /// TODO: doc enum message
pub(super) async fn pop_blocks(&mut self) -> Result<(u64, [u8; 32]), Error> { pub(super) async fn pop_blocks() -> Result<(u64, [u8; 32]), Error> {
Ok(todo!()) Ok(todo!())
} }
/// TODO: doc enum message /// TODO: doc enum message
pub(super) async fn prune(&mut self) -> Result<(), Error> { pub(super) async fn prune() -> Result<(), Error> {
Ok(todo!()) Ok(todo!())
} }
/// TODO: doc enum message /// TODO: doc enum message
pub(super) async fn pruned(&mut self) -> Result<bool, Error> { pub(super) async fn pruned() -> Result<bool, Error> {
Ok(todo!()) Ok(todo!())
} }
}

View file

@ -107,6 +107,12 @@ fn map_request(
R::CompactChainHistory => compact_chain_history(env), R::CompactChainHistory => compact_chain_history(env),
R::FindFirstUnknown(block_ids) => find_first_unknown(env, &block_ids), R::FindFirstUnknown(block_ids) => find_first_unknown(env, &block_ids),
R::AltBlocksInChain(chain_id) => alt_blocks_in_chain(env, chain_id), R::AltBlocksInChain(chain_id) => alt_blocks_in_chain(env, chain_id),
R::TotalTxCount
| R::DatabaseSize
| R::Difficulty(_)
| R::OutputHistogram
| R::CoinbaseTxSum
| R::MinerData => todo!(),
} }
/* SOMEDAY: post-request handling, run some code for each request? */ /* SOMEDAY: post-request handling, run some code for each request? */

View file

@ -103,6 +103,24 @@ pub enum BlockchainReadRequest {
/// A request for all alt blocks in the chain with the given [`ChainId`]. /// A request for all alt blocks in the chain with the given [`ChainId`].
AltBlocksInChain(ChainId), AltBlocksInChain(ChainId),
/// TODO
TotalTxCount,
/// TODO
DatabaseSize,
// TODO
Difficulty(usize),
/// TODO
OutputHistogram,
/// TODO
CoinbaseTxSum,
/// TODO
MinerData,
} }
//---------------------------------------------------------------------------------------------------- WriteRequest //---------------------------------------------------------------------------------------------------- WriteRequest
@ -227,6 +245,36 @@ pub enum BlockchainResponse {
/// Contains all the alt blocks in the alt-chain in chronological order. /// Contains all the alt blocks in the alt-chain in chronological order.
AltBlocksInChain(Vec<AltBlockInformation>), AltBlocksInChain(Vec<AltBlockInformation>),
/// The response for [`BlockchainReadRequest::TotalTxCount`].
///
/// TODO
TotalTxCount(usize),
/// The response for [`BlockchainReadRequest::TotalTxCount`].
///
/// TODO
DatabaseSize { database_size: u64, free_space: u64 },
/// The response for [`BlockchainReadRequest::TotalTxCount`].
///
// TODO
Difficulty(u128),
/// The response for [`BlockchainReadRequest::TotalTxCount`].
///
/// TODO
OutputHistogram(std::convert::Infallible),
/// The response for [`BlockchainReadRequest::TotalTxCount`].
///
/// TODO
CoinbaseTxSum(std::convert::Infallible),
/// The response for [`BlockchainReadRequest::TotalTxCount`].
///
/// TODO
MinerData(std::convert::Infallible),
//------------------------------------------------------ Writes //------------------------------------------------------ Writes
/// A generic Ok response to indicate a request was successfully handled. /// A generic Ok response to indicate a request was successfully handled.
/// ///
@ -236,6 +284,7 @@ pub enum BlockchainResponse {
/// - [`BlockchainWriteRequest::ReverseReorg`] /// - [`BlockchainWriteRequest::ReverseReorg`]
/// - [`BlockchainWriteRequest::FlushAltBlocks`] /// - [`BlockchainWriteRequest::FlushAltBlocks`]
Ok, Ok,
/// The response for [`BlockchainWriteRequest::PopBlocks`]. /// The response for [`BlockchainWriteRequest::PopBlocks`].
/// ///
/// The inner value is the alt-chain ID for the old main chain blocks. /// The inner value is the alt-chain ID for the old main chain blocks.