Skip to content

Commit 16bdc39

Browse files
WIP: in-mem overlay proptest
1 parent 36e0358 commit 16bdc39

File tree

3 files changed

+295
-64
lines changed

3 files changed

+295
-64
lines changed

crates/trie/trie/src/forward_cursor.rs

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,32 @@ impl<'a, K, V> ForwardInMemoryCursor<'a, K, V> {
2323
self.is_empty
2424
}
2525

26-
/// Returns the last entry iterated to. Returns `None` if never iterated or if exhausted.
26+
/// Returns the current entry pointed to be the cursor, or `None` if no entries are left.
2727
#[inline]
28-
pub fn current(&self) -> Option<&'a (K, V)> {
28+
pub fn current(&self) -> Option<&(K, V)> {
2929
self.entries.clone().next()
3030
}
31-
}
32-
33-
impl<'a, K, V> Iterator for ForwardInMemoryCursor<'a, K, V> {
34-
type Item = &'a (K, V);
3531

36-
fn next(&mut self) -> Option<&'a (K, V)> {
37-
self.entries.next();
38-
self.current()
32+
#[inline]
33+
fn next(&mut self) -> Option<&(K, V)> {
34+
self.entries.next()
3935
}
4036
}
4137

42-
impl<'a, K, V> ForwardInMemoryCursor<'a, K, V>
38+
impl<K, V> ForwardInMemoryCursor<'_, K, V>
4339
where
4440
K: PartialOrd + Clone,
4541
V: Clone,
4642
{
4743
/// Returns the first entry from the current cursor position that's greater or equal to the
4844
/// provided key. This method advances the cursor forward.
49-
pub fn seek(&mut self, key: &K) -> Option<&'a (K, V)> {
45+
pub fn seek(&mut self, key: &K) -> Option<(K, V)> {
5046
self.advance_while(|k| k < key)
5147
}
5248

5349
/// Returns the first entry from the current cursor position that's greater than the provided
5450
/// key. This method advances the cursor forward.
55-
pub fn first_after(&mut self, key: &K) -> Option<&'a (K, V)> {
51+
pub fn first_after(&mut self, key: &K) -> Option<(K, V)> {
5652
self.advance_while(|k| k <= key)
5753
}
5854

@@ -61,15 +57,17 @@ where
6157
///
6258
/// Returns the first entry for which `predicate` returns `false` or `None`. The cursor will
6359
/// point to the returned entry.
64-
fn advance_while(&mut self, predicate: impl Fn(&'a K) -> bool) -> Option<&'a (K, V)> {
60+
fn advance_while(&mut self, predicate: impl Fn(&K) -> bool) -> Option<(K, V)> {
61+
let mut entry;
6562
loop {
66-
if self.current().is_some_and(|(k, _)| predicate(k)) {
67-
self.entries.next();
63+
entry = self.current();
64+
if entry.is_some_and(|(k, _)| predicate(k)) {
65+
self.next();
6866
} else {
6967
break;
7068
}
7169
}
72-
self.current()
70+
entry.cloned()
7371
}
7472
}
7573

@@ -80,17 +78,18 @@ mod tests {
8078
#[test]
8179
fn test_cursor() {
8280
let mut cursor = ForwardInMemoryCursor::new(&[(1, ()), (2, ()), (3, ()), (4, ()), (5, ())]);
81+
assert_eq!(cursor.current(), Some(&(1, ())));
8382

84-
assert_eq!(cursor.seek(&0), Some(&(1, ())));
83+
assert_eq!(cursor.seek(&0), Some((1, ())));
8584
assert_eq!(cursor.current(), Some(&(1, ())));
8685

87-
assert_eq!(cursor.seek(&3), Some(&(3, ())));
86+
assert_eq!(cursor.seek(&3), Some((3, ())));
8887
assert_eq!(cursor.current(), Some(&(3, ())));
8988

90-
assert_eq!(cursor.seek(&3), Some(&(3, ())));
89+
assert_eq!(cursor.seek(&3), Some((3, ())));
9190
assert_eq!(cursor.current(), Some(&(3, ())));
9291

93-
assert_eq!(cursor.seek(&4), Some(&(4, ())));
92+
assert_eq!(cursor.seek(&4), Some((4, ())));
9493
assert_eq!(cursor.current(), Some(&(4, ())));
9594

9695
assert_eq!(cursor.seek(&6), None);

crates/trie/trie/src/hashed_cursor/post_state.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ where
9393
// It's an exact match, return the account from post state without looking up in the
9494
// database.
9595
if post_state_entry.is_some_and(|entry| entry.0 == key) {
96-
return Ok(post_state_entry.copied())
96+
return Ok(post_state_entry)
9797
}
9898

9999
// It's not an exact match, reposition to the first greater or equal account that wasn't
@@ -104,7 +104,7 @@ where
104104
}
105105

106106
// Compare two entries and return the lowest.
107-
Ok(Self::compare_entries(post_state_entry.copied(), db_entry))
107+
Ok(Self::compare_entries(post_state_entry, db_entry))
108108
}
109109

110110
fn next_inner(&mut self, last_account: B256) -> Result<Option<(B256, Account)>, DatabaseError> {
@@ -120,7 +120,7 @@ where
120120
}
121121

122122
// Compare two entries and return the lowest.
123-
Ok(Self::compare_entries(post_state_entry.copied(), db_entry))
123+
Ok(Self::compare_entries(post_state_entry, db_entry))
124124
}
125125

126126
/// Return the account with the lowest hashed account key.
@@ -228,7 +228,7 @@ where
228228
// If database storage was wiped or it's an exact match,
229229
// return the storage slot from post state without looking up in the database.
230230
if self.storage_wiped || post_state_entry.is_some_and(|entry| entry.0 == subkey) {
231-
return Ok(post_state_entry.copied())
231+
return Ok(post_state_entry)
232232
}
233233

234234
// It's not an exact match and storage was not wiped,
@@ -239,7 +239,7 @@ where
239239
}
240240

241241
// Compare two entries and return the lowest.
242-
Ok(Self::compare_entries(post_state_entry.copied(), db_entry))
242+
Ok(Self::compare_entries(post_state_entry, db_entry))
243243
}
244244

245245
/// Find the storage entry that is right after current cursor position.
@@ -250,7 +250,7 @@ where
250250

251251
// Return post state entry immediately if database was wiped.
252252
if self.storage_wiped {
253-
return Ok(post_state_entry.copied())
253+
return Ok(post_state_entry)
254254
}
255255

256256
// If post state was given precedence, move the cursor forward.
@@ -264,7 +264,7 @@ where
264264
}
265265

266266
// Compare two entries and return the lowest.
267-
Ok(Self::compare_entries(post_state_entry.copied(), db_entry))
267+
Ok(Self::compare_entries(post_state_entry, db_entry))
268268
}
269269

270270
/// Return the storage entry with the lowest hashed storage key (hashed slot).

0 commit comments

Comments
 (0)