Skip to content

Commit

Permalink
feat: use caching layer when fetching blocks for logs (#2350)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
  • Loading branch information
chirag-bgh and mattsse authored Apr 23, 2023
1 parent 370c395 commit 1026be0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
4 changes: 2 additions & 2 deletions crates/rpc/rpc-builder/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ where
{
// spawn a new cache task
let eth_cache = EthStateCache::spawn_with(client.clone(), Default::default(), executor);
let eth_api = EthApi::new(client.clone(), pool.clone(), network, eth_cache);
let eth_filter = EthFilter::new(client, pool);
let eth_api = EthApi::new(client.clone(), pool.clone(), network, eth_cache.clone());
let eth_filter = EthFilter::new(client, pool, eth_cache.clone());
launch_with_eth_api(eth_api, eth_filter, engine_api, socket_addr, secret).await
}

Expand Down
2 changes: 1 addition & 1 deletion crates/rpc/rpc-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ where
self.network.clone(),
cache.clone(),
);
let filter = EthFilter::new(self.client.clone(), self.pool.clone());
let filter = EthFilter::new(self.client.clone(), self.pool.clone(), cache.clone());

let pubsub = EthPubSub::new(
self.client.clone(),
Expand Down
10 changes: 7 additions & 3 deletions crates/rpc/rpc/src/eth/filter.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use super::cache::EthStateCache;
use crate::{
eth::{error::EthApiError, logs_utils},
result::{internal_rpc_err, rpc_error_with_code, ToRpcResult},
Expand Down Expand Up @@ -26,13 +27,14 @@ pub struct EthFilter<Client, Pool> {

impl<Client, Pool> EthFilter<Client, Pool> {
/// Creates a new, shareable instance.
pub fn new(client: Client, pool: Pool) -> Self {
pub fn new(client: Client, pool: Pool, eth_cache: EthStateCache) -> Self {
let inner = EthFilterInner {
client,
active_filters: Default::default(),
pool,
id_provider: Arc::new(EthSubscriptionIdProvider::default()),
max_logs_in_response: DEFAULT_MAX_LOGS_IN_RESPONSE,
eth_cache,
};
Self { inner: Arc::new(inner) }
}
Expand Down Expand Up @@ -172,6 +174,8 @@ struct EthFilterInner<Client, Pool> {
id_provider: Arc<dyn IdProvider>,
/// Maximum number of logs that can be returned in a response
max_logs_in_response: usize,
/// The async cache frontend for eth related data
eth_cache: EthStateCache,
}

impl<Client, Pool> EthFilterInner<Client, Pool>
Expand All @@ -185,10 +189,10 @@ where
FilterBlockOption::AtBlockHash(block_hash) => {
let mut all_logs = Vec::new();
// all matching logs in the block, if it exists
if let Some(block) = self.client.block(block_hash.into()).to_rpc_result()? {
if let Some(block) = self.eth_cache.get_block(block_hash).await.to_rpc_result()? {
// get receipts for the block
if let Some(receipts) =
self.client.receipts_by_block(block.number.into()).to_rpc_result()?
self.eth_cache.get_receipts(block_hash).await.to_rpc_result()?
{
let filter = FilteredParams::new(Some(filter));
logs_utils::append_matching_block_logs(
Expand Down

0 comments on commit 1026be0

Please sign in to comment.