fix(block): validate block after applying instead of before during sync - #2579
Conversation
There was a problem hiding this comment.
Summary of Changes
Hello @julienrbrt, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request implements a fix that alters the sequence of operations during block synchronization. Specifically, it changes when a block is validated: instead of validating a block before it's applied to the blockchain state, the validation now occurs after the block has been applied. This adjustment allows the validation process to leverage the updated state of the blockchain, which can be critical for certain verification functions.
Highlights
- Block Validation Timing Change: The logic for validating incoming blocks during the synchronization process has been reordered. Previously, blocks were validated before being applied to the blockchain state. Now, the validation occurs after the block has been applied.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in issue comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
Footnotes
-
Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩
|
The latest Buf updates on your PR. Results from workflow CI and Release / buf-check (pull_request).
|
There was a problem hiding this comment.
Code Review
This pull request modifies the block synchronization logic to perform block validation after applying the block, rather than before. The stated reason is to support custom verification functions that may depend on the state of the blockchain after transaction execution. This change aligns the synchronization path with the block production path.
My review focuses on the trade-offs of this change. While necessary for the described use case, it introduces the possibility of performing expensive work in applyBlock that is then discarded if validation fails. I've added a comment to discuss this and suggest considering a more transactional approach for block application in the future to improve robustness.
| newState, err := m.applyBlock(ctx, h.Header, d) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to apply block: %w", err) | ||
| } | ||
|
|
||
| // validate the received block after applying | ||
| // a custom verification function can depend on the state of the blockchain | ||
| if err := m.Validate(ctx, h, d); err != nil { | ||
| return fmt.Errorf("failed to validate block: %w", err) | ||
| } |
There was a problem hiding this comment.
By moving validation to after applyBlock, we risk performing computationally expensive work that will be discarded if validation subsequently fails. If applyBlock (specifically, m.exec.ExecuteTxs) has side effects on the executor's in-memory state, a validation failure would leave the executor in an intermediate state, which would likely only be resolved by a node restart.
While this change is necessary if signature verification depends on transaction execution results, it's worth acknowledging this trade-off. For future improvements, consider if the Executor interface could be extended to support a more transactional block application process. For example:
PrepareBlockto execute transactions and return a handle to a candidate state.Validatethe block against this candidate state.CommitBlockorAbortBlockbased on the validation outcome.
This would create a more robust and explicit flow, avoiding wasted work and transient inconsistencies.
|
Claude finished @julienrbrt's task —— View job I'll review this pull request and provide feedback on code quality, potential issues, and other considerations. Todo List
Code Review SummaryThis PR changes the block validation timing during synchronization from before applying the block to after applying the block. The change is necessary to support signature verifiers in ev-abci that depend on post-execution state. ✅ Positive Aspects
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2579 +/- ##
==========================================
+ Coverage 72.65% 72.74% +0.09%
==========================================
Files 72 72
Lines 7311 7310 -1
==========================================
+ Hits 5312 5318 +6
+ Misses 1574 1568 -6
+ Partials 425 424 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Overview
We validate after during execution, but before during syncing, but the signature verifier provided by ev-abci needs some data that is saved after applying.