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
27 changes: 15 additions & 12 deletions crates/store/src/state/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
//! - **Persistent mode** (`rocksdb` feature enabled): Trees are loaded from persistent storage if
//! data exists, otherwise rebuilt from the database and persisted.

use std::future::Future;
use std::path::Path;

use miden_protocol::Word;
use miden_protocol::block::account_tree::account_id_to_smt_key;
use miden_protocol::block::account_tree::{AccountTree, account_id_to_smt_key};
use miden_protocol::block::nullifier_tree::NullifierTree;
use miden_protocol::block::{BlockHeader, BlockNumber, Blockchain};
#[cfg(not(feature = "rocksdb"))]
Expand Down Expand Up @@ -84,15 +85,13 @@ pub trait StorageLoader: SmtStorage + Sized {
fn load_account_tree(
self,
db: &mut Db,
) -> impl std::future::Future<Output = Result<LargeSmt<Self>, StateInitializationError>> + Send;
) -> impl Future<Output = Result<AccountTree<LargeSmt<Self>>, StateInitializationError>> + Send;

/// Loads a nullifier tree, either from persistent storage or by rebuilding from DB.
fn load_nullifier_tree(
self,
db: &mut Db,
) -> impl std::future::Future<
Output = Result<NullifierTree<LargeSmt<Self>>, StateInitializationError>,
> + Send;
) -> impl Future<Output = Result<NullifierTree<LargeSmt<Self>>, StateInitializationError>> + Send;
}

// MEMORY STORAGE IMPLEMENTATION
Expand All @@ -107,13 +106,14 @@ impl StorageLoader for MemoryStorage {
async fn load_account_tree(
self,
db: &mut Db,
) -> Result<LargeSmt<Self>, StateInitializationError> {
) -> Result<AccountTree<LargeSmt<Self>>, StateInitializationError> {
let account_data = db.select_all_account_commitments().await?;
let smt_entries = account_data
.into_iter()
.map(|(id, commitment)| (account_id_to_smt_key(id), commitment));
LargeSmt::with_entries(self, smt_entries)
.map_err(account_tree_large_smt_error_to_init_error)
let smt = LargeSmt::with_entries(self, smt_entries)
.map_err(account_tree_large_smt_error_to_init_error)?;
AccountTree::new(smt).map_err(StateInitializationError::FailedToCreateAccountsTree)
}

async fn load_nullifier_tree(
Expand Down Expand Up @@ -144,22 +144,25 @@ impl StorageLoader for RocksDbStorage {
async fn load_account_tree(
self,
db: &mut Db,
) -> Result<LargeSmt<Self>, StateInitializationError> {
) -> Result<AccountTree<LargeSmt<Self>>, StateInitializationError> {
// If RocksDB storage has data, load from it directly
let has_data = self
.has_leaves()
.map_err(|e| StateInitializationError::AccountTreeIoError(e.to_string()))?;
if has_data {
return load_smt(self);
let smt = load_smt(self)?;
return AccountTree::new(smt)
.map_err(StateInitializationError::FailedToCreateAccountsTree);
}

info!(target: COMPONENT, "RocksDB account tree storage is empty, populating from SQLite");
let account_data = db.select_all_account_commitments().await?;
let smt_entries = account_data
.into_iter()
.map(|(id, commitment)| (account_id_to_smt_key(id), commitment));
LargeSmt::with_entries(self, smt_entries)
.map_err(account_tree_large_smt_error_to_init_error)
let smt = LargeSmt::with_entries(self, smt_entries)
.map_err(account_tree_large_smt_error_to_init_error)?;
AccountTree::new(smt).map_err(StateInitializationError::FailedToCreateAccountsTree)
}

async fn load_nullifier_tree(
Expand Down
14 changes: 5 additions & 9 deletions crates/store/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ use miden_protocol::Word;
use miden_protocol::account::delta::AccountUpdateDetails;
use miden_protocol::account::{AccountId, StorageMapWitness, StorageSlotName};
use miden_protocol::asset::{AssetVaultKey, AssetWitness};
use miden_protocol::block::account_tree::{AccountTree, AccountWitness};
use miden_protocol::block::nullifier_tree::NullifierWitness;
use miden_protocol::block::account_tree::AccountWitness;
use miden_protocol::block::nullifier_tree::{NullifierTree, NullifierWitness};
use miden_protocol::block::{BlockHeader, BlockInputs, BlockNumber, Blockchain, ProvenBlock};
use miden_protocol::crypto::merkle::mmr::{Forest, MmrDelta, MmrPeaks, MmrProof, PartialMmr};
use miden_protocol::crypto::merkle::smt::{SmtProof, SmtStorage};
use miden_protocol::crypto::merkle::smt::{LargeSmt, SmtProof, SmtStorage};
use miden_protocol::note::{NoteDetails, NoteId, NoteScript, Nullifier};
use miden_protocol::transaction::{OutputNote, PartialBlockchain};
use miden_protocol::utils::Serializable;
Expand Down Expand Up @@ -91,9 +91,7 @@ struct InnerState<S>
where
S: SmtStorage,
{
nullifier_tree: miden_protocol::block::nullifier_tree::NullifierTree<
miden_protocol::crypto::merkle::smt::LargeSmt<S>,
>,
nullifier_tree: NullifierTree<LargeSmt<S>>,
blockchain: Blockchain,
account_tree: AccountTreeWithHistory<S>,
}
Expand Down Expand Up @@ -156,9 +154,7 @@ impl State {
let latest_block_num = blockchain.chain_tip().unwrap_or(BlockNumber::GENESIS);

let account_storage = TreeStorage::create(data_path, ACCOUNT_TREE_STORAGE_DIR)?;
let smt = account_storage.load_account_tree(&mut db).await?;
let account_tree =
AccountTree::new(smt).map_err(StateInitializationError::FailedToCreateAccountsTree)?;
let account_tree = account_storage.load_account_tree(&mut db).await?;

let nullifier_storage = TreeStorage::create(data_path, NULLIFIER_TREE_STORAGE_DIR)?;
let nullifier_tree = nullifier_storage.load_nullifier_tree(&mut db).await?;
Expand Down
Loading