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,344 +23,320 @@ 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 blockchain_read: BlockchainReadHandle,
&mut self, height: u64,
height: u64, ) -> Result<ExtendedBlockHeader, Error> {
) -> Result<ExtendedBlockHeader, Error> { let BlockchainResponse::BlockExtendedHeader(header) = blockchain_read
let BlockchainResponse::BlockExtendedHeader(header) = self .ready()
.blockchain_read .await?
.ready() .call(BlockchainReadRequest::BlockExtendedHeader(u64_to_usize(
.await? height,
.call(BlockchainReadRequest::BlockExtendedHeader(u64_to_usize( )))
height, .await?
))) else {
.await? unreachable!();
else { };
unreachable!();
};
Ok(header) Ok(header)
} }
/// [`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( u64_to_usize(height),
u64_to_usize(height), chain,
chain, ))
)) .await?
.await? else {
else { unreachable!();
unreachable!(); };
};
Ok(hash)
Ok(hash) }
}
/// [`BlockchainReadRequest::FindBlock`].
/// [`BlockchainReadRequest::FindBlock`]. pub(super) async fn find_block(
pub(super) async fn find_block( mut blockchain_read: BlockchainReadHandle,
&mut self, block_hash: [u8; 32],
block_hash: [u8; 32], ) -> Result<Option<(Chain, usize)>, Error> {
) -> Result<Option<(Chain, usize)>, Error> { let BlockchainResponse::FindBlock(option) = blockchain_read
let BlockchainResponse::FindBlock(option) = self .ready()
.blockchain_read .await?
.ready() .call(BlockchainReadRequest::FindBlock(block_hash))
.await? .await?
.call(BlockchainReadRequest::FindBlock(block_hash)) else {
.await? unreachable!();
else { };
unreachable!();
}; Ok(option)
}
Ok(option)
} /// [`BlockchainReadRequest::FilterUnknownHashes`].
pub(super) async fn filter_unknown_hashes(
/// [`BlockchainReadRequest::FilterUnknownHashes`]. mut blockchain_read: BlockchainReadHandle,
pub(super) async fn filter_unknown_hashes( block_hashes: HashSet<[u8; 32]>,
&mut self, ) -> Result<HashSet<[u8; 32]>, Error> {
block_hashes: HashSet<[u8; 32]>, let BlockchainResponse::FilterUnknownHashes(output) = blockchain_read
) -> Result<HashSet<[u8; 32]>, Error> { .ready()
let BlockchainResponse::FilterUnknownHashes(output) = self .await?
.blockchain_read .call(BlockchainReadRequest::FilterUnknownHashes(block_hashes))
.ready() .await?
.await? else {
.call(BlockchainReadRequest::FilterUnknownHashes(block_hashes)) unreachable!();
.await? };
else {
unreachable!(); Ok(output)
}; }
Ok(output) /// [`BlockchainReadRequest::BlockExtendedHeaderInRange`]
} pub(super) async fn block_extended_header_in_range(
mut blockchain_read: BlockchainReadHandle,
/// [`BlockchainReadRequest::BlockExtendedHeaderInRange`] range: Range<usize>,
pub(super) async fn block_extended_header_in_range( chain: Chain,
&mut self, ) -> Result<Vec<ExtendedBlockHeader>, Error> {
range: Range<usize>, let BlockchainResponse::BlockExtendedHeaderInRange(output) = blockchain_read
chain: Chain, .ready()
) -> Result<Vec<ExtendedBlockHeader>, Error> { .await?
let BlockchainResponse::BlockExtendedHeaderInRange(output) = self .call(BlockchainReadRequest::BlockExtendedHeaderInRange(
.blockchain_read range, chain,
.ready() ))
.await? .await?
.call(BlockchainReadRequest::BlockExtendedHeaderInRange( else {
range, chain, unreachable!();
)) };
.await?
else { Ok(output)
unreachable!(); }
};
/// [`BlockchainReadRequest::ChainHeight`].
Ok(output) pub(super) async fn chain_height(
} mut blockchain_read: BlockchainReadHandle,
) -> Result<(u64, [u8; 32]), Error> {
/// [`BlockchainReadRequest::ChainHeight`]. let BlockchainResponse::ChainHeight(height, hash) = blockchain_read
pub(super) async fn chain_height(&mut self) -> Result<(u64, [u8; 32]), Error> { .ready()
let BlockchainResponse::ChainHeight(height, hash) = self .await?
.blockchain_read .call(BlockchainReadRequest::ChainHeight)
.ready() .await?
.await? else {
.call(BlockchainReadRequest::ChainHeight) unreachable!();
.await? };
else {
unreachable!(); Ok((usize_to_u64(height), hash))
}; }
Ok((usize_to_u64(height), hash)) /// [`BlockchainReadRequest::GeneratedCoins`].
} pub(super) async fn generated_coins(
mut blockchain_read: BlockchainReadHandle,
/// [`BlockchainReadRequest::GeneratedCoins`]. block_height: u64,
pub(super) async fn generated_coins(&mut self, block_height: u64) -> Result<u64, Error> { ) -> Result<u64, Error> {
let BlockchainResponse::GeneratedCoins(generated_coins) = self let BlockchainResponse::GeneratedCoins(generated_coins) = blockchain_read
.blockchain_read .ready()
.ready() .await?
.await? .call(BlockchainReadRequest::GeneratedCoins(u64_to_usize(
.call(BlockchainReadRequest::GeneratedCoins(u64_to_usize( block_height,
block_height, )))
))) .await?
.await? else {
else { unreachable!();
unreachable!(); };
};
Ok(generated_coins)
Ok(generated_coins) }
}
/// [`BlockchainReadRequest::Outputs`]
/// [`BlockchainReadRequest::Outputs`] pub(super) async fn outputs(
pub(super) async fn outputs( mut blockchain_read: BlockchainReadHandle,
&mut self, 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) = blockchain_read
let BlockchainResponse::Outputs(outputs) = self .ready()
.blockchain_read .await?
.ready() .call(BlockchainReadRequest::Outputs(outputs))
.await? .await?
.call(BlockchainReadRequest::Outputs(outputs)) else {
.await? unreachable!();
else { };
unreachable!();
}; Ok(outputs)
}
Ok(outputs)
} /// [`BlockchainReadRequest::NumberOutputsWithAmount`]
pub(super) async fn number_outputs_with_amount(
/// [`BlockchainReadRequest::NumberOutputsWithAmount`] mut blockchain_read: BlockchainReadHandle,
pub(super) async fn number_outputs_with_amount( output_amounts: Vec<u64>,
&mut self, ) -> Result<HashMap<u64, usize>, Error> {
output_amounts: Vec<u64>, let BlockchainResponse::NumberOutputsWithAmount(map) = blockchain_read
) -> Result<HashMap<u64, usize>, Error> { .ready()
let BlockchainResponse::NumberOutputsWithAmount(map) = self .await?
.blockchain_read .call(BlockchainReadRequest::NumberOutputsWithAmount(
.ready() output_amounts,
.await? ))
.call(BlockchainReadRequest::NumberOutputsWithAmount( .await?
output_amounts, else {
)) unreachable!();
.await? };
else {
unreachable!(); Ok(map)
}; }
Ok(map) /// [`BlockchainReadRequest::KeyImagesSpent`]
} pub(super) async fn key_images_spent(
mut blockchain_read: BlockchainReadHandle,
/// [`BlockchainReadRequest::KeyImagesSpent`] key_images: HashSet<[u8; 32]>,
pub(super) async fn key_images_spent( ) -> Result<bool, Error> {
&mut self, let BlockchainResponse::KeyImagesSpent(is_spent) = blockchain_read
key_images: HashSet<[u8; 32]>, .ready()
) -> Result<bool, Error> { .await?
let BlockchainResponse::KeyImagesSpent(is_spent) = self .call(BlockchainReadRequest::KeyImagesSpent(key_images))
.blockchain_read .await?
.ready() else {
.await? unreachable!();
.call(BlockchainReadRequest::KeyImagesSpent(key_images)) };
.await?
else { Ok(is_spent)
unreachable!(); }
};
/// [`BlockchainReadRequest::CompactChainHistory`]
Ok(is_spent) pub(super) async fn compact_chain_history(
} mut blockchain_read: BlockchainReadHandle,
) -> Result<(Vec<[u8; 32]>, u128), Error> {
/// [`BlockchainReadRequest::CompactChainHistory`] let BlockchainResponse::CompactChainHistory {
pub(super) async fn compact_chain_history(&mut self) -> Result<(Vec<[u8; 32]>, u128), Error> { block_ids,
let BlockchainResponse::CompactChainHistory { cumulative_difficulty,
block_ids, } = blockchain_read
cumulative_difficulty, .ready()
} = self .await?
.blockchain_read .call(BlockchainReadRequest::CompactChainHistory)
.ready() .await?
.await? else {
.call(BlockchainReadRequest::CompactChainHistory) unreachable!();
.await? };
else {
unreachable!(); Ok((block_ids, cumulative_difficulty))
}; }
Ok((block_ids, cumulative_difficulty)) /// [`BlockchainReadRequest::FindFirstUnknown`]
} pub(super) async fn find_first_unknown(
mut blockchain_read: BlockchainReadHandle,
/// [`BlockchainReadRequest::FindFirstUnknown`] hashes: Vec<[u8; 32]>,
pub(super) async fn find_first_unknown( ) -> Result<Option<(usize, u64)>, Error> {
&mut self, let BlockchainResponse::FindFirstUnknown(resp) = blockchain_read
hashes: Vec<[u8; 32]>, .ready()
) -> Result<Option<(usize, u64)>, Error> { .await?
let BlockchainResponse::FindFirstUnknown(resp) = self .call(BlockchainReadRequest::FindFirstUnknown(hashes))
.blockchain_read .await?
.ready() else {
.await? unreachable!();
.call(BlockchainReadRequest::FindFirstUnknown(hashes)) };
.await?
else { Ok(resp.map(|(index, height)| (index, usize_to_u64(height))))
unreachable!(); }
};
/// [`BlockchainReadRequest::TotalTxCount`]
Ok(resp.map(|(index, height)| (index, usize_to_u64(height)))) pub(super) async fn total_tx_count(
} mut blockchain_read: BlockchainReadHandle,
) -> Result<u64, Error> {
//------------------------------------------------------------------------------------------ new let BlockchainResponse::TotalTxCount(tx_count) = blockchain_read
.ready()
// /// [`BlockchainReadRequest::Block`]. .await?
// pub(super) async fn block(&mut self, height: u64) -> Result<Block, Error> { .call(BlockchainReadRequest::TotalTxCount)
// let BlockchainResponse::Block(block) = self .await?
// else {
// .blockchain_read unreachable!();
// .ready() };
// .await?
// .call(BlockchainReadRequest::Block(u64_to_usize(height))) Ok(usize_to_u64(tx_count))
// .await? }
// else {
// unreachable!(); /// [`BlockchainReadRequest::DatabaseSize`]
// }; pub(super) async fn database_size(
mut blockchain_read: BlockchainReadHandle,
// Ok(block) ) -> Result<(u64, u64), Error> {
// } let BlockchainResponse::DatabaseSize {
database_size,
// /// [`BlockchainReadRequest::BlockByHash`]. free_space,
// pub(super) async fn block_by_hash(&mut self, hash: [u8; 32]) -> Result<Block, Error> { } = blockchain_read
// let BlockchainResponse::BlockByHash(block) = self .ready()
// .await?
// .blockchain_read .call(BlockchainReadRequest::DatabaseSize)
// .ready() .await?
// .await? else {
// .call(BlockchainReadRequest::BlockByHash(hash)) unreachable!();
// .await? };
// else {
// unreachable!(); Ok((database_size, free_space))
// }; }
// Ok(block) /// [`BlockchainReadRequest::Difficulty`]
// } pub(super) async fn difficulty(
mut blockchain_read: BlockchainReadHandle,
// /// [`BlockchainReadRequest::BlockExtendedHeaderByHash`]. block_height: u64,
// pub(super) async fn block_extended_header_by_hash( ) -> Result<u128, Error> {
// &mut self, let BlockchainResponse::Difficulty(difficulty) = blockchain_read
// hash: [u8; 32], .ready()
// ) -> Result<ExtendedBlockHeader, Error> { .await?
// let BlockchainResponse::BlockExtendedHeaderByHash(header) = self .call(BlockchainReadRequest::Difficulty(u64_to_usize(
// block_height,
// .blockchain_read )))
// .ready() .await?
// .await? else {
// .call(BlockchainReadRequest::BlockExtendedHeaderByHash(hash)) unreachable!();
// .await? };
// else {
// unreachable!(); Ok(difficulty)
// }; }
// Ok(header) /// [`BlockchainReadRequest::OutputHistogram`]
// } pub(super) async fn output_histogram(
mut blockchain_read: BlockchainReadHandle,
// /// [`BlockchainReadRequest::TopBlockFull`]. ) -> Result<(), Error> {
// pub(super) async fn top_block_full(&mut self) -> Result<(Block, ExtendedBlockHeader), Error> { let BlockchainResponse::OutputHistogram(_) = blockchain_read
// let BlockchainResponse::TopBlockFull(block, header) = self .ready()
// .await?
// .blockchain_read .call(BlockchainReadRequest::OutputHistogram)
// .ready() .await?
// .await? else {
// .call(BlockchainReadRequest::TopBlockFull) unreachable!();
// .await? };
// else {
// unreachable!(); Ok(todo!())
// }; }
// Ok((block, header)) /// [`BlockchainReadRequest::CoinbaseTxSum`]
// } pub(super) async fn coinbase_tx_sum(
mut blockchain_read: BlockchainReadHandle,
// /// [`BlockchainReadRequest::CurrentHardFork`] ) -> Result<(), Error> {
// pub(super) async fn current_hard_fork(&mut self) -> Result<HardFork, Error> { let BlockchainResponse::CoinbaseTxSum(_) = blockchain_read
// let BlockchainResponse::CurrentHardFork(hard_fork) = self .ready()
// .await?
// .blockchain_read .call(BlockchainReadRequest::CoinbaseTxSum)
// .ready() .await?
// .await? else {
// .call(BlockchainReadRequest::CurrentHardFork) unreachable!();
// .await? };
// else {
// unreachable!(); Ok(todo!())
// }; }
// Ok(hard_fork) /// [`BlockchainReadRequest::MinerData`]
// } pub(super) async fn miner_data(mut blockchain_read: BlockchainReadHandle) -> Result<(), Error> {
let BlockchainResponse::MinerData(_) = blockchain_read
// /// [`BlockchainReadRequest::PopBlocks`] .ready()
// pub(super) async fn pop_blocks(&mut self, nblocks: u64) -> Result<u64, Error> { .await?
// let BlockchainResponse::PopBlocks(height) = self .call(BlockchainReadRequest::MinerData)
// .await?
// .blockchain_write else {
// .ready() unreachable!();
// .await? };
// .call(BlockchainWriteRequest::PopBlocks(nblocks))
// .await? Ok(todo!())
// 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( /// TODO: doc enum message
unreachable_code, pub(super) async fn pop_blocks() -> Result<(u64, [u8; 32]), Error> {
clippy::needless_pass_by_ref_mut, Ok(todo!())
reason = "TODO: remove after impl" }
)]
impl CupratedRpcHandlerState { /// TODO: doc enum message
/// TODO: doc enum message pub(super) async fn prune() -> Result<(), Error> {
pub(super) async fn pop_blocks(&mut self) -> Result<(u64, [u8; 32]), Error> { Ok(todo!())
Ok(todo!()) }
}
/// TODO: doc enum message
/// TODO: doc enum message pub(super) async fn pruned() -> Result<bool, Error> {
pub(super) async fn prune(&mut self) -> Result<(), Error> { Ok(todo!())
Ok(todo!())
}
/// TODO: doc enum message
pub(super) async fn pruned(&mut self) -> Result<bool, Error> {
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.