Skip to content

Commit cf79096

Browse files
meyer9dhyaniarun1993
authored andcommitted
chore: separate storage and account cursors (#229)
This PR separates storage and account cursors which makes sense because these two cursors may have very different seek/next implementations. In the case of in-memory, a single cursor impl can still handle both, but for MDBX, it makes sense to implement separate cursors. Closes #236 --------- Co-authored-by: Arun Dhyani <[email protected]>
1 parent 21ef399 commit cf79096

7 files changed

Lines changed: 92 additions & 58 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/optimism/exex/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ reth-node-api.workspace = true
1919
reth-provider.workspace = true
2020
reth-chainspec.workspace = true
2121

22+
# for ethereum types, `serde-bincode-compat` is added by `reth-storage-api`, however this does not work with `op` until
23+
# `reth-storage-api` is updated to support `op`, so we add it here.
24+
reth-optimism-primitives = { workspace = true, features = ["reth-codec", "serde-bincode-compat", "serde"] }
25+
2226
# ethereum
2327
alloy-primitives.workspace = true
2428

crates/optimism/trie/src/api.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ pub struct BlockStateDiff {
7070
#[auto_impl(Arc)]
7171
pub trait OpProofsStorage: Send + Sync + Debug {
7272
/// Cursor for iterating over trie branches.
73-
type TrieCursor: OpProofsTrieCursor;
73+
type StorageTrieCursor: OpProofsTrieCursor;
74+
75+
/// Cursor for iterating over account trie branches.
76+
type AccountTrieCursor: OpProofsTrieCursor;
7477

7578
/// Cursor for iterating over storage leaves.
7679
type StorageCursor: OpProofsHashedCursor<Value = U256>;
@@ -123,11 +126,17 @@ pub trait OpProofsStorage: Send + Sync + Debug {
123126
) -> impl Future<Output = OpProofsStorageResult<Option<(u64, B256)>>> + Send;
124127

125128
/// Get a trie cursor for the storage backend
126-
fn trie_cursor(
129+
fn storage_trie_cursor(
130+
&self,
131+
hashed_address: B256,
132+
max_block_number: u64,
133+
) -> OpProofsStorageResult<Self::StorageTrieCursor>;
134+
135+
/// Get a trie cursor for the account backend
136+
fn account_trie_cursor(
127137
&self,
128-
hashed_address: Option<B256>,
129138
max_block_number: u64,
130-
) -> OpProofsStorageResult<Self::TrieCursor>;
139+
) -> OpProofsStorageResult<Self::AccountTrieCursor>;
131140

132141
/// Get a storage cursor for the storage backend
133142
fn storage_hashed_cursor(

crates/optimism/trie/src/backfill.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ mod tests {
510510
job.backfill_accounts_trie().await.unwrap();
511511

512512
// Verify data was stored
513-
let mut trie_cursor = storage.trie_cursor(None, 100).unwrap();
513+
let mut trie_cursor = storage.account_trie_cursor(100).unwrap();
514514
let mut count = 0;
515515
while let Some((path, _node)) = trie_cursor.next().unwrap() {
516516
assert_eq!(path, nodes[count].0 .0);
@@ -568,7 +568,7 @@ mod tests {
568568
job.backfill_storages_trie().await.unwrap();
569569

570570
// Verify data was stored for addr1
571-
let mut trie_cursor = storage.trie_cursor(Some(addr1), 100).unwrap();
571+
let mut trie_cursor = storage.storage_trie_cursor(addr1, 100).unwrap();
572572
let mut found = vec![];
573573
while let Some((path, _node)) = trie_cursor.next().unwrap() {
574574
found.push(path);
@@ -578,7 +578,7 @@ mod tests {
578578
assert_eq!(found[1], nodes[1].1.nibbles.0);
579579

580580
// Verify data was stored for addr2
581-
let mut trie_cursor = storage.trie_cursor(Some(addr2), 100).unwrap();
581+
let mut trie_cursor = storage.storage_trie_cursor(addr2, 100).unwrap();
582582
let mut found = vec![];
583583
while let Some((path, _node)) = trie_cursor.next().unwrap() {
584584
found.push(path);
@@ -662,10 +662,10 @@ mod tests {
662662
let mut storage_cursor = storage.storage_hashed_cursor(addr, 100).unwrap();
663663
assert!(storage_cursor.next().unwrap().is_some());
664664

665-
let mut trie_cursor = storage.trie_cursor(None, 100).unwrap();
665+
let mut trie_cursor = storage.account_trie_cursor(100).unwrap();
666666
assert!(trie_cursor.next().unwrap().is_some());
667667

668-
let mut storage_trie_cursor = storage.trie_cursor(Some(addr), 100).unwrap();
668+
let mut storage_trie_cursor = storage.storage_trie_cursor(addr, 100).unwrap();
669669
assert!(storage_trie_cursor.next().unwrap().is_some());
670670
}
671671

crates/optimism/trie/src/db/store.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ impl MdbxProofsStorage {
2727
}
2828

2929
impl OpProofsStorage for MdbxProofsStorage {
30-
type TrieCursor = MdbxTrieCursor;
30+
type StorageTrieCursor = MdbxTrieCursor;
31+
type AccountTrieCursor = MdbxTrieCursor;
3132
type StorageCursor = MdbxStorageCursor;
3233
type AccountHashedCursor = MdbxAccountCursor;
3334

@@ -73,11 +74,18 @@ impl OpProofsStorage for MdbxProofsStorage {
7374
unimplemented!()
7475
}
7576

76-
fn trie_cursor(
77+
fn storage_trie_cursor(
78+
&self,
79+
_hashed_address: B256,
80+
_max_block_number: u64,
81+
) -> OpProofsStorageResult<Self::StorageTrieCursor> {
82+
unimplemented!()
83+
}
84+
85+
fn account_trie_cursor(
7786
&self,
78-
_hashed_address: Option<B256>,
7987
_max_block_number: u64,
80-
) -> OpProofsStorageResult<Self::TrieCursor> {
88+
) -> OpProofsStorageResult<Self::AccountTrieCursor> {
8189
unimplemented!()
8290
}
8391

crates/optimism/trie/src/in_memory.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,8 @@ impl OpProofsHashedCursor for InMemoryAccountCursor {
380380
}
381381

382382
impl OpProofsStorage for InMemoryProofsStorage {
383-
type TrieCursor = InMemoryTrieCursor;
383+
type StorageTrieCursor = InMemoryTrieCursor;
384+
type AccountTrieCursor = InMemoryTrieCursor;
384385
type StorageCursor = InMemoryStorageCursor;
385386
type AccountHashedCursor = InMemoryAccountCursor;
386387

@@ -459,17 +460,28 @@ impl OpProofsStorage for InMemoryProofsStorage {
459460
}
460461
}
461462

462-
fn trie_cursor(
463+
fn storage_trie_cursor(
463464
&self,
464-
hashed_address: Option<B256>,
465+
hashed_address: B256,
465466
max_block_number: u64,
466-
) -> OpProofsStorageResult<Self::TrieCursor> {
467+
) -> OpProofsStorageResult<Self::StorageTrieCursor> {
467468
// For synchronous methods, we need to try_read() and handle potential blocking
468469
let inner = self
469470
.inner
470471
.try_read()
471472
.map_err(|_| OpProofsStorageError::Other(eyre::eyre!("Failed to acquire read lock")))?;
472-
Ok(InMemoryTrieCursor::new(&inner, hashed_address, max_block_number))
473+
Ok(InMemoryTrieCursor::new(&inner, Some(hashed_address), max_block_number))
474+
}
475+
476+
fn account_trie_cursor(
477+
&self,
478+
max_block_number: u64,
479+
) -> OpProofsStorageResult<Self::AccountTrieCursor> {
480+
let inner = self
481+
.inner
482+
.try_read()
483+
.map_err(|_| OpProofsStorageError::Other(eyre::eyre!("Failed to acquire read lock")))?;
484+
Ok(InMemoryTrieCursor::new(&inner, None, max_block_number))
473485
}
474486

475487
fn storage_hashed_cursor(

0 commit comments

Comments
 (0)