diff --git a/CHANGELOG.md b/CHANGELOG.md index 904cd0f..2086aca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,42 @@ All notable changes to blockrun-llm will be documented in this file. ## Unreleased +## 1.22.3 — 2026-11-14 + +### Fixed +- **A Solana paid call that answered 5xx is never paid for twice.** + sol.blockrun.ai settles in parallel with the upstream call, so a request can + be charged and still answer 502/503. The paid leg re-sends the same signed + payment once on a 5xx (one signature settles at most once, so that resend is + safe), and the gateway, still holding that payment's nonce, refuses the + resend with a bare `402 PAYMENT_REPLAY`. The whole-request retry read that + as a concurrent nonce collision and signed a new payment, which settled too: + two charges for one call. Reproduced against a fake gateway on every retry + loop (chat, streaming chat, raw POST, raw GET; sync and async). Now any 402 + that refuses a resend after a 5xx raises without signing again, carries the + first attempt's status, gateway request id + (`x-blockrun-gateway-request-id`) and receipt under + `exc.response["prior_attempt"]`, and says the first attempt may have been + charged. A 5xx that itself reports the payment (`payment_status` other than + `not_charged`, a `tx_hash`, `CHARGED_UPSTREAM_FAILED`, or a receipt header) + is raised as is instead of resent, so its receipt reaches the caller. Kept: + the same-signature resend after a 5xx the gateway never charged, and the + re-sign after a `payment_status: "not_charged"` 5xx or a replay with no 5xx + before it. +- **Media poll re-signs use a fresh blockhash again.** sol quotes + `recentBlockhash` on every 402, and since 1.22.1 (`x402>=2.17`) a signer + signs on a quote, so re-signing a video's submit challenge to keep the + payment fresh produced the same aging hash every time. Poll re-signs now drop + the quote first and sign on the latest blockhash; amount and recipient are + unchanged. +- `AsyncSolanaLLMClient` streaming raised `TypeError` instead of the + gateway's `APIError` on every stream error (the sync class's static + `_raise_stream_error` was aliased unwrapped). +- The old-`x402` warning now says `pip install -U 'blockrun-llm[solana]'`. + The previous `pip install -U 'x402[svm]>=2.17.0'` resolved x402 without this + package's `solana<0.40` pin and could leave an incompatible x402/solders/solana + mix. + ## 1.22.2 — 2026-11-13 ### Fixed diff --git a/VERSION b/VERSION index 6fee2fe..89144db 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.22.2 +1.22.3 diff --git a/blockrun_llm/__init__.py b/blockrun_llm/__init__.py index 0460356..9673a1e 100644 --- a/blockrun_llm/__init__.py +++ b/blockrun_llm/__init__.py @@ -198,7 +198,7 @@ create_wallet as generate_wallet, # User-friendly alias ) -__version__ = "1.22.2" +__version__ = "1.22.3" __all__ = [ "DEFAULT_API_KEY_URL", "ENV_API_KEY", diff --git a/blockrun_llm/solana_client.py b/blockrun_llm/solana_client.py index 21bf8bb..57ae1b5 100644 --- a/blockrun_llm/solana_client.py +++ b/blockrun_llm/solana_client.py @@ -341,6 +341,11 @@ def _is_safe_resign_error(exc: PaymentError) -> bool: no fresh signature makes them pass, and each wasted attempt costs the gateway its own verify retries. """ + # Raised after this signature already drew a 5xx (see + # _refused_after_paid_5xx): that earlier attempt may have settled, so + # whatever the refusal says about the RESEND, a new signature can pay twice. + if getattr(exc, _SETTLED_ATTR, False): + return False body = exc.response if isinstance(exc.response, dict) else {} # A replay that names the earlier request's result (poll_url / job_id / # recoverable / wallet_auth) says that request owns the authorization and @@ -372,6 +377,95 @@ def _is_safe_resign_error(exc: PaymentError) -> bool: return message.startswith(_PRE_BROADCAST_TITLES) +# --- A paid leg that answered 5xx --------------------------------------------- +# +# sol settles in parallel with the upstream call (and Fable 5.2 at a 45s bound), +# so a paid request can be CHARGED and still answer 5xx ("CHARGED BUT REQUEST +# FAILED" in the gateway log). The paid legs re-send the SAME signed payment +# once on a 5xx: one signature settles at most once, so that resend can never +# pay twice, and it is the only recovery from a 5xx the gateway itself never +# saw (a load-balancer 502). But if the first attempt did settle, the gateway +# still holds its nonce claim and answers the resend with a bare 402 +# PAYMENT_REPLAY, which the whole-request retry used to read as a concurrent +# nonce collision and re-sign: a second payment for one call. A refusal of a +# resend therefore says nothing about the FIRST attempt and is never re-signed, +# unless that first 5xx itself proved nothing was charged. + +_GATEWAY_REQUEST_ID_HEADER = "x-blockrun-gateway-request-id" + +# The one payment_status a gateway 5xx uses to say no money moved (sol's +# pre-settle refusals: upstream cooldown, verifier outage). "settlement_failed" +# is deliberately NOT here: a failed settle result cannot prove the transfer +# did not land. +_PAID_5XX_NOT_CHARGED = "not_charged" + + +def _json_object(response: Any) -> dict[str, Any]: + try: + body = response.json() + except Exception: + return {} + return body if isinstance(body, dict) else {} + + +def _paid_5xx_proves_no_charge(response: Any) -> bool: + return _json_object(response).get("payment_status") == _PAID_5XX_NOT_CHARGED + + +def _paid_5xx_reports_payment(response: Any) -> bool: + """Does a paid leg's 5xx itself say the payment did or may have moved? + + Then it is surfaced as is, never re-sent: the resend could only come back + as a replay refusal, and the receipt in this answer would be lost. + """ + body = _json_object(response) + status = body.get("payment_status") + if isinstance(status, str) and status and status != _PAID_5XX_NOT_CHARGED: + return True + if body.get("tx_hash") or body.get("code") == "CHARGED_UPSTREAM_FAILED": + return True + headers = getattr(response, "headers", None) or {} + return bool(headers.get("X-Payment-Receipt") or headers.get("PAYMENT-RESPONSE")) + + +def _refused_after_paid_5xx(response: Any, first_5xx: Any | None) -> PaymentError: + """The error for a paid leg's 402, given the 5xx (if any) that preceded it. + + Without a prior 5xx this is exactly :func:`build_payment_rejected_error`. + After one, the 402 refused a RESEND of a signature the gateway already + received once, so the error says the first attempt may have been charged, + names that attempt's gateway request id and receipt, and is tagged + settled so no retry loop signs again. + """ + exc = build_payment_rejected_error(response) + if first_5xx is None or _paid_5xx_proves_no_charge(first_5xx): + return exc + detail: dict[str, Any] = dict(exc.response) if isinstance(exc.response, dict) else {} + first_body = _json_object(first_5xx) + prior: dict[str, Any] = {"status_code": first_5xx.status_code} + for key in ("code", "payment_status", "tx_hash"): + if isinstance(first_body.get(key), str): + prior[key] = first_body[key] + headers = getattr(first_5xx, "headers", None) or {} + receipt = headers.get("X-Payment-Receipt") + if receipt and "tx_hash" not in prior: + prior["tx_hash"] = receipt + request_id = headers.get(_GATEWAY_REQUEST_ID_HEADER) + if request_id: + prior["gateway_request_id"] = request_id + detail["prior_attempt"] = prior + where = f" (gateway request id {request_id})" if request_id else "" + tx = f" tx {prior['tx_hash']}." if prior.get("tx_hash") else "" + message = ( + f"{exc} This signed payment first got HTTP {first_5xx.status_code}{where}, " + "and that attempt may already have been charged, so no new payment was " + f"signed.{tx} Check your wallet before paying for this request again." + ) + wrapped = type(exc)(message, status_code=exc.status_code, response=detail) + setattr(wrapped, _SETTLED_ATTR, True) + return wrapped + + def _get_user_agent() -> str: from . import __version__ @@ -462,8 +556,46 @@ def _warn_if_x402_ignores_quoted_blockhash() -> None: if not hasattr(_svm_exact_client, "resolve_blockhash"): logger.warning( "The installed x402 package signs Solana payments on its own blockhash " - "and ignores the one the 402 quotes. Upgrade: pip install -U 'x402[svm]>=2.17.0'" + "and ignores the one the 402 quotes. Upgrade: pip install -U 'blockrun-llm[solana]'" ) + # Not `pip install -U 'x402[svm]>=2.17.0'`: resolved without this + # package, pip takes the latest x402 (2.28 pulls solders 0.29), outside + # what our `solana>=0.36,<0.40` pin co-installs with (x402 2.26, + # solders 0.27), and leaves a mixed install. + + +# Fields of a 402's `extra` that describe the blockhash it QUOTES. A signer on +# x402 >= 2.17 signs on `recentBlockhash` whenever it is present. +_QUOTED_BLOCKHASH_KEYS = ("recentBlockhash", "lastValidBlockHeight", "blockrunCreateProof") + + +def _without_quoted_blockhash(payment_required: Any) -> Any: + """A copy of a challenge with its quoted blockhash removed. + + For re-signing a challenge that was issued earlier (the media poll's + keep-fresh re-sign): the quote ages with the challenge, so re-signing it + reproduces the same aging hash. Without the quote the signer fetches the + latest one, which is what the re-sign is for. Amount, payTo and the rest of + `extra` are untouched. Anything not shaped like a challenge is returned as is. + """ + accepts = getattr(payment_required, "accepts", None) + if not isinstance(accepts, list): + return payment_required + stripped: list[Any] = [] + changed = False + for requirement in accepts: + extra = getattr(requirement, "extra", None) + if isinstance(extra, dict) and any(k in extra for k in _QUOTED_BLOCKHASH_KEYS): + requirement = requirement.model_copy( + update={ + "extra": {k: v for k, v in extra.items() if k not in _QUOTED_BLOCKHASH_KEYS} + } + ) + changed = True + stripped.append(requirement) + if not changed: + return payment_required + return payment_required.model_copy(update={"accepts": stripped}) # CAIP-2 ids of the Solana clusters (genesis-hash prefixes; fixed by the chain). @@ -1458,6 +1590,7 @@ def _stream_once( # ----- Phase 2: stream with PAYMENT-SIGNATURE ----- assert payment_headers is not None + first_5xx: httpx.Response | None = None try: for attempt in range(len(backoffs) + 1): with self._client.stream( @@ -1479,8 +1612,14 @@ def _stream_once( # Account rail: a 402 is the account being out of credit, not a # challenge to sign. Nothing here can sign, so say so plainly. raise_for_api_key_402(resp2, self.api_key) - raise build_payment_rejected_error(resp2) - if resp2.status_code in self._STREAM_5XX_STATUSES and attempt < len(backoffs): + raise _refused_after_paid_5xx(resp2, first_5xx) + if ( + resp2.status_code in self._STREAM_5XX_STATUSES + and attempt < len(backoffs) + and not _paid_5xx_reports_payment(resp2) + ): + if first_5xx is None: + first_5xx = resp2 import time time.sleep(backoffs[attempt]) @@ -1742,7 +1881,12 @@ def _handle_payment_and_retry( retry_response = self._client.post( url, json=body, headers=payment_headers, timeout=eff_timeout ) - if retry_response.status_code in (502, 503): + # Re-send the SAME signature once; see _refused_after_paid_5xx. + first_5xx: httpx.Response | None = None + if retry_response.status_code in (502, 503) and not _paid_5xx_reports_payment( + retry_response + ): + first_5xx = retry_response import time time.sleep(1) @@ -1754,7 +1898,7 @@ def _handle_payment_and_retry( # Account rail: a 402 is the account being out of credit, not a # challenge to sign. Nothing here can sign, so say so plainly. raise_for_api_key_402(retry_response, self.api_key) - raise build_payment_rejected_error(retry_response) + raise _refused_after_paid_5xx(retry_response, first_5xx) if not retry_response.is_success: try: @@ -1921,7 +2065,12 @@ def _handle_payment_and_retry_raw( retry_response = self._client.post( url, json=body, headers=payment_headers, timeout=eff_timeout ) - if retry_response.status_code in (502, 503): + # Re-send the SAME signature once; see _refused_after_paid_5xx. + first_5xx: httpx.Response | None = None + if retry_response.status_code in (502, 503) and not _paid_5xx_reports_payment( + retry_response + ): + first_5xx = retry_response import time time.sleep(1) @@ -1933,7 +2082,7 @@ def _handle_payment_and_retry_raw( # Account rail: a 402 is the account being out of credit, not a # challenge to sign. Nothing here can sign, so say so plainly. raise_for_api_key_402(retry_response, self.api_key) - raise build_payment_rejected_error(retry_response) + raise _refused_after_paid_5xx(retry_response, first_5xx) if not retry_response.is_success: try: @@ -2072,7 +2221,12 @@ def _handle_get_payment_and_retry( retry_response = self._client.get( url, params=params, headers=payment_headers, timeout=eff_timeout ) - if retry_response.status_code in (502, 503): + # Re-send the SAME signature once; see _refused_after_paid_5xx. + first_5xx: httpx.Response | None = None + if retry_response.status_code in (502, 503) and not _paid_5xx_reports_payment( + retry_response + ): + first_5xx = retry_response import time time.sleep(1) @@ -2084,7 +2238,7 @@ def _handle_get_payment_and_retry( # Account rail: a 402 is the account being out of credit, not a # challenge to sign. Nothing here can sign, so say so plainly. raise_for_api_key_402(retry_response, self.api_key) - raise build_payment_rejected_error(retry_response) + raise _refused_after_paid_5xx(retry_response, first_5xx) if not retry_response.is_success: try: @@ -2277,7 +2431,10 @@ def _request_image_with_payment( submit_resp = self._client.post( url, json=body, headers=paid_headers, timeout=eff_timeout ) - if submit_resp.status_code in (502, 503): + # Re-send the SAME signature once; see _refused_after_paid_5xx. + first_5xx: httpx.Response | None = None + if submit_resp.status_code in (502, 503) and not _paid_5xx_reports_payment(submit_resp): + first_5xx = submit_resp _time.sleep(1) submit_resp = self._client.post( url, json=body, headers=paid_headers, timeout=eff_timeout @@ -2287,7 +2444,7 @@ def _request_image_with_payment( # Account rail: a 402 is the account being out of credit, not a # challenge to sign. Nothing here can sign, so say so plainly. raise_for_api_key_402(submit_resp, self.api_key) - raise build_payment_rejected_error(submit_resp) + raise _refused_after_paid_5xx(submit_resp, first_5xx) if submit_resp.status_code == 200: # Fast path — image was produced inline. @@ -2481,7 +2638,7 @@ def _fall_back_to_payment_polls(reason: str) -> None: if payment_required is not None: try: poll_headers["PAYMENT-SIGNATURE"] = encode_payment_signature_header( - self._sign_payment(payment_required) + self._sign_payment(_without_quoted_blockhash(payment_required)) ) last_resign_at = _time.monotonic() except Exception: @@ -2523,7 +2680,9 @@ def _fall_back_to_payment_polls(reason: str) -> None: # Keep the settlement blockhash fresh (poll-based media path only, # gated on max_resigns). Re-sign the ORIGINAL challenge — same - # amount/pay_to, only a freshly-fetched blockhash — so that whenever + # amount/pay_to, only a freshly-fetched blockhash (the challenge's quoted + # blockhash is dropped first: x402 >= 2.17 signs on a quote, and this + # one is as old as the challenge) — so that whenever # upstream flips to "completed" the signature is # None: and _time.monotonic() - last_resign_at >= self.MEDIA_RESIGN_FRESH_SECONDS ): try: - fresh_payload = self._sign_payment(payment_required) + fresh_payload = self._sign_payment( + _without_quoted_blockhash(payment_required) + ) poll_headers["PAYMENT-SIGNATURE"] = encode_payment_signature_header( fresh_payload ) @@ -4592,6 +4753,7 @@ async def _stream_once( # ----- Phase 2: stream with PAYMENT-SIGNATURE ----- assert payment_headers is not None + first_5xx: httpx.Response | None = None try: for attempt in range(len(backoffs) + 1): async with self._client.stream( @@ -4614,8 +4776,14 @@ async def _stream_once( # Account rail: a 402 is the account being out of credit, not a # challenge to sign. Nothing here can sign, so say so plainly. raise_for_api_key_402(resp2, self.api_key) - raise build_payment_rejected_error(resp2) - if resp2.status_code in self._STREAM_5XX_STATUSES and attempt < len(backoffs): + raise _refused_after_paid_5xx(resp2, first_5xx) + if ( + resp2.status_code in self._STREAM_5XX_STATUSES + and attempt < len(backoffs) + and not _paid_5xx_reports_payment(resp2) + ): + if first_5xx is None: + first_5xx = resp2 import asyncio await asyncio.sleep(backoffs[attempt]) @@ -4743,8 +4911,11 @@ async def _sign_payment_from_response( cost_usd, ) - # Reuse the sync class's pure helper — it doesn't touch async state. - _raise_stream_error = SolanaLLMClient._raise_stream_error + # Reuse the sync class's pure helper — it doesn't touch async state. Class + # attribute access unwraps the staticmethod, so it must be re-wrapped: bare, + # every call bound `self` into `response` and raised TypeError in place of + # the gateway's error. + _raise_stream_error = staticmethod(SolanaLLMClient._raise_stream_error) async def _request_with_payment( self, endpoint: str, body: dict[str, Any], timeout: float | None = None @@ -4827,7 +4998,12 @@ async def _handle_payment_and_retry( retry_response = await self._client.post( url, json=body, headers=payment_headers, timeout=eff_timeout ) - if retry_response.status_code in (502, 503): + # Re-send the SAME signature once; see _refused_after_paid_5xx. + first_5xx: httpx.Response | None = None + if retry_response.status_code in (502, 503) and not _paid_5xx_reports_payment( + retry_response + ): + first_5xx = retry_response import asyncio await asyncio.sleep(1) @@ -4839,7 +5015,7 @@ async def _handle_payment_and_retry( # Account rail: a 402 is the account being out of credit, not a # challenge to sign. Nothing here can sign, so say so plainly. raise_for_api_key_402(retry_response, self.api_key) - raise build_payment_rejected_error(retry_response) + raise _refused_after_paid_5xx(retry_response, first_5xx) if not retry_response.is_success: try: error_body = retry_response.json() @@ -4934,13 +5110,18 @@ async def _request_with_payment_raw_once( retry_response = await self._client.post( url, json=body, headers=payment_headers, timeout=eff_timeout ) - if retry_response.status_code in (502, 503): + # Re-send the SAME signature once; see _refused_after_paid_5xx. + first_5xx: httpx.Response | None = None + if retry_response.status_code in (502, 503) and not _paid_5xx_reports_payment( + retry_response + ): + first_5xx = retry_response await asyncio.sleep(1) retry_response = await self._client.post( url, json=body, headers=payment_headers, timeout=eff_timeout ) if retry_response.status_code == 402: - raise build_payment_rejected_error(retry_response) + raise _refused_after_paid_5xx(retry_response, first_5xx) if not retry_response.is_success: try: error_body = retry_response.json() @@ -5040,13 +5221,18 @@ async def _get_with_payment_raw_once( retry_response = await self._client.get( url, params=params, headers=payment_headers, timeout=eff_timeout ) - if retry_response.status_code in (502, 503): + # Re-send the SAME signature once; see _refused_after_paid_5xx. + first_5xx: httpx.Response | None = None + if retry_response.status_code in (502, 503) and not _paid_5xx_reports_payment( + retry_response + ): + first_5xx = retry_response await asyncio.sleep(1) retry_response = await self._client.get( url, params=params, headers=payment_headers, timeout=eff_timeout ) if retry_response.status_code == 402: - raise build_payment_rejected_error(retry_response) + raise _refused_after_paid_5xx(retry_response, first_5xx) if not retry_response.is_success: try: error_body = retry_response.json() @@ -5867,7 +6053,10 @@ async def _request_image_with_payment( submit_resp = await self._client.post( url, json=body, headers=payment_headers, timeout=eff_timeout ) - if submit_resp.status_code in (502, 503): + # Re-send the SAME signature once; see _refused_after_paid_5xx. + first_5xx: httpx.Response | None = None + if submit_resp.status_code in (502, 503) and not _paid_5xx_reports_payment(submit_resp): + first_5xx = submit_resp await asyncio.sleep(1) submit_resp = await self._client.post( url, json=body, headers=payment_headers, timeout=eff_timeout @@ -5877,7 +6066,7 @@ async def _request_image_with_payment( # Account rail: a 402 is the account being out of credit, not a # challenge to sign. Nothing here can sign, so say so plainly. raise_for_api_key_402(submit_resp, self.api_key) - raise build_payment_rejected_error(submit_resp) + raise _refused_after_paid_5xx(submit_resp, first_5xx) if submit_resp.status_code == 200: # Fast path — image produced inline. @@ -6074,7 +6263,7 @@ async def _fall_back_to_payment_polls(reason: str) -> None: if payment_required is not None: try: poll_headers["PAYMENT-SIGNATURE"] = encode_payment_signature_header( - await self._sign_payment(payment_required) + await self._sign_payment(_without_quoted_blockhash(payment_required)) ) last_resign_at = _time.monotonic() except Exception: @@ -6116,7 +6305,9 @@ async def _fall_back_to_payment_polls(reason: str) -> None: # Keep the settlement blockhash fresh (poll-based media path only, # gated on max_resigns). Re-sign the ORIGINAL challenge — same - # amount/pay_to, only a freshly-fetched blockhash — so that whenever + # amount/pay_to, only a freshly-fetched blockhash (the challenge's quoted + # blockhash is dropped first: x402 >= 2.17 signs on a quote, and this + # one is as old as the challenge) — so that whenever # upstream flips to "completed" the signature is # None: >= SolanaLLMClient.MEDIA_RESIGN_FRESH_SECONDS ): try: - fresh_payload = await self._sign_payment(payment_required) + fresh_payload = await self._sign_payment( + _without_quoted_blockhash(payment_required) + ) poll_headers["PAYMENT-SIGNATURE"] = encode_payment_signature_header( fresh_payload ) diff --git a/pyproject.toml b/pyproject.toml index f043ec2..b3b6ff8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "blockrun-llm" -version = "1.22.2" +version = "1.22.3" description = "BlockRun SDK - Pay-per-request AI (LLM, Image, Video, Music, Voice) via x402 on Base and Solana" readme = "README.md" license = "MIT" diff --git a/tests/unit/test_solana_paid_5xx_no_resign.py b/tests/unit/test_solana_paid_5xx_no_resign.py new file mode 100644 index 0000000..a567b08 --- /dev/null +++ b/tests/unit/test_solana_paid_5xx_no_resign.py @@ -0,0 +1,377 @@ +"""A paid Solana request that answered 5xx is never paid for a second time. + +sol.blockrun.ai settles in parallel with the upstream call, so a request can be +charged and still answer 5xx (the gateway logs "CHARGED BUT REQUEST FAILED"). +The paid leg re-sends the SAME signed payment once on a 5xx, and the gateway, +still holding that payment's nonce claim, answers the resend with its bare +``402 PAYMENT_REPLAY``. Up to 1.22.2 the whole-request retry read that replay +as a concurrent nonce collision and signed a NEW payment: two settlements for +one call. These tests drive the real client and the real ``x402`` signer +against a fake gateway that settles every distinct authorization it receives, +and count the settlements. + +Guarded with importorskip: the 3.9 CI job installs the SDK without the solana +extra. +""" + +from __future__ import annotations + +import asyncio +import base64 +import json +from types import SimpleNamespace +from typing import Any, Callable + +import httpx +import pytest + +pytest.importorskip("x402") +pytest.importorskip("solders") + +from solders.hash import Hash +from solders.keypair import Keypair +from x402.mechanisms.svm.exact.client import ExactSvmScheme + +import blockrun_llm.cache as cache_mod +from blockrun_llm.client import _SETTLED_ATTR +from blockrun_llm.solana_client import ( + AsyncSolanaLLMClient, + SolanaLLMClient, + _is_safe_resign_error, +) +from blockrun_llm.types import APIError, PaymentReplayError + +TEST_BS58_KEY = ( + "433C7KFcM4y1ZEVdZYSH7wheSNAM384UcbgXEyD5FV7Q2HsQ1BwjEDx4GbBZUqPkZTVhFPyLyuZnzK8wCeAkU7wG" +) +TOKEN_PROGRAM = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" +USDC = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" +NETWORK = "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp" +REQUEST_ID = "8f1c0e1e-5a52-4d7e-9a43-4f0b1d1b2c3d" + +# sol's literal replay body (chat/completions route.ts and payment-nonce.ts). +BARE_REPLAY = { + "error": "Payment authorization already used", + "message": "This payment signature was already redeemed. Sign a new payment for each request.", + "code": "PAYMENT_REPLAY", +} +# A 5xx that says nothing about the payment (load balancer, "all providers +# exhausted" after the parallel settle). +BARE_502 = (502, {"error": "Bad gateway"}) +# sol's Fable 5.2 past-the-45s-bound answer: charged, with its receipt. +CHARGED_502 = ( + 502, + { + "error": "Upstream failed after payment", + "code": "CHARGED_UPSTREAM_FAILED", + "payment_status": "charged", + "tx_hash": "5settledTx", + "message": "Your payment settled (tx 5settledTx) before the model failed.", + }, +) +# sol's pre-settle refusal (upstream cooldown): nothing charged, header unspent. +NOT_CHARGED_503 = ( + 503, + { + "error": "Model temporarily rate-limited upstream", + "code": "UPSTREAM_RATE_LIMITED", + "payment_status": "not_charged", + }, +) + +CHAT_OK = { + "id": "c", + "object": "chat.completion", + "created": 1, + "model": "openai/gpt-5.5", + "choices": [ + {"index": 0, "message": {"role": "assistant", "content": "hi"}, "finish_reason": "stop"} + ], +} +SSE_OK = ( + b'data: {"id":"c","object":"chat.completion.chunk","created":1,"model":"m",' + b'"choices":[{"index":0,"delta":{"content":"hi"},"finish_reason":"stop"}]}\n\n' + b"data: [DONE]\n\n" +) +MESSAGES = [{"role": "user", "content": "hi"}] + + +class _FakeRpc: + def get_account_info(self, _mint: Any) -> Any: + data = bytes(44) + bytes([6]) + bytes(37) + return SimpleNamespace(value=SimpleNamespace(owner=TOKEN_PROGRAM, data=data)) + + def get_latest_blockhash(self, *_args: Any, **_kwargs: Any) -> Any: + fresh = Hash.from_bytes(bytes(Keypair().pubkey())) + return SimpleNamespace(value=SimpleNamespace(blockhash=fresh, last_valid_block_height=1)) + + +class Gateway: + """Settles every distinct authorization; the first one then fails 5xx. + + A resend of an authorization it has already seen gets ``replay``. When + ``first_claim_released`` is set the first authorization is handed back + (sol's ``releasePaymentNonce`` on a pre-settle refusal), so its resend is + served instead of refused. + """ + + def __init__( + self, + first: tuple[int, dict[str, Any]], + *, + stream: bool = False, + replay: dict[str, Any] = BARE_REPLAY, + first_settles: bool = True, + first_claim_released: bool = False, + ) -> None: + self.first = first + self.stream = stream + self.replay = replay + self.first_settles = first_settles + self.first_claim_released = first_claim_released + self.signed: list[str] = [] + self.settled: list[str] = [] + self.paid_requests = 0 + + def __call__(self, request: httpx.Request) -> httpx.Response: + header = request.headers.get("PAYMENT-SIGNATURE") + if header is None: + return self._challenge(request) + self.paid_requests += 1 + if header in self.signed: + if self.first_claim_released and header == self.signed[0]: + self.first_claim_released = False + return self._served(request, header) + return httpx.Response(402, json=self.replay) + self.signed.append(header) + if len(self.signed) == 1: + if self.first_settles: + self.settled.append(header) + status, body = self.first + return httpx.Response( + status, json=body, headers={"x-blockrun-gateway-request-id": REQUEST_ID} + ) + return self._served(request, header) + + def _served(self, request: httpx.Request, header: str) -> httpx.Response: + if header not in self.settled: + self.settled.append(header) + if self.stream: + return httpx.Response( + 200, content=SSE_OK, headers={"content-type": "text/event-stream"} + ) + if request.url.path.endswith("/chat/completions"): + return httpx.Response(200, json=CHAT_OK) + return httpx.Response(200, json={"ok": True}) + + def _challenge(self, request: httpx.Request) -> httpx.Response: + challenge = { + "x402Version": 2, + "accepts": [ + { + "scheme": "exact", + "network": NETWORK, + "amount": "10000", + "asset": USDC, + "payTo": str(Keypair().pubkey()), + "maxTimeoutSeconds": 300, + "extra": {"feePayer": str(Keypair().pubkey())}, + } + ], + "resource": { + "url": str(request.url), + "description": "paid call", + "mimeType": "application/json", + }, + "error": "Payment Required", + } + return httpx.Response(402, json=challenge) + + +@pytest.fixture(autouse=True) +def _isolate(monkeypatch: pytest.MonkeyPatch, tmp_path: Any) -> None: + monkeypatch.setattr(cache_mod, "CACHE_DIR", tmp_path / "cache") + monkeypatch.setattr(cache_mod, "DATA_DIR", tmp_path / "data") + monkeypatch.setattr(cache_mod, "COST_LOG_PATH", tmp_path / "cost_log.jsonl") + monkeypatch.delenv("BLOCKRUN_API_KEY", raising=False) + monkeypatch.setattr(ExactSvmScheme, "_get_client", lambda self, network: _FakeRpc()) + monkeypatch.setattr("time.sleep", lambda _s: None) + real_sleep = asyncio.sleep + + async def _no_wait(_s: float, *a: Any, **k: Any) -> None: + await real_sleep(0) + + monkeypatch.setattr(asyncio, "sleep", _no_wait) + + +def _sync(gw: Gateway) -> SolanaLLMClient: + client = SolanaLLMClient(private_key=TEST_BS58_KEY) + client._client = httpx.Client(transport=httpx.MockTransport(gw)) + return client + + +def _async(gw: Gateway) -> AsyncSolanaLLMClient: + client = AsyncSolanaLLMClient(private_key=TEST_BS58_KEY) + client._client = httpx.AsyncClient(transport=httpx.MockTransport(gw)) + return client + + +async def _drain(agen: Any) -> list[Any]: + return [chunk async for chunk in agen] + + +# (id, stream?, sync call, async call). Every whole-request retry loop the +# client has: non-stream chat, stream chat, raw POST, raw GET. +SITES: list[tuple[str, bool, Callable[[Any], Any], Callable[[Any], Any]]] = [ + ( + "chat", + False, + lambda c: c.chat_completion("openai/gpt-5.5", MESSAGES), + lambda c: c.chat_completion("openai/gpt-5.5", MESSAGES), + ), + ( + "stream", + True, + lambda c: list(c.chat_completion_stream("openai/gpt-5.5", MESSAGES)), + lambda c: _drain(c.chat_completion_stream("openai/gpt-5.5", MESSAGES)), + ), + ( + "raw-post", + False, + lambda c: c.modal_sandbox_exec("sb-1", ["date"]), + lambda c: c.modal_sandbox_exec("sb-1", ["date"]), + ), + ( + "raw-get", + False, + lambda c: c._get_with_payment_raw("/v1/pm/markets"), + lambda c: c._get_with_payment_raw("/v1/pm/markets"), + ), +] +SITE_IDS = [s[0] for s in SITES] + + +def _run(is_async: bool, gw: Gateway, site: tuple) -> Any: + _, _, sync_call, async_call = site + if is_async: + return asyncio.run(async_call(_async(gw))) + return sync_call(_sync(gw)) + + +@pytest.mark.parametrize("is_async", [False, True], ids=["sync", "async"]) +@pytest.mark.parametrize("site", SITES, ids=SITE_IDS) +def test_replay_after_a_settled_5xx_is_not_paid_again(is_async: bool, site: tuple) -> None: + """The double charge: settled, 502, resend refused as a bare replay.""" + gw = Gateway(BARE_502, stream=site[1]) + with pytest.raises(PaymentReplayError) as caught: + _run(is_async, gw, site) + assert len(gw.signed) == 1, "a second payment was signed for one call" + assert len(gw.settled) == 1 + assert gw.paid_requests == 2 # the paid POST and its one same-signature resend + exc = caught.value + assert getattr(exc, _SETTLED_ATTR, False) is True + assert _is_safe_resign_error(exc) is False + assert exc.response["prior_attempt"] == { + "status_code": 502, + "gateway_request_id": REQUEST_ID, + } + assert REQUEST_ID in str(exc) + assert "may already have been charged" in str(exc) + + +@pytest.mark.parametrize("is_async", [False, True], ids=["sync", "async"]) +@pytest.mark.parametrize("site", SITES, ids=SITE_IDS) +def test_any_refusal_of_the_resend_is_terminal(is_async: bool, site: tuple) -> None: + """A verify rejection of the RESEND is pre-broadcast for the resend only. + The first attempt may have landed, so it is not a licence to re-sign.""" + expired = { + "error": "Payment verification failed", + "code": "PAYMENT_INVALID", + "reason": "expired_signature", + } + gw = Gateway(BARE_502, stream=site[1], replay=expired) + with pytest.raises(Exception) as caught: + _run(is_async, gw, site) + assert len(gw.signed) == 1 + assert _is_safe_resign_error(caught.value) is False + + +@pytest.mark.parametrize("is_async", [False, True], ids=["sync", "async"]) +@pytest.mark.parametrize("site", SITES, ids=SITE_IDS) +def test_a_5xx_that_reports_the_charge_is_surfaced_not_resent(is_async: bool, site: tuple) -> None: + """The gateway's own receipt reaches the caller; nothing is resent.""" + gw = Gateway(CHARGED_502, stream=site[1]) + with pytest.raises(APIError) as caught: + _run(is_async, gw, site) + assert len(gw.signed) == 1 + assert gw.paid_requests == 1 + assert caught.value.status_code == 502 + assert caught.value.response["tx_hash"] == "5settledTx" + assert caught.value.response["payment_status"] == "charged" + + +@pytest.mark.parametrize("is_async", [False, True], ids=["sync", "async"]) +@pytest.mark.parametrize("site", SITES, ids=SITE_IDS) +def test_a_transient_5xx_the_gateway_never_charged_still_recovers( + is_async: bool, site: tuple +) -> None: + """Honest retry kept: the same signature resent once, served, paid once.""" + gw = Gateway(BARE_502, stream=site[1], first_settles=False, first_claim_released=True) + _run(is_async, gw, site) + assert len(gw.signed) == 1 + assert len(gw.settled) == 1 + assert gw.paid_requests == 2 + + +@pytest.mark.parametrize("is_async", [False, True], ids=["sync", "async"]) +@pytest.mark.parametrize("site", SITES, ids=SITE_IDS) +def test_a_proven_not_charged_5xx_keeps_the_collision_resign(is_async: bool, site: tuple) -> None: + """`payment_status: not_charged` proves the first attempt moved nothing, so + a replay of the resend is the ordinary concurrent collision and is re-signed.""" + gw = Gateway(NOT_CHARGED_503, stream=site[1], first_settles=False) + _run(is_async, gw, site) + assert len(gw.signed) == 2 + assert len(gw.settled) == 1 + + +def test_a_bare_replay_with_no_prior_5xx_is_still_resigned() -> None: + """The documented concurrent-collision re-sign is untouched.""" + state = {"n": 0} + inner = Gateway(BARE_502) + + def handler(request: httpx.Request) -> httpx.Response: + if request.headers.get("PAYMENT-SIGNATURE") is None: + return inner._challenge(request) + state["n"] += 1 + if state["n"] == 1: + return httpx.Response(402, json=BARE_REPLAY) + return httpx.Response(200, json={"ok": True}) + + client = SolanaLLMClient(private_key=TEST_BS58_KEY) + client._client = httpx.Client(transport=httpx.MockTransport(handler)) + assert client.modal_sandbox_exec("sb-1", ["date"]) == {"ok": True} + assert state["n"] == 2 + + +async def test_async_stream_error_after_payment_is_an_api_error() -> None: + """The async client aliased the sync staticmethod `_raise_stream_error` as a + plain function, so every async stream error raised TypeError instead.""" + + def handler(request: httpx.Request) -> httpx.Response: + if request.headers.get("PAYMENT-SIGNATURE") is None: + return Gateway(BARE_502)._challenge(request) + return httpx.Response(400, json={"error": "Bad request", "code": "INVALID_REQUEST"}) + + client = AsyncSolanaLLMClient(private_key=TEST_BS58_KEY) + client._client = httpx.AsyncClient(transport=httpx.MockTransport(handler)) + with pytest.raises(APIError) as caught: + await _drain(client.chat_completion_stream("openai/gpt-5.5", MESSAGES)) + assert caught.value.status_code == 400 + + +def test_signed_payload_decodes() -> None: + """Sanity for the harness: the client really signs distinct payments.""" + gw = Gateway(NOT_CHARGED_503, first_settles=False) + _sync(gw).modal_sandbox_exec("sb-1", ["date"]) + decoded = [json.loads(base64.b64decode(h)) for h in gw.signed] + assert len({d["payload"]["transaction"] for d in decoded}) == 2 diff --git a/tests/unit/test_solana_quoted_blockhash.py b/tests/unit/test_solana_quoted_blockhash.py index 93a4429..eaf8674 100644 --- a/tests/unit/test_solana_quoted_blockhash.py +++ b/tests/unit/test_solana_quoted_blockhash.py @@ -235,3 +235,122 @@ def test_no_warning_on_a_current_x402(monkeypatch, caplog): with caplog.at_level(logging.WARNING, logger=solana_client_mod.logger.name): solana_client_mod._warn_if_x402_ignores_quoted_blockhash() assert not [r for r in caplog.records if "ignores the one the 402 quotes" in r.getMessage()] + + +# --- Media poll re-signs never reuse the challenge's quote ------------------- +# +# A video's payment polls keep the signature fresh by re-signing the submit +# challenge every MEDIA_RESIGN_FRESH_SECONDS. sol quotes `recentBlockhash` on +# every 402 (x402-solana.ts create402ResponseParts), and x402 >= 2.17 signs on +# a quote, so re-signing the original challenge reproduced the same aging hash +# every time: the "fresh" signature died with the submit's blockhash. + + +def _video_job_handler(extra: dict[str, Any], poll_hashes: list[str], *, in_progress: int): + state = {"polls": 0} + + def handler(request: httpx.Request) -> httpx.Response: + header = request.headers.get("PAYMENT-SIGNATURE") + if request.method == "POST": + if header is None: + return _modal_402(extra) + return httpx.Response( + 202, + json={ + "id": "JOB", + "poll_url": "/api/v1/videos/generations/JOB", + "status": "queued", + }, + ) + assert header is not None, "payment-signature polling expected" + poll_hashes.append(_signed_blockhash(json.loads(base64.b64decode(header)))) + state["polls"] += 1 + if state["polls"] <= in_progress: + return httpx.Response(202, json={"status": "in_progress"}) + return httpx.Response( + 200, + json={"status": "completed", "created": 1, "data": [{"url": "https://cdn/v.mp4"}]}, + ) + + return handler + + +_POLL_KW: dict[str, Any] = { + "poll_budget_seconds": 5.0, + "poll_interval_seconds": 0.001, + "max_resigns": 2, + "label": "Video generation", +} + + +def test_poll_resign_signs_on_a_fresh_blockhash_not_the_quote(harness, monkeypatch): + client, rpc, _payments, _state = harness + quoted = _new_hash() + extra = {"feePayer": FEE_PAYER, "recentBlockhash": quoted, "lastValidBlockHeight": "1"} + poll_hashes: list[str] = [] + client._client = httpx.Client( + transport=httpx.MockTransport(_video_job_handler(extra, poll_hashes, in_progress=2)) + ) + client._poll_keypair = None # a gateway without poll proofs: payment polls + monkeypatch.setattr(SolanaLLMClient, "MEDIA_RESIGN_FRESH_SECONDS", 0.0) + + data = client._request_image_with_payment( + "/v1/videos/generations", {"model": "xai/grok-imagine-video", "prompt": "a cat"}, **_POLL_KW + ) + + assert data["data"][0]["url"] == "https://cdn/v.mp4" + assert len(poll_hashes) == 3 + # Every poll carried a re-signed payment, none of it on the submit's quote. + assert quoted not in poll_hashes, poll_hashes + assert set(poll_hashes) == {rpc.blockhash} + + +async def test_async_poll_resign_signs_on_a_fresh_blockhash_not_the_quote(monkeypatch, tmp_path): + monkeypatch.setattr(cache_mod, "CACHE_DIR", tmp_path / "cache") + monkeypatch.setattr(cache_mod, "DATA_DIR", tmp_path / "data") + monkeypatch.setattr(cache_mod, "COST_LOG_PATH", tmp_path / "cost_log.jsonl") + rpc = _FakeRpc(_new_hash()) + monkeypatch.setattr(ExactSvmScheme, "_get_client", lambda self, network: rpc) + monkeypatch.setattr(SolanaLLMClient, "MEDIA_RESIGN_FRESH_SECONDS", 0.0) + quoted = _new_hash() + extra = {"feePayer": FEE_PAYER, "recentBlockhash": quoted, "lastValidBlockHeight": "1"} + poll_hashes: list[str] = [] + + client = AsyncSolanaLLMClient(private_key=TEST_BS58_KEY) + client._client = httpx.AsyncClient( + transport=httpx.MockTransport(_video_job_handler(extra, poll_hashes, in_progress=2)) + ) + client._poll_keypair = None + + await client._request_image_with_payment( + "/v1/videos/generations", {"model": "xai/grok-imagine-video", "prompt": "a cat"}, **_POLL_KW + ) + + assert len(poll_hashes) == 3 + assert quoted not in poll_hashes, poll_hashes + assert set(poll_hashes) == {rpc.blockhash} + + +def test_stripping_the_quote_keeps_the_payment_terms(): + from x402.http.utils import decode_payment_required_header + + response = _modal_402( + { + "feePayer": FEE_PAYER, + "recentBlockhash": _new_hash(), + "lastValidBlockHeight": "9", + "blockrunCreateProof": "p", + } + ) + challenge = decode_payment_required_header(base64.b64encode(response.content).decode()) + stripped = solana_client_mod._without_quoted_blockhash(challenge) + original, after = challenge.accepts[0], stripped.accepts[0] + assert after.extra == {"feePayer": FEE_PAYER} + assert (after.amount, after.pay_to, after.asset, after.network) == ( + original.amount, + original.pay_to, + original.asset, + original.network, + ) + # The original challenge is not mutated: the submit already signed on it. + assert "recentBlockhash" in original.extra