-
Notifications
You must be signed in to change notification settings - Fork 8
fix: add transaction cache to L1 watcher #433
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
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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
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
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 |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| use crate::error::CacheError; | ||
|
|
||
| use super::{EthRequestError, L1WatcherResult}; | ||
|
|
||
| use std::num::NonZeroUsize; | ||
|
|
||
| use alloy_primitives::{TxHash, B256}; | ||
| use alloy_provider::Provider; | ||
| use alloy_rpc_types_eth::{Transaction, TransactionTrait}; | ||
| use lru::LruCache; | ||
|
|
||
| /// The L1 watcher cache. | ||
| #[derive(Debug)] | ||
| pub(crate) struct Cache { | ||
| transaction_cache: TransactionCache, | ||
| // TODO: introduce block cache. | ||
| } | ||
|
|
||
| impl Cache { | ||
| /// Creates a new [`Cache`] instance with the given capacity for the transaction cache. | ||
| pub(crate) fn new(transaction_cache_capacity: NonZeroUsize) -> Self { | ||
| Self { transaction_cache: TransactionCache::new(transaction_cache_capacity) } | ||
| } | ||
|
|
||
| /// Gets the transaction for the given hash, fetching it from the provider if not cached. | ||
| pub(crate) async fn get_transaction_by_hash<P: Provider>( | ||
| &mut self, | ||
| tx_hash: TxHash, | ||
| provider: &P, | ||
| ) -> L1WatcherResult<Transaction> { | ||
| self.transaction_cache.get_transaction_by_hash(tx_hash, provider).await | ||
| } | ||
|
|
||
| /// Gets the next blob versioned hash for the given transaction hash. | ||
| /// | ||
| /// Errors if the transaction is not in the cache. This method must be called only after | ||
| /// fetching the transaction via [`Self::get_transaction_by_hash`]. | ||
| pub(crate) async fn get_transaction_next_blob_versioned_hash( | ||
| &mut self, | ||
| tx_hash: TxHash, | ||
| ) -> L1WatcherResult<Option<B256>> { | ||
| self.transaction_cache.get_transaction_next_blob_versioned_hash(tx_hash).await | ||
| } | ||
| } | ||
|
|
||
| /// A cache for transactions fetched from the provider. | ||
| #[derive(Debug)] | ||
| struct TransactionCache { | ||
| cache: LruCache<TxHash, TransactionEntry>, | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| struct TransactionEntry { | ||
| transaction: Transaction, | ||
| blob_versioned_hashes: Vec<B256>, | ||
| blob_versioned_hashes_cursor: usize, | ||
| } | ||
|
|
||
| impl TransactionCache { | ||
| fn new(capacity: NonZeroUsize) -> Self { | ||
| Self { cache: LruCache::new(capacity) } | ||
| } | ||
|
|
||
| async fn get_transaction_by_hash<P: Provider>( | ||
| &mut self, | ||
| tx_hash: TxHash, | ||
| provider: &P, | ||
| ) -> L1WatcherResult<Transaction> { | ||
| if let Some(entry) = self.cache.get(&tx_hash) { | ||
| return Ok(entry.transaction.clone()); | ||
| } | ||
|
|
||
| let transaction = provider | ||
| .get_transaction_by_hash(tx_hash) | ||
| .await? | ||
| .ok_or(EthRequestError::MissingTransactionHash(tx_hash))?; | ||
| self.cache.put(tx_hash, transaction.clone().into()); | ||
| Ok(transaction) | ||
| } | ||
|
|
||
| async fn get_transaction_next_blob_versioned_hash( | ||
| &mut self, | ||
| tx_hash: TxHash, | ||
| ) -> L1WatcherResult<Option<B256>> { | ||
| if let Some(entry) = self.cache.get_mut(&tx_hash) { | ||
| let blob_versioned_hash = | ||
| entry.blob_versioned_hashes.get(entry.blob_versioned_hashes_cursor).copied(); | ||
| entry.blob_versioned_hashes_cursor += 1; | ||
| Ok(blob_versioned_hash) | ||
| } else { | ||
| Err(CacheError::MissingTransactionInCacheForBlobVersionedHash(tx_hash).into()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl From<Transaction> for TransactionEntry { | ||
| fn from(transaction: Transaction) -> Self { | ||
| let blob_versioned_hashes = | ||
| transaction.blob_versioned_hashes().map(|hashes| hashes.to_vec()).unwrap_or_default(); | ||
|
|
||
| Self { transaction, blob_versioned_hashes, blob_versioned_hashes_cursor: 0 } | ||
| } | ||
| } | ||
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
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.
just wonder why
get_transaction_next_blob_versioned_hashalso fetches it from the provider when the cache doesn't find it.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.
Because
get_transaction_next_blob_versioned_hashis a stateful process, I thought it would reduce errors if we made the process more explicit and required a specific sequence.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.
Could it be that there is no need to call
get_transaction_next_blob_versioned_hashseparately in the future?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.
Yes, it is possible we can remove this method in the future with a bit of refactoring.