Skip to content

Commit

Permalink
feat(api): add public API to get an endpoint's list of bootstrap nodes
Browse files Browse the repository at this point in the history
- this commit also makes some improvements to information in logs
- also adds a force argument for BootstrapCache::try_sync_to_disk
  • Loading branch information
lionel-faber committed Apr 26, 2021
1 parent 0684769 commit 075c89e
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 16 deletions.
8 changes: 5 additions & 3 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use super::{
};
use futures::future;
use log::{debug, error, info, trace};
use std::collections::HashSet;
use std::net::{SocketAddr, UdpSocket};
use std::path::PathBuf;

Expand Down Expand Up @@ -97,6 +98,7 @@ impl QuicP2p {

let mut bootstrap_cache =
BootstrapCache::new(cfg.hard_coded_contacts, custom_dirs.as_ref())?;
trace!("Peers in bootstrap cache: {:?}", bootstrap_cache.peers());
if !use_bootstrap_cache {
let bootstrap_cache = bootstrap_cache.peers_mut();
bootstrap_cache.clear();
Expand Down Expand Up @@ -288,12 +290,12 @@ impl QuicP2p {

/// Clears the current bootstrap cache and replaces the peer list with the provided
/// bootstrap nodes.
pub fn update_bootstrap_cache(&mut self, bootstrap_nodes: &[SocketAddr]) -> Result<()> {
pub fn update_bootstrap_contacts(&mut self, bootstrap_nodes: &[SocketAddr]) {
self.qp2p_config.hard_coded_contacts = HashSet::new();
let bootstrap_cache = self.bootstrap_cache.peers_mut();
bootstrap_cache.clear();
bootstrap_cache.extend(bootstrap_nodes);
self.bootstrap_cache.try_sync_to_disk();
Ok(())
self.bootstrap_cache.try_sync_to_disk(true);
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/bootstrap_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl BootstrapCache {
if self.peers.len() > MAX_CACHE_SIZE {
let _ = self.peers.pop_front();
}
self.try_sync_to_disk();
self.try_sync_to_disk(false);
}

pub fn clear_from_disk(user_override: Option<&PathBuf>) -> Result<()> {
Expand All @@ -134,8 +134,9 @@ impl BootstrapCache {
}

/// Write cached peers to disk every 10 inserted peers.
pub(crate) fn try_sync_to_disk(&mut self) {
if self.add_count > 9 {
/// If the bootstrap cache is to be updated explicitly force can be set to `true`.
pub(crate) fn try_sync_to_disk(&mut self, force: bool) {
if self.add_count > 9 || force {
if let Err(e) = write_to_disk(&self.cache_path, &self.peers) {
warn!("Failed to write bootstrap cache to disk: {}", e);
}
Expand Down
5 changes: 1 addition & 4 deletions src/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,6 @@ pub async fn read_bytes(recv: &mut quinn::RecvStream) -> Result<WireMsg> {
pub async fn send_msg(mut send_stream: &mut quinn::SendStream, msg: Bytes) -> Result<()> {
let wire_msg = WireMsg::UserMsg(msg);
wire_msg.write_to_stream(&mut send_stream).await?;

trace!("Message was sent to remote peer");

Ok(())
}

Expand Down Expand Up @@ -198,7 +195,7 @@ pub(super) fn listen_for_incoming_messages(
)
.await;

log::trace!("The connection has been terminated.");
log::trace!("The connection to {:?} has been terminated.", src);
let _ = disconnection_tx.send(src);
remover.remove();
});
Expand Down
5 changes: 4 additions & 1 deletion src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,10 @@ impl Endpoint {
result
}

pub(crate) fn bootstrap_nodes(&self) -> &[SocketAddr] {
/// Returns the list of boostrap nodes that the endpoint will try bootstrapping to.
/// This is the combined list of contacts from the Hard Coded contacts in the config
/// and the bootstrap cache (if enabled)
pub fn bootstrap_nodes(&self) -> &[SocketAddr] {
&self.bootstrap_nodes
}
}
1 change: 1 addition & 0 deletions src/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ async fn many_messages() -> Result<()> {
// bootstrap contacts later.
#[tokio::test]
async fn connection_attempts_to_bootstrap_contacts_should_succeed() -> Result<()> {
utils::init_logging();
let qp2p = new_qp2p()?;

let (ep1, _, _, _) = qp2p.new_endpoint().await?;
Expand Down
5 changes: 0 additions & 5 deletions src/wire_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use crate::{
utils,
};
use bytes::Bytes;
use log::trace;
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;
use std::{fmt, net::SocketAddr};
Expand All @@ -37,11 +36,9 @@ impl WireMsg {
// Read a message's bytes from the provided stream
pub async fn read_from_stream(recv: &mut quinn::RecvStream) -> Result<Self> {
let mut header_bytes = [0; MSG_HEADER_LEN];
log::debug!("reading header");
recv.read_exact(&mut header_bytes).await?;

let msg_header = MsgHeader::from_bytes(header_bytes);
log::debug!("reading data of length: {}", msg_header.data_len());
// https://github.com/rust-lang/rust/issues/70460 for work on a cleaner alternative:
#[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
{
Expand All @@ -61,7 +58,6 @@ impl WireMsg {
let msg_flag = msg_header.usr_msg_flag();

recv.read_exact(&mut data).await?;
trace!("Got new message with {} bytes.", data.len());

if data.is_empty() {
Err(Error::EmptyResponse)
Expand All @@ -81,7 +77,6 @@ impl WireMsg {
WireMsg::UserMsg(ref m) => (m.clone(), USER_MSG_FLAG),
_ => (From::from(bincode::serialize(&self)?), ECHO_SRVC_MSG_FLAG),
};
trace!("Sending message to remote peer ({} bytes)", msg_bytes.len());

let msg_header = MsgHeader::new(&msg_bytes, msg_flag)?;
let header_bytes = msg_header.to_bytes();
Expand Down

0 comments on commit 075c89e

Please sign in to comment.