Repository navigation
fix: wrap block builder to fix PoL sender recovery - #219
Conversation
📝 WalkthroughWalkthroughAdds a BerachainBlockBuilder wrapper to the EVM block-building pipeline that fixes PoL-injection sender mismatches during block finalization, wires it into EVM configuration, declares the new private module, and adds Changes
Sequence Diagram(s)sequenceDiagram
participant Caller as Caller
participant BBB as BerachainBlockBuilder
participant IB as InnerBuilder
participant CS as ChainSpec
participant SP as StateProvider
Caller->>BBB: finish(state_provider)
BBB->>IB: finish(state_provider)
IB-->>BBB: BlockBuilderOutcome{block, txs, senders}
BBB->>CS: is_prague1_active(block.timestamp)
CS-->>BBB: bool
BBB->>BBB: validate invariants & extract PoL sender
BBB->>BBB: reconstruct senders (prefix PoL sender)
BBB-->>Caller: BlockBuilderOutcome{block_with_fixed_senders,...}
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 5 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (5 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 |
fridrik01
left a comment
There was a problem hiding this comment.
just some nits mostly for my notes as this code is already on the sequenver-dev branch
BerachainBlockBuilder wraps the inner block builder to fix sender count mismatch for PoL transactions. The reth block builder doesn't recover senders for transactions that bypass the pool (system txs), so we extract senders from PoL transactions directly. Invariants are strictly enforced based on Prague1 activation: - Pre-Prague1: num_transactions == num_senders (no PoL injection) - Post-Prague1: num_transactions > num_senders (PoL always injected) Violations in either direction indicate a bug in block assembly. Fixes: #129
01ed6e9 to
c222947
Compare
Replace match on tuple with linear if/else flow. Assert exactly 1 injected PoL transaction instead of iterating over an unknown count.
c222947 to
c42f05d
Compare
There was a problem hiding this comment.
Pull request overview
Fixes sender/transaction misalignment introduced by Proof-of-Liquidity (PoL) transaction injection after the Prague1 hardfork by wrapping the block builder and repairing the recovered senders list at build finalization time.
Changes:
- Add
BerachainBlockBuilderwrapper that overridesfinish()to detect sender/tx count mismatch and reconstruct senders from injected PoL tx(s). - Wire the wrapper into
BerachainEvmConfigvia a customcreate_block_builder()implementation. - Add
reth-storage-apidependency to satisfy theStateProviderbound used byfinish().
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
src/node/evm/mod.rs |
Registers the new builder module. |
src/node/evm/config.rs |
Overrides block builder creation to wrap BasicBlockBuilder with BerachainBlockBuilder. |
src/node/evm/builder.rs |
Implements sender list repair and Prague1-era invariants in finish(). |
Cargo.toml |
Adds reth-storage-api dependency for StateProvider. |
Cargo.lock |
Locks the new dependency. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
♻️ Duplicate comments (1)
src/node/evm/builder.rs (1)
97-114:⚠️ Potential issue | 🟡 MinorUse exact equality for the pre-Prague1 invariant.
This branch only rejects
num_txs > num_senders. Ifnum_senders > num_txs, the wrapper still returnsOk(outcome)even though the invariant before Prague1 is strict equality.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/node/evm/builder.rs` around lines 97 - 114, The pre-Prague1 branch currently only rejects num_txs > num_senders because has_mismatch is defined as num_txs > num_senders; change the invariant to require exact equality by either redefining has_mismatch = num_txs != num_senders or by adding an extra check for num_senders > num_txs in the pre-Prague1 branch (keep the existing error path using BlockExecutionError::msg and the same contextual values num_txs, num_senders, timestamp); update only the logic around has_mismatch / the pre-Prague1 if-block so pre-Prague1 returns Err when num_txs != num_senders.
🧹 Nitpick comments (2)
src/node/evm/builder.rs (2)
116-123: Name the PoL protocol constants.
1and0encode the “exactly one injected PoL transaction at index 0” rule in executable code. Hoisting them into named constants would make the invariant easier to audit and reuse.As per coding guidelines, "Extract magic numbers into documented constants".
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/node/evm/builder.rs` around lines 116 - 123, Replace the magic literals 1 and 0 used to enforce "exactly one injected PoL transaction at index 0" with named, documented constants (e.g., EXPECTED_INJECTED_POL_TX_COUNT and INJECTED_POL_TX_INDEX) and use those constants in the checks and accessors: compute num_injected = num_txs - num_senders and compare against EXPECTED_INJECTED_POL_TX_COUNT (instead of 1) when returning BlockExecutionError, and index into outcome.block.body().transactions using INJECTED_POL_TX_INDEX (instead of 0) when assigning pol_tx; add a short doc comment on each constant describing the invariant.
101-120: Promote these invariant failures to typed errors.These are four distinct protocol failures, but
BlockExecutionError::msg(...)flattens them into opaque strings. DedicatedBerachainExecutionErrorvariants would make matching, tests, and telemetry cleaner.As per coding guidelines, "Use custom error types over generic errors".
Also applies to: 127-130
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/node/evm/builder.rs` around lines 101 - 120, Replace the opaque BlockExecutionError::msg(...) strings with typed BerachainExecutionError variants for each distinct invariant (e.g., TransactionSenderMismatchPrePrague, NoPolInjectionPostPrague, UnexpectedInjectedPolCount) and return those variants wrapped as BlockExecutionError (implement or use an existing From<BerachainExecutionError> -> BlockExecutionError if needed). Update the checks in builder.rs (the branches that currently call BlockExecutionError::msg for the pre-Prague mismatch, the post-Prague "no mismatch" case, the "expected exactly 1 injected PoL" case, and the similar cases around lines 127-130) to construct and return the appropriate BerachainExecutionError variant with the contextual fields (num_txs, num_senders, timestamp, num_injected) instead of formatting strings.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Duplicate comments:
In `@src/node/evm/builder.rs`:
- Around line 97-114: The pre-Prague1 branch currently only rejects num_txs >
num_senders because has_mismatch is defined as num_txs > num_senders; change the
invariant to require exact equality by either redefining has_mismatch = num_txs
!= num_senders or by adding an extra check for num_senders > num_txs in the
pre-Prague1 branch (keep the existing error path using BlockExecutionError::msg
and the same contextual values num_txs, num_senders, timestamp); update only the
logic around has_mismatch / the pre-Prague1 if-block so pre-Prague1 returns Err
when num_txs != num_senders.
---
Nitpick comments:
In `@src/node/evm/builder.rs`:
- Around line 116-123: Replace the magic literals 1 and 0 used to enforce
"exactly one injected PoL transaction at index 0" with named, documented
constants (e.g., EXPECTED_INJECTED_POL_TX_COUNT and INJECTED_POL_TX_INDEX) and
use those constants in the checks and accessors: compute num_injected = num_txs
- num_senders and compare against EXPECTED_INJECTED_POL_TX_COUNT (instead of 1)
when returning BlockExecutionError, and index into
outcome.block.body().transactions using INJECTED_POL_TX_INDEX (instead of 0)
when assigning pol_tx; add a short doc comment on each constant describing the
invariant.
- Around line 101-120: Replace the opaque BlockExecutionError::msg(...) strings
with typed BerachainExecutionError variants for each distinct invariant (e.g.,
TransactionSenderMismatchPrePrague, NoPolInjectionPostPrague,
UnexpectedInjectedPolCount) and return those variants wrapped as
BlockExecutionError (implement or use an existing From<BerachainExecutionError>
-> BlockExecutionError if needed). Update the checks in builder.rs (the branches
that currently call BlockExecutionError::msg for the pre-Prague mismatch, the
post-Prague "no mismatch" case, the "expected exactly 1 injected PoL" case, and
the similar cases around lines 127-130) to construct and return the appropriate
BerachainExecutionError variant with the contextual fields (num_txs,
num_senders, timestamp, num_injected) instead of formatting strings.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: b8ef078d-92a4-4496-967e-79e244afdf51
⛔ Files ignored due to path filters (1)
Cargo.lockis excluded by!**/*.lock
📒 Files selected for processing (4)
Cargo.tomlsrc/node/evm/builder.rssrc/node/evm/config.rssrc/node/evm/mod.rs
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/node/evm/builder.rs (1)
99-131: Consider using custom error types instead ofBlockExecutionError::msg().The coding guidelines specify using custom error types over generic errors. The current implementation uses
BlockExecutionError::msg()with format strings in multiple places (lines 101-104, 110-113, 118-120, 127-130).Consider adding these as variants to
BerachainExecutionError(used elsewhere in the codebase) for better error handling and pattern matching:♻️ Example error variant additions
// In your error types module pub enum BerachainExecutionError { // ... existing variants SenderMismatchPrePrague1 { num_txs: usize, num_senders: usize, timestamp: u64 }, MissingPolInjectionPostPrague1 { num_txs: usize, num_senders: usize, timestamp: u64 }, UnexpectedInjectedCount { expected: usize, found: usize }, InvalidPolTransactionType { tx_type: u8 }, }As per coding guidelines: "Use custom error types over generic errors".
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/node/evm/builder.rs` around lines 99 - 131, Replace the generic BlockExecutionError::msg(...) calls in the is_prague1/has_mismatch logic with specific BerachainExecutionError variants: use SenderMismatchPrePrague1 for the pre-Prague1 mismatch check (pass num_txs, num_senders, timestamp), MissingPolInjectionPostPrague1 for the "no transaction/sender mismatch post-Prague1" case (pass num_txs, num_senders, timestamp), UnexpectedInjectedCount for the num_injected != 1 check (pass expected=1, found=num_injected), and InvalidPolTransactionType for the non-PoL first-transaction case (pass the pol_tx.tx_type()). Update the error enum BerachainExecutionError to include these variants and return those variants (converted to BlockExecutionError if needed) from the function instead of using BlockExecutionError::msg().
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/node/evm/builder.rs`:
- Around line 99-131: Replace the generic BlockExecutionError::msg(...) calls in
the is_prague1/has_mismatch logic with specific BerachainExecutionError
variants: use SenderMismatchPrePrague1 for the pre-Prague1 mismatch check (pass
num_txs, num_senders, timestamp), MissingPolInjectionPostPrague1 for the "no
transaction/sender mismatch post-Prague1" case (pass num_txs, num_senders,
timestamp), UnexpectedInjectedCount for the num_injected != 1 check (pass
expected=1, found=num_injected), and InvalidPolTransactionType for the non-PoL
first-transaction case (pass the pol_tx.tx_type()). Update the error enum
BerachainExecutionError to include these variants and return those variants
(converted to BlockExecutionError if needed) from the function instead of using
BlockExecutionError::msg().
Extract fix_pol_senders() from BerachainBlockBuilder::finish() to enable direct unit testing of era-dependent invariants without mocking the full BlockBuilder/BlockExecutor trait chain. https://claude.ai/code/session_01ATVftCVMQV8MFRapJAFVbj
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/node/evm/builder.rs (1)
69-78: Pre-Prague1 invariant check is asymmetric.The check only errors when
num_txs > num_senders, but per the documented invariant (line 39: "Pre-Prague1:num_transactions == num_senders"), any inequality should be an error. Whilenum_senders > num_txsis unlikely in practice (senders derive from transactions), a stricter check would be more defensive.♻️ Suggested fix for symmetric equality check
- let has_mismatch = num_txs > num_senders; + let has_injection_mismatch = num_txs > num_senders; if !is_prague1 { - if has_mismatch { + if num_txs != num_senders { return Err(BlockExecutionError::msg(format!( "transaction/sender mismatch pre-Prague1: {num_txs} txs vs {num_senders} \ senders at timestamp {timestamp}" ))); } return Ok(()); } - if !has_mismatch { + if !has_injection_mismatch {🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/node/evm/builder.rs` around lines 69 - 78, The pre-Prague1 invariant in builder.rs currently only fails when num_txs > num_senders; change the check to enforce strict equality by testing num_txs != num_senders so any mismatch (num_txs > num_senders or num_senders > num_txs) returns Err(BlockExecutionError::msg(...)); update the error message in that branch (and remove the has_mismatch semantic if desired) so it clearly states the expected equality (e.g., "transaction/sender mismatch pre-Prague1: expected equal counts, got {num_txs} txs vs {num_senders} senders at timestamp {timestamp}"), keeping the surrounding is_prague1 logic unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/node/evm/builder.rs`:
- Around line 69-78: The pre-Prague1 invariant in builder.rs currently only
fails when num_txs > num_senders; change the check to enforce strict equality by
testing num_txs != num_senders so any mismatch (num_txs > num_senders or
num_senders > num_txs) returns Err(BlockExecutionError::msg(...)); update the
error message in that branch (and remove the has_mismatch semantic if desired)
so it clearly states the expected equality (e.g., "transaction/sender mismatch
pre-Prague1: expected equal counts, got {num_txs} txs vs {num_senders} senders
at timestamp {timestamp}"), keeping the surrounding is_prague1 logic unchanged.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 86b79532-bb1c-40c3-855e-25e0f889da97
⛔ Files ignored due to path filters (1)
Cargo.lockis excluded by!**/*.lock
📒 Files selected for processing (2)
Cargo.tomlsrc/node/evm/builder.rs
some more refactoring done after your review. if its easier, you can review again so you can port these changes now into sequencer-dev branch to avoid dealing w merge conflicts later
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/node/evm/builder.rs (1)
70-72: Consider using custom error variants instead of generic string errors.Per coding guidelines, custom error types are preferred over generic errors. The
BlockExecutionError::msg(format!(...))pattern works but makes programmatic error handling harder. Consider adding variants toBerachainExecutionErrorfor these cases (e.g.,SenderTransactionMismatch,MissingPolInjection).Also applies to: 78-80, 87-89, 95-96, 104-106
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/node/evm/builder.rs` around lines 70 - 72, Replace the generic BlockExecutionError::msg(...) string errors with concrete enum variants on BerachainExecutionError (e.g., add SenderTransactionMismatch { num_senders: usize, num_txs: usize, timestamp: u64 } and MissingPolInjection { context: String } or similarly named variants) and update the builder code to return those variants instead of formatting strings; specifically modify the places that currently call BlockExecutionError::msg(format!(...)) in builder.rs to construct and return the new BerachainExecutionError variants (retain the original data fields used in the format! calls), and update any From/Display/impl conversions so existing callers still get meaningful messages.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@src/node/evm/builder.rs`:
- Around line 70-72: Replace the generic BlockExecutionError::msg(...) string
errors with concrete enum variants on BerachainExecutionError (e.g., add
SenderTransactionMismatch { num_senders: usize, num_txs: usize, timestamp: u64 }
and MissingPolInjection { context: String } or similarly named variants) and
update the builder code to return those variants instead of formatting strings;
specifically modify the places that currently call
BlockExecutionError::msg(format!(...)) in builder.rs to construct and return the
new BerachainExecutionError variants (retain the original data fields used in
the format! calls), and update any From/Display/impl conversions so existing
callers still get meaningful messages.
Summary
Replaces #203
Resolves #129
This PR introduces a critical fix for a transaction/sender mismatch that occurs when Proof-of-Liquidity (PoL) transactions are injected into blocks after the Prague1 hardfork.
PoL Transaction Sender Mismatch Fix
src/node/evm/builder.rs- IntroducesBerachainBlockBuilderwrapper that fixes sender/transaction misalignmentBasicBlockBuilder::finish()tracks executed transactions but doesn't account for PoL transactions injected byBerachainBlockAssembler, causing senders list to be shorter than transactions listfromfields from injected PoL transactionsSummary by CodeRabbit
Bug Fixes
Tests
Chores