Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
yash-atreya committed Sep 27, 2024
1 parent c82901c commit 32a7beb
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions crates/provider/src/layers/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,25 +217,28 @@ where
let client = self.inner.weak_client();
let cache = self.cache.clone();
RpcWithBlock::new_provider(move |block_id| {
let keys = keys.clone();
let client = client.clone();
let req = RequestType::Proof((address, keys.clone(), block_id));
let hash = req.params_hash().ok();

if let Some(hash) = hash {
// let cache = cache.read();
if let Some(cached) = cache.write().get(&hash) {
let result = serde_json::from_str(cached).map_err(TransportErrorKind::custom);

return ProviderCall::BoxedFuture(Box::pin(async move {
let res = result?;
Ok(res)
}));
}
}

let client = client.upgrade().ok_or_else(|| {
TransportErrorKind::custom_str("RPC client was dropped before the request was made")
});
let cache = cache.clone();
let keys = keys.clone();
ProviderCall::BoxedFuture(Box::pin(async move {
let req = RequestType::Proof((address, keys.clone(), block_id));

let client = client.upgrade().ok_or_else(|| {
TransportErrorKind::custom_str(
"RPC client was dropped before the request was made",
)
})?;

let hash = req.params_hash()?;

if let Some(cached) = cache.write().get(&hash).cloned() {
let result =
serde_json::from_str(&cached).map_err(TransportErrorKind::custom)?;
return Ok(result);
}
let client = client?;

let result = client
.request("eth_getProof", (address, keys))
Expand All @@ -244,9 +247,11 @@ where

let res = result.await?;

// Insert into cache.
let json_str = serde_json::to_string(&res).map_err(TransportErrorKind::custom)?;

let _ = cache.write().put(hash, json_str);
let hash = req.params_hash()?;
let mut cache = cache.write();
let _ = cache.put(hash, json_str);

Ok(res)
}))
Expand Down

0 comments on commit 32a7beb

Please sign in to comment.