-
Notifications
You must be signed in to change notification settings - Fork 1.7k
devp2p: Move UDP socket handling from Discovery to Host. #8790
Conversation
It looks like @jimpo signed our Contributor License Agreement. 👍 Many thanks, Parity Technologies CLA Bot |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is indeed better this way.
util/network-devp2p/src/host.rs
Outdated
fn discovery_readable(&self, io: &IoContext<NetworkIoMessage>) { | ||
let node_changes = match (self.udp_socket.lock().as_ref(), self.discovery.lock().as_mut()) { | ||
(Some(udp_socket), Some(discovery)) => { | ||
let mut buf: [u8; MAX_DATAGRAM_SIZE] = unsafe { mem::uninitialized() }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please initialize this to zero instead of using unsafe
.
util/network-devp2p/src/discovery.rs
Outdated
discovery_round: u16, | ||
discovery_id: NodeId, | ||
discovery_nodes: HashSet<NodeId>, | ||
node_buckets: Vec<NodeBucket>, | ||
send_queue: VecDeque<Datagramm>, | ||
pub send_queue: VecDeque<Datagramm>, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would be in favour of adding methods to Discovery
to pop from the send queue instead of making this field public, but I don't have a strong opinion on this.
@tomaka Thanks for the review, addressed comments. |
util/network-devp2p/src/discovery.rs
Outdated
struct Datagramm { | ||
payload: Bytes, | ||
address: SocketAddr, | ||
pub struct Datagramm { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/Datagramm/Datagram
util/network-devp2p/src/discovery.rs
Outdated
} | ||
|
||
pub fn requeue_send(&mut self, datagramm: Datagramm) { | ||
self.send_queue.push_front(datagramm) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/datagramm/datagram
util/network-devp2p/src/discovery.rs
Outdated
}; | ||
event_loop.reregister(&self.udp_socket, Token(self.token), registration, PollOpt::edge()).expect("Error reregistering UDP socket"); | ||
Ok(()) | ||
pub fn dequeue_send(&mut self) -> Option<Datagramm> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/Datagramm/Datagram
@@ -620,8 +566,8 @@ mod tests { | |||
let key2 = Random.generate().unwrap(); | |||
let ep1 = NodeEndpoint { address: SocketAddr::from_str("127.0.0.1:40444").unwrap(), udp_port: 40444 }; | |||
let ep2 = NodeEndpoint { address: SocketAddr::from_str("127.0.0.1:40445").unwrap(), udp_port: 40445 }; | |||
let mut discovery1 = Discovery::new(&key1, ep1.address.clone(), ep1.clone(), 0, IpFilter::default()); | |||
let mut discovery2 = Discovery::new(&key2, ep2.address.clone(), ep2.clone(), 0, IpFilter::default()); | |||
let mut discovery1 = Discovery::new(&key1, ep1.clone(), IpFilter::default()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ep: NodeEntrypoint
is a copy type so no need to explictly call clone
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NodeEntrypoint is not Copy, just Clone.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I got it wrong sorry!
util/network-devp2p/src/discovery.rs
Outdated
@@ -632,14 +578,12 @@ mod tests { | |||
discovery2.refresh(); | |||
|
|||
for _ in 0 .. 10 { | |||
while !discovery1.send_queue.is_empty() { | |||
let datagramm = discovery1.send_queue.pop_front().unwrap(); | |||
while let Some(datagramm) = discovery1.dequeue_send() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/datagramm/datagram
util/network-devp2p/src/discovery.rs
Outdated
if datagramm.address == ep2.address { | ||
discovery2.on_packet(&datagramm.payload, ep1.address.clone()).ok(); | ||
} | ||
} | ||
while !discovery2.send_queue.is_empty() { | ||
let datagramm = discovery2.send_queue.pop_front().unwrap(); | ||
while let Some(datagramm) = discovery2.dequeue_send() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/datagramm/datagram
util/network-devp2p/src/host.rs
Outdated
} else { None } | ||
}; | ||
|
||
if let Some(mut discovery) = discovery { | ||
let mut udp_addr = local_endpoint.address.clone(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for explicit clone
for Copy type
util/network-devp2p/src/host.rs
Outdated
let writable = discovery.any_sends_queued(); | ||
let res = match udp_socket.recv_from(&mut buf) { | ||
Ok(Some((len, address))) => discovery.on_packet(&buf[0..len], address).unwrap_or_else(|e| { | ||
debug!("Error processing UDP packet: {:?}", e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add debug target
such as debug!(target: "network", "Error processing UDP packet: {:?}", listen_address);
util/network-devp2p/src/host.rs
Outdated
}), | ||
Ok(_) => None, | ||
Err(e) => { | ||
debug!("Error reading UPD socket: {:?}", e); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add target, see previous comment!
util/network-devp2p/src/host.rs
Outdated
let new_writable = discovery.any_sends_queued(); | ||
if writable != new_writable { | ||
io.update_registration(DISCOVERY) | ||
.unwrap_or_else(|e| debug!("Error updating discovery registration: {:?}", e)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add target, see previous comment!
util/network-devp2p/src/host.rs
Outdated
Ok(Some(size)) if size == data.payload.len() => { | ||
}, | ||
Ok(Some(_)) => { | ||
warn!("UDP sent incomplete datagramm"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add target, see previous comment!
util/network-devp2p/src/host.rs
Outdated
return; | ||
} | ||
Err(e) => { | ||
debug!("UDP send error: {:?}, address: {:?}", e, &data.address); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add target, see previous comment!
util/network-devp2p/src/host.rs
Outdated
} | ||
} | ||
io.update_registration(DISCOVERY) | ||
.unwrap_or_else(|e| debug!("Error updating discovery registration: {:?}", e)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add target, see previous comment!
This works right now because the Host handler happens to be the first one registered on the IoService.
sed -i 's/Datagramm/Datagram/g' util/network-devp2p/src/discovery.rs util/network-devp2p/src/host.rs sed -i 's/datagramm/datagram/g' util/network-devp2p/src/discovery.rs util/network-devp2p/src/host.rs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, looks good!
* master: Fix subcrate test compile (#8862) network-devp2p: downgrade logging to debug, add target (#8784) Clearing up a comment about the prefix for signing (#8828) Disable parallel verification and skip verifiying already imported txs. (#8834) devp2p: Move UDP socket handling from Discovery to Host. (#8790) Fixed AuthorityRound deadlock on shutdown, closes #8088 (#8803) Specify critical release flag per network (#8821) Fix `deadlock_detection` feature branch compilation (#8824) Use system allocator when profiling memory (#8831) added from and to to Receipt (#8756)
I think moving the UDP IO handling into Host is a better separation, because Host already manages all of the other IO. This gives Discovery a concrete interface in line with how the tests interact with Discovery. Also, it is annoying that Discovery binds to a port in tests, which can cause conflicts.
Even if people decide against this approach, the first commit should get considered, as it is a bug fix.