Skip to content

Commit 46fb65c

Browse files
jtragliaAgeManning
andauthored
Update max responses check for nodes response (#143)
* Update max responses check for NODES response * Update comment * Slight modification * fmt Co-authored-by: Age Manning <[email protected]>
1 parent 517eb3f commit 46fb65c

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

src/service.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use crate::{
2222
handler::{Handler, HandlerIn, HandlerOut},
2323
kbucket::{
2424
self, ConnectionDirection, ConnectionState, FailureReason, InsertResult, KBucketsTable,
25-
NodeStatus, UpdateResult,
25+
NodeStatus, UpdateResult, MAX_NODES_PER_BUCKET,
2626
},
2727
node_info::{NodeAddress, NodeContact, NonContactable},
2828
packet::MAX_PACKET_SIZE,
@@ -50,6 +50,13 @@ mod test;
5050
/// NOTE: This must not be larger than 127.
5151
pub(crate) const DISTANCES_TO_REQUEST_PER_PEER: usize = 3;
5252

53+
/// Currently, a maximum of `DISTANCES_TO_REQUEST_PER_PEER * BUCKET_SIZE` peers
54+
/// can be returned. Datagrams have a max size of 1280 and ENR's have a max size
55+
/// of 300 bytes. Bucket sizes should be 16. Therefore, to return all required peers
56+
/// there should be no more than `5 * DISTANCES_TO_REQUEST_PER_PEER` responses.
57+
pub(crate) const MAX_NODES_RESPONSES: usize =
58+
(MAX_NODES_PER_BUCKET / 4 + 1) * DISTANCES_TO_REQUEST_PER_PEER;
59+
5360
/// Request type for Protocols using `TalkReq` message.
5461
///
5562
/// Automatically responds with an empty body on drop if
@@ -637,14 +644,10 @@ impl Service {
637644

638645
match response.body {
639646
ResponseBody::Nodes { total, mut nodes } => {
640-
// Currently a maximum of DISTANCES_TO_REQUEST_PER_PEER*BUCKET_SIZE peers can be returned. Datagrams have a max
641-
// size of 1280 and ENR's have a max size of 300 bytes.
642-
//
643-
// Bucket sizes should be 16. In this case, there should be no more than 5*DISTANCES_TO_REQUEST_PER_PEER responses, to return all required peers.
644-
if total > 5 * DISTANCES_TO_REQUEST_PER_PEER as u64 {
647+
if total > MAX_NODES_RESPONSES as u64 {
645648
warn!(
646649
"NodesResponse has a total larger than {}, nodes will be truncated",
647-
DISTANCES_TO_REQUEST_PER_PEER * 5
650+
MAX_NODES_RESPONSES
648651
);
649652
}
650653

@@ -727,12 +730,13 @@ impl Service {
727730
"Nodes Response: {} of {} received",
728731
current_response.count, total
729732
);
730-
// if there are more requests coming, store the nodes and wait for
733+
// If there are more requests coming, store the nodes and wait for
731734
// another response
732-
// We allow for implementations to send at a minimum 3 nodes per response.
733-
// We allow for the number of nodes to be returned as the maximum we emit.
734-
if current_response.count < self.config.max_nodes_response / 3 + 1
735+
// If we have already received all our required nodes, drop any extra
736+
// rpc messages.
737+
if current_response.received_nodes.len() < self.config.max_nodes_response
735738
&& (current_response.count as u64) < total
739+
&& current_response.count < MAX_NODES_RESPONSES
736740
{
737741
current_response.count += 1;
738742

0 commit comments

Comments
 (0)