From 61821ac7327d0821b9818aa58877f34afa0a8fe4 Mon Sep 17 00:00:00 2001 From: Paul Lange Date: Fri, 21 Aug 2026 10:35:54 +0200 Subject: [PATCH] op-reth: use the engine's shared execution cache on the payload build path `build_payload` swept `execution_cache` into the `..` alongside the state root handle, so `--engine.share-execution-cache-with-payload-builder` never reached a build: every payload read accounts, storage and bytecode straight from the provider. Wire it through the same way reth's Ethereum builder does, by wrapping the state provider in a `CachedStateProvider` before the database is built. Reads served this way are only accounts, storage and bytecode; trie and proof reads still go to the inner provider. Expect a closed plumbing gap rather than a latency win. --- rust/Cargo.lock | 1 + rust/Cargo.toml | 1 + rust/op-reth/crates/payload/Cargo.toml | 1 + rust/op-reth/crates/payload/src/builder.rs | 26 ++++++++++++++++++++-- 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 638880c3379..a6edb88b804 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -13617,6 +13617,7 @@ dependencies = [ "reth-basic-payload-builder", "reth-chainspec", "reth-evm", + "reth-execution-cache", "reth-execution-types", "reth-metrics", "reth-optimism-chainspec", diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 10c404b6ef5..d21f41d7bfb 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -388,6 +388,7 @@ reth-evm = { git = "https://github.com/op-rs/reth", rev = "aef8d3ef92117f91455e1 reth-evm-ethereum = { git = "https://github.com/op-rs/reth", rev = "aef8d3ef92117f91455e16969f0adf5bf7c6e9e1" } reth-exex = { git = "https://github.com/op-rs/reth", rev = "aef8d3ef92117f91455e16969f0adf5bf7c6e9e1" } reth-exex-test-utils = { git = "https://github.com/op-rs/reth", rev = "aef8d3ef92117f91455e16969f0adf5bf7c6e9e1" } +reth-execution-cache = { git = "https://github.com/op-rs/reth", rev = "aef8d3ef92117f91455e16969f0adf5bf7c6e9e1" } reth-execution-errors = { git = "https://github.com/op-rs/reth", rev = "aef8d3ef92117f91455e16969f0adf5bf7c6e9e1", default-features = false } reth-execution-types = { git = "https://github.com/op-rs/reth", rev = "aef8d3ef92117f91455e16969f0adf5bf7c6e9e1", default-features = false } reth-fs-util = { git = "https://github.com/op-rs/reth", rev = "aef8d3ef92117f91455e16969f0adf5bf7c6e9e1" } diff --git a/rust/op-reth/crates/payload/Cargo.toml b/rust/op-reth/crates/payload/Cargo.toml index a2d0ef59c9e..d3b02548077 100644 --- a/rust/op-reth/crates/payload/Cargo.toml +++ b/rust/op-reth/crates/payload/Cargo.toml @@ -19,6 +19,7 @@ reth-revm = { workspace = true, features = ["witness"] } reth-transaction-pool.workspace = true reth-storage-api.workspace = true reth-evm.workspace = true +reth-execution-cache.workspace = true reth-execution-types.workspace = true reth-payload-builder-primitives.workspace = true reth-payload-util.workspace = true diff --git a/rust/op-reth/crates/payload/src/builder.rs b/rust/op-reth/crates/payload/src/builder.rs index 85db244f048..9e33af386d7 100644 --- a/rust/op-reth/crates/payload/src/builder.rs +++ b/rust/op-reth/crates/payload/src/builder.rs @@ -22,6 +22,7 @@ use reth_evm::{ BlockBuilder, BlockBuilderOutcome, BlockExecutionError, BlockExecutor, BlockValidationError, }, }; +use reth_execution_cache::{CachedStateMetrics, CachedStateMetricsSource, CachedStateProvider}; use reth_execution_types::BlockExecutionOutput; use reth_metrics::{ Metrics, @@ -254,7 +255,9 @@ where Txs: PayloadTransactions + OpPooledTx>, { - let BuildArguments { mut cached_reads, config, cancel, best_payload, .. } = args; + let BuildArguments { + mut cached_reads, execution_cache, config, cancel, best_payload, .. + } = args; let ctx = OpPayloadBuilderCtx { evm_config: self.evm_config.clone(), @@ -267,7 +270,26 @@ where let builder = OpBuilder::new(best); - let state_provider = self.client.state_by_block_hash(ctx.parent().hash())?; + let mut state_provider = self.client.state_by_block_hash(ctx.parent().hash())?; + + // Serve account, storage and bytecode reads from the engine's cache when it shares one. + // Trie and proof reads still go to the inner provider, which `CachedStateProvider` + // delegates to, so the state root walk in `BlockBuilder::finish` is unaffected. The + // provider reads without ever filling (`CacheFillMode::LookupOnly`), keeping the builder + // out of the engine's way. + if let Some(execution_cache) = execution_cache { + // The cache is only sound on the parent it was populated against. `get_cache_for` + // repairs a stale slot with `clear_with_hash(parent_hash)` before handing it out, so a + // mismatch means reth broke that contract, not a case to handle at runtime. + debug_assert_eq!(execution_cache.executed_block_hash(), ctx.parent().hash()); + + state_provider = Box::new(CachedStateProvider::new( + state_provider, + execution_cache.cache().clone(), + Some(CachedStateMetrics::zeroed(CachedStateMetricsSource::Builder)), + )); + } + let state = StateProviderDatabase::new(&state_provider); if ctx.attributes().no_tx_pool() {