Skip to content
Open
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
10 changes: 5 additions & 5 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ src/

Triggered by `POST /deposits/check`. The processor returns within ~2-3 s with the in-flight deposits as a list of pending statuses with their `deposit_id` (empty when there is none); the actual sweep continues in a background task. Clients poll `GET /deposits/status/{deposit_id}`.

Every asset is claimed by balance (the request names a registered `token_id`, which fixes the source chain and the asset; without one every configured token is checked, their balances read in parallel). `claim_balance` reads the current balance and saves a `SCHEDULED` record with a random `deposit_id`. Neither the amount nor the nonce is fixed when the sweep is scheduled: the sweep decides both when it signs, taking the deposit address's current nonce under the address lock, so sweeps of one address simply run one after another. At most one unfinished sweep per (address, asset) exists, so a repeated claim reports the one in flight (or `busy` while a sweep holds the address lock). The credit waits until the sweep transaction is `finality_depth` blocks deep, so neither a reorged deposit nor a reorged sweep is ever credited. Native sweeps are paid for by the operator: a fresh gas budget is funded per claim, the swept amount leaves that budget behind, and the leftover is credited with the next claim.
Every asset is claimed by balance (the request names a registered `token_id`, which fixes the source chain and the asset; without one every configured token is checked, their balances read in parallel). `claim_balance` reads the current balance and saves a `SCHEDULED` record with a random `deposit_id`. Neither the amount nor the nonce is fixed when the sweep is scheduled: the sweep decides both when it signs, taking the deposit address's current nonce under the address lock, which it holds until its sweep transaction is mined, so sweeps of one address follow each other one mined transaction apart. At most one unfinished sweep per (address, asset) exists, so a repeated claim reports the one in flight (or `busy` while another sweep of the address is being funded, signed or mined). The credit waits until the sweep transaction is `finality_depth` blocks deep, so neither a reorged deposit nor a reorged sweep is ever credited; that wait runs without the address lock, so another asset at the address can be claimed and swept meanwhile. Native sweeps are paid for by the operator: a fresh gas budget is funded per claim, the swept amount leaves that budget behind, and the leftover is credited with the next claim.

```
Client DepositProcessor SweepEngine Sapphire
Expand Down Expand Up @@ -102,15 +102,15 @@ Key files: `services/onramp_intent.py`, `services/onramp.py`, `services/transak.
```

Concurrency:
- One `asyncio.Lock` per `(deposit_address, chain_id)` — concurrent claims and recovery passes for the same address queue.
- Every driver of a record (the original sweep task, the periodic loop) takes that lock and re-reads the record from disk before acting, so a record another driver already settled is gone and is never swept a second time.
- One `asyncio.Lock` per `(deposit_address, chain_id)`, held from gas funding until the sweep transaction is mined: concurrent claims and recovery passes for the same address queue, and the next sweep signs with the nonce after the mined one. The finality wait and the credit run without it; each deposit is credited only after its own sweep is final.
- One driver per `deposit_id` at a time: `sweep()` for a deposit another task is already driving returns at once. A record whose sweep transaction is mined (the node's receipt decides, so a sweep reorged out takes the lock again) or `SWEPT` is finished without the lock. A driver that takes the lock re-reads the record from disk first, so a record settled meanwhile is gone and is never swept a second time.
- One global lock around gas-tank nonce reads (prevents concurrent sweeps from reusing the same nonce on the gas tank).

Persistence: one JSON file per active sweep at `/data/sweep-engine/sweep_<deposit_id>.json` (matches `SweepRecord` dataclass; at most one unfinished sweep exists per asset and address, which `claim_balance` finds by scanning the records).

Recovery: the recovery loop (`SWEEP_RECOVERY_INTERVAL = 60s`, first pass at startup) calls `sweep()` for every unfinished record whose address lock is free, so recovery follows the state on disk:
Recovery: the recovery loop (`SWEEP_RECOVERY_INTERVAL = 60s`, first pass at startup) calls `sweep()` for every unfinished record (one another task is already driving returns at once), so recovery follows the state on disk:
- `SCHEDULED` / `GAS_FUNDED` → fund gas if it was not funded yet, then sign and prepare a fresh sweep.
- `SWEEP_PREPARED` → the node knows the sweep tx: wait until it is `finality_depth` blocks deep. The node does not know it: if another tx already used its nonce the tx can never be mined and the record is deleted (the funds stay at the address and the next claim sweeps them); otherwise the recorded signed tx is broadcast again (same hash, amount and nonce; a node that already has it just says so). A sweep is never re-signed, so an attempt the node merely lost cannot end up uncredited. A reverted sweep is logged for manual investigation.
- `SWEEP_PREPARED` → the node knows the sweep tx: wait without the lock until it is `finality_depth` blocks deep. The node does not know it (under the lock): if another tx used its nonce and is final, the tx can never be mined and the record is deleted (the funds stay at the address and the next claim sweeps them); otherwise the recorded signed tx is broadcast again (same hash, amount and nonce; a node that already has it just says so) and mined within 120 s or left for the next pass. If the address nonce is not the tx's own (a gap below it, or the nonce taken but not final yet), the pass ends right after the broadcast instead of holding the lock through that wait. A sweep is never re-signed, so an attempt the node merely lost cannot end up uncredited. A final reverted sweep is logged and its record deleted; the next claim sweeps the balance.
- `SWEPT` → retry `creditDeposit` (idempotent via `DepositAlreadyProcessed` revert).

Key file: `services/sweep_engine.py`.
Expand Down
Loading
Loading