P2P: give the protocol handler access to the peer info (#302)
Some checks failed
CI / fmt (push) Has been cancelled
CI / typo (push) Has been cancelled
Audit / audit (push) Has been cancelled
CI / ci (macos-latest, stable, bash) (push) Has been cancelled
CI / ci (ubuntu-latest, stable, bash) (push) Has been cancelled
CI / ci (windows-latest, stable-x86_64-pc-windows-gnu, msys2 {0}) (push) Has been cancelled
Deny / audit (push) Has been cancelled
Doc / build (push) Has been cancelled
Doc / deploy (push) Has been cancelled

* give the protocol handler access to the peer info

* add trait alias

* clippy + fmt

* update doc

* simplify trait aliases

* use tower `Shared`

* clean import

* fmt

* Update Cargo.toml

Co-authored-by: hinto-janai <hinto.janai@protonmail.com>

* fix merge

---------

Co-authored-by: hinto-janai <hinto.janai@protonmail.com>
This commit is contained in:
Boog900 2024-09-30 23:19:53 +01:00 committed by GitHub
parent 6da9d2d734
commit 521bf877db
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 153 additions and 102 deletions

69
Cargo.lock generated
View file

@ -129,9 +129,9 @@ dependencies = [
"serde_urlencoded", "serde_urlencoded",
"sync_wrapper 1.0.1", "sync_wrapper 1.0.1",
"tokio", "tokio",
"tower", "tower 0.4.13",
"tower-layer", "tower-layer 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"tower-service", "tower-service 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"tracing", "tracing",
] ]
@ -151,8 +151,8 @@ dependencies = [
"pin-project-lite", "pin-project-lite",
"rustversion", "rustversion",
"sync_wrapper 0.1.2", "sync_wrapper 0.1.2",
"tower-layer", "tower-layer 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"tower-service", "tower-service 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"tracing", "tracing",
] ]
@ -527,7 +527,7 @@ dependencies = [
"thiserror", "thiserror",
"tokio", "tokio",
"tokio-util", "tokio-util",
"tower", "tower 0.5.1",
"tracing", "tracing",
] ]
@ -565,7 +565,7 @@ dependencies = [
"tempfile", "tempfile",
"thread_local", "thread_local",
"tokio", "tokio",
"tower", "tower 0.5.1",
] ]
[[package]] [[package]]
@ -592,7 +592,7 @@ dependencies = [
"tokio", "tokio",
"tokio-test", "tokio-test",
"tokio-util", "tokio-util",
"tower", "tower 0.5.1",
"tracing", "tracing",
] ]
@ -638,7 +638,7 @@ dependencies = [
"thiserror", "thiserror",
"tokio", "tokio",
"tokio-util", "tokio-util",
"tower", "tower 0.5.1",
"tracing", "tracing",
] ]
@ -668,7 +668,7 @@ dependencies = [
"futures", "futures",
"rayon", "rayon",
"serde", "serde",
"tower", "tower 0.5.1",
] ]
[[package]] [[package]]
@ -700,7 +700,7 @@ dependencies = [
"sha3", "sha3",
"thiserror", "thiserror",
"tokio", "tokio",
"tower", "tower 0.5.1",
] ]
[[package]] [[package]]
@ -785,7 +785,7 @@ dependencies = [
"tokio-stream", "tokio-stream",
"tokio-test", "tokio-test",
"tokio-util", "tokio-util",
"tower", "tower 0.5.1",
"tracing", "tracing",
] ]
@ -808,7 +808,7 @@ dependencies = [
"tokio-stream", "tokio-stream",
"tokio-test", "tokio-test",
"tokio-util", "tokio-util",
"tower", "tower 0.5.1",
"tracing", "tracing",
] ]
@ -836,7 +836,7 @@ dependencies = [
"serde", "serde",
"serde_json", "serde_json",
"tokio", "tokio",
"tower", "tower 0.5.1",
"ureq", "ureq",
] ]
@ -897,7 +897,7 @@ dependencies = [
"tempfile", "tempfile",
"thiserror", "thiserror",
"tokio", "tokio",
"tower", "tower 0.5.1",
] ]
[[package]] [[package]]
@ -992,7 +992,7 @@ dependencies = [
"tokio", "tokio",
"tokio-stream", "tokio-stream",
"tokio-util", "tokio-util",
"tower", "tower 0.5.1",
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",
] ]
@ -1491,7 +1491,7 @@ dependencies = [
"rustls-pki-types", "rustls-pki-types",
"tokio", "tokio",
"tokio-rustls", "tokio-rustls",
"tower-service", "tower-service 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
@ -1509,8 +1509,8 @@ dependencies = [
"pin-project-lite", "pin-project-lite",
"socket2", "socket2",
"tokio", "tokio",
"tower", "tower 0.4.13",
"tower-service", "tower-service 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"tracing", "tracing",
] ]
@ -2539,7 +2539,7 @@ dependencies = [
"hyper-rustls", "hyper-rustls",
"hyper-util", "hyper-util",
"tokio", "tokio",
"tower-service", "tower-service 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]
@ -2821,9 +2821,24 @@ dependencies = [
"pin-project", "pin-project",
"pin-project-lite", "pin-project-lite",
"tokio", "tokio",
"tower-layer 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"tower-service 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
"tracing",
]
[[package]]
name = "tower"
version = "0.5.1"
source = "git+https://github.com/Cuprate/tower.git?rev=6c7faf0#6c7faf0e9dbc74aef5d3110313324bc7e1f997cf"
dependencies = [
"futures-core",
"futures-util",
"pin-project-lite",
"sync_wrapper 0.1.2",
"tokio",
"tokio-util", "tokio-util",
"tower-layer", "tower-layer 0.3.3 (git+https://github.com/Cuprate/tower.git?rev=6c7faf0)",
"tower-service", "tower-service 0.3.3 (git+https://github.com/Cuprate/tower.git?rev=6c7faf0)",
"tracing", "tracing",
] ]
@ -2833,12 +2848,22 @@ version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e"
[[package]]
name = "tower-layer"
version = "0.3.3"
source = "git+https://github.com/Cuprate/tower.git?rev=6c7faf0#6c7faf0e9dbc74aef5d3110313324bc7e1f997cf"
[[package]] [[package]]
name = "tower-service" name = "tower-service"
version = "0.3.3" version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3"
[[package]]
name = "tower-service"
version = "0.3.3"
source = "git+https://github.com/Cuprate/tower.git?rev=6c7faf0#6c7faf0e9dbc74aef5d3110313324bc7e1f997cf"
[[package]] [[package]]
name = "tracing" name = "tracing"
version = "0.1.40" version = "0.1.40"

View file

@ -82,7 +82,7 @@ thread_local = { version = "1.1.8", default-features = false }
tokio-util = { version = "0.7.12", default-features = false } tokio-util = { version = "0.7.12", default-features = false }
tokio-stream = { version = "0.1.16", default-features = false } tokio-stream = { version = "0.1.16", default-features = false }
tokio = { version = "1.40.0", default-features = false } tokio = { version = "1.40.0", default-features = false }
tower = { version = "0.4.13", default-features = false } tower = { git = "https://github.com/Cuprate/tower.git", rev = "6c7faf0", default-features = false } # <https://github.com/tower-rs/tower/pull/796>
tracing-subscriber = { version = "0.3.18", default-features = false } tracing-subscriber = { version = "0.3.18", default-features = false }
tracing = { version = "0.1.40", default-features = false } tracing = { version = "0.1.40", default-features = false }

View file

@ -19,7 +19,7 @@ tokio-util = { workspace = true, features = ["codec"] }
tokio-stream = { workspace = true, features = ["sync"]} tokio-stream = { workspace = true, features = ["sync"]}
futures = { workspace = true, features = ["std"] } futures = { workspace = true, features = ["std"] }
async-trait = { workspace = true } async-trait = { workspace = true }
tower = { workspace = true, features = ["util", "tracing"] } tower = { workspace = true, features = ["util", "tracing", "make"] }
cfg-if = { workspace = true } cfg-if = { workspace = true }
thiserror = { workspace = true } thiserror = { workspace = true }

View file

@ -17,7 +17,7 @@ use tower::{Service, ServiceExt};
use crate::{ use crate::{
client::{handshaker::HandShaker, Client, DoHandshakeRequest, HandshakeError, InternalPeerID}, client::{handshaker::HandShaker, Client, DoHandshakeRequest, HandshakeError, InternalPeerID},
AddressBook, BroadcastMessage, ConnectionDirection, CoreSyncSvc, NetworkZone, AddressBook, BroadcastMessage, ConnectionDirection, CoreSyncSvc, NetworkZone,
ProtocolRequestHandler, ProtocolRequestHandlerMaker,
}; };
/// A request to connect to a peer. /// A request to connect to a peer.
@ -32,25 +32,27 @@ pub struct ConnectRequest<Z: NetworkZone> {
} }
/// The connector service, this service connects to peer and returns the [`Client`]. /// The connector service, this service connects to peer and returns the [`Client`].
pub struct Connector<Z: NetworkZone, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr> { pub struct Connector<Z: NetworkZone, AdrBook, CSync, ProtoHdlrMkr, BrdcstStrmMkr> {
handshaker: HandShaker<Z, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr>, handshaker: HandShaker<Z, AdrBook, CSync, ProtoHdlrMkr, BrdcstStrmMkr>,
} }
impl<Z: NetworkZone, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr> impl<Z: NetworkZone, AdrBook, CSync, ProtoHdlrMkr, BrdcstStrmMkr>
Connector<Z, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr> Connector<Z, AdrBook, CSync, ProtoHdlrMkr, BrdcstStrmMkr>
{ {
/// Create a new connector from a handshaker. /// Create a new connector from a handshaker.
pub const fn new(handshaker: HandShaker<Z, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr>) -> Self { pub const fn new(
handshaker: HandShaker<Z, AdrBook, CSync, ProtoHdlrMkr, BrdcstStrmMkr>,
) -> Self {
Self { handshaker } Self { handshaker }
} }
} }
impl<Z: NetworkZone, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr, BrdcstStrm> impl<Z: NetworkZone, AdrBook, CSync, ProtoHdlrMkr, BrdcstStrmMkr, BrdcstStrm>
Service<ConnectRequest<Z>> for Connector<Z, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr> Service<ConnectRequest<Z>> for Connector<Z, AdrBook, CSync, ProtoHdlrMkr, BrdcstStrmMkr>
where where
AdrBook: AddressBook<Z> + Clone, AdrBook: AddressBook<Z> + Clone,
CSync: CoreSyncSvc + Clone, CSync: CoreSyncSvc + Clone,
ProtoHdlr: ProtocolRequestHandler + Clone, ProtoHdlrMkr: ProtocolRequestHandlerMaker<Z> + Clone,
BrdcstStrm: Stream<Item = BroadcastMessage> + Send + 'static, BrdcstStrm: Stream<Item = BroadcastMessage> + Send + 'static,
BrdcstStrmMkr: Fn(InternalPeerID<Z::Addr>) -> BrdcstStrm + Clone + Send + 'static, BrdcstStrmMkr: Fn(InternalPeerID<Z::Addr>) -> BrdcstStrm + Clone + Send + 'static,
{ {

View file

@ -42,7 +42,7 @@ use crate::{
handles::HandleBuilder, handles::HandleBuilder,
AddressBook, AddressBookRequest, AddressBookResponse, BroadcastMessage, ConnectionDirection, AddressBook, AddressBookRequest, AddressBookResponse, BroadcastMessage, ConnectionDirection,
CoreSyncDataRequest, CoreSyncDataResponse, CoreSyncSvc, NetZoneAddress, NetworkZone, CoreSyncDataRequest, CoreSyncDataResponse, CoreSyncSvc, NetZoneAddress, NetworkZone,
ProtocolRequestHandler, SharedError, ProtocolRequestHandlerMaker, SharedError,
}; };
pub mod builder; pub mod builder;
@ -86,13 +86,13 @@ pub struct DoHandshakeRequest<Z: NetworkZone> {
/// The peer handshaking service. /// The peer handshaking service.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct HandShaker<Z: NetworkZone, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr> { pub struct HandShaker<Z: NetworkZone, AdrBook, CSync, ProtoHdlrMkr, BrdcstStrmMkr> {
/// The address book service. /// The address book service.
address_book: AdrBook, address_book: AdrBook,
/// The core sync data service. /// The core sync data service.
core_sync_svc: CSync, core_sync_svc: CSync,
/// The protocol request handler service. /// The protocol request handler service.
protocol_request_svc: ProtoHdlr, protocol_request_svc_maker: ProtoHdlrMkr,
/// Our [`BasicNodeData`] /// Our [`BasicNodeData`]
our_basic_node_data: BasicNodeData, our_basic_node_data: BasicNodeData,
@ -106,14 +106,14 @@ pub struct HandShaker<Z: NetworkZone, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr>
_zone: PhantomData<Z>, _zone: PhantomData<Z>,
} }
impl<Z: NetworkZone, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr> impl<Z: NetworkZone, AdrBook, CSync, ProtoHdlrMkr, BrdcstStrmMkr>
HandShaker<Z, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr> HandShaker<Z, AdrBook, CSync, ProtoHdlrMkr, BrdcstStrmMkr>
{ {
/// Creates a new handshaker. /// Creates a new handshaker.
const fn new( const fn new(
address_book: AdrBook, address_book: AdrBook,
core_sync_svc: CSync, core_sync_svc: CSync,
protocol_request_svc: ProtoHdlr, protocol_request_svc_maker: ProtoHdlrMkr,
broadcast_stream_maker: BrdcstStrmMkr, broadcast_stream_maker: BrdcstStrmMkr,
our_basic_node_data: BasicNodeData, our_basic_node_data: BasicNodeData,
connection_parent_span: Span, connection_parent_span: Span,
@ -121,7 +121,7 @@ impl<Z: NetworkZone, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr>
Self { Self {
address_book, address_book,
core_sync_svc, core_sync_svc,
protocol_request_svc, protocol_request_svc_maker,
broadcast_stream_maker, broadcast_stream_maker,
our_basic_node_data, our_basic_node_data,
connection_parent_span, connection_parent_span,
@ -130,12 +130,12 @@ impl<Z: NetworkZone, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr>
} }
} }
impl<Z: NetworkZone, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr, BrdcstStrm> impl<Z: NetworkZone, AdrBook, CSync, ProtoHdlrMkr, BrdcstStrmMkr, BrdcstStrm>
Service<DoHandshakeRequest<Z>> for HandShaker<Z, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr> Service<DoHandshakeRequest<Z>> for HandShaker<Z, AdrBook, CSync, ProtoHdlrMkr, BrdcstStrmMkr>
where where
AdrBook: AddressBook<Z> + Clone, AdrBook: AddressBook<Z> + Clone,
CSync: CoreSyncSvc + Clone, CSync: CoreSyncSvc + Clone,
ProtoHdlr: ProtocolRequestHandler + Clone, ProtoHdlrMkr: ProtocolRequestHandlerMaker<Z> + Clone,
BrdcstStrm: Stream<Item = BroadcastMessage> + Send + 'static, BrdcstStrm: Stream<Item = BroadcastMessage> + Send + 'static,
BrdcstStrmMkr: Fn(InternalPeerID<Z::Addr>) -> BrdcstStrm + Clone + Send + 'static, BrdcstStrmMkr: Fn(InternalPeerID<Z::Addr>) -> BrdcstStrm + Clone + Send + 'static,
{ {
@ -152,7 +152,7 @@ where
let broadcast_stream_maker = self.broadcast_stream_maker.clone(); let broadcast_stream_maker = self.broadcast_stream_maker.clone();
let address_book = self.address_book.clone(); let address_book = self.address_book.clone();
let protocol_request_svc = self.protocol_request_svc.clone(); let protocol_request_svc_maker = self.protocol_request_svc_maker.clone();
let core_sync_svc = self.core_sync_svc.clone(); let core_sync_svc = self.core_sync_svc.clone();
let our_basic_node_data = self.our_basic_node_data.clone(); let our_basic_node_data = self.our_basic_node_data.clone();
@ -168,7 +168,7 @@ where
broadcast_stream_maker, broadcast_stream_maker,
address_book, address_book,
core_sync_svc, core_sync_svc,
protocol_request_svc, protocol_request_svc_maker,
our_basic_node_data, our_basic_node_data,
connection_parent_span, connection_parent_span,
), ),
@ -222,21 +222,21 @@ pub async fn ping<N: NetworkZone>(addr: N::Addr) -> Result<u64, HandshakeError>
} }
/// This function completes a handshake with the requested peer. /// This function completes a handshake with the requested peer.
async fn handshake<Z: NetworkZone, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr, BrdcstStrm>( async fn handshake<Z: NetworkZone, AdrBook, CSync, ProtoHdlrMkr, BrdcstStrmMkr, BrdcstStrm>(
req: DoHandshakeRequest<Z>, req: DoHandshakeRequest<Z>,
broadcast_stream_maker: BrdcstStrmMkr, broadcast_stream_maker: BrdcstStrmMkr,
mut address_book: AdrBook, mut address_book: AdrBook,
mut core_sync_svc: CSync, mut core_sync_svc: CSync,
protocol_request_handler: ProtoHdlr, mut protocol_request_svc_maker: ProtoHdlrMkr,
our_basic_node_data: BasicNodeData, our_basic_node_data: BasicNodeData,
connection_parent_span: Span, connection_parent_span: Span,
) -> Result<Client<Z>, HandshakeError> ) -> Result<Client<Z>, HandshakeError>
where where
AdrBook: AddressBook<Z> + Clone, AdrBook: AddressBook<Z> + Clone,
CSync: CoreSyncSvc + Clone, CSync: CoreSyncSvc + Clone,
ProtoHdlr: ProtocolRequestHandler, ProtoHdlrMkr: ProtocolRequestHandlerMaker<Z>,
BrdcstStrm: Stream<Item = BroadcastMessage> + Send + 'static, BrdcstStrm: Stream<Item = BroadcastMessage> + Send + 'static,
BrdcstStrmMkr: Fn(InternalPeerID<Z::Addr>) -> BrdcstStrm + Send + 'static, BrdcstStrmMkr: Fn(InternalPeerID<Z::Addr>) -> BrdcstStrm + Send + 'static,
{ {
@ -458,6 +458,13 @@ where
core_sync_data: Arc::new(Mutex::new(peer_core_sync)), core_sync_data: Arc::new(Mutex::new(peer_core_sync)),
}; };
let protocol_request_handler = protocol_request_svc_maker
.as_service()
.ready()
.await?
.call(info.clone())
.await?;
let request_handler = PeerRequestHandler { let request_handler = PeerRequestHandler {
address_book_svc: address_book.clone(), address_book_svc: address_book.clone(),
our_sync_svc: core_sync_svc.clone(), our_sync_svc: core_sync_svc.clone(),

View file

@ -1,13 +1,14 @@
use std::marker::PhantomData; use std::{convert::Infallible, marker::PhantomData};
use futures::{stream, Stream}; use futures::{stream, Stream};
use tower::{make::Shared, util::MapErr};
use tracing::Span; use tracing::Span;
use cuprate_wire::BasicNodeData; use cuprate_wire::BasicNodeData;
use crate::{ use crate::{
client::{handshaker::HandShaker, InternalPeerID}, client::{handshaker::HandShaker, InternalPeerID},
AddressBook, BroadcastMessage, CoreSyncSvc, NetworkZone, ProtocolRequestHandler, AddressBook, BroadcastMessage, CoreSyncSvc, NetworkZone, ProtocolRequestHandlerMaker,
}; };
mod dummy; mod dummy;
@ -16,7 +17,7 @@ pub use dummy::{DummyAddressBook, DummyCoreSyncSvc, DummyProtocolRequestHandler}
/// A [`HandShaker`] [`Service`](tower::Service) builder. /// A [`HandShaker`] [`Service`](tower::Service) builder.
/// ///
/// This builder applies default values to make usage easier, behaviour and drawbacks of the defaults are documented /// This builder applies default values to make usage easier, behaviour and drawbacks of the defaults are documented
/// on the `with_*` method to change it, for example [`HandshakerBuilder::with_protocol_request_handler`]. /// on the `with_*` method to change it, for example [`HandshakerBuilder::with_protocol_request_handler_maker`].
/// ///
/// If you want to use any network other than [`Mainnet`](crate::Network::Mainnet) /// If you want to use any network other than [`Mainnet`](crate::Network::Mainnet)
/// you will need to change the core sync service with [`HandshakerBuilder::with_core_sync_svc`], /// you will need to change the core sync service with [`HandshakerBuilder::with_core_sync_svc`],
@ -26,7 +27,7 @@ pub struct HandshakerBuilder<
N: NetworkZone, N: NetworkZone,
AdrBook = DummyAddressBook, AdrBook = DummyAddressBook,
CSync = DummyCoreSyncSvc, CSync = DummyCoreSyncSvc,
ProtoHdlr = DummyProtocolRequestHandler, ProtoHdlrMkr = MapErr<Shared<DummyProtocolRequestHandler>, fn(Infallible) -> tower::BoxError>,
BrdcstStrmMkr = fn( BrdcstStrmMkr = fn(
InternalPeerID<<N as NetworkZone>::Addr>, InternalPeerID<<N as NetworkZone>::Addr>,
) -> stream::Pending<BroadcastMessage>, ) -> stream::Pending<BroadcastMessage>,
@ -36,7 +37,7 @@ pub struct HandshakerBuilder<
/// The core sync data service. /// The core sync data service.
core_sync_svc: CSync, core_sync_svc: CSync,
/// The protocol request service. /// The protocol request service.
protocol_request_svc: ProtoHdlr, protocol_request_svc_maker: ProtoHdlrMkr,
/// Our [`BasicNodeData`] /// Our [`BasicNodeData`]
our_basic_node_data: BasicNodeData, our_basic_node_data: BasicNodeData,
/// A function that returns a stream that will give items to be broadcast by a connection. /// A function that returns a stream that will give items to be broadcast by a connection.
@ -54,7 +55,10 @@ impl<N: NetworkZone> HandshakerBuilder<N> {
Self { Self {
address_book: DummyAddressBook, address_book: DummyAddressBook,
core_sync_svc: DummyCoreSyncSvc::static_mainnet_genesis(), core_sync_svc: DummyCoreSyncSvc::static_mainnet_genesis(),
protocol_request_svc: DummyProtocolRequestHandler, protocol_request_svc_maker: MapErr::new(
Shared::new(DummyProtocolRequestHandler),
tower::BoxError::from,
),
our_basic_node_data, our_basic_node_data,
broadcast_stream_maker: |_| stream::pending(), broadcast_stream_maker: |_| stream::pending(),
connection_parent_span: None, connection_parent_span: None,
@ -83,7 +87,7 @@ impl<N: NetworkZone, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr>
{ {
let Self { let Self {
core_sync_svc, core_sync_svc,
protocol_request_svc, protocol_request_svc_maker,
our_basic_node_data, our_basic_node_data,
broadcast_stream_maker, broadcast_stream_maker,
connection_parent_span, connection_parent_span,
@ -93,7 +97,7 @@ impl<N: NetworkZone, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr>
HandshakerBuilder { HandshakerBuilder {
address_book: new_address_book, address_book: new_address_book,
core_sync_svc, core_sync_svc,
protocol_request_svc, protocol_request_svc_maker,
our_basic_node_data, our_basic_node_data,
broadcast_stream_maker, broadcast_stream_maker,
connection_parent_span, connection_parent_span,
@ -123,7 +127,7 @@ impl<N: NetworkZone, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr>
{ {
let Self { let Self {
address_book, address_book,
protocol_request_svc, protocol_request_svc_maker,
our_basic_node_data, our_basic_node_data,
broadcast_stream_maker, broadcast_stream_maker,
connection_parent_span, connection_parent_span,
@ -133,7 +137,7 @@ impl<N: NetworkZone, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr>
HandshakerBuilder { HandshakerBuilder {
address_book, address_book,
core_sync_svc: new_core_sync_svc, core_sync_svc: new_core_sync_svc,
protocol_request_svc, protocol_request_svc_maker,
our_basic_node_data, our_basic_node_data,
broadcast_stream_maker, broadcast_stream_maker,
connection_parent_span, connection_parent_span,
@ -141,19 +145,20 @@ impl<N: NetworkZone, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr>
} }
} }
/// Changes the protocol request handler, which handles [`ProtocolRequest`](crate::ProtocolRequest)s to our node. /// Changes the protocol request handler maker, which creates the service that handles [`ProtocolRequest`](crate::ProtocolRequest)s
/// to our node.
/// ///
/// ## Default Protocol Request Handler /// ## Default Protocol Request Handler
/// ///
/// The default protocol request handler will not respond to any protocol requests, this should not /// The default service maker will create services that will not respond to any protocol requests, this should not
/// be an issue as long as peers do not think we are ahead of them, if they do they will send requests /// be an issue as long as peers do not think we are ahead of them, if they do they will send requests
/// for our blocks, and we won't respond which will cause them to disconnect. /// for our blocks, and we won't respond which will cause them to disconnect.
pub fn with_protocol_request_handler<NProtoHdlr>( pub fn with_protocol_request_handler_maker<NProtoHdlrMkr>(
self, self,
new_protocol_handler: NProtoHdlr, new_protocol_request_svc_maker: NProtoHdlrMkr,
) -> HandshakerBuilder<N, AdrBook, CSync, NProtoHdlr, BrdcstStrmMkr> ) -> HandshakerBuilder<N, AdrBook, CSync, NProtoHdlrMkr, BrdcstStrmMkr>
where where
NProtoHdlr: ProtocolRequestHandler + Clone, NProtoHdlrMkr: ProtocolRequestHandlerMaker<N> + Clone,
{ {
let Self { let Self {
address_book, address_book,
@ -167,7 +172,7 @@ impl<N: NetworkZone, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr>
HandshakerBuilder { HandshakerBuilder {
address_book, address_book,
core_sync_svc, core_sync_svc,
protocol_request_svc: new_protocol_handler, protocol_request_svc_maker: new_protocol_request_svc_maker,
our_basic_node_data, our_basic_node_data,
broadcast_stream_maker, broadcast_stream_maker,
connection_parent_span, connection_parent_span,
@ -193,7 +198,7 @@ impl<N: NetworkZone, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr>
let Self { let Self {
address_book, address_book,
core_sync_svc, core_sync_svc,
protocol_request_svc, protocol_request_svc_maker,
our_basic_node_data, our_basic_node_data,
connection_parent_span, connection_parent_span,
.. ..
@ -202,7 +207,7 @@ impl<N: NetworkZone, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr>
HandshakerBuilder { HandshakerBuilder {
address_book, address_book,
core_sync_svc, core_sync_svc,
protocol_request_svc, protocol_request_svc_maker,
our_basic_node_data, our_basic_node_data,
broadcast_stream_maker: new_broadcast_stream_maker, broadcast_stream_maker: new_broadcast_stream_maker,
connection_parent_span, connection_parent_span,
@ -228,7 +233,7 @@ impl<N: NetworkZone, AdrBook, CSync, ProtoHdlr, BrdcstStrmMkr>
HandShaker::new( HandShaker::new(
self.address_book, self.address_book,
self.core_sync_svc, self.core_sync_svc,
self.protocol_request_svc, self.protocol_request_svc_maker,
self.broadcast_stream_maker, self.broadcast_stream_maker,
self.our_basic_node_data, self.our_basic_node_data,
self.connection_parent_span.unwrap_or(Span::none()), self.connection_parent_span.unwrap_or(Span::none()),

View file

@ -66,7 +66,7 @@ cfg_if::cfg_if! {
} }
} }
use std::{fmt::Debug, future::Future, hash::Hash}; use std::{fmt::Debug, hash::Hash};
use futures::{Sink, Stream}; use futures::{Sink, Stream};
@ -197,26 +197,21 @@ pub trait AddressBook<Z: NetworkZone>:
AddressBookRequest<Z>, AddressBookRequest<Z>,
Response = AddressBookResponse<Z>, Response = AddressBookResponse<Z>,
Error = tower::BoxError, Error = tower::BoxError,
Future = Self::Future2, Future: Send + 'static,
> + Send > + Send
+ 'static + 'static
{ {
// This allows us to put more restrictive bounds on the future without defining the future here
// explicitly.
type Future2: Future<Output = Result<Self::Response, Self::Error>> + Send + 'static;
} }
impl<T, Z: NetworkZone> AddressBook<Z> for T impl<T, Z: NetworkZone> AddressBook<Z> for T where
where
T: tower::Service< T: tower::Service<
AddressBookRequest<Z>, AddressBookRequest<Z>,
Response = AddressBookResponse<Z>, Response = AddressBookResponse<Z>,
Error = tower::BoxError, Error = tower::BoxError,
Future: Send + 'static,
> + Send > + Send
+ 'static, + 'static
T::Future: Future<Output = Result<Self::Response, Self::Error>> + Send + 'static,
{ {
type Future2 = T::Future;
} }
pub trait CoreSyncSvc: pub trait CoreSyncSvc:
@ -224,26 +219,21 @@ pub trait CoreSyncSvc:
CoreSyncDataRequest, CoreSyncDataRequest,
Response = CoreSyncDataResponse, Response = CoreSyncDataResponse,
Error = tower::BoxError, Error = tower::BoxError,
Future = Self::Future2, Future: Send + 'static,
> + Send > + Send
+ 'static + 'static
{ {
// This allows us to put more restrictive bounds on the future without defining the future here
// explicitly.
type Future2: Future<Output = Result<Self::Response, Self::Error>> + Send + 'static;
} }
impl<T> CoreSyncSvc for T impl<T> CoreSyncSvc for T where
where
T: tower::Service< T: tower::Service<
CoreSyncDataRequest, CoreSyncDataRequest,
Response = CoreSyncDataResponse, Response = CoreSyncDataResponse,
Error = tower::BoxError, Error = tower::BoxError,
Future: Send + 'static,
> + Send > + Send
+ 'static, + 'static
T::Future: Future<Output = Result<Self::Response, Self::Error>> + Send + 'static,
{ {
type Future2 = T::Future;
} }
pub trait ProtocolRequestHandler: pub trait ProtocolRequestHandler:
@ -251,21 +241,43 @@ pub trait ProtocolRequestHandler:
ProtocolRequest, ProtocolRequest,
Response = ProtocolResponse, Response = ProtocolResponse,
Error = tower::BoxError, Error = tower::BoxError,
Future = Self::Future2, Future: Send + 'static,
> + Send > + Send
+ 'static + 'static
{ {
// This allows us to put more restrictive bounds on the future without defining the future here
// explicitly.
type Future2: Future<Output = Result<Self::Response, Self::Error>> + Send + 'static;
} }
impl<T> ProtocolRequestHandler for T impl<T> ProtocolRequestHandler for T where
where T: tower::Service<
T: tower::Service<ProtocolRequest, Response = ProtocolResponse, Error = tower::BoxError> ProtocolRequest,
+ Send Response = ProtocolResponse,
+ 'static, Error = tower::BoxError,
T::Future: Future<Output = Result<Self::Response, Self::Error>> + Send + 'static, Future: Send + 'static,
> + Send
+ 'static
{
}
pub trait ProtocolRequestHandlerMaker<Z: NetworkZone>:
tower::MakeService<
client::PeerInformation<Z::Addr>,
ProtocolRequest,
MakeError = tower::BoxError,
Service: ProtocolRequestHandler,
Future: Send + 'static,
> + Send
+ 'static
{
}
impl<T, Z: NetworkZone> ProtocolRequestHandlerMaker<Z> for T where
T: tower::MakeService<
client::PeerInformation<Z::Addr>,
ProtocolRequest,
MakeError = tower::BoxError,
Service: ProtocolRequestHandler,
Future: Send + 'static,
> + Send
+ 'static
{ {
type Future2 = T::Future;
} }

View file

@ -14,7 +14,7 @@ use cuprate_p2p_core::{
client::Connector, client::Connector,
client::InternalPeerID, client::InternalPeerID,
services::{AddressBookRequest, AddressBookResponse}, services::{AddressBookRequest, AddressBookResponse},
CoreSyncSvc, NetworkZone, ProtocolRequestHandler, CoreSyncSvc, NetworkZone, ProtocolRequestHandlerMaker,
}; };
mod block_downloader; mod block_downloader;
@ -41,14 +41,14 @@ use connection_maintainer::MakeConnectionRequest;
/// - A core sync service, which keeps track of the sync state of our node /// - A core sync service, which keeps track of the sync state of our node
#[instrument(level = "debug", name = "net", skip_all, fields(zone = N::NAME))] #[instrument(level = "debug", name = "net", skip_all, fields(zone = N::NAME))]
pub async fn initialize_network<N, PR, CS>( pub async fn initialize_network<N, PR, CS>(
protocol_request_handler: PR, protocol_request_handler_maker: PR,
core_sync_svc: CS, core_sync_svc: CS,
config: P2PConfig<N>, config: P2PConfig<N>,
) -> Result<NetworkInterface<N>, tower::BoxError> ) -> Result<NetworkInterface<N>, tower::BoxError>
where where
N: NetworkZone, N: NetworkZone,
N::Addr: borsh::BorshDeserialize + borsh::BorshSerialize, N::Addr: borsh::BorshDeserialize + borsh::BorshSerialize,
PR: ProtocolRequestHandler + Clone, PR: ProtocolRequestHandlerMaker<N> + Clone,
CS: CoreSyncSvc + Clone, CS: CoreSyncSvc + Clone,
{ {
let address_book = let address_book =
@ -73,7 +73,7 @@ where
cuprate_p2p_core::client::HandshakerBuilder::new(basic_node_data) cuprate_p2p_core::client::HandshakerBuilder::new(basic_node_data)
.with_address_book(address_book.clone()) .with_address_book(address_book.clone())
.with_core_sync_svc(core_sync_svc) .with_core_sync_svc(core_sync_svc)
.with_protocol_request_handler(protocol_request_handler) .with_protocol_request_handler_maker(protocol_request_handler_maker)
.with_broadcast_stream_maker(outbound_mkr) .with_broadcast_stream_maker(outbound_mkr)
.with_connection_parent_span(Span::current()); .with_connection_parent_span(Span::current());