mirror of
https://github.com/hinto-janai/cuprate.git
synced 2024-12-23 12:09:57 +00:00
Compare commits
No commits in common. "fd136b23f60bcd7cb4534a53268e93456fba7729" and "93be3be3652465402bd75e1d98be42e0cabc8a5a" have entirely different histories.
fd136b23f6
...
93be3be365
8 changed files with 63 additions and 54 deletions
|
@ -3,7 +3,6 @@
|
|||
use std::task::{Context, Poll};
|
||||
|
||||
use anyhow::Error;
|
||||
use cuprate_consensus::BlockChainContextService;
|
||||
use futures::future::BoxFuture;
|
||||
use monero_serai::block::Block;
|
||||
use tower::Service;
|
||||
|
@ -103,9 +102,6 @@ pub struct CupratedRpcHandler {
|
|||
/// Read handle to the blockchain database.
|
||||
pub blockchain_read: BlockchainReadHandle,
|
||||
|
||||
/// Handle to the blockchain context service.
|
||||
pub blockchain_context: BlockChainContextService,
|
||||
|
||||
/// Handle to the blockchain manager.
|
||||
pub blockchain_manager: BlockchainManagerHandle,
|
||||
|
||||
|
@ -121,7 +117,6 @@ impl CupratedRpcHandler {
|
|||
pub const fn new(
|
||||
restricted: bool,
|
||||
blockchain_read: BlockchainReadHandle,
|
||||
blockchain_context: BlockChainContextService,
|
||||
blockchain_manager: BlockchainManagerHandle,
|
||||
txpool_read: TxpoolReadHandle,
|
||||
txpool_manager: std::convert::Infallible,
|
||||
|
@ -129,7 +124,6 @@ impl CupratedRpcHandler {
|
|||
Self {
|
||||
restricted,
|
||||
blockchain_read,
|
||||
blockchain_context,
|
||||
blockchain_manager,
|
||||
txpool_read,
|
||||
txpool_manager,
|
||||
|
|
|
@ -38,11 +38,7 @@ use cuprate_rpc_types::{
|
|||
CORE_RPC_VERSION,
|
||||
};
|
||||
|
||||
use crate::rpc::{
|
||||
helper,
|
||||
request::{blockchain, blockchain_context, blockchain_manager},
|
||||
CupratedRpcHandler,
|
||||
};
|
||||
use crate::rpc::{helper, request::blockchain, CupratedRpcHandler};
|
||||
|
||||
/// Map a [`JsonRpcRequest`] to the function that will lead to a [`JsonRpcResponse`].
|
||||
pub(super) async fn map_request(
|
||||
|
@ -119,8 +115,7 @@ async fn on_get_block_hash(
|
|||
request: OnGetBlockHashRequest,
|
||||
) -> Result<OnGetBlockHashResponse, Error> {
|
||||
let [height] = request.block_height;
|
||||
let hash = blockchain::block_hash(&mut state.blockchain_read, height, todo!("access to chain"))
|
||||
.await?;
|
||||
let hash = blockchain::block_hash(&mut state.blockchain_read, height, todo!()).await?;
|
||||
let block_hash = hex::encode(hash);
|
||||
|
||||
Ok(OnGetBlockHashResponse { block_hash })
|
||||
|
@ -131,14 +126,21 @@ async fn submit_block(
|
|||
mut state: CupratedRpcHandler,
|
||||
request: SubmitBlockRequest,
|
||||
) -> Result<SubmitBlockResponse, Error> {
|
||||
// Parse hex into block.
|
||||
let [blob] = request.block_blob;
|
||||
|
||||
let limit = todo!(); //blockchain::cumulative_block_weight_limit(&mut state.blockchain_read).await?;
|
||||
|
||||
// if blob.len() > limit + BLOCK_SIZE_SANITY_LEEWAY {
|
||||
// return Err(anyhow!("Block size is too big, rejecting block"));
|
||||
// }
|
||||
|
||||
let bytes = hex::decode(blob)?;
|
||||
let block = Block::read(&mut bytes.as_slice())?;
|
||||
let block_id = hex::encode(block.hash());
|
||||
|
||||
// Attempt to relay the block.
|
||||
blockchain_manager::relay_block(&mut state.blockchain_manager, block).await?;
|
||||
// <https://github.com/monero-project/monero/blob/master/src/cryptonote_core/cryptonote_core.cpp#L1540>
|
||||
let block_id = todo!("submit block to DB");
|
||||
todo!("relay to P2P");
|
||||
todo!("send to txpool");
|
||||
|
||||
Ok(SubmitBlockResponse {
|
||||
base: ResponseBase::ok(),
|
||||
|
@ -381,7 +383,7 @@ async fn hard_fork_info(
|
|||
Ok(HardForkInfoResponse {
|
||||
base: AccessResponseBase::ok(),
|
||||
earliest_height: todo!(),
|
||||
enabled: todo!("hard_fork.is_latest() is not always correct"),
|
||||
enabled: hard_fork.is_current(),
|
||||
state: todo!(),
|
||||
threshold: todo!(),
|
||||
version: hard_fork.as_u8(),
|
||||
|
|
|
@ -12,7 +12,7 @@ use cuprate_p2p_core::{
|
|||
};
|
||||
|
||||
/// [`AddressBookRequest::PeerlistSize`]
|
||||
pub(crate) async fn peerlist_size<Z: NetworkZone>(
|
||||
pub(super) async fn peerlist_size<Z: NetworkZone>(
|
||||
address_book: &mut impl AddressBook<Z>,
|
||||
) -> Result<(u64, u64), Error> {
|
||||
let AddressBookResponse::PeerlistSize { white, grey } = address_book
|
||||
|
@ -30,7 +30,7 @@ pub(crate) async fn peerlist_size<Z: NetworkZone>(
|
|||
}
|
||||
|
||||
/// [`AddressBookRequest::ConnectionCount`]
|
||||
pub(crate) async fn connection_count<Z: NetworkZone>(
|
||||
pub(super) async fn connection_count<Z: NetworkZone>(
|
||||
address_book: &mut impl AddressBook<Z>,
|
||||
) -> Result<(u64, u64), Error> {
|
||||
let AddressBookResponse::ConnectionCount { incoming, outgoing } = address_book
|
||||
|
@ -48,7 +48,7 @@ pub(crate) async fn connection_count<Z: NetworkZone>(
|
|||
}
|
||||
|
||||
/// [`AddressBookRequest::SetBan`]
|
||||
pub(crate) async fn set_ban<Z: NetworkZone>(
|
||||
pub(super) async fn set_ban<Z: NetworkZone>(
|
||||
address_book: &mut impl AddressBook<Z>,
|
||||
peer: cuprate_p2p_core::ban::SetBan<Z::Addr>,
|
||||
) -> Result<(), Error> {
|
||||
|
@ -67,7 +67,7 @@ pub(crate) async fn set_ban<Z: NetworkZone>(
|
|||
}
|
||||
|
||||
/// [`AddressBookRequest::GetBan`]
|
||||
pub(crate) async fn get_ban<Z: NetworkZone>(
|
||||
pub(super) async fn get_ban<Z: NetworkZone>(
|
||||
address_book: &mut impl AddressBook<Z>,
|
||||
peer: Z::Addr,
|
||||
) -> Result<Option<std::time::Instant>, Error> {
|
||||
|
@ -86,7 +86,7 @@ pub(crate) async fn get_ban<Z: NetworkZone>(
|
|||
}
|
||||
|
||||
/// [`AddressBookRequest::GetBans`]
|
||||
pub(crate) async fn get_bans<Z: NetworkZone>(
|
||||
pub(super) async fn get_bans<Z: NetworkZone>(
|
||||
address_book: &mut impl AddressBook<Z>,
|
||||
) -> Result<(), Error> {
|
||||
let AddressBookResponse::GetBans(bans) = address_book
|
||||
|
|
|
@ -12,8 +12,9 @@ use cuprate_consensus::context::{
|
|||
use cuprate_types::{FeeEstimate, HardFork, HardForkInfo};
|
||||
|
||||
/// [`BlockChainContextRequest::Context`].
|
||||
pub(crate) async fn context(
|
||||
pub(super) async fn context(
|
||||
service: &mut BlockChainContextService,
|
||||
height: u64,
|
||||
) -> Result<BlockChainContext, Error> {
|
||||
let BlockChainContextResponse::Context(context) = service
|
||||
.ready()
|
||||
|
@ -30,7 +31,7 @@ pub(crate) async fn context(
|
|||
}
|
||||
|
||||
/// [`BlockChainContextRequest::HardForkInfo`].
|
||||
pub(crate) async fn hard_fork_info(
|
||||
pub(super) async fn hard_fork_info(
|
||||
service: &mut BlockChainContextService,
|
||||
hard_fork: HardFork,
|
||||
) -> Result<HardForkInfo, Error> {
|
||||
|
@ -49,7 +50,7 @@ pub(crate) async fn hard_fork_info(
|
|||
}
|
||||
|
||||
/// [`BlockChainContextRequest::FeeEstimate`].
|
||||
pub(crate) async fn fee_estimate(
|
||||
pub(super) async fn fee_estimate(
|
||||
service: &mut BlockChainContextService,
|
||||
grace_blocks: u64,
|
||||
) -> Result<FeeEstimate, Error> {
|
||||
|
|
|
@ -11,7 +11,7 @@ use crate::rpc::handler::{
|
|||
};
|
||||
|
||||
/// [`BlockchainManagerRequest::PopBlocks`]
|
||||
pub(crate) async fn pop_blocks(
|
||||
pub(super) async fn pop_blocks(
|
||||
blockchain_manager: &mut BlockchainManagerHandle,
|
||||
amount: u64,
|
||||
) -> Result<u64, Error> {
|
||||
|
@ -30,7 +30,7 @@ pub(crate) async fn pop_blocks(
|
|||
}
|
||||
|
||||
/// [`BlockchainManagerRequest::Prune`]
|
||||
pub(crate) async fn prune(blockchain_manager: &mut BlockchainManagerHandle) -> Result<(), Error> {
|
||||
pub(super) async fn prune(blockchain_manager: &mut BlockchainManagerHandle) -> Result<(), Error> {
|
||||
let BlockchainManagerResponse::Ok = blockchain_manager
|
||||
.ready()
|
||||
.await?
|
||||
|
@ -44,7 +44,7 @@ pub(crate) async fn prune(blockchain_manager: &mut BlockchainManagerHandle) -> R
|
|||
}
|
||||
|
||||
/// [`BlockchainManagerRequest::Pruned`]
|
||||
pub(crate) async fn pruned(
|
||||
pub(super) async fn pruned(
|
||||
blockchain_manager: &mut BlockchainManagerHandle,
|
||||
) -> Result<bool, Error> {
|
||||
let BlockchainManagerResponse::Pruned(pruned) = blockchain_manager
|
||||
|
@ -60,7 +60,7 @@ pub(crate) async fn pruned(
|
|||
}
|
||||
|
||||
/// [`BlockchainManagerRequest::RelayBlock`]
|
||||
pub(crate) async fn relay_block(
|
||||
pub(super) async fn relay_block(
|
||||
blockchain_manager: &mut BlockchainManagerHandle,
|
||||
block: Block,
|
||||
) -> Result<(), Error> {
|
||||
|
@ -77,7 +77,7 @@ pub(crate) async fn relay_block(
|
|||
}
|
||||
|
||||
/// [`BlockchainManagerRequest::Syncing`]
|
||||
pub(crate) async fn syncing(
|
||||
pub(super) async fn syncing(
|
||||
blockchain_manager: &mut BlockchainManagerHandle,
|
||||
) -> Result<bool, Error> {
|
||||
let BlockchainManagerResponse::Syncing(syncing) = blockchain_manager
|
||||
|
@ -93,7 +93,7 @@ pub(crate) async fn syncing(
|
|||
}
|
||||
|
||||
/// [`BlockchainManagerRequest::Synced`]
|
||||
pub(crate) async fn synced(
|
||||
pub(super) async fn synced(
|
||||
blockchain_manager: &mut BlockchainManagerHandle,
|
||||
) -> Result<bool, Error> {
|
||||
let BlockchainManagerResponse::Synced(syncing) = blockchain_manager
|
||||
|
@ -109,7 +109,7 @@ pub(crate) async fn synced(
|
|||
}
|
||||
|
||||
/// [`BlockchainManagerRequest::Target`]
|
||||
pub(crate) async fn target(
|
||||
pub(super) async fn target(
|
||||
blockchain_manager: &mut BlockchainManagerHandle,
|
||||
) -> Result<std::time::Duration, Error> {
|
||||
let BlockchainManagerResponse::Target(target) = blockchain_manager
|
||||
|
@ -125,7 +125,7 @@ pub(crate) async fn target(
|
|||
}
|
||||
|
||||
/// [`BlockchainManagerRequest::TargetHeight`]
|
||||
pub(crate) async fn target_height(
|
||||
pub(super) async fn target_height(
|
||||
blockchain_manager: &mut BlockchainManagerHandle,
|
||||
) -> Result<u64, Error> {
|
||||
let BlockchainManagerResponse::TargetHeight { height } = blockchain_manager
|
||||
|
|
|
@ -15,7 +15,7 @@ use cuprate_txpool::{
|
|||
};
|
||||
|
||||
/// [`TxpoolReadRequest::Backlog`]
|
||||
pub(crate) async fn backlog(txpool_read: &mut TxpoolReadHandle) -> Result<Vec<TxEntry>, Error> {
|
||||
pub(super) async fn backlog(txpool_read: &mut TxpoolReadHandle) -> Result<Vec<TxEntry>, Error> {
|
||||
let TxpoolReadResponse::Backlog(tx_entries) = txpool_read
|
||||
.ready()
|
||||
.await
|
||||
|
@ -31,7 +31,7 @@ pub(crate) async fn backlog(txpool_read: &mut TxpoolReadHandle) -> Result<Vec<Tx
|
|||
}
|
||||
|
||||
/// [`TxpoolReadRequest::Size`]
|
||||
pub(crate) async fn size(txpool_read: &mut TxpoolReadHandle) -> Result<u64, Error> {
|
||||
pub(super) async fn size(txpool_read: &mut TxpoolReadHandle) -> Result<u64, Error> {
|
||||
let TxpoolReadResponse::Size(size) = txpool_read
|
||||
.ready()
|
||||
.await
|
||||
|
@ -48,7 +48,7 @@ pub(crate) async fn size(txpool_read: &mut TxpoolReadHandle) -> Result<u64, Erro
|
|||
|
||||
/// TODO
|
||||
#[expect(clippy::needless_pass_by_ref_mut, reason = "TODO: remove after impl")]
|
||||
pub(crate) async fn flush(
|
||||
pub(super) async fn flush(
|
||||
txpool_read: &mut TxpoolReadHandle,
|
||||
tx_hashes: Vec<[u8; 32]>,
|
||||
) -> Result<(), Error> {
|
||||
|
|
|
@ -820,7 +820,7 @@ define_request_and_response! {
|
|||
HardForkInfo,
|
||||
|
||||
#[doc = serde_doc_test!(
|
||||
HARD_FORK_INFO_REQUEST => HardForkInfoRequest {
|
||||
HARD_FORK_INFO => HardForkInfo {
|
||||
version: 16,
|
||||
}
|
||||
)]
|
||||
|
@ -836,9 +836,9 @@ define_request_and_response! {
|
|||
enabled: true,
|
||||
state: 0,
|
||||
threshold: 0,
|
||||
version: 16,
|
||||
version: 3,
|
||||
votes: 10080,
|
||||
voting: 16,
|
||||
voting: 3,
|
||||
window: 10080
|
||||
}
|
||||
)]
|
||||
|
|
|
@ -73,13 +73,13 @@ pub enum HardFork {
|
|||
}
|
||||
|
||||
impl HardFork {
|
||||
/// The latest [`HardFork`].
|
||||
/// The current [`HardFork`].
|
||||
///
|
||||
/// ```rust
|
||||
/// # use cuprate_types::HardFork;
|
||||
/// assert_eq!(HardFork::LATEST, HardFork::V16);
|
||||
/// assert_eq!(HardFork::CURRENT, HardFork::V16);
|
||||
/// ```
|
||||
pub const LATEST: Self = Self::VARIANTS[Self::COUNT - 1];
|
||||
pub const CURRENT: Self = Self::VARIANTS[Self::COUNT - 1];
|
||||
|
||||
/// Returns the hard-fork for a blocks [`BlockHeader::hardfork_version`] field.
|
||||
///
|
||||
|
@ -101,9 +101,21 @@ impl HardFork {
|
|||
/// ```
|
||||
#[inline]
|
||||
pub const fn from_version(version: u8) -> Result<Self, HardForkError> {
|
||||
match Self::from_repr(version) {
|
||||
Some(this) => Ok(this),
|
||||
None => Err(HardForkError::HardForkUnknown),
|
||||
#[expect(
|
||||
clippy::cast_possible_truncation,
|
||||
reason = "we check that `HardFork::COUNT` fits into a `u8`."
|
||||
)]
|
||||
const COUNT_AS_U8: u8 = {
|
||||
const COUNT: usize = HardFork::COUNT;
|
||||
assert!(COUNT <= u8::MAX as usize);
|
||||
COUNT as u8
|
||||
};
|
||||
|
||||
if version == 0 || version > COUNT_AS_U8 {
|
||||
Err(HardForkError::HardForkUnknown)
|
||||
} else {
|
||||
// INVARIANT: we've proved above that `version` is in a valid range.
|
||||
Ok(Self::VARIANTS[(version - 1) as usize])
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -116,7 +128,7 @@ impl HardFork {
|
|||
/// # use strum::VariantArray;
|
||||
/// // 0 is interpreted as 1.
|
||||
/// assert_eq!(HardFork::from_vote(0), HardFork::V1);
|
||||
/// // Unknown defaults to `LATEST`.
|
||||
/// // Unknown defaults to `CURRENT`.
|
||||
/// assert_eq!(HardFork::from_vote(17), HardFork::V16);
|
||||
///
|
||||
/// for (vote, hf) in HardFork::VARIANTS.iter().enumerate() {
|
||||
|
@ -131,7 +143,7 @@ impl HardFork {
|
|||
Self::V1
|
||||
} else {
|
||||
// This must default to the latest hard-fork!
|
||||
Self::from_version(vote).unwrap_or(Self::LATEST)
|
||||
Self::from_version(vote).unwrap_or(Self::CURRENT)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -176,21 +188,21 @@ impl HardFork {
|
|||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if `self` is [`Self::LATEST`].
|
||||
/// Returns `true` if `self` is [`Self::CURRENT`].
|
||||
///
|
||||
/// ```rust
|
||||
/// # use cuprate_types::HardFork;
|
||||
/// # use strum::VariantArray;
|
||||
///
|
||||
/// for hf in HardFork::VARIANTS.iter() {
|
||||
/// if *hf == HardFork::LATEST {
|
||||
/// assert!(hf.is_latest());
|
||||
/// if *hf == HardFork::CURRENT {
|
||||
/// assert!(hf.is_current());
|
||||
/// } else {
|
||||
/// assert!(!hf.is_latest());
|
||||
/// assert!(!hf.is_current());
|
||||
/// }
|
||||
/// }
|
||||
/// ```
|
||||
pub const fn is_latest(self) -> bool {
|
||||
matches!(self, Self::LATEST)
|
||||
pub const fn is_current(self) -> bool {
|
||||
matches!(self, Self::CURRENT)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue