feat: Implement StateJournal and in-memory snapshots for state rollback - #134
feat: Implement StateJournal and in-memory snapshots for state rollback#134SIDDHANTCOOKIE wants to merge 1 commit into
Conversation
|
Warning Review limit reached
Next review available in: 53 minutes You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
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 |
Addressed Issues:
Previously, the node relied heavily on full copy.deepcopy() operations of the entire state dictionary. This PR replaces that with a highly optimized two-tier rollback architecture:
Problem: When validating transactions inside a block, the node was making an expensive deep copy of the state before every transaction to allow for gas refunds/reverts upon failure.
Solution: Introduced a StateJournal proxy dictionary in minichain/state.py. It acts as an in-memory cache that tracks diffs during transaction execution in O(1) time.
Impact: If a transaction fails mid-execution (e.g., out of gas), the journal simply discards the cache (rollback()). If it succeeds, the diff is flushed to the backing dictionary (commit()), completely eliminating the need for transaction-level deep copies.
Problem: During a chain reorganization (resolve_conflicts), the node had to fetch the genesis state and sequentially re-apply every single transaction in history up to the fork point.
Solution: Implemented _state_snapshots in minichain/chain.py (an OrderedDict limited to the 10 most recent blocks).
Impact: When a shallow reorg occurs, the node now fast-forwards the difficulty/timestamp calculations and restores the exact state at the fork point directly from memory. Replaying from genesis is now strictly a fallback mechanism for incredibly deep reorgs.
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:
I have used the following AI models and tools: TODO
Checklist