Skip to content

feat: Implement StateJournal and in-memory snapshots for state rollback - #134

Open
SIDDHANTCOOKIE wants to merge 1 commit into
mainfrom
feature/state-rollback
Open

feat: Implement StateJournal and in-memory snapshots for state rollback#134
SIDDHANTCOOKIE wants to merge 1 commit into
mainfrom
feature/state-rollback

Conversation

@SIDDHANTCOOKIE

Copy link
Copy Markdown
Member

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:

  1. Transaction-level Undo Log (StateJournal)
    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.
  2. Block-level In-Memory Snapshots
    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:

  • 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.

@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

@SIDDHANTCOOKIE, you've reached your PR review limit, so we couldn't start this review.

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 @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: c7fc15ba-1cf6-446a-908f-0c1e261d6667

📥 Commits

Reviewing files that changed from the base of the PR and between 20d148e and 4e3387a.

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

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.

@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown

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.

1 participant