From 09b77eed7d49629b2077a05f9bfbe1d3cbe37ca3 Mon Sep 17 00:00:00 2001 From: MartinquaXD Date: Sat, 4 Jan 2025 11:13:06 +0000 Subject: [PATCH] Remove ttl-cache dependency --- Cargo.lock | 16 ---------------- crates/shared/Cargo.toml | 1 - .../src/sources/uniswap_v2/pool_fetching.rs | 14 ++++++-------- 3 files changed, 6 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d2bbe2848c..469333f95d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2927,12 +2927,6 @@ dependencies = [ "vcpkg", ] -[[package]] -name = "linked-hash-map" -version = "0.5.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" - [[package]] name = "linux-raw-sys" version = "0.4.14" @@ -4511,7 +4505,6 @@ dependencies = [ "tokio", "tracing", "tracing-subscriber", - "ttl_cache", "url", "web3", ] @@ -5420,15 +5413,6 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" -[[package]] -name = "ttl_cache" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4189890526f0168710b6ee65ceaedf1460c48a14318ceec933cb26baa492096a" -dependencies = [ - "linked-hash-map", -] - [[package]] name = "typenum" version = "1.17.0" diff --git a/crates/shared/Cargo.toml b/crates/shared/Cargo.toml index 492b6bfb6e..f6068410f3 100644 --- a/crates/shared/Cargo.toml +++ b/crates/shared/Cargo.toml @@ -22,7 +22,6 @@ contracts = { path = "../contracts" } dashmap = { workspace = true } database = { path = "../database" } derive_more = { workspace = true } -ttl_cache = "0.5" derivative = { workspace = true } ethcontract = { workspace = true } ethrpc = { path = "../ethrpc" } diff --git a/crates/shared/src/sources/uniswap_v2/pool_fetching.rs b/crates/shared/src/sources/uniswap_v2/pool_fetching.rs index 45bc54c536..de75cfecd4 100644 --- a/crates/shared/src/sources/uniswap_v2/pool_fetching.rs +++ b/crates/shared/src/sources/uniswap_v2/pool_fetching.rs @@ -2,6 +2,7 @@ use { super::pair_provider::PairProvider, crate::{baseline_solver::BaselineSolvable, ethrpc::Web3, recent_block_cache::Block}, anyhow::Result, + cached::{Cached, TimedCache}, contracts::{errors::EthcontractErrorType, IUniswapLikePair, ERC20}, ethcontract::{errors::MethodError, BlockId, H160, U256}, futures::{ @@ -11,7 +12,6 @@ use { model::TokenPair, num::rational::Ratio, std::{collections::HashSet, sync::RwLock, time::Duration}, - ttl_cache::TtlCache, }; const POOL_SWAP_GAS_COST: usize = 60_000; @@ -182,8 +182,7 @@ impl BaselineSolvable for Pool { pub struct PoolFetcher { pub pool_reader: Reader, pub web3: Web3, - pub cache_time: Duration, - pub non_existent_pools: RwLock>, + pub non_existent_pools: RwLock>, } impl PoolFetcher { @@ -191,8 +190,7 @@ impl PoolFetcher { Self { pool_reader: reader, web3, - cache_time, - non_existent_pools: RwLock::new(TtlCache::new(usize::MAX)), + non_existent_pools: RwLock::new(TimedCache::with_lifespan(cache_time.as_secs())), } } } @@ -205,8 +203,8 @@ where async fn fetch(&self, token_pairs: HashSet, at_block: Block) -> Result> { let mut token_pairs: Vec<_> = token_pairs.into_iter().collect(); { - let non_existent_pools = self.non_existent_pools.read().unwrap(); - token_pairs.retain(|pair| !non_existent_pools.contains_key(pair)); + let mut non_existent_pools = self.non_existent_pools.write().unwrap(); + token_pairs.retain(|pair| non_existent_pools.cache_get(pair).is_none()); } let block = BlockId::Number(at_block.into()); let futures = token_pairs @@ -228,7 +226,7 @@ where tracing::debug!(token_pairs = ?new_missing_pairs, "stop indexing liquidity"); let mut non_existent_pools = self.non_existent_pools.write().unwrap(); for pair in new_missing_pairs { - non_existent_pools.insert(pair, (), self.cache_time); + non_existent_pools.cache_set(pair, ()); } } Ok(pools)