Skip to content

Commit

Permalink
feat(config): add --fresh and --clean flags to the config to prevent use
Browse files Browse the repository at this point in the history
of older configurations
  • Loading branch information
lionel-faber committed Nov 10, 2020
1 parent 4a14488 commit b685d77
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
12 changes: 9 additions & 3 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,12 @@ impl QuicP2p {

let mut qp2p_config = cfg.clone();

if cfg.clean {
BootstrapCache::clear_from_disk(custom_dirs.as_ref())?;
}

let mut bootstrap_cache =
BootstrapCache::new(cfg.hard_coded_contacts, custom_dirs.as_ref())?;
BootstrapCache::new(cfg.hard_coded_contacts, custom_dirs.as_ref(), cfg.fresh)?;
if use_bootstrap_cache {
bootstrap_cache.peers_mut().extend(bootstrap_nodes);
} else {
Expand Down Expand Up @@ -428,9 +432,11 @@ fn bind(

fn unwrap_config_or_default(cfg: Option<Config>) -> Result<Config> {
let mut cfg = cfg.map_or(Config::read_or_construct_default(None)?, |cfg| cfg);
if cfg.ip.is_none() {
if cfg.ip.is_none() && cfg.forward_port {
cfg.ip = crate::igd::get_local_ip().ok();
};

if cfg.clean {
Config::clear_config_from_disk(None)?;
}
Ok(cfg)
}
34 changes: 28 additions & 6 deletions src/bootstrap_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ impl BootstrapCache {
pub fn new(
hard_coded_contacts: HashSet<SocketAddr>,
user_override: Option<&Dirs>,
fresh: bool,
) -> Result<BootstrapCache> {
let path = |dir: &Dirs| {
let path = dir.cache_dir();
Expand All @@ -55,7 +56,11 @@ impl BootstrapCache {
)?;

let peers = if cache_path.exists() {
utils::read_from_disk(&cache_path)?
if fresh {
VecDeque::new()
} else {
utils::read_from_disk(&cache_path)?
}
} else {
let cache_dir = cache_path
.parent()
Expand Down Expand Up @@ -107,6 +112,23 @@ impl BootstrapCache {
self.try_sync_to_disk();
}

pub fn clear_from_disk(user_override: Option<&Dirs>) -> Result<()> {
let path = |dir: &Dirs| {
let path = dir.cache_dir();
path.join("bootstrap_cache")
};

let cache_path = user_override.map_or_else(
|| Ok::<_, Error>(path(&utils::project_dir()?)),
|d| Ok(path(d)),
)?;

if cache_path.exists() {
std::fs::remove_file(cache_path)?;
}
Ok(())
}

fn move_to_cache_top(&mut self, peer: SocketAddr) {
if let Some(pos) = self.peers.iter().position(|p| *p == peer) {
let _ = self.peers.remove(pos);
Expand Down Expand Up @@ -138,15 +160,15 @@ mod tests {
#[test]
fn when_10_peers_are_added_they_are_synced_to_disk() {
let dirs = test_dirs();
let mut cache = unwrap!(BootstrapCache::new(Default::default(), Some(&dirs)));
let mut cache = unwrap!(BootstrapCache::new(Default::default(), Some(&dirs), false));

for _ in 0..10 {
cache.add_peer(rand_node_addr());
}

assert_eq!(cache.peers.len(), 10);

let cache = unwrap!(BootstrapCache::new(Default::default(), Some(&dirs)));
let cache = unwrap!(BootstrapCache::new(Default::default(), Some(&dirs), false));
assert_eq!(cache.peers.len(), 10);
}

Expand All @@ -158,7 +180,7 @@ mod tests {
assert!(hard_coded.insert(peer1));

let dirs = test_dirs();
let mut cache = BootstrapCache::new(hard_coded, Some(&dirs)).unwrap();
let mut cache = BootstrapCache::new(hard_coded, Some(&dirs), false).unwrap();

cache.add_peer(peer1);
cache.add_peer(peer2);
Expand All @@ -172,7 +194,7 @@ mod tests {
let dirs = test_dirs();
let port_base = 5000;

let mut cache = unwrap!(BootstrapCache::new(Default::default(), Some(&dirs)));
let mut cache = unwrap!(BootstrapCache::new(Default::default(), Some(&dirs), false));

for i in 0..MAX_CACHE_SIZE {
cache.add_peer(make_node_addr(port_base + i as u16));
Expand All @@ -190,7 +212,7 @@ mod tests {
#[test]
fn it_moves_given_node_to_the_top_of_the_list() {
let dirs = test_dirs();
let mut cache = BootstrapCache::new(Default::default(), Some(&dirs)).unwrap();
let mut cache = BootstrapCache::new(Default::default(), Some(&dirs), false).unwrap();
let peer1 = rand_node_addr();
let peer2 = rand_node_addr();
let peer3 = rand_node_addr();
Expand Down
15 changes: 15 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ pub struct Config {
/// Specify if port forwarding via UPnP should be done or not
#[structopt(long)]
pub forward_port: bool,
/// Use a fresh config without re-using any config available on disk
#[structopt(long)]
pub fresh: bool,
/// Clean all existing config available on disk
#[structopt(long)]
pub clean: bool,
}

impl Config {
Expand All @@ -91,6 +97,15 @@ impl Config {
Ok(cfg)
}
}

/// Clear all configuration files from disk
pub fn clear_config_from_disk(user_override: Option<&Dirs>) -> Result<()> {
let config_path = config_path(user_override)?;
if config_path.exists() {
fs::remove_file(&config_path)?;
}
Ok(())
}
}

/// To be used to read and write our certificate and private key to disk esp. as a part of our
Expand Down
1 change: 1 addition & 0 deletions src/tests/echo_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ async fn echo_service() -> Result<()> {
Some(Config {
port: None,
ip: None,
forward_port: true,
..Default::default()
}),
Default::default(),
Expand Down

0 comments on commit b685d77

Please sign in to comment.