Skip to content

Commit

Permalink
init config
Browse files Browse the repository at this point in the history
  • Loading branch information
Boog900 committed Oct 6, 2024
1 parent 80bfe0a commit a280221
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 12 deletions.
4 changes: 4 additions & 0 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions binaries/cuprated/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ cuprate-fixed-bytes = { path = "../../net/fixed-bytes" }
cuprate-levin = { path = "../../net/levin" }
cuprate-wire = { path = "../../net/wire" }
cuprate-p2p = { path = "../../p2p/p2p" }
cuprate-p2p-core = { path = "../../p2p/p2p-core" }
cuprate-p2p-core = { path = "../../p2p/p2p-core", features = ["serde"] }
cuprate-dandelion-tower = { path = "../../p2p/dandelion-tower" }
cuprate-async-buffer = { path = "../../p2p/async-buffer" }
cuprate-address-book = { path = "../../p2p/address-book" }
cuprate-address-book = { path = "../../p2p/address-book", features = ["serde_config"] }
cuprate-blockchain = { path = "../../storage/blockchain" }
cuprate-database-service = { path = "../../storage/service" }
cuprate-txpool = { path = "../../storage/txpool" }
Expand Down
11 changes: 11 additions & 0 deletions binaries/cuprated/src/config.rs
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
//! cuprated config
use serde::{Deserialize, Serialize};

mod sections;

use sections::P2PConfig;

#[derive(Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields, default)]
pub struct Config {
p2p: P2PConfig,
}
44 changes: 44 additions & 0 deletions binaries/cuprated/src/config/sections.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use cuprate_address_book::AddressBookConfig;
use cuprate_p2p_core::ClearNetServerCfg;
use serde::{Deserialize, Serialize};

#[derive(Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields, default)]
pub struct P2PConfig {
clear_net: ClearNetConfig,
}

#[derive(Default, Deserialize, Serialize)]
#[serde(deny_unknown_fields, default)]
pub struct ClearNetConfig {
server: ClearNetServerCfg,
#[serde(flatten)]
flattened: SharedNetConfig,
}

#[derive(Deserialize, Serialize)]
#[serde(deny_unknown_fields, default)]
pub struct SharedNetConfig {
/// 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
pub max_inbound_connections: usize,
/// port to use to accept p2p connections.
pub p2p_port: u16,
/// The address book config.
pub address_book_config: AddressBookConfig,
}

impl Default for SharedNetConfig {
fn default() -> Self {
Self {
outbound_connections: 32,
extra_outbound_connections: 8,
max_inbound_connections: 128,
p2p_port: 18080,
address_book_config: AddressBookConfig::default(),
}
}
}
2 changes: 2 additions & 0 deletions helper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ futures = { workspace = true, optional = true, features = ["std"] }
monero-serai = { workspace = true, optional = true }
rayon = { workspace = true, optional = true }

serde = { workspace = true, optional = true, features = ["derive"] }

# This is kinda a stupid work around.
# [thread] needs to activate one of these libs (windows|libc)
# although it depends on what target we're building for.
Expand Down
2 changes: 2 additions & 0 deletions helper/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! into it's own crate.
//!
//! `#[no_std]` compatible.
// TODO: move to types crate.

const MAINNET_NETWORK_ID: [u8; 16] = [
0x12, 0x30, 0xF1, 0x71, 0x61, 0x04, 0x41, 0x61, 0x17, 0x31, 0x00, 0x82, 0x16, 0xA1, 0xA1, 0x10,
Expand All @@ -18,6 +19,7 @@ const STAGENET_NETWORK_ID: [u8; 16] = [

/// An enum representing every Monero network.
#[derive(Debug, Clone, Copy, Default)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum Network {
/// Mainnet
#[default]
Expand Down
7 changes: 6 additions & 1 deletion p2p/address-book/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ edition = "2021"
license = "MIT"
authors = ["Boog900"]

[features]
defualt = []
serde_config = ["dep:serde", "dep:cuprate-helper"]

[dependencies]
cuprate-constants = { path = "../../constants" }
cuprate-helper = { path = "../../helper", features = ["std", "fs"], optional = true }
cuprate-pruning = { path = "../../pruning" }
cuprate-p2p-core = { path = "../p2p-core" }

Expand All @@ -23,7 +27,8 @@ indexmap = { workspace = true, features = ["std"] }

rand = { workspace = true, features = ["std", "std_rng"] }

borsh = { workspace = true, features = ["derive", "std"]}
borsh = { workspace = true, features = ["derive", "std"] }
serde = { workspace = true, features = ["derive", "std"], optional = true }

[dev-dependencies]
cuprate-test-utils = {path = "../../test-utils"}
Expand Down
23 changes: 16 additions & 7 deletions p2p/address-book/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ mod store;

/// The address book config.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde_config", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde_config", serde(deny_unknown_fields, default))]
pub struct AddressBookConfig {
/// The maximum number of white peers in the peer list.
///
Expand All @@ -29,12 +31,24 @@ pub struct AddressBookConfig {
///
/// Gray peers are peers we are yet to make a connection to.
pub max_gray_list_length: usize,
/// The location to store the address book.
pub peer_store_file: PathBuf,
/// The location to store the peer store file.
pub peer_store_folder: PathBuf,
/// The amount of time between saving the address book to disk.
pub peer_save_period: Duration,
}

#[cfg(feature = "serde_config")]
impl Default for AddressBookConfig {
fn default() -> Self {
Self {
max_white_list_length: 1000,
max_gray_list_length: 5000,
peer_store_folder: cuprate_helper::fs::CUPRATE_CACHE_DIR.clone(),
peer_save_period: Duration::from_secs(90),
}
}
}

/// Possible errors when dealing with the address book.
/// This is boxed when returning an error in the [`tower::Service`].
#[derive(Debug, thiserror::Error, Eq, PartialEq)]
Expand Down Expand Up @@ -63,11 +77,6 @@ pub enum AddressBookError {
pub async fn init_address_book<Z: BorshNetworkZone>(
cfg: AddressBookConfig,
) -> Result<book::AddressBook<Z>, std::io::Error> {
tracing::info!(
"Loading peers from file: {} ",
cfg.peer_store_file.display()
);

let (white_list, gray_list) = match store::read_peers_from_disk::<Z>(&cfg).await {
Ok(res) => res,
Err(e) if e.kind() == ErrorKind::NotFound => (vec![], vec![]),
Expand Down
7 changes: 5 additions & 2 deletions p2p/address-book/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub(crate) fn save_peers_to_disk<Z: BorshNetworkZone>(
})
.unwrap();

let file = cfg.peer_store_file.clone();
let file = cfg.peer_store_folder.join(format!("{}_p2p_state", Z::NAME));
spawn_blocking(move || fs::write(&file, &data))
}

Expand All @@ -52,7 +52,10 @@ pub(crate) async fn read_peers_from_disk<Z: BorshNetworkZone>(
),
std::io::Error,
> {
let file = cfg.peer_store_file.clone();
let file = cfg.peer_store_folder.join(format!("{}_p2p_state", Z::NAME));

tracing::info!("Loading peers from file: {} ", file.display());

let data = spawn_blocking(move || fs::read(file)).await.unwrap()?;

let de_ser: DeserPeerDataV1<Z::Addr> = from_slice(&data)?;
Expand Down
1 change: 1 addition & 0 deletions p2p/p2p-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ tracing = { workspace = true, features = ["std", "attributes"] }
hex-literal = { workspace = true }

borsh = { workspace = true, features = ["derive", "std"], optional = true }
serde = { workspace = true, features = ["derive", "std"], optional = true }

[dev-dependencies]
cuprate-test-utils = { path = "../../test-utils" }
Expand Down
10 changes: 10 additions & 0 deletions p2p/p2p-core/src/network_zones/clear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,20 @@ impl NetZoneAddress for SocketAddr {
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct ClearNetServerCfg {
pub ip: IpAddr,
}

impl Default for ClearNetServerCfg {
fn default() -> Self {
Self {
ip: IpAddr::V4(Ipv4Addr::UNSPECIFIED),
}
}
}

#[derive(Clone, Copy)]
pub enum ClearNet {}

Expand Down

0 comments on commit a280221

Please sign in to comment.