Skip to content

feat: Add Ethereum-style timestamp validation and PoW hash check#117

Open
SIDDHANTCOOKIE wants to merge 1 commit into
mainfrom
feat/timestamp-pow-validation
Open

feat: Add Ethereum-style timestamp validation and PoW hash check#117
SIDDHANTCOOKIE wants to merge 1 commit into
mainfrom
feat/timestamp-pow-validation

Conversation

@SIDDHANTCOOKIE

@SIDDHANTCOOKIE SIDDHANTCOOKIE commented Jul 7, 2026

Copy link
Copy Markdown
Member

Addressed Issues:

This PR introduces critical security and consensus fixes to the Blockchain class to prevent difficulty manipulation and secure the Proof-of-Work validation loop.

The Vulnerability Fixed: Previously, validate_block_link_and_hash verified that a block's hash mathematically matched its contents, but it never checked if the hash actually satisfied the target difficulty. A malicious actor could broadcast a block with nonce=0 and as long as they set the difficulty integer to the required target, the network would accept it instantly without any actual PoW being performed.

Screenshots/Recordings:

TODO: If applicable, add screenshots or recordings that demonstrate the interface before and after the changes.

Additional Notes:

AI Usage Disclosure:

We encourage contributors to use AI tools responsibly when creating Pull Requests. While AI can be a valuable aid, it is essential to ensure that your contributions meet the task requirements, build successfully, include relevant tests, and pass all linters. Submissions that do not meet these standards may be closed without warning to maintain the quality and integrity of the project. Please take the time to understand the changes you are proposing and their impact. AI slop is strongly discouraged and may lead to banning and blocking. Do not spam our repos with AI slop.

Check one of the checkboxes below:

  • This PR does not contain AI-generated code at all.
  • This PR contains AI-generated code. I have read the AI Usage Policy and this PR complies with this policy. I have tested the code locally and I am responsible for it.

I have used the following AI models and tools: TODO

Checklist

  • My PR addresses a single issue, fixes a single bug or makes a single improvement.
  • My code follows the project's code style and conventions
  • If applicable, I have made corresponding changes or additions to the documentation
  • If applicable, I have made corresponding changes or additions to tests
  • My changes generate no new warnings or errors
  • I have joined the Discord server and I will share a link to this PR with the project maintainers there
  • I have read the Contribution Guidelines
  • Once I submit my PR, CodeRabbit AI will automatically review it and I will address CodeRabbit's comments.
  • I have filled this PR template completely and carefully, and I understand that my PR may be closed without review otherwise.

Summary by CodeRabbit

  • Bug Fixes

    • Added stricter block validation to reject links with invalid proof-of-work, non-increasing timestamps, or timestamps too far in the future.
    • Improved rejection handling so invalid blocks are classified more accurately.
  • Tests

    • Updated persistence tests to use deterministic block timestamps for more reliable results.

@coderabbitai

coderabbitai Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 30b952fb-e731-425c-a981-200885e1447f

📥 Commits

Reviewing files that changed from the base of the PR and between 9419fdb and 1335026.

📒 Files selected for processing (2)
  • minichain/chain.py
  • tests/test_persistence.py

Walkthrough

Block validation in chain.py now enforces Proof-of-Work difficulty (leading-zero hash prefix) and timestamp rules (strict increase from previous block, bounded future tolerance of 15,000 ms), with ValueErrors mapped to INVALID or FAILED status. Persistence tests updated to use deterministic incrementing timestamps.

Changes

Block Validation Rules

Layer / File(s) Summary
PoW difficulty and timestamp validation
minichain/chain.py
Adds time import; validates block hash against difficulty-based leading-zero prefix, enforces strictly increasing timestamps, and rejects timestamps more than 15,000 ms in the future.
Deterministic test timestamps
tests/test_persistence.py
Updates block-building test helpers to set timestamps as previous block's timestamp plus 1000 instead of current wall-clock time.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested labels: Python Lang

Poem

A rabbit hops with pride so keen,
Each block now proves what work has been.
Time ticks forward, never back,
No future dates upon the track. ⏰
Hop, hop, hooray — the chain's secure! 🐇⛓️

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: adding PoW hash verification and timestamp validation to block checks.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/timestamp-pow-validation

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant