Skip to content
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

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

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
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;
Expand Down Expand Up @@ -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::*;
Expand Down
2 changes: 1 addition & 1 deletion crates/networking/p2p/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,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;
Expand Down
Loading