Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
14 commits
Select commit Hold shift + click to select a range
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
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ GENLAYER_STUDIO_RECEIPT_WRAPPER_BYTES='1024'
GENLAYER_STUDIO_GHOST_FACTORY_ADDRESS='0x130e09c996462963A6398CA04e1011e6ef9d68a6'
GENLAYER_STUDIO_GHOST_BYTECODE_HASH='0x4249189346902cb6f0afc196d5719e1b4cbc861a700ae3b0c6dcbe51e5e566de'
GENLAYER_STUDIO_GHOST_FACTORY_INITIAL_NONCE='2'
# Also emit NewTransaction in receipts for SDKs that only parse it (genlayer-js
# before v2). Temporary upgrade compat; those SDKs also need gasless mode.
GENLAYER_STUDIO_LEGACY_NEW_TRANSACTION_RECEIPT_LOG='false'

# Ollama Server Configuration
OLAMAPORT='11434'
Expand Down
18 changes: 14 additions & 4 deletions .github/workflows/backend_integration_tests_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,20 @@ jobs:
echo "bake_cache=" >> "$GITHUB_OUTPUT"
fi

# Precompiled modules are rejected on CPUs lacking features they were built for (e.g. avx512bitalg).
- name: Fingerprint host CPU features
id: cpu
run: echo "flags=$(grep -m1 '^flags' /proc/cpuinfo | tr ' ' '\n' | sort | sha256sum | cut -c1-16)" >> "$GITHUB_OUTPUT"

- name: Restore GenVM precompile cache
uses: actions/cache@v5
with:
path: ${{ env.GENVM_CACHE_DIR }}
key: genvm-precompile-${{ runner.os }}-${{ runner.arch }}-${{ steps.genvm.outputs.binding }}
key: genvm-precompile-per-executor-${{ runner.os }}-${{ runner.arch }}-${{ steps.cpu.outputs.flags }}-${{ steps.genvm.outputs.binding }}

- name: Prepare GenVM cache directory
run: |
sudo mkdir -p "$GENVM_CACHE_DIR/pc"
sudo mkdir -p "$GENVM_CACHE_DIR"
sudo chown -R 999:999 "$GENVM_CACHE_DIR"

# Source builds need the GenVM runner fixed-output derivations built under
Expand Down Expand Up @@ -459,15 +464,20 @@ jobs:
echo "bake_cache=" >> "$GITHUB_OUTPUT"
fi

# Precompiled modules are rejected on CPUs lacking features they were built for (e.g. avx512bitalg).
- name: Fingerprint host CPU features
id: cpu
run: echo "flags=$(grep -m1 '^flags' /proc/cpuinfo | tr ' ' '\n' | sort | sha256sum | cut -c1-16)" >> "$GITHUB_OUTPUT"

- name: Restore GenVM precompile cache
uses: actions/cache@v5
with:
path: ${{ env.GENVM_CACHE_DIR }}
key: genvm-precompile-${{ runner.os }}-${{ runner.arch }}-${{ steps.genvm.outputs.binding }}
key: genvm-precompile-per-executor-${{ runner.os }}-${{ runner.arch }}-${{ steps.cpu.outputs.flags }}-${{ steps.genvm.outputs.binding }}

- name: Prepare GenVM cache directory
run: |
sudo mkdir -p "$GENVM_CACHE_DIR/pc"
sudo mkdir -p "$GENVM_CACHE_DIR"
sudo chown -R 999:999 "$GENVM_CACHE_DIR"

# Source builds need the GenVM runner fixed-output derivations built under
Expand Down
9 changes: 7 additions & 2 deletions .github/workflows/load-test-oha.yml
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,20 @@ jobs:
echo "bake_cache=" >> "$GITHUB_OUTPUT"
fi

# Precompiled modules are rejected on CPUs lacking features they were built for (e.g. avx512bitalg).
- name: Fingerprint host CPU features
id: cpu
run: echo "flags=$(grep -m1 '^flags' /proc/cpuinfo | tr ' ' '\n' | sort | sha256sum | cut -c1-16)" >> "$GITHUB_OUTPUT"

- name: Restore GenVM precompile cache
uses: actions/cache@v5
with:
path: ${{ env.GENVM_CACHE_DIR }}
key: genvm-precompile-${{ runner.os }}-${{ runner.arch }}-${{ steps.genvm.outputs.binding }}
key: genvm-precompile-per-executor-${{ runner.os }}-${{ runner.arch }}-${{ steps.cpu.outputs.flags }}-${{ steps.genvm.outputs.binding }}

- name: Prepare GenVM cache directory
run: |
sudo mkdir -p "$GENVM_CACHE_DIR/pc"
sudo mkdir -p "$GENVM_CACHE_DIR"
sudo chown -R 999:999 "$GENVM_CACHE_DIR"

# Source builds need the GenVM runner fixed-output derivations built under
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@


def upgrade() -> None:
# Lift c1d2e3f4a5b6's 30s limit; env.py runs the whole chain in one transaction.
op.execute("SET LOCAL statement_timeout = 0")
op.create_table(
"evm_envelopes",
sa.Column("hash", sa.String(length=66), nullable=False),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@


def upgrade() -> None:
# Lift c1d2e3f4a5b6's 30s limit; env.py runs the whole chain in one transaction.
op.execute("SET LOCAL statement_timeout = 0")
op.execute("CREATE SEQUENCE transactions_queue_order_seq")
op.add_column(
"transactions",
Expand Down
12 changes: 11 additions & 1 deletion backend/node/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import backend.node.genvm.base as genvmbase
import backend.node.genvm.origin.calldata as calldata
from backend.node.genvm.executor_selection import (
clamp_to_legacy_line_activation,
legacy_executor_selector_for_code,
uses_legacy_storage,
)
Expand Down Expand Up @@ -1081,7 +1082,16 @@ async def _run_genvm(
if transaction_datetime is None:
transaction_datetime = datetime.datetime.now(datetime.UTC)
assert transaction_datetime.tzinfo is not None
message["transaction_timestamp"] = transaction_datetime.isoformat()
run_datetime = clamp_to_legacy_line_activation(
transaction_datetime, self.contract_snapshot.genvm_executor_selector
)
if run_datetime != transaction_datetime:
logger.warning(
"transaction time predates the v0.2 executor line, running at its activation",
requested=transaction_datetime.isoformat(),
used=run_datetime.isoformat(),
)
message["transaction_timestamp"] = run_datetime.isoformat()
perms = "rcn" # read/call/spawn nondet
if not readonly:
perms += "ws" # write/send
Expand Down
12 changes: 12 additions & 0 deletions backend/node/genvm/executor_selection.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Compatibility routing for the retained v0.2 text-contract executor."""

import datetime
import functools
import io
import json
Expand All @@ -12,6 +13,8 @@
# manager choose the newest available patch in this line, not a hardcoded
# directory or the newest semver-major-0 executor (which is v0.3).
LEGACY_EXECUTOR_SELECTOR = r"re:^v0\.2\."
# The "studio" network's genvm/executor/v0.2 date in genvm-manager's data/genvm-chainforks.json
LEGACY_LINE_ACTIVATION = datetime.datetime(2026, 1, 1, tzinfo=datetime.UTC)
_LEGACY_HEADER = re.compile(
rb"(?:#|//|--)\s*v0\.2\.(?:[0-9]+|\*)(?:-[0-9A-Za-z.-]+)?\s*"
)
Expand Down Expand Up @@ -62,6 +65,15 @@ def uses_legacy_storage(selector: str | None) -> bool:
)


def clamp_to_legacy_line_activation(
timestamp: datetime.datetime, selector: str | None
) -> datetime.datetime:
"""Keep a v0.2 line selector resolvable: the manager drops the line before its fork."""
if selector == LEGACY_EXECUTOR_SELECTOR and timestamp < LEGACY_LINE_ACTIVATION:
return LEGACY_LINE_ACTIVATION
return timestamp


def _declared_runners(code: bytes) -> set[str]:
"""Runner ids named by `Depends` actions in the code's runner spec."""
spec = _runner_spec(code)
Expand Down
60 changes: 49 additions & 11 deletions backend/protocol_rpc/endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
from backend.database_handler.snapshot_manager import SnapshotManager
from backend.node.base import Manager as GenVMManager
import asyncio
from starlette.concurrency import run_in_threadpool

# Limit concurrent GenVM executions on the jsonrpc path to prevent uvloop fd
# conflicts and DB pool exhaustion while calls hold request-scoped sessions.
Expand Down Expand Up @@ -169,6 +170,11 @@ def __init__(
_gen_call_singleflight_tasks: dict[str, asyncio.Task[str]] = {}
_gen_call_singleflight_lock = asyncio.Lock()

# Compat for SDKs that only parse NewTransaction (genlayer-js before v2).
_LEGACY_NEW_TRANSACTION_RECEIPT_LOG = os.environ.get(
"GENLAYER_STUDIO_LEGACY_NEW_TRANSACTION_RECEIPT_LOG", "false"
).lower() in {"1", "true", "yes", "on"}


def _check_rate_limit(address: str) -> None:
"""Reject if address exceeds rate limit. Prunes old entries."""
Expand Down Expand Up @@ -561,13 +567,12 @@ def fund_account(
0,
None,
transaction_hash,
commit=False,
)
accounts_manager.credit_tx_value_once(
transaction_hash, account_address, normalized_amount
)
# Release the current_state row lock before returning: sync handlers run on
# the event loop, so a concurrent caller blocking on this row would freeze
# the loop and prevent the request-scoped session from ever committing.
# Publish the tx already credited, or a worker can claim it and credit it again.
session.commit()
return transaction_hash

Expand Down Expand Up @@ -1279,7 +1284,9 @@ async def get_contract_schema(
contract_address: str,
) -> dict:
try:
contract_snapshot = ContractSnapshot(contract_address, session)
contract_snapshot = await run_in_threadpool(
ContractSnapshot, contract_address, session
)
except ContractNotFoundError:
raise NotFoundError(
message=f"Contract {contract_address} not found",
Expand Down Expand Up @@ -1749,20 +1756,22 @@ async def _gen_call_with_validator(
sim_config is not None and sim_config.genvm_datetime is not None
)

def create_node() -> Node:
async def create_node() -> Node:
# A fee estimate can execute twice: first to discover exact message
# keys and then to meter them. Each pass must start from the same DB
# snapshot; _SnapshotView writes through to its ContractSnapshot.
if type == "deploy":
contract_snapshot = ContractSnapshot(None, session)
contract_snapshot = await run_in_threadpool(ContractSnapshot, None, session)
contract_snapshot.contract_address = to_address
contract_snapshot.balance = 0
contract_snapshot.states = {"accepted": {}, "finalized": {}}
contract_snapshot.genvm_executor_selector = (
sim_config.genvm_executor_selector if sim_config else None
)
else:
contract_snapshot = ContractSnapshot(to_address, session)
contract_snapshot = await run_in_threadpool(
ContractSnapshot, to_address, session
)
if type in {"write", "deploy"}:
_stage_simulated_call_value(contract_snapshot, call_value)
return Node(
Expand All @@ -1777,7 +1786,7 @@ def create_node() -> Node:
)

async def execute(active_fee_accounting):
node = create_node()
node = await create_node()
if type == "read":
# Pre-parse timestamp override and map errors
txn_dt = None
Expand Down Expand Up @@ -2399,8 +2408,8 @@ async def eth_call(

# Check if this is a ConsensusData contract call that we should handle locally
# This should happen before early return to allow interception even without 'from'
consensus_data_result = handle_consensus_data_call(
transactions_processor, to_address, data
consensus_data_result = await run_in_threadpool(
handle_consensus_data_call, transactions_processor, to_address, data
)
if consensus_data_result is not None:
return consensus_data_result
Expand All @@ -2426,7 +2435,9 @@ async def eth_call(
)
as_validator = snapshot.nodes[0].validator
try:
target_contract_snapshot = ContractSnapshot(to_address, session)
target_contract_snapshot = await run_in_threadpool(
ContractSnapshot, to_address, session
)
except ContractNotFoundError:
raise NotFoundError(
message=f"Contract {to_address} not found",
Expand Down Expand Up @@ -3590,6 +3601,11 @@ def get_gas_estimate(data: Any) -> str:
return hex(0x7A120) # 500,000 — fits within block limit, enough for any Studio tx


def _address_log_topic(address: str | None) -> str:
bare_address = (address or "").removeprefix("0x").lower()
return "0x" + bare_address.rjust(64, "0")


def get_transaction_receipt(
transactions_processor: TransactionsProcessor,
transaction_hash: str,
Expand Down Expand Up @@ -3662,6 +3678,28 @@ def get_transaction_receipt(
"removed": False,
}
)
if _LEGACY_NEW_TRANSACTION_RECEIPT_LOG:
logs.append(
{
"address": to_addr,
"topics": [
"0x"
+ eth_utils.keccak(
text="NewTransaction(bytes32,address,address)"
).hex(),
transaction_hash,
_address_log_topic(transaction.get("to_address")),
_address_log_topic(from_addr),
],
"data": "0x",
"blockNumber": 0,
"transactionHash": transaction_hash,
"transactionIndex": 0,
"blockHash": transaction_hash,
"logIndex": 1,
"removed": False,
}
)

receipt = {
"transactionHash": transaction_hash,
Expand Down
33 changes: 27 additions & 6 deletions backend/protocol_rpc/message_handler/fastapi_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def __init__(self, broadcast: Broadcast, config: GlobalConfiguration):
self.config = config
self.client_session_id = None
self._pending_tasks: set[asyncio.Task] = set()
try:
self._loop: asyncio.AbstractEventLoop | None = asyncio.get_running_loop()
except RuntimeError:
self._loop = None

def _track_background_task(self, task: asyncio.Task) -> None:
"""Retain a background task and report failures until it completes."""
Expand All @@ -49,6 +53,7 @@ def _background_task_done(self, task: asyncio.Task) -> None:
def with_client_session(self, client_session_id: str):
new_msg_handler = MessageHandler(self.broadcast, self.config)
new_msg_handler._pending_tasks = self._pending_tasks
new_msg_handler._loop = self._loop
new_msg_handler.client_session_id = client_session_id
return new_msg_handler

Expand Down Expand Up @@ -92,17 +97,33 @@ def _publish(self, channel: str, payload: dict[str, Any]) -> None:

message = json.dumps(payload)
try:
loop = asyncio.get_running_loop()
running_loop = asyncio.get_running_loop()
except RuntimeError:
running_loop = None

loop = self._loop
if loop is None or not loop.is_running():
loop = running_loop
if loop is None or not loop.is_running():
return
self._loop = loop

def publish() -> None:
task = loop.create_task(
self.broadcast.publish(channel=channel, message=message)
)
self._track_background_task(task)

if not loop.is_running():
if running_loop is loop:
publish()
return

task = loop.create_task(
self.broadcast.publish(channel=channel, message=message)
)
self._track_background_task(task)
# Sync RPC handlers run in worker threads; Broadcast belongs to the app loop.
try:
loop.call_soon_threadsafe(publish)
except RuntimeError:
# The loop closed while a worker was still finishing.
pass

def _socket_emit(self, log_event: LogEvent) -> None:
"""Emit a log event via broadcast channels.
Expand Down
12 changes: 10 additions & 2 deletions backend/protocol_rpc/rpc_endpoint_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from fastapi.requests import Request
from pydantic import BaseModel, ConfigDict
from sqlalchemy.exc import SQLAlchemyError
from starlette.concurrency import run_in_threadpool

from backend.protocol_rpc.exceptions import (
InternalError,
Expand Down Expand Up @@ -313,7 +314,7 @@ async def _call_endpoint(
call_kwargs = bound_arguments
if "msg_handler" in call_kwargs:
call_kwargs["msg_handler"] = session_logger
result = registered.dependant.call(**call_kwargs)
result = await self._invoke_handler(registered.dependant.call, call_kwargs)
if inspect.isawaitable(result):
result = await result
return result
Expand Down Expand Up @@ -374,11 +375,18 @@ async def _call_endpoint(
if "msg_handler" in call_kwargs:
call_kwargs["msg_handler"] = session_logger

result = registered.dependant.call(**call_kwargs)
result = await self._invoke_handler(registered.dependant.call, call_kwargs)
if inspect.isawaitable(result):
result = await result
return result

@staticmethod
async def _invoke_handler(handler: Any, kwargs: Dict[str, Any]) -> Any:
# A sync pool checkout on the loop blocks the cleanup that returns connections.
if inspect.iscoroutinefunction(handler):
return await handler(**kwargs)
return await run_in_threadpool(handler, **kwargs)

def _bind_rpc_arguments(
self,
registered: RegisteredEndpoint,
Expand Down
Loading
Loading