Skip to content

chore(trie): Remove ExecutedBlockWithTrieUpdates#18919

Merged
mediocregopher merged 5 commits into18460-trie-changesetsfrom
mediocregopher/18467-trie-cs-engine
Oct 10, 2025
Merged

chore(trie): Remove ExecutedBlockWithTrieUpdates#18919
mediocregopher merged 5 commits into18460-trie-changesetsfrom
mediocregopher/18467-trie-cs-engine

Conversation

@mediocregopher
Copy link
Member

@mediocregopher mediocregopher commented Oct 9, 2025

This PR removes the type ExecutedBlockWithTrieUpdates, as well as the related ExecutedTrieUpdates type. Computation of the TrieInput is modified to take advantage of the trie changesets tables to generate reverts as necessary, the same as is done for hashed post state. This allows us to therefore always have valid TrieUpdates for a block. With this in place we can remove a lot of extraneous logic and types.

There is a new DB method added in this PR which was necessary to produce ExecutedBlocks for historical blocks without re-executing them. The new method calculates the TrieUpdates (not the reverts!) for a historical block based on the changesets.

.get_state(block.header().number())?
.ok_or_else(|| ProviderError::StateForNumberNotFound(block.header().number()))?;
let hashed_state = self.provider.hashed_post_state(execution_output.state());
let trie_updates = self.provider.get_block_trie_updates(block.number())?;
Copy link
Member Author

Choose a reason for hiding this comment

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

This is where the new db method was required to be used, to properly populate ExecutedBlocks that aren't in memory

/// block.
/// 3. Once in-memory blocks are collected and optionally filtered, we compute the
/// [`HashedPostState`] from them.
fn compute_trie_input<TP: DBProvider + BlockNumReader>(
Copy link
Member Author

Choose a reason for hiding this comment

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

We had an entire 1:1 copy of this function in this module for some reason. The version in payload_validator.rs remains.

};
input.append(revert_state);

input.append_cached(revert_trie.into(), revert_state);
Copy link
Member Author

Choose a reason for hiding this comment

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

append_cached specifically does not populate the prefix sets from the revert_state into the trie input. The trie state is fully reverted at this point, so the prefix set is not necessary.

/// Map of hash to trie updates for canonical blocks that are persisted but not finalized.
///
/// Contains the block number for easy removal.
pub(crate) persisted_trie_updates: HashMap<B256, (BlockNumber, Arc<TrieUpdates>)>,
Copy link
Member Author

Choose a reason for hiding this comment

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

This was only used (indirectly) by canonical_block_by_hash to construct historical ExecutedBlocks. We have the DB method for that now.

}
}

impl From<TrieUpdatesSorted> for TrieUpdates {
Copy link
Member Author

Choose a reason for hiding this comment

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

Required because TrieInputs still uses TrieUpdates, but trie_reverts outputs a TrieUpdatesSorted. In a later PR I intend on making TrieInputs use TrieUpdatesSorted as well.

@jenpaff jenpaff linked an issue Oct 9, 2025 that may be closed by this pull request
Copy link
Member

@Rjected Rjected left a comment

Choose a reason for hiding this comment

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

makes sense to me, glad that we can get rid of so much code

Comment on lines -2292 to -2310
// Try reinserting the reorged canonical chain. This is only possible if we have
// `persisted_trie_updates` for those blocks.
let old = old
.iter()
.filter_map(|block| {
let trie = self
.state
.tree_state
.persisted_trie_updates
.get(&block.recovered_block.hash())?
.1
.clone();
Some(ExecutedBlockWithTrieUpdates {
block: block.clone(),
trie: ExecutedTrieUpdates::Present(trie),
})
})
.collect::<Vec<_>>();
self.reinsert_reorged_blocks(old);
Copy link
Member

Choose a reason for hiding this comment

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

so glad we can get rid of this

@github-project-automation github-project-automation bot moved this from Backlog to In Progress in Reth Tracker Oct 9, 2025
Copy link
Member

@yongkangc yongkangc left a comment

Choose a reason for hiding this comment

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

much clearer logic, nice

///
/// Also see [`reth_chain_state::ExecutedTrieUpdates::Missing`].
#[error("Missing ancestor with hash {0}")]
MissingAncestor(B256),
Copy link
Member

Choose a reason for hiding this comment

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

nice, we can remove whole class of error because now we generate trie data for any block from the database on the fly and therefore the parent block's (ancestor's) trie data can never be "missing"

/// calculation, and saves the trie updates.
///
/// Returns an error if the state root calculation fails.
fn get_canonical_blocks_to_persist(
Copy link
Member

Choose a reason for hiding this comment

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

i guess this is the main crux? - where we no longer have to persist blocks missing trie updates because we can compute them on demand

@mediocregopher mediocregopher merged commit 69177e2 into 18460-trie-changesets Oct 10, 2025
38 checks passed
@mediocregopher mediocregopher deleted the mediocregopher/18467-trie-cs-engine branch October 10, 2025 10:20
@github-project-automation github-project-automation bot moved this from In Progress to Done in Reth Tracker Oct 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Trie ChangeSets: Engine API TreeState

3 participants