Skip to content

feat(firehose): add a post-block hook for state changes outside the EVM - #98

Closed
valvecitydev wants to merge 1 commit into
streamingfast:release/reth-2.xfrom
valvecitydev:sf/post-block-hook
Closed

valvecitydev wants to merge 1 commit into
streamingfast:release/reth-2.xfrom
valvecitydev:sf/post-block-hook

Conversation

@valvecitydev

Copy link
Copy Markdown

Some chains change state in the block executor's finish, outside the EVM. The Firehose inspector cannot see those writes. FirehoseWrappedExecutor already has PreTxAdjust and PostTxExtras for gaps around each transaction. This PR adds the same kind of hook at the block level, and makes FirehoseEvmConfig<F> keep the hooks of the configuration it wraps.

The hook

PostTxExtras gains capture_post_block(&self, evm, block_number, chain_id) -> Option<Box<dyn PostBlockChanges>>. The wrapper calls it before the inner finish, so evm reads the state after the block's last transaction. The wrapper then calls PostBlockChanges::emit(tracer, state) after the inner finish and after the withdrawal balance changes, with the final BundleState. The default returns None, so Ethereum output does not change. BlockFees now carries the block number for this call.

We need this hook for PulseChain's PrimordialPulse fork block, which credits balances and replaces a contract in finish. With the hook, that code lives in our PulseChain crate, and reth-firehose keeps no chain-specific code.

Hooks of the wrapped configuration

Before this PR, FirehoseEvmConfig<F> always used NoPreTxAdjust and NoPostTxExtras, on both paths. A chain that wrapped its EVM configuration in FirehoseEvmConfig therefore lost its hooks without an error. Now:

  • FirehoseLiveHooks for FirehoseEvmConfig<F> uses F::PreTxAdjust and F::PostTxExtras (engine path).
  • FirehoseEvmConfig::batch_executor uses a new LiveChainHooks, a ChainHooks that installs the hooks F selects through FirehoseLiveHooks (pipeline path).
  • ConfigureEvm for FirehoseEvmConfig<F> now requires F: FirehoseLiveHooks. EthEvmConfig, NoopEvmConfig and the reth-bb configuration already implement it.

Tests

crates/firehose-tests/tests/post_block_hook.rs (new test binary, process-wide tracer) defines a test EVM configuration with its own hooks. Its PostTxExtras marks each transaction with a balance change, and its capture_post_block records an account's balance before finish and reports that account's change across finish. The test runs the nop_transfer block with one added withdrawal to that account through FirehoseEvmConfig::batch_executor and execute_and_trace_one. It checks that:

  • FirehoseEvmConfig<TestEvmConfig> selects the test configuration's PostTxExtras type for the engine path.
  • The pipeline runs the test configuration's post-transaction hook.
  • The hook's change has the pre-finish balance as old value and the final balance as new value, and it comes after the withdrawal change, by ordinal.

Fail before, pass after. On release/reth-2.x the test does not compile (no capture_post_block, no PostBlockChanges). With only the hook parts of the test removed, it fails on release/reth-2.x at the type check (left: TypeId(..) right: TypeId(..)). With the type check also removed, it fails with "the post-transaction hook did not run". With this PR, it passes. All reth-firehose and reth-firehose-tests tests pass. cargo check passes for reth-engine-tree --tests, reth-node-ethereum --tests and reth --no-default-features.

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, clone_on_copy, double_ended_iterator_last, ptr_arg). With those lints allowed, clippy is clean.

This PR and the pre-Merge reward 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 withdrawals, then pre-Merge rewards, then the chain's post-block changes.

A design question for you: the hook returns a boxed trait object, so a chain can capture any shape of state without a new associated type on PostTxExtras. If you prefer an associated type, I can change it.

Some chains change state in the block executor's finish, outside the
EVM, where the Firehose inspector cannot see it. PostTxExtras gains
capture_post_block, which runs before the inner finish and returns a
PostBlockChanges. The wrapper emits it after the inner finish, after
the withdrawal balance changes. The default captures nothing, so
Ethereum output does not change.

FirehoseEvmConfig<F> now uses F's hooks instead of the no-op ones: on
the engine path through FirehoseLiveHooks, and on the pipeline path
through a new LiveChainHooks for its batch executor. Before, a chain
that wrapped its EVM configuration in FirehoseEvmConfig lost its hooks.
@valvecitydev

Copy link
Copy Markdown
Author

Thanks @maoueh for carrying this forward in #108 with the block context follow-up. Closing in favour of it. I have added you and @UlysseCorbeil as collaborators on valvecitydev/reth, so you can push to our PR branches directly next time.

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