Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
59 changes: 30 additions & 29 deletions crates/sqlite-store/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ use miden_client::account::{
StorageSlotType,
};
use miden_client::asset::{Asset, AssetVault, AssetWitness, FungibleAsset, NonFungibleDeltaAction};
use miden_client::crypto::{MerkleStore, SmtLeaf, SmtProof};
use miden_client::crypto::{SmtLeaf, SmtProof};
use miden_client::store::{AccountRecord, AccountStatus, StoreError};
use miden_client::sync::NoteTagRecord;
use miden_client::utils::{Deserializable, Serializable};
use miden_client::{AccountError, Felt, Word};
use miden_objects::account::StorageMapWitness;
use miden_objects::asset::AssetVaultKey;
use miden_objects::crypto::merkle::SmtForest;
use rusqlite::types::Value;
use rusqlite::{Connection, Params, Transaction, named_params, params};

Expand Down Expand Up @@ -186,7 +187,7 @@ impl SqliteStore {

pub(crate) fn insert_account(
conn: &mut Connection,
merkle_store: &Arc<RwLock<MerkleStore>>,
smt_forest: &Arc<RwLock<SmtForest>>,
account: &Account,
initial_address: &Address,
) -> Result<(), StoreError> {
Expand All @@ -207,16 +208,16 @@ impl SqliteStore {

tx.commit().into_store_error()?;

let mut merkle_store = merkle_store.write().expect("merkle_store write lock not poisoned");
insert_storage_map_nodes(&mut merkle_store, account.storage());
insert_asset_nodes(&mut merkle_store, account.vault());
let mut smt_forest = smt_forest.write().expect("smt_forest write lock not poisoned");
insert_storage_map_nodes(&mut smt_forest, account.storage());
insert_asset_nodes(&mut smt_forest, account.vault());

Ok(())
}

pub(crate) fn update_account(
conn: &mut Connection,
merkle_store: &Arc<RwLock<MerkleStore>>,
smt_forest: &Arc<RwLock<SmtForest>>,
new_account_state: &Account,
) -> Result<(), StoreError> {
const QUERY: &str = "SELECT id FROM accounts WHERE id = ?";
Expand All @@ -242,9 +243,9 @@ impl SqliteStore {
return Err(StoreError::AccountDataNotFound(new_account_state.id()));
}

let mut merkle_store = merkle_store.write().expect("merkle_store write lock not poisoned");
let mut smt_forest = smt_forest.write().expect("smt_forest write lock not poisoned");
let tx = conn.transaction().into_store_error()?;
Self::update_account_state(&tx, &mut merkle_store, new_account_state)?;
Self::update_account_state(&tx, &mut smt_forest, new_account_state)?;
tx.commit().into_store_error()
}

Expand Down Expand Up @@ -330,10 +331,10 @@ impl SqliteStore {
}

/// Fetches a specific asset from the account's vault without the need of loading the entire
/// vault. The Merkle proof is also retrieved from the [`MerkleStore`].
/// vault. The Merkle proof is also retrieved from the [`SmtForest`].
pub(crate) fn get_account_asset(
conn: &mut Connection,
merkle_store: &Arc<RwLock<MerkleStore>>,
smt_forest: &Arc<RwLock<SmtForest>>,
account_id: AccountId,
faucet_id_prefix: AccountIdPrefix,
) -> Result<Option<(Asset, AssetWitness)>, StoreError> {
Expand All @@ -351,9 +352,9 @@ impl SqliteStore {
return Ok(None);
};

let merkle_store = merkle_store.read().expect("merkle_store read lock not poisoned");
let smt_forest = smt_forest.read().expect("smt_forest read lock not poisoned");

let proof = get_asset_proof(&merkle_store, header.vault_root(), &asset)?;
let proof = get_asset_proof(&smt_forest, header.vault_root(), &asset)?;
let witness = AssetWitness::new(proof)?;

Ok(Some((asset, witness)))
Expand All @@ -363,7 +364,7 @@ impl SqliteStore {
/// The Merkle proof is also retrieved from the [`MerkleStore`].
pub(crate) fn get_account_map_item(
conn: &mut Connection,
merkle_store: &Arc<RwLock<MerkleStore>>,
smt_forest: &Arc<RwLock<SmtForest>>,
account_id: AccountId,
index: u8,
key: Word,
Expand All @@ -384,10 +385,10 @@ impl SqliteStore {
};

let item = map.get(&key);
let merkle_store = merkle_store.read().expect("merkle_store read lock not poisoned");
let smt_forest = smt_forest.read().expect("smt_forest read lock not poisoned");

// TODO: change the api of get_storage_map_item_proof
let path = get_storage_map_item_proof(&merkle_store, map.root(), key)?.1.try_into()?;
let path = get_storage_map_item_proof(&smt_forest, map.root(), key)?.1.try_into()?;
let leaf = SmtLeaf::new_single(StorageMap::hash_key(key), item);
let proof = SmtProof::new(path, leaf)?;

Expand Down Expand Up @@ -444,7 +445,7 @@ impl SqliteStore {
/// `updated_fungible_assets` and `updated_storage_maps` parameters.
pub(super) fn apply_account_delta(
tx: &Transaction<'_>,
merkle_store: &mut MerkleStore,
smt_forest: &mut SmtForest,
init_account_state: &AccountHeader,
final_account_state: &AccountHeader,
mut updated_fungible_assets: BTreeMap<AccountIdPrefix, FungibleAsset>,
Expand Down Expand Up @@ -524,7 +525,7 @@ impl SqliteStore {
.into_store_error()?;

update_asset_nodes(
merkle_store,
smt_forest,
init_account_state.vault_root(),
updated_assets.values().copied(),
)?;
Expand All @@ -546,7 +547,7 @@ impl SqliteStore {
let mut map = updated_storage_maps.remove(index).unwrap_or_default();

update_storage_map_nodes(
merkle_store,
smt_forest,
map.root(),
map_delta.entries().iter().map(|(key, value)| ((*key).into(), *value)),
)?;
Expand Down Expand Up @@ -700,16 +701,16 @@ impl SqliteStore {
/// We can later identify the proper account state by looking at the nonce.
pub(super) fn update_account_state(
tx: &Transaction<'_>,
merkle_store: &mut MerkleStore,
smt_forest: &mut SmtForest,
new_account_state: &Account,
) -> Result<(), StoreError> {
insert_storage_map_nodes(merkle_store, new_account_state.storage());
insert_storage_map_nodes(smt_forest, new_account_state.storage());
Self::insert_storage_slots(
tx,
new_account_state.storage().commitment(),
new_account_state.storage().slots().iter().enumerate(),
)?;
insert_asset_nodes(merkle_store, new_account_state.vault());
insert_asset_nodes(smt_forest, new_account_state.vault());
Self::insert_assets(
tx,
new_account_state.vault().root(),
Expand Down Expand Up @@ -1271,16 +1272,16 @@ mod tests {

let account_id = account.id();
let final_state: AccountHeader = (&account_after_delta).into();
let merkle_store = store.merkle_store.clone();
let smt_forest = store.smt_forest.clone();
store
.interact_with_connection(move |conn| {
let tx = conn.transaction().into_store_error()?;
let mut merkle_store =
merkle_store.write().expect("merkle_store write lock not poisoned");
let mut smt_forest =
smt_forest.write().expect("smt_forest write lock not poisoned");

SqliteStore::apply_account_delta(
&tx,
&mut merkle_store,
&mut smt_forest,
&account.into(),
&final_state,
BTreeMap::default(),
Expand Down Expand Up @@ -1350,7 +1351,7 @@ mod tests {
let account_id = account.id();
let final_state: AccountHeader = (&account_after_delta).into();

let merkle_store = store.merkle_store.clone();
let smt_forest = store.smt_forest.clone();
store
.interact_with_connection(move |conn| {
let fungible_assets = SqliteStore::get_account_fungible_assets_for_delta(
Expand All @@ -1364,12 +1365,12 @@ mod tests {
&delta,
)?;
let tx = conn.transaction().into_store_error()?;
let mut merkle_store =
merkle_store.write().expect("merkle_store write lock not poisoned");
let mut smt_forest =
smt_forest.write().expect("smt_forest write lock not poisoned");

SqliteStore::apply_account_delta(
&tx,
&mut merkle_store,
&mut smt_forest,
&account.into(),
&final_state,
fungible_assets,
Expand Down
36 changes: 18 additions & 18 deletions crates/sqlite-store/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use miden_client::account::{
};
use miden_client::asset::{Asset, AssetVault, AssetWitness};
use miden_client::block::BlockHeader;
use miden_client::crypto::{InOrderIndex, MerkleStore, MmrPeaks};
use miden_client::crypto::{InOrderIndex, MmrPeaks};
use miden_client::note::{BlockNumber, NoteScript, NoteTag, Nullifier};
use miden_client::store::{
AccountRecord,
Expand All @@ -48,6 +48,7 @@ use miden_client::store::{
use miden_client::sync::{NoteTagRecord, StateSyncUpdate};
use miden_client::transaction::{TransactionRecord, TransactionStoreUpdate};
use miden_objects::account::StorageMapWitness;
use miden_objects::crypto::merkle::SmtForest;
use rusqlite::Connection;
use rusqlite::types::Value;
use sql_error::SqlResultExt;
Expand Down Expand Up @@ -75,7 +76,7 @@ pub use builder::ClientBuilderSqliteExt;
/// Current table definitions can be found at `store.sql` migration file.
pub struct SqliteStore {
pub(crate) pool: Pool,
merkle_store: Arc<RwLock<MerkleStore>>,
smt_forest: Arc<RwLock<SmtForest>>,
}

impl SqliteStore {
Expand All @@ -98,18 +99,17 @@ impl SqliteStore {

let store = SqliteStore {
pool,
merkle_store: Arc::new(RwLock::new(MerkleStore::new())),
smt_forest: Arc::new(RwLock::new(SmtForest::new())),
};

// Initialize merkle store
for id in store.get_account_ids().await? {
let vault = store.get_account_vault(id).await?;
let storage = store.get_account_storage(id).await?;

let mut merkle_store =
store.merkle_store.write().expect("merkle_store write lock not poisoned");
insert_asset_nodes(&mut merkle_store, &vault);
insert_storage_map_nodes(&mut merkle_store, &storage);
let mut smt_forest = store.smt_forest.write().expect("smt write lock not poisoned");
insert_asset_nodes(&mut smt_forest, &vault);
insert_storage_map_nodes(&mut smt_forest, &storage);
}

Ok(store)
Expand Down Expand Up @@ -169,9 +169,9 @@ impl Store for SqliteStore {
}

async fn apply_state_sync(&self, state_sync_update: StateSyncUpdate) -> Result<(), StoreError> {
let merkle_store = self.merkle_store.clone();
let smt_forest = self.smt_forest.clone();
self.interact_with_connection(move |conn| {
SqliteStore::apply_state_sync(conn, &merkle_store, state_sync_update)
SqliteStore::apply_state_sync(conn, &smt_forest, state_sync_update)
})
.await
}
Expand All @@ -187,9 +187,9 @@ impl Store for SqliteStore {
}

async fn apply_transaction(&self, tx_update: TransactionStoreUpdate) -> Result<(), StoreError> {
let merkle_store = self.merkle_store.clone();
let smt_forest = self.smt_forest.clone();
self.interact_with_connection(move |conn| {
SqliteStore::apply_transaction(conn, &merkle_store, &tx_update)
SqliteStore::apply_transaction(conn, &smt_forest, &tx_update)
})
.await
}
Expand Down Expand Up @@ -304,17 +304,17 @@ impl Store for SqliteStore {
initial_address: Address,
) -> Result<(), StoreError> {
let cloned_account = account.clone();
let merkle_store = self.merkle_store.clone();
let smt_forest = self.smt_forest.clone();

self.interact_with_connection(move |conn| {
SqliteStore::insert_account(conn, &merkle_store, &cloned_account, &initial_address)
SqliteStore::insert_account(conn, &smt_forest, &cloned_account, &initial_address)
})
.await
}

async fn update_account(&self, account: &Account) -> Result<(), StoreError> {
let cloned_account = account.clone();
let merkle_store = self.merkle_store.clone();
let merkle_store = self.smt_forest.clone();

self.interact_with_connection(move |conn| {
SqliteStore::update_account(conn, &merkle_store, &cloned_account)
Expand Down Expand Up @@ -411,9 +411,9 @@ impl Store for SqliteStore {
account_id: AccountId,
faucet_id_prefix: AccountIdPrefix,
) -> Result<Option<(Asset, AssetWitness)>, StoreError> {
let merkle_store = self.merkle_store.clone();
let smt_forest = self.smt_forest.clone();
self.interact_with_connection(move |conn| {
SqliteStore::get_account_asset(conn, &merkle_store, account_id, faucet_id_prefix)
SqliteStore::get_account_asset(conn, &smt_forest, account_id, faucet_id_prefix)
})
.await
}
Expand All @@ -434,10 +434,10 @@ impl Store for SqliteStore {
index: u8,
key: Word,
) -> Result<(Word, StorageMapWitness), StoreError> {
let merkle_store = self.merkle_store.clone();
let smt_forest = self.smt_forest.clone();

self.interact_with_connection(move |conn| {
SqliteStore::get_account_map_item(conn, &merkle_store, account_id, index, key)
SqliteStore::get_account_map_item(conn, &smt_forest, account_id, index, key)
})
.await
}
Expand Down
Loading
Loading