Skip to content

fix: load withdrawal validation test module by path#5579

Closed
iamdinhthuan wants to merge 2 commits into
Scottcjn:mainfrom
iamdinhthuan:fix-withdrawal-validation-import-5476
Closed

fix: load withdrawal validation test module by path#5579
iamdinhthuan wants to merge 2 commits into
Scottcjn:mainfrom
iamdinhthuan:fix-withdrawal-validation-import-5476

Conversation

@iamdinhthuan
Copy link
Copy Markdown

Summary

  • load node/tests/test_withdrawal_validation.py via importlib.util.spec_from_file_location so the dotted filename rustchain_v2_integrated_v2.2.1_rip200.py is not parsed as a Python package import
  • set temporary RUSTCHAIN_DB_PATH and test RC_ADMIN_KEY before importing the integrated node, then reuse the loaded module across the test file

Closes #5476.

Validation

  • python3 -B -m py_compile node/tests/test_withdrawal_validation.py
  • PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 PYTHONPATH=. python3 -B -m pytest -q node/tests/test_withdrawal_validation.py --noconftest -> 7 passed
  • git diff --check
  • python3 tools/bcos_spdx_check.py --base-ref origin/main -> BCOS SPDX check: OK

Note

  • A combined run with node/tests/test_withdraw_amount_validation.py still exposes an existing cross-file integrated-node reimport/Prometheus registration problem outside this one-file fix; this PR keeps the test_withdrawal_validation.py fails to compile #5476 compile repair scoped.

@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!

@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 size/S PR: 11-50 lines labels May 17, 2026
@508704820
Copy link
Copy Markdown
Contributor

Security Review ✅

Load withdrawal validation test module by path. Test infrastructure improvement.

Reviewed by Xeophon - Solana: Lt9nERv6VHsojw15LpFeiaabuphAggzfLF9sM9UXRrZ

Copy link
Copy Markdown

@ZacharyZhang-NY ZacharyZhang-NY left a comment

Choose a reason for hiding this comment

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

Reviewed PR #5579 at head f23f292.

Approved. Loading rustchain_v2_integrated_v2.2.1_rip200.py by file path is the right shape for this test module because the filename contains dots and cannot be imported through a normal Python module path. The fixture also sets RUSTCHAIN_DB_PATH and RC_ADMIN_KEY before import, which keeps the integrated node import isolated for this test file.

Validation run:

git diff --check origin/main...HEAD -- node/tests/test_withdrawal_validation.py
python -B -m py_compile node/tests/test_withdrawal_validation.py
PYTHONPATH=node python -B -m pytest -q node/tests/test_withdrawal_validation.py --noconftest --tb=short

Result: diff check clean, py_compile clean, withdrawal validation tests 7 passed.

Copy link
Copy Markdown
Contributor

@HCIE2054 HCIE2054 left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Copy Markdown

@TJCurnutte TJCurnutte left a comment

Choose a reason for hiding this comment

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

Approved.

I checked this on head f23f2920aa9c62f155abf73014e0a18fba6ecf54. The prior normal import path for rustchain_v2_integrated_v2.2.1_rip200.py is not a valid Python module path because of the dotted numeric filename, so loading it with importlib.util.spec_from_file_location() is the right shape here. The fixture also scopes RUSTCHAIN_DB_PATH to a tmp SQLite DB, sets/restores RC_ADMIN_KEY, and then drives the real Flask app through the existing withdrawal validation cases.

Validation run:

git diff --check origin/main...HEAD -- node/tests/test_withdrawal_validation.py
python3 -B -m py_compile node/tests/test_withdrawal_validation.py
PYTHONPATH=node python3 -B -m pytest -q node/tests/test_withdrawal_validation.py

Result: 7 passed, 1 warning in 0.38s.

No blockers from me.

Copy link
Copy Markdown
Contributor

@jaxint jaxint left a comment

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. 🚀

Copy link
Copy Markdown
Contributor

@HCIE2054 HCIE2054 left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Copy Markdown
Contributor

@kekehanshujun kekehanshujun left a comment

Choose a reason for hiding this comment

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

Requesting changes because this can still fail during pytest collection/import in a normal test run where prometheus_client is installed.

The repository tests/conftest.py already preloads node/rustchain_v2_integrated_v2.2.1_rip200.py as integrated_node. This PR's fixture then executes the same integrated node file again under a second module name via spec_from_file_location(...). Since that module defines Prometheus Counter/Gauge objects at import time with fixed names, the second import can raise duplicated-timeseries errors before the withdrawal tests run.

Please either reuse the conftest-loaded integrated_node module or patch/unregister the Prometheus metric constructors during the second import, matching the more defensive pattern used in other withdrawal-validation fixes.

@github-actions github-actions Bot added size/M PR: 51-200 lines and removed size/S PR: 11-50 lines labels May 17, 2026
@iamdinhthuan
Copy link
Copy Markdown
Author

Fixed in 37249d0a.

What changed:

  • _load_integrated_node() now reuses the tests/conftest.py preloaded sys.modules["integrated_node"] module when it exists, so this test file does not execute rustchain_v2_integrated_v2.2.1_rip200.py a second time under another module name.
  • Added a regression that pins this reuse behavior.

RED check before the fix:

PYTHONPATH=node python3 -B -m pytest -q node/tests/test_withdrawal_validation.py::test_loader_reuses_preloaded_integrated_node --tb=short
# failed: loader attempted the second module import instead of returning the preloaded module

Validation after the fix:

PYTHONPATH=node python3 -B -m pytest -q node/tests/test_withdrawal_validation.py::test_loader_reuses_preloaded_integrated_node --tb=short
# 1 passed

PYTHONPATH=node python3 -B -m pytest -q node/tests/test_withdrawal_validation.py --tb=short
# 8 passed

PYTHONPATH=node:. python3 -B -m pytest -q tests/test_fingerprint.py node/tests/test_withdrawal_validation.py --tb=short
# 16 passed

python3 -B -m py_compile node/tests/test_withdrawal_validation.py
# passed

git diff --check -- node/tests/test_withdrawal_validation.py
# passed

python3 tools/bcos_spdx_check.py --base-ref origin/main
# BCOS SPDX check: OK

No private keys, tokens, wallet secrets, production writes, withdrawals, transfers, or fund movement were used.

Copy link
Copy Markdown
Contributor

@HCIE2054 HCIE2054 left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Copy Markdown
Contributor

@HCIE2054 HCIE2054 left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Copy Markdown
Contributor

@HCIE2054 HCIE2054 left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Copy Markdown
Contributor

@HCIE2054 HCIE2054 left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Copy Markdown
Contributor

@HCIE2054 HCIE2054 left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Copy Markdown
Contributor

@HCIE2054 HCIE2054 left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Copy Markdown
Contributor

@HCIE2054 HCIE2054 left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Copy Markdown
Contributor

@HCIE2054 HCIE2054 left a comment

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 duplicate of #5478 (canonical first-poster on this issue per Codex forensic audit 2026-05-18). Both PRs target the same root cause: test-loader module-import-with-dots fix. Bounty credit goes to #5478. Thanks for the contribution — for next time, please check open PRs against the same issue before filing.

@Scottcjn Scottcjn closed this May 18, 2026
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.

test_withdrawal_validation.py fails to compile

8 participants