Skip to content

Commit

Permalink
feat: add support for on-chain notes to block-producer (#310)
Browse files Browse the repository at this point in the history
Co-authored-by: igamigo <[email protected]>
Co-authored-by: polydez <[email protected]>
  • Loading branch information
3 people authored Apr 11, 2024
1 parent 7056c4f commit 6aa2c90
Show file tree
Hide file tree
Showing 22 changed files with 219 additions and 230 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ jobs:
uses: dtolnay/rust-toolchain@stable
- name: Install cargo make
run: cargo install cargo-make
- name: cargo make - test
- name: cargo make - test-all
run: cargo make test-all
69 changes: 36 additions & 33 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 @@ -24,11 +24,11 @@ exclude = [".github/"]

[workspace.dependencies]
miden-air = { version = "0.9", default-features = false }
miden-lib = { git = "https://github.com/0xPolygonMiden/miden-base.git", branch = "next" }
miden-objects = { git = "https://github.com/0xPolygonMiden/miden-base.git", branch = "next" }
miden-lib = { version = "0.2"}
miden-objects = { version = "0.2" }
miden-processor = { version = "0.9" }
miden-stdlib = { version = "0.9", default-features = false }
miden-tx = { git = "https://github.com/0xPolygonMiden/miden-base.git", branch = "next" }
miden-tx = { version = "0.2" }
thiserror = { version = "1.0" }
tracing = { version = "0.1" }
tracing-subscriber = { version = "0.3", features = [
Expand Down
63 changes: 30 additions & 33 deletions block-producer/src/batch_builder/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use miden_objects::{
accounts::AccountId,
batches::BatchNoteTree,
crypto::hash::blake::{Blake3Digest, Blake3_256},
notes::{NoteEnvelope, Nullifier},
transaction::AccountDetails,
notes::Nullifier,
transaction::{AccountDetails, OutputNote},
Digest, MAX_NOTES_PER_BATCH,
};
use tracing::instrument;
Expand All @@ -28,8 +28,7 @@ pub struct TransactionBatch {
updated_accounts: BTreeMap<AccountId, AccountStates>,
produced_nullifiers: Vec<Nullifier>,
created_notes_smt: BatchNoteTree,
/// The notes stored `created_notes_smt`
created_notes: Vec<NoteEnvelope>,
created_notes: Vec<OutputNote>,
}

impl TransactionBatch {
Expand Down Expand Up @@ -62,31 +61,29 @@ impl TransactionBatch {
.collect();

let produced_nullifiers =
txs.iter().flat_map(|tx| tx.input_notes().iter()).cloned().collect();

let (created_notes, created_notes_smt) = {
let created_notes: Vec<NoteEnvelope> = txs
.iter()
.flat_map(|tx| tx.output_notes().iter())
.cloned()
.map(NoteEnvelope::from)
.collect();

if created_notes.len() > MAX_NOTES_PER_BATCH {
return Err(BuildBatchError::TooManyNotesCreated(created_notes.len(), txs));
}

// TODO: document under what circumstances SMT creating can fail
(
created_notes.clone(),
BatchNoteTree::with_contiguous_leaves(
created_notes
.iter()
.map(|note_envelope| (note_envelope.id(), note_envelope.metadata())),
txs.iter().flat_map(|tx| tx.input_notes().iter()).copied().collect();

let created_notes: Vec<_> =
txs.iter().flat_map(|tx| tx.output_notes().iter()).cloned().collect();

if created_notes.len() > MAX_NOTES_PER_BATCH {
return Err(BuildBatchError::TooManyNotesCreated(created_notes.len(), txs));
}

// TODO: document under what circumstances SMT creating can fail
let created_notes_smt =
BatchNoteTree::with_contiguous_leaves(created_notes.iter().map(|note| {
(
note.id(),
// TODO: Substitute by using just note.metadata() once this getter returns reference
// Tracking PR: https://github.com/0xPolygonMiden/miden-base/pull/593
match note {
OutputNote::Public(note) => note.metadata(),
OutputNote::Private(note) => note.metadata(),
},
)
.map_err(|e| BuildBatchError::NotesSmtError(e, txs))?,
)
};
}))
.map_err(|e| BuildBatchError::NotesSmtError(e, txs))?;

Ok(Self {
id,
Expand Down Expand Up @@ -130,16 +127,16 @@ impl TransactionBatch {
self.produced_nullifiers.iter().cloned()
}

/// Returns an iterator over created notes.
pub fn created_notes(&self) -> impl Iterator<Item = &NoteEnvelope> + '_ {
self.created_notes.iter()
}

/// Returns the root hash of the created notes SMT.
pub fn created_notes_root(&self) -> Digest {
self.created_notes_smt.root()
}

/// Returns created notes list.
pub fn created_notes(&self) -> &Vec<OutputNote> {
&self.created_notes
}

// HELPER FUNCTIONS
// --------------------------------------------------------------------------------------------

Expand Down
7 changes: 5 additions & 2 deletions block-producer/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ use miden_node_proto::{
use miden_objects::{
accounts::AccountId,
crypto::merkle::{MerklePath, MmrPeaks, SmtProof},
notes::{NoteEnvelope, Nullifier},
notes::Nullifier,
transaction::OutputNote,
BlockHeader, Digest,
};

use crate::store::BlockInputsError;

pub(crate) type NoteBatch = Vec<OutputNote>;

#[derive(Debug, Clone)]
pub struct Block {
pub header: BlockHeader,
pub updated_accounts: Vec<AccountUpdateDetails>,
pub created_notes: Vec<(usize, usize, NoteEnvelope)>,
pub created_notes: Vec<NoteBatch>,
pub produced_nullifiers: Vec<Nullifier>,
// TODO:
// - full states for created public notes
Expand Down
13 changes: 3 additions & 10 deletions block-producer/src/block_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,9 @@ where

let updated_accounts: Vec<_> =
batches.iter().flat_map(TransactionBatch::updated_accounts).collect();
let created_notes = batches
.iter()
.enumerate()
.flat_map(|(batch_idx, batch)| {
batch
.created_notes()
.enumerate()
.map(move |(note_idx_in_batch, note)| (batch_idx, note_idx_in_batch, *note))
})
.collect();

let created_notes = batches.iter().map(|batch| batch.created_notes().clone()).collect();

let produced_nullifiers: Vec<Nullifier> =
batches.iter().flat_map(TransactionBatch::produced_nullifiers).collect();

Expand Down
9 changes: 2 additions & 7 deletions block-producer/src/block_builder/prover/block_witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,8 @@ impl BlockWitness {
let batch_created_notes_roots = batches
.iter()
.enumerate()
.filter_map(|(batch_index, batch)| {
if batch.created_notes().next().is_none() {
None
} else {
Some((batch_index, batch.created_notes_root()))
}
})
.filter(|(_, batch)| !batch.created_notes().is_empty())
.map(|(batch_index, batch)| (batch_index, batch.created_notes_root()))
.collect();

Ok(Self {
Expand Down
Loading

0 comments on commit 6aa2c90

Please sign in to comment.