p2p msg changes, docs
Some checks are pending
Audit / audit (push) Waiting to run
Deny / audit (push) Waiting to run

This commit is contained in:
hinto.janai 2024-10-01 21:03:56 -04:00
parent 3fb69f23cb
commit f9ff59345c
No known key found for this signature in database
GPG key ID: D47CE05FA175A499
17 changed files with 196 additions and 167 deletions

View file

@ -3,6 +3,7 @@
#![allow(
unused_imports,
unreachable_pub,
unreachable_code,
unused_crate_dependencies,
dead_code,
unused_variables,

View file

@ -8,4 +8,4 @@ mod json;
mod other;
mod request;
pub use handler::{CupratedRpcHandler, CupratedRpcHandlerState};
pub use handler::CupratedRpcHandler;

View file

@ -10,11 +10,11 @@ use cuprate_rpc_types::{
json::{GetOutputDistributionRequest, GetOutputDistributionResponse},
};
use crate::rpc::CupratedRpcHandlerState;
use crate::rpc::CupratedRpcHandler;
/// Map a [`BinRequest`] to the function that will lead to a [`BinResponse`].
pub(super) async fn map_request(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: BinRequest,
) -> Result<BinResponse, Error> {
use BinRequest as Req;
@ -36,49 +36,49 @@ pub(super) async fn map_request(
}
async fn get_blocks(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetBlocksRequest,
) -> Result<GetBlocksResponse, Error> {
todo!()
}
async fn get_blocks_by_height(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetBlocksByHeightRequest,
) -> Result<GetBlocksByHeightResponse, Error> {
todo!()
}
async fn get_hashes(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetHashesRequest,
) -> Result<GetHashesResponse, Error> {
todo!()
}
async fn get_output_indexes(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetOutputIndexesRequest,
) -> Result<GetOutputIndexesResponse, Error> {
todo!()
}
async fn get_outs(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetOutsRequest,
) -> Result<GetOutsResponse, Error> {
todo!()
}
async fn get_transaction_pool_hashes(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetTransactionPoolHashesRequest,
) -> Result<GetTransactionPoolHashesResponse, Error> {
todo!()
}
async fn get_output_distribution(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetOutputDistributionRequest,
) -> Result<GetOutputDistributionResponse, Error> {
todo!()

View file

@ -22,35 +22,44 @@ use crate::rpc::{bin, json, other};
#[derive(Clone)]
#[expect(clippy::large_enum_variant)]
pub enum BlockchainManagerRequest {
/// Pop blocks off the top of the blockchain.
///
/// Input is the amount of blocks to pop.
PopBlocks { amount: usize },
/// TODO
/// Start pruning the blockchain.
Prune,
/// TODO
/// Is the blockchain pruned?
Pruned,
/// TODO
/// Relay a block to the network.
RelayBlock(Block),
/// TODO
/// Is the blockchain in the middle of syncing?
///
/// This returning `false` does not necessarily
/// mean [`BlockchainManagerRequest::Synced`] will
/// return `true`, for example, if the network has been
/// cut off and we have no peers, this will return `false`,
/// however, [`BlockchainManagerRequest::Synced`] may return
/// `true` if the latest known chain tip is equal to our height.
Syncing,
/// TODO
/// Is the blockchain fully synced?
Synced,
/// TODO
/// Current target block time.
Target,
/// TODO
/// The height of the next block in the chain.
TargetHeight,
}
/// TODO: use real type when public.
#[derive(Clone)]
pub enum BlockchainManagerResponse {
/// TODO
/// General OK response.
///
/// Response to:
/// - [`BlockchainManagerRequest::Prune`]
@ -58,33 +67,21 @@ pub enum BlockchainManagerResponse {
Ok,
/// Response to [`BlockchainManagerRequest::PopBlocks`]
///
/// TODO
PopBlocks { new_height: usize },
/// Response to [`BlockchainManagerRequest::Pruned`]
///
/// TODO
Pruned(bool),
/// Response to [`BlockchainManagerRequest::Syncing`]
///
/// TODO
Syncing(bool),
/// Response to [`BlockchainManagerRequest::Synced`]
///
/// TODO
Synced(bool),
/// Response to [`BlockchainManagerRequest::Target`]
///
/// TODO
Target { height: usize },
Target(std::time::Duration),
/// Response to [`BlockchainManagerRequest::TargetHeight`]
///
/// TODO
TargetHeight { height: usize },
}
@ -97,19 +94,10 @@ pub type BlockchainManagerHandle = cuprate_database_service::DatabaseReadService
/// TODO
#[derive(Clone)]
pub struct CupratedRpcHandler {
/// State needed for request -> response mapping.
pub state: CupratedRpcHandlerState,
}
/// TODO
#[derive(Clone)]
pub struct CupratedRpcHandlerState {
/// Should this RPC server be [restricted](RpcHandler::restricted)?
//
// INVARIANT:
// We don't need to include this in `state` and check for
// `self.is_restricted()` because `cuprate-rpc-interface` handles that.
pub restricted: bool,
///
/// This is not `pub` on purpose, as it should not be mutated after [`Self::new`].
restricted: bool,
/// Read handle to the blockchain database.
pub blockchain_read: BlockchainReadHandle,
@ -129,14 +117,28 @@ pub struct CupratedRpcHandlerState {
impl CupratedRpcHandler {
/// TODO
pub fn init() {
todo!()
pub const fn new(
restricted: bool,
blockchain_read: BlockchainReadHandle,
blockchain_write: BlockchainWriteHandle,
blockchain_manager: BlockchainManagerHandle,
txpool_read: TxpoolReadHandle,
txpool_write: TxpoolWriteHandle,
) -> Self {
Self {
restricted,
blockchain_read,
blockchain_write,
blockchain_manager,
txpool_read,
txpool_write,
}
}
}
impl RpcHandler for CupratedRpcHandler {
fn restricted(&self) -> bool {
self.state.restricted
self.restricted
}
}
@ -150,7 +152,7 @@ impl Service<JsonRpcRequest> for CupratedRpcHandler {
}
fn call(&mut self, request: JsonRpcRequest) -> Self::Future {
let state = CupratedRpcHandlerState::clone(&self.state);
let state = self.clone();
Box::pin(json::map_request(state, request))
}
}
@ -165,7 +167,7 @@ impl Service<BinRequest> for CupratedRpcHandler {
}
fn call(&mut self, request: BinRequest) -> Self::Future {
let state = CupratedRpcHandlerState::clone(&self.state);
let state = self.clone();
Box::pin(bin::map_request(state, request))
}
}
@ -180,7 +182,7 @@ impl Service<OtherRequest> for CupratedRpcHandler {
}
fn call(&mut self, request: OtherRequest) -> Self::Future {
let state = CupratedRpcHandlerState::clone(&self.state);
let state = self.clone();
Box::pin(other::map_request(state, request))
}
}

View file

@ -23,11 +23,11 @@ use cuprate_rpc_types::json::{
SyncInfoRequest, SyncInfoResponse,
};
use crate::rpc::CupratedRpcHandlerState;
use crate::rpc::CupratedRpcHandler;
/// Map a [`JsonRpcRequest`] to the function that will lead to a [`JsonRpcResponse`].
pub(super) async fn map_request(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: JsonRpcRequest,
) -> Result<JsonRpcResponse, Error> {
use JsonRpcRequest as Req;
@ -84,210 +84,210 @@ pub(super) async fn map_request(
}
async fn get_block_count(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetBlockCountRequest,
) -> Result<GetBlockCountResponse, Error> {
todo!()
}
async fn on_get_block_hash(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: OnGetBlockHashRequest,
) -> Result<OnGetBlockHashResponse, Error> {
todo!()
}
async fn submit_block(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: SubmitBlockRequest,
) -> Result<SubmitBlockResponse, Error> {
todo!()
}
async fn generate_blocks(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GenerateBlocksRequest,
) -> Result<GenerateBlocksResponse, Error> {
todo!()
}
async fn get_last_block_header(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetLastBlockHeaderRequest,
) -> Result<GetLastBlockHeaderResponse, Error> {
todo!()
}
async fn get_block_header_by_hash(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetBlockHeaderByHashRequest,
) -> Result<GetBlockHeaderByHashResponse, Error> {
todo!()
}
async fn get_block_header_by_height(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetBlockHeaderByHeightRequest,
) -> Result<GetBlockHeaderByHeightResponse, Error> {
todo!()
}
async fn get_block_headers_range(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetBlockHeadersRangeRequest,
) -> Result<GetBlockHeadersRangeResponse, Error> {
todo!()
}
async fn get_block(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetBlockRequest,
) -> Result<GetBlockResponse, Error> {
todo!()
}
async fn get_connections(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetConnectionsRequest,
) -> Result<GetConnectionsResponse, Error> {
todo!()
}
async fn get_info(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetInfoRequest,
) -> Result<GetInfoResponse, Error> {
todo!()
}
async fn hard_fork_info(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: HardForkInfoRequest,
) -> Result<HardForkInfoResponse, Error> {
todo!()
}
async fn set_bans(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: SetBansRequest,
) -> Result<SetBansResponse, Error> {
todo!()
}
async fn get_bans(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetBansRequest,
) -> Result<GetBansResponse, Error> {
todo!()
}
async fn banned(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: BannedRequest,
) -> Result<BannedResponse, Error> {
todo!()
}
async fn flush_transaction_pool(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: FlushTransactionPoolRequest,
) -> Result<FlushTransactionPoolResponse, Error> {
todo!()
}
async fn get_output_histogram(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetOutputHistogramRequest,
) -> Result<GetOutputHistogramResponse, Error> {
todo!()
}
async fn get_coinbase_tx_sum(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetCoinbaseTxSumRequest,
) -> Result<GetCoinbaseTxSumResponse, Error> {
todo!()
}
async fn get_version(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetVersionRequest,
) -> Result<GetVersionResponse, Error> {
todo!()
}
async fn get_fee_estimate(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetFeeEstimateRequest,
) -> Result<GetFeeEstimateResponse, Error> {
todo!()
}
async fn get_alternate_chains(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetAlternateChainsRequest,
) -> Result<GetAlternateChainsResponse, Error> {
todo!()
}
async fn relay_tx(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: RelayTxRequest,
) -> Result<RelayTxResponse, Error> {
todo!()
}
async fn sync_info(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: SyncInfoRequest,
) -> Result<SyncInfoResponse, Error> {
todo!()
}
async fn get_transaction_pool_backlog(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetTransactionPoolBacklogRequest,
) -> Result<GetTransactionPoolBacklogResponse, Error> {
todo!()
}
async fn get_miner_data(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetMinerDataRequest,
) -> Result<GetMinerDataResponse, Error> {
todo!()
}
async fn prune_blockchain(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: PruneBlockchainRequest,
) -> Result<PruneBlockchainResponse, Error> {
todo!()
}
async fn calc_pow(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: CalcPowRequest,
) -> Result<CalcPowResponse, Error> {
todo!()
}
async fn flush_cache(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: FlushCacheRequest,
) -> Result<FlushCacheResponse, Error> {
todo!()
}
async fn add_aux_pow(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: AddAuxPowRequest,
) -> Result<AddAuxPowResponse, Error> {
todo!()
}
async fn get_tx_ids_loose(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetTxIdsLooseRequest,
) -> Result<GetTxIdsLooseResponse, Error> {
todo!()

View file

@ -17,11 +17,11 @@ use cuprate_rpc_types::other::{
StopDaemonResponse, StopMiningRequest, StopMiningResponse, UpdateRequest, UpdateResponse,
};
use crate::rpc::CupratedRpcHandlerState;
use crate::rpc::CupratedRpcHandler;
/// Map a [`OtherRequest`] to the function that will lead to a [`OtherResponse`].
pub(super) async fn map_request(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: OtherRequest,
) -> Result<OtherResponse, Error> {
use OtherRequest as Req;
@ -71,189 +71,189 @@ pub(super) async fn map_request(
}
async fn get_height(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetHeightRequest,
) -> Result<GetHeightResponse, Error> {
todo!()
}
async fn get_transactions(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetTransactionsRequest,
) -> Result<GetTransactionsResponse, Error> {
todo!()
}
async fn get_alt_blocks_hashes(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetAltBlocksHashesRequest,
) -> Result<GetAltBlocksHashesResponse, Error> {
todo!()
}
async fn is_key_image_spent(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: IsKeyImageSpentRequest,
) -> Result<IsKeyImageSpentResponse, Error> {
todo!()
}
async fn send_raw_transaction(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: SendRawTransactionRequest,
) -> Result<SendRawTransactionResponse, Error> {
todo!()
}
async fn start_mining(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: StartMiningRequest,
) -> Result<StartMiningResponse, Error> {
todo!()
}
async fn stop_mining(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: StopMiningRequest,
) -> Result<StopMiningResponse, Error> {
todo!()
}
async fn mining_status(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: MiningStatusRequest,
) -> Result<MiningStatusResponse, Error> {
todo!()
}
async fn save_bc(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: SaveBcRequest,
) -> Result<SaveBcResponse, Error> {
todo!()
}
async fn get_peer_list(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetPeerListRequest,
) -> Result<GetPeerListResponse, Error> {
todo!()
}
async fn set_log_hash_rate(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: SetLogHashRateRequest,
) -> Result<SetLogHashRateResponse, Error> {
todo!()
}
async fn set_log_level(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: SetLogLevelRequest,
) -> Result<SetLogLevelResponse, Error> {
todo!()
}
async fn set_log_categories(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: SetLogCategoriesRequest,
) -> Result<SetLogCategoriesResponse, Error> {
todo!()
}
async fn set_bootstrap_daemon(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: SetBootstrapDaemonRequest,
) -> Result<SetBootstrapDaemonResponse, Error> {
todo!()
}
async fn get_transaction_pool(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetTransactionPoolRequest,
) -> Result<GetTransactionPoolResponse, Error> {
todo!()
}
async fn get_transaction_pool_stats(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetTransactionPoolStatsRequest,
) -> Result<GetTransactionPoolStatsResponse, Error> {
todo!()
}
async fn stop_daemon(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: StopDaemonRequest,
) -> Result<StopDaemonResponse, Error> {
todo!()
}
async fn get_limit(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetLimitRequest,
) -> Result<GetLimitResponse, Error> {
todo!()
}
async fn set_limit(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: SetLimitRequest,
) -> Result<SetLimitResponse, Error> {
todo!()
}
async fn out_peers(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: OutPeersRequest,
) -> Result<OutPeersResponse, Error> {
todo!()
}
async fn in_peers(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: InPeersRequest,
) -> Result<InPeersResponse, Error> {
todo!()
}
async fn get_net_stats(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetNetStatsRequest,
) -> Result<GetNetStatsResponse, Error> {
todo!()
}
async fn get_outs(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetOutsRequest,
) -> Result<GetOutsResponse, Error> {
todo!()
}
async fn update(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: UpdateRequest,
) -> Result<UpdateResponse, Error> {
todo!()
}
async fn pop_blocks(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: PopBlocksRequest,
) -> Result<PopBlocksResponse, Error> {
todo!()
}
async fn get_transaction_pool_hashes(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetTransactionPoolHashesRequest,
) -> Result<GetTransactionPoolHashesResponse, Error> {
todo!()
}
async fn get_public_nodes(
state: CupratedRpcHandlerState,
state: CupratedRpcHandler,
request: GetPublicNodesRequest,
) -> Result<GetPublicNodesResponse, Error> {
todo!()

View file

@ -1,7 +1,7 @@
//! Convenience functions for requests/responses.
//!
//! This module implements many methods on
//! [`CupratedRpcHandlerState`](crate::rpc::CupratedRpcHandlerState)
//! This module implements many methods for
//! [`CupratedRpcHandler`](crate::rpc::CupratedRpcHandler)
//! that are simple wrappers around the request/response API provided
//! by the multiple [`tower::Service`]s.
//!

View file

@ -50,7 +50,7 @@ pub(super) async fn connection_count<Z: NetworkZone>(
/// [`AddressBookRequest::SetBan`]
pub(super) async fn set_ban<Z: NetworkZone>(
address_book: &mut impl AddressBook<Z>,
peer: Infallible,
peer: cuprate_p2p_core::ban::SetBan<Z::Addr>,
) -> Result<(), Error> {
let AddressBookResponse::Ok = address_book
.ready()
@ -69,9 +69,9 @@ pub(super) async fn set_ban<Z: NetworkZone>(
/// [`AddressBookRequest::GetBan`]
pub(super) async fn get_ban<Z: NetworkZone>(
address_book: &mut impl AddressBook<Z>,
peer: Infallible,
) -> Result<(), Error> {
let AddressBookResponse::GetBan(ban) = address_book
peer: Z::Addr,
) -> Result<Option<std::time::Instant>, Error> {
let AddressBookResponse::GetBan { unban_instant } = address_book
.ready()
.await
.expect("TODO")
@ -82,7 +82,7 @@ pub(super) async fn get_ban<Z: NetworkZone>(
unreachable!();
};
Ok(())
Ok(unban_instant)
}
/// [`AddressBookRequest::GetBans`]

View file

@ -1,4 +1,4 @@
//! Functions for [`BlockchainReadRequest`] and [`BlockchainWriteRequest`].
//! Functions for [`BlockchainReadRequest`].
use std::{
collections::{HashMap, HashSet},

View file

@ -10,7 +10,7 @@ use crate::rpc::handler::{
BlockchainManagerHandle, BlockchainManagerRequest, BlockchainManagerResponse,
};
/// [`BlockchainManagerResponse::PopBlocks`]
/// [`BlockchainManagerRequest::PopBlocks`]
pub(super) async fn pop_blocks(
blockchain_manager: &mut BlockchainManagerHandle,
amount: u64,
@ -29,7 +29,7 @@ pub(super) async fn pop_blocks(
Ok(usize_to_u64(new_height))
}
/// [`BlockchainManagerResponse::Prune`]
/// [`BlockchainManagerRequest::Prune`]
pub(super) async fn prune(blockchain_manager: &mut BlockchainManagerHandle) -> Result<(), Error> {
let BlockchainManagerResponse::Ok = blockchain_manager
.ready()
@ -43,7 +43,7 @@ pub(super) async fn prune(blockchain_manager: &mut BlockchainManagerHandle) -> R
Ok(())
}
/// [`BlockchainManagerResponse::Pruned`]
/// [`BlockchainManagerRequest::Pruned`]
pub(super) async fn pruned(
blockchain_manager: &mut BlockchainManagerHandle,
) -> Result<bool, Error> {
@ -59,7 +59,7 @@ pub(super) async fn pruned(
Ok(pruned)
}
/// [`BlockchainManagerResponse::RelayBlock`]
/// [`BlockchainManagerRequest::RelayBlock`]
pub(super) async fn relay_block(
blockchain_manager: &mut BlockchainManagerHandle,
block: Block,
@ -76,7 +76,7 @@ pub(super) async fn relay_block(
Ok(())
}
/// [`BlockchainManagerResponse::Syncing`]
/// [`BlockchainManagerRequest::Syncing`]
pub(super) async fn syncing(
blockchain_manager: &mut BlockchainManagerHandle,
) -> Result<bool, Error> {
@ -92,7 +92,7 @@ pub(super) async fn syncing(
Ok(syncing)
}
/// [`BlockchainManagerResponse::Synced`]
/// [`BlockchainManagerRequest::Synced`]
pub(super) async fn synced(
blockchain_manager: &mut BlockchainManagerHandle,
) -> Result<bool, Error> {
@ -108,9 +108,11 @@ pub(super) async fn synced(
Ok(syncing)
}
/// [`BlockchainManagerResponse::Target`]
pub(super) async fn target(blockchain_manager: &mut BlockchainManagerHandle) -> Result<u64, Error> {
let BlockchainManagerResponse::Target { height } = blockchain_manager
/// [`BlockchainManagerRequest::Target`]
pub(super) async fn target(
blockchain_manager: &mut BlockchainManagerHandle,
) -> Result<std::time::Duration, Error> {
let BlockchainManagerResponse::Target(target) = blockchain_manager
.ready()
.await?
.call(BlockchainManagerRequest::Target)
@ -119,10 +121,10 @@ pub(super) async fn target(blockchain_manager: &mut BlockchainManagerHandle) ->
unreachable!();
};
Ok(usize_to_u64(height))
Ok(target)
}
/// [`BlockchainManagerResponse::TargetHeight`]
/// [`BlockchainManagerRequest::TargetHeight`]
pub(super) async fn target_height(
blockchain_manager: &mut BlockchainManagerHandle,
) -> Result<u64, Error> {

View file

@ -43,7 +43,7 @@ pub(super) async fn size(txpool_read: &mut TxpoolReadHandle) -> Result<u64, Erro
Ok(usize_to_u64(size))
}
// [`::Flush`]
/// TODO
#[expect(clippy::needless_pass_by_ref_mut, reason = "TODO: remove after impl")]
pub(super) async fn flush(
txpool_read: &mut TxpoolReadHandle,

View file

@ -229,6 +229,15 @@ impl<Z: BorshNetworkZone> AddressBook<Z> {
self.banned_peers.contains_key(&peer.ban_id())
}
/// Checks when a peer will be unbanned.
///
/// - If the peer is banned, this returns [`Some`] containing
/// the [`Instant`] the peer will be unbanned
/// - If the peer is not banned, this returns [`None`]
fn peer_unban_instant(&self, peer: &Z::Addr) -> Option<Instant> {
self.banned_peers.get(&peer.ban_id()).copied()
}
fn handle_incoming_peer_list(
&mut self,
mut peer_list: Vec<ZoneSpecificPeerListEntryBase<Z::Addr>>,
@ -408,14 +417,15 @@ impl<Z: BorshNetworkZone> Service<AddressBookRequest<Z>> for AddressBook<Z> {
AddressBookRequest::GetWhitePeers(len) => {
Ok(AddressBookResponse::Peers(self.get_white_peers(len)))
}
AddressBookRequest::IsPeerBanned(addr) => Ok(AddressBookResponse::IsPeerBanned(
self.is_peer_banned(&addr),
)),
AddressBookRequest::GetBan(addr) => Ok(AddressBookResponse::GetBan {
unban_instant: self.peer_unban_instant(&addr).map(Instant::into_std),
}),
AddressBookRequest::PeerlistSize
| AddressBookRequest::ConnectionCount
| AddressBookRequest::SetBan(_)
| AddressBookRequest::GetBan(_)
| AddressBookRequest::GetBans => todo!("finish https://github.com/Cuprate/cuprate/pull/297"),
| AddressBookRequest::GetBans => {
todo!("finish https://github.com/Cuprate/cuprate/pull/297")
}
};
ready(response)

19
p2p/p2p-core/src/ban.rs Normal file
View file

@ -0,0 +1,19 @@
//! Data structures related to bans.
use std::time::{Duration, Instant};
use crate::NetZoneAddress;
/// TODO
pub struct SetBan<A: NetZoneAddress> {
pub address: A,
pub ban: bool,
pub duration: Duration,
}
/// TODO
pub struct BanState<A: NetZoneAddress> {
pub address: A,
pub banned: bool,
pub unban_instant: Instant,
}

View file

@ -105,12 +105,15 @@ impl<N: NetworkZone> Service<AddressBookRequest<N>> for DummyAddressBook {
AddressBookRequest::NewConnection { .. } | AddressBookRequest::IncomingPeerList(_) => {
AddressBookResponse::Ok
}
AddressBookRequest::IsPeerBanned(_) => AddressBookResponse::IsPeerBanned(false),
AddressBookRequest::GetBan(_) => AddressBookResponse::GetBan {
unban_instant: None,
},
AddressBookRequest::PeerlistSize
| AddressBookRequest::ConnectionCount
| AddressBookRequest::SetBan(_)
| AddressBookRequest::GetBan(_)
| AddressBookRequest::GetBans => todo!("finish https://github.com/Cuprate/cuprate/pull/297"),
| AddressBookRequest::GetBans => {
todo!("finish https://github.com/Cuprate/cuprate/pull/297")
}
}))
}
}

View file

@ -75,6 +75,7 @@ use cuprate_wire::{
NetworkAddress,
};
pub mod ban;
pub mod client;
mod constants;
pub mod error;

View file

@ -1,9 +1,13 @@
use std::time::Instant;
use cuprate_pruning::{PruningError, PruningSeed};
use cuprate_wire::{CoreSyncData, PeerListEntryBase};
use crate::{
client::InternalPeerID, handles::ConnectionHandle, NetZoneAddress, NetworkAddressIncorrectZone,
NetworkZone,
ban::{BanState, SetBan},
client::InternalPeerID,
handles::ConnectionHandle,
NetZoneAddress, NetworkAddressIncorrectZone, NetworkZone,
};
/// A request to the core sync service for our node's [`CoreSyncData`].
@ -111,22 +115,19 @@ pub enum AddressBookRequest<Z: NetworkZone> {
/// Gets the specified number of white peers, or less if we don't have enough.
GetWhitePeers(usize),
/// Checks if the given peer is banned.
IsPeerBanned(Z::Addr),
/// TODO
/// Get the amount of white & grey peers.
PeerlistSize,
/// TODO
/// Get the amount of incoming & outgoing connections.
ConnectionCount,
/// TODO: `cuprate_rpc_types::json::SetBanRequest` input
SetBan(std::convert::Infallible),
/// (Un)ban a peer.
SetBan(SetBan<Z::Addr>),
/// TODO
GetBan(std::convert::Infallible),
/// Checks if the given peer is banned.
GetBan(Z::Addr),
/// TODO
/// Get the state of all bans.
GetBans,
}
@ -148,28 +149,18 @@ pub enum AddressBookResponse<Z: NetworkZone> {
/// Response to [`AddressBookRequest::GetWhitePeers`].
Peers(Vec<ZoneSpecificPeerListEntryBase<Z::Addr>>),
/// Response to [`AddressBookRequest::IsPeerBanned`].
///
/// Contains `true` if the peer is banned.
IsPeerBanned(bool),
/// Response to [`AddressBookRequest::PeerlistSize`].
///
/// TODO
PeerlistSize { white: usize, grey: usize },
/// Response to [`AddressBookRequest::ConnectionCount`].
///
/// TODO
ConnectionCount { incoming: usize, outgoing: usize },
/// Response to [`AddressBookRequest::GetBan`].
///
/// TODO
GetBan(std::convert::Infallible),
/// This returns [`None`] if the peer is not banned,
/// else it returns how long the peer is banned for.
GetBan { unban_instant: Option<Instant> },
/// Response to [`AddressBookRequest::GetBans`].
///
/// TODO
GetBans(std::convert::Infallible),
GetBans(Vec<BanState<Z::Addr>>),
}

View file

@ -79,16 +79,16 @@ where
// If peer is banned, drop connection
if let Some(addr) = &addr {
let AddressBookResponse::IsPeerBanned(banned) = address_book
let AddressBookResponse::GetBan { unban_instant } = address_book
.ready()
.await?
.call(AddressBookRequest::IsPeerBanned(*addr))
.call(AddressBookRequest::GetBan(*addr))
.await?
else {
panic!("Address book returned incorrect response!");
};
if banned {
if unban_instant.is_some() {
continue;
}
}