@@ -22,7 +22,7 @@ use crate::{
22
22
handler:: { Handler , HandlerIn , HandlerOut } ,
23
23
kbucket:: {
24
24
self , ConnectionDirection , ConnectionState , FailureReason , InsertResult , KBucketsTable ,
25
- NodeStatus , UpdateResult ,
25
+ NodeStatus , UpdateResult , MAX_NODES_PER_BUCKET ,
26
26
} ,
27
27
node_info:: { NodeAddress , NodeContact , NonContactable } ,
28
28
packet:: MAX_PACKET_SIZE ,
@@ -50,6 +50,13 @@ mod test;
50
50
/// NOTE: This must not be larger than 127.
51
51
pub ( crate ) const DISTANCES_TO_REQUEST_PER_PEER : usize = 3 ;
52
52
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
+
53
60
/// Request type for Protocols using `TalkReq` message.
54
61
///
55
62
/// Automatically responds with an empty body on drop if
@@ -637,14 +644,10 @@ impl Service {
637
644
638
645
match response. body {
639
646
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 {
645
648
warn ! (
646
649
"NodesResponse has a total larger than {}, nodes will be truncated" ,
647
- DISTANCES_TO_REQUEST_PER_PEER * 5
650
+ MAX_NODES_RESPONSES
648
651
) ;
649
652
}
650
653
@@ -727,12 +730,13 @@ impl Service {
727
730
"Nodes Response: {} of {} received" ,
728
731
current_response. count, total
729
732
) ;
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
731
734
// 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
735
738
&& ( current_response. count as u64 ) < total
739
+ && current_response. count < MAX_NODES_RESPONSES
736
740
{
737
741
current_response. count += 1 ;
738
742
0 commit comments