Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Reset peers.json if the content is not loadable (#405)
Browse files Browse the repository at this point in the history
* Reset peers.json if the content is not loadable

Fix #404

* Whitespace
  • Loading branch information
chevdor authored and gavofyork committed Jul 27, 2018
1 parent 0d99cb2 commit 90a6dd5
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions substrate/network-libp2p/src/network_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ use std::fs;
use std::io::{Error as IoError, ErrorKind as IoErrorKind, Read, Write};
use std::path::Path;
use std::sync::atomic;
use std::{thread, time};
use std::time::{Duration, Instant};

// File where the peers are stored.
Expand Down Expand Up @@ -174,17 +175,35 @@ impl NetworkState {
let node_store = if let Some(ref path) = config.net_config_path {
let path = Path::new(path).join(NODES_FILE);
if let Ok(node_store) = JsonPeerstore::new(path.clone()) {
debug!(target: "sub-libp2p", "Initialized peer store for JSON \
file {:?}", path);
debug!(target: "sub-libp2p", "Initialized peer store for JSON file {:?}", path);
NodeStore::Json(node_store)
} else {
warn!(target: "sub-libp2p", "Failed to open peer storage {:?} \
; peers won't be saved", path);
NodeStore::Memory(MemoryPeerstore::empty())
warn!(target: "sub-libp2p", "Failed to open peer storage {:?}; peers file will be reset", path);
fs::remove_file(&path).expect("Failed deleting peers.json");

// we check for about 1s if the file was really deleted and move on
for _x in 0..200 {
if !Path::new(&path).exists() {
break;
} else {
debug!("Waiting for effective deletion of invalid/outdate peers.json");
thread::sleep(time::Duration::from_millis(5));
}
}

if let Ok(peerstore) = JsonPeerstore::new(path.clone()) {
debug!("peers.json reset");
NodeStore::Json(peerstore)
} else {
warn!(target: "sub-libp2p",
"Failed to reset peer storage {:?}; peers change will not be saved",
path
);
NodeStore::Memory(MemoryPeerstore::empty())
}
}
} else {
debug!(target: "sub-libp2p", "No peers file configured ; peers \
won't be saved");
debug!(target: "sub-libp2p", "No peers file configured ; peers won't be saved");
NodeStore::Memory(MemoryPeerstore::empty())
};

Expand Down

0 comments on commit 90a6dd5

Please sign in to comment.