Skip to content

Commit 1c4851c

Browse files
fix(l1): use correct target to get closest neighbors in discv4 (#5595)
** Motivation ** Instead of using sender id to get the distance use `target` provided from the `FindNodesMessage` which is correct spec. Co-authored-by: lakshya-sky <[email protected]> Co-authored-by: Tomás Grüner <[email protected]>
1 parent 78f64f8 commit 1c4851c

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

crates/networking/p2p/discv4/server.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ impl DiscoveryServer {
182182
return Ok(());
183183
}
184184

185-
self.handle_find_node(sender_public_key, from).await?;
185+
self.handle_find_node(sender_public_key, find_node_message.target, from)
186+
.await?;
186187
}
187188
Message::Neighbors(neighbors_message) => {
188189
trace!(received = "Neighbors", msg = ?neighbors_message, from = %format!("{sender_public_key:#x}"));
@@ -425,14 +426,18 @@ impl DiscoveryServer {
425426
async fn handle_find_node(
426427
&mut self,
427428
sender_public_key: H512,
429+
target: H512,
428430
from: SocketAddr,
429431
) -> Result<(), DiscoveryServerError> {
430-
let node_id = node_id(&sender_public_key);
432+
let sender_id = node_id(&sender_public_key);
431433
if let Ok(contact) = self
432-
.validate_contact(sender_public_key, node_id, from, "FindNode")
434+
.validate_contact(sender_public_key, sender_id, from, "FindNode")
433435
.await
434436
{
435-
let neighbors = self.peer_table.get_closest_nodes(&node_id).await?;
437+
// According to https://github.com/ethereum/devp2p/blob/master/discv4.md#findnode-packet-0x03
438+
// reply closest 16 nodes to target
439+
let target_id = node_id(&target);
440+
let neighbors = self.peer_table.get_closest_nodes(&target_id).await?;
436441

437442
// A single node encodes to at most 89B, so 8 of them are at most 712B plus
438443
// recursive length and expiration time, well within bound of 1280B per packet.

0 commit comments

Comments
 (0)