Skip to content

Commit 9eba746

Browse files
committed
fix: flatten Result type of LruCache::flush
1 parent 6713580 commit 9eba746

File tree

2 files changed

+28
-22
lines changed

2 files changed

+28
-22
lines changed

stacks-common/src/util/lru_cache.rs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,18 @@ impl std::fmt::Display for LruCacheCorrupted {
5151

5252
impl std::error::Error for LruCacheCorrupted {}
5353

54+
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
55+
pub enum FlushError<E> {
56+
LruCacheCorrupted,
57+
FlushError(E),
58+
}
59+
60+
impl<E> From<E> for FlushError<E> {
61+
fn from(e: E) -> Self {
62+
FlushError::FlushError(e)
63+
}
64+
}
65+
5466
/// LRU cache
5567
pub struct LruCache<K, V> {
5668
capacity: usize,
@@ -197,14 +209,12 @@ impl<K: Eq + std::hash::Hash + Clone, V: Copy> LruCache<K, V> {
197209
pub fn flush<E>(
198210
&mut self,
199211
mut f: impl FnMut(&K, V) -> Result<(), E>,
200-
) -> Result<Result<(), E>, LruCacheCorrupted> {
212+
) -> Result<(), FlushError<E>> {
201213
for node in self.order.iter_mut().filter(|n| n.dirty) {
202-
match f(&node.key, node.value) {
203-
Ok(()) => node.dirty = false,
204-
Err(e) => return Ok(Err(e)),
205-
}
214+
f(&node.key, node.value)?;
215+
node.dirty = false;
206216
}
207-
Ok(Ok(()))
217+
Ok(())
208218
}
209219

210220
/// Helper function to remove a node from the linked list (by index)
@@ -338,8 +348,7 @@ mod tests {
338348
flushed.push((*k, v));
339349
Ok::<(), ()>(())
340350
})
341-
.expect("cache corrupted")
342-
.expect("flush failed");
351+
.expect("cache corrupted or flush failed");
343352

344353
assert_eq!(flushed, vec![(1, 1)]);
345354

@@ -352,8 +361,7 @@ mod tests {
352361
flushed.push((*k, v));
353362
Ok::<(), ()>(())
354363
})
355-
.expect("cache corrupted")
356-
.expect("flush failed");
364+
.expect("cache corrupted or flush failed");
357365

358366
flushed.sort();
359367
assert_eq!(flushed, vec![(1, 3), (2, 2)]);
@@ -386,8 +394,7 @@ mod tests {
386394
flushed.push((*k, v));
387395
Ok::<(), ()>(())
388396
})
389-
.expect("cache corrupted")
390-
.expect("flush failed");
397+
.expect("cache corrupted or flush failed");
391398

392399
flushed.sort();
393400
assert_eq!(flushed, [(2, 2), (3, 3)]);
@@ -442,8 +449,7 @@ mod tests {
442449
flushed.push((*k, v));
443450
Ok::<(), ()>(())
444451
})
445-
.expect("cache corrupted")
446-
.expect("flush failed");
452+
.expect("cache corrupted or flush failed");
447453

448454
assert_eq!(flushed, vec![(1, 1)]);
449455

@@ -455,8 +461,7 @@ mod tests {
455461
flushed.push((*k, v));
456462
Ok::<(), ()>(())
457463
})
458-
.expect("cache corrupted")
459-
.expect("flush failed");
464+
.expect("cache corrupted or flush failed");
460465

461466
assert_eq!(flushed, vec![(2, 2)]);
462467
}
@@ -548,7 +553,7 @@ mod property_tests {
548553
CacheOp::Insert(k, v) => { cache.insert(k, v).expect("cache corrupted"); }
549554
CacheOp::Get(k) => { cache.get(&k).expect("cache corrupted"); }
550555
CacheOp::InsertClean(k, v) => { cache.insert_clean(k, v).expect("cache corrupted"); }
551-
CacheOp::Flush => { cache.flush(|_, _| Ok::<(), ()>(())).expect("cache corrupted").expect("flush failed"); }
556+
CacheOp::Flush => { cache.flush(|_, _| Ok::<(), ()>(())).expect("cache corrupted or flush failed"); }
552557
}
553558
}
554559
}
@@ -572,7 +577,7 @@ mod property_tests {
572577
CacheOp::Insert(k, v) => { cache.insert(k, v).expect("cache corrupted"); }
573578
CacheOp::Get(k) => { cache.get(&k).expect("cache corrupted"); }
574579
CacheOp::InsertClean(k, v) => { cache.insert_clean(k, v).expect("cache corrupted"); }
575-
CacheOp::Flush => { cache.flush(|_, _| Ok::<(), ()>(())).expect("cache corrupted").expect("flush failed"); }
580+
CacheOp::Flush => { cache.flush(|_, _| Ok::<(), ()>(())).expect("cache corrupted or flush failed"); }
576581
}
577582
// Verify linked list integrity
578583
if !cache.order.is_empty() {
@@ -617,7 +622,7 @@ mod property_tests {
617622
cache.flush(|k, v| {
618623
flushed.push((*k, v));
619624
Ok::<(), ()>(())
620-
}).expect("cache corrupted").expect("flush failed");
625+
}).expect("cache corrupted or flush failed");
621626
simple.flush(|k, v| {
622627
simple_flushed.push((*k, v));
623628
Ok::<(), ()>(())

stackslib/src/core/nonce_cache.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::thread;
1818
use std::time::Duration;
1919

2020
use clarity::types::chainstate::StacksAddress;
21-
use clarity::util::lru_cache::LruCache;
21+
use clarity::util::lru_cache::{FlushError, LruCache, LruCacheCorrupted};
2222
use clarity::vm::clarity::ClarityConnection;
2323
use rand::Rng;
2424
use rusqlite::params;
@@ -181,13 +181,14 @@ impl NonceCache {
181181
tx.execute(sql, params![addr, nonce])?;
182182
Ok::<(), db_error>(())
183183
}) {
184-
Ok(inner) => inner?,
185-
Err(_) => {
184+
Ok(_) => {}
185+
Err(FlushError::LruCacheCorrupted) => {
186186
drop(tx);
187187
// The cache is corrupt, reset it and return
188188
self.reset_cache(conn);
189189
return Ok(());
190190
}
191+
Err(FlushError::FlushError(e)) => return Err(e),
191192
};
192193

193194
tx.commit()?;

0 commit comments

Comments
 (0)