Skip to content
Merged
Show file tree
Hide file tree
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
54 changes: 27 additions & 27 deletions crates/optimism/trie/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,33 +87,6 @@ pub trait OpProofsStore: Send + Sync + Debug {
where
Self: 'tx;

/// Store a batch of account trie branches. Used for saving existing state. For live state
/// capture, use [store_trie_updates](OpProofsStore::store_trie_updates).
fn store_account_branches(
&self,
account_nodes: Vec<(Nibbles, Option<BranchNodeCompact>)>,
) -> OpProofsStorageResult<()>;

/// Store a batch of storage trie branches. Used for saving existing state.
fn store_storage_branches(
&self,
hashed_address: B256,
storage_nodes: Vec<(Nibbles, Option<BranchNodeCompact>)>,
) -> OpProofsStorageResult<()>;

/// Store a batch of account trie leaf nodes. Used for saving existing state.
fn store_hashed_accounts(
&self,
accounts: Vec<(B256, Option<Account>)>,
) -> OpProofsStorageResult<()>;

/// Store a batch of storage trie leaf nodes. Used for saving existing state.
fn store_hashed_storages(
&self,
hashed_address: B256,
storages: Vec<(B256, U256)>,
) -> OpProofsStorageResult<()>;

/// Get the earliest block number and hash that has been stored
///
/// This is used to determine the block number of trie nodes with block number 0.
Expand Down Expand Up @@ -225,6 +198,33 @@ pub trait OpProofsInitialStateStore: Send + Sync + Debug {
/// Returns `Err` if an anchor already exists (prevents accidental overwrite).
fn set_initial_state_anchor(&self, anchor: BlockNumHash) -> OpProofsStorageResult<()>;

/// Store a batch of account trie branches. Used for saving existing state. For live state
/// capture, use [store_trie_updates](OpProofsStore::store_trie_updates).
fn store_account_branches(
&self,
account_nodes: Vec<(Nibbles, Option<BranchNodeCompact>)>,
) -> OpProofsStorageResult<()>;

/// Store a batch of storage trie branches. Used for saving existing state.
fn store_storage_branches(
&self,
hashed_address: B256,
storage_nodes: Vec<(Nibbles, Option<BranchNodeCompact>)>,
) -> OpProofsStorageResult<()>;

/// Store a batch of account trie leaf nodes. Used for saving existing state.
fn store_hashed_accounts(
&self,
accounts: Vec<(B256, Option<Account>)>,
) -> OpProofsStorageResult<()>;

/// Store a batch of storage trie leaf nodes. Used for saving existing state.
fn store_hashed_storages(
&self,
hashed_address: B256,
storages: Vec<(B256, U256)>,
) -> OpProofsStorageResult<()>;

/// Commit the initial state - mark the anchor as completed and also set the earliest block
/// number to anchor.
fn commit_initial_state(&self) -> OpProofsStorageResult<BlockNumHash>;
Expand Down
168 changes: 84 additions & 84 deletions crates/optimism/trie/src/db/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -636,90 +636,6 @@ impl OpProofsStore for MdbxProofsStorage {
where
Self: 'tx;

fn store_account_branches(
&self,
account_nodes: Vec<(Nibbles, Option<BranchNodeCompact>)>,
) -> OpProofsStorageResult<()> {
let mut account_nodes = account_nodes;
if account_nodes.is_empty() {
return Ok(());
}

account_nodes.sort_by_key(|(key, _)| *key);

self.env.update(|tx| {
self.persist_history_batch(tx, 0, account_nodes.into_iter(), true)?;
Ok(())
})?
}

fn store_storage_branches(
&self,
hashed_address: B256,
storage_nodes: Vec<(Nibbles, Option<BranchNodeCompact>)>,
) -> OpProofsStorageResult<()> {
let mut storage_nodes = storage_nodes;
if storage_nodes.is_empty() {
return Ok(());
}

storage_nodes.sort_by_key(|(key, _)| *key);

self.env.update(|tx| {
self.persist_history_batch(
tx,
0,
storage_nodes.into_iter().map(|(path, node)| (hashed_address, path, node)),
true,
)?;
Ok(())
})?
}

fn store_hashed_accounts(
&self,
accounts: Vec<(B256, Option<Account>)>,
) -> OpProofsStorageResult<()> {
let mut accounts = accounts;
if accounts.is_empty() {
return Ok(());
}

// sort the accounts by key to ensure insertion is efficient
accounts.sort_by_key(|(key, _)| *key);

self.env.update(|tx| {
self.persist_history_batch(tx, 0, accounts.into_iter(), true)?;
Ok(())
})?
}

fn store_hashed_storages(
&self,
hashed_address: B256,
storages: Vec<(B256, U256)>,
) -> OpProofsStorageResult<()> {
let mut storages = storages;
if storages.is_empty() {
return Ok(());
}

// sort the storages by key to ensure insertion is efficient
storages.sort_by_key(|(key, _)| *key);

self.env.update(|tx| {
self.persist_history_batch(
tx,
0,
storages
.into_iter()
.map(|(key, val)| (hashed_address, key, Some(StorageValue(val)))),
true,
)?;
Ok(())
})?
}

fn get_earliest_block_number(&self) -> OpProofsStorageResult<Option<(u64, B256)>> {
self.env.view(|tx| self.inner_get_block_number_hash(tx, ProofWindowKey::EarliestBlock))?
}
Expand Down Expand Up @@ -1064,6 +980,90 @@ impl OpProofsInitialStateStore for MdbxProofsStorage {
})?
}

fn store_account_branches(
&self,
account_nodes: Vec<(Nibbles, Option<BranchNodeCompact>)>,
) -> OpProofsStorageResult<()> {
let mut account_nodes = account_nodes;
if account_nodes.is_empty() {
return Ok(());
}

account_nodes.sort_by_key(|(key, _)| *key);

self.env.update(|tx| {
self.persist_history_batch(tx, 0, account_nodes.into_iter(), true)?;
Ok(())
})?
}

fn store_storage_branches(
&self,
hashed_address: B256,
storage_nodes: Vec<(Nibbles, Option<BranchNodeCompact>)>,
) -> OpProofsStorageResult<()> {
let mut storage_nodes = storage_nodes;
if storage_nodes.is_empty() {
return Ok(());
}

storage_nodes.sort_by_key(|(key, _)| *key);

self.env.update(|tx| {
self.persist_history_batch(
tx,
0,
storage_nodes.into_iter().map(|(path, node)| (hashed_address, path, node)),
true,
)?;
Ok(())
})?
}

fn store_hashed_accounts(
&self,
accounts: Vec<(B256, Option<Account>)>,
) -> OpProofsStorageResult<()> {
let mut accounts = accounts;
if accounts.is_empty() {
return Ok(());
}

// sort the accounts by key to ensure insertion is efficient
accounts.sort_by_key(|(key, _)| *key);

self.env.update(|tx| {
self.persist_history_batch(tx, 0, accounts.into_iter(), true)?;
Ok(())
})?
}

fn store_hashed_storages(
&self,
hashed_address: B256,
storages: Vec<(B256, U256)>,
) -> OpProofsStorageResult<()> {
let mut storages = storages;
if storages.is_empty() {
return Ok(());
}

// sort the storages by key to ensure insertion is efficient
storages.sort_by_key(|(key, _)| *key);

self.env.update(|tx| {
self.persist_history_batch(
tx,
0,
storages
.into_iter()
.map(|(key, val)| (hashed_address, key, Some(StorageValue(val)))),
true,
)?;
Ok(())
})?
}

fn commit_initial_state(&self) -> OpProofsStorageResult<BlockNumHash> {
let anchor = self.get_initial_state_anchor()?.ok_or(NoBlocksFound)?;
self.set_earliest_block_number(anchor.number, anchor.hash)?;
Expand Down
Loading
Loading