Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

devp2p: Move UDP socket handling from Discovery to Host. #8790

Merged
merged 6 commits into from
Jun 8, 2018

Conversation

jimpo
Copy link
Contributor

@jimpo jimpo commented Jun 5, 2018

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.

@parity-cla-bot
Copy link

It looks like @jimpo signed our Contributor License Agreement. 👍

Many thanks,

Parity Technologies CLA Bot

@5chdn 5chdn added A0-pleasereview 🤓 Pull request needs code review. M4-core ⛓ Core client code / Rust. labels Jun 5, 2018
@5chdn 5chdn added this to the 1.12 milestone Jun 5, 2018
Copy link
Contributor

@tomaka tomaka left a 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.

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() };
Copy link
Contributor

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.

discovery_round: u16,
discovery_id: NodeId,
discovery_nodes: HashSet<NodeId>,
node_buckets: Vec<NodeBucket>,
send_queue: VecDeque<Datagramm>,
pub send_queue: VecDeque<Datagramm>,
Copy link
Contributor

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 tomaka added the A5-grumble 🔥 Pull request has minor issues that must be addressed before merging. label Jun 5, 2018
@debris debris removed the A0-pleasereview 🤓 Pull request needs code review. label Jun 5, 2018
@jimpo
Copy link
Contributor Author

jimpo commented Jun 6, 2018

@tomaka Thanks for the review, addressed comments.

@5chdn 5chdn added A0-pleasereview 🤓 Pull request needs code review. and removed A5-grumble 🔥 Pull request has minor issues that must be addressed before merging. labels Jun 7, 2018
@tomaka tomaka added A8-looksgood 🦄 Pull request is reviewed well. and removed A0-pleasereview 🤓 Pull request needs code review. labels Jun 7, 2018
struct Datagramm {
payload: Bytes,
address: SocketAddr,
pub struct Datagramm {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/Datagramm/Datagram

}

pub fn requeue_send(&mut self, datagramm: Datagramm) {
self.send_queue.push_front(datagramm)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/datagramm/datagram

};
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> {
Copy link
Collaborator

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());
Copy link
Collaborator

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

Copy link
Contributor Author

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.

Copy link
Collaborator

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!

@@ -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() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/datagramm/datagram

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() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/datagramm/datagram

} else { None }
};

if let Some(mut discovery) = discovery {
let mut udp_addr = local_endpoint.address.clone();
Copy link
Collaborator

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

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);
Copy link
Collaborator

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);

}),
Ok(_) => None,
Err(e) => {
debug!("Error reading UPD socket: {:?}", e);
Copy link
Collaborator

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!

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));
Copy link
Collaborator

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!

Ok(Some(size)) if size == data.payload.len() => {
},
Ok(Some(_)) => {
warn!("UDP sent incomplete datagramm");
Copy link
Collaborator

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!

return;
}
Err(e) => {
debug!("UDP send error: {:?}, address: {:?}", e, &data.address);
Copy link
Collaborator

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!

}
}
io.update_registration(DISCOVERY)
.unwrap_or_else(|e| debug!("Error updating discovery registration: {:?}", e));
Copy link
Collaborator

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!

@rphmeier rphmeier added A5-grumble 🔥 Pull request has minor issues that must be addressed before merging. and removed A8-looksgood 🦄 Pull request is reviewed well. labels Jun 7, 2018
This works right now because the Host handler happens to be the first one
registered on the IoService.
jimpo added 5 commits June 7, 2018 11:10
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
Copy link
Collaborator

@niklasad1 niklasad1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, looks good!

@niklasad1 niklasad1 added A8-looksgood 🦄 Pull request is reviewed well. and removed A5-grumble 🔥 Pull request has minor issues that must be addressed before merging. labels Jun 7, 2018
@5chdn 5chdn merged commit 13bc922 into openethereum:master Jun 8, 2018
dvdplm added a commit that referenced this pull request Jun 11, 2018
* 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)
@jimpo jimpo deleted the discovery-io branch June 13, 2018 16:23
@5chdn 5chdn mentioned this pull request Jun 18, 2018
57 tasks
@andresilva andresilva mentioned this pull request Jun 18, 2018
18 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A8-looksgood 🦄 Pull request is reviewed well. M4-core ⛓ Core client code / Rust.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants