Skip to content

Commit 772d4b1

Browse files
authored
networking: add discovery support (#254)
* networking: add discovery support * cleanup * cleanup * cleanup
1 parent 566e1ad commit 772d4b1

5 files changed

Lines changed: 1628 additions & 0 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
"""
2+
Discovery v5 Protocol Specification
3+
4+
Node Discovery Protocol v5.1 for finding peers in Ethereum networks.
5+
6+
References:
7+
- https://github.com/ethereum/devp2p/blob/master/discv5/discv5.md
8+
"""
9+
10+
from .config import DiscoveryConfig
11+
from .messages import (
12+
MAX_REQUEST_ID_LENGTH,
13+
PROTOCOL_ID,
14+
PROTOCOL_VERSION,
15+
Distance,
16+
FindNode,
17+
HandshakeAuthdata,
18+
IdNonce,
19+
IPv4,
20+
IPv6,
21+
MessageType,
22+
Nodes,
23+
Nonce,
24+
PacketFlag,
25+
Ping,
26+
Pong,
27+
Port,
28+
RequestId,
29+
StaticHeader,
30+
TalkReq,
31+
TalkResp,
32+
WhoAreYouAuthdata,
33+
)
34+
from .routing import KBucket, NodeEntry, RoutingTable
35+
36+
__all__ = [
37+
"DiscoveryConfig",
38+
"MAX_REQUEST_ID_LENGTH",
39+
"PROTOCOL_ID",
40+
"PROTOCOL_VERSION",
41+
"Distance",
42+
"IdNonce",
43+
"IPv4",
44+
"IPv6",
45+
"Nonce",
46+
"Port",
47+
"RequestId",
48+
"MessageType",
49+
"PacketFlag",
50+
"FindNode",
51+
"Nodes",
52+
"Ping",
53+
"Pong",
54+
"TalkReq",
55+
"TalkResp",
56+
"HandshakeAuthdata",
57+
"StaticHeader",
58+
"WhoAreYouAuthdata",
59+
"KBucket",
60+
"NodeEntry",
61+
"RoutingTable",
62+
]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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

Comments
 (0)