Skip to content
Merged
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
48 changes: 48 additions & 0 deletions backend/protocol_rpc/endpoints.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# rpc/endpoints.py
import random
import json
import time
import eth_utils
import logging
from functools import partial, wraps
Expand Down Expand Up @@ -62,6 +63,40 @@
_GENVM_CONCURRENCY = int(os.environ.get("GENVM_MAX_CONCURRENT", "8"))
_genvm_semaphore = asyncio.Semaphore(_GENVM_CONCURRENCY)

# ---------------------------------------------------------------------------
# Per-address rate limiting for gen_call / sim_call
# Prevents a single contract from monopolising all GenVM execution slots.
# ---------------------------------------------------------------------------
_RATE_LIMIT_WINDOW = float(
os.environ.get("GEN_CALL_RATE_LIMIT_WINDOW", "10")
) # seconds
_RATE_LIMIT_MAX = int(
os.environ.get("GEN_CALL_RATE_LIMIT_MAX", "20")
) # max requests per window per address

_address_request_log: dict[str, list[float]] = {} # {address: [timestamp, ...]}

_rate_limit_logger = logging.getLogger(__name__ + ".rate_limit")


def _check_rate_limit(address: str) -> None:
"""Reject if address exceeds rate limit. Prunes old entries."""
now = time.monotonic()
timestamps = _address_request_log.get(address, [])
cutoff = now - _RATE_LIMIT_WINDOW
timestamps = [t for t in timestamps if t > cutoff]
if len(timestamps) >= _RATE_LIMIT_MAX:
_rate_limit_logger.warning(
f"Rate limit exceeded for {address}: {len(timestamps)} requests in {_RATE_LIMIT_WINDOW}s window"
)
raise JSONRPCError(
code=-32005,
message=f"Rate limit exceeded: max {_RATE_LIMIT_MAX} gen_call requests per {_RATE_LIMIT_WINDOW}s per contract address",
data={"address": address, "retry_after_seconds": _RATE_LIMIT_WINDOW},
)
timestamps.append(now)
_address_request_log[address] = timestamps
Comment on lines +77 to +98

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

_address_request_log grows without bound for inactive addresses.

Entries are only pruned when the same address is checked again. Addresses that go idle leave stale entries in the dict forever. Over time in production (many distinct contract addresses), this is a slow memory leak.

Consider periodically evicting addresses whose timestamps have all expired — for example, by cleaning during each _check_rate_limit call (amortised), or via a background task.

♻️ Lightweight amortised cleanup sketch
+import itertools
+
+_CLEANUP_EVERY = 500  # prune stale addresses every N calls
+_check_count = 0
+
 def _check_rate_limit(address: str) -> None:
     """Reject if address exceeds rate limit. Prunes old entries."""
+    global _check_count
     now = time.monotonic()
+    cutoff = now - _RATE_LIMIT_WINDOW
+
+    # Amortised full sweep
+    _check_count += 1
+    if _check_count >= _CLEANUP_EVERY:
+        _check_count = 0
+        stale = [a for a, ts in _address_request_log.items() if ts and ts[-1] <= cutoff]
+        for a in stale:
+            del _address_request_log[a]
+
     timestamps = _address_request_log.get(address, [])
-    cutoff = now - _RATE_LIMIT_WINDOW
     timestamps = [t for t in timestamps if t > cutoff]
🤖 Prompt for AI Agents
In `@backend/protocol_rpc/endpoints.py` around lines 77 - 98, The
_address_request_log dictionary accumulates stale entries for addresses that
never get checked again; modify _check_rate_limit to perform amortised cleanup
by pruning expired timestamps for all addresses as well as the current one:
compute cutoff = now - _RATE_LIMIT_WINDOW, iterate over keys of
_address_request_log and replace each list with only timestamps > cutoff, and
delete any address whose list becomes empty to free memory; then proceed with
the existing per-address rate check logic (using
_RATE_LIMIT_MAX/_RATE_LIMIT_WINDOW) and keep logging via _rate_limit_logger as
before.



####### ADMIN ACCESS CONTROL #######
def require_admin_access(func):
Expand Down Expand Up @@ -936,6 +971,9 @@ async def _gen_call_with_validator(
if not accounts_manager.is_valid_address(to_address):
raise InvalidAddressError(to_address)

# Rate limit per contract address — reject early before acquiring resources
_check_rate_limit(to_address)

if transaction_hash_variant == "latest-final":
state_status = "finalized"
else:
Expand Down Expand Up @@ -965,6 +1003,16 @@ async def _gen_call_with_validator(
sim_config is not None and sim_config.genvm_datetime is not None
)

if _genvm_semaphore.locked():
_rate_limit_logger.warning(
f"GenVM at capacity ({_GENVM_CONCURRENCY} concurrent) — rejecting gen_call to {to_address}"
)
raise JSONRPCError(
code=-32006,
message=f"Server busy: all {_GENVM_CONCURRENCY} execution slots occupied, retry later",
data={"retry_after_seconds": 2},
)

async with _genvm_semaphore:
if type == "read":
# Pre-parse timestamp override and map errors
Expand Down
33 changes: 33 additions & 0 deletions backend/protocol_rpc/health.py
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,39 @@ async def health_database() -> Dict[str, Any]:
return {"status": "error", "error": str(e)}


@health_router.get("/health/ratelimit")
async def health_ratelimit() -> Dict[str, Any]:
"""Show per-address gen_call rate limit state."""
from backend.protocol_rpc.endpoints import (
_address_request_log,
_RATE_LIMIT_WINDOW,
_RATE_LIMIT_MAX,
_genvm_semaphore,
_GENVM_CONCURRENCY,
)
import time as _time

now = _time.monotonic()
cutoff = now - _RATE_LIMIT_WINDOW
addresses = {}
for addr, timestamps in _address_request_log.items():
recent = [t for t in timestamps if t > cutoff]
if recent:
addresses[addr] = {
"requests_in_window": len(recent),
"limit": _RATE_LIMIT_MAX,
"oldest_in_window_age_s": round(now - min(recent), 1),
}
return {
"window_seconds": _RATE_LIMIT_WINDOW,
"max_per_window": _RATE_LIMIT_MAX,
"genvm_concurrency_limit": _GENVM_CONCURRENCY,
"genvm_semaphore_available": _genvm_semaphore._value, # noqa: SLF001

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🌐 Web query:

asyncio.Semaphore._value Python 3.12 public API stable documentation

💡 Result:

asyncio.Semaphore._value is not a public, stable API in Python 3.12.

  • The official Python 3.12 documentation for asyncio.Semaphore documents the constructor and the methods acquire(), locked(), and release()—but does not document any .value/._value attribute as supported API. [1]
  • In CPython’s 3.12 implementation, the internal counter is stored in an instance attribute named self._value, i.e., an internal (underscore-prefixed) implementation detail that can change without notice. [2]

If you need supported behavior, rely on acquire()/release()/locked() (or maintain your own counter / wrap the semaphore), rather than reading or writing ._value. [1][2]

Sources: [1] [2]


🏁 Script executed:

# Check linting configuration to see if SLF001 is enabled
find . -name "*.toml" -o -name "*.cfg" -o -name ".ruff*" | head -20

Repository: genlayerlabs/genlayer-studio

Length of output: 83


🏁 Script executed:

# Examine the actual file and surrounding context
head -950 backend/protocol_rpc/health.py | tail -50

Repository: genlayerlabs/genlayer-studio

Length of output: 1753


🏁 Script executed:

# Check imports at the top of the file
head -30 backend/protocol_rpc/health.py

Repository: genlayerlabs/genlayer-studio

Length of output: 1088


🏁 Script executed:

# Check ruff.toml to see if SLF001 is enabled or disabled
cat ruff.toml

Repository: genlayerlabs/genlayer-studio

Length of output: 978


Remove unused noqa directive and avoid accessing asyncio.Semaphore._value, a CPython implementation detail.

The _value attribute is not part of the public API and is not documented in Python 3.12 documentation. It could break on alternative runtimes or future CPython versions. The noqa: SLF001 directive is also unused since the SLF rule is not enabled in the project's ruff.toml.

Fix: remove unused noqa
-        "genvm_semaphore_available": _genvm_semaphore._value,  # noqa: SLF001
+        "genvm_semaphore_available": _genvm_semaphore._value,

For a more robust solution, consider computing availability from the concurrency limit minus an in-use counter, or exposing it from the semaphore wrapper.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"genvm_semaphore_available": _genvm_semaphore._value, # noqa: SLF001
"genvm_semaphore_available": _genvm_semaphore._value,
🧰 Tools
🪛 Ruff (0.15.0)

[warning] 941-941: Unused noqa directive (non-enabled: SLF001)

Remove unused noqa directive

(RUF100)

🤖 Prompt for AI Agents
In `@backend/protocol_rpc/health.py` at line 941, Remove the unused "noqa: SLF001"
and stop reading the CPython internal attribute _genvm_semaphore._value; instead
expose availability via a public API or compute it from a tracked counter.
Replace the direct access to _genvm_semaphore._value in the health payload by
calling a new/existing public accessor (e.g., genvm_semaphore.available() or
get_genvm_semaphore_available()) or compute availability as (concurrency_limit -
genvm_in_use_counter) maintained where acquire/release happen; update the code
paths that change the semaphore to increment/decrement that counter so the
health endpoint can report availability without touching internal attributes.

"active_addresses": len(addresses),
"addresses": addresses,
}


@health_router.get("/health/memory")
async def health_memory() -> Dict[str, Any]:
"""Show detailed memory usage statistics."""
Expand Down
Loading