Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions libp2p/relay/circuit_v2/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
RelayLimits,
)

# === Constants for relay configuration ===
DEFAULT_MIN_RELAYS = 3
DEFAULT_MAX_RELAYS = 20
DEFAULT_DISCOVERY_INTERVAL = 300 # seconds
DEFAULT_RESERVATION_TTL = 3600 # seconds
DEFAULT_MAX_CIRCUIT_DURATION = 3600 # seconds
DEFAULT_MAX_CIRCUIT_BYTES = 1024 * 1024 * 1024 # 1GB


@dataclass
class RelayConfig:
Expand All @@ -33,14 +41,14 @@ class RelayConfig:

# Discovery configuration
bootstrap_relays: list[PeerInfo] = field(default_factory=list)
min_relays: int = 3
max_relays: int = 20
discovery_interval: int = 300 # seconds
min_relays: int = DEFAULT_MIN_RELAYS
max_relays: int = DEFAULT_MAX_RELAYS
discovery_interval: int = DEFAULT_DISCOVERY_INTERVAL # seconds

# Connection configuration
reservation_ttl: int = 3600 # seconds
max_circuit_duration: int = 3600 # seconds
max_circuit_bytes: int = 1024 * 1024 * 1024 # 1GB
reservation_ttl: int = DEFAULT_RESERVATION_TTL # seconds
max_circuit_duration: int = DEFAULT_MAX_CIRCUIT_DURATION # seconds
max_circuit_bytes: int = DEFAULT_MAX_CIRCUIT_BYTES # 1GB
Comment on lines +49 to +51
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since the explanatory comments are next to where the constants are defined, the can be removed here.


def __post_init__(self) -> None:
"""Initialize default values."""
Expand Down
13 changes: 7 additions & 6 deletions libp2p/relay/circuit_v2/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@

logger = logging.getLogger("libp2p.relay.circuit_v2.discovery")

# Constants
# === Constants for relay discovery ===
MAX_RELAYS_TO_TRACK = 10
DEFAULT_DISCOVERY_INTERVAL = 60 # seconds
STREAM_TIMEOUT = 10 # seconds
PEER_PROTOCOL_TIMEOUT = 5 # seconds


# Extended interfaces for type checking
Expand Down Expand Up @@ -166,19 +167,19 @@ async def discover_relays(self) -> None:
continue

# Check if peer supports the relay protocol
with trio.move_on_after(5): # Don't wait too long for protocol info
with trio.move_on_after(PEER_PROTOCOL_TIMEOUT): # Don't wait too long for protocol info
if await self._supports_relay_protocol(peer_id):
await self._add_relay(peer_id)

# Limit number of relays we track
if len(self._discovered_relays) > self.max_relays:
if len(self._discovered_relays) > MAX_RELAYS_TO_TRACK:
# Sort by last seen time and keep only the most recent ones
sorted_relays = sorted(
self._discovered_relays.items(),
key=lambda x: x[1].last_seen,
reverse=True,
)
to_remove = sorted_relays[self.max_relays :]
to_remove = sorted_relays[MAX_RELAYS_TO_TRACK :]
for peer_id, _ in to_remove:
del self._discovered_relays[peer_id]

Expand Down Expand Up @@ -298,7 +299,7 @@ async def _check_via_mux(self, peer_id: ID) -> bool | None:

for protocol in available_protocols:
try:
with trio.fail_after(2): # Quick check
with trio.fail_after(PEER_PROTOCOL_TIMEOUT): # Quick check
# Ensure we have a proper protocol object
# Use string representation since we can't use isinstance
is_tprotocol = str(type(protocol)) == str(type(TProtocol))
Expand Down Expand Up @@ -463,7 +464,7 @@ async def _cleanup_expired(self) -> None:

for peer_id, relay_info in self._discovered_relays.items():
# Check if relay hasn't been seen in a while (3x discovery interval)
if now - relay_info.last_seen > self.discovery_interval * 3:
if now - relay_info.last_seen > DEFAULT_DISCOVERY_INTERVAL * 3:
to_remove.append(peer_id)
continue

Expand Down
8 changes: 6 additions & 2 deletions libp2p/relay/circuit_v2/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
# Import the protobuf definitions
from .pb.circuit_pb2 import Reservation as PbReservation

# === Constants for resource management ===
RANDOM_BYTES_LENGTH = 16 # 128 bits of randomness
TIMESTAMP_MULTIPLIER = 1000000 # To convert seconds to microseconds


@dataclass
class RelayLimits:
Expand Down Expand Up @@ -68,8 +72,8 @@ def _generate_voucher(self) -> bytes:
# - Peer ID to bind it to the specific peer
# - Timestamp for uniqueness
# - Hash everything for a fixed size output
random_bytes = os.urandom(16) # 128 bits of randomness
timestamp = str(int(self.created_at * 1000000)).encode()
random_bytes = os.urandom(RANDOM_BYTES_LENGTH) # 128 bits of randomness
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't need inline comment here and where defined.

timestamp = str(int(self.created_at * TIMESTAMP_MULTIPLIER)).encode()
peer_bytes = self.peer_id.to_bytes()

# Combine all elements and hash them
Expand Down