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 blockchain_read: BlockchainReadHandle,
&mut self,
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(
@ -41,16 +40,15 @@ impl CupratedRpcHandlerState {
}; };
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(
@ -63,15 +61,14 @@ impl CupratedRpcHandlerState {
}; };
Ok(hash) Ok(hash)
} }
/// [`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))
@ -81,15 +78,14 @@ impl CupratedRpcHandlerState {
}; };
Ok(option) Ok(option)
} }
/// [`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))
@ -99,16 +95,15 @@ impl CupratedRpcHandlerState {
}; };
Ok(output) Ok(output)
} }
/// [`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(
@ -120,12 +115,13 @@ impl CupratedRpcHandlerState {
}; };
Ok(output) Ok(output)
} }
/// [`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)
@ -135,12 +131,14 @@ impl CupratedRpcHandlerState {
}; };
Ok((usize_to_u64(height), hash)) Ok((usize_to_u64(height), hash))
} }
/// [`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(
@ -152,15 +150,14 @@ impl CupratedRpcHandlerState {
}; };
Ok(generated_coins) Ok(generated_coins)
} }
/// [`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))
@ -170,15 +167,14 @@ impl CupratedRpcHandlerState {
}; };
Ok(outputs) Ok(outputs)
} }
/// [`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(
@ -190,15 +186,14 @@ impl CupratedRpcHandlerState {
}; };
Ok(map) Ok(map)
} }
/// [`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))
@ -208,15 +203,16 @@ impl CupratedRpcHandlerState {
}; };
Ok(is_spent) Ok(is_spent)
} }
/// [`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)
@ -226,15 +222,14 @@ impl CupratedRpcHandlerState {
}; };
Ok((block_ids, cumulative_difficulty)) Ok((block_ids, cumulative_difficulty))
} }
/// [`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))
@ -244,122 +239,104 @@ 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(
// /// [`BlockchainReadRequest::Block`]. mut blockchain_read: BlockchainReadHandle,
// pub(super) async fn block(&mut self, height: u64) -> Result<Block, Error> { ) -> Result<u64, Error> {
// let BlockchainResponse::Block(block) = self let BlockchainResponse::TotalTxCount(tx_count) = blockchain_read
// .ready()
// .blockchain_read .await?
// .ready() .call(BlockchainReadRequest::TotalTxCount)
// .await? .await?
// .call(BlockchainReadRequest::Block(u64_to_usize(height))) else {
// .await? unreachable!();
// else { };
// unreachable!();
// }; Ok(usize_to_u64(tx_count))
}
// Ok(block)
// } /// [`BlockchainReadRequest::DatabaseSize`]
pub(super) async fn database_size(
// /// [`BlockchainReadRequest::BlockByHash`]. mut blockchain_read: BlockchainReadHandle,
// pub(super) async fn block_by_hash(&mut self, hash: [u8; 32]) -> Result<Block, Error> { ) -> Result<(u64, u64), Error> {
// let BlockchainResponse::BlockByHash(block) = self let BlockchainResponse::DatabaseSize {
// database_size,
// .blockchain_read free_space,
// .ready() } = blockchain_read
// .await? .ready()
// .call(BlockchainReadRequest::BlockByHash(hash)) .await?
// .await? .call(BlockchainReadRequest::DatabaseSize)
// else { .await?
// unreachable!(); else {
// }; unreachable!();
};
// Ok(block)
// } Ok((database_size, free_space))
}
// /// [`BlockchainReadRequest::BlockExtendedHeaderByHash`].
// pub(super) async fn block_extended_header_by_hash( /// [`BlockchainReadRequest::Difficulty`]
// &mut self, pub(super) async fn difficulty(
// hash: [u8; 32], mut blockchain_read: BlockchainReadHandle,
// ) -> Result<ExtendedBlockHeader, Error> { block_height: u64,
// let BlockchainResponse::BlockExtendedHeaderByHash(header) = self ) -> Result<u128, Error> {
// let BlockchainResponse::Difficulty(difficulty) = blockchain_read
// .blockchain_read .ready()
// .ready() .await?
// .await? .call(BlockchainReadRequest::Difficulty(u64_to_usize(
// .call(BlockchainReadRequest::BlockExtendedHeaderByHash(hash)) block_height,
// .await? )))
// else { .await?
// unreachable!(); else {
// }; unreachable!();
};
// Ok(header)
// } Ok(difficulty)
}
// /// [`BlockchainReadRequest::TopBlockFull`].
// pub(super) async fn top_block_full(&mut self) -> Result<(Block, ExtendedBlockHeader), Error> { /// [`BlockchainReadRequest::OutputHistogram`]
// let BlockchainResponse::TopBlockFull(block, header) = self pub(super) async fn output_histogram(
// mut blockchain_read: BlockchainReadHandle,
// .blockchain_read ) -> Result<(), Error> {
// .ready() let BlockchainResponse::OutputHistogram(_) = blockchain_read
// .await? .ready()
// .call(BlockchainReadRequest::TopBlockFull) .await?
// .await? .call(BlockchainReadRequest::OutputHistogram)
// else { .await?
// unreachable!(); else {
// }; unreachable!();
};
// Ok((block, header))
// } Ok(todo!())
}
// /// [`BlockchainReadRequest::CurrentHardFork`]
// pub(super) async fn current_hard_fork(&mut self) -> Result<HardFork, Error> { /// [`BlockchainReadRequest::CoinbaseTxSum`]
// let BlockchainResponse::CurrentHardFork(hard_fork) = self pub(super) async fn coinbase_tx_sum(
// mut blockchain_read: BlockchainReadHandle,
// .blockchain_read ) -> Result<(), Error> {
// .ready() let BlockchainResponse::CoinbaseTxSum(_) = blockchain_read
// .await? .ready()
// .call(BlockchainReadRequest::CurrentHardFork) .await?
// .await? .call(BlockchainReadRequest::CoinbaseTxSum)
// else { .await?
// unreachable!(); else {
// }; unreachable!();
};
// Ok(hard_fork)
// } Ok(todo!())
}
// /// [`BlockchainReadRequest::PopBlocks`]
// pub(super) async fn pop_blocks(&mut self, nblocks: u64) -> Result<u64, Error> { /// [`BlockchainReadRequest::MinerData`]
// let BlockchainResponse::PopBlocks(height) = self pub(super) async fn miner_data(mut blockchain_read: BlockchainReadHandle) -> Result<(), Error> {
// let BlockchainResponse::MinerData(_) = blockchain_read
// .blockchain_write .ready()
// .ready() .await?
// .await? .call(BlockchainReadRequest::MinerData)
// .call(BlockchainWriteRequest::PopBlocks(nblocks)) .await?
// .await? else {
// else { unreachable!();
// unreachable!(); };
// };
Ok(todo!())
// 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!())
}
/// TODO: doc enum message
pub(super) async fn pruned() -> Result<bool, Error> {
Ok(todo!()) Ok(todo!())
}
/// TODO: doc enum message
pub(super) async fn prune(&mut self) -> Result<(), Error> {
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.