Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swarm Bootstrap Nodes #201

Merged
merged 11 commits into from
Aug 15, 2023
7 changes: 6 additions & 1 deletion homestar-runtime/src/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use crate::{
use anyhow::Result;
use async_trait::async_trait;
use fnv::FnvHashMap;
use libp2p::{futures::StreamExt, kad::QueryId, request_response::RequestId, swarm::Swarm};
use libp2p::{
core::ConnectedPoint, futures::StreamExt, kad::QueryId, request_response::RequestId,
swarm::Swarm, PeerId,
};
use std::{sync::Arc, time::Duration};
use swarm_event::ResponseEvent;
use tokio::{select, sync::mpsc};
Expand Down Expand Up @@ -48,6 +51,7 @@ pub(crate) struct EventHandler<DB: Database> {
sender: Arc<mpsc::Sender<Event>>,
receiver: mpsc::Receiver<Event>,
query_senders: FnvHashMap<QueryId, (RequestResponseKey, P2PSender)>,
connected_peers: FnvHashMap<PeerId, ConnectedPoint>,
mriise marked this conversation as resolved.
Show resolved Hide resolved
request_response_senders: FnvHashMap<RequestId, (RequestResponseKey, P2PSender)>,
}

Expand All @@ -71,6 +75,7 @@ where
sender: Arc::new(sender),
receiver,
query_senders: FnvHashMap::default(),
connected_peers: FnvHashMap::default(),
request_response_senders: FnvHashMap::default(),
}
}
Expand Down
20 changes: 20 additions & 0 deletions homestar-runtime/src/event_handler/swarm_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,26 @@ async fn handle_swarm_event<THandlerErr: fmt::Debug + Send, DB: Database>(
);
}
SwarmEvent::IncomingConnection { .. } => {}
SwarmEvent::ConnectionEstablished {
peer_id, endpoint, ..
} => {
let behavior = event_handler.swarm.behaviour_mut();
// only listener addresses should be added to the routing table.
// TODO: add identify here to discover more listen addresses and add those.
if endpoint.is_listener() {
// ignores if the peer failed or not to be added in the routing table.
behavior
.kademlia
.add_address(&peer_id, endpoint.get_remote_address().clone());
mriise marked this conversation as resolved.
Show resolved Hide resolved
}

// add peer to connected peers list
event_handler.connected_peers.insert(peer_id, endpoint);
mriise marked this conversation as resolved.
Show resolved Hide resolved
}
SwarmEvent::ConnectionClosed { peer_id, cause, .. } => {
info!("peer connection closed {peer_id}, cause: {cause:?}");
event_handler.connected_peers.remove_entry(&peer_id);
zeeshanlakhani marked this conversation as resolved.
Show resolved Hide resolved
}
_ => {}
}
}
Expand Down
13 changes: 6 additions & 7 deletions homestar-runtime/src/network/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,20 @@ pub(crate) async fn new(settings: &settings::Node) -> Result<Swarm<ComposedBehav
}

fn startup<T: NetworkBehaviour>(swarm: &mut Swarm<T>, settings: &settings::Network) -> Result<()> {
// Dial bootstrap nodes specified in settings. Failure here shouldn't halt node startup.
// Listen-on given address
swarm.listen_on(settings.listen_address.to_string().parse()?)?;

// Dial trusted nodes specified in settings. Failure here shouldn't halt node startup.
for trusted_addr in &settings.trusted_node_addresses {
swarm
.dial(trusted_addr.clone())
.map(|_| {
info!(trusted_address=?trusted_addr, "Successfully dialed configured bootstrap node")
info!(trusted_address=?trusted_addr, "Successfully dialed configured trusted node")
})
// log dial failure and continue
.map_err(|e| warn!(err=?e, "Failed to dial bootstrap node"))
.map_err(|e| warn!(err=?e, "Failed to dial trusted node"))
.ok();
}

// Listen-on given address
swarm.listen_on(settings.listen_address.to_string().parse()?)?;

Ok(())
}

Expand Down