Skip to content

Commit 57e5c26

Browse files
authored
Merge 32db62e into 9e3a7c9
2 parents 9e3a7c9 + 32db62e commit 57e5c26

File tree

10 files changed

+68
-136
lines changed

10 files changed

+68
-136
lines changed

Cargo.lock

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

crates/merod/src/cli/init.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ pub struct InitCommand {
108108
#[clap(overrides_with("mdns"))]
109109
pub no_mdns: bool,
110110

111+
/// Advertise observed address
112+
#[clap(long, default_value_t = false)]
113+
#[clap(overrides_with("no_mdns"))]
114+
pub advertise_address: bool,
115+
111116
#[clap(
112117
long,
113118
default_value = "3",
@@ -395,6 +400,7 @@ impl InitCommand {
395400
BootstrapConfig::new(BootstrapNodes::new(boot_nodes)),
396401
DiscoveryConfig::new(
397402
mdns,
403+
self.advertise_address,
398404
RendezvousConfig::new(self.rendezvous_registrations_limit),
399405
RelayConfig::new(self.relay_registrations_limit),
400406
AutonatConfig::new(self.autonat_confidence_threshold),

crates/network/src/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ pub struct DiscoveryConfig {
119119
#[serde(default = "calimero_primitives::common::bool_true")]
120120
pub mdns: bool,
121121

122+
pub advertise_address: bool,
123+
122124
pub rendezvous: RendezvousConfig,
123125

124126
pub relay: RelayConfig,
@@ -130,12 +132,14 @@ impl DiscoveryConfig {
130132
#[must_use]
131133
pub const fn new(
132134
mdns: bool,
135+
advertise_address: bool,
133136
rendezvous: RendezvousConfig,
134137
relay: RelayConfig,
135138
autonat: AutonatConfig,
136139
) -> Self {
137140
Self {
138141
mdns,
142+
advertise_address,
139143
rendezvous,
140144
relay,
141145
autonat,
@@ -147,6 +151,7 @@ impl Default for DiscoveryConfig {
147151
fn default() -> Self {
148152
Self {
149153
mdns: true,
154+
advertise_address: false,
150155
rendezvous: RendezvousConfig::default(),
151156
relay: RelayConfig::default(),
152157
autonat: AutonatConfig::default(),

crates/network/src/discovery.rs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub mod state;
1414

1515
#[derive(Debug)]
1616
pub struct Discovery {
17+
pub(crate) advertise_address: bool,
1718
pub(crate) state: DiscoveryState,
1819
pub(crate) rendezvous_config: RendezvousConfig,
1920
pub(crate) relay_config: RelayConfig,
@@ -22,11 +23,13 @@ pub struct Discovery {
2223

2324
impl Discovery {
2425
pub(crate) fn new(
26+
advertise_address: bool,
2527
rendezvous_config: &RendezvousConfig,
2628
relay_config: &RelayConfig,
2729
autonat_config: &AutonatConfig,
2830
) -> Self {
2931
Self {
32+
advertise_address,
3033
state: DiscoveryState::default(),
3134
rendezvous_config: rendezvous_config.clone(),
3235
relay_config: relay_config.clone(),
@@ -282,27 +285,28 @@ impl EventLoop {
282285
Ok(())
283286
}
284287

285-
// Add a peer to the list of servers that may be used for determining our NAT status.
286-
// These peers are used for dial-request even if they are currently not connected,
287-
// in which case a connection will be established before sending the dial-request.
288-
pub(crate) fn add_autonat_server(&mut self, autonat_peer: &PeerId) -> EyreResult<()> {
289-
let peer_info = self
290-
.discovery
291-
.state
292-
.get_peer_info(autonat_peer)
293-
.wrap_err("Failed to get peer info")?;
294-
295-
debug!(
296-
%autonat_peer,
297-
?peer_info,
298-
"Adding peer to the list of autonat servers"
299-
);
300-
301-
self.swarm
302-
.behaviour_mut()
303-
.autonat
304-
.add_server(*autonat_peer, None);
305-
306-
Ok(())
307-
}
288+
// TODO: Revisit AutoNAT protocol integration
289+
// // Add a peer to the list of servers that may be used for determining our NAT status.
290+
// // These peers are used for dial-request even if they are currently not connected,
291+
// // in which case a connection will be established before sending the dial-request.
292+
// pub(crate) fn add_autonat_server(&mut self, autonat_peer: &PeerId) -> EyreResult<()> {
293+
// let peer_info = self
294+
// .discovery
295+
// .state
296+
// .get_peer_info(autonat_peer)
297+
// .wrap_err("Failed to get peer info")?;
298+
299+
// debug!(
300+
// %autonat_peer,
301+
// ?peer_info,
302+
// "Adding peer to the list of autonat servers"
303+
// );
304+
305+
// self.swarm
306+
// .behaviour_mut()
307+
// .autonat
308+
// .add_server(*autonat_peer, None);
309+
310+
// Ok(())
311+
// }
308312
}

crates/network/src/discovery/state.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,10 @@ impl DiscoveryState {
164164
self.rendezvous_index.contains(peer_id)
165165
}
166166

167-
pub(crate) fn is_peer_autonat(&self, peer_id: &PeerId) -> bool {
168-
self.autonat_index.contains(peer_id)
169-
}
167+
// TOOD: Revisit AutoNAT protocol integration
168+
// pub(crate) fn is_peer_autonat(&self, peer_id: &PeerId) -> bool {
169+
// self.autonat_index.contains(peer_id)
170+
// }
170171

171172
#[expect(
172173
clippy::arithmetic_side_effects,

crates/network/src/events.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use eyre::eyre;
77
use libp2p::core::ConnectedPoint;
88
use multiaddr::Protocol;
9-
use tracing::error;
9+
use tracing::{error, info};
1010

1111
use super::*;
1212
use crate::discovery::state::{PeerDiscoveryMechanism, RelayReservationStatus};
@@ -148,26 +148,24 @@ impl EventLoop {
148148
trace!("New external address candidate: {}", address);
149149
}
150150
SwarmEvent::ExternalAddrConfirmed { address } => {
151-
debug!("External address confirmed: {}", address);
151+
info!("External address confirmed: {}", address);
152152
if let Ok(relayed_addr) = RelayedMultiaddr::try_from(&address) {
153153
self.discovery.state.update_relay_reservation_status(
154154
&relayed_addr.relay_peer,
155155
RelayReservationStatus::Accepted,
156156
);
157-
158-
self.broadcast_rendezvous_registrations();
159157
}
158+
self.broadcast_rendezvous_registrations();
160159
}
161160
SwarmEvent::ExternalAddrExpired { address } => {
162-
debug!("External address expired: {}", address);
161+
info!("External address expired: {}", address);
163162
if let Ok(relayed_addr) = RelayedMultiaddr::try_from(&address) {
164163
self.discovery.state.update_relay_reservation_status(
165164
relayed_addr.relay_peer_id(),
166165
RelayReservationStatus::Expired,
167166
);
168-
169-
self.broadcast_rendezvous_registrations();
170167
}
168+
self.broadcast_rendezvous_registrations();
171169
}
172170
SwarmEvent::NewExternalAddrOfPeer { peer_id, address } => {
173171
debug!("New external address of peer: {} {}", peer_id, address);

crates/network/src/events/identify.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use libp2p::identify::Event;
22
use owo_colors::OwoColorize;
3-
use tracing::{debug, error};
3+
use tracing::{debug, error, info};
44

55
use super::{EventHandler, EventLoop};
66

@@ -13,32 +13,28 @@ impl EventHandler<Event> for EventLoop {
1313
.state
1414
.update_peer_protocols(&peer_id, &info.protocols);
1515

16-
if self.discovery.state.is_peer_autonat(&peer_id) {
17-
if let Err(err) = self.add_autonat_server(&peer_id) {
18-
error!(%err, "Failed to add autonat server");
19-
};
20-
}
16+
// TODO: Revist AutoNAT protocol implementation
17+
// if self.discovery.state.is_peer_autonat(&peer_id) {
18+
// if let Err(err) = self.add_autonat_server(&peer_id) {
19+
// error!(%err, "Failed to add autonat server");
20+
// };
21+
// }
2122

22-
if self.discovery.state.is_peer_relay(&peer_id)
23-
&& self.discovery.state.is_autonat_status_private()
24-
{
25-
if let Err(err) = self.create_relay_reservation(&peer_id) {
26-
error!(%err, "Failed to handle relay reservation");
27-
};
23+
if self.discovery.advertise_address {
24+
info!("Adding external address: {:?}", info.observed_addr);
25+
self.swarm.add_external_address(info.observed_addr);
26+
} else {
27+
if self.discovery.state.is_peer_relay(&peer_id) {
28+
if let Err(err) = self.create_relay_reservation(&peer_id) {
29+
error!(%err, "Failed to handle relay reservation");
30+
};
31+
}
2832
}
2933

30-
if self.discovery.state.is_peer_rendezvous(&peer_id)
31-
&& self.discovery.state.is_autonat_status_public()
32-
&& self.swarm.behaviour().autonat.confidence()
33-
>= self.discovery.autonat_config.confidence_threshold
34-
{
34+
if self.discovery.state.is_peer_rendezvous(&peer_id) {
3535
if let Err(err) = self.rendezvous_discover(&peer_id) {
3636
error!(%err, "Failed to perform rendezvous discovery");
3737
};
38-
39-
if let Err(err) = self.rendezvous_register(&peer_id) {
40-
error!(%err, "Failed to update registration discovery");
41-
};
4238
}
4339
}
4440
}

crates/network/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ fn init(
183183
};
184184

185185
let discovery = Discovery::new(
186+
config.discovery.advertise_address,
186187
&config.discovery.rendezvous,
187188
&config.discovery.relay,
188189
&config.discovery.autonat,

crates/server/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ license.workspace = true
88

99
[dependencies]
1010
axum.workspace = true
11-
axum-server.workspace = true
12-
axum-server-dual-protocol.workspace = true
11+
# axum-server.workspace = true
12+
# axum-server-dual-protocol.workspace = true
1313
base64.workspace = true
1414
borsh = { workspace = true, features = ["derive"] }
1515
bs58.workspace = true

crates/server/src/lib.rs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
use core::net::{IpAddr, SocketAddr};
2-
use std::io::Error as IoError;
32
use std::sync::Arc;
43

54
use admin::storage::jwt_secret::get_or_create_jwt_secret;
65
use axum::http::Method;
76
use axum::Router;
8-
use axum_server::tls_rustls::RustlsConfig;
9-
use axum_server_dual_protocol::bind_dual_protocol;
107
use calimero_context::ContextManager;
118
use calimero_node_primitives::ServerSender;
129
use calimero_primitives::events::NodeEvent;
@@ -22,7 +19,6 @@ use tower_http::cors::{Any, CorsLayer};
2219
use tracing::warn;
2320

2421
use crate::admin::service::{setup, site};
25-
use crate::certificates::get_certificate;
2622

2723
pub mod certificates;
2824

@@ -167,34 +163,12 @@ pub async fn start(
167163
])
168164
.allow_private_network(true),
169165
);
170-
// Check if the certificate exists and if they contain the current local IP address
171-
let (cert_pem, key_pem) = get_certificate(&store)?;
172-
173-
// Configure certificate and private key used by https
174-
let rustls_config = match RustlsConfig::from_pem(cert_pem, key_pem).await {
175-
Ok(config) => config,
176-
Err(e) => {
177-
eprintln!("Failed to load TLS configuration: {e:?}");
178-
return Err(e.into());
179-
}
180-
};
181166

182167
let mut set = JoinSet::new();
183168

184169
for listener in listeners {
185-
let rustls_config = rustls_config.clone();
186170
let app = app.clone();
187-
let addr = listener.local_addr().unwrap();
188-
drop(set.spawn(async move {
189-
if let Err(e) = bind_dual_protocol(addr, rustls_config)
190-
.serve(app.into_make_service())
191-
.await
192-
{
193-
eprintln!("Server error: {e:?}");
194-
return Err(e);
195-
}
196-
Ok::<(), IoError>(())
197-
}));
171+
drop(set.spawn(async move { axum::serve(listener, app).await }));
198172
}
199173

200174
while let Some(result) = set.join_next().await {

0 commit comments

Comments
 (0)