Compare commits

..

2 commits

Author SHA1 Message Date
hinto.janai
e618bb7a9e
fmt 2024-09-24 20:39:25 -04:00
hinto.janai
e8cb951615
add blockchain msgs 2024-09-24 20:39:08 -04:00
6 changed files with 387 additions and 361 deletions

View file

@ -7,6 +7,7 @@ use std::{
};
use anyhow::{anyhow, Error};
use cuprate_blockchain::service::BlockchainReadHandle;
use futures::StreamExt;
use monero_serai::block::Block;
use tower::{Service, ServiceExt};
@ -22,344 +23,320 @@ use cuprate_types::{
use crate::rpc::CupratedRpcHandlerState;
impl CupratedRpcHandlerState {
/// [`BlockchainReadRequest::BlockExtendedHeader`].
pub(super) async fn block_extended_header(
&mut self,
height: u64,
) -> Result<ExtendedBlockHeader, Error> {
let BlockchainResponse::BlockExtendedHeader(header) = self
.blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::BlockExtendedHeader(u64_to_usize(
height,
)))
.await?
else {
unreachable!();
};
/// [`BlockchainReadRequest::BlockExtendedHeader`].
pub(super) async fn block_extended_header(
mut blockchain_read: BlockchainReadHandle,
height: u64,
) -> Result<ExtendedBlockHeader, Error> {
let BlockchainResponse::BlockExtendedHeader(header) = blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::BlockExtendedHeader(u64_to_usize(
height,
)))
.await?
else {
unreachable!();
};
Ok(header)
}
/// [`BlockchainReadRequest::BlockHash`].
pub(super) async fn block_hash(
&mut self,
height: u64,
chain: Chain,
) -> Result<[u8; 32], Error> {
let BlockchainResponse::BlockHash(hash) = self
.blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::BlockHash(
u64_to_usize(height),
chain,
))
.await?
else {
unreachable!();
};
Ok(hash)
}
/// [`BlockchainReadRequest::FindBlock`].
pub(super) async fn find_block(
&mut self,
block_hash: [u8; 32],
) -> Result<Option<(Chain, usize)>, Error> {
let BlockchainResponse::FindBlock(option) = self
.blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::FindBlock(block_hash))
.await?
else {
unreachable!();
};
Ok(option)
}
/// [`BlockchainReadRequest::FilterUnknownHashes`].
pub(super) async fn filter_unknown_hashes(
&mut self,
block_hashes: HashSet<[u8; 32]>,
) -> Result<HashSet<[u8; 32]>, Error> {
let BlockchainResponse::FilterUnknownHashes(output) = self
.blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::FilterUnknownHashes(block_hashes))
.await?
else {
unreachable!();
};
Ok(output)
}
/// [`BlockchainReadRequest::BlockExtendedHeaderInRange`]
pub(super) async fn block_extended_header_in_range(
&mut self,
range: Range<usize>,
chain: Chain,
) -> Result<Vec<ExtendedBlockHeader>, Error> {
let BlockchainResponse::BlockExtendedHeaderInRange(output) = self
.blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::BlockExtendedHeaderInRange(
range, chain,
))
.await?
else {
unreachable!();
};
Ok(output)
}
/// [`BlockchainReadRequest::ChainHeight`].
pub(super) async fn chain_height(&mut self) -> Result<(u64, [u8; 32]), Error> {
let BlockchainResponse::ChainHeight(height, hash) = self
.blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::ChainHeight)
.await?
else {
unreachable!();
};
Ok((usize_to_u64(height), hash))
}
/// [`BlockchainReadRequest::GeneratedCoins`].
pub(super) async fn generated_coins(&mut self, block_height: u64) -> Result<u64, Error> {
let BlockchainResponse::GeneratedCoins(generated_coins) = self
.blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::GeneratedCoins(u64_to_usize(
block_height,
)))
.await?
else {
unreachable!();
};
Ok(generated_coins)
}
/// [`BlockchainReadRequest::Outputs`]
pub(super) async fn outputs(
&mut self,
outputs: HashMap<u64, HashSet<u64>>,
) -> Result<HashMap<u64, HashMap<u64, OutputOnChain>>, Error> {
let BlockchainResponse::Outputs(outputs) = self
.blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::Outputs(outputs))
.await?
else {
unreachable!();
};
Ok(outputs)
}
/// [`BlockchainReadRequest::NumberOutputsWithAmount`]
pub(super) async fn number_outputs_with_amount(
&mut self,
output_amounts: Vec<u64>,
) -> Result<HashMap<u64, usize>, Error> {
let BlockchainResponse::NumberOutputsWithAmount(map) = self
.blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::NumberOutputsWithAmount(
output_amounts,
))
.await?
else {
unreachable!();
};
Ok(map)
}
/// [`BlockchainReadRequest::KeyImagesSpent`]
pub(super) async fn key_images_spent(
&mut self,
key_images: HashSet<[u8; 32]>,
) -> Result<bool, Error> {
let BlockchainResponse::KeyImagesSpent(is_spent) = self
.blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::KeyImagesSpent(key_images))
.await?
else {
unreachable!();
};
Ok(is_spent)
}
/// [`BlockchainReadRequest::CompactChainHistory`]
pub(super) async fn compact_chain_history(&mut self) -> Result<(Vec<[u8; 32]>, u128), Error> {
let BlockchainResponse::CompactChainHistory {
block_ids,
cumulative_difficulty,
} = self
.blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::CompactChainHistory)
.await?
else {
unreachable!();
};
Ok((block_ids, cumulative_difficulty))
}
/// [`BlockchainReadRequest::FindFirstUnknown`]
pub(super) async fn find_first_unknown(
&mut self,
hashes: Vec<[u8; 32]>,
) -> Result<Option<(usize, u64)>, Error> {
let BlockchainResponse::FindFirstUnknown(resp) = self
.blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::FindFirstUnknown(hashes))
.await?
else {
unreachable!();
};
Ok(resp.map(|(index, height)| (index, usize_to_u64(height))))
}
//------------------------------------------------------------------------------------------ new
// /// [`BlockchainReadRequest::Block`].
// pub(super) async fn block(&mut self, height: u64) -> Result<Block, Error> {
// let BlockchainResponse::Block(block) = self
//
// .blockchain_read
// .ready()
// .await?
// .call(BlockchainReadRequest::Block(u64_to_usize(height)))
// .await?
// else {
// unreachable!();
// };
// Ok(block)
// }
// /// [`BlockchainReadRequest::BlockByHash`].
// pub(super) async fn block_by_hash(&mut self, hash: [u8; 32]) -> Result<Block, Error> {
// let BlockchainResponse::BlockByHash(block) = self
//
// .blockchain_read
// .ready()
// .await?
// .call(BlockchainReadRequest::BlockByHash(hash))
// .await?
// else {
// unreachable!();
// };
// Ok(block)
// }
// /// [`BlockchainReadRequest::BlockExtendedHeaderByHash`].
// pub(super) async fn block_extended_header_by_hash(
// &mut self,
// hash: [u8; 32],
// ) -> Result<ExtendedBlockHeader, Error> {
// let BlockchainResponse::BlockExtendedHeaderByHash(header) = self
//
// .blockchain_read
// .ready()
// .await?
// .call(BlockchainReadRequest::BlockExtendedHeaderByHash(hash))
// .await?
// else {
// unreachable!();
// };
// Ok(header)
// }
// /// [`BlockchainReadRequest::TopBlockFull`].
// pub(super) async fn top_block_full(&mut self) -> Result<(Block, ExtendedBlockHeader), Error> {
// let BlockchainResponse::TopBlockFull(block, header) = self
//
// .blockchain_read
// .ready()
// .await?
// .call(BlockchainReadRequest::TopBlockFull)
// .await?
// else {
// unreachable!();
// };
// Ok((block, header))
// }
// /// [`BlockchainReadRequest::CurrentHardFork`]
// pub(super) async fn current_hard_fork(&mut self) -> Result<HardFork, Error> {
// let BlockchainResponse::CurrentHardFork(hard_fork) = self
//
// .blockchain_read
// .ready()
// .await?
// .call(BlockchainReadRequest::CurrentHardFork)
// .await?
// else {
// unreachable!();
// };
// Ok(hard_fork)
// }
// /// [`BlockchainReadRequest::PopBlocks`]
// pub(super) async fn pop_blocks(&mut self, nblocks: u64) -> Result<u64, Error> {
// 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)
// }
Ok(header)
}
/// [`BlockchainReadRequest::BlockHash`].
pub(super) async fn block_hash(
mut blockchain_read: BlockchainReadHandle,
height: u64,
chain: Chain,
) -> Result<[u8; 32], Error> {
let BlockchainResponse::BlockHash(hash) = blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::BlockHash(
u64_to_usize(height),
chain,
))
.await?
else {
unreachable!();
};
Ok(hash)
}
/// [`BlockchainReadRequest::FindBlock`].
pub(super) async fn find_block(
mut blockchain_read: BlockchainReadHandle,
block_hash: [u8; 32],
) -> Result<Option<(Chain, usize)>, Error> {
let BlockchainResponse::FindBlock(option) = blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::FindBlock(block_hash))
.await?
else {
unreachable!();
};
Ok(option)
}
/// [`BlockchainReadRequest::FilterUnknownHashes`].
pub(super) async fn filter_unknown_hashes(
mut blockchain_read: BlockchainReadHandle,
block_hashes: HashSet<[u8; 32]>,
) -> Result<HashSet<[u8; 32]>, Error> {
let BlockchainResponse::FilterUnknownHashes(output) = blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::FilterUnknownHashes(block_hashes))
.await?
else {
unreachable!();
};
Ok(output)
}
/// [`BlockchainReadRequest::BlockExtendedHeaderInRange`]
pub(super) async fn block_extended_header_in_range(
mut blockchain_read: BlockchainReadHandle,
range: Range<usize>,
chain: Chain,
) -> Result<Vec<ExtendedBlockHeader>, Error> {
let BlockchainResponse::BlockExtendedHeaderInRange(output) = blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::BlockExtendedHeaderInRange(
range, chain,
))
.await?
else {
unreachable!();
};
Ok(output)
}
/// [`BlockchainReadRequest::ChainHeight`].
pub(super) async fn chain_height(
mut blockchain_read: BlockchainReadHandle,
) -> Result<(u64, [u8; 32]), Error> {
let BlockchainResponse::ChainHeight(height, hash) = blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::ChainHeight)
.await?
else {
unreachable!();
};
Ok((usize_to_u64(height), hash))
}
/// [`BlockchainReadRequest::GeneratedCoins`].
pub(super) async fn generated_coins(
mut blockchain_read: BlockchainReadHandle,
block_height: u64,
) -> Result<u64, Error> {
let BlockchainResponse::GeneratedCoins(generated_coins) = blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::GeneratedCoins(u64_to_usize(
block_height,
)))
.await?
else {
unreachable!();
};
Ok(generated_coins)
}
/// [`BlockchainReadRequest::Outputs`]
pub(super) async fn outputs(
mut blockchain_read: BlockchainReadHandle,
outputs: HashMap<u64, HashSet<u64>>,
) -> Result<HashMap<u64, HashMap<u64, OutputOnChain>>, Error> {
let BlockchainResponse::Outputs(outputs) = blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::Outputs(outputs))
.await?
else {
unreachable!();
};
Ok(outputs)
}
/// [`BlockchainReadRequest::NumberOutputsWithAmount`]
pub(super) async fn number_outputs_with_amount(
mut blockchain_read: BlockchainReadHandle,
output_amounts: Vec<u64>,
) -> Result<HashMap<u64, usize>, Error> {
let BlockchainResponse::NumberOutputsWithAmount(map) = blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::NumberOutputsWithAmount(
output_amounts,
))
.await?
else {
unreachable!();
};
Ok(map)
}
/// [`BlockchainReadRequest::KeyImagesSpent`]
pub(super) async fn key_images_spent(
mut blockchain_read: BlockchainReadHandle,
key_images: HashSet<[u8; 32]>,
) -> Result<bool, Error> {
let BlockchainResponse::KeyImagesSpent(is_spent) = blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::KeyImagesSpent(key_images))
.await?
else {
unreachable!();
};
Ok(is_spent)
}
/// [`BlockchainReadRequest::CompactChainHistory`]
pub(super) async fn compact_chain_history(
mut blockchain_read: BlockchainReadHandle,
) -> Result<(Vec<[u8; 32]>, u128), Error> {
let BlockchainResponse::CompactChainHistory {
block_ids,
cumulative_difficulty,
} = blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::CompactChainHistory)
.await?
else {
unreachable!();
};
Ok((block_ids, cumulative_difficulty))
}
/// [`BlockchainReadRequest::FindFirstUnknown`]
pub(super) async fn find_first_unknown(
mut blockchain_read: BlockchainReadHandle,
hashes: Vec<[u8; 32]>,
) -> Result<Option<(usize, u64)>, Error> {
let BlockchainResponse::FindFirstUnknown(resp) = blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::FindFirstUnknown(hashes))
.await?
else {
unreachable!();
};
Ok(resp.map(|(index, height)| (index, usize_to_u64(height))))
}
/// [`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!();
};
Ok(usize_to_u64(tx_count))
}
/// [`BlockchainReadRequest::DatabaseSize`]
pub(super) async fn database_size(
mut blockchain_read: BlockchainReadHandle,
) -> Result<(u64, u64), Error> {
let BlockchainResponse::DatabaseSize {
database_size,
free_space,
} = blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::DatabaseSize)
.await?
else {
unreachable!();
};
Ok((database_size, free_space))
}
/// [`BlockchainReadRequest::Difficulty`]
pub(super) async fn difficulty(
mut blockchain_read: BlockchainReadHandle,
block_height: u64,
) -> Result<u128, Error> {
let BlockchainResponse::Difficulty(difficulty) = blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::Difficulty(u64_to_usize(
block_height,
)))
.await?
else {
unreachable!();
};
Ok(difficulty)
}
/// [`BlockchainReadRequest::OutputHistogram`]
pub(super) async fn output_histogram(
mut blockchain_read: BlockchainReadHandle,
) -> Result<(), Error> {
let BlockchainResponse::OutputHistogram(_) = blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::OutputHistogram)
.await?
else {
unreachable!();
};
Ok(todo!())
}
/// [`BlockchainReadRequest::CoinbaseTxSum`]
pub(super) async fn coinbase_tx_sum(
mut blockchain_read: BlockchainReadHandle,
) -> Result<(), Error> {
let BlockchainResponse::CoinbaseTxSum(_) = blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::CoinbaseTxSum)
.await?
else {
unreachable!();
};
Ok(todo!())
}
/// [`BlockchainReadRequest::MinerData`]
pub(super) async fn miner_data(mut blockchain_read: BlockchainReadHandle) -> Result<(), Error> {
let BlockchainResponse::MinerData(_) = blockchain_read
.ready()
.await?
.call(BlockchainReadRequest::MinerData)
.await?
else {
unreachable!();
};
Ok(todo!())
}

View file

@ -22,24 +22,17 @@ use cuprate_types::{
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
pub(super) async fn pop_blocks(&mut self) -> Result<(u64, [u8; 32]), Error> {
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!())
}
/// TODO: doc enum message
pub(super) async fn pop_blocks() -> Result<(u64, [u8; 32]), Error> {
Ok(todo!())
}
/// TODO: doc enum message
pub(super) async fn prune() -> Result<(), Error> {
Ok(todo!())
}
/// TODO: doc enum message
pub(super) async fn pruned() -> Result<bool, Error> {
Ok(todo!())
}

View file

@ -326,7 +326,7 @@ pub enum BlockChainContextRequest {
FeeEstimate {
/// TODO
grace_blocks: u64,
}
},
}
pub enum BlockChainContextResponse {

View file

@ -325,7 +325,8 @@ impl<D: Database + Clone + Send + 'static> ContextTask<D> {
self.alt_chain_cache_map.add_alt_cache(prev_id, cache);
BlockChainContextResponse::Ok
}
BlockChainContextRequest::HardForkInfo(_) | BlockChainContextRequest::FeeEstimate { .. } => {
BlockChainContextRequest::HardForkInfo(_)
| BlockChainContextRequest::FeeEstimate { .. } => {
todo!()
}
})

View file

@ -107,6 +107,12 @@ 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!(),
}
/* 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`].
AltBlocksInChain(ChainId),
/// TODO
TotalTxCount,
/// TODO
DatabaseSize,
// TODO
Difficulty(usize),
/// TODO
OutputHistogram,
/// TODO
CoinbaseTxSum,
/// TODO
MinerData,
}
//---------------------------------------------------------------------------------------------------- WriteRequest
@ -227,6 +245,36 @@ pub enum BlockchainResponse {
/// Contains all the alt blocks in the alt-chain in chronological order.
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
/// A generic Ok response to indicate a request was successfully handled.
///
@ -236,6 +284,7 @@ pub enum BlockchainResponse {
/// - [`BlockchainWriteRequest::ReverseReorg`]
/// - [`BlockchainWriteRequest::FlushAltBlocks`]
Ok,
/// The response for [`BlockchainWriteRequest::PopBlocks`].
///
/// The inner value is the alt-chain ID for the old main chain blocks.