Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions crates/optimism/trie/src/backfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,28 @@ async fn backfill<
Ok(total_entries)
}

/// Save storage branches to storage.
async fn save_storage_branches<S: OpProofsStore>(
storage: &S,
entries: Vec<(B256, StorageTrieEntry)>,
) -> Result<(), OpProofsStorageError> {
// Group entries by hashed address
let mut by_address: HashMap<B256, Vec<(Nibbles, Option<BranchNodeCompact>)>> =
HashMap::default();
for (hashed_address, storage_entry) in entries {
by_address
.entry(hashed_address)
.or_default()
.push((storage_entry.nibbles.0, Some(storage_entry.node)));
}

// Store each address's storage trie branches
for (address, branches) in by_address {
storage.store_storage_branches(address, branches).await?;
}
Ok(())
}

impl<'a, Tx: DbTx, S: OpProofsStore + Send> BackfillJob<'a, Tx, S> {
/// Create a new backfill job.
pub const fn new(storage: S, tx: &'a Tx) -> Self {
Expand Down Expand Up @@ -324,31 +346,12 @@ impl<'a, Tx: DbTx, S: OpProofsStore + Send> BackfillJob<'a, Tx, S> {

let source = StoragesTrieIter::new(start_cursor);
let storage = &self.storage;
let save_fn =
async |entries: Vec<(B256, StorageTrieEntry)>| -> Result<(), OpProofsStorageError> {
// Group entries by hashed address
let mut by_address: HashMap<B256, Vec<(Nibbles, Option<BranchNodeCompact>)>> =
HashMap::default();
for (hashed_address, storage_entry) in entries {
by_address
.entry(hashed_address)
.or_default()
.push((storage_entry.nibbles.0, Some(storage_entry.node)));
}

// Store each address's storage trie branches
for (address, branches) in by_address {
storage.store_storage_branches(address, branches).await?;
}
Ok(())
};

backfill(
"storage trie",
source,
BACKFILL_STORAGE_THRESHOLD,
BACKFILL_LOG_THRESHOLD,
save_fn,
|entries| save_storage_branches(storage, entries),
)
.await?;

Expand Down
Loading