Skip to content

Commit 69b89a1

Browse files
committed
fix entry block-id/hash cache refactor
1 parent c0faf3e commit 69b89a1

File tree

2 files changed

+32
-24
lines changed

2 files changed

+32
-24
lines changed

stackslib/src/chainstate/stacks/index/cache.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,23 @@ impl<T: MarfTrieId> TrieCacheState<T> {
138138
self.block_hash_cache.get(&block_id).cloned()
139139
}
140140

141-
/// Return the HashMap Entry for a block_id
142-
pub fn entry_block_hash(&mut self, block_id: u32) -> Entry<'_, u32, T> {
143-
self.block_hash_cache.entry(block_id)
141+
/// Get cached entry for a block hash, given its ID, or, if not
142+
/// found, use `lookup` to get the corresponding block hash and
143+
/// store it in the cache
144+
pub fn get_block_hash_caching<E, F: FnOnce(u32) -> Result<T, E>>(
145+
&mut self,
146+
id: u32,
147+
lookup: F,
148+
) -> Result<&T, E> {
149+
match self.block_hash_cache.entry(id) {
150+
Entry::Occupied(occupied_entry) => Ok(occupied_entry.into_mut()),
151+
Entry::Vacant(vacant_entry) => {
152+
let block_hash = lookup(id)?;
153+
let block_hash_ref = vacant_entry.insert(block_hash.clone());
154+
self.block_id_cache.insert(block_hash, id);
155+
Ok(block_hash_ref)
156+
}
157+
}
144158
}
145159

146160
/// Cache a block hash, given its ID
@@ -315,9 +329,15 @@ impl<T: MarfTrieId> TrieCache<T> {
315329
self.state_mut().load_block_hash(block_id)
316330
}
317331

318-
/// Get entry for a block hash, given its ID
319-
pub fn entry_block_hash<'a>(&'a mut self, block_id: u32) -> Entry<'a, u32, T> {
320-
self.state_mut().entry_block_hash(block_id)
332+
/// Get cached entry for a block hash, given its ID, or, if not
333+
/// found, use `lookup` to get the corresponding block hash and
334+
/// store it in the cache
335+
pub fn get_block_hash_caching<E, F: FnOnce(u32) -> Result<T, E>>(
336+
&mut self,
337+
id: u32,
338+
lookup: F,
339+
) -> Result<&T, E> {
340+
self.state_mut().get_block_hash_caching(id, lookup)
321341
}
322342

323343
/// Store a block's ID and hash to teh cache.

stackslib/src/chainstate/stacks/index/storage.rs

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,8 @@ impl<T: MarfTrieId> BlockMap for TrieFileStorage<T> {
7373
}
7474

7575
fn get_block_hash_caching(&mut self, id: u32) -> Result<&T, Error> {
76-
if !self.is_block_hash_cached(id) {
77-
let block_hash = self.get_block_hash(id)?;
78-
self.cache.store_block_hash(id, block_hash);
79-
}
80-
self.cache.ref_block_hash(id).ok_or(Error::NotFoundError)
76+
self.cache
77+
.get_block_hash_caching(id, |id| trie_sql::get_block_hash(&self.db, id))
8178
}
8279

8380
fn is_block_hash_cached(&self, id: u32) -> bool {
@@ -110,14 +107,8 @@ impl<T: MarfTrieId> BlockMap for TrieStorageConnection<'_, T> {
110107
}
111108

112109
fn get_block_hash_caching<'a>(&'a mut self, id: u32) -> Result<&'a T, Error> {
113-
match self.cache.entry_block_hash(id) {
114-
Entry::Occupied(occupied_entry) => Ok(occupied_entry.into_mut()),
115-
Entry::Vacant(vacant_entry) => {
116-
let block_hash = trie_sql::get_block_hash(&self.db, id)?;
117-
let block_hash_ref = vacant_entry.insert(block_hash);
118-
Ok(block_hash_ref)
119-
}
120-
}
110+
self.cache
111+
.get_block_hash_caching(id, |id| trie_sql::get_block_hash(&self.db, id))
121112
}
122113

123114
fn is_block_hash_cached(&self, id: u32) -> bool {
@@ -174,11 +165,8 @@ impl<T: MarfTrieId> BlockMap for TrieSqlHashMapCursor<'_, T> {
174165
}
175166

176167
fn get_block_hash_caching(&mut self, id: u32) -> Result<&T, Error> {
177-
if !self.is_block_hash_cached(id) {
178-
let block_hash = self.get_block_hash(id)?;
179-
self.cache.store_block_hash(id, block_hash);
180-
}
181-
self.cache.ref_block_hash(id).ok_or(Error::NotFoundError)
168+
self.cache
169+
.get_block_hash_caching(id, |id| trie_sql::get_block_hash(&self.db, id))
182170
}
183171

184172
fn is_block_hash_cached(&self, id: u32) -> bool {

0 commit comments

Comments
 (0)