feat: implement external genesis configuration support#87
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 (4)
💤 Files with no reviewable changes (1)
WalkthroughAdds a filesystem-backed ChangesGenesis Block Configurability
Sequence Diagram(s)sequenceDiagram
participant Blockchain
participant OS
participant JSON as JSONParser
participant State
Blockchain->>OS: read genesis_path file
OS-->>JSON: file contents
JSON-->>Blockchain: parsed genesis config
Blockchain->>State: apply alloc balances
Blockchain->>Blockchain: set timestamp and difficulty
Blockchain->>Blockchain: calculate genesis header hash
Blockchain->>Blockchain: verify or use configured hash
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 4✅ Passed checks (4 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 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 52-56: The alloc loop in minichain/chain.py assigns balances from
external JSON without validation; update the loop that iterates over alloc
(where account = self.state.get_account(address) and account['balance'] = ...)
to validate each balance is an integer and non-negative before committing: parse
numeric strings safely (or reject non-numeric), ensure value >= 0, and either
raise a clear ValueError (or log and skip/normalize) when validation fails so
invalid balances are not written into state.
- Around line 44-50: The node currently continues startup with empty state when
genesis.json is missing or invalid; change the logic in chain.py around
genesis_path so that missing file or JSON parse errors cause a fail-fast exit:
if not os.path.exists(genesis_path) or json.load fails, log a clear error via
logger.error (including the exception/message) and terminate startup immediately
(e.g., raise SystemExit or call sys.exit(1)) instead of proceeding with a
default/empty config; update the block handling "with open(genesis_path...)" and
any surrounding code that assumes config to ensure startup is aborted on
load/parse failure.
- Around line 57-67: The code sets genesis_block.hash directly from config which
can create an inconsistent block; change the logic around the genesis Block
creation (the genesis_block assignment) to compute the canonical hash from the
genesis header (use the existing Block.calculate_hash or the module's
calculate_hash function) and then: if config contains a "hash", verify it equals
the computed hash and raise/log an error (or raise an exception) on mismatch; if
no "hash" present, assign the computed hash to genesis_block.hash. Ensure this
verification happens immediately after creating genesis_block and before
returning or persisting it.
🪄 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: 2c0355a2-c2cd-4ae7-903d-390e6dd51d9a
📒 Files selected for processing (2)
genesis.jsonminichain/chain.py
Addressed Issues:
This PR fulfills the first requirement of Phase 1 (Week 3) of our roadmap by implementing externalized genesis configuration for MiniChain. Previously, the genesis block was hardcoded into
chain.pywith zero initial balances.Now, MiniChain boots up by reading
genesis.json. This gives us total control over the network's initial state without touching core logic.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
New Features
Bug Fixes
Tests
Chores