feat: Add neighbours_max_ipv4 test (#836)

This commit is contained in:
David Kulman
2023-01-12 17:39:23 +01:00
committed by GitHub
parent 429cd69ed4
commit 49132a8d1a
2 changed files with 15 additions and 0 deletions

View File

@ -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

View File

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