-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Reset peers.json if the content is not loadable #405
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -175,17 +176,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", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can also happen if the user doesn't have the permission to open the file, so the message should just be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would not the message L182 already say that? If the file cannot be open because of permissions, this should be caught by the first tests. The message |
||
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()) | ||
}; | ||
|
||
|
@@ -554,7 +573,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, | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.