Skip to content

feat: release the deposit address once its sweep is mined - #376

Open
rube-de wants to merge 4 commits into
masterfrom
rube/feature/373-same-address-deposits
Open

rube-de wants to merge 4 commits into
masterfrom
rube/feature/373-same-address-deposits

Conversation

@rube-de

@rube-de rube-de commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Closes #373.

Summary

The address lock now covers funding, signing and the wait for the sweep to be mined (capped at 120 s), not the finality wait and credit. A second asset at the same address sweeps one block later instead of one finality wait later.

 sweep(deposit_id)
+  return if deposit_id is already being driven      # replaces recovery's lock filter (#299)
+  if sweep tx not mined:
     with address lock:
       fund gas, sign
-      broadcast, wait until final, credit
+      broadcast, wait until mined
+  wait until final, credit                          # no lock

Two guards against failures the shorter lock makes likelier (in #373's proposal):

  • Balance is read between two equal nonce reads, so a late-mined sweep can't leave the next one signed with a pre-fee balance.
  • A resent sweep whose nonce isn't next is broadcast without the 120 s receipt wait, so recovery doesn't hold the address every pass.

Also corrects two stale docs: a nonexistent PENDING state, and reverted sweeps being "logged for investigation" (the record is deleted).

Evidence

Anvil, Ethereum settings (12 s blocks, depth 32):

Before After
Single deposit → credit 409.7 s 407.9 s
ETH + USDC together, USDC credited 816.8 s 432.9 s
Second asset invisible (BUSY) 410.1 s 24.2 s
Address lock held 409.6 s 24.1 s

Mocked-chain tests cover overlapping finality waits, one driver per deposit, lock-free restart, resend after reorg, the 120 s release and both guards. Full pytest, ruff, mypy and the OpenAPI check pass.

Merge Danger

Door: two-way. No new state or record format.

Blast Radius: deposits. Known limitations (reorg after release, lagging RPC nonce) are in #373.

- Hold the per-address lock from gas funding until the sweep tx is mined (120 s receipt wait);
  run the finality wait and the credit outside it, so another asset at the address overlaps.
- Drive each deposit_id once (_driving) instead of skipping locked addresses in recovery;
  a mined sweep resumes without the lock.
- Read the balance between two equal nonce reads before signing.
- Broadcast a resent sweep whose nonce is not next without waiting for its receipt.
- Share one fake chain between the sweep engine and recovery tests.

Fixes #373
@netlify

netlify Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for oasisprotocol-privana canceled.

Name Link
🔨 Latest commit f1d0a77
🔍 Latest deploy log https://app.netlify.com/projects/oasisprotocol-privana/deploys/6b084d77ad53fc946458a7c4

- The sweep mines between the receipt lookup and the latest-nonce read; it must go on
  to finality and be credited instead of failing as 'nonce not next'.
- Remove the recovery test for a second asset during finality (same path as the claim test).
- Drop the nonce-taken half of the 120 s release test (covered by the stale-nonce test).
- Run overlapping-finality, nonce-not-next and one-driver tests on one case each where
  the extra cases took the same code path.
- Restore the 'gap below the nonce' case of the nonce-not-next resend test; the
  nonce-taken case alone passes if the check is narrowed to '>'.
- Move the ERC20 full-cycle and L1-fee tests onto FakeChain and drop the
  _receipts_by_hash helper that duplicated it.
@rube-de
rube-de marked this pull request as ready for review November 26, 2026 15:50
Copilot AI balanced review requested due to automatic review settings November 26, 2026 15:50

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The change is blocked because review findings identify moderate unresolved error-handling gaps in src/services/sweep_engine.py where _get_receipt() doesn’t handle transient get_transaction_receipt() RPC failures, potentially causing sweeps to fail instead of deferring and retrying.

1 open issue
What changed in this PR

Reworks sweep execution so the per-address lock is held only through funding/signing and until the sweep transaction is mined (or receipt wait timeout), then releases the lock while finality waiting and crediting proceed, improving same-address multi-asset deposit throughput.

Changes:

  • Refactors sweep flow to release the address lock after the sweep tx is mined, and adds a per-deposit_id in-process driver guard.
  • Adds nonce/balance and resend safeguards to reduce lock holding and avoid signing with stale assumptions.
  • Expands mocked-chain test coverage via a FakeChain harness and updates documentation to reflect current states/behavior.
File Description
test/​py/​test_sweep_recovery.py Adds recovery-focused tests to validate single-driver behavior per deposit and correct lock-free resumption through finality and crediting.
test/​py/​test_sweep_engine.py Updates and expands sweep-engine tests (using the fake chain) to cover overlapping sweeps, resend behavior, and lock-release timing.
test/​py/​fake_chain.py Introduces a deterministic fake EVM node model to simulate mining/finality, nonce behavior, and receipt edge cases for tests.
src/​services/​sweep_engine.py Implements mined-only address locking, deposit-level driver guarding, and updated send/finality/credit orchestration with resend/nonce safeguards.
src/​README.md Refreshes documentation to match the revised lock scope, sweep/recovery behavior, and state semantics.

Review effort: Balanced
Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.

"""The tx receipt, or None while the node knows of no mined tx with this hash."""
try:
return await w3.eth.get_transaction_receipt(tx_hash)
except TransactionNotFound:

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Same-address deposits: start the next sweep once the previous one is mined, not credited

2 participants