Skip to content

Commit

Permalink
wip: move discarded_tx changes to apply_nullifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
SantiagoPittella committed Feb 28, 2025
1 parent 05782f0 commit 80b35ec
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 37 deletions.
2 changes: 2 additions & 0 deletions crates/rust-client/src/store/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use miden_objects::{
/// The account should be stored in the database with its parts normalized. Meaning that the
/// account header, vault, storage and code are stored separately. This is done to avoid data
/// duplication as the header can reference the same elements if they have equal roots.
#[derive(Debug)]
pub struct AccountRecord {
/// Full account object.
account: Account,
Expand Down Expand Up @@ -54,6 +55,7 @@ impl From<AccountRecord> for Account {
/// Represents the status of an account tracked by the client.
///
/// The status of an account may change by local or external factors.
#[derive(Debug)]
pub enum AccountStatus {
/// The account is new and hasn't been used yet. The seed used to create the account is
/// stored in this state.
Expand Down
85 changes: 48 additions & 37 deletions crates/rust-client/src/store/sqlite_store/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use miden_objects::{
};
use miden_tx::utils::{Deserializable, Serializable};
use rusqlite::{params, Connection, Transaction};
use tracing::info;

use super::SqliteStore;
use crate::{
Expand Down Expand Up @@ -116,19 +117,6 @@ impl SqliteStore {
tags_to_remove,
} = state_sync_update;

// First we need the `transaction` entries from the `transactions` table that matches the
// `transactions_to_discard`

let transactions_to_discard =
Self::get_transactions(conn, &TransactionFilter::Ids(discarded_transactions.clone()))?;

let outdated_accounts = transactions_to_discard
.iter()
.map(|transaction_record| {
Self::get_account(conn, transaction_record.account_id).unwrap().unwrap()
})
.collect::<Vec<_>>();

let tx = conn.transaction()?;

// Update state sync block number
Expand All @@ -154,30 +142,6 @@ impl SqliteStore {
// Marc transactions as discarded
Self::mark_transactions_as_discarded(&tx, &discarded_transactions)?;

// TODO: here we need to remove the `accounts` table entries that are originated from the
// discarded transactions

// Transaction records have a final_account_state field, which is the hash of the account in
// the final state after the transaction is applied. We can use this field to
// identify the accounts that are originated from the discarded transactions.

let accounts_to_remove = transactions_to_discard
.iter()
.map(|tx| {
let final_account_state = tx.final_account_state;

let account = outdated_accounts
.iter()
.find(|account| account.account().hash() == final_account_state)
.unwrap();

account.account().id()
})
.collect::<Vec<AccountId>>();

// Remove the accounts that are originated from the discarded transactions
Self::delete_accounts(&tx, &accounts_to_remove)?;

// Update public accounts on the db that have been updated onchain
for account in updated_accounts.updated_public_accounts() {
update_account(&tx, account)?;
Expand All @@ -198,12 +162,59 @@ impl SqliteStore {
note_updates: &NoteUpdates,
transactions_to_discard: &[TransactionId],
) -> Result<(), StoreError> {
info!("APPLY NULLIFIERS");
// First we need the `transaction` entries from the `transactions` table that matches the
// `transactions_to_discard`

info!("discarded_transactions: {:?}", transactions_to_discard);
let transactions_records_to_discard = Self::get_transactions(
conn,
&TransactionFilter::Ids(transactions_to_discard.to_vec()),
)?;

info!("transactions_to_discard: {:?}", transactions_to_discard);
let outdated_accounts = transactions_records_to_discard
.iter()
.map(|transaction_record| {
Self::get_account(conn, transaction_record.account_id).unwrap().unwrap()
})
.collect::<Vec<_>>();

info!("outdated_accounts: {:?}", outdated_accounts);

let tx = conn.transaction()?;

apply_note_updates_tx(&tx, note_updates)?;

Self::mark_transactions_as_discarded(&tx, transactions_to_discard)?;

// TODO: here we need to remove the `accounts` table entries that are originated from the
// discarded transactions

// Transaction records have a final_account_state field, which is the hash of the account in
// the final state after the transaction is applied. We can use this field to
// identify the accounts that are originated from the discarded transactions.

let accounts_to_remove = transactions_records_to_discard
.iter()
.map(|tx| {
let final_account_state = tx.final_account_state;
info!("final_account_state: {:?}", final_account_state);
let account = outdated_accounts
.iter()
.find(|account| account.account().hash() == final_account_state)
.unwrap();

info!("account: {:?}", account);
account.account().id()
})
.collect::<Vec<AccountId>>();

info!("accounts_to_remove: {:?}", accounts_to_remove);

// Remove the accounts that are originated from the discarded transactions
Self::delete_accounts(&tx, &accounts_to_remove)?;

tx.commit()?;

Ok(())
Expand Down

0 comments on commit 80b35ec

Please sign in to comment.