This repository was archived by the owner on Jan 16, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 212
feat(derive): Online ChainProvider
#93
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
06a40a7
feat(derive): Online `ChainProvider` impl
clabby e8f5a1f
feat(derive): Add start of `L2SafeBlockProvider`
clabby af7711d
feat(derive): Add caching to providers
clabby 02d00a1
chore(derive): Rename `alloy-providers` feature to `online`
clabby e78b15b
chore(derive): rebase
clabby 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
Large diffs are not rendered by default.
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 |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| //! This module contains concrete implementations of the data provider traits, using an alloy | ||
| //! provider on the backend. | ||
|
|
||
| use crate::{ | ||
| traits::{ChainProvider, L2ChainProvider}, | ||
| types::{Block, BlockInfo, ExecutionPayloadEnvelope, L2BlockInfo, RollupConfig}, | ||
| }; | ||
| use alloc::{boxed::Box, sync::Arc, vec::Vec}; | ||
| use alloy_consensus::{Header, Receipt, ReceiptWithBloom, TxEnvelope, TxType}; | ||
| use alloy_primitives::{Bytes, B256, U64}; | ||
| use alloy_provider::Provider; | ||
| use alloy_rlp::{Buf, Decodable}; | ||
| use alloy_transport_http::Http; | ||
| use anyhow::{anyhow, Result}; | ||
| use async_trait::async_trait; | ||
| use core::num::NonZeroUsize; | ||
| use lru::LruCache; | ||
|
|
||
| const CACHE_SIZE: usize = 16; | ||
|
|
||
| /// The [AlloyChainProvider] is a concrete implementation of the [ChainProvider] trait, providing | ||
| /// data over Ethereum JSON-RPC using an alloy provider as the backend. | ||
| /// | ||
| /// **Note**: | ||
| /// This provider fetches data using the `debug_getRawHeader`, `debug_getRawReceipts`, and | ||
| /// `debug_getRawBlock` methods. The RPC must support this namespace. | ||
| #[derive(Debug)] | ||
| pub struct AlloyChainProvider<T: Provider<Http<reqwest::Client>>> { | ||
| /// The inner Ethereum JSON-RPC provider. | ||
| inner: T, | ||
| /// `block_info_by_number` LRU cache. | ||
| block_info_by_number_cache: LruCache<u64, BlockInfo>, | ||
| /// `block_info_by_number` LRU cache. | ||
| receipts_by_hash_cache: LruCache<B256, Vec<Receipt>>, | ||
| /// `block_info_and_transactions_by_hash` LRU cache. | ||
| block_info_and_transactions_by_hash_cache: LruCache<B256, (BlockInfo, Vec<TxEnvelope>)>, | ||
| } | ||
|
|
||
| impl<T: Provider<Http<reqwest::Client>>> AlloyChainProvider<T> { | ||
| /// Creates a new [AlloyChainProvider] with the given alloy provider. | ||
| pub fn new(inner: T) -> Self { | ||
| Self { | ||
| inner, | ||
| block_info_by_number_cache: LruCache::new(NonZeroUsize::new(CACHE_SIZE).unwrap()), | ||
| receipts_by_hash_cache: LruCache::new(NonZeroUsize::new(CACHE_SIZE).unwrap()), | ||
| block_info_and_transactions_by_hash_cache: LruCache::new( | ||
| NonZeroUsize::new(CACHE_SIZE).unwrap(), | ||
| ), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl<T: Provider<Http<reqwest::Client>>> ChainProvider for AlloyChainProvider<T> { | ||
| async fn block_info_by_number(&mut self, number: u64) -> Result<BlockInfo> { | ||
| if let Some(block_info) = self.block_info_by_number_cache.get(&number) { | ||
| return Ok(*block_info); | ||
| } | ||
|
|
||
| let raw_header: Bytes = self | ||
| .inner | ||
| .client() | ||
| .request("debug_getRawHeader", [U64::from(number)]) | ||
| .await | ||
| .map_err(|e| anyhow!(e))?; | ||
| let header = Header::decode(&mut raw_header.as_ref()).map_err(|e| anyhow!(e))?; | ||
|
|
||
| let block_info = BlockInfo { | ||
| hash: header.hash_slow(), | ||
| number, | ||
| parent_hash: header.parent_hash, | ||
| timestamp: header.timestamp, | ||
| }; | ||
| self.block_info_by_number_cache.put(number, block_info); | ||
| Ok(block_info) | ||
| } | ||
|
|
||
| async fn receipts_by_hash(&mut self, hash: B256) -> Result<Vec<Receipt>> { | ||
| if let Some(receipts) = self.receipts_by_hash_cache.get(&hash) { | ||
| return Ok(receipts.clone()); | ||
| } | ||
|
|
||
| let raw_receipts: Vec<Bytes> = self | ||
| .inner | ||
| .client() | ||
| .request("debug_getRawReceipts", [hash]) | ||
| .await | ||
| .map_err(|e| anyhow!(e))?; | ||
|
|
||
| let receipts = raw_receipts | ||
| .iter() | ||
| .map(|r| { | ||
| let r = &mut r.as_ref(); | ||
|
|
||
| // Skip the transaction type byte if it exists | ||
| if !r.is_empty() && r[0] <= TxType::Eip4844 as u8 { | ||
| r.advance(1); | ||
| } | ||
|
|
||
| Ok(ReceiptWithBloom::decode(r).map_err(|e| anyhow!(e))?.receipt) | ||
| }) | ||
| .collect::<Result<Vec<_>>>()?; | ||
| self.receipts_by_hash_cache.put(hash, receipts.clone()); | ||
| Ok(receipts) | ||
| } | ||
|
|
||
| async fn block_info_and_transactions_by_hash( | ||
| &mut self, | ||
| hash: B256, | ||
| ) -> Result<(BlockInfo, Vec<TxEnvelope>)> { | ||
| if let Some(block_info_and_txs) = self.block_info_and_transactions_by_hash_cache.get(&hash) | ||
| { | ||
| return Ok(block_info_and_txs.clone()); | ||
| } | ||
|
|
||
| let raw_block: Bytes = self | ||
| .inner | ||
| .client() | ||
| .request("debug_getRawBlock", [hash]) | ||
| .await | ||
| .map_err(|e| anyhow!(e))?; | ||
| let block = Block::decode(&mut raw_block.as_ref()).map_err(|e| anyhow!(e))?; | ||
|
|
||
| let block_info = BlockInfo { | ||
| hash: block.header.hash_slow(), | ||
| number: block.header.number, | ||
| parent_hash: block.header.parent_hash, | ||
| timestamp: block.header.timestamp, | ||
| }; | ||
| self.block_info_and_transactions_by_hash_cache.put(hash, (block_info, block.body.clone())); | ||
| Ok((block_info, block.body)) | ||
| } | ||
| } | ||
|
|
||
| /// The [AlloyL2SafeHeadProvider] is a concrete implementation of the [L2ChainProvider] trait, | ||
| /// providing data over Ethereum JSON-RPC using an alloy provider as the backend. | ||
| /// | ||
| /// **Note**: | ||
| /// This provider fetches data using the `debug_getRawBlock` method. The RPC must support this | ||
| /// namespace. | ||
| #[derive(Debug)] | ||
| pub struct AlloyL2SafeHeadProvider<T: Provider<Http<reqwest::Client>>> { | ||
| /// The inner Ethereum JSON-RPC provider. | ||
| inner: T, | ||
| /// The rollup configuration. | ||
| rollup_config: Arc<RollupConfig>, | ||
| /// `payload_by_number` LRU cache. | ||
| payload_by_number_cache: LruCache<u64, ExecutionPayloadEnvelope>, | ||
| /// `l2_block_info_by_number` LRU cache. | ||
| l2_block_info_by_number_cache: LruCache<u64, L2BlockInfo>, | ||
| } | ||
|
|
||
| impl<T: Provider<Http<reqwest::Client>>> AlloyL2SafeHeadProvider<T> { | ||
| /// Creates a new [AlloyL2SafeHeadProvider] with the given alloy provider and [RollupConfig]. | ||
| pub fn new(inner: T, rollup_config: Arc<RollupConfig>) -> Self { | ||
| Self { | ||
| inner, | ||
| rollup_config, | ||
| payload_by_number_cache: LruCache::new(NonZeroUsize::new(CACHE_SIZE).unwrap()), | ||
| l2_block_info_by_number_cache: LruCache::new(NonZeroUsize::new(CACHE_SIZE).unwrap()), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl<T: Provider<Http<reqwest::Client>>> L2ChainProvider for AlloyL2SafeHeadProvider<T> { | ||
| async fn l2_block_info_by_number(&mut self, number: u64) -> Result<L2BlockInfo> { | ||
| if let Some(l2_block_info) = self.l2_block_info_by_number_cache.get(&number) { | ||
| return Ok(*l2_block_info); | ||
| } | ||
|
|
||
| let payload = self.payload_by_number(number).await?; | ||
| let l2_block_info = payload.to_l2_block_ref(self.rollup_config.as_ref())?; | ||
| self.l2_block_info_by_number_cache.put(number, l2_block_info); | ||
| Ok(l2_block_info) | ||
| } | ||
|
|
||
| async fn payload_by_number(&mut self, number: u64) -> Result<ExecutionPayloadEnvelope> { | ||
| if let Some(payload) = self.payload_by_number_cache.get(&number) { | ||
| return Ok(payload.clone()); | ||
| } | ||
|
|
||
| let raw_block: Bytes = self | ||
| .inner | ||
| .client() | ||
| .request("debug_getRawBlock", [U64::from(number)]) | ||
| .await | ||
| .map_err(|e| anyhow!(e))?; | ||
| let block = Block::decode(&mut raw_block.as_ref()).map_err(|e| anyhow!(e))?; | ||
| let payload_envelope: ExecutionPayloadEnvelope = block.into(); | ||
|
|
||
| self.payload_by_number_cache.put(number, payload_envelope.clone()); | ||
| Ok(payload_envelope) | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.