Skip to content

Commit

Permalink
P2P: remove peer sync service (#299)
Browse files Browse the repository at this point in the history
* remove peer sync service

* change `p2p` to not use the peer sync service

* fmt & clippy

* doc updates

* review fixes

* add a little more detail to comment
  • Loading branch information
Boog900 authored Sep 30, 2024
1 parent 12bbadd commit 6da9d2d
Show file tree
Hide file tree
Showing 19 changed files with 118 additions and 785 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions p2p/p2p-core/src/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{
fmt::{Debug, Display, Formatter},
sync::Arc,
sync::{Arc, Mutex},
task::{ready, Context, Poll},
};

Expand All @@ -15,6 +15,7 @@ use tracing::Instrument;

use cuprate_helper::asynch::InfallibleOneshotReceiver;
use cuprate_pruning::PruningSeed;
use cuprate_wire::CoreSyncData;

use crate::{
handles::{ConnectionGuard, ConnectionHandle},
Expand Down Expand Up @@ -59,8 +60,17 @@ pub struct PeerInformation<A> {
pub handle: ConnectionHandle,
/// The direction of this connection (inbound|outbound).
pub direction: ConnectionDirection,
/// The peers pruning seed.
/// The peer's [`PruningSeed`].
pub pruning_seed: PruningSeed,
/// The [`CoreSyncData`] of this peer.
///
/// Data across fields are not necessarily related, so [`CoreSyncData::top_id`] is not always the
/// block hash for the block at height one below [`CoreSyncData::current_height`].
///
/// This value is behind a [`Mutex`] and is updated whenever the peer sends new information related
/// to their sync state. It is publicly accessible to anyone who has a peers [`Client`] handle. You
/// probably should not mutate this value unless you are creating a custom [`ProtocolRequestHandler`](crate::ProtocolRequestHandler).
pub core_sync_data: Arc<Mutex<CoreSyncData>>,
}

/// This represents a connection to a peer.
Expand Down
11 changes: 5 additions & 6 deletions p2p/p2p-core/src/client/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
constants::{REQUEST_TIMEOUT, SENDING_TIMEOUT},
handles::ConnectionGuard,
AddressBook, BroadcastMessage, CoreSyncSvc, MessageID, NetworkZone, PeerError, PeerRequest,
PeerResponse, PeerSyncSvc, ProtocolRequestHandler, ProtocolResponse, SharedError,
PeerResponse, ProtocolRequestHandler, ProtocolResponse, SharedError,
};

/// A request to the connection task from a [`Client`](crate::client::Client).
Expand Down Expand Up @@ -71,7 +71,7 @@ const fn levin_command_response(message_id: MessageID, command: LevinCommand) ->
}

/// This represents a connection to a peer.
pub(crate) struct Connection<Z: NetworkZone, A, CS, PS, PR, BrdcstStrm> {
pub(crate) struct Connection<Z: NetworkZone, A, CS, PR, BrdcstStrm> {
/// The peer sink - where we send messages to the peer.
peer_sink: Z::Sink,

Expand All @@ -86,20 +86,19 @@ pub(crate) struct Connection<Z: NetworkZone, A, CS, PS, PR, BrdcstStrm> {
broadcast_stream: Pin<Box<BrdcstStrm>>,

/// The inner handler for any requests that come from the requested peer.
peer_request_handler: PeerRequestHandler<Z, A, CS, PS, PR>,
peer_request_handler: PeerRequestHandler<Z, A, CS, PR>,

/// The connection guard which will send signals to other parts of Cuprate when this connection is dropped.
connection_guard: ConnectionGuard,
/// An error slot which is shared with the client.
error: SharedError<PeerError>,
}

impl<Z, A, CS, PS, PR, BrdcstStrm> Connection<Z, A, CS, PS, PR, BrdcstStrm>
impl<Z, A, CS, PR, BrdcstStrm> Connection<Z, A, CS, PR, BrdcstStrm>
where
Z: NetworkZone,
A: AddressBook<Z>,
CS: CoreSyncSvc,
PS: PeerSyncSvc<Z>,
PR: ProtocolRequestHandler,
BrdcstStrm: Stream<Item = BroadcastMessage> + Send + 'static,
{
Expand All @@ -108,7 +107,7 @@ where
peer_sink: Z::Sink,
client_rx: mpsc::Receiver<ConnectionTaskRequest>,
broadcast_stream: BrdcstStrm,
peer_request_handler: PeerRequestHandler<Z, A, CS, PS, PR>,
peer_request_handler: PeerRequestHandler<Z, A, CS, PR>,
connection_guard: ConnectionGuard,
error: SharedError<PeerError>,
) -> Self {
Expand Down
19 changes: 8 additions & 11 deletions p2p/p2p-core/src/client/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use tower::{Service, ServiceExt};

use crate::{
client::{handshaker::HandShaker, Client, DoHandshakeRequest, HandshakeError, InternalPeerID},
AddressBook, BroadcastMessage, ConnectionDirection, CoreSyncSvc, NetworkZone, PeerSyncSvc,
AddressBook, BroadcastMessage, ConnectionDirection, CoreSyncSvc, NetworkZone,
ProtocolRequestHandler,
};

Expand All @@ -32,27 +32,24 @@ pub struct ConnectRequest<Z: NetworkZone> {
}

/// The connector service, this service connects to peer and returns the [`Client`].
pub struct Connector<Z: NetworkZone, AdrBook, CSync, PSync, ProtoHdlr, BrdcstStrmMkr> {
handshaker: HandShaker<Z, AdrBook, CSync, PSync, ProtoHdlr, BrdcstStrmMkr>,
pub struct Connector<Z: NetworkZone, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr> {
handshaker: HandShaker<Z, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr>,
}

impl<Z: NetworkZone, AdrBook, CSync, PSync, ProtoHdlr, BrdcstStrmMkr>
Connector<Z, AdrBook, CSync, PSync, ProtoHdlr, BrdcstStrmMkr>
impl<Z: NetworkZone, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr>
Connector<Z, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr>
{
/// Create a new connector from a handshaker.
pub const fn new(
handshaker: HandShaker<Z, AdrBook, CSync, PSync, ProtoHdlr, BrdcstStrmMkr>,
) -> Self {
pub const fn new(handshaker: HandShaker<Z, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr>) -> Self {
Self { handshaker }
}
}

impl<Z: NetworkZone, AdrBook, CSync, PSync, ProtoHdlr, BrdcstStrmMkr, BrdcstStrm>
Service<ConnectRequest<Z>> for Connector<Z, AdrBook, CSync, PSync, ProtoHdlr, BrdcstStrmMkr>
impl<Z: NetworkZone, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr, BrdcstStrm>
Service<ConnectRequest<Z>> for Connector<Z, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr>
where
AdrBook: AddressBook<Z> + Clone,
CSync: CoreSyncSvc + Clone,
PSync: PeerSyncSvc<Z> + Clone,
ProtoHdlr: ProtocolRequestHandler + Clone,
BrdcstStrm: Stream<Item = BroadcastMessage> + Send + 'static,
BrdcstStrmMkr: Fn(InternalPeerID<Z::Addr>) -> BrdcstStrm + Clone + Send + 'static,
Expand Down
47 changes: 11 additions & 36 deletions p2p/p2p-core/src/client/handshaker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
future::Future,
marker::PhantomData,
pin::Pin,
sync::Arc,
sync::{Arc, Mutex},
task::{Context, Poll},
};

Expand Down Expand Up @@ -40,10 +40,9 @@ use crate::{
PING_TIMEOUT,
},
handles::HandleBuilder,
services::PeerSyncRequest,
AddressBook, AddressBookRequest, AddressBookResponse, BroadcastMessage, ConnectionDirection,
CoreSyncDataRequest, CoreSyncDataResponse, CoreSyncSvc, NetZoneAddress, NetworkZone,
PeerSyncSvc, ProtocolRequestHandler, SharedError,
ProtocolRequestHandler, SharedError,
};

pub mod builder;
Expand Down Expand Up @@ -87,13 +86,11 @@ pub struct DoHandshakeRequest<Z: NetworkZone> {

/// The peer handshaking service.
#[derive(Debug, Clone)]
pub struct HandShaker<Z: NetworkZone, AdrBook, CSync, PSync, ProtoHdlr, BrdcstStrmMkr> {
pub struct HandShaker<Z: NetworkZone, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr> {
/// The address book service.
address_book: AdrBook,
/// The core sync data service.
core_sync_svc: CSync,
/// The peer sync service.
peer_sync_svc: PSync,
/// The protocol request handler service.
protocol_request_svc: ProtoHdlr,

Expand All @@ -109,13 +106,12 @@ pub struct HandShaker<Z: NetworkZone, AdrBook, CSync, PSync, ProtoHdlr, BrdcstSt
_zone: PhantomData<Z>,
}

impl<Z: NetworkZone, AdrBook, CSync, PSync, ProtoHdlr, BrdcstStrmMkr>
HandShaker<Z, AdrBook, CSync, PSync, ProtoHdlr, BrdcstStrmMkr>
impl<Z: NetworkZone, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr>
HandShaker<Z, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr>
{
/// Creates a new handshaker.
const fn new(
address_book: AdrBook,
peer_sync_svc: PSync,
core_sync_svc: CSync,
protocol_request_svc: ProtoHdlr,
broadcast_stream_maker: BrdcstStrmMkr,
Expand All @@ -124,7 +120,6 @@ impl<Z: NetworkZone, AdrBook, CSync, PSync, ProtoHdlr, BrdcstStrmMkr>
) -> Self {
Self {
address_book,
peer_sync_svc,
core_sync_svc,
protocol_request_svc,
broadcast_stream_maker,
Expand All @@ -135,13 +130,11 @@ impl<Z: NetworkZone, AdrBook, CSync, PSync, ProtoHdlr, BrdcstStrmMkr>
}
}

impl<Z: NetworkZone, AdrBook, CSync, PSync, ProtoHdlr, BrdcstStrmMkr, BrdcstStrm>
Service<DoHandshakeRequest<Z>>
for HandShaker<Z, AdrBook, CSync, PSync, ProtoHdlr, BrdcstStrmMkr>
impl<Z: NetworkZone, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr, BrdcstStrm>
Service<DoHandshakeRequest<Z>> for HandShaker<Z, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr>
where
AdrBook: AddressBook<Z> + Clone,
CSync: CoreSyncSvc + Clone,
PSync: PeerSyncSvc<Z> + Clone,
ProtoHdlr: ProtocolRequestHandler + Clone,
BrdcstStrm: Stream<Item = BroadcastMessage> + Send + 'static,
BrdcstStrmMkr: Fn(InternalPeerID<Z::Addr>) -> BrdcstStrm + Clone + Send + 'static,
Expand All @@ -161,7 +154,6 @@ where
let address_book = self.address_book.clone();
let protocol_request_svc = self.protocol_request_svc.clone();
let core_sync_svc = self.core_sync_svc.clone();
let peer_sync_svc = self.peer_sync_svc.clone();
let our_basic_node_data = self.our_basic_node_data.clone();

let connection_parent_span = self.connection_parent_span.clone();
Expand All @@ -176,7 +168,6 @@ where
broadcast_stream_maker,
address_book,
core_sync_svc,
peer_sync_svc,
protocol_request_svc,
our_basic_node_data,
connection_parent_span,
Expand Down Expand Up @@ -231,23 +222,20 @@ pub async fn ping<N: NetworkZone>(addr: N::Addr) -> Result<u64, HandshakeError>
}

/// This function completes a handshake with the requested peer.
#[expect(clippy::too_many_arguments)]
async fn handshake<Z: NetworkZone, AdrBook, CSync, PSync, ProtoHdlr, BrdcstStrmMkr, BrdcstStrm>(
async fn handshake<Z: NetworkZone, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr, BrdcstStrm>(
req: DoHandshakeRequest<Z>,

broadcast_stream_maker: BrdcstStrmMkr,

mut address_book: AdrBook,
mut core_sync_svc: CSync,
mut peer_sync_svc: PSync,
protocol_request_handler: ProtoHdlr,
our_basic_node_data: BasicNodeData,
connection_parent_span: Span,
) -> Result<Client<Z>, HandshakeError>
where
AdrBook: AddressBook<Z> + Clone,
CSync: CoreSyncSvc + Clone,
PSync: PeerSyncSvc<Z> + Clone,
ProtoHdlr: ProtocolRequestHandler,
BrdcstStrm: Stream<Item = BroadcastMessage> + Send + 'static,
BrdcstStrmMkr: Fn(InternalPeerID<Z::Addr>) -> BrdcstStrm + Send + 'static,
Expand Down Expand Up @@ -458,17 +446,6 @@ where
})
.await?;

// Tell the core sync service about the new peer.
peer_sync_svc
.ready()
.await?
.call(PeerSyncRequest::IncomingCoreSyncData(
addr,
handle.clone(),
peer_core_sync,
))
.await?;

// Set up the connection data.
let error_slot = SharedError::new();
let (connection_tx, client_rx) = mpsc::channel(1);
Expand All @@ -478,18 +455,18 @@ where
handle,
direction,
pruning_seed,
core_sync_data: Arc::new(Mutex::new(peer_core_sync)),
};

let request_handler = PeerRequestHandler {
address_book_svc: address_book.clone(),
our_sync_svc: core_sync_svc.clone(),
peer_sync_svc: peer_sync_svc.clone(),
protocol_request_handler,
our_basic_node_data,
peer_info: info.clone(),
};

let connection = Connection::<Z, _, _, _, _, _>::new(
let connection = Connection::<Z, _, _, _, _>::new(
peer_sink,
client_rx,
broadcast_stream_maker(addr),
Expand All @@ -509,13 +486,11 @@ where
let semaphore = Arc::new(Semaphore::new(1));

let timeout_handle = tokio::spawn(connection_timeout_monitor_task(
info.id,
info.handle.clone(),
info.clone(),
connection_tx.clone(),
Arc::clone(&semaphore),
address_book,
core_sync_svc,
peer_sync_svc,
));

let client = Client::<Z>::new(
Expand Down
Loading

0 comments on commit 6da9d2d

Please sign in to comment.