Skip to content

Commit a1ca920

Browse files
committed
feat: add bon support
1 parent 5fd2e1b commit a1ca920

5 files changed

Lines changed: 42 additions & 104 deletions

File tree

Cargo.lock

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ tracing-subscriber = { version = "0.3", features = ["env-filter"] }
6060
tracing-loki = "0.2"
6161
vise-exporter = "0.3"
6262
vise = "0.3"
63+
bon = "3.8"
6364

6465
# Crates in the workspace
6566
charon = { path = "crates/charon" }

crates/relay-server/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ publish.workspace = true
88

99
[dependencies]
1010
axum.workspace = true
11+
bon.workspace = true
1112
libp2p.workspace = true
1213
thiserror.workspace = true
1314
k256.workspace = true

crates/relay-server/examples/relay_server.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ async fn main() {
1515
charon_tracing::init(&TracingConfig::default()).expect("Failed to initialize tracing");
1616

1717
let config = Config::builder()
18-
.with_p2p_config(
18+
.p2p_config(
1919
P2PConfig::builder()
2020
.with_tcp_addrs(vec![
2121
multiaddr::Multiaddr::from_str("/ip4/0.0.0.0/tcp/0")
@@ -24,9 +24,9 @@ async fn main() {
2424
])
2525
.build(),
2626
)
27-
.with_max_conns(100)
28-
.with_max_res_per_peer(10)
29-
.with_http_addr(Some("0.0.0.0:8888".to_string()))
27+
.max_conns(100)
28+
.max_res_per_peer(10)
29+
.http_addr("0.0.0.0:8888".to_string())
3030
.build();
3131
let key = SecretKey::random(&mut OsRng);
3232

crates/relay-server/src/config.rs

Lines changed: 10 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use std::{path::PathBuf, time::Duration};
22

3+
use bon::Builder;
4+
use charon_p2p::config::P2PConfig;
35
use charon_tracing::TracingConfig;
46
use libp2p::relay;
57

6-
use charon_p2p::config::P2PConfig;
7-
88
/// One hour in seconds.
99
pub const ONE_HOUR_SECONDS: u64 = 60 * 60;
1010
/// One minute in seconds.
@@ -15,125 +15,35 @@ pub const MB_32: u64 = 32 * 1024 * 1024;
1515
pub const EXTERNAL_HOST_RESOLVE_INTERVAL: Duration = Duration::from_secs(5 * 60);
1616

1717
/// Configuration for the relay P2P layer.
18-
#[derive(Default, Debug, Clone)]
18+
#[derive(Default, Debug, Clone, Builder)]
1919
pub struct Config {
2020
/// The directory to store the relay data.
21-
pub data_dir: PathBuf,
21+
pub data_dir: Option<PathBuf>,
2222
/// The HTTP address to listen on.
2323
pub http_addr: Option<String>,
2424
/// The monitoring address to listen on.
25-
pub monitoring_addr: String,
25+
pub monitoring_addr: Option<String>,
2626
/// The debug address to listen on.
27-
pub debug_addr: String,
27+
pub debug_addr: Option<String>,
2828
/// The P2P configuration.
2929
pub p2p_config: P2PConfig,
3030
/// The logging configuration.
31-
pub log_config: TracingConfig,
31+
pub log_config: Option<TracingConfig>,
3232
/// Whether to automatically generate a P2P key.
33+
#[builder(default = false)]
3334
pub auto_p2p_key: bool,
3435
/// The maximum number of resources per peer.
3536
pub max_res_per_peer: usize,
3637
/// The maximum number of connections.
3738
pub max_conns: usize,
3839
/// Whether to filter private addresses.
40+
#[builder(default = false)]
3941
pub filter_private_addrs: bool,
4042
/// LibP2PLogLevel.
43+
#[builder(default = "Info".to_string())]
4144
pub libp2p_log_level: String,
4245
}
4346

44-
impl Config {
45-
/// Returns a new builder for configuring a relay P2P layer.
46-
pub fn builder() -> ConfigBuilder {
47-
ConfigBuilder::new()
48-
}
49-
}
50-
51-
/// Builder for [`Config`].
52-
#[derive(Default, Debug, Clone)]
53-
pub struct ConfigBuilder {
54-
config: Config,
55-
}
56-
57-
impl ConfigBuilder {
58-
/// Creates a new builder with default configuration.
59-
pub fn new() -> Self {
60-
Self {
61-
config: Config::default(),
62-
}
63-
}
64-
65-
/// Sets the data directory.
66-
pub fn with_data_dir(mut self, data_dir: PathBuf) -> Self {
67-
self.config.data_dir = data_dir;
68-
self
69-
}
70-
71-
/// Sets the HTTP address.
72-
pub fn with_http_addr(mut self, http_addr: Option<String>) -> Self {
73-
self.config.http_addr = http_addr;
74-
self
75-
}
76-
77-
/// Sets the monitoring address.
78-
pub fn with_monitoring_addr(mut self, monitoring_addr: String) -> Self {
79-
self.config.monitoring_addr = monitoring_addr;
80-
self
81-
}
82-
83-
/// Sets the debug address.
84-
pub fn with_debug_addr(mut self, debug_addr: String) -> Self {
85-
self.config.debug_addr = debug_addr;
86-
self
87-
}
88-
89-
/// Sets the P2P configuration.
90-
pub fn with_p2p_config(mut self, p2p_config: P2PConfig) -> Self {
91-
self.config.p2p_config = p2p_config;
92-
self
93-
}
94-
95-
/// Sets the logging configuration.
96-
pub fn with_log_config(mut self, log_config: TracingConfig) -> Self {
97-
self.config.log_config = log_config;
98-
self
99-
}
100-
101-
/// Sets whether to automatically generate a P2P key.
102-
pub fn with_auto_p2p_key(mut self, auto_p2p_key: bool) -> Self {
103-
self.config.auto_p2p_key = auto_p2p_key;
104-
self
105-
}
106-
107-
/// Sets the maximum number of resources per peer.
108-
pub fn with_max_res_per_peer(mut self, max_res_per_peer: usize) -> Self {
109-
self.config.max_res_per_peer = max_res_per_peer;
110-
self
111-
}
112-
113-
/// Sets the maximum number of connections.
114-
pub fn with_max_conns(mut self, max_conns: usize) -> Self {
115-
self.config.max_conns = max_conns;
116-
self
117-
}
118-
119-
/// Sets whether to filter private addresses.
120-
pub fn with_filter_private_addrs(mut self, filter_private_addrs: bool) -> Self {
121-
self.config.filter_private_addrs = filter_private_addrs;
122-
self
123-
}
124-
125-
/// Sets the LibP2P log level.
126-
pub fn with_libp2p_log_level(mut self, libp2p_log_level: String) -> Self {
127-
self.config.libp2p_log_level = libp2p_log_level;
128-
self
129-
}
130-
131-
/// Builds the [`Config`].
132-
pub fn build(self) -> Config {
133-
self.config
134-
}
135-
}
136-
13747
pub(crate) fn create_relay_config(config: &Config) -> relay::Config {
13848
relay::Config {
13949
max_reservations: config.max_conns,

0 commit comments

Comments
 (0)