Skip to content

feat(firehose): emit pre-Merge block and uncle reward balance changes - #97

Open
valvecitydev wants to merge 2 commits into
streamingfast:release/reth-2.xfrom
valvecitydev:sf/pre-merge-rewards
Open

valvecitydev wants to merge 2 commits into
streamingfast:release/reth-2.xfrom
valvecitydev:sf/pre-merge-rewards

Conversation

@valvecitydev

@valvecitydev valvecitydev commented Nov 12, 2026 •

Copy link
Copy Markdown

Before the Merge, the block executor credits the miner reward and the uncle rewards in finish, through increment_balances. That path bypasses the EVM, so FirehoseInspector never sees it. A Firehose block for a proof-of-work block therefore has no REASON_REWARD_MINE_BLOCK change and no REASON_REWARD_MINE_UNCLE change, but go-ethereum's Firehose emits both. Any chain synced from genesis hits this: Ethereum mainnet before block 15,537,394, the early history of the testnets, and forks of Ethereum history.

This PR gives FirehoseWrappedExecutor the ommers of the block through a new with_ommers builder method. run_wrapped_block, which the pipeline uses, reads them from the block body. The engine path needs no change, because a post-Merge block has no ommers and no static reward.

Before the inner finish, the wrapper computes the credits and reads the balance of each credited account. After the inner finish, and before the withdrawal changes, it emits one RewardMineUncle change per ommer in block-body order, then the miner's RewardMineBlock change. This is the order of go-ethereum's ethash accumulateRewards. Each change spans only its own credit. So when the miner is also an uncle beneficiary, or two uncles share a beneficiary, the account gets one change per credit with running balances, as in go-ethereum.

The amounts come from evm2's own ommer_reward and block_reward. The base reward comes from the block's spec: 5 ETH, 3 ETH from Byzantium, 2 ETH from Petersburg, none from the Merge. evm2 has no Constantinople spec, but the executor switches to 2 ETH at Constantinople. On Ethereum mainnet both forks activate at the same block. On a chain where they differ, the computed credits do not match the paid increase, so the wrapper falls back to one change per account over its whole increase and logs a warning. The final balances stay correct in that case.

When no credited account's balance changes, the executor paid no reward, so the wrapper emits nothing. This covers a chain that replays post-Merge blocks under a pre-Merge spec. From the Merge on, by the block's spec, there is no static reward, so the wrapper reads no state.

Tests

  • crates/firehose-tests/tests/block_reward.rs (new test binary, process-wide tracer): a Petersburg block at height 100 with two ommers, one paying a separate account and one paying the miner, runs through FirehoseBlockExecutor::execute_and_trace_one. It checks the exact block-level balance changes, their order and their ordinals, and that the emitted values end at the executor's final balances.
  • Unit tests in executor.rs (tests::block_reward): base reward per spec, credit order and amounts, sequential changes, an account credited twice, a miner that is also an uncle beneficiary, no reward paid, and the mismatch fallback.

Fail before, pass after: with executor.rs from release/reth-2.x, block_reward fails with left: [] (no reward change at all). With this PR it passes. The unit tests need the new functions, so they do not compile on release/reth-2.x. All reth-firehose and reth-firehose-tests tests pass (92 unit tests in the crate, plus every integration binary).

I have no go-ethereum Firehose golden for a block with uncles. The expected values in the test follow go-ethereum's accumulateRewards by hand. A golden from go-ethereum's Firehose would be a good addition if you have one.

Notes

cargo clippy -p reth-firehose -p reth-firehose-tests --all-targets -- -D warnings fails on release/reth-2.x already, on lines this PR does not touch (doc_markdown, missing_const_for_fn, use_self, if_then_some_else_none and clone_on_copy in lib.rs, mapper.rs and prelude.rs; double_ended_iterator_last and ptr_arg in firehose-tests). With those lints allowed, clippy is clean.

This PR and the post-block hook PR both change the two finish_* methods of FirehoseWrappedExecutor and add BlockFees::block_number. Whichever lands second needs a small rebase. The intended order of block-level changes is rewards, then withdrawals, then chain-specific post-block changes. A block has rewards or withdrawals, never both.

Before the Merge, the block executor credits the miner and uncle
rewards in finish, outside the EVM, so the inspector never sees them
and the Firehose block has no RewardMineBlock or RewardMineUncle
change. Geth's Firehose emits both.

The wrapper now gets the block's ommers (run_wrapped_block passes them
through with_ommers), reads each recipient's balance before the inner
finish, and after it emits one RewardMineUncle change per ommer, then
the miner's RewardMineBlock change, in the order Geth's ethash
accumulateRewards applies them. Each change spans only its own credit,
so an account credited twice gets two changes. The amounts use evm2's
ommer_reward and block_reward. A block with withdrawals has no static
reward, so the wrapper skips it.
@dfuse-bot
dfuse-bot requested a review from billettc November 12, 2026 01:12
Comment on lines +658 to +660
if let Some(reward) = &reward {
emit_block_reward_balance_changes(&inspector, reward, &output.state);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put that just before withdrawal.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 5c8e9d6. Rewards are now emitted before the withdrawal changes, in both finish methods.

Comment thread crates/firehose/src/executor.rs Outdated
Comment on lines +495 to +499
// The static block reward exists only before the Merge, and every post-Shanghai block
// carries withdrawals, so a block with withdrawals has no reward to sample.
if self.withdrawals.is_some() {
return None;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we check for a specific fork instead of indirect knoweldge, I think the executor has access spec and can check if a specific fork is active or not if I remember right.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 5c8e9d6. It now checks self.fees.spec.enables(SpecId::MERGE), the same rule base_block_reward uses.

Comment thread crates/firehose/src/executor.rs Outdated
pre.credits.iter().filter(|c| c.address == address).map(|c| c.amount).sum::<U256>()
};

if pre.pre.iter().all(|&(address, before)| increase(address, before) == Some(U256::ZERO)) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pre.pre bothers me, I think I would opt to rename the struct field

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed to balances_before in 5c8e9d6.

Comment thread crates/firehose/src/executor.rs Outdated
/// | `on_tx_end(Some(receipt_data))` | After `commit_transaction`. |
/// | withdrawal balance changes | After the inner finish, so they land in |
/// | | `block.balance_changes`. |
/// | pre-Merge block and uncle rewards | After the withdrawal balance changes. |

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make more sense before withdrawal changes IMO, since rewards were there before withdrawals and there can be no withdrawals when rewards exists.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. The table now lists rewards before withdrawals and says a block has one or the other.

…em before withdrawals

Review on paradigmxyz#97:

- The wrapper now skips the reward pre-state from the Merge on, by the
  block's spec, instead of inferring the Merge from withdrawals.
- Rewards are emitted before the withdrawal balance changes, in both finish
  methods and in the hook-order table. A block has one or the other.
- `BlockRewardPreState::pre` is now `balances_before`.
@billettc
billettc removed their request for review November 16, 2026 12:38
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.

2 participants