Repository navigation
feat(firehose): emit pre-Merge block and uncle reward balance changes - #97
valvecitydev wants to merge 2 commits into
Conversation
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.
| if let Some(reward) = &reward { | ||
| emit_block_reward_balance_changes(&inspector, reward, &output.state); | ||
| } |
There was a problem hiding this comment.
I would put that just before withdrawal.
There was a problem hiding this comment.
Done in 5c8e9d6. Rewards are now emitted before the withdrawal changes, in both finish methods.
| // 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; | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Done in 5c8e9d6. It now checks self.fees.spec.enables(SpecId::MERGE), the same rule base_block_reward uses.
| 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)) { |
There was a problem hiding this comment.
pre.pre bothers me, I think I would opt to rename the struct field
| /// | `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. | |
There was a problem hiding this comment.
Make more sense before withdrawal changes IMO, since rewards were there before withdrawals and there can be no withdrawals when rewards exists.
There was a problem hiding this comment.
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`.
Before the Merge, the block executor credits the miner reward and the uncle rewards in
finish, throughincrement_balances. That path bypasses the EVM, soFirehoseInspectornever sees it. A Firehose block for a proof-of-work block therefore has noREASON_REWARD_MINE_BLOCKchange and noREASON_REWARD_MINE_UNCLEchange, 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
FirehoseWrappedExecutorthe ommers of the block through a newwith_ommersbuilder 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
RewardMineUnclechange per ommer in block-body order, then the miner'sRewardMineBlockchange. This is the order of go-ethereum's ethashaccumulateRewards. 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_rewardandblock_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 throughFirehoseBlockExecutor::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.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.rsfromrelease/reth-2.x,block_rewardfails withleft: [](no reward change at all). With this PR it passes. The unit tests need the new functions, so they do not compile onrelease/reth-2.x. Allreth-firehoseandreth-firehose-teststests 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
accumulateRewardsby 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 warningsfails onrelease/reth-2.xalready, on lines this PR does not touch (doc_markdown,missing_const_for_fn,use_self,if_then_some_else_noneandclone_on_copyinlib.rs,mapper.rsandprelude.rs;double_ended_iterator_lastandptr_arginfirehose-tests). With those lints allowed, clippy is clean.This PR and the post-block hook PR both change the two
finish_*methods ofFirehoseWrappedExecutorand addBlockFees::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.