maker.rate_limiting
maker.rate_limiting
Rate limiting for maker bot connections.
Provides two rate limiters: - OrderbookRateLimiter: Per-peer rate limiting for orderbook requests with exponential backoff - DirectConnectionRateLimiter: Per-connection rate limiting for direct hidden service connections
Attributes
DEFAULT_BAN_DURATION = 3600.0
module-attribute
DEFAULT_HOSTID = 'onion-network'
module-attribute
DEFAULT_HP2_ADMISSION_BURST = 120
module-attribute
DEFAULT_HP2_ADMISSION_REFILL_PER_SECOND = 2.0
module-attribute
DEFAULT_HP2_RELAY_WORK_BURST = 2
module-attribute
DEFAULT_HP2_RELAY_WORK_REFILL_PER_SECOND = 1.0 / 30.0
module-attribute
DEFAULT_MAX_TRACKED_IDENTITIES = 10000
module-attribute
DEFAULT_ORDERBOOK_PROOF_WORK_BURST = 20
module-attribute
DEFAULT_ORDERBOOK_PROOF_WORK_REFILL_PER_SECOND = 1.0
module-attribute
DEFAULT_ORDERBOOK_RATE_INTERVAL = 10.0
module-attribute
DEFAULT_ORDERBOOK_RATE_LIMIT = 1
module-attribute
DEFAULT_VIOLATION_BAN_THRESHOLD = 100
module-attribute
DEFAULT_VIOLATION_SEVERE_THRESHOLD = 50
module-attribute
DEFAULT_VIOLATION_WARNING_THRESHOLD = 10
module-attribute
Classes
DirectConnectionRateLimiter
Rate limiter for direct hidden service connections.
Unlike the nick-based OrderbookRateLimiter, this tracks by connection address to prevent nick rotation attacks where attackers use a different nick per request.
Since Tor creates a new circuit for each connection, the "peer address" from the local perspective will be 127.0.0.1:random_port. However, we can still track by this local port since each attacking circuit gets a unique port.
This provides: 1. Per-connection message rate limiting (general flood protection) 2. Per-connection orderbook request limiting (specific attack mitigation) 3. Connection banning after excessive violations
Source code in maker/src/maker/rate_limiting.py
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 | |
Attributes
ban_duration = ban_duration
instance-attribute
max_tracked_identities = max_tracked_identities
instance-attribute
message_burst = message_burst
instance-attribute
message_rate_per_sec = message_rate_per_sec
instance-attribute
orderbook_ban_threshold = orderbook_ban_threshold
instance-attribute
orderbook_interval = orderbook_interval
instance-attribute
Methods:
__init__(message_rate_per_sec: float = 5.0, message_burst: int = 20, orderbook_interval: float = 30.0, orderbook_ban_threshold: int = 10, ban_duration: float = 3600.0, max_tracked_identities: int = DEFAULT_MAX_TRACKED_IDENTITIES)
Initialize the direct connection rate limiter.
Args: message_rate_per_sec: Max sustained message rate per connection message_burst: Allowed burst of messages orderbook_interval: Minimum interval between orderbook requests orderbook_ban_threshold: Ban after this many orderbook violations ban_duration: How long to ban connections (seconds) max_tracked_identities: Maximum connection states retained in memory.
Source code in maker/src/maker/rate_limiting.py
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 | |
check_message(conn_id: str) -> bool
Check if a message from this connection should be allowed.
Uses token bucket algorithm for general rate limiting.
Args: conn_id: Connection identifier (e.g., "127.0.0.1:54321")
Returns: True if allowed, False if rate limited
Source code in maker/src/maker/rate_limiting.py
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 | |
check_orderbook(conn_id: str) -> bool
Check if an orderbook request from this connection should be allowed.
Uses stricter limiting than general messages since orderbook responses are expensive (fidelity bond proofs).
Args: conn_id: Connection identifier
Returns: True if allowed, False if rate limited or banned
Source code in maker/src/maker/rate_limiting.py
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 | |
cleanup_old_entries(max_age: float = 3600.0) -> None
Remove stale entries to prevent memory growth.
Source code in maker/src/maker/rate_limiting.py
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 | |
get_statistics() -> dict[str, Any]
Get rate limiter statistics for monitoring.
Source code in maker/src/maker/rate_limiting.py
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 | |
get_violation_count(conn_id: str) -> int
Get violation count for a connection.
Source code in maker/src/maker/rate_limiting.py
537 538 539 | |
is_banned(conn_id: str) -> bool
Check if a connection is currently banned.
Source code in maker/src/maker/rate_limiting.py
526 527 528 529 530 531 532 533 534 535 | |
OrderbookRateLimiter
Per-peer rate limiter for orderbook requests with exponential backoff and banning.
Prevents DoS attacks by limiting how often each peer can request the orderbook. Uses a timestamp-based approach with escalating penalties:
- Normal operation: 1 response per interval (default 10s)
- After 10 violations: Exponential backoff starts (60s interval)
- After 50 violations: Severe backoff (300s = 5min interval)
- After 100 violations: Permanent ban until cleanup/restart
This is crucial because: 1. !orderbook responses include fidelity bond proofs which are expensive to compute 2. Unlimited responses can flood log files 3. A bad actor can exhaust maker resources by spamming requests
Note: This limiter tracks by peer_id which can be either a nick (for directory messages) or a connection address (for direct connections). For direct connections, use the peer address to prevent nick rotation attacks.
Source code in maker/src/maker/rate_limiting.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | |
Attributes
ban_duration = ban_duration
instance-attribute
directory_sources = directory_sources
instance-attribute
interval = interval
instance-attribute
max_tracked_identities = max_tracked_identities
instance-attribute
violation_ban_threshold = violation_ban_threshold
instance-attribute
violation_severe_threshold = violation_severe_threshold
instance-attribute
violation_warning_threshold = violation_warning_threshold
instance-attribute
Methods:
__init__(rate_limit: int = DEFAULT_ORDERBOOK_RATE_LIMIT, interval: float = DEFAULT_ORDERBOOK_RATE_INTERVAL, violation_ban_threshold: int = DEFAULT_VIOLATION_BAN_THRESHOLD, violation_warning_threshold: int = DEFAULT_VIOLATION_WARNING_THRESHOLD, violation_severe_threshold: int = DEFAULT_VIOLATION_SEVERE_THRESHOLD, ban_duration: float = DEFAULT_BAN_DURATION, max_tracked_identities: int = DEFAULT_MAX_TRACKED_IDENTITIES, directory_sources: frozenset[str] = frozenset()) -> None
Initialize the rate limiter.
Args: rate_limit: Maximum number of responses per interval (currently unused, always 1 response per interval for simplicity) interval: Base time window in seconds violation_ban_threshold: Ban peer after this many violations violation_warning_threshold: Start exponential backoff after this violation_severe_threshold: Severe backoff threshold ban_duration: How long to ban peers (seconds) max_tracked_identities: Maximum peer states retained in memory. directory_sources: Known directory identities eligible for fanout suppression.
Source code in maker/src/maker/rate_limiting.py
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | |
check(peer_nick: str, source: str | None = None) -> bool
Check if we should respond to an orderbook request from this peer.
A first copy from each other configured directory is suppressed without a violation during the base interval following an admitted request.
Returns True if allowed, False if rate limited or banned.
Source code in maker/src/maker/rate_limiting.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | |
cleanup_old_entries(max_age: float = 3600.0) -> None
Remove entries older than max_age to prevent memory growth.
Source code in maker/src/maker/rate_limiting.py
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 | |
get_statistics() -> dict[str, Any]
Get rate limiter statistics for monitoring.
Returns: Dict with keys: - total_violations: Total violation count across all peers - tracked_peers: Number of peers being tracked - banned_peers: List of currently banned peer nicks - top_violators: List of (nick, violations) tuples, top 10 - fanout_duplicates: Cumulative cross-directory duplicates suppressed
Source code in maker/src/maker/rate_limiting.py
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | |
get_violation_count(peer_nick: str) -> int
Get the number of rate limit violations for a peer.
Source code in maker/src/maker/rate_limiting.py
282 283 284 | |
is_banned(peer_nick: str) -> bool
Check if a peer is currently banned.
Source code in maker/src/maker/rate_limiting.py
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | |
ProcessWideTokenBucket
A thread-safe monotonic token bucket for aggregate maker work.
Source code in maker/src/maker/rate_limiting.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
Attributes
burst = burst
instance-attribute
refill_per_second = refill_per_second
instance-attribute
Methods:
__init__(burst: int, refill_per_second: float, clock: Callable[[], float] | None = None) -> None
Source code in maker/src/maker/rate_limiting.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | |
try_consume() -> bool
Consume one token immediately, returning False when depleted.
Source code in maker/src/maker/rate_limiting.py
63 64 65 66 67 68 69 70 71 72 73 | |