Skip to content

refactor(l1): abstract request logic into PeerHandler #1845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 31, 2025
Merged
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
33 changes: 32 additions & 1 deletion crates/networking/p2p/kademlia.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use std::sync::Arc;

use crate::{
discv4::messages::FindNodeRequest,
peer_channels::PeerChannels,
rlpx::p2p::Capability,
types::{Node, NodeRecord},
RLPxMessage,
};
use ethrex_core::{H256, H512, U256};
use sha3::{Digest, Keccak256};
use tokio::sync::mpsc::UnboundedSender;
use tokio::sync::{mpsc, Mutex};
use tracing::{debug, info};

pub const MAX_NODES_PER_BUCKET: usize = 16;
@@ -395,6 +398,34 @@ impl PeerData {
}
}

pub const MAX_MESSAGES_IN_PEER_CHANNEL: usize = 25;

#[derive(Debug, Clone)]
/// Holds the respective sender and receiver ends of the communication channels bewteen the peer data and its active connection
pub struct PeerChannels {
pub(crate) sender: mpsc::Sender<RLPxMessage>,
pub(crate) receiver: Arc<Mutex<mpsc::Receiver<RLPxMessage>>>,
}

impl PeerChannels {
/// Sets up the communication channels for the peer
/// Returns the channel endpoints to send to the active connection's listen loop
pub(crate) fn create() -> (Self, mpsc::Sender<RLPxMessage>, mpsc::Receiver<RLPxMessage>) {
let (sender, connection_receiver) =
mpsc::channel::<RLPxMessage>(MAX_MESSAGES_IN_PEER_CHANNEL);
let (connection_sender, receiver) =
mpsc::channel::<RLPxMessage>(MAX_MESSAGES_IN_PEER_CHANNEL);
(
Self {
sender,
receiver: Arc::new(Mutex::new(receiver)),
},
connection_sender,
connection_receiver,
)
}
}

#[cfg(test)]
mod tests {
use super::*;
2 changes: 1 addition & 1 deletion crates/networking/p2p/net.rs
Original file line number Diff line number Diff line change
@@ -23,7 +23,7 @@ use types::Node;
pub mod bootnode;
pub(crate) mod discv4;
pub(crate) mod kademlia;
pub mod peer_channels;
pub mod peer_handler;
pub mod rlpx;
pub(crate) mod snap;
pub mod sync;
514 changes: 0 additions & 514 deletions crates/networking/p2p/peer_channels.rs

This file was deleted.

604 changes: 604 additions & 0 deletions crates/networking/p2p/peer_handler.rs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion crates/networking/p2p/rlpx/connection.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
peer_channels::PeerChannels,
kademlia::PeerChannels,
rlpx::{
error::RLPxError,
eth::{
401 changes: 176 additions & 225 deletions crates/networking/p2p/sync.rs

Large diffs are not rendered by default.