From e8baf3d40d0b6e77e254fbe4b437a5744b13765d Mon Sep 17 00:00:00 2001 From: Boog900 <54e72d8a-345f-4599-bd90-c6b9bc7d0ec5@aleeas.com> Date: Mon, 17 Jun 2024 19:52:22 +0100 Subject: [PATCH] remove borsh requirement --- Cargo.lock | 50 +------------------ p2p/address-book/src/book.rs | 11 ++-- p2p/address-book/src/lib.rs | 20 +++++++- p2p/address-book/src/store.rs | 8 +-- p2p/cuprate-p2p/Cargo.toml | 3 +- p2p/cuprate-p2p/src/lib.rs | 1 + p2p/monero-p2p/src/client/handshaker.rs | 1 + .../src/client/handshaker/builder.rs | 23 ++++++--- p2p/monero-p2p/src/lib.rs | 33 ------------ 9 files changed, 48 insertions(+), 102 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a1fd8d766..cb44311a7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -598,6 +598,7 @@ dependencies = [ name = "cuprate-p2p" version = "0.1.0" dependencies = [ + "borsh", "bytes", "cuprate-helper", "cuprate-test-utils", @@ -621,7 +622,6 @@ dependencies = [ "tokio-util", "tower", "tracing", - "tracing-subscriber", ] [[package]] @@ -1710,16 +1710,6 @@ dependencies = [ "zeroize", ] -[[package]] -name = "nu-ansi-term" -version = "0.46.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" -dependencies = [ - "overload", - "winapi", -] - [[package]] name = "num-traits" version = "0.2.19" @@ -1767,12 +1757,6 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" -[[package]] -name = "overload" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" - [[package]] name = "page_size" version = "0.6.0" @@ -2399,15 +2383,6 @@ dependencies = [ "keccak", ] -[[package]] -name = "sharded-slab" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" -dependencies = [ - "lazy_static", -] - [[package]] name = "signal-hook-registry" version = "1.4.2" @@ -2772,18 +2747,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" dependencies = [ "once_cell", - "valuable", -] - -[[package]] -name = "tracing-log" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" -dependencies = [ - "log", - "once_cell", - "tracing-core", ] [[package]] @@ -2792,12 +2755,7 @@ version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" dependencies = [ - "nu-ansi-term", - "sharded-slab", - "smallvec", - "thread_local", "tracing-core", - "tracing-log", ] [[package]] @@ -2853,12 +2811,6 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" -[[package]] -name = "valuable" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" - [[package]] name = "version_check" version = "0.9.4" diff --git a/p2p/address-book/src/book.rs b/p2p/address-book/src/book.rs index 2f0617e9b..fddf72e28 100644 --- a/p2p/address-book/src/book.rs +++ b/p2p/address-book/src/book.rs @@ -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; @@ -45,7 +48,7 @@ pub struct ConnectionPeerEntry { rpc_credits_per_hash: u32, } -pub struct AddressBook { +pub struct AddressBook { /// Our white peers - the peers we have previously connected to. white_list: PeerList, /// Our gray peers - the peers we have been told about but haven't connected to. @@ -66,7 +69,7 @@ pub struct AddressBook { cfg: AddressBookConfig, } -impl AddressBook { +impl AddressBook { pub fn new( cfg: AddressBookConfig, white_peers: Vec>, @@ -351,7 +354,7 @@ impl AddressBook { } } -impl Service> for AddressBook { +impl Service> for AddressBook { type Response = AddressBookResponse; type Error = AddressBookError; type Future = Ready>; diff --git a/p2p/address-book/src/lib.rs b/p2p/address-book/src/lib.rs index 51f83ddc8..66cc5c842 100644 --- a/p2p/address-book/src/lib.rs +++ b/p2p/address-book/src/lib.rs @@ -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; @@ -61,7 +61,7 @@ pub enum AddressBookError { } /// Initializes the P2P address book for a specific network zone. -pub async fn init_address_book( +pub async fn init_address_book( cfg: AddressBookConfig, ) -> Result, std::io::Error> { tracing::info!( @@ -82,3 +82,19 @@ pub async fn init_address_book( Ok(address_book) } + +use sealed::*; +mod sealed { + use super::*; + + pub trait BorshNetworkZone: NetworkZone { + type BorshAddr: NetZoneAddress + borsh::BorshDeserialize + borsh::BorshSerialize; + } + + impl BorshNetworkZone for T + where + T::Addr: borsh::BorshDeserialize + borsh::BorshSerialize, + { + type BorshAddr = T::Addr; + } +} diff --git a/p2p/address-book/src/store.rs b/p2p/address-book/src/store.rs index c15e0a77b..5d799ea4d 100644 --- a/p2p/address-book/src/store.rs +++ b/p2p/address-book/src/store.rs @@ -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. @@ -21,7 +21,7 @@ struct DeserPeerDataV1 { gray_list: Vec>, } -pub fn save_peers_to_disk( +pub fn save_peers_to_disk( cfg: &AddressBookConfig, white_list: &PeerList, gray_list: &PeerList, @@ -38,7 +38,7 @@ pub fn save_peers_to_disk( spawn_blocking(move || fs::write(&file, &data)) } -pub async fn read_peers_from_disk( +pub async fn read_peers_from_disk( cfg: &AddressBookConfig, ) -> Result< ( diff --git a/p2p/cuprate-p2p/Cargo.toml b/p2p/cuprate-p2p/Cargo.toml index 687493a0d..147bc3093 100644 --- a/p2p/cuprate-p2p/Cargo.toml +++ b/p2p/cuprate-p2p/Cargo.toml @@ -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" } diff --git a/p2p/cuprate-p2p/src/lib.rs b/p2p/cuprate-p2p/src/lib.rs index ffb3266b0..8141c330d 100644 --- a/p2p/cuprate-p2p/src/lib.rs +++ b/p2p/cuprate-p2p/src/lib.rs @@ -49,6 +49,7 @@ pub async fn initialize_network( ) -> Result, tower::BoxError> where N: NetworkZone, + N::Addr: borsh::BorshDeserialize + borsh::BorshSerialize, R: PeerRequestHandler + Clone, CS: CoreSyncSvc + Clone, { diff --git a/p2p/monero-p2p/src/client/handshaker.rs b/p2p/monero-p2p/src/client/handshaker.rs index 65c00c4c4..a2e36c962 100644 --- a/p2p/monero-p2p/src/client/handshaker.rs +++ b/p2p/monero-p2p/src/client/handshaker.rs @@ -229,6 +229,7 @@ pub async fn ping(addr: N::Addr) -> Result } /// This function completes a handshake with the requested peer. +#[allow(clippy::too_many_arguments)] async fn handshake( req: DoHandshakeRequest, diff --git a/p2p/monero-p2p/src/client/handshaker/builder.rs b/p2p/monero-p2p/src/client/handshaker/builder.rs index c3af5139e..ffcd3d70a 100644 --- a/p2p/monero-p2p/src/client/handshaker/builder.rs +++ b/p2p/monero-p2p/src/client/handshaker/builder.rs @@ -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, diff --git a/p2p/monero-p2p/src/lib.rs b/p2p/monero-p2p/src/lib.rs index 4ae97e8fe..4b6bb6211 100644 --- a/p2p/monero-p2p/src/lib.rs +++ b/p2p/monero-p2p/src/lib.rs @@ -39,7 +39,6 @@ pub enum ConnectionDirection { OutBound, } -#[cfg(not(feature = "borsh"))] pub trait NetZoneAddress: TryFrom + Into @@ -69,38 +68,6 @@ pub trait NetZoneAddress: fn should_add_to_peer_list(&self) -> bool; } -#[cfg(feature = "borsh")] -pub trait NetZoneAddress: - TryFrom - + Into - + 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 {