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

Commit 0588f3d

Browse files
committed
Reset peers.json if the content is not loadable
Fix #404
1 parent 3269611 commit 0588f3d

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

substrate/network-libp2p/src/network_state.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use std::fs;
3434
use std::io::{Error as IoError, ErrorKind as IoErrorKind, Read, Write};
3535
use std::path::Path;
3636
use std::sync::atomic;
37+
use std::{thread, time};
3738
use std::time::{Duration, Instant};
3839

3940
// File where the peers are stored.
@@ -180,8 +181,21 @@ impl NetworkState {
180181
PeersStorage::Json(peerstore)
181182
} else {
182183
warn!(target: "sub-libp2p", "Failed to open peer storage {:?} \
183-
; peers won't be saved", path);
184-
PeersStorage::Memory(MemoryPeerstore::empty())
184+
; peers file will be reset", path);
185+
fs::remove_file(&path).unwrap();
186+
while Path::new(&path).exists() {
187+
debug!("Waiting for effective deletion of invalid/outdate \
188+
peers.json");
189+
thread::sleep(time::Duration::from_millis(5));
190+
}
191+
if let Ok(peerstore) = JsonPeerstore::new(path.clone()) {
192+
debug!("peers.json reset");
193+
PeersStorage::Json(peerstore)
194+
} else {
195+
warn!(target: "sub-libp2p", "Failed to reset peer storage\
196+
{:?}; peers change will not be saved", path);
197+
PeersStorage::Memory(MemoryPeerstore::empty())
198+
}
185199
}
186200
} else {
187201
debug!(target: "sub-libp2p", "No peers file configured ; peers \
@@ -559,7 +573,7 @@ impl NetworkState {
559573
/// You must pass an `UnboundedSender` which will be used by the `send`
560574
/// method. Actually sending the data is not covered by this code.
561575
///
562-
/// The various methods of the `NetworkState` that close a connection do
576+
/// The various methods of the `NetworkState` that close a connection do
563577
/// so by dropping this sender.
564578
pub fn custom_proto(
565579
&self,
@@ -826,7 +840,7 @@ fn parse_and_add_to_peerstore(addr_str: &str, peerstore: &PeersStorage)
826840
.peer_or_create(&peer_id)
827841
.add_addr(addr, Duration::from_secs(100000 * 365 * 24 * 3600)),
828842
}
829-
843+
830844
Ok(peer_id)
831845
}
832846

0 commit comments

Comments
 (0)