Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/net/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ workspace = true
[dependencies]
# reth
reth-chainspec.workspace = true
reth-fs-util.workspace = true
reth-primitives.workspace = true
reth-net-banlist.workspace = true
reth-network-api.workspace = true
Expand Down Expand Up @@ -55,6 +56,7 @@ metrics.workspace = true
# misc
auto_impl.workspace = true
aquamarine.workspace = true
eyre.workspace = true
Comment thread
mattsse marked this conversation as resolved.
Outdated
tracing.workspace = true
rustc-hash.workspace = true
thiserror.workspace = true
Expand Down
19 changes: 19 additions & 0 deletions crates/net/network/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use reth_eth_wire::{
capability::{Capabilities, CapabilityMessage},
DisconnectReason, EthVersion, Status,
};
use reth_fs_util::{self as fs, FsPathError};
use reth_metrics::common::mpsc::UnboundedMeteredSender;
use reth_network_api::{EthProtocolInfo, NetworkStatus, PeerInfo, ReputationChangeKind};
use reth_network_peers::{NodeRecord, PeerId};
Expand All @@ -51,6 +52,7 @@ use reth_tokio_util::EventSender;
use secp256k1::SecretKey;
use std::{
net::SocketAddr,
path::Path,
pin::Pin,
sync::{
atomic::{AtomicU64, AtomicUsize, Ordering},
Expand Down Expand Up @@ -336,13 +338,30 @@ where
self.swarm.state().peers().iter_peers()
}

/// Returns the number of peers in the peer set.
pub fn num_known_peers(&self) -> usize {
self.swarm.state().peers().num_known_peers()
}

/// Returns a new [`PeersHandle`] that can be cloned and shared.
///
/// The [`PeersHandle`] can be used to interact with the network's peer set.
pub fn peers_handle(&self) -> PeersHandle {
self.swarm.state().peers().handle()
}

/// Collect the peers from the [`NetworkManager`] and write them to the given
/// `persistent_peers_file`.
pub fn write_peers_to_file(&self, persistent_peers_file: &Path) -> Result<(), FsPathError> {
let known_peers = self.all_peers().collect::<Vec<_>>();
let known_peers = serde_json::to_string_pretty(&known_peers).map_err(|e| {
FsPathError::WriteJson { source: e, path: persistent_peers_file.to_path_buf() }
})?;
persistent_peers_file.parent().map(fs::create_dir_all).transpose()?;
fs::write(persistent_peers_file, known_peers)?;
Ok(())
}

/// Returns a new [`FetchClient`] that can be cloned and shared.
///
/// The [`FetchClient`] is the entrypoint for sending requests to the network.
Expand Down
3 changes: 3 additions & 0 deletions crates/node/builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,8 @@ confy.workspace = true
rayon.workspace = true
backon.workspace = true

# tracing
tracing.workspace = true

[dev-dependencies]
tempfile.workspace = true
15 changes: 13 additions & 2 deletions crates/node/builder/src/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ use reth_node_core::{
dirs::{ChainPath, DataDirPath, MaybePlatformPath},
node_config::NodeConfig,
primitives::Head,
utils::write_peers_to_file,
};
use reth_primitives::revm_primitives::EnvKzgSettings;
use reth_provider::{providers::BlockchainProvider, ChainSpecProvider};
Expand All @@ -39,6 +38,7 @@ use reth_transaction_pool::{PoolConfig, TransactionPool};
use secp256k1::SecretKey;
pub use states::*;
use std::sync::Arc;
use tracing::{info, trace, warn};

mod states;

Expand Down Expand Up @@ -509,7 +509,18 @@ impl<Node: FullNodeTypes> BuilderContext<Node> {
"p2p network task",
|shutdown| {
network.run_until_graceful_shutdown(shutdown, |network| {
write_peers_to_file(&network, known_peers_file)
if let Some(peers_file) = known_peers_file {
let num_known_peers = network.num_known_peers();
trace!(target: "reth::cli", peers_file=?peers_file, num_peers=%num_known_peers, "Saving current peers");
match network.write_peers_to_file(peers_file.as_path()) {
Ok(_) => {
info!(target: "reth::cli", peers_file=?peers_file, "Wrote network peers to file");
}
Err(err) => {
warn!(target: "reth::cli", %err, "Failed to write network peers to file");
}
}
}
})
},
);
Expand Down
28 changes: 1 addition & 27 deletions crates/node/core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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,
Expand Down