feat: use caching layer when fetching blocks for logs (#2350)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
chirag-bgh
2023-04-23 16:25:29 +05:30
committed by GitHub
parent 370c39525a
commit 1026be0b64
3 changed files with 10 additions and 6 deletions

View File

@ -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
}

View File

@ -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(),

View File

@ -1,3 +1,4 @@
use super::cache::EthStateCache;
use crate::{
eth::{error::EthApiError, logs_utils},
result::{internal_rpc_err, rpc_error_with_code, ToRpcResult},
@ -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) }
}
@ -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>
@ -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(