|
| 1 | +""" |
| 2 | +Discovery v5 Configuration |
| 3 | +
|
| 4 | +Protocol constants and configuration for Node Discovery Protocol v5.1. |
| 5 | +
|
| 6 | +References: |
| 7 | + - https://github.com/ethereum/devp2p/blob/master/discv5/discv5-theory.md |
| 8 | +""" |
| 9 | + |
| 10 | +from typing_extensions import Final |
| 11 | + |
| 12 | +from lean_spec.types import StrictBaseModel |
| 13 | + |
| 14 | +# Protocol Constants |
| 15 | +# ------------------ |
| 16 | +# Values derived from the Discovery v5 specification and Kademlia design. |
| 17 | + |
| 18 | +K_BUCKET_SIZE: Final = 16 |
| 19 | +"""Nodes per k-bucket. Standard Kademlia value balancing table size and lookup efficiency.""" |
| 20 | + |
| 21 | +ALPHA: Final = 3 |
| 22 | +"""Concurrent queries during lookup. Balances speed against network load.""" |
| 23 | + |
| 24 | +BUCKET_COUNT: Final = 256 |
| 25 | +"""Total k-buckets. One per bit of the 256-bit node ID space.""" |
| 26 | + |
| 27 | +REQUEST_TIMEOUT_SECS: Final = 0.5 |
| 28 | +"""Single request timeout. Spec recommends 500ms for request/response.""" |
| 29 | + |
| 30 | +HANDSHAKE_TIMEOUT_SECS: Final = 1.0 |
| 31 | +"""Handshake completion timeout. Spec recommends 1s for full handshake.""" |
| 32 | + |
| 33 | +MAX_NODES_RESPONSE: Final = 16 |
| 34 | +"""Max ENRs per NODES message. Keeps responses under 1280 byte UDP limit.""" |
| 35 | + |
| 36 | +BOND_EXPIRY_SECS: Final = 86400 |
| 37 | +"""Liveness revalidation interval. 24 hours before re-checking a node.""" |
| 38 | + |
| 39 | +MAX_PACKET_SIZE: Final = 1280 |
| 40 | +"""Maximum UDP packet size in bytes.""" |
| 41 | + |
| 42 | +MIN_PACKET_SIZE: Final = 63 |
| 43 | +"""Minimum valid packet size in bytes.""" |
| 44 | + |
| 45 | + |
| 46 | +class DiscoveryConfig(StrictBaseModel): |
| 47 | + """Runtime configuration for Discovery v5.""" |
| 48 | + |
| 49 | + k_bucket_size: int = K_BUCKET_SIZE |
| 50 | + """Maximum nodes stored per k-bucket in the routing table.""" |
| 51 | + |
| 52 | + alpha: int = ALPHA |
| 53 | + """Number of concurrent FINDNODE queries during lookup.""" |
| 54 | + |
| 55 | + request_timeout_secs: float = REQUEST_TIMEOUT_SECS |
| 56 | + """Timeout for a single request/response exchange.""" |
| 57 | + |
| 58 | + handshake_timeout_secs: float = HANDSHAKE_TIMEOUT_SECS |
| 59 | + """Timeout for completing the full handshake sequence.""" |
| 60 | + |
| 61 | + max_nodes_response: int = MAX_NODES_RESPONSE |
| 62 | + """Maximum ENR records returned in a single NODES response.""" |
| 63 | + |
| 64 | + bond_expiry_secs: int = BOND_EXPIRY_SECS |
| 65 | + """Seconds before a bonded node requires liveness revalidation.""" |
0 commit comments