Skip to content

Commit

Permalink
chore(l1): rename net crate to p2p. (#1882)
Browse files Browse the repository at this point in the history
**Motivation**
`net` was a legacy name.
  • Loading branch information
mpaulucci authored Feb 7, 2025
1 parent a1e9c17 commit 2e66c11
Show file tree
Hide file tree
Showing 22 changed files with 64 additions and 60 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
2 changes: 1 addition & 1 deletion cmd/ethrex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/ethrex/cli.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
10 changes: 5 additions & 5 deletions cmd/ethrex/ethrex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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,
Expand All @@ -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()));
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/ethrex/networks.rs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down
4 changes: 2 additions & 2 deletions crates/networking/p2p/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "ethrex-net"
name = "ethrex-p2p"
version = "0.1.0"
edition = "2021"

Expand Down Expand Up @@ -40,4 +40,4 @@ rand = "0.8.5"
hex-literal = "0.4.1"

[lib]
path = "./net.rs"
path = "./p2p.rs"
3 changes: 1 addition & 2 deletions crates/networking/p2p/discv4/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 7 additions & 7 deletions crates/networking/p2p/discv4/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<Self, DiscoveryError> {
let udp_socket = UdpSocket::bind(ctx.local_node.udp_addr())
.await
Expand Down Expand Up @@ -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>(
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions crates/networking/p2p/kademlia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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::{
Expand Down
38 changes: 16 additions & 22 deletions crates/networking/p2p/net.rs → crates/networking/p2p/network.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,31 @@
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},
sync::Mutex,
};
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<Mutex<KademliaTable>> {
let local_node_id = node_id_from_signing_key(&signer);
Expand All @@ -44,14 +38,14 @@ pub enum NetworkError {
}

#[derive(Clone, Debug)]
struct P2PContext {
tracker: TaskTracker,
signer: SigningKey,
table: Arc<Mutex<KademliaTable>>,
storage: Store,
broadcast: RLPxConnBroadcastSender,
local_node: Node,
enr_seq: u64,
pub struct P2PContext {
pub tracker: TaskTracker,
pub signer: SigningKey,
pub table: Arc<Mutex<KademliaTable>>,
pub storage: Store,
pub(crate) broadcast: RLPxConnBroadcastSender,
pub local_node: Node,
pub enr_seq: u64,
}

pub async fn start_network(
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions crates/networking/p2p/p2p.rs
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 2 additions & 2 deletions crates/networking/p2p/peer_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@ use ethrex_trie::{verify_range, Node};
use tokio::sync::Mutex;

use crate::{
kademlia::PeerChannels,
kademlia::{KademliaTable, PeerChannels},
rlpx::{
eth::{
blocks::{
BlockBodies, BlockHeaders, GetBlockBodies, GetBlockHeaders, BLOCK_HEADER_LIMIT,
},
receipts::{GetReceipts, Receipts},
},
message::Message as RLPxMessage,
p2p::Capability,
snap::{
AccountRange, ByteCodes, GetAccountRange, GetByteCodes, GetStorageRanges, GetTrieNodes,
StorageRanges, TrieNodes,
},
},
snap::encodable_to_proof,
KademliaTable, RLPxMessage,
};
use tracing::info;
pub const PEER_REPLY_TIMOUT: Duration = Duration::from_secs(45);
Expand Down
2 changes: 1 addition & 1 deletion crates/networking/p2p/rlpx/handshake.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::net::SocketAddr;

use crate::{
network::P2PContext,
rlpx::{
connection::{LocalState, RLPxConnection, RemoteState},
error::RLPxError,
frame::RLPxCodec,
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};
Expand Down
2 changes: 1 addition & 1 deletion crates/networking/p2p/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion crates/networking/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/networking/rpc/admin/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion crates/networking/rpc/eth/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down
2 changes: 1 addition & 1 deletion crates/networking/rpc/eth/gas_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion crates/networking/rpc/eth/max_priority_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions crates/networking/rpc/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion crates/networking/rpc/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down

0 comments on commit 2e66c11

Please sign in to comment.