forked from paradigmxyz/reth
-
Notifications
You must be signed in to change notification settings - Fork 10
feat: implement live state collector using external provider #198
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
c365ff3
feat: add proof methods to `reth-optimism-trie`
meyer9 904e81d
feat: add provider to `reth-optimism-trie`
meyer9 78cac62
fix: lint and fix errors
meyer9 b5d5a6c
fix: remove unimplemented method
meyer9 a7ac239
feat: add live trie collector
meyer9 de822d3
fix: format
meyer9 8162a0f
Merge branch 'unstable' into meyer9/169-1-state-provider
emhane f77094e
fix: address PR comments
meyer9 bcd70f1
fix: remove map_err
meyer9 5d8c040
Merge branch 'unstable' into meyer9/169-1-state-provider
meyer9 3f81144
Merge branch 'meyer9/169-1-state-provider' into meyer9/169-2-state-pr…
emhane 7de9798
Merge branch 'unstable' into meyer9/169-1-state-provider
meyer9 bc7bf11
fix: trie cursor split conflict
meyer9 bd63ff6
Merge branch 'meyer9/169-1-state-provider' into meyer9/169-2-state-pr…
meyer9 fc18ad4
fix: use dot syntax for deps
meyer9 411e146
fix: trie cursor defs
meyer9 de95616
Merge branch 'meyer9/169-1-state-provider' into meyer9/169-2-state-pr…
meyer9 30c6b18
Merge branch 'unstable' into meyer9/169-2-state-provider
meyer9 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,3 +31,5 @@ pub mod db; | |
| pub mod proof; | ||
|
|
||
| pub mod provider; | ||
|
|
||
| pub mod live; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| //! Live trie collector for external proofs storage. | ||
|
|
||
| use crate::{ | ||
| api::{BlockStateDiff, OpProofsStorage}, | ||
| provider::OpProofsStateProviderRef, | ||
| }; | ||
| use reth_evm::{execute::Executor, ConfigureEvm}; | ||
| use reth_node_api::{FullNodeComponents, NodePrimitives, NodeTypes}; | ||
| use reth_primitives_traits::{AlloyBlockHeader, RecoveredBlock}; | ||
| use reth_provider::{ | ||
| DatabaseProviderFactory, HashedPostStateProvider, StateProviderFactory, StateReader, | ||
| StateRootProvider, | ||
| }; | ||
| use reth_revm::database::StateProviderDatabase; | ||
| use std::time::Instant; | ||
| use tracing::debug; | ||
|
|
||
| /// Live trie collector for external proofs storage. | ||
| #[derive(Debug)] | ||
| pub struct LiveTrieCollector<Node, PreimageStore> | ||
| where | ||
| Node: FullNodeComponents, | ||
| Node::Provider: StateReader + DatabaseProviderFactory + StateProviderFactory, | ||
| { | ||
| evm_config: Node::Evm, | ||
| provider: Node::Provider, | ||
| storage: PreimageStore, | ||
| } | ||
|
|
||
| impl<Node, Store, Primitives> LiveTrieCollector<Node, Store> | ||
| where | ||
| Node: FullNodeComponents<Types: NodeTypes<Primitives = Primitives>>, | ||
| Primitives: NodePrimitives, | ||
| Store: OpProofsStorage + Clone + 'static, | ||
| { | ||
| /// Create a new `LiveTrieCollector` instance | ||
| pub const fn new(evm_config: Node::Evm, provider: Node::Provider, storage: Store) -> Self { | ||
| Self { evm_config, provider, storage } | ||
| } | ||
|
|
||
| /// Execute a block and store the updates in the storage. | ||
| pub async fn execute_and_store_block_updates( | ||
| &self, | ||
| block: &RecoveredBlock<Primitives::Block>, | ||
| ) -> eyre::Result<()> { | ||
| let start = Instant::now(); | ||
| // ensure that we have the state of the parent block | ||
| let (Some((earliest, _)), Some((latest, _))) = ( | ||
| self.storage.get_earliest_block_number().await?, | ||
| self.storage.get_latest_block_number().await?, | ||
| ) else { | ||
| return Err(eyre::eyre!("No blocks stored")); | ||
| }; | ||
|
|
||
| let fetch_block_duration = start.elapsed(); | ||
|
|
||
| let parent_block_number = block.number() - 1; | ||
| if parent_block_number < earliest { | ||
| return Err(eyre::eyre!( | ||
| "Parent block number is less than earliest stored block number" | ||
| )); | ||
| } | ||
|
|
||
| if parent_block_number > latest { | ||
| return Err(eyre::eyre!( | ||
| "Cannot execute block updates for block {} without parent state {} (latest stored block number: {})", | ||
| block.number(), | ||
| parent_block_number, | ||
| latest | ||
| )); | ||
| } | ||
|
|
||
| let block_number = block.number(); | ||
|
|
||
| // TODO: should we check block hash here? | ||
|
|
||
| let state_provider = OpProofsStateProviderRef::new( | ||
| self.provider.state_by_block_hash(block.parent_hash())?, | ||
| self.storage.clone(), | ||
| parent_block_number, | ||
| ); | ||
|
|
||
| let init_provider_duration = start.elapsed() - fetch_block_duration; | ||
|
|
||
| let db = StateProviderDatabase::new(&state_provider); | ||
| let block_executor = self.evm_config.batch_executor(db); | ||
|
|
||
| let execution_result = | ||
| block_executor.execute(&(*block).clone()).map_err(|err| eyre::eyre!(err))?; | ||
|
|
||
| let execute_block_duration = start.elapsed() - init_provider_duration; | ||
|
|
||
| let hashed_state = state_provider.hashed_post_state(&execution_result.state); | ||
| let (state_root, trie_updates) = | ||
| state_provider.state_root_with_updates(hashed_state.clone())?; | ||
|
|
||
| let calculate_state_root_duration = start.elapsed() - execute_block_duration; | ||
|
|
||
| if state_root != block.state_root() { | ||
| return Err(eyre::eyre!( | ||
| "State root mismatch for block {} (have: {}, expected: {})", | ||
| block.number(), | ||
| state_root, | ||
| block.state_root() | ||
| )); | ||
| } | ||
|
|
||
| self.storage | ||
| .store_trie_updates( | ||
| block_number, | ||
| BlockStateDiff { trie_updates, post_state: hashed_state }, | ||
| ) | ||
| .await?; | ||
|
|
||
| let write_trie_updates_duration = start.elapsed() - calculate_state_root_duration; | ||
|
|
||
| debug!("execute_and_store_block_updates duration: {:?}", start.elapsed()); | ||
| debug!("- fetch_block_duration: {:?}", fetch_block_duration); | ||
| debug!("- init_provider_duration: {:?}", init_provider_duration); | ||
| debug!("- execute_block_duration: {:?}", execute_block_duration); | ||
| debug!("- calculate_state_root_duration: {:?}", calculate_state_root_duration); | ||
| debug!("- write_trie_updates_duration: {:?}", write_trie_updates_duration); | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#245