Skip to content

Commit

Permalink
start network init
Browse files Browse the repository at this point in the history
  • Loading branch information
Boog900 committed May 19, 2024
1 parent 8eaaac5 commit f818b6a
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 11 deletions.
2 changes: 1 addition & 1 deletion p2p/address-book/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ monero-pruning = { path = "../../pruning" }
monero-wire = { path= "../../net/monero-wire" }
monero-p2p = { path = "../monero-p2p" }

tower = { workspace = true, features = ["util", "buffer"] }
tower = { workspace = true, features = ["util"] }
tokio = { workspace = true, features = ["time", "fs", "rt"]}
tokio-util = { workspace = true, features = ["time"] }

Expand Down
4 changes: 2 additions & 2 deletions p2p/address-book/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub enum AddressBookError {
/// Initializes the P2P address book for a specific network zone.
pub async fn init_address_book<Z: NetworkZone>(
cfg: AddressBookConfig,
) -> Result<Buffer<book::AddressBook<Z>, AddressBookRequest<Z>>, std::io::Error> {
) -> Result<book::AddressBook<Z>, std::io::Error> {
tracing::info!(
"Loading peers from file: {} ",
cfg.peer_store_file.display()
Expand All @@ -82,5 +82,5 @@ pub async fn init_address_book<Z: NetworkZone>(

let address_book = book::AddressBook::<Z>::new(cfg, white_list, gray_list, Vec::new());

Ok(Buffer::new(address_book, 150))
Ok(address_book)
}
2 changes: 1 addition & 1 deletion p2p/cuprate-p2p/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ cuprate-helper = { path = "../../helper", features = ["asynch"] }

monero-serai = { workspace = true, features = ["std"] }

tower = { workspace = true }
tower = { workspace = true, features = ["buffer"] }
tokio = { workspace = true, features = ["rt"] }
rayon = { workspace = true }
tokio-util = { workspace = true }
Expand Down
41 changes: 40 additions & 1 deletion p2p/cuprate-p2p/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,51 @@
use cuprate_helper::network::Network;
use monero_address_book::AddressBookConfig;
use monero_p2p::NetworkZone;
use monero_wire::common::PeerSupportFlags;
use monero_wire::BasicNodeData;

/// P2P config.
#[derive(Clone, Debug)]
pub struct P2PConfig {
pub struct P2PConfig<N: NetworkZone> {
pub network: Network,

/// The number of outbound connections to make and try keep.
pub outbound_connections: usize,
/// The amount of extra connections we can make if we are under load from the rest of Cuprate.
pub extra_outbound_connections: usize,
/// The maximum amount of inbound connections, only relevant if [`P2PConfig::server_config`] is set to [`Some`]
pub max_inbound_connections: usize,
/// The percent of outbound peers that should be gray aka never connected to before.
///
/// Only values 0..=1 are valid.
pub gray_peers_percent: f64,
/// The inbound server configuration,
///
/// If this is [`None`] no inbound connections will be accepted.
pub server_config: Option<N::ServerCfg>,

/// The port to listen on for inbound connections, only relevant if [`P2PConfig::server_config`] is set to [`Some`].
pub p2p_port: u16,
/// The public RPC port to tell peers about so wallets can use our node. `0` if we do not have a public RPC port.
pub rpc_port: u16,

pub address_book_config: AddressBookConfig,
}

impl<N: NetworkZone> P2PConfig<N> {
/// Returns the [`BasicNodeData`] for this [`P2PConfig`].
///
/// [`BasicNodeData::peer_id`] is set to a random u64, so this function should only be called once
/// per [`NetworkZone`].
pub(crate) fn basic_node_data(&self) -> BasicNodeData {
BasicNodeData {
my_port: self.p2p_port as u32,
network_id: self.network.network_id(),
peer_id: rand::random(),
support_flags: PeerSupportFlags::FLUFFY_BLOCKS,
rpc_port: self.rpc_port,
// We do not (and probably will never) support paying for RPC with hashes.
rpc_credits_per_hash: 0,
}
}
}
4 changes: 2 additions & 2 deletions p2p/cuprate-p2p/src/connection_maintainer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ pub struct OutboundConnectionKeeper<N: NetworkZone, A, C> {
/// we add a permit to the semaphore and keep track here, upto a value in config.
pub extra_peers: usize,
/// The p2p config.
pub config: P2PConfig,
pub config: P2PConfig<N>,
/// The [`Bernoulli`] distribution, when sampled will return true if we should connect to a gray peer or
/// false if we should connect to a white peer.
///
Expand All @@ -76,7 +76,7 @@ where
C::Future: Send + 'static,
{
pub fn new(
config: P2PConfig,
config: P2PConfig<N>,
client_pool: Arc<ClientPool<N>>,
make_connection_rx: mpsc::Receiver<MakeConnectionRequest>,
address_book_svc: A,
Expand Down
64 changes: 63 additions & 1 deletion p2p/cuprate-p2p/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,73 @@
//!
#![allow(dead_code)]

use std::sync::Arc;
use tokio::sync::watch;
use tower::buffer::Buffer;

use monero_p2p::NetworkZone;

mod broadcast;
pub mod client_pool;
mod client_pool;
pub mod config;
pub mod connection_maintainer;
mod constants;
mod sync_states;

pub use config::P2PConfig;

pub async fn initialize_network<N, R, CS>(
peer_req_handler: R,
core_sync_svc: CS,
config: P2PConfig<N>,
) -> Result<NetworkInterface<N>, tower::BoxError>
where
N: NetworkZone,
{
let address_book = monero_address_book::init_address_book(config.address_book_config).await?;
let address_book = Buffer::new(
address_book,
config.max_inbound_connections + config.outbound_connections,
);

let (sync_states_svc, top_block_watcher) = sync_states::PeerSyncSvc::new();

// Use the default config. Changing the defaults effects tx fluff times, which could effect D++ so for now don't allow changing
// this.
let (broadcast_svc, outbound_mkr, inbound_mkr) =
broadcast::init_broadcast_channels(&broadcast::BroadcastConfig::default());

let mut basic_node_data = config.basic_node_data();

if !N::CHECK_NODE_ID {
// TODO: make sure this is the value monerod sets for anonn networks.
basic_node_data.peer_id = 1;
}

let outbound_handshaker = monero_p2p::client::HandShaker::new(
address_book.clone(),
sync_states_svc,
core_sync_svc,
peer_req_handler,
outbound_mkr,
basic_node_data,
);

let inbound_handshaker = monero_p2p::client::HandShaker::new(
address_book.clone(),
sync_states_svc,
core_sync_svc,
peer_req_handler,
inbound_mkr,
basic_node_data,
);

let outb

}

pub struct NetworkInterface<N: NetworkZone> {
pool: Arc<client_pool::ClientPool<N>>,
broadcast_svc: broadcast::BroadcastSvc<N>,
top_block_watch: watch::Receiver<sync_states::NewSyncInfo>,
}
6 changes: 3 additions & 3 deletions p2p/cuprate-p2p/src/sync_states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ use crate::{client_pool::disconnect_monitor::PeerDisconnectFut, constants::SHORT
#[derive(Debug)]
pub struct NewSyncInfo {
/// The peers chain height.
chain_height: u64,
pub chain_height: u64,
/// The peers top block's hash.
top_hash: [u8; 32],
pub top_hash: [u8; 32],
/// The peers cumulative difficulty.
cumulative_difficulty: u128,
pub cumulative_difficulty: u128,
}

/// A service that keeps track of our peers blockchains.
Expand Down

0 comments on commit f818b6a

Please sign in to comment.