From 49132a8d1af13646c94ecb45c61644468800443c Mon Sep 17 00:00:00 2001 From: David Kulman <77690992+Kuly14@users.noreply.github.com> Date: Thu, 12 Jan 2023 17:39:23 +0100 Subject: [PATCH] feat: Add neighbours_max_ipv4 test (#836) --- crates/net/discv4/src/lib.rs | 1 + crates/net/discv4/src/proto.rs | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/crates/net/discv4/src/lib.rs b/crates/net/discv4/src/lib.rs index 3fb4f700f..c6dd2979b 100644 --- a/crates/net/discv4/src/lib.rs +++ b/crates/net/discv4/src/lib.rs @@ -93,6 +93,7 @@ const MAX_NODES_PING: usize = 2 * MAX_NODES_PER_BUCKET; /// fit in one datagram. The safe number of nodes that always fit in a datagram is 12, with worst /// case all of them being IPv6 nodes. This is calculated by `(MAX_PACKET_SIZE - (header + expire + /// rlp overhead) / size(rlp(Node_IPv6))` +/// Even in the best case where all nodes are IPv4, only 14 nodes fit into one packet. const SAFE_MAX_DATAGRAM_NEIGHBOUR_RECORDS: usize = (MAX_PACKET_SIZE - 109) / 91; /// The timeout used to identify expired nodes, 24h diff --git a/crates/net/discv4/src/proto.rs b/crates/net/discv4/src/proto.rs index e99909341..935174341 100644 --- a/crates/net/discv4/src/proto.rs +++ b/crates/net/discv4/src/proto.rs @@ -563,6 +563,20 @@ mod tests { } } + #[test] + fn neighbours_max_ipv4() { + let mut rng = thread_rng(); + let msg = Message::Neighbours(Neighbours { + nodes: std::iter::repeat_with(|| rng_ipv4_record(&mut rng)).take(16).collect(), + expire: rng.gen(), + }); + let (secret_key, _) = SECP256K1.generate_keypair(&mut rng); + + let (encoded, _) = msg.encode(&secret_key); + // Assret that 16 nodes never fit into one packet + assert!(encoded.len() > MAX_PACKET_SIZE, "{} {msg:?}", encoded.len()); + } + #[test] fn neighbours_max_nodes() { let mut rng = thread_rng();