Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ reth-tracing = { git = "https://github.com/okx/reth", rev = "b6a31f31af91abdecb4
reth-transaction-pool = { git = "https://github.com/okx/reth", rev = "b6a31f31af91abdecb475f2a991906bff9bbef7f" }
reth-optimism-flashblocks = { git = "https://github.com/okx/reth", rev = "b6a31f31af91abdecb475f2a991906bff9bbef7f" }
reth-trie = { git = "https://github.com/okx/reth", rev = "b6a31f31af91abdecb475f2a991906bff9bbef7f" }
reth-trie-db = { git = "https://github.com/okx/reth", rev = "b6a31f31af91abdecb475f2a991906bff9bbef7f" }

# ==============================================================================
# Revm Dependencies (follows upstream reth)
Expand Down Expand Up @@ -201,6 +202,7 @@ eyre = { version = "0.6.12" }
hex = "0.4"
metrics = "0.24.1"
moka = { version = "0.12.11", features = ["sync"] }
criterion = { version = "0.5", features = ["html_reports"] }
once_cell = "1.19"
parking_lot = { version = "0.12.3" }
secp256k1 = { version = "0.30" }
Expand Down
67 changes: 61 additions & 6 deletions crates/builder/src/flashblocks/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ use reth_revm::{
State,
};
use reth_transaction_pool::TransactionPool;
use reth_trie::{updates::TrieUpdates, HashedPostState};
use reth_trie::{updates::TrieUpdates, HashedPostState, TrieInput};
use revm::Database;
use std::{collections::BTreeMap, sync::Arc, time::Instant};
use tokio::sync::mpsc;
Expand Down Expand Up @@ -108,6 +108,9 @@ pub(super) struct FlashblocksState {
/// Index into `ExecutionInfo` tracking the last consumed flashblock.
/// Used for slicing transactions/receipts per flashblock.
last_flashblock_tx_index: usize,
/// Cached trie updates from previous flashblock for incremental state root calculation.
/// None only for the first flashblock; populated after each subsequent state root calculation.
prev_trie_updates: Option<Arc<TrieUpdates>>,
}

impl FlashblocksState {
Expand All @@ -127,6 +130,7 @@ impl FlashblocksState {
target_gas_for_batch,
target_da_for_batch,
target_da_footprint_for_batch,
prev_trie_updates: self.prev_trie_updates.clone(),
..*self
}
}
Expand Down Expand Up @@ -984,26 +988,70 @@ where
// calculate the state root
let state_root_start_time = Instant::now();
let mut state_root = B256::ZERO;
let mut trie_output = TrieUpdates::default();
let mut hashed_state = HashedPostState::default();
let mut trie_updates_to_cache: Option<Arc<TrieUpdates>> = None;

if calculate_state_root {
let state_provider = state.database.as_ref();

// prev_trie_updates is None for the first flashblock.
let prev_trie = fb_state.as_deref().and_then(|s| s.prev_trie_updates.clone());
let flashblock_index = fb_state.as_deref().map(|s| s.flashblock_index()).unwrap_or(0);

hashed_state = state_provider.hashed_post_state(&state.bundle_state);
(state_root, trie_output) = {

let trie_output;
(state_root, trie_output) = if let Some(prev_trie) = prev_trie {
// Incremental path: Use cached trie from previous flashblock
debug!(
target: "payload_builder",
flashblock_index,
"Using incremental state root calculation with cached trie"
);

let trie_input = TrieInput::new(
(*prev_trie).clone(),
hashed_state.clone(),
hashed_state.construct_prefix_sets(),
);

state_provider
.state_root_from_nodes_with_updates(trie_input)
.map_err(PayloadBuilderError::other)?
} else {
debug!(
target: "payload_builder",
flashblock_index,
"Using full state root calculation"
);

state.database.as_ref().state_root_with_updates(hashed_state.clone()).inspect_err(
|err| {
warn!(target: "payload_builder",
parent_header=%ctx.parent().hash(),
warn!(
target: "payload_builder",
parent_header=%ctx.parent().hash(),
%err,
"failed to calculate state root for payload"
);
},
)?
};

// Cache trie updates to apply in fb_state later (avoids mut on fb_state parameter).
// Wrap in Arc once so the same allocation is reused for both `executed` and fb_state.
trie_updates_to_cache = Some(Arc::new(trie_output));

let state_root_calculation_time = state_root_start_time.elapsed();
ctx.metrics.state_root_calculation_duration.record(state_root_calculation_time);
ctx.metrics.state_root_calculation_gauge.set(state_root_calculation_time);

debug!(
target: "payload_builder",
flashblock_index,
state_root = %state_root,
duration_ms = state_root_calculation_time.as_millis(),
"State root calculation completed"
);
}

let mut requests_hash = None;
Expand Down Expand Up @@ -1090,7 +1138,11 @@ where
let executed = BuiltPayloadExecutedBlock {
recovered_block: Arc::new(recovered_block),
execution_output: Arc::new(execution_output),
trie_updates: either::Either::Left(Arc::new(trie_output)),
trie_updates: either::Either::Left(
trie_updates_to_cache
.clone()
.unwrap_or_else(|| Arc::new(TrieUpdates::default())),
),
hashed_state: either::Either::Left(Arc::new(hashed_state)),
};
debug!(
Expand Down Expand Up @@ -1121,6 +1173,9 @@ where

let new_receipts = info.receipts[last_idx..].to_vec();
if let Some(fb) = fb_state {
if let Some(updates) = trie_updates_to_cache.take() {
fb.prev_trie_updates = Some(updates);
}
fb.set_last_flashblock_tx_index(info.executed_transactions.len());
}
let receipts_with_hash = new_transactions
Expand Down