Settlement の二重送金を防ぐ原子的claim(review_passed→settling etagロック) - #4
Merged
Merged
Conversation
…claim dogfood #1 で二重送金を観測(1000 JPYC × 2、block 40122642/643)。原因は Settlement の冪等性ガードが read-then-act(TOCTOU)で、ローカル駆動の Settlement と closed(merged) webhook の Settlement が ~1.5s 並走し、両者が「未送金」を同時に読んで 両方とも transfer したため。 修正: 送金の直前に review_passed → settling を etag 楽観ロック(既存 transitionOrder の IfMatch)で原子的に確保する。並走する2つの Settlement のうち claim に勝てるのは1つ だけで、敗者は canTransition / etag 412 で送金前に弾かれる。これで「二重送金は構造的に 不可能」が実装で担保される(ピッチ Q&A の主張を事実に)。 - order.ts: settling 状態を追加。review_passed→settling→settled、失敗時 settling→ review_passed ロールバックを許可。 - settlement.ts: 送金前の原子的 claim、転送失敗時の claim 解放。 - cosmos-fake.ts: 同状態への遷移を etag conflict として拒否(実 Cosmos の IfMatch 412 を模す)。 - settlement.test.ts: 2つの Settlement が同一 order を並走しても transfer はちょうど 1回、を検証するレーステスト追加(全31テスト通過)。
Owner
Author
💴 JPYC 送金完了 — Settled by Agentic Gig-Flow
|
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.
Closes #3
概要
Settlement の二重送金を構造的に不可能にする修正です。dogfood #1(PR #2)の
運用中に、実際に 1000 JPYC が2回送金される二重送金を観測しました(Amoy block
40122642 / 40122643)。本 PR はその根本原因を断ちます。
根本原因(dogfood #1 で観測)
Settlement の冪等性ガードが read-then-act(TOCTOU)でした。2つの Settlement
(ローカル駆動と
closed(merged)webhook)が ~1.5s 並走し、両方が「まだ未送金」を同時に読んでから、両方とも
transfer()してしまいました。txHash存在チェックは送金完了後にしか書かれないため、並走窓では効きません。
修正
送金の直前に
review_passed → settlingを etag 楽観ロック(既存transitionOrderの IfMatch)で原子的に確保します。並走する2つの Settlement のうち claim に勝てるのは
1つだけで、敗者は
canTransition/ etag 412 で送金前に弾かれます。転送失敗時はsettling → review_passedにロールバックしてリトライ可能にします。検収基準への対応
settlement.test.ts: 2つのrunSettlementが同一 order を並走しても
transferはちょうど1回、を検証tsc --noEmit全パッケージ通過)変更点
packages/shared/src/types/order.ts:settling状態を追加。review_passed→settling→settledと失敗時settling→review_passedを許可。packages/functions/src/agents/settlement.ts: 送金前の原子的 claim、失敗時の claim 解放。packages/functions/src/lib/cosmos-fake.ts: 同状態への遷移を etag conflict として拒否(実 Cosmos の IfMatch 412 を模す)。
packages/functions/src/agents/settlement.test.ts: 並走レーステスト追加。これでピッチ Q&A の「冪等性キーで二重送金は構造的に不可能」が実装で担保されます。