9
9
dataclass ,
10
10
field ,
11
11
)
12
+ from enum import Flag , auto
12
13
13
14
from libp2p .peer .peerinfo import (
14
15
PeerInfo ,
18
19
RelayLimits ,
19
20
)
20
21
22
+ DEFAULT_MIN_RELAYS = 3
23
+ DEFAULT_MAX_RELAYS = 20
24
+ DEFAULT_DISCOVERY_INTERVAL = 300 # seconds
25
+ DEFAULT_RESERVATION_TTL = 3600 # seconds
26
+ DEFAULT_MAX_CIRCUIT_DURATION = 3600 # seconds
27
+ DEFAULT_MAX_CIRCUIT_BYTES = 1024 * 1024 * 1024 # 1GB
28
+
29
+ DEFAULT_MAX_CIRCUIT_CONNS = 8
30
+ DEFAULT_MAX_RESERVATIONS = 4
31
+
32
+ MAX_RESERVATIONS_PER_IP = 8
33
+ MAX_CIRCUITS_PER_IP = 16
34
+ RESERVATION_RATE_PER_IP = 4 # per minute
35
+ CIRCUIT_RATE_PER_IP = 8 # per minute
36
+ MAX_CIRCUITS_TOTAL = 64
37
+ MAX_RESERVATIONS_TOTAL = 32
38
+ MAX_BANDWIDTH_PER_CIRCUIT = 1024 * 1024 # 1MB/s
39
+ MAX_BANDWIDTH_TOTAL = 10 * 1024 * 1024 # 10MB/s
40
+
41
+ MIN_RELAY_SCORE = 0.5
42
+ MAX_RELAY_LATENCY = 1.0 # seconds
43
+ ENABLE_AUTO_RELAY = True
44
+ AUTO_RELAY_TIMEOUT = 30 # seconds
45
+ MAX_AUTO_RELAY_ATTEMPTS = 3
46
+ RESERVATION_REFRESH_THRESHOLD = 0.8 # Refresh at 80% of TTL
47
+ MAX_CONCURRENT_RESERVATIONS = 2
48
+
49
+ # Timeout constants for different components
50
+ DEFAULT_DISCOVERY_STREAM_TIMEOUT = 10 # seconds
51
+ DEFAULT_PEER_PROTOCOL_TIMEOUT = 5 # seconds
52
+ DEFAULT_PROTOCOL_READ_TIMEOUT = 15 # seconds
53
+ DEFAULT_PROTOCOL_WRITE_TIMEOUT = 15 # seconds
54
+ DEFAULT_PROTOCOL_CLOSE_TIMEOUT = 10 # seconds
55
+ DEFAULT_DCUTR_READ_TIMEOUT = 30 # seconds
56
+ DEFAULT_DCUTR_WRITE_TIMEOUT = 30 # seconds
57
+ DEFAULT_DIAL_TIMEOUT = 10 # seconds
58
+
59
+
60
+ @dataclass
61
+ class TimeoutConfig :
62
+ """Timeout configuration for different Circuit Relay v2 components."""
63
+
64
+ # Discovery timeouts
65
+ discovery_stream_timeout : int = DEFAULT_DISCOVERY_STREAM_TIMEOUT
66
+ peer_protocol_timeout : int = DEFAULT_PEER_PROTOCOL_TIMEOUT
67
+
68
+ # Core protocol timeouts
69
+ protocol_read_timeout : int = DEFAULT_PROTOCOL_READ_TIMEOUT
70
+ protocol_write_timeout : int = DEFAULT_PROTOCOL_WRITE_TIMEOUT
71
+ protocol_close_timeout : int = DEFAULT_PROTOCOL_CLOSE_TIMEOUT
72
+
73
+ # DCUtR timeouts
74
+ dcutr_read_timeout : int = DEFAULT_DCUTR_READ_TIMEOUT
75
+ dcutr_write_timeout : int = DEFAULT_DCUTR_WRITE_TIMEOUT
76
+ dial_timeout : int = DEFAULT_DIAL_TIMEOUT
77
+
78
+
79
+ # Relay roles enum
80
+ class RelayRole (Flag ):
81
+ """
82
+ Bit-flag enum that captures the three possible relay capabilities.
83
+
84
+ A node can combine multiple roles using bit-wise OR, for example::
85
+
86
+ RelayRole.HOP | RelayRole.STOP
87
+ """
88
+
89
+ HOP = auto () # Act as a relay for others ("hop")
90
+ STOP = auto () # Accept relayed connections ("stop")
91
+ CLIENT = auto () # Dial through existing relays ("client")
92
+
21
93
22
94
@dataclass
23
95
class RelayConfig :
24
96
"""Configuration for Circuit Relay v2."""
25
97
26
- # Role configuration
27
- enable_hop : bool = False # Whether to act as a relay (hop)
28
- enable_stop : bool = True # Whether to accept relayed connections (stop)
29
- enable_client : bool = True # Whether to use relays for dialing
98
+ # Role configuration (bit-flags)
99
+ roles : RelayRole = RelayRole .STOP | RelayRole .CLIENT
30
100
31
101
# Resource limits
32
102
limits : RelayLimits | None = None
33
103
34
104
# Discovery configuration
35
105
bootstrap_relays : list [PeerInfo ] = field (default_factory = list )
36
- min_relays : int = 3
37
- max_relays : int = 20
38
- discovery_interval : int = 300 # seconds
106
+ min_relays : int = DEFAULT_MIN_RELAYS
107
+ max_relays : int = DEFAULT_MAX_RELAYS
108
+ discovery_interval : int = DEFAULT_DISCOVERY_INTERVAL
39
109
40
110
# Connection configuration
41
- reservation_ttl : int = 3600 # seconds
42
- max_circuit_duration : int = 3600 # seconds
43
- max_circuit_bytes : int = 1024 * 1024 * 1024 # 1GB
111
+ reservation_ttl : int = DEFAULT_RESERVATION_TTL
112
+ max_circuit_duration : int = DEFAULT_MAX_CIRCUIT_DURATION
113
+ max_circuit_bytes : int = DEFAULT_MAX_CIRCUIT_BYTES
114
+
115
+ # Timeout configuration
116
+ timeouts : TimeoutConfig = field (default_factory = TimeoutConfig )
117
+
118
+ # ---------------------------------------------------------------------
119
+ # Backwards-compat boolean helpers. Existing code that still accesses
120
+ # ``cfg.enable_hop, cfg.enable_stop, cfg.enable_client`` will continue to work.
121
+ # ---------------------------------------------------------------------
122
+
123
+ @property
124
+ def enable_hop (self ) -> bool : # pragma: no cover – helper
125
+ return bool (self .roles & RelayRole .HOP )
126
+
127
+ @property
128
+ def enable_stop (self ) -> bool : # pragma: no cover – helper
129
+ return bool (self .roles & RelayRole .STOP )
130
+
131
+ @property
132
+ def enable_client (self ) -> bool : # pragma: no cover – helper
133
+ return bool (self .roles & RelayRole .CLIENT )
44
134
45
135
def __post_init__ (self ) -> None :
46
136
"""Initialize default values."""
47
137
if self .limits is None :
48
138
self .limits = RelayLimits (
49
139
duration = self .max_circuit_duration ,
50
140
data = self .max_circuit_bytes ,
51
- max_circuit_conns = 8 ,
52
- max_reservations = 4 ,
141
+ max_circuit_conns = DEFAULT_MAX_CIRCUIT_CONNS ,
142
+ max_reservations = DEFAULT_MAX_RESERVATIONS ,
53
143
)
54
144
55
145
@@ -58,35 +148,35 @@ class HopConfig:
58
148
"""Configuration specific to relay (hop) nodes."""
59
149
60
150
# Resource limits per IP
61
- max_reservations_per_ip : int = 8
62
- max_circuits_per_ip : int = 16
151
+ max_reservations_per_ip : int = MAX_RESERVATIONS_PER_IP
152
+ max_circuits_per_ip : int = MAX_CIRCUITS_PER_IP
63
153
64
154
# Rate limiting
65
- reservation_rate_per_ip : int = 4 # per minute
66
- circuit_rate_per_ip : int = 8 # per minute
155
+ reservation_rate_per_ip : int = RESERVATION_RATE_PER_IP
156
+ circuit_rate_per_ip : int = CIRCUIT_RATE_PER_IP
67
157
68
158
# Resource quotas
69
- max_circuits_total : int = 64
70
- max_reservations_total : int = 32
159
+ max_circuits_total : int = MAX_CIRCUITS_TOTAL
160
+ max_reservations_total : int = MAX_RESERVATIONS_TOTAL
71
161
72
162
# Bandwidth limits
73
- max_bandwidth_per_circuit : int = 1024 * 1024 # 1MB/s
74
- max_bandwidth_total : int = 10 * 1024 * 1024 # 10MB/s
163
+ max_bandwidth_per_circuit : int = MAX_BANDWIDTH_PER_CIRCUIT
164
+ max_bandwidth_total : int = MAX_BANDWIDTH_TOTAL
75
165
76
166
77
167
@dataclass
78
168
class ClientConfig :
79
169
"""Configuration specific to relay clients."""
80
170
81
171
# Relay selection
82
- min_relay_score : float = 0.5
83
- max_relay_latency : float = 1.0 # seconds
172
+ min_relay_score : float = MIN_RELAY_SCORE
173
+ max_relay_latency : float = MAX_RELAY_LATENCY
84
174
85
175
# Auto-relay settings
86
- enable_auto_relay : bool = True
87
- auto_relay_timeout : int = 30 # seconds
88
- max_auto_relay_attempts : int = 3
176
+ enable_auto_relay : bool = ENABLE_AUTO_RELAY
177
+ auto_relay_timeout : int = AUTO_RELAY_TIMEOUT
178
+ max_auto_relay_attempts : int = MAX_AUTO_RELAY_ATTEMPTS
89
179
90
180
# Reservation management
91
- reservation_refresh_threshold : float = 0.8 # Refresh at 80% of TTL
92
- max_concurrent_reservations : int = 2
181
+ reservation_refresh_threshold : float = RESERVATION_REFRESH_THRESHOLD
182
+ max_concurrent_reservations : int = MAX_CONCURRENT_RESERVATIONS
0 commit comments