From 2e66c11c7e39261b4c66f125bb5f6f2d4ce3cb43 Mon Sep 17 00:00:00 2001 From: Martin Paulucci Date: Fri, 7 Feb 2025 12:12:15 +0100 Subject: [PATCH] chore(l1): rename `net` crate to `p2p`. (#1882) **Motivation** `net` was a legacy name. --- Cargo.lock | 6 +-- Cargo.toml | 2 +- cmd/ethrex/Cargo.toml | 2 +- cmd/ethrex/cli.rs | 2 +- cmd/ethrex/ethrex.rs | 10 ++--- cmd/ethrex/networks.rs | 2 +- crates/networking/p2p/Cargo.toml | 4 +- crates/networking/p2p/discv4/lookup.rs | 3 +- crates/networking/p2p/discv4/server.rs | 14 +++---- crates/networking/p2p/kademlia.rs | 6 +-- crates/networking/p2p/{net.rs => network.rs} | 38 ++++++++----------- crates/networking/p2p/p2p.rs | 11 ++++++ crates/networking/p2p/peer_handler.rs | 4 +- crates/networking/p2p/rlpx/handshake.rs | 2 +- crates/networking/p2p/types.rs | 2 +- crates/networking/rpc/Cargo.toml | 2 +- crates/networking/rpc/admin/mod.rs | 2 +- crates/networking/rpc/eth/filter.rs | 2 +- crates/networking/rpc/eth/gas_price.rs | 2 +- crates/networking/rpc/eth/max_priority_fee.rs | 2 +- crates/networking/rpc/rpc.rs | 4 +- crates/networking/rpc/utils.rs | 2 +- 22 files changed, 64 insertions(+), 60 deletions(-) rename crates/networking/p2p/{net.rs => network.rs} (89%) create mode 100644 crates/networking/p2p/p2p.rs diff --git a/Cargo.lock b/Cargo.lock index 1824182b13..75ead9051d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2802,7 +2802,7 @@ dependencies = [ "ethrex-dev", "ethrex-l2", "ethrex-metrics", - "ethrex-net", + "ethrex-p2p", "ethrex-rlp", "ethrex-rpc", "ethrex-storage", @@ -2963,7 +2963,7 @@ dependencies = [ ] [[package]] -name = "ethrex-net" +name = "ethrex-p2p" version = "0.1.0" dependencies = [ "aes", @@ -3041,7 +3041,7 @@ dependencies = [ "bytes", "ethrex-blockchain", "ethrex-core", - "ethrex-net", + "ethrex-p2p", "ethrex-rlp", "ethrex-storage", "ethrex-vm", diff --git a/Cargo.toml b/Cargo.toml index 978787bc49..69abdc3aec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,7 +33,7 @@ edition = "2021" [workspace.dependencies] ethrex-blockchain = { path = "./crates/blockchain" } ethrex-core = { path = "./crates/common" } -ethrex-net = { path = "./crates/networking/p2p" } +ethrex-p2p = { path = "./crates/networking/p2p" } ethrex-rpc = { path = "./crates/networking/rpc" } ethrex-storage = { path = "./crates/storage/store" } ethrex-vm = { path = "./crates/vm" } diff --git a/cmd/ethrex/Cargo.toml b/cmd/ethrex/Cargo.toml index 05f707e54a..3aa1dc0816 100644 --- a/cmd/ethrex/Cargo.toml +++ b/cmd/ethrex/Cargo.toml @@ -9,7 +9,7 @@ edition = "2021" ethrex-blockchain.workspace = true ethrex-rpc.workspace = true ethrex-core.workspace = true -ethrex-net.workspace = true +ethrex-p2p.workspace = true ethrex-storage = { workspace = true, optional = true } ethrex-vm.workspace = true ethrex-rlp.workspace = true diff --git a/cmd/ethrex/cli.rs b/cmd/ethrex/cli.rs index f81c782653..fa1e5ee786 100644 --- a/cmd/ethrex/cli.rs +++ b/cmd/ethrex/cli.rs @@ -1,5 +1,5 @@ use clap::{Arg, ArgAction, Command}; -use ethrex_net::types::Node; +use ethrex_p2p::types::Node; use tracing::Level; pub fn cli() -> Command { diff --git a/cmd/ethrex/ethrex.rs b/cmd/ethrex/ethrex.rs index 4d0c74fc60..b7c29c25a9 100644 --- a/cmd/ethrex/ethrex.rs +++ b/cmd/ethrex/ethrex.rs @@ -2,11 +2,11 @@ use bytes::Bytes; use directories::ProjectDirs; use ethrex_blockchain::{add_block, fork_choice::apply_fork_choice}; use ethrex_core::types::{Block, Genesis}; -use ethrex_net::{ - node_id_from_signing_key, peer_table, +use ethrex_p2p::{ + kademlia::KademliaTable, + network::{node_id_from_signing_key, peer_table}, sync::{SyncManager, SyncMode}, types::{Node, NodeRecord}, - KademliaTable, }; use ethrex_rlp::decode::RLPDecode; use ethrex_storage::{EngineType, Store}; @@ -287,7 +287,7 @@ async fn main() { let block_producer_engine = ethrex_dev::block_producer::start_block_producer(url, authrpc_jwtsecret.into(), head_block_hash, max_tries, 1000, ethrex_core::Address::default()); tracker.spawn(block_producer_engine); } else { - ethrex_net::start_network( + ethrex_p2p::start_network( local_p2p_node, tracker.clone(), bootnodes, @@ -296,7 +296,7 @@ async fn main() { store, ) .await.expect("Network starts"); - tracker.spawn(ethrex_net::periodically_show_peer_stats(peer_table.clone())); + tracker.spawn(ethrex_p2p::periodically_show_peer_stats(peer_table.clone())); } } diff --git a/cmd/ethrex/networks.rs b/cmd/ethrex/networks.rs index f12bdf6450..47fa22f1cd 100644 --- a/cmd/ethrex/networks.rs +++ b/cmd/ethrex/networks.rs @@ -1,4 +1,4 @@ -use ethrex_net::types::Node; +use ethrex_p2p::types::Node; use lazy_static::lazy_static; pub const HOLESKY_GENESIS_PATH: &str = "cmd/ethrex/networks/holesky/genesis.json"; diff --git a/crates/networking/p2p/Cargo.toml b/crates/networking/p2p/Cargo.toml index 7935e1034b..24f326a809 100644 --- a/crates/networking/p2p/Cargo.toml +++ b/crates/networking/p2p/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "ethrex-net" +name = "ethrex-p2p" version = "0.1.0" edition = "2021" @@ -40,4 +40,4 @@ rand = "0.8.5" hex-literal = "0.4.1" [lib] -path = "./net.rs" +path = "./p2p.rs" diff --git a/crates/networking/p2p/discv4/lookup.rs b/crates/networking/p2p/discv4/lookup.rs index c1a62f4418..0a4c4ab873 100644 --- a/crates/networking/p2p/discv4/lookup.rs +++ b/crates/networking/p2p/discv4/lookup.rs @@ -5,9 +5,8 @@ use super::{ }; use crate::{ kademlia::{bucket_number, MAX_NODES_PER_BUCKET}, - node_id_from_signing_key, + network::{node_id_from_signing_key, P2PContext}, types::Node, - P2PContext, }; use ethrex_core::H512; use k256::ecdsa::SigningKey; diff --git a/crates/networking/p2p/discv4/server.rs b/crates/networking/p2p/discv4/server.rs index e6a497789c..952a23e7be 100644 --- a/crates/networking/p2p/discv4/server.rs +++ b/crates/networking/p2p/discv4/server.rs @@ -9,10 +9,9 @@ use super::{ }, }; use crate::{ - handle_peer_as_initiator, - kademlia::MAX_NODES_PER_BUCKET, + kademlia::{KademliaTable, MAX_NODES_PER_BUCKET}, + network::{handle_peer_as_initiator, P2PContext}, types::{Endpoint, Node, NodeRecord}, - KademliaTable, P2PContext, }; use ethrex_core::H256; use k256::ecdsa::{signature::hazmat::PrehashVerifier, Signature, VerifyingKey}; @@ -52,8 +51,8 @@ pub struct Discv4Server { } impl Discv4Server { - /// Initializes a Discv4 UDP socket and creates a new `Discv4Server` instance. - /// Returns an error if the socket binding fails. + /// Initializes a Discv4 UDP socket and creates a new `Discv4Server` instance. + /// Returns an error if the socket binding fails. pub async fn try_new(ctx: P2PContext) -> Result { let udp_socket = UdpSocket::bind(ctx.local_node.udp_addr()) .await @@ -512,7 +511,7 @@ impl Discv4Server { /// Attempts to add a node to the Kademlia table and send a ping if necessary. /// - /// - If the node is **not found** in the table and there is enough space, it will be added, + /// - If the node is **not found** in the table and there is enough space, it will be added, /// and a ping message will be sent to verify connectivity. /// - If the node is **already present**, no action is taken. async fn try_add_peer_and_ping<'a>( @@ -628,7 +627,8 @@ impl Discv4Server { pub(super) mod tests { use super::*; use crate::{ - node_id_from_signing_key, rlpx::message::Message as RLPxMessage, MAX_MESSAGES_TO_BROADCAST, + network::{node_id_from_signing_key, MAX_MESSAGES_TO_BROADCAST}, + rlpx::message::Message as RLPxMessage, }; use ethrex_storage::{EngineType, Store}; use k256::ecdsa::SigningKey; diff --git a/crates/networking/p2p/kademlia.rs b/crates/networking/p2p/kademlia.rs index cd01a430f9..6a8f536d59 100644 --- a/crates/networking/p2p/kademlia.rs +++ b/crates/networking/p2p/kademlia.rs @@ -2,9 +2,8 @@ use std::sync::Arc; use crate::{ discv4::messages::FindNodeRequest, - rlpx::p2p::Capability, + rlpx::{message::Message as RLPxMessage, p2p::Capability}, types::{Node, NodeRecord}, - RLPxMessage, }; use ethrex_core::{H256, H512, U256}; use sha3::{Digest, Keccak256}; @@ -428,8 +427,9 @@ impl PeerChannels { #[cfg(test)] mod tests { + use crate::network::node_id_from_signing_key; + use super::*; - use crate::node_id_from_signing_key; use hex_literal::hex; use k256::{ecdsa::SigningKey, elliptic_curve::rand_core::OsRng}; use std::{ diff --git a/crates/networking/p2p/net.rs b/crates/networking/p2p/network.rs similarity index 89% rename from crates/networking/p2p/net.rs rename to crates/networking/p2p/network.rs index c963ba5268..8a33986c41 100644 --- a/crates/networking/p2p/net.rs +++ b/crates/networking/p2p/network.rs @@ -1,15 +1,18 @@ -use discv4::{ +use crate::discv4::{ helpers::current_unix_time, server::{DiscoveryError, Discv4Server}, }; +use crate::kademlia::KademliaTable; +use crate::rlpx::{ + connection::RLPxConnBroadcastSender, handshake, message::Message as RLPxMessage, +}; +use crate::types::Node; use ethrex_core::H512; use ethrex_storage::Store; use k256::{ ecdsa::SigningKey, elliptic_curve::{sec1::ToEncodedPoint, PublicKey}, }; -pub use kademlia::KademliaTable; -use rlpx::{connection::RLPxConnBroadcastSender, handshake, message::Message as RLPxMessage}; use std::{io, net::SocketAddr, sync::Arc}; use tokio::{ net::{TcpListener, TcpSocket, TcpStream}, @@ -17,21 +20,12 @@ use tokio::{ }; use tokio_util::task::TaskTracker; use tracing::{debug, error, info}; -use types::Node; - -pub(crate) mod discv4; -pub(crate) mod kademlia; -pub mod peer_handler; -pub mod rlpx; -pub(crate) mod snap; -pub mod sync; -pub mod types; // Totally arbitrary limit on how // many messages the connections can queue, // if we miss messages to broadcast, maybe // we should bump this limit. -const MAX_MESSAGES_TO_BROADCAST: usize = 1000; +pub const MAX_MESSAGES_TO_BROADCAST: usize = 1000; pub fn peer_table(signer: SigningKey) -> Arc> { let local_node_id = node_id_from_signing_key(&signer); @@ -44,14 +38,14 @@ pub enum NetworkError { } #[derive(Clone, Debug)] -struct P2PContext { - tracker: TaskTracker, - signer: SigningKey, - table: Arc>, - storage: Store, - broadcast: RLPxConnBroadcastSender, - local_node: Node, - enr_seq: u64, +pub struct P2PContext { + pub tracker: TaskTracker, + pub signer: SigningKey, + pub table: Arc>, + pub storage: Store, + pub(crate) broadcast: RLPxConnBroadcastSender, + pub local_node: Node, + pub enr_seq: u64, } pub async fn start_network( @@ -144,7 +138,7 @@ async fn handle_peer_as_receiver(context: P2PContext, peer_addr: SocketAddr, str } } -async fn handle_peer_as_initiator(context: P2PContext, node: Node) { +pub async fn handle_peer_as_initiator(context: P2PContext, node: Node) { let addr = SocketAddr::new(node.ip, node.tcp_port); let stream = match tcp_stream(addr).await { Ok(result) => result, diff --git a/crates/networking/p2p/p2p.rs b/crates/networking/p2p/p2p.rs new file mode 100644 index 0000000000..97433fe3a7 --- /dev/null +++ b/crates/networking/p2p/p2p.rs @@ -0,0 +1,11 @@ +pub(crate) mod discv4; +pub mod kademlia; +pub mod network; +pub mod peer_handler; +pub mod rlpx; +pub(crate) mod snap; +pub mod sync; +pub mod types; + +pub use network::periodically_show_peer_stats; +pub use network::start_network; diff --git a/crates/networking/p2p/peer_handler.rs b/crates/networking/p2p/peer_handler.rs index b3dca68a95..b1e3dca348 100644 --- a/crates/networking/p2p/peer_handler.rs +++ b/crates/networking/p2p/peer_handler.rs @@ -11,7 +11,7 @@ use ethrex_trie::{verify_range, Node}; use tokio::sync::Mutex; use crate::{ - kademlia::PeerChannels, + kademlia::{KademliaTable, PeerChannels}, rlpx::{ eth::{ blocks::{ @@ -19,6 +19,7 @@ use crate::{ }, receipts::{GetReceipts, Receipts}, }, + message::Message as RLPxMessage, p2p::Capability, snap::{ AccountRange, ByteCodes, GetAccountRange, GetByteCodes, GetStorageRanges, GetTrieNodes, @@ -26,7 +27,6 @@ use crate::{ }, }, snap::encodable_to_proof, - KademliaTable, RLPxMessage, }; use tracing::info; pub const PEER_REPLY_TIMOUT: Duration = Duration::from_secs(45); diff --git a/crates/networking/p2p/rlpx/handshake.rs b/crates/networking/p2p/rlpx/handshake.rs index cdc7f6fbb4..2ae301722f 100644 --- a/crates/networking/p2p/rlpx/handshake.rs +++ b/crates/networking/p2p/rlpx/handshake.rs @@ -1,6 +1,7 @@ use std::net::SocketAddr; use crate::{ + network::P2PContext, rlpx::{ connection::{LocalState, RLPxConnection, RemoteState}, error::RLPxError, @@ -8,7 +9,6 @@ use crate::{ utils::{ecdh_xchng, id2pubkey, kdf, log_peer_debug, pubkey2id, sha256, sha256_hmac}, }, types::Node, - P2PContext, }; use aes::cipher::{KeyIvInit, StreamCipher}; use ethrex_core::{Signature, H128, H256, H512}; diff --git a/crates/networking/p2p/types.rs b/crates/networking/p2p/types.rs index 0a5b2a43b3..f45ae6ebc0 100644 --- a/crates/networking/p2p/types.rs +++ b/crates/networking/p2p/types.rs @@ -387,7 +387,7 @@ impl RLPEncode for Node { #[cfg(test)] mod tests { use crate::{ - node_id_from_signing_key, + network::node_id_from_signing_key, types::{Node, NodeRecord}, }; use ethrex_core::H512; diff --git a/crates/networking/rpc/Cargo.toml b/crates/networking/rpc/Cargo.toml index e4bb30d9dd..3fbee3012a 100644 --- a/crates/networking/rpc/Cargo.toml +++ b/crates/networking/rpc/Cargo.toml @@ -17,7 +17,7 @@ ethrex-core.workspace = true ethrex-storage.workspace = true ethrex-vm.workspace = true ethrex-blockchain.workspace = true -ethrex-net.workspace = true +ethrex-p2p.workspace = true ethrex-rlp.workspace = true hex.workspace = true axum-extra = { version = "0.10.0", features = ["typed-header"] } diff --git a/crates/networking/rpc/admin/mod.rs b/crates/networking/rpc/admin/mod.rs index fe95aca6ae..f2d37b8f0a 100644 --- a/crates/networking/rpc/admin/mod.rs +++ b/crates/networking/rpc/admin/mod.rs @@ -1,5 +1,5 @@ use ethrex_core::types::ChainConfig; -use ethrex_net::types::{Node, NodeRecord}; +use ethrex_p2p::types::{Node, NodeRecord}; use ethrex_storage::Store; use serde::Serialize; use serde_json::Value; diff --git a/crates/networking/rpc/eth/filter.rs b/crates/networking/rpc/eth/filter.rs index 9b5b12e1c8..e686c0dc6a 100644 --- a/crates/networking/rpc/eth/filter.rs +++ b/crates/networking/rpc/eth/filter.rs @@ -268,7 +268,7 @@ mod tests { utils::{test_utils::example_p2p_node, RpcRequest}, }; use ethrex_core::types::Genesis; - use ethrex_net::sync::SyncManager; + use ethrex_p2p::sync::SyncManager; use ethrex_storage::{EngineType, Store}; use serde_json::{json, Value}; diff --git a/crates/networking/rpc/eth/gas_price.rs b/crates/networking/rpc/eth/gas_price.rs index fe54c53c4d..4b89a5bce3 100644 --- a/crates/networking/rpc/eth/gas_price.rs +++ b/crates/networking/rpc/eth/gas_price.rs @@ -61,7 +61,7 @@ mod tests { utils::{parse_json_hex, test_utils::example_p2p_node, RpcRequest}, RpcApiContext, RpcHandler, }; - use ethrex_net::sync::SyncManager; + use ethrex_p2p::sync::SyncManager; use serde_json::json; use std::sync::Arc; use tokio::sync::Mutex; diff --git a/crates/networking/rpc/eth/max_priority_fee.rs b/crates/networking/rpc/eth/max_priority_fee.rs index 9cd5e122c6..07cff452e5 100644 --- a/crates/networking/rpc/eth/max_priority_fee.rs +++ b/crates/networking/rpc/eth/max_priority_fee.rs @@ -44,7 +44,7 @@ mod tests { utils::{parse_json_hex, test_utils::example_p2p_node, RpcRequest}, RpcApiContext, RpcHandler, }; - use ethrex_net::sync::SyncManager; + use ethrex_p2p::sync::SyncManager; use serde_json::{json, Value}; use std::sync::Arc; use tokio::sync::Mutex; diff --git a/crates/networking/rpc/rpc.rs b/crates/networking/rpc/rpc.rs index 0a6ffee924..b0ebcedac5 100644 --- a/crates/networking/rpc/rpc.rs +++ b/crates/networking/rpc/rpc.rs @@ -36,7 +36,7 @@ use eth::{ GetTransactionByHashRequest, GetTransactionReceiptRequest, }, }; -use ethrex_net::{sync::SyncManager, types::NodeRecord}; +use ethrex_p2p::{sync::SyncManager, types::NodeRecord}; use serde_json::Value; use std::{ collections::HashMap, @@ -62,7 +62,7 @@ pub mod utils; mod web3; use axum::extract::State; -use ethrex_net::types::Node; +use ethrex_p2p::types::Node; use ethrex_storage::{error::StoreError, Store}; #[derive(Debug, Clone)] diff --git a/crates/networking/rpc/utils.rs b/crates/networking/rpc/utils.rs index f50e0254fd..e71db2681b 100644 --- a/crates/networking/rpc/utils.rs +++ b/crates/networking/rpc/utils.rs @@ -255,7 +255,7 @@ pub mod test_utils { use std::{net::SocketAddr, str::FromStr}; use ethrex_core::H512; - use ethrex_net::{ + use ethrex_p2p::{ sync::SyncManager, types::{Node, NodeRecord}, };