Skip to content

Commit

Permalink
remove borsh requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
Boog900 committed Jun 17, 2024
1 parent cc27b61 commit e8baf3d
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 102 deletions.
50 changes: 1 addition & 49 deletions Cargo.lock

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

11 changes: 7 additions & 4 deletions p2p/address-book/src/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ use monero_p2p::{
};
use monero_pruning::PruningSeed;

use crate::{peer_list::PeerList, store::save_peers_to_disk, AddressBookConfig, AddressBookError};
use crate::{
peer_list::PeerList, store::save_peers_to_disk, AddressBookConfig, AddressBookError,
BorshNetworkZone,
};

#[cfg(test)]
mod tests;
Expand All @@ -45,7 +48,7 @@ pub struct ConnectionPeerEntry<Z: NetworkZone> {
rpc_credits_per_hash: u32,
}

pub struct AddressBook<Z: NetworkZone> {
pub struct AddressBook<Z: BorshNetworkZone> {
/// Our white peers - the peers we have previously connected to.
white_list: PeerList<Z>,
/// Our gray peers - the peers we have been told about but haven't connected to.
Expand All @@ -66,7 +69,7 @@ pub struct AddressBook<Z: NetworkZone> {
cfg: AddressBookConfig,
}

impl<Z: NetworkZone> AddressBook<Z> {
impl<Z: BorshNetworkZone> AddressBook<Z> {
pub fn new(
cfg: AddressBookConfig,
white_peers: Vec<ZoneSpecificPeerListEntryBase<Z::Addr>>,
Expand Down Expand Up @@ -351,7 +354,7 @@ impl<Z: NetworkZone> AddressBook<Z> {
}
}

impl<Z: NetworkZone> Service<AddressBookRequest<Z>> for AddressBook<Z> {
impl<Z: BorshNetworkZone> Service<AddressBookRequest<Z>> for AddressBook<Z> {
type Response = AddressBookResponse<Z>;
type Error = AddressBookError;
type Future = Ready<Result<Self::Response, Self::Error>>;
Expand Down
20 changes: 18 additions & 2 deletions p2p/address-book/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//!
use std::{io::ErrorKind, path::PathBuf, time::Duration};

use monero_p2p::NetworkZone;
use monero_p2p::{NetZoneAddress, NetworkZone};

mod book;
mod peer_list;
Expand Down Expand Up @@ -61,7 +61,7 @@ pub enum AddressBookError {
}

/// Initializes the P2P address book for a specific network zone.
pub async fn init_address_book<Z: NetworkZone>(
pub async fn init_address_book<Z: BorshNetworkZone>(
cfg: AddressBookConfig,
) -> Result<book::AddressBook<Z>, std::io::Error> {
tracing::info!(
Expand All @@ -82,3 +82,19 @@ pub async fn init_address_book<Z: NetworkZone>(

Ok(address_book)
}

use sealed::*;
mod sealed {
use super::*;

pub trait BorshNetworkZone: NetworkZone<Addr = Self::BorshAddr> {
type BorshAddr: NetZoneAddress + borsh::BorshDeserialize + borsh::BorshSerialize;
}

impl<T: NetworkZone> BorshNetworkZone for T
where
T::Addr: borsh::BorshDeserialize + borsh::BorshSerialize,
{
type BorshAddr = T::Addr;
}
}
8 changes: 4 additions & 4 deletions p2p/address-book/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::fs;
use borsh::{from_slice, to_vec, BorshDeserialize, BorshSerialize};
use tokio::task::{spawn_blocking, JoinHandle};

use monero_p2p::{services::ZoneSpecificPeerListEntryBase, NetZoneAddress, NetworkZone};
use monero_p2p::{services::ZoneSpecificPeerListEntryBase, NetZoneAddress};

use crate::{peer_list::PeerList, AddressBookConfig};
use crate::{peer_list::PeerList, AddressBookConfig, BorshNetworkZone};

// TODO: store anchor and ban list.

Expand All @@ -21,7 +21,7 @@ struct DeserPeerDataV1<A: NetZoneAddress> {
gray_list: Vec<ZoneSpecificPeerListEntryBase<A>>,
}

pub fn save_peers_to_disk<Z: NetworkZone>(
pub fn save_peers_to_disk<Z: BorshNetworkZone>(
cfg: &AddressBookConfig,
white_list: &PeerList<Z>,
gray_list: &PeerList<Z>,
Expand All @@ -38,7 +38,7 @@ pub fn save_peers_to_disk<Z: NetworkZone>(
spawn_blocking(move || fs::write(&file, &data))
}

pub async fn read_peers_from_disk<Z: NetworkZone>(
pub async fn read_peers_from_disk<Z: BorshNetworkZone>(
cfg: &AddressBookConfig,
) -> Result<
(
Expand Down
3 changes: 1 addition & 2 deletions p2p/cuprate-p2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ rand = { workspace = true, features = ["std", "std_rng"] }
rand_distr = { workspace = true, features = ["std"] }
hex = { workspace = true, features = ["std"] }
tracing = { workspace = true, features = ["std", "attributes"] }

tracing-subscriber = "0.3.18"
borsh = { workspace = true, features = ["derive", "std"] }

[dev-dependencies]
cuprate-test-utils = { path = "../../test-utils" }
1 change: 1 addition & 0 deletions p2p/cuprate-p2p/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub async fn initialize_network<N, R, CS>(
) -> Result<NetworkInterface<N>, tower::BoxError>
where
N: NetworkZone,
N::Addr: borsh::BorshDeserialize + borsh::BorshSerialize,
R: PeerRequestHandler + Clone,
CS: CoreSyncSvc + Clone,
{
Expand Down
1 change: 1 addition & 0 deletions p2p/monero-p2p/src/client/handshaker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ pub async fn ping<N: NetworkZone>(addr: N::Addr) -> Result<u64, HandshakeError>
}

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

Expand Down
23 changes: 15 additions & 8 deletions p2p/monero-p2p/src/client/handshaker/builder.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
use std::{
future::{ready, Ready},
marker::PhantomData,
task::{Context, Poll},
};

use futures::{stream, Stream};
use std::future::{ready, Ready};
use std::marker::PhantomData;
use std::task::{Context, Poll};
use tower::Service;
use tracing::Span;

use monero_wire::{BasicNodeData, CoreSyncData};

use crate::client::{handshaker::HandShaker, InternalPeerID};
use crate::services::{
AddressBookRequest, AddressBookResponse, CoreSyncDataRequest, CoreSyncDataResponse,
PeerSyncRequest, PeerSyncResponse,
};
use crate::{
client::{handshaker::HandShaker, InternalPeerID},
services::{
AddressBookRequest, AddressBookResponse, CoreSyncDataRequest, CoreSyncDataResponse,
PeerSyncRequest, PeerSyncResponse,
},
AddressBook, BroadcastMessage, CoreSyncSvc, NetworkZone, PeerRequest, PeerRequestHandler,
PeerResponse, PeerSyncSvc,
};

/// A [`HandShaker`] [`Service`] builder.
///
///
///
#[derive(Debug, Clone)]
pub struct HandshakerBuilder<
N: NetworkZone,
Expand Down
33 changes: 0 additions & 33 deletions p2p/monero-p2p/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ pub enum ConnectionDirection {
OutBound,
}

#[cfg(not(feature = "borsh"))]
pub trait NetZoneAddress:
TryFrom<NetworkAddress, Error = NetworkAddressIncorrectZone>
+ Into<NetworkAddress>
Expand Down Expand Up @@ -69,38 +68,6 @@ pub trait NetZoneAddress:
fn should_add_to_peer_list(&self) -> bool;
}

#[cfg(feature = "borsh")]
pub trait NetZoneAddress:
TryFrom<NetworkAddress, Error = NetworkAddressIncorrectZone>
+ Into<NetworkAddress>
+ std::fmt::Display
+ borsh::BorshSerialize
+ borsh::BorshDeserialize
+ Hash
+ Eq
+ Copy
+ Send
+ Sync
+ Unpin
+ 'static
{
/// Cuprate needs to be able to ban peers by IP addresses and not just by SocketAddr as
/// that include the port, to be able to facilitate this network addresses must have a ban ID
/// which for hidden services could just be the address it self but for clear net addresses will
/// be the IP address.
/// TODO: IP zone banning?
type BanID: Debug + Hash + Eq + Clone + Copy + Send + 'static;

/// Changes the port of this address to `port`.
fn set_port(&mut self, port: u16);

fn make_canonical(&mut self);

fn ban_id(&self) -> Self::BanID;

fn should_add_to_peer_list(&self) -> bool;
}

/// An abstraction over a network zone (tor/i2p/clear)
#[async_trait::async_trait]
pub trait NetworkZone: Clone + Copy + Send + 'static {
Expand Down

0 comments on commit e8baf3d

Please sign in to comment.