Skip to content

fix: require manual payout recovery after broadcast uncertainty - #5577

Closed
iamdinhthuan wants to merge 1 commit into
Scottcjn:mainfrom
iamdinhthuan:fix-payout-worker-ambiguous-recovery-5346
Closed

iamdinhthuan wants to merge 1 commit into
Scottcjn:mainfrom
iamdinhthuan:fix-payout-worker-ambiguous-recovery-5346

Conversation

@iamdinhthuan

Copy link
Copy Markdown
Contributor

Summary

  • Move startup-orphaned processing withdrawals into explicit recovery_required state without refunding balances
  • Treat post-deduction broadcast/completion failures as manual-recovery cases instead of automatically refunding
  • Expose recovery_required in payout worker stats

Fixes #5346.

Verification

  • RED: python3 -m pytest tests/test_payout_worker_production_noop.py::test_processing_withdrawal_failure_requires_manual_recovery_without_refund -q failed with assert 100 == 89 before the fix
  • RED: python3 -m pytest tests/test_payout_worker_production_noop.py::test_startup_recovery_moves_processing_withdrawals_to_manual_review_without_refund -q failed with missing method before the fix
  • GREEN: python3 -m pytest tests/test_payout_worker_production_noop.py -q -> 4 passed
  • python3 -m py_compile node/payout_worker.py tests/test_payout_worker_production_noop.py
  • git diff --check

Safety

@github-actions github-actions Bot added BCOS-L1 Beacon Certified Open Source tier BCOS-L1 (required for non-doc PRs) node Node server related tests Test suite changes labels May 17, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Welcome to RustChain! Thanks for your first pull request.

Before we review, please make sure:

  • Non-doc PRs have a BCOS-L1 or BCOS-L2 label
  • Doc-only PRs are exempt from BCOS tier labels when they only touch docs/**, *.md, or common image/PDF files
  • New code files include an SPDX license header
  • You've tested your changes against the live node

Bounty tiers: Micro (1-10 RTC) | Standard (20-50) | Major (75-100) | Critical (100-150)

A maintainer will review your PR soon. Thanks for contributing!

@strongkeep-debug strongkeep-debug 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.

I rechecked the #5346 recovery path at head 2e6651c7e4e7b3f1e449484e95e5d866c0c1f7bb and this resolves the unsafe automatic refund shape for both in-flight failures and startup-orphaned processing rows.

What I verified:

Area Result
Duplicate/overlap check #5346 is open/unassigned and search found this as the only overlapping payout_worker recovery PR
process_withdrawal() failure after deduction leaves the deducted balance in place and moves the row to recovery_required for manual chain-status review
startup recovery moves orphaned processing rows to recovery_required without refunding
worker stats exposes recovery_required so the manual queue is visible

Local validation:

git diff --check origin/main...HEAD
python -m py_compile node/payout_worker.py tests/test_payout_worker_production_noop.py
python tools/bcos_spdx_check.py --base-ref origin/main
python -m pytest tests/test_payout_worker_production_noop.py -q

Result: diff-check and py_compile were clean, SPDX check passed, and the focused pytest suite reported 4 passed.

@dicnunz dicnunz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed under Scottcjn/rustchain-bounties#73 on head 2e6651c.

Approved. The change correctly stops auto-refunding after the withdrawal has been marked processing, which is the ambiguous point where a broadcast may already have escaped. It moves post-deduction failures and startup-orphaned processing rows to recovery_required, keeps the deducted balance intact, and leaves the existing production-noop path pending before any balance change.

Validation I ran:

uv run --no-project --with pytest --with flask python -m pytest tests/test_payout_worker_production_noop.py -q
# 4 passed
python3 -m py_compile node/payout_worker.py tests/test_payout_worker_production_noop.py
git diff --check origin/main...HEAD
python3 tools/bcos_spdx_check.py --base-ref origin/main
# BCOS SPDX check: OK

The failing repository test check is the known unrelated collection failure from missing optional CI dependencies (aiohttp, flask_cors, matplotlib), not from this PR. I did not find a blocking issue in this patch.

@508704820

Copy link
Copy Markdown
Contributor

Security Review ✅ HIGH

Manual payout recovery after broadcast uncertainty. Previous code auto-refunded on broadcast failure, but post-deduction failures should NOT auto-refund — the funds may have been successfully sent. Moving to recovery_required state prevents incorrect refunds and protects against double-spend scenarios.

Reviewed by Xeophon - Solana: Lt9nERv6VHsojw15LpFeiaabuphAggzfLF9sM9UXRrZ

@ZacharyZhang-NY ZacharyZhang-NY 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.

Reviewed PR #5577 at head 2e6651c.

Approved. The payout worker now preserves deducted withdrawals after ambiguous post-processing failures by moving them to recovery_required, moves startup-orphaned processing rows to manual recovery, keeps the production no-op path pending before any deduction, and exposes recovery_required in stats.

Validation run:

  • git diff --check origin/main...HEAD -- node/payout_worker.py tests/test_payout_worker_production_noop.py
  • python -B -m py_compile node/payout_worker.py tests/test_payout_worker_production_noop.py
  • python -B -m pytest -q tests/test_payout_worker_production_noop.py --noconftest
  • python tools/bcos_spdx_check.py --base-ref origin/main

Result: diff check clean, py_compile clean, focused pytest 4 passed, BCOS SPDX check OK. No blocking issue found in this patch.

@2balmprune 2balmprune 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.

Reviewed head 2e6651c7e4e7b3f1e449484e95e5d866c0c1f7bb for the #5346 payout-worker recovery issue. Approved.

The patch removes the unsafe auto-refund path after a withdrawal has been moved to processing, moves ambiguous post-deduction/broadcast failures to recovery_required, and also moves startup-orphaned processing rows to the same manual-review state before the worker loop starts. That preserves the deducted balance until an operator checks chain status, which matches the safer recovery model requested in #5346. The existing production-noop path still leaves pending withdrawals untouched before any balance deduction.

Validation performed:

  • git diff --check origin/main...HEAD
  • python3 -m py_compile node/payout_worker.py tests/test_payout_worker_production_noop.py
  • python3 tools/bcos_spdx_check.py --base-ref origin/main -> BCOS SPDX check OK
  • Manual SQLite probes for post-deduction broadcast uncertainty and startup processing recovery -> both kept the deducted balance and set recovery_required

I did not find a blocking issue in this patch.

@HCIE2054 HCIE2054 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@weilixiong

Copy link
Copy Markdown
Contributor

Claiming #5577 - will implement recovery_required state + regression test.

@TJCurnutte TJCurnutte 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.

Reviewed the payout recovery path and ran the focused checks. This is the safer behavior around ambiguous broadcast state.

What I checked:

git diff --check origin/main...HEAD -- node/payout_worker.py tests/test_payout_worker_production_noop.py
PYTHONPATH=. python3 -B -m py_compile node/payout_worker.py tests/test_payout_worker_production_noop.py
PYTHONPATH=. python3 -B -m pytest -q -o addopts='' tests/test_payout_worker_production_noop.py

Results:

  • git diff --check passed.
  • py_compile passed for the worker and focused test file.
  • Focused pytest passed: 4 passed, 1 warning in 0.06s.

I also ran a temp-SQLite runtime probe with MOCK_MODE=True and an execute_withdrawal() override that raises RuntimeError("network timeout after possible broadcast") after the worker has deducted funds and moved the withdrawal to processing. The resulting state was:

{'balance': 89, 'status': 'recovery_required', 'tx_hash': None, 'recovery_required': 1}

That confirms the important invariant here: once funds have been deducted / processing has started, the worker no longer auto-refunds on an ambiguous execution failure. It preserves the deducted balance state, marks the withdrawal for manual recovery, and surfaces the recovery count in stats. The startup sweep also moves orphaned processing rows into the same manual-review state.

Approved.

@jaxint jaxint left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM! Great work on this PR. 🚀

@HCIE2054 HCIE2054 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@kekehanshujun kekehanshujun left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I reviewed the payout worker patch flow and tests. This fixes the key ambiguity by only moving a withdrawal to recovery_required after the worker has already marked it processing/deducted funds, instead of automatically refunding when broadcast outcome is unknown.

The startup recovery hook is wired through main() before the loop, and the tests cover both ambiguous broadcast failure and startup recovery of an orphaned processing withdrawal without refunding the account. I do not see a blocker in this version.

@HCIE2054 HCIE2054 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@HCIE2054 HCIE2054 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@HCIE2054 HCIE2054 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@HCIE2054 HCIE2054 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@HCIE2054 HCIE2054 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@HCIE2054 HCIE2054 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@HCIE2054 HCIE2054 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@HCIE2054 HCIE2054 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@HCIE2054 HCIE2054 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@HCIE2054 HCIE2054 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@guangningsun

Copy link
Copy Markdown
Contributor

PR Review — PR #5577

Title: fix: require manual payout recovery after broadcast uncertain

Author: iamdinhthuan

What I reviewed

  • node/payout_worker.py
  • tests/test_payout_worker_production_noop.py (new file, 93 lines)

Observations

  1. Introduces a new status RECOVERY_REQUIRED_STATUS for payout states that need manual intervention after broadcast uncertainty.
  2. Adds 57 lines to payout_worker.py and a new 93-line test file for production payout recovery scenarios.
  3. The fix addresses a gap where uncertain broadcast states didn't require manual recovery, potentially leaving withdrawals in an inconsistent state.
  4. The test test_process_withdrawal_leaves_pending_when_production_broadcast_is_not_confirmed covers the scenario where production broadcast failure leaves the withdrawal as "pending".

LGTM pending CI. Important consistency fix with good test coverage. The manual recovery requirement prevents silent failures in the payout flow.

Bounty: #2782
Disclosure: I received RTC compensation for this review.

@HCIE2054 HCIE2054 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@HCIE2054 HCIE2054 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@HCIE2054 HCIE2054 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

@Scottcjn

Copy link
Copy Markdown
Owner

Closing as stale branch — would cause destructive deletions if merged.

Your branch was filed roughly 155 commits behind current main. Since then, many overlapping fixes from other contributors have landed via parallel PRs. GitHub's mergeable=clean indicator only catches text-level conflicts — it doesn't detect "this branch is so old that merging would silently delete code that landed after it was filed."

Bounty credit acknowledged

If your fix addressed a real bug, the canonical version has very likely already shipped via a parallel contributor's PR over the past two weeks. Specific cases covered by today's audit:

If you want fresh review

Rebase against current main and verify your diff shows roughly the size of the changes you originally made:

git fetch upstream main
git rebase upstream/main
git diff main..HEAD --shortstat

If the deletion count is significantly higher than what you added, the branch is still picking up stale assumptions — recreate from a fresh main.

Thanks for the contribution work this week.

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

Labels

BCOS-L1 Beacon Certified Open Source tier BCOS-L1 (required for non-doc PRs) node Node server related size/M PR: 51-200 lines tests Test suite changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[SECURITY] payout_worker orphan recovery can refund in-flight withdrawals on restart