Skip to content

fix: validate proof of work on block acceptance#115

Open
Jiyacodex wants to merge 7 commits into
StabilityNexus:mainfrom
Jiyacodex:test-pow-validation
Open

fix: validate proof of work on block acceptance#115
Jiyacodex wants to merge 7 commits into
StabilityNexus:mainfrom
Jiyacodex:test-pow-validation

Conversation

@Jiyacodex

@Jiyacodex Jiyacodex commented Jul 4, 2026

Copy link
Copy Markdown

Addressed Issues:

Received blocks were checked for linkage and hash consistency, but their hashes were not verified against the required Proof-of-Work difficulty.

This PR adds PoW validation to validate_block_link_and_hash(). Since both add_block() and resolve_conflicts() use this shared validator, invalid-PoW blocks are now rejected during both normal block acceptance and chain reorganization.

Screenshots/Recordings:

Not applicable.

Additional Notes:

Added regression tests covering both affected paths:

  • add_block() rejects a block whose hash does not satisfy its claimed difficulty.
  • resolve_conflicts() rejects a candidate chain containing a block with invalid PoW.

The regression tests fail without the PoW validation check and pass with the fix.

Testing:

  • python -m pytest tests/test_pow_validation.py -v: 2 passed
  • python -m pytest: 75 passed, 1 pre-existing failure in TestSmartContract.test_out_of_gas
    • Expected: Out of gas!
    • Actual: Execution timed out
    • The same failure was reproduced on untouched origin/main

AI Usage Disclosure:

  • 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: ChatGPT and Claude for reviewing the changes and refining the tests.

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
    • Strengthened block validation to reject proof-of-work failures, enforce the expected difficulty, and improve timestamp checks.
    • Refined persisted-chain loading to replay blocks through the normal apply path with stricter genesis/hash verification; invalid blocks now fail to load.
    • Tightened block hash checking to consistently detect hash mismatches.
  • Tests
    • Added PoW and difficulty-tampering rejection tests for both block addition and conflict resolution.
    • Added/updated persistence load tests to ensure the apply pipeline is used and corrupted persisted data is rejected.

@coderabbitai

coderabbitai Bot commented Jul 4, 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: 4ac1c3d3-4a6f-43dc-8c93-246504544f3f

📥 Commits

Reviewing files that changed from the base of the PR and between fda0c04 and 472fff1.

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

Walkthrough

Strengthens Proof-of-Work and difficulty validation, applies those checks during block acceptance and conflict resolution, and changes persistence loading to replay blocks through Blockchain._apply_block() with expanded validation coverage.

Changes

Consensus and Persistence Validation

Layer / File(s) Summary
Consensus validation and rejection handling
minichain/chain.py, minichain/block.py
Adds difficulty and Proof-of-Work validation, enforces timestamps and hash presence, tightens total-work calculation, and handles invalid blocks during application and conflict resolution.
Persisted chain replay
minichain/persistence.py
Validates persisted accounts and genesis data, replays blocks through _apply_block(), and restores replay-derived state, difficulty, and timing.
Validation and persistence regression coverage
tests/test_pow_validation.py, tests/test_persistence.py, tests/test_persistence_runtime.py
Adds invalid-PoW and lied-difficulty tests, verifies replay-based loading, and updates persistence fixtures and snapshot assertions.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Snapshot
  participant persistence.load
  participant Blockchain
  participant State
  Snapshot->>persistence.load: provide persisted accounts and blocks
  persistence.load->>Blockchain: validate genesis and initialize replay state
  persistence.load->>Blockchain: apply each non-genesis block
  Blockchain->>State: validate and apply block state changes
  Blockchain-->>persistence.load: return status and updated difficulty
  persistence.load-->>Snapshot: return reconstructed blockchain
Loading

Possibly related PRs

Suggested labels: Python Lang

Suggested reviewers: siddhantcookie, zahnentferner

Poem

I hopped through blocks beneath the moon,
Checking zeros one by one.
Replay the chain, let truth prevail,
False work goes straight into the fail.
🐰

🚥 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 summarizes the main change: enforcing proof-of-work validation when blocks are accepted.
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 unit tests (beta)
  • Create PR with unit tests

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.

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@minichain/chain.py`:
- Around line 28-31: The PoW validation in block handling is currently tied to
fragile string matching in add_block, where ValueError classification depends on
whether the message contains “hash.” Update the proof-of-work check in the block
validation path to raise a distinct error type or carry an explicit reason/code
from the PoW check, and update add_block to classify based on that symbol
instead of substring inspection. Use the existing block validation logic and
add_block flow to keep INVALID vs FAILED handling stable even if the error
wording changes.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 3f2bc44e-a4f5-4c11-a81e-8a5cdfd08fec

📥 Commits

Reviewing files that changed from the base of the PR and between 4334478 and 2b20b43.

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

Comment thread minichain/chain.py Outdated
@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown

⚠️ This PR has merge conflicts.

Please resolve the merge conflicts before review.

Your PR will only be reviewed by a maintainer after all conflicts have been resolved.

📺 Watch this video to understand why conflicts occur and how to resolve them:
https://www.youtube.com/watch?v=Sqsz1-o7nXk

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@minichain/chain.py`:
- Around line 32-34: The PoW validation in validate_block_link_and_hash() uses
block.difficulty directly, which can cause type coercion issues or expensive
string allocation before the later checks in _apply_block(). Add a difficulty
sanity check in validate_block_link_and_hash() using the existing
block.difficulty and InvalidProofOfWorkError path so add_block() rejects
non-integer, boolean, negative, or otherwise invalid difficulty values early and
consistently.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: ada65b4d-f692-4efc-9c72-48ee7c94821e

📥 Commits

Reviewing files that changed from the base of the PR and between 2b20b43 and 9423173.

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

Comment thread minichain/chain.py Outdated
Comment thread minichain/chain.py
pass


def validate_difficulty(difficulty, max_difficulty):

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.

Do we really need this validation function? Don't we know that we will always call this function with arguments that satisfy the conditions in line 18?

Also, some conditions in line 18 seem redundant anyway. For example, if difficulty is greater than len(block.hash), line 39 will fail anyway.

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.

Unless I'm overlooking something important, let's remove this validation function.

Comment thread minichain/chain.py Outdated
raise ValueError(f"invalid difficulty {difficulty}")


def validate_block_link_and_hash(previous_block, block):

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.

This function is now also checking difficulty, but its name is still suggesting that it checks only block link and hash.

Let's rename it to validate_block.

Comment thread minichain/chain.py
raise ValueError(f"invalid hash {block.hash}")

validate_difficulty(block.difficulty, len(block.hash))

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.

In addition to the check in line 39, we must also check that block.difficulty is equal to the expected difficulty based on previous blocks.

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.

Otherwise, the miner can circumvent the difficulty check, by simply writing a small difficulty in block.difficulty.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@Zahnentferner you're right. When I reviewed the attack was blocked by the separate difficulty checks in add_block and resolve_conflicts in the initial commit, but the architecture was flawed which I didn't notice since the checks already protected.
The difficulty check should have been moved inside the validator at the same time as the PoW check. That would have been the proper, complete fix. I focused too narrowly on, if the PoW check now exists or not.
The validation function and related issues came in later commits.

@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown

⚠️ This PR has merge conflicts.

Please resolve the merge conflicts before review.

Your PR will only be reviewed by a maintainer after all conflicts have been resolved.

📺 Watch this video to understand why conflicts occur and how to resolve them:
https://www.youtube.com/watch?v=Sqsz1-o7nXk

mac added 2 commits July 17, 2026 01:50
…all-clock

Fixes non-deterministic difficulty drift: using time.time() created a
multi-year gap from genesis's fixed timestamp, causing EMA difficulty
adjustment to swing unpredictably run-to-run. Anchoring to
last_block.timestamp + target_block_time matches the pattern already
used in test_difficulty.py and keeps difficulty stable and deterministic.

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

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@minichain/chain.py`:
- Around line 151-154: Update the chain validation loop before the total-work
calculation to validate each block’s hash as a required SHA256-length value,
rejecting missing or malformed hashes before calling validate_difficulty. Avoid
using len(block.hash) as the difficulty bound; use the established SHA256
hash-length constraint, while preserving the existing work summation for valid
chains.

In `@tests/test_persistence_runtime.py`:
- Around line 80-82: Update the blockchain fixture helper around mine_block and
bc.add_block to assert that add_block accepts the mined fixture block before
returning bc. Keep returning the populated chain only after the acceptance
assertion succeeds, so failures cannot produce a silently genesis-only fixture.

In `@tests/test_persistence.py`:
- Around line 303-314: Make replay fixture timestamps deterministic by deriving
each new block timestamp from the preceding block and target interval. In
tests/test_persistence.py at lines 303-314, update block2’s timestamp to use
restored.last_block.timestamp plus restored.target_block_time; in
tests/test_persistence_runtime.py at lines 70-79, update the corresponding block
builder to use bc.last_block.timestamp plus bc.target_block_time.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: d0a92967-fff0-4f50-8244-ca8210158181

📥 Commits

Reviewing files that changed from the base of the PR and between 9423173 and fda0c04.

📒 Files selected for processing (5)
  • minichain/chain.py
  • minichain/persistence.py
  • tests/test_persistence.py
  • tests/test_persistence_runtime.py
  • tests/test_pow_validation.py

Comment thread minichain/chain.py
Comment on lines +151 to +154
for block in chain_list:
validate_difficulty(block.difficulty, len(block.hash))

return sum(2 ** block.difficulty for block in chain_list)

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.

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

Reject malformed hashes before measuring total work.

Block.from_dict() permits a missing hash, so an untrusted candidate can reach len(None). That raises TypeError, which resolve_conflicts() does not catch. Using the supplied hash length as the difficulty bound also accepts non-SHA256 lengths.

Proposed fix
     for block in chain_list:
-        validate_difficulty(block.difficulty, len(block.hash))
+        if not isinstance(block.hash, str) or len(block.hash) != 64:
+            raise ValueError("invalid block hash")
+        validate_difficulty(block.difficulty, 64)
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
for block in chain_list:
validate_difficulty(block.difficulty, len(block.hash))
return sum(2 ** block.difficulty for block in chain_list)
for block in chain_list:
if not isinstance(block.hash, str) or len(block.hash) != 64:
raise ValueError("invalid block hash")
validate_difficulty(block.difficulty, 64)
return sum(2 ** block.difficulty for block in chain_list)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@minichain/chain.py` around lines 151 - 154, Update the chain validation loop
before the total-work calculation to validate each block’s hash as a required
SHA256-length value, rejecting missing or malformed hashes before calling
validate_difficulty. Avoid using len(block.hash) as the difficulty bound; use
the established SHA256 hash-length constraint, while preserving the existing
work summation for valid chains.

Comment on lines +80 to 82
mine_block(block, difficulty=bc.current_difficulty)
bc.add_block(block)
return bc

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.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Assert that the fixture block was accepted.

If add_block() regresses, this helper silently returns a genesis-only chain and the load test can still pass by comparing that chain with itself.

Proposed fix
         mine_block(block, difficulty=bc.current_difficulty)
-        bc.add_block(block)
+        result = bc.add_block(block)
+        self.assertEqual(result, ValidationStatus.VALID)
+        self.assertEqual(len(bc.chain), 2)
         return bc
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
mine_block(block, difficulty=bc.current_difficulty)
bc.add_block(block)
return bc
mine_block(block, difficulty=bc.current_difficulty)
result = bc.add_block(block)
self.assertEqual(result, ValidationStatus.VALID)
self.assertEqual(len(bc.chain), 2)
return bc
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/test_persistence_runtime.py` around lines 80 - 82, Update the
blockchain fixture helper around mine_block and bc.add_block to assert that
add_block accepts the mined fixture block before returning bc. Keep returning
the populated chain only after the acceptance assertion succeeds, so failures
cannot produce a silently genesis-only fixture.

Comment thread tests/test_persistence.py
Comment on lines 303 to 314
block2 = Block(
index=len(restored.chain),
previous_hash=restored.last_block.hash,
transactions=[tx2],
difficulty=restored.current_difficulty,
state_root=temp_state.state_root(),
state_root=temp_state2.state_root(),
receipt_root=calculate_receipt_root([receipt2]),
receipts=[receipt2],
timestamp=restored.last_block.timestamp + 1000,
timestamp=int(time.time() * 1000) + 3000,
miner=new_pk,
)
mine_block(block2)

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.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Keep replay fixtures independent of wall-clock time.

Both block builders derive timestamps from the test environment rather than the preceding block, making ordering and difficulty adjustment nondeterministic.

  • tests/test_persistence.py#L303-L314: use restored.last_block.timestamp + restored.target_block_time.
  • tests/test_persistence_runtime.py#L70-L79: explicitly use bc.last_block.timestamp + bc.target_block_time.
📍 Affects 2 files
  • tests/test_persistence.py#L303-L314 (this comment)
  • tests/test_persistence_runtime.py#L70-L79
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/test_persistence.py` around lines 303 - 314, Make replay fixture
timestamps deterministic by deriving each new block timestamp from the preceding
block and target interval. In tests/test_persistence.py at lines 303-314, update
block2’s timestamp to use restored.last_block.timestamp plus
restored.target_block_time; in tests/test_persistence_runtime.py at lines 70-79,
update the corresponding block builder to use bc.last_block.timestamp plus
bc.target_block_time.

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.

3 participants