refactor: Consolidate scattered validation logic#77
refactor: Consolidate scattered validation logic#77SIDDHANTCOOKIE wants to merge 2 commits intoStabilityNexus:mainfrom
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (3)
WalkthroughValidation and deserialization were consolidated into domain objects: Changes
Sequence Diagram(s)sequenceDiagram
participant Peer
participant P2PNetwork
participant Transaction
participant Block
participant Mempool
participant State
Peer->>P2PNetwork: send message (tx/block payload)
alt payload is transaction
P2PNetwork->>Transaction: Transaction.from_dict(payload)
Transaction-->>P2PNetwork: tx or None
P2PNetwork->>Transaction: tx.is_valid()
alt valid
P2PNetwork->>Mempool: add_transaction(tx)
Mempool-->>P2PNetwork: accepted
else invalid
P2PNetwork-->>Peer: reject
end
else payload is block
P2PNetwork->>Block: Block.from_dict(payload)
Block-->>P2PNetwork: block or None
P2PNetwork->>Block: block.is_valid()
alt valid
P2PNetwork->>State: propose block (apply transactions)
State->>State: apply_transaction(tx) for each tx
alt all applied
State-->>P2PNetwork: commit
else failure
State-->>P2PNetwork: reject
end
else invalid
P2PNetwork-->>Peer: reject
end
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@minichain/block.py`:
- Around line 121-122: The constructor currently overrides the computed merkle
root with the incoming payload's "merkle_root" (block.merkle_root), which can be
spoofed; remove that unconditional override or (preferred) keep accepting the
field but add verification in is_valid(): compute
_calculate_merkle_root(self.transactions) and compare it to self.merkle_root,
returning False or raising a validation error if they differ; update is_valid()
to reference block.merkle_root, _calculate_merkle_root, and the constructor's
handling of the "merkle_root" payload so the stored value must match the
computed value.
In `@minichain/transaction.py`:
- Around line 72-74: Simplify the nested checks around the receiver by combining
them into one conditional: replace the outer if self.receiver is not None with a
single if that tests both presence and validity (e.g., if self.receiver is not
None and (not isinstance(self.receiver, str) or not
self.is_valid_address(self.receiver)): return False), referencing the receiver
attribute and the is_valid_address method to keep the same behavior but with a
single, clearer condition.
- Around line 44-47: Move the import of the regular expression module out of the
is_valid_address static method to the module level: add import re alongside the
other top-level imports, then remove the inline import inside is_valid_address
so the method simply uses re.fullmatch; this avoids repeated module lookups and
keeps the method body minimal.
🪄 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: 4e2d52f3-0c75-442c-917c-cf0ae4a7fa35
📒 Files selected for processing (9)
main.pyminichain/block.pyminichain/chain.pyminichain/mempool.pyminichain/p2p.pyminichain/state.pyminichain/transaction.pyminichain/validators.pytests/test_protocol_hardening.py
💤 Files with no reviewable changes (1)
- minichain/validators.py
Addressed Issues:
This PR fundamentally sorts the network's validation logic into two clean, distinct buckets:
This structurally stops the chain from repeating duplicate security checks, rapidly accelerating network validation and increasing spam resilience, all while resulting in a net decrease of 31 lines of code.
Screenshots/Recordings:
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:
I have used the following AI models and tools: TODO
Checklist
Summary by CodeRabbit
Bug Fixes
Refactor