Use blank peer list if saved peer list cannot be read. ()

* Use blank peer list if saved peer list cannot be read.

* Format corrections

* The delete operation is not needed.
This commit is contained in:
Brandon Trussell 2025-04-10 13:25:46 +00:00 committed by GitHub
parent 95aca1d4a5
commit 24265ac43c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 11 additions and 3 deletions
p2p/address-book/src

View file

@ -67,8 +67,11 @@ pub async fn init_address_book<Z: BorshNetworkZone>(
Ok(res) => res,
Err(e) if e.kind() == ErrorKind::NotFound => (vec![], vec![]),
Err(e) => {
tracing::error!("Failed to open peer list, {}", e);
panic!("{e}");
tracing::error!(
"Error: Failed to open peer list,\n{},\nstarting with an empty list",
e
);
(vec![], vec![])
}
};

View file

@ -41,10 +41,15 @@ pub(crate) fn save_peers_to_disk<Z: BorshNetworkZone>(
let dir = cfg.peer_store_directory.clone();
let file = dir.join(Z::NAME);
let mut tmp_file = file.clone();
tmp_file.set_extension("tmp");
spawn_blocking(move || {
fs::create_dir_all(dir)?;
fs::write(&file, &data)
match fs::write(&tmp_file, &data) {
Ok(_) => fs::rename(tmp_file, file),
Err(x) => Err(x),
}
})
}