-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Serve typed blocks from recent block cache to avoid repeated deserialization #6400
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,14 +17,16 @@ use crate::{ | |
| prelude::BlockNumber, | ||
| }; | ||
|
|
||
| use super::json_block::EthereumJsonBlock; | ||
|
|
||
| pub type AnyTransaction = Transaction<AnyTxEnvelope>; | ||
| pub type AnyBlock = Block<AnyTransaction, Header<AnyHeader>>; | ||
| /// Like alloy's `AnyTransactionReceipt` but without the `WithOtherFields` wrapper, | ||
| /// avoiding `#[serde(flatten)]` overhead during deserialization. | ||
| pub type AnyTransactionReceiptBare = TransactionReceipt<AnyReceiptEnvelope<Log>>; | ||
|
|
||
| #[allow(dead_code)] | ||
| #[derive(Debug, Deserialize, Serialize)] | ||
| #[derive(Clone, Debug, Deserialize, Serialize)] | ||
| pub struct LightEthereumBlock(AnyBlock); | ||
|
|
||
| impl Default for LightEthereumBlock { | ||
|
|
@@ -259,3 +261,55 @@ impl<'a> From<&'a EthereumCall> for BlockPtr { | |
| BlockPtr::from((call.block_hash, call.block_number)) | ||
| } | ||
| } | ||
|
|
||
| /// Typed cached block for Ethereum. Stores the deserialized block so that | ||
| /// repeated reads from the in-memory cache avoid `serde_json::from_value()`. | ||
| #[derive(Clone, Debug)] | ||
|
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. It would be nice to get rid of |
||
| #[allow(clippy::large_enum_variant)] | ||
| pub enum CachedBlock { | ||
| Full(EthereumBlock), | ||
| Light(LightEthereumBlock), | ||
| } | ||
|
|
||
| impl CachedBlock { | ||
| pub fn light_block(&self) -> &LightEthereumBlock { | ||
| match self { | ||
| CachedBlock::Full(block) => &block.block, | ||
| CachedBlock::Light(block) => block, | ||
| } | ||
| } | ||
|
|
||
| pub fn into_light_block(self) -> LightEthereumBlock { | ||
| match self { | ||
| CachedBlock::Full(block) => block.block.as_ref().clone(), | ||
|
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. If you change this to pub fn into_light_block(self) -> Arc<LightEthereumBlock> {
match self {
CachedBlock::Full(block) => block.block,
CachedBlock::Light(block) => Arc::new(block),
}
}you can avoid a deep clone of the block |
||
| CachedBlock::Light(block) => block, | ||
| } | ||
| } | ||
|
|
||
| pub fn into_full_block(self) -> Option<EthereumBlock> { | ||
| match self { | ||
| CachedBlock::Full(block) => Some(block), | ||
| CachedBlock::Light(_) => None, | ||
| } | ||
| } | ||
|
|
||
| pub fn from_json(value: serde_json::Value) -> Option<Self> { | ||
| let json_block = EthereumJsonBlock::new(value); | ||
| if json_block.is_shallow() { | ||
| return None; | ||
| } | ||
| json_block.try_into_cached_block() | ||
| } | ||
|
|
||
| pub fn timestamp(&self) -> Option<u64> { | ||
| Some(self.light_block().timestamp_u64()) | ||
| } | ||
|
|
||
| pub fn parent_ptr(&self) -> Option<BlockPtr> { | ||
| self.light_block().parent_ptr() | ||
| } | ||
|
|
||
| pub fn ptr(&self) -> BlockPtr { | ||
| self.light_block().block_ptr() | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ use async_trait::async_trait; | |
| use super::*; | ||
| use crate::blockchain::block_stream::{EntitySourceOperation, FirehoseCursor}; | ||
| use crate::blockchain::{BlockTime, ChainIdentifier, ExtendedBlockPtr}; | ||
| use crate::components::ethereum::CachedBlock; | ||
| use crate::components::metrics::stopwatch::StopwatchMetrics; | ||
| use crate::components::network_provider::ChainName; | ||
| use crate::components::server::index_node::VersionInfo; | ||
|
|
@@ -553,8 +554,12 @@ pub trait ChainStore: ChainHeadStore { | |
| ancestor_count: BlockNumber, | ||
| ) -> Result<Option<B256>, Error>; | ||
|
|
||
| /// Returns the blocks present in the store. | ||
| async fn blocks( | ||
| /// Returns the blocks present in the store as typed cached blocks. | ||
| async fn blocks(self: Arc<Self>, hashes: Vec<BlockHash>) -> Result<Vec<CachedBlock>, Error>; | ||
|
|
||
| /// Returns blocks as raw JSON. Used by callers that need the original | ||
| /// JSON representation (e.g., GraphQL block queries, CLI tools). | ||
| async fn blocks_as_json( | ||
|
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. We should try and get rid of this one, but that's also for another PR |
||
| self: Arc<Self>, | ||
| hashes: Vec<BlockHash>, | ||
| ) -> Result<Vec<serde_json::Value>, Error>; | ||
|
|
@@ -584,7 +589,7 @@ pub trait ChainStore: ChainHeadStore { | |
| block_ptr: BlockPtr, | ||
| offset: BlockNumber, | ||
| root: Option<BlockHash>, | ||
| ) -> Result<Option<(serde_json::Value, BlockPtr)>, Error>; | ||
| ) -> Result<Option<(CachedBlock, BlockPtr)>, Error>; | ||
|
|
||
| /// Remove old blocks from the cache we maintain in the database and | ||
| /// return a pair containing the number of the oldest block retained | ||
|
|
||
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.
This then becomes
.map(CachedBlock::into_light_block)