Skip to content
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

feat(rpc-eth-types+api): add latest chain state tracking to EthStateCache and EthStateCacheService #13164

Conversation

lean-apple
Copy link
Contributor

Closes #13069.

Only use latest_block_with_senders in crates/rpc/rpc-eth-api/src/helpers/block.rs for now.

Comment on lines +265 to +298
let block = match block_id {
BlockId::Number(BlockNumberOrTag::Latest) => {
if let Some(block) = self
.cache()
.latest_block_with_senders()
.await
.map_err(Self::Error::from_eth_err)?
{
Some(block)
} else {
// Fallback to traditional lookup if latest isn't cached
match self
.provider()
.block_hash_for_id(block_id)
.map_err(Self::Error::from_eth_err)?
{
Some(block_hash) => self
.cache()
.get_sealed_block_with_senders(block_hash)
.await
.map_err(Self::Error::from_eth_err)?,
None => None,
}
}
}
_ => {
let block_hash = match self
.provider()
.block_hash_for_id(block_id)
.map_err(Self::Error::from_eth_err)?
{
Some(block_hash) => block_hash,
None => return Ok(None),
};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattsse A bit stuck on the integration of the new function latest_block_with_senders, I have to add this fallback to fix the integration test but the code is very redundant

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can look at this later, I believe we can simplify this a bit by introducing a tmp var let mut maybe_block = None;

and then do a if block_id.is_latest() { maybe_block = ... }

Copy link
Collaborator

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the last entry in this list will always be the latest block

CacheAction::CacheNewCanonicalChain { chain_change } => {
for block in chain_change.blocks {
this.on_new_block(block.hash(), Ok(Some(Arc::new(block))));
}

so we can track that separately.

and to fetch the latest block, we can add a new action variant, like this but without the hash:

GetBlockWithSenders {
block_hash: B256,
response_tx: BlockWithSendersResponseSender<B>,
},

Comment on lines +265 to +266
let block = match block_id {
BlockId::Number(BlockNumberOrTag::Latest) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can use is_latest() here

fn clone(&self) -> Self {
Self { to_service: self.to_service.clone() }
}
latest_chain_change: Option<ChainChange<B, R>>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need to share this and can instead rely on message passing and always fetch this from the service

Comment on lines +193 to +197
if let Some(chain_change) = &self.latest_chain_change {
if let Some(latest_block) = chain_change.blocks.last() {
return self.get_sealed_block_with_senders(latest_block.hash()).await;
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should always send an action to the service, but for fetching the latest, we'd need a new action variant

@mattsse mattsse added C-enhancement New feature or request A-rpc Related to the RPC implementation labels Dec 10, 2024
mattsse and others added 20 commits December 11, 2024 10:37
mattsse and others added 28 commits December 11, 2024 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-rpc Related to the RPC implementation C-enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Track latest block in rpc cache