Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return the list of committed transactions in state sync endpoint #377

Merged
merged 17 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
77 changes: 57 additions & 20 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ readme = "README.md"

[workspace.dependencies]
miden-air = { version = "0.9", default-features = false }
miden-lib = { version = "0.3" }
miden-lib = { git = "https://github.com/0xPolygonMiden/miden-base.git", branch = "polydez-block-tx-ids" }
bobbinth marked this conversation as resolved.
Show resolved Hide resolved
miden-node-block-producer = { path = "crates/block-producer", version = "0.3" }
miden-node-faucet = { path = "bin/faucet", version = "0.3" }
miden-node-proto = { path = "crates/proto", version = "0.3" }
miden-node-rpc = { path = "crates/rpc", version = "0.3" }
miden-node-store = { path = "crates/store", version = "0.3" }
miden-node-test-macro = { path = "crates/test-macro" }
miden-node-utils = { path = "crates/utils", version = "0.3" }
miden-objects = { version = "0.3" }
miden-objects = { git = "https://github.com/0xPolygonMiden/miden-base.git", branch = "polydez-block-tx-ids" }
miden-processor = { version = "0.9" }
miden-stdlib = { version = "0.9", default-features = false }
miden-tx = { version = "0.3" }
miden-tx = { git = "https://github.com/0xPolygonMiden/miden-base.git", branch = "polydez-block-tx-ids" }
thiserror = { version = "1.0" }
tracing = { version = "0.1" }
tracing-subscriber = { version = "0.3", features = ["fmt", "json", "env-filter"] }
12 changes: 7 additions & 5 deletions crates/block-producer/src/batch_builder/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use miden_objects::{
block::BlockAccountUpdate,
crypto::hash::blake::{Blake3Digest, Blake3_256},
notes::Nullifier,
transaction::{OutputNote, TxAccountUpdate},
transaction::{OutputNote, TransactionId, TxAccountUpdate},
Digest, MAX_NOTES_PER_BATCH,
};
use tracing::instrument;
Expand All @@ -23,7 +23,7 @@ pub type BatchId = Blake3Digest<32>;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TransactionBatch {
id: BatchId,
updated_accounts: Vec<TxAccountUpdate>,
updated_accounts: Vec<(TransactionId, TxAccountUpdate)>,
produced_nullifiers: Vec<Nullifier>,
created_notes_smt: BatchNoteTree,
created_notes: Vec<OutputNote>,
Expand All @@ -48,7 +48,8 @@ impl TransactionBatch {
// the same account (e.g., transaction `x` takes account from state `A` to `B` and
// transaction `y` takes account from state `B` to `C`). These will need to be merged
// into a single "update" `A` to `C`.
let updated_accounts = txs.iter().map(ProvenTransaction::account_update).cloned().collect();
let updated_accounts =
txs.iter().map(|tx| (tx.id(), tx.account_update().clone())).collect();

let produced_nullifiers =
txs.iter().flat_map(|tx| tx.input_notes().iter()).copied().collect();
Expand Down Expand Up @@ -88,17 +89,18 @@ impl TransactionBatch {
pub fn account_initial_states(&self) -> impl Iterator<Item = (AccountId, Digest)> + '_ {
self.updated_accounts
.iter()
.map(|update| (update.account_id(), update.init_state_hash()))
.map(|(_, update)| (update.account_id(), update.init_state_hash()))
}

/// 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 = BlockAccountUpdate> + '_ {
self.updated_accounts.iter().map(|update| {
self.updated_accounts.iter().map(|(transaction_id, update)| {
BlockAccountUpdate::new(
update.account_id(),
update.final_state_hash(),
update.details().clone(),
vec![*transaction_id],
)
})
}
Expand Down
14 changes: 6 additions & 8 deletions crates/block-producer/src/block_builder/prover/block_witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use miden_objects::{
accounts::AccountId,
crypto::merkle::{EmptySubtreeRoots, MerklePath, MerkleStore, MmrPeaks, SmtProof},
notes::Nullifier,
transaction::TransactionId,
vm::{AdviceInputs, StackInputs},
BlockHeader, Digest, Felt, BLOCK_OUTPUT_NOTES_TREE_DEPTH, MAX_BATCHES_PER_BLOCK, ZERO,
};
Expand Down Expand Up @@ -62,6 +63,7 @@ impl BlockWitness {
initial_state_hash,
final_state_hash: update.new_state_hash(),
proof,
transactions: update.transactions().to_vec(),
},
)
})
Expand Down Expand Up @@ -269,14 +271,9 @@ impl BlockWitness {
// add accounts merkle paths
merkle_store
.add_merkle_paths(self.updated_accounts.into_iter().map(
|(
account_id,
AccountUpdateWitness {
initial_state_hash,
final_state_hash: _,
proof,
},
)| { (u64::from(account_id), initial_state_hash, proof) },
|(account_id, AccountUpdateWitness { initial_state_hash, proof, .. })| {
(u64::from(account_id), initial_state_hash, proof)
},
))
.map_err(BlockProverError::InvalidMerklePaths)?;

Expand Down Expand Up @@ -314,6 +311,7 @@ pub(super) struct AccountUpdateWitness {
pub initial_state_hash: Digest,
pub final_state_hash: Digest,
pub proof: MerklePath,
pub transactions: Vec<TransactionId>,
}

// HELPERS
Expand Down
22 changes: 19 additions & 3 deletions crates/block-producer/src/block_builder/prover/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::{SystemTime, UNIX_EPOCH};

use miden_objects::{assembly::Assembler, BlockHeader, Digest};
use miden_objects::{assembly::Assembler, BlockHeader, Digest, Felt, FieldElement, Hasher};
use miden_processor::{execute, DefaultHost, ExecutionOptions, MemAdviceProvider, Program};
use miden_stdlib::StdLibrary;

Expand Down Expand Up @@ -212,9 +212,9 @@ impl BlockProver {
let block_num = witness.prev_header.block_num() + 1;
let version = witness.prev_header.version();

let tx_hash = self.compute_tx_hash(&witness);
let (account_root, note_root, nullifier_root, chain_root) = self.compute_roots(witness)?;

let batch_root = Digest::default();
let proof_hash = Digest::default();
let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
Expand All @@ -231,7 +231,7 @@ impl BlockProver {
account_root,
nullifier_root,
note_root,
batch_root,
tx_hash,
proof_hash,
timestamp,
))
Expand Down Expand Up @@ -279,4 +279,20 @@ impl BlockProver {
new_chain_mmr_root.into(),
))
}

fn compute_tx_hash(&self, witness: &BlockWitness) -> Digest {
let elements: Vec<Felt> = witness
.updated_accounts
.iter()
.flat_map(|(&account_id, update)| {
update.transactions.iter().flat_map(move |&tx| {
let mut result = vec![Felt::ZERO, Felt::ZERO, Felt::ZERO, account_id.into()];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to add the account id to the elements to hash?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please look into this issue for clarifications: 0xPolygonMiden/miden-base#722

bobbinth marked this conversation as resolved.
Show resolved Hide resolved
result.extend_from_slice(tx.as_elements());
result
})
})
.collect();

Hasher::hash_elements(&elements)
}
}
1 change: 1 addition & 0 deletions crates/block-producer/src/block_builder/prover/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ async fn test_compute_account_root_success() {
account_id,
account_hash.into(),
AccountUpdateDetails::Private,
vec![],
)
})
.collect(),
Expand Down
3 changes: 3 additions & 0 deletions crates/block-producer/src/state_view/tests/apply_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ async fn test_apply_block_ab1() {
mock_account.id,
mock_account.states[1],
AccountUpdateDetails::Private,
vec![tx.id()],
)
})
.collect(),
Expand Down Expand Up @@ -88,6 +89,7 @@ async fn test_apply_block_ab2() {
mock_account.id,
mock_account.states[1],
AccountUpdateDetails::Private,
vec![],
)
})
.collect(),
Expand Down Expand Up @@ -139,6 +141,7 @@ async fn test_apply_block_ab3() {
mock_account.id,
mock_account.states[1],
AccountUpdateDetails::Private,
vec![],
)
})
.collect(),
Expand Down
4 changes: 2 additions & 2 deletions crates/proto/proto/block_header.proto
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ message BlockHeader {
digest.Digest nullifier_root = 6;
// a commitment to all notes created in the current block.
digest.Digest note_root = 7;
// a commitment to a set of transaction batches executed as a part of this block.
digest.Digest batch_root = 8;
// a commitment to a set of IDs of transactions which affected accounts in this block.
digest.Digest tx_hash = 8;
// a hash of a STARK proof attesting to the correct state transition.
digest.Digest proof_hash = 9;
// the time when the block was created.
Expand Down
Loading
Loading