Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
25 changes: 24 additions & 1 deletion cmd/ethrex/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ use clap::{ArgAction, Parser as ClapParser, Subcommand as ClapSubcommand};
use ethrex_blockchain::{BlockchainOptions, BlockchainType, L2Config, error::ChainError};
use ethrex_common::types::{Block, DEFAULT_BUILDER_GAS_CEIL, Genesis};
use ethrex_p2p::{
discv4::peer_table::TARGET_PEERS, sync::SyncMode, tx_broadcaster::BROADCAST_INTERVAL_MS,
discv4::{
peer_table::TARGET_PEERS,
server::{INITIAL_LOOKUP_INTERVAL, LOOKUP_INTERVAL},
},
sync::SyncMode,
tx_broadcaster::BROADCAST_INTERVAL_MS,
types::Node,
};
use ethrex_rlp::encode::RLPEncode;
Expand Down Expand Up @@ -226,6 +231,22 @@ pub struct Options {
help_heading = "P2P options"
)]
pub tx_broadcasting_time_interval: u64,
#[arg(
long = "p2p.initial-lookup-interval",
default_value_t = INITIAL_LOOKUP_INTERVAL,
value_name = "INITIAL_INTERVAL_MS",
help = "The initial interval between peer lookups, until the number of peers reaches the target number of peers or the number of contacts reaches the target number of contacts.",
help_heading = "P2P options"
)]
pub p2p_initial_lookup_interval: u64,
#[arg(
long = "p2p.lookup-interval",
default_value_t = LOOKUP_INTERVAL,
value_name = "INTERVAL_MS",
help = "The interval between peer lookups once the number of peers reaches the target number of peers or the number of contacts reaches the target number of contacts.",
help_heading = "P2P options"
)]
pub p2p_lookup_interval: u64,
#[arg(
long = "target.peers",
default_value_t = TARGET_PEERS,
Expand Down Expand Up @@ -319,6 +340,8 @@ impl Default for Options {
force: false,
mempool_max_size: Default::default(),
tx_broadcasting_time_interval: Default::default(),
p2p_initial_lookup_interval: Default::default(),
p2p_lookup_interval: Default::default(),
target_peers: Default::default(),
extra_data: get_minimal_client_version(),
gas_limit: DEFAULT_BUILDER_GAS_CEIL,
Expand Down
2 changes: 2 additions & 0 deletions cmd/ethrex/initializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,8 @@ pub async fn init_l1(
get_client_version(),
None,
opts.tx_broadcasting_time_interval,
opts.p2p_initial_lookup_interval,
opts.p2p_lookup_interval,
)
.await
.expect("P2P context could not be created");
Expand Down
2 changes: 2 additions & 0 deletions cmd/ethrex/l2/initializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ pub async fn init_l2(
),
}),
opts.node_opts.tx_broadcasting_time_interval,
opts.node_opts.p2p_initial_lookup_interval,
opts.node_opts.p2p_lookup_interval,
)
.await
.expect("P2P context could not be created");
Expand Down
14 changes: 10 additions & 4 deletions crates/networking/p2p/discv4/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const REVALIDATION_INTERVAL: Duration = Duration::from_secs(12 * 60 * 60); // 12
/// The initial interval between peer lookups, until the number of peers reaches
/// [target_peers](DiscoverySideCarState::target_peers), or the number of
/// contacts reaches [target_contacts](DiscoverySideCarState::target_contacts).
pub const INITIAL_LOOKUP_INTERVAL: Duration = Duration::from_millis(100); // 10 per second
pub const LOOKUP_INTERVAL: Duration = Duration::from_millis(600); // 100 per minute
pub const INITIAL_LOOKUP_INTERVAL: u64 = 100; // 10 per second
pub const LOOKUP_INTERVAL: u64 = 600; // 100 per minute
const CHANGE_FIND_NODE_MESSAGE_INTERVAL: Duration = Duration::from_secs(5);
const PRUNE_INTERVAL: Duration = Duration::from_secs(5);

Expand Down Expand Up @@ -85,6 +85,8 @@ pub struct DiscoveryServer {
/// The last `FindNode` message sent, cached due to message
/// signatures being expensive.
find_node_message: BytesMut,
initial_lookup_interval: Duration,
lookup_interval: Duration,
}

impl DiscoveryServer {
Expand All @@ -94,6 +96,8 @@ impl DiscoveryServer {
udp_socket: Arc<UdpSocket>,
mut peer_table: PeerTable,
bootnodes: Vec<Node>,
initial_lookup_interval: Duration,
lookup_interval: Duration,
) -> Result<(), DiscoveryServerError> {
info!("Starting Discovery Server");

Expand All @@ -106,6 +110,8 @@ impl DiscoveryServer {
udp_socket,
peer_table: peer_table.clone(),
find_node_message: Self::random_message(&signer),
initial_lookup_interval,
lookup_interval,
};

info!(count = bootnodes.len(), "Adding bootnodes");
Expand Down Expand Up @@ -256,10 +262,10 @@ impl DiscoveryServer {

async fn get_lookup_interval(&mut self) -> Duration {
if !self.peer_table.target_reached().await.unwrap_or(false) {
INITIAL_LOOKUP_INTERVAL
self.initial_lookup_interval
} else {
trace!("Reached target number of peers or contacts. Using longer lookup interval.");
LOOKUP_INTERVAL
self.lookup_interval
}
}

Expand Down
10 changes: 10 additions & 0 deletions crates/networking/p2p/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ pub struct P2PContext {
#[cfg(feature = "l2")]
pub based_context: Option<P2PBasedContext>,
pub tx_broadcaster: GenServerHandle<TxBroadcaster>,
// The initial interval between peer lookups, until the number of peers reaches [target_peers](RLPxInitiatorState::target_peers).
Copy link

Copilot AI Nov 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation reference RLPxInitiatorState::target_peers is incorrect. The struct RLPxInitiatorState does not exist in the codebase. This should refer to the actual field or remove the broken documentation link.

Suggested change
// The initial interval between peer lookups, until the number of peers reaches [target_peers](RLPxInitiatorState::target_peers).
// The initial interval between peer lookups, until the number of peers reaches the target number.

Copilot uses AI. Check for mistakes.
pub p2p_initial_lookup_interval: Duration,
// We use the same lookup intervals as Discovery to try to get both process to check at the same rate
pub p2p_lookup_interval: Duration,
}

impl P2PContext {
Expand All @@ -60,6 +64,8 @@ impl P2PContext {
client_version: String,
based_context: Option<P2PBasedContext>,
tx_broadcasting_time_interval: u64,
p2p_initial_lookup_interval: u64,
p2p_lookup_interval: u64,
) -> Result<Self, NetworkError> {
let (channel_broadcast_send_end, _) = tokio::sync::broadcast::channel::<(
tokio::task::Id,
Expand Down Expand Up @@ -91,6 +97,8 @@ impl P2PContext {
#[cfg(feature = "l2")]
based_context,
tx_broadcaster,
p2p_initial_lookup_interval: Duration::from_millis(p2p_initial_lookup_interval),
p2p_lookup_interval: Duration::from_millis(p2p_lookup_interval),
})
}
}
Expand All @@ -116,6 +124,8 @@ pub async fn start_network(context: P2PContext, bootnodes: Vec<Node>) -> Result<
udp_socket.clone(),
context.table.clone(),
bootnodes,
context.p2p_initial_lookup_interval,
context.p2p_lookup_interval,
)
.await
.inspect_err(|e| {
Expand Down
20 changes: 3 additions & 17 deletions crates/networking/p2p/rlpx/initiator.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
use crate::types::Node;
use crate::{
discv4::{
peer_table::PeerTableError,
server::{INITIAL_LOOKUP_INTERVAL, LOOKUP_INTERVAL},
},
metrics::METRICS,
network::P2PContext,
discv4::peer_table::PeerTableError, metrics::METRICS, network::P2PContext,
rlpx::connection::server::PeerConnection,
};
use spawned_concurrency::{
Expand All @@ -24,12 +19,6 @@ pub enum RLPxInitiatorError {
#[derive(Debug, Clone)]
pub struct RLPxInitiator {
context: P2PContext,

/// The initial interval between peer lookups, until the number of peers
/// reaches [target_peers](RLPxInitiatorState::target_peers).
initial_lookup_interval: Duration,
lookup_interval: Duration,

/// The target number of RLPx connections to reach.
target_peers: u64,
}
Expand All @@ -38,9 +27,6 @@ impl RLPxInitiator {
pub fn new(context: P2PContext) -> Self {
Self {
context,
// We use the same lookup intervals as Discovery to try to get both process to check at the same rate
initial_lookup_interval: INITIAL_LOOKUP_INTERVAL,
lookup_interval: LOOKUP_INTERVAL,
target_peers: 50,
}
}
Expand Down Expand Up @@ -69,10 +55,10 @@ impl RLPxInitiator {
let num_peers = self.context.table.peer_count().await.unwrap_or(0) as u64;

if num_peers < self.target_peers {
self.initial_lookup_interval
self.context.p2p_initial_lookup_interval
} else {
debug!("Reached target number of peers. Using longer lookup interval.");
self.lookup_interval
self.context.p2p_lookup_interval
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions crates/networking/rpc/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ use ethrex_common::{
},
};
use ethrex_p2p::{
discv4::peer_table::{PeerTable, TARGET_PEERS},
discv4::{
peer_table::{PeerTable, TARGET_PEERS},
server::{INITIAL_LOOKUP_INTERVAL, LOOKUP_INTERVAL},
},
network::P2PContext,
peer_handler::PeerHandler,
rlpx::initiator::RLPxInitiator,
sync::SyncMode,
sync_manager::SyncManager,
tx_broadcaster::BROADCAST_INTERVAL_MS,
types::{Node, NodeRecord},
};
use ethrex_storage::{EngineType, Store};
Expand Down Expand Up @@ -331,7 +335,9 @@ pub async fn dummy_p2p_context(peer_table: PeerTable) -> P2PContext {
Arc::new(Blockchain::default_with_store(storage)),
"".to_string(),
None,
1000,
BROADCAST_INTERVAL_MS,
INITIAL_LOOKUP_INTERVAL,
LOOKUP_INTERVAL,
)
.await
.unwrap()
Expand Down
12 changes: 11 additions & 1 deletion docs/CLI.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ P2P options:

[default: 1000]

--p2p.initial-lookup-interval <INITIAL_INTERVAL_MS>
The initial interval between peer lookups, until the number of peers reaches the target number of peers or the number of contacts reaches the target number of contacts.

[default: 100]

--p2p.lookup-interval <INTERVAL_MS>
The interval between peer lookups once the number of peers reaches the target number of peers or the number of contacts reaches the target number of contacts.

[default: 600]

--target.peers <MAX_PEERS>
Max amount of connected peers.

Expand Down Expand Up @@ -147,7 +157,7 @@ Block building options:
--builder.extra-data <EXTRA_DATA>
Block extra data message.

[default: "ethrex 5.0.0"]
[default: "ethrex 6.0.0"]

--builder.gas-limit <GAS_LIMIT>
Target block gas limit.
Expand Down
Loading