Skip to content

fix: reject negative block reward heights - #4759

Closed
Asti1982 wants to merge 1 commit into
Scottcjn:mainfrom
Asti1982:nomad/fix-negative-block-reward-height
Closed

Asti1982 wants to merge 1 commit into
Scottcjn:mainfrom
Asti1982:nomad/fix-negative-block-reward-height

Conversation

@Asti1982

Copy link
Copy Markdown
Contributor

Summary

  • reject negative heights in the live rips/rustchain-core/config/chain_params.py::calculate_block_reward() path
  • add focused regression coverage for -1 and -210000, the cases that previously inflated rewards through Python floor division
  • preserve the existing halving and tail-emission schedule for valid heights

Fixes #4636.

Validation

python -m pytest tests\test_chain_params_block_reward.py -q
# 2 passed

$env:PYTHONUTF8='1'; python -m pytest tests\test_epoch_window_consistency.py tests\test_chain_params_block_reward.py -q
# 7 passed

python -m py_compile rips\rustchain-core\config\chain_params.py tests\test_chain_params_block_reward.py
git diff --check -- rips/rustchain-core/config/chain_params.py tests/test_chain_params_block_reward.py
# both passed

Notes

tests/test_epoch_window_consistency.py needs UTF-8 mode on this Windows host because the existing source scan hits non-CP1252 bytes in a large integrated-node file. The focused reward regression itself passes without that environment override.

@github-actions

Copy link
Copy Markdown
Contributor

Welcome to RustChain! Thanks for your first pull request.

Before we review, please make sure:

  • Your PR has a BCOS-L1 or BCOS-L2 label
  • 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 size/M PR: 51-200 lines BCOS-L1 Beacon Certified Open Source tier BCOS-L1 (required for non-doc PRs) tests Test suite changes labels May 12, 2026

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

Approved current head 5546df0.

This fixes the live rips/rustchain-core/config/chain_params.py::calculate_block_reward() path rather than adding a parallel calculator, and it covers the regression gap that earlier #4636 fixes left open. The guard runs before Python floor division can turn negative heights into negative halving counts, while the new tests keep the valid halving and tail-emission schedule unchanged.

Check Result
Negative-height repro -1 and -210000 now raise ValueError from the production function.
Focused regression uv run --no-project --with pytest --with flask python -m pytest tests\test_chain_params_block_reward.py -q -> 2 passed.
Adjacent reward consistency PYTHONUTF8=1 with tests\test_epoch_window_consistency.py tests\test_chain_params_block_reward.py -> 7 passed.
Hygiene gates py_compile, git diff --check, and python tools\bcos_spdx_check.py --base-ref origin/main all passed.

There are older overlapping #4636 PRs, so maintainers should still merge only one candidate. This one is materially stronger than the bare guard variants because the production change is paired with focused boundary regression coverage.

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

Independently validated this PR against #4636. The production function now rejects negative heights before floor division can produce negative halving counts, and the tests cover both the previous inflation cases and unchanged valid boundary rewards.

Validation run locally:

  • python -m pytest tests\test_chain_params_block_reward.py -q -> 2 passed
  • $env:PYTHONUTF8='1'; python -m pytest tests\test_epoch_window_consistency.py tests\test_chain_params_block_reward.py -q -> 7 passed
  • python -m py_compile rips\rustchain-core\config\chain_params.py tests\test_chain_params_block_reward.py
  • git diff --check origin/main...HEAD -- rips/rustchain-core/config/chain_params.py tests/test_chain_params_block_reward.py
  • python tools\bcos_spdx_check.py --base-ref origin/main -> OK

There are overlapping fixes for the same issue, so maintainers should merge only one. This one is scoped and includes useful regression coverage.

@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! Thanks for contributing. Approved.

@godd-ctrl godd-ctrl 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.

Approved after local validation of current head 5546df0.

Scope reviewed:

  • rips/rustchain-core/config/chain_params.py rejects negative heights before floor division can produce negative halving counts.
  • tests/test_chain_params_block_reward.py covers the prior negative-height cases and confirms valid halving plus tail-emission behavior is unchanged.

Validation run locally on Windows:

  • uv run --no-project --with pytest --with flask python -m pytest tests\test_chain_params_block_reward.py -q -> 2 passed
  • PYTHONUTF8=1 uv run --no-project --with pytest --with flask python -m pytest tests\test_epoch_window_consistency.py tests\test_chain_params_block_reward.py -q -> 7 passed
  • python -m py_compile rips\rustchain-core\config\chain_params.py tests\test_chain_params_block_reward.py -> passed
  • uv run --no-project --with ruff python -m ruff check rips\rustchain-core\config\chain_params.py tests\test_chain_params_block_reward.py --select F821,F401 --output-format=concise -> All checks passed
  • git diff --check origin/main...HEAD -- rips/rustchain-core/config/chain_params.py tests/test_chain_params_block_reward.py -> passed
  • python tools\bcos_spdx_check.py --base-ref origin/main -> BCOS SPDX check: OK

No blocking issue found. There are overlapping fixes for the same negative-height reward issue, so maintainers should merge only one candidate; this one is a scoped production fix with useful boundary regression coverage.

@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! Thanks for contributing. 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.

Code review: LGTM! Thanks for contributing to RustChain. 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.

Great contribution! LGTM. 🚀

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

Review for Scottcjn/rustchain-bounties#73 on current head 5546df0a3f6d04aae37fc4d266ca102db1076c89.

The block reward fix is correctly placed in the production calculate_block_reward() path and rejects negative heights before Python floor division can inflate rewards through negative halving counts. The regression also preserves the valid halving and tail-emission schedule.

Validation performed:

  • PYTEST_DISABLE_PLUGIN_AUTOLOAD=1 PYTHONUTF8=1 uv run --no-project --with pytest --with flask python -m pytest tests/test_epoch_window_consistency.py tests/test_chain_params_block_reward.py -q -> 7 passed
  • python3 -m py_compile rips/rustchain-core/config/chain_params.py tests/test_chain_params_block_reward.py -> passed
  • git diff --check origin/main...HEAD -- rips/rustchain-core/config/chain_params.py tests/test_chain_params_block_reward.py -> passed
  • python3 tools/bcos_spdx_check.py --base-ref origin/main -> OK

No blocking issues found. Maintainers should still merge only one of the overlapping negative-height reward fixes.

@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! Thanks for contributing.

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

Approved. The negative-height guard lands before the halving calculation, so negative block heights can no longer flow through Python floor-division into the normal reward schedule, while the existing halving/tail-emission outputs stay unchanged.

Validation I ran on the checked-out PR branch:

git diff --check origin/main...HEAD -- rips/rustchain-core/config/chain_params.py tests/test_chain_params_block_reward.py
/tmp/rustchain-review-venv/bin/python -B -m py_compile rips/rustchain-core/config/chain_params.py tests/test_chain_params_block_reward.py
/tmp/rustchain-review-venv/bin/python -B -m pytest tests/test_chain_params_block_reward.py -q

Result: pytest passed with 2 passed in 0.03s.

I also ran a focused import/runtime probe: calculate_block_reward(-1) and calculate_block_reward(-210000) both raise ValueError with the expected height in the message; representative valid rewards still return 1.5 at height 0, 0.75 at the first halving boundary, and 0.09375 in the tail-emission window.

@guangningsun

Copy link
Copy Markdown
Contributor

PR Review — Standard Quality ✓

PR: #4759 — fix: reject negative block reward heights

What I reviewed

  • rips/rustchain-core/config/chain_params.py
  • tests/test_chain_params_block_reward.py (new test file)

Observations

  1. Input validation for height < 0 is the correct fix. Block heights must be non-negative integers. Rejecting negative values prevents undefined behavior (e.g., negative Decimal rewards) and enforces the chain's invariant at the earliest possible point.

  2. The guard is placed at the correct layer — calculate_block_reward() is the right function to validate this, since it's the public interface for computing rewards. The check is simple and efficient: if height < 0: raise ValueError("height must be non-negative").

  3. The new test file is appropriate — test_chain_params_block_reward.py tests both negative heights (should raise) and positive heights (should return correct Decimal). This follows the test coverage pattern for the bounty Auto Batch - V2 Features (700 RTC) #1589.

LGTM.

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

@Scottcjn

Copy link
Copy Markdown
Owner

Codex audit review (would award high-tier (50 RTC) once fixed):

Issue: Prevents negative block heights from inflating block rewards; focused fix that likely only needs a rebase.

Address the specific point and ping for re-review.

@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! Thanks for contributing. Approved.

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

Reviewed PR 4759. Standard 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! Thanks for contributing to RustChain!

@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! Great contribution to the RustChain ecosystem. Thanks for keeping the codebase clean and well-tested. Approved ✅

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

@508704820

Copy link
Copy Markdown
Contributor

Security Review ✅ CRITICAL

Reject negative block reward heights. Negative block reward = infinite mint attack — attacker could create blocks with negative height that generate unlimited RTC. Fix validates height >= 0.

Reviewed by Xeophon - Solana: Lt9nERv6VHsojw15LpFeiaabuphAggzfLF9sM9UXRrZ

@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 is 966 commits behind current main. Filed during the May 11-13 contributor burst, the codebase has since moved substantially. GitHub's mergeable=clean indicator doesn't detect this; only an explicit git diff main..your-branch --shortstat does.

Bounty credit acknowledged where applicable

Most of the canonical fixes from your work-period have already shipped via other contributors' parallel PRs that landed earlier this week. Specific cases credited via the Codex audit batches:

If you want fresh review

Rebase against current main and verify your diff matches the size of your original changes:

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

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

Thanks for the contribution work.

@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) size/M PR: 51-200 lines tests Test suite changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

calculate_block_reward() accepts negative height - gives DOUBLE reward instead of error