Skip to content

Commit 0d5c6fa

Browse files
authored
fix: only save sig cache on last drop (#11153)
1 parent 45b8a29 commit 0d5c6fa

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

crates/evm/traces/src/identifier/signatures.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,12 @@ impl SignaturesCache {
149149
/// An identifier that tries to identify functions and events using signatures found at
150150
/// `https://openchain.xyz` or a local cache.
151151
#[derive(Clone, Debug)]
152-
pub struct SignaturesIdentifier {
152+
pub struct SignaturesIdentifier(Arc<SignaturesIdentifierInner>);
153+
154+
#[derive(Debug)]
155+
struct SignaturesIdentifierInner {
153156
/// Cached selectors for functions, events and custom errors.
154-
cache: Arc<RwLock<SignaturesCache>>,
157+
cache: RwLock<SignaturesCache>,
155158
/// Location where to save the signature cache.
156159
cache_path: Option<PathBuf>,
157160
/// The OpenChain client to fetch signatures from. `None` if disabled on construction.
@@ -182,17 +185,16 @@ impl SignaturesIdentifier {
182185
} else {
183186
Default::default()
184187
};
185-
Ok(Self { cache: Arc::new(RwLock::new(cache)), cache_path, client })
188+
Ok(Self(Arc::new(SignaturesIdentifierInner {
189+
cache: RwLock::new(cache),
190+
cache_path,
191+
client,
192+
})))
186193
}
187194

188195
/// Saves the cache to the file system.
189196
pub fn save(&self) {
190-
if let Some(path) = &self.cache_path {
191-
self.cache
192-
.try_read()
193-
.expect("SignaturesIdentifier cache is locked while attempting to save")
194-
.save(path);
195-
}
197+
self.0.save();
196198
}
197199

198200
/// Identifies `Function`s.
@@ -241,20 +243,20 @@ impl SignaturesIdentifier {
241243
}
242244
trace!(target: "evm::traces", ?selectors, "identifying selectors");
243245

244-
let mut cache_r = self.cache.read().await;
245-
if let Some(client) = &self.client {
246+
let mut cache_r = self.0.cache.read().await;
247+
if let Some(client) = &self.0.client {
246248
let query =
247249
selectors.iter().copied().filter(|v| !cache_r.contains_key(v)).collect::<Vec<_>>();
248250
if !query.is_empty() {
249251
drop(cache_r);
250-
let mut cache_w = self.cache.write().await;
252+
let mut cache_w = self.0.cache.write().await;
251253
if let Ok(res) = client.decode_selectors(&query).await {
252254
for (selector, signatures) in std::iter::zip(query, res) {
253255
cache_w.signatures.insert(selector, signatures.into_iter().next());
254256
}
255257
}
256258
drop(cache_w);
257-
cache_r = self.cache.read().await;
259+
cache_r = self.0.cache.read().await;
258260
}
259261
}
260262
selectors.iter().map(|selector| cache_r.get(selector).unwrap_or_default()).collect()
@@ -270,7 +272,18 @@ impl SignaturesIdentifier {
270272
}
271273
}
272274

273-
impl Drop for SignaturesIdentifier {
275+
impl SignaturesIdentifierInner {
276+
fn save(&self) {
277+
if let Some(path) = &self.cache_path {
278+
self.cache
279+
.try_read()
280+
.expect("SignaturesIdentifier cache is locked while attempting to save")
281+
.save(path);
282+
}
283+
}
284+
}
285+
286+
impl Drop for SignaturesIdentifierInner {
274287
fn drop(&mut self) {
275288
self.save();
276289
}

0 commit comments

Comments
 (0)