Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Reset peers.json if the content is not loadable
Fix #404
  • Loading branch information
chevdor committed Jul 24, 2018
commit a805bcc784cd7149af665ac42535181967fe6d04
31 changes: 26 additions & 5 deletions substrate/network-libp2p/src/network_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,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 @@ -179,9 +180,29 @@ impl NetworkState {
file {:?}", path);
PeersStorage::Json(peerstore)
} else {
warn!(target: "sub-libp2p", "Failed to open peer storage {:?} \
; peers won't be saved", path);
PeersStorage::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");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this also happens if the file doesn't exist in the first place, ie. if the user starts polkadot for the first time, so the message is not totally correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the file does not exist in the first place, the first test L179 will kick in and the message you mention L196 will never show up.

PeersStorage::Json(peerstore)
} else {
warn!(target: "sub-libp2p", "Failed to reset peer storage\
{:?}; peers change will not be saved", path);
PeersStorage::Memory(MemoryPeerstore::empty())
}
}
} else {
debug!(target: "sub-libp2p", "No peers file configured ; peers \
Expand Down Expand Up @@ -559,7 +580,7 @@ impl NetworkState {
/// You must pass an `UnboundedSender` which will be used by the `send`
/// method. Actually sending the data is not covered by this code.
///
/// The various methods of the `NetworkState` that close a connection do
/// The various methods of the `NetworkState` that close a connection do
/// so by dropping this sender.
pub fn custom_proto(
&self,
Expand Down Expand Up @@ -826,7 +847,7 @@ fn parse_and_add_to_peerstore(addr_str: &str, peerstore: &PeersStorage)
.peer_or_create(&peer_id)
.add_addr(addr, Duration::from_secs(100000 * 365 * 24 * 3600)),
}

Ok(peer_id)
}

Expand Down