refactor: move write_peers_to_file to NetworkManager impl (#9134)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Querty
2024-07-03 16:05:44 +02:00
committed by GitHub
parent d41aac380b
commit c5167a4784
6 changed files with 39 additions and 29 deletions

View File

@@ -4,22 +4,19 @@
use eyre::Result;
use reth_chainspec::ChainSpec;
use reth_consensus_common::validation::validate_block_pre_execution;
use reth_fs_util as fs;
use reth_network::NetworkManager;
use reth_network_p2p::{
bodies::client::BodiesClient,
headers::client::{HeadersClient, HeadersRequest},
priority::Priority,
};
use reth_primitives::{BlockHashOrNumber, HeadersDirection, SealedBlock, SealedHeader};
use reth_provider::BlockReader;
use reth_rpc_types::engine::{JwtError, JwtSecret};
use std::{
env::VarError,
path::{Path, PathBuf},
sync::Arc,
};
use tracing::{debug, info, trace, warn};
use tracing::{debug, info};
/// Parses a user-specified path with support for environment variables and common shorthands (e.g.
/// ~ for the user's home directory).
@@ -38,29 +35,6 @@ pub fn get_or_create_jwt_secret_from_path(path: &Path) -> Result<JwtSecret, JwtE
}
}
/// Collect the peers from the [`NetworkManager`] and write them to the given
/// `persistent_peers_file`, if configured.
pub fn write_peers_to_file<C>(network: &NetworkManager<C>, persistent_peers_file: Option<PathBuf>)
where
C: BlockReader + Unpin,
{
if let Some(file_path) = persistent_peers_file {
let known_peers = network.all_peers().collect::<Vec<_>>();
if let Ok(known_peers) = serde_json::to_string_pretty(&known_peers) {
trace!(target: "reth::cli", peers_file =?file_path, num_peers=%known_peers.len(), "Saving current peers");
let parent_dir = file_path.parent().map(fs::create_dir_all).transpose();
match parent_dir.and_then(|_| fs::write(&file_path, known_peers)) {
Ok(_) => {
info!(target: "reth::cli", peers_file=?file_path, "Wrote network peers to file");
}
Err(err) => {
warn!(target: "reth::cli", %err, peers_file=?file_path, "Failed to write network peers to file");
}
}
}
}
}
/// Get a single header from network
pub async fn get_single_header<Client>(
client: Client,