-
Notifications
You must be signed in to change notification settings - Fork 11
feat: live collector integration #306
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
Changes from 10 commits
6b28e98
41a68bf
cf74177
528c6ce
397c0c7
8846dee
f2fe8cb
b4f1c9c
8498c42
ba0f0db
86c8870
886f577
c7ac73c
aafbfb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,42 +8,43 @@ | |
| #![cfg_attr(docsrs, feature(doc_cfg))] | ||
| #![cfg_attr(not(test), warn(unused_crate_dependencies))] | ||
|
|
||
| use alloy_consensus::BlockHeader; | ||
| use derive_more::Constructor; | ||
| use futures_util::TryStreamExt; | ||
| use reth_chainspec::ChainInfo; | ||
| use reth_exex::{ExExContext, ExExEvent}; | ||
| use reth_exex::{ExExContext, ExExEvent, ExExNotification}; | ||
| use reth_node_api::{FullNodeComponents, NodePrimitives}; | ||
| use reth_node_types::NodeTypes; | ||
| use reth_optimism_trie::{BackfillJob, OpProofsStore}; | ||
| use reth_optimism_trie::{live::LiveTrieCollector, BackfillJob, OpProofsStorage, OpProofsStore}; | ||
| use reth_provider::{BlockNumReader, DBProvider, DatabaseProviderFactory}; | ||
| use tracing::{debug, error}; | ||
|
|
||
| /// OP Proofs ExEx - processes blocks and tracks state changes within fault proof window. | ||
| /// | ||
| /// Saves and serves trie nodes to make proofs faster. This handles the process of | ||
| /// saving the current state, new blocks as they're added, and serving proof RPCs | ||
| /// based on the saved data. | ||
| #[derive(Debug, Constructor)] | ||
| pub struct OpProofsExEx<Node, S> | ||
| pub struct OpProofsExEx<Node, Storage> | ||
| where | ||
| Node: FullNodeComponents, | ||
| S: OpProofsStore + Clone, | ||
| { | ||
| /// The ExEx context containing the node related utilities e.g. provider, notifications, | ||
| /// events. | ||
| ctx: ExExContext<Node>, | ||
| /// The type of storage DB. | ||
| storage: S, | ||
| storage: OpProofsStorage<Storage>, | ||
| /// The window to span blocks for proofs history. Value is the number of blocks, received as | ||
| /// cli arg. | ||
| #[expect(dead_code)] | ||
| proofs_history_window: u64, | ||
| } | ||
|
|
||
| impl<Node, S, Primitives> OpProofsExEx<Node, S> | ||
| impl<Node, Storage, Primitives> OpProofsExEx<Node, Storage> | ||
| where | ||
| Node: FullNodeComponents<Types: NodeTypes<Primitives = Primitives>>, | ||
| Primitives: NodePrimitives, | ||
| S: OpProofsStore + Clone, | ||
| Storage: OpProofsStore + Clone + 'static, | ||
| { | ||
| /// Main execution loop for the ExEx | ||
| pub async fn run(mut self) -> eyre::Result<()> { | ||
|
|
@@ -53,10 +54,84 @@ where | |
| let ChainInfo { best_number, best_hash } = self.ctx.provider().chain_info()?; | ||
| BackfillJob::new(self.storage.clone(), &db_tx).run(best_number, best_hash).await?; | ||
|
|
||
| let collector = LiveTrieCollector::new( | ||
| self.ctx.evm_config().clone(), | ||
| self.ctx.provider().clone(), | ||
| &self.storage, | ||
| ); | ||
|
|
||
| while let Some(notification) = self.ctx.notifications.try_next().await? { | ||
| // match ¬ification { | ||
| // _ => {} | ||
| // }; | ||
| match ¬ification { | ||
| ExExNotification::ChainCommitted { new } => { | ||
| debug!( | ||
| block_number = new.tip().number(), | ||
| block_hash = ?new.tip().hash(), | ||
| "ChainCommitted notification received", | ||
| ); | ||
|
|
||
| // Get latest stored number (ignore stored hash for now) | ||
| let latest_stored_block_number = | ||
| match self.storage.get_latest_block_number().await? { | ||
| Some((n, _)) => n, | ||
| None => { | ||
| return Err(eyre::eyre!("No blocks stored in proofs storage")); | ||
|
dhyaniarun1993 marked this conversation as resolved.
|
||
| } | ||
| }; | ||
|
|
||
| // If tip is not newer than what we have, nothing to do. | ||
| if new.tip().number() <= latest_stored_block_number { | ||
| debug!( | ||
| block_number = new.tip().number(), | ||
| latest_stored = latest_stored_block_number, | ||
| "Tip number is less than or equal to latest stored, skipping" | ||
| ); | ||
| continue; | ||
| } | ||
|
|
||
| // Start from the next block after the latest stored one. | ||
| let start = latest_stored_block_number.saturating_add(1); | ||
| debug!( | ||
| start, | ||
| end = new.tip().number(), | ||
| "Applying updates for blocks in committed chain" | ||
| ); | ||
| for block_number in start..=new.tip().number() { | ||
| match new.blocks().get(&block_number) { | ||
| Some(block) => { | ||
| collector.execute_and_store_block_updates(block).await?; | ||
| } | ||
| None => { | ||
| error!( | ||
| block_number, | ||
| "Missing block in committed chain; stopping incremental application", | ||
|
dhyaniarun1993 marked this conversation as resolved.
Outdated
|
||
| ); | ||
| return Err(eyre::eyre!( | ||
| "Missing block {} in committed chain", | ||
| block_number | ||
| )); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ExExNotification::ChainReorged { old, new } => { | ||
| debug!( | ||
| old_block_number = old.tip().number(), | ||
| old_block_hash = ?old.tip().hash(), | ||
| new_block_number = new.tip().number(), | ||
| new_block_hash = ?new.tip().hash(), | ||
| "ChainReorged notification received", | ||
| ); | ||
| // handle reorg logic here | ||
| } | ||
| ExExNotification::ChainReverted { old } => { | ||
| debug!( | ||
| old_block_number = old.tip().number(), | ||
| old_block_hash = ?old.tip().hash(), | ||
| "ChainReverted notification received", | ||
| ); | ||
| // handle revert logic here | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we could even do
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Make sense. I have updated the code. |
||
| } | ||
| }; | ||
|
|
||
| if let Some(committed_chain) = notification.committed_chain() { | ||
| self.ctx | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.