Skip to content

Commit

Permalink
feat: writing account details on block applying
Browse files Browse the repository at this point in the history
  • Loading branch information
polydez committed Apr 3, 2024
1 parent 54d3527 commit 63be641
Show file tree
Hide file tree
Showing 20 changed files with 421 additions and 53 deletions.
142 changes: 142 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ exclude = [".github/"]
[workspace.dependencies]
miden-air = { version = "0.8", default-features = false }
miden-lib = { git = "https://github.com/0xPolygonMiden/miden-base.git", branch = "next" }
miden-mock = { git = "https://github.com/0xPolygonMiden/miden-base.git", branch = "next" }
miden-objects = { git = "https://github.com/0xPolygonMiden/miden-base.git", branch = "next" }
miden-processor = { version = "0.8" }
miden-stdlib = { version = "0.8", default-features = false }
Expand Down
17 changes: 11 additions & 6 deletions block-producer/src/batch_builder/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use miden_objects::{
merkle::SimpleSmt,
},
notes::{NoteEnvelope, Nullifier},
transaction::AccountDetails,
Digest, BATCH_OUTPUT_NOTES_TREE_DEPTH, MAX_NOTES_PER_BATCH,
};
use tracing::instrument;
Expand Down Expand Up @@ -55,6 +56,7 @@ impl TransactionBatch {
AccountStates {
initial_state: tx.initial_account_hash(),
final_state: tx.final_account_hash(),
details: tx.account_details().cloned(),
},
)
})
Expand Down Expand Up @@ -108,12 +110,14 @@ impl TransactionBatch {
.map(|(account_id, account_states)| (*account_id, account_states.initial_state))
}

/// Returns an iterator over (account_id, new_state_hash) tuples for accounts that were
/// Returns an iterator over (account_id, details, new_state_hash) tuples for accounts that were
/// modified in this transaction batch.
pub fn updated_accounts(&self) -> impl Iterator<Item = (AccountId, Digest)> + '_ {
self.updated_accounts
.iter()
.map(|(account_id, account_states)| (*account_id, account_states.final_state))
pub fn updated_accounts(
&self
) -> impl Iterator<Item = (AccountId, Option<AccountDetails>, Digest)> + '_ {
self.updated_accounts.iter().map(|(account_id, account_states)| {
(*account_id, account_states.details.clone(), account_states.final_state)
})
}

/// Returns an iterator over produced nullifiers for all consumed notes.
Expand Down Expand Up @@ -147,8 +151,9 @@ impl TransactionBatch {
/// account.
///
/// TODO: should this be moved into domain objects?
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Debug, Clone, PartialEq, Eq)]
struct AccountStates {
initial_state: Digest,
final_state: Digest,
details: Option<AccountDetails>,
}
4 changes: 2 additions & 2 deletions block-producer/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use miden_objects::{
accounts::AccountId,
crypto::merkle::{MerklePath, MmrPeaks, SmtProof},
notes::{NoteEnvelope, Nullifier},
transaction::AccountDetails,
BlockHeader, Digest,
};

Expand All @@ -17,11 +18,10 @@ use crate::store::BlockInputsError;
#[derive(Debug, Clone)]
pub struct Block {
pub header: BlockHeader,
pub updated_accounts: Vec<(AccountId, Digest)>,
pub updated_accounts: Vec<(AccountId, Option<AccountDetails>, Digest)>,
pub created_notes: BTreeMap<u64, NoteEnvelope>,
pub produced_nullifiers: Vec<Nullifier>,
// TODO:
// - full states for updated public accounts
// - full states for created public notes
// - zk proof
}
Expand Down
8 changes: 5 additions & 3 deletions block-producer/src/block_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use std::sync::Arc;

use async_trait::async_trait;
use miden_node_utils::formatting::{format_array, format_blake3_digest};
use miden_objects::{accounts::AccountId, notes::Nullifier, Digest, MAX_NOTES_PER_BATCH};
use miden_objects::{
accounts::AccountId, notes::Nullifier, transaction::AccountDetails, Digest, MAX_NOTES_PER_BATCH,
};
use tracing::{debug, info, instrument};

use crate::{
Expand Down Expand Up @@ -77,7 +79,7 @@ where
batches = %format_array(batches.iter().map(|batch| format_blake3_digest(batch.id()))),
);

let updated_accounts: Vec<(AccountId, Digest)> =
let updated_accounts: Vec<(AccountId, Option<AccountDetails>, Digest)> =
batches.iter().flat_map(TransactionBatch::updated_accounts).collect();
let created_notes = batches
.iter()
Expand All @@ -95,7 +97,7 @@ where
let block_inputs = self
.store
.get_block_inputs(
updated_accounts.iter().map(|(account_id, _)| account_id),
updated_accounts.iter().map(|(account_id, _, _)| account_id),
produced_nullifiers.iter(),
)
.await?;
Expand Down
6 changes: 3 additions & 3 deletions block-producer/src/block_builder/prover/block_witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl BlockWitness {

let updated_accounts = {
let mut account_initial_states: BTreeMap<AccountId, Digest> =
batches.iter().flat_map(|batch| batch.account_initial_states()).collect();
batches.iter().flat_map(TransactionBatch::account_initial_states).collect();

let mut account_merkle_proofs: BTreeMap<AccountId, MerklePath> = block_inputs
.accounts
Expand All @@ -47,8 +47,8 @@ impl BlockWitness {

batches
.iter()
.flat_map(|batch| batch.updated_accounts())
.map(|(account_id, final_state_hash)| {
.flat_map(TransactionBatch::updated_accounts)
.map(|(account_id, _details, final_state_hash)| {
let initial_state_hash = account_initial_states
.remove(&account_id)
.expect("already validated that key exists");
Expand Down
2 changes: 1 addition & 1 deletion block-producer/src/block_builder/prover/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ async fn test_compute_account_root_success() {
account_ids
.iter()
.zip(account_final_states.iter())
.map(|(&account_id, &account_hash)| (account_id, account_hash.into()))
.map(|(&account_id, &account_hash)| (account_id, None, account_hash.into()))
.collect(),
)
.build();
Expand Down
Loading

0 comments on commit 63be641

Please sign in to comment.