Repository navigation
fix(solana): never pay twice after a paid 5xx; poll re-signs on a fresh blockhash (1.22.3) - #113
Merged
Merged
Conversation
…sh blockhash (1.22.3) A Solana paid request can be charged and still answer 502/503 (sol settles in parallel with the upstream call). The paid leg re-sent the same signature once, the gateway refused that resend with a bare 402 PAYMENT_REPLAY, and the whole-request retry read the bare replay as a concurrent nonce collision and signed a new payment. Reproduced against a fake gateway on chat, streaming chat, raw POST and raw GET, sync and async: 2 settlements for one call. - A 402 that refuses a resend after a 5xx is tagged settled, never re-signed, and carries the first attempt's status, gateway request id and receipt under response["prior_attempt"]. Only a 5xx with payment_status "not_charged" keeps the collision re-sign. - A 5xx that reports the payment (payment_status, tx_hash, CHARGED_UPSTREAM_FAILED, receipt header) is raised as is, not resent. - The same-signature resend stays: one signature settles at most once. - Media poll keep-fresh re-signs drop the challenge's quoted blockhash; on x402>=2.17 they reused the submit's aging quote. - AsyncSolanaLLMClient streaming raised TypeError on every stream error (unwrapped staticmethod alias). - Old-x402 warning says pip install -U 'blockrun-llm[solana]'.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The double charge (real)
GLM's review of #112 (Finding 1) and #111 asked whether a Solana paid call can be charged twice. It can, on every whole-request retry loop.
402 PAYMENT_REPLAY("Sign a new payment for each request"). That body has no field saying the first attempt settled._is_safe_resign_errortreatsPAYMENT_REPLAYwith nopoll_url/job_idas pre-broadcast, so the retry loop signs a new payment and runs the call again. That payment settles too.I reproduced this against a fake gateway that settles every distinct authorization it receives. The first one answers 502 and a resend is refused as a replay. On main, every site signed 2, settled 2, and returned OK:
PaymentReplayErrormodal_sandbox_exec(sync/async)The Base client (
LLMClient/AsyncLLMClient) and the TS SDK do not have this bug. Base re-sends the same signature and then raisesPaymentReplayError, with no re-sign loop. TS Solana never re-sends after a 5xx and re-signs only on a stale-blockhash verify rejection.Fix
_SETTLED_ATTR) and is never re-signed._is_safe_resign_errornow checks that tag first. The error says the first attempt may have been charged. It also carriesresponse["prior_attempt"]: the status, thex-blockrun-gateway-request-id, andcode/payment_status/tx_hashwhen the 5xx had them. This covers every 402, not only a replay: a verify rejection of the resend is pre-broadcast only for the resend.payment_statusother thannot_charged, atx_hash,CHARGED_UPSTREAM_FAILED, or anX-Payment-Receipt/PAYMENT-RESPONSEheader.payment_status: "not_charged"5xx (sol's pre-settle refusals), and the re-sign after a bare replay with no 5xx before it (the documented collision case).Also in this PR (#111 review)
recentBlockhashon every 402, not only Modal create. I checked a live unpaidPOST /api/v1/videos/generationsand it quotes one. On x402>=2.17, re-signing the submit challenge signed on that same aging hash each time. Poll re-signs (proactive and fall-back-to-payment-polls) now sign a copy withrecentBlockhash/lastValidBlockHeight/blockrunCreateProofremoved, so x402 fetches the latest hash. Amount and payTo are unchanged. On sol today, video polls use ownership proofs and pay against a fresh completion challenge. So this mainly affected the payment-poll fallback, music, and gateways without poll proofs.AsyncSolanaLLMClientstreaming raisedTypeErroron every stream error. This includes a paid 4xx and exhausted 5xx retries. The sync class's staticmethod was aliased unwrapped, soselfwas bound intoresponse. Now wrapped withstaticmethod(...).pip install -U 'blockrun-llm[solana]'.x402[svm]>=2.17.0resolved alone picks x402 2.28 + solders 0.29, which does not co-install with oursolana<0.40pin (x402 2.26 + solders 0.27).pip install --dry-run 'blockrun-llm[solana]'fails on 3.9. It also failed on 1.22.0 (x402>=2.0.0): no x402 2.x release supports 3.9, so this is not a fix(solana): sign on the 402's quoted blockhash (x402[svm]>=2.17.0), 1.22.1 #111 regression. The base package resolves on 3.9. CI's 3.9 leg already installs without the extra. No change here.Tests
tests/unit/test_solana_paid_5xx_no_resign.py(40 parametrized): real client, real x402 signer, fake RPC and gateway. It counts signed vs settled payments for 4 sites × sync/async across five scenarios:not_charged5xx keeping the collision re-sign (control)It also covers the async stream
TypeErrorand a no-5xx bare replay that is still re-signed (control).tests/unit/test_solana_quoted_blockhash.py(+3): sync and async video payment polls sign on the RPC hash, never the quote. Stripping keeps amount, payTo, asset and network, and does not mutate the original challenge.Revert check: on origin/main, 28 of the new tests fail ("DID NOT RAISE": it paid twice and returned; the poll test's polls all carried the quoted hash). The controls pass on both. Mutations: removing the settled-tag check fails 16; removing the report-payment checks fails 8.
pytest(full): 1569 passed, 15 skipped (main: 1523).black --check .,ruff check .are clean.mypy blockrun_llmgives 248 errors, the same as main.Patch bump 1.22.2 → 1.22.3. Not published.