init config

This commit is contained in:
Boog900 2024-10-06 01:07:47 +01:00
parent 80bfe0a34c
commit a280221cd9
No known key found for this signature in database
GPG key ID: 42AB1287CB0041C2
11 changed files with 103 additions and 12 deletions

4
Cargo.lock generated
View file

@ -519,12 +519,14 @@ version = "0.1.0"
dependencies = [ dependencies = [
"borsh", "borsh",
"cuprate-constants", "cuprate-constants",
"cuprate-helper",
"cuprate-p2p-core", "cuprate-p2p-core",
"cuprate-pruning", "cuprate-pruning",
"cuprate-test-utils", "cuprate-test-utils",
"futures", "futures",
"indexmap", "indexmap",
"rand", "rand",
"serde",
"thiserror", "thiserror",
"tokio", "tokio",
"tokio-util", "tokio-util",
@ -733,6 +735,7 @@ dependencies = [
"libc", "libc",
"monero-serai", "monero-serai",
"rayon", "rayon",
"serde",
"tokio", "tokio",
"windows", "windows",
] ]
@ -812,6 +815,7 @@ dependencies = [
"futures", "futures",
"hex", "hex",
"hex-literal", "hex-literal",
"serde",
"thiserror", "thiserror",
"tokio", "tokio",
"tokio-stream", "tokio-stream",

View file

@ -19,10 +19,10 @@ cuprate-fixed-bytes = { path = "../../net/fixed-bytes" }
cuprate-levin = { path = "../../net/levin" } cuprate-levin = { path = "../../net/levin" }
cuprate-wire = { path = "../../net/wire" } cuprate-wire = { path = "../../net/wire" }
cuprate-p2p = { path = "../../p2p/p2p" } 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-dandelion-tower = { path = "../../p2p/dandelion-tower" }
cuprate-async-buffer = { path = "../../p2p/async-buffer" } 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-blockchain = { path = "../../storage/blockchain" }
cuprate-database-service = { path = "../../storage/service" } cuprate-database-service = { path = "../../storage/service" }
cuprate-txpool = { path = "../../storage/txpool" } cuprate-txpool = { path = "../../storage/txpool" }

View file

@ -1 +1,12 @@
//! cuprated config //! 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,
}

View file

@ -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(),
}
}
}

View file

@ -33,6 +33,8 @@ futures = { workspace = true, optional = true, features = ["std"] }
monero-serai = { workspace = true, optional = true } monero-serai = { workspace = true, optional = true }
rayon = { workspace = true, optional = true } rayon = { workspace = true, optional = true }
serde = { workspace = true, optional = true, features = ["derive"] }
# This is kinda a stupid work around. # This is kinda a stupid work around.
# [thread] needs to activate one of these libs (windows|libc) # [thread] needs to activate one of these libs (windows|libc)
# although it depends on what target we're building for. # although it depends on what target we're building for.

View file

@ -5,6 +5,7 @@
//! into it's own crate. //! into it's own crate.
//! //!
//! `#[no_std]` compatible. //! `#[no_std]` compatible.
// TODO: move to types crate.
const MAINNET_NETWORK_ID: [u8; 16] = [ const MAINNET_NETWORK_ID: [u8; 16] = [
0x12, 0x30, 0xF1, 0x71, 0x61, 0x04, 0x41, 0x61, 0x17, 0x31, 0x00, 0x82, 0x16, 0xA1, 0xA1, 0x10, 0x12, 0x30, 0xF1, 0x71, 0x61, 0x04, 0x41, 0x61, 0x17, 0x31, 0x00, 0x82, 0x16, 0xA1, 0xA1, 0x10,
@ -18,6 +19,7 @@ const STAGENET_NETWORK_ID: [u8; 16] = [
/// An enum representing every Monero network. /// An enum representing every Monero network.
#[derive(Debug, Clone, Copy, Default)] #[derive(Debug, Clone, Copy, Default)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum Network { pub enum Network {
/// Mainnet /// Mainnet
#[default] #[default]

View file

@ -5,9 +5,13 @@ edition = "2021"
license = "MIT" license = "MIT"
authors = ["Boog900"] authors = ["Boog900"]
[features]
defualt = []
serde_config = ["dep:serde", "dep:cuprate-helper"]
[dependencies] [dependencies]
cuprate-constants = { path = "../../constants" } cuprate-constants = { path = "../../constants" }
cuprate-helper = { path = "../../helper", features = ["std", "fs"], optional = true }
cuprate-pruning = { path = "../../pruning" } cuprate-pruning = { path = "../../pruning" }
cuprate-p2p-core = { path = "../p2p-core" } cuprate-p2p-core = { path = "../p2p-core" }
@ -24,6 +28,7 @@ indexmap = { workspace = true, features = ["std"] }
rand = { workspace = true, features = ["std", "std_rng"] } 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] [dev-dependencies]
cuprate-test-utils = {path = "../../test-utils"} cuprate-test-utils = {path = "../../test-utils"}

View file

@ -20,6 +20,8 @@ mod store;
/// The address book config. /// The address book config.
#[derive(Debug, Clone)] #[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 { pub struct AddressBookConfig {
/// The maximum number of white peers in the peer list. /// The maximum number of white peers in the peer list.
/// ///
@ -29,12 +31,24 @@ pub struct AddressBookConfig {
/// ///
/// Gray peers are peers we are yet to make a connection to. /// Gray peers are peers we are yet to make a connection to.
pub max_gray_list_length: usize, pub max_gray_list_length: usize,
/// The location to store the address book. /// The location to store the peer store file.
pub peer_store_file: PathBuf, pub peer_store_folder: PathBuf,
/// The amount of time between saving the address book to disk. /// The amount of time between saving the address book to disk.
pub peer_save_period: Duration, 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. /// Possible errors when dealing with the address book.
/// This is boxed when returning an error in the [`tower::Service`]. /// This is boxed when returning an error in the [`tower::Service`].
#[derive(Debug, thiserror::Error, Eq, PartialEq)] #[derive(Debug, thiserror::Error, Eq, PartialEq)]
@ -63,11 +77,6 @@ pub enum AddressBookError {
pub async fn init_address_book<Z: BorshNetworkZone>( pub async fn init_address_book<Z: BorshNetworkZone>(
cfg: AddressBookConfig, cfg: AddressBookConfig,
) -> Result<book::AddressBook<Z>, std::io::Error> { ) -> 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 { let (white_list, gray_list) = match store::read_peers_from_disk::<Z>(&cfg).await {
Ok(res) => res, Ok(res) => res,
Err(e) if e.kind() == ErrorKind::NotFound => (vec![], vec![]), Err(e) if e.kind() == ErrorKind::NotFound => (vec![], vec![]),

View file

@ -39,7 +39,7 @@ pub(crate) fn save_peers_to_disk<Z: BorshNetworkZone>(
}) })
.unwrap(); .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)) spawn_blocking(move || fs::write(&file, &data))
} }
@ -52,7 +52,10 @@ pub(crate) async fn read_peers_from_disk<Z: BorshNetworkZone>(
), ),
std::io::Error, 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 data = spawn_blocking(move || fs::read(file)).await.unwrap()?;
let de_ser: DeserPeerDataV1<Z::Addr> = from_slice(&data)?; let de_ser: DeserPeerDataV1<Z::Addr> = from_slice(&data)?;

View file

@ -27,6 +27,7 @@ tracing = { workspace = true, features = ["std", "attributes"] }
hex-literal = { workspace = true } hex-literal = { workspace = true }
borsh = { workspace = true, features = ["derive", "std"], optional = true } borsh = { workspace = true, features = ["derive", "std"], optional = true }
serde = { workspace = true, features = ["derive", "std"], optional = true }
[dev-dependencies] [dev-dependencies]
cuprate-test-utils = { path = "../../test-utils" } cuprate-test-utils = { path = "../../test-utils" }

View file

@ -38,10 +38,20 @@ impl NetZoneAddress for SocketAddr {
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[cfg_attr(feature = "serde", serde(deny_unknown_fields))]
pub struct ClearNetServerCfg { pub struct ClearNetServerCfg {
pub ip: IpAddr, pub ip: IpAddr,
} }
impl Default for ClearNetServerCfg {
fn default() -> Self {
Self {
ip: IpAddr::V4(Ipv4Addr::UNSPECIFIED),
}
}
}
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub enum ClearNet {} pub enum ClearNet {}