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

Add support for on-chain notes to block-producer #310

Merged
merged 18 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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
20 changes: 10 additions & 10 deletions Cargo.lock

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

43 changes: 25 additions & 18 deletions block-producer/src/batch_builder/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use miden_objects::{
batches::BatchNoteTree,
crypto::hash::blake::{Blake3Digest, Blake3_256},
notes::{NoteEnvelope, Nullifier},
transaction::AccountDetails,
transaction::{AccountDetails, OutputNote},
utils::serde::Serializable,
Digest, MAX_NOTES_PER_BATCH,
};
use tracing::instrument;
Expand All @@ -28,8 +29,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_note_envelopes_with_details: Vec<(NoteEnvelope, Option<Vec<u8>>)>,
bobbinth marked this conversation as resolved.
Show resolved Hide resolved
}

impl TransactionBatch {
Expand Down Expand Up @@ -64,25 +64,30 @@ impl TransactionBatch {
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
let (created_note_envelopes_with_details, created_notes_smt) = {
let created_note_envelopes_with_details: Vec<(NoteEnvelope, Option<Vec<u8>>)> = txs
phklive marked this conversation as resolved.
Show resolved Hide resolved
.iter()
.flat_map(|tx| tx.output_notes().iter())
.cloned()
.map(NoteEnvelope::from)
.map(|note| match note {
OutputNote::Public(note) => (note.into(), Some(note.to_bytes())),
OutputNote::Private(envelope) => (*envelope, None),
})
.collect();

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

// TODO: document under what circumstances SMT creating can fail
(
created_notes.clone(),
created_note_envelopes_with_details.clone(),
BatchNoteTree::with_contiguous_leaves(
created_notes
created_note_envelopes_with_details
.iter()
.map(|note_envelope| (note_envelope.id(), note_envelope.metadata())),
.map(|(note_envelope, _)| (note_envelope.id(), note_envelope.metadata())),
)
.map_err(|e| BuildBatchError::NotesSmtError(e, txs))?,
)
Expand All @@ -93,7 +98,7 @@ impl TransactionBatch {
updated_accounts,
produced_nullifiers,
created_notes_smt,
created_notes,
created_note_envelopes_with_details,
})
}

Expand Down Expand Up @@ -130,16 +135,18 @@ 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 an iterator over created note envelopes.
pub fn created_note_envelopes_with_details(
&self
) -> impl Iterator<Item = &(NoteEnvelope, Option<Vec<u8>>)> + '_ {
self.created_note_envelopes_with_details.iter()
}

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

Expand Down
6 changes: 3 additions & 3 deletions block-producer/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use std::collections::BTreeMap;
use miden_node_proto::{
domain::accounts::AccountUpdateDetails,
errors::{ConversionError, MissingFieldHelper},
generated::responses::GetBlockInputsResponse,
generated::{note::NoteCreated, responses::GetBlockInputsResponse},
AccountInputRecord, NullifierWitness,
};
use miden_objects::{
accounts::AccountId,
crypto::merkle::{MerklePath, MmrPeaks, SmtProof},
notes::{NoteEnvelope, Nullifier},
notes::Nullifier,
BlockHeader, Digest,
};

Expand All @@ -19,7 +19,7 @@ use crate::store::BlockInputsError;
pub struct Block {
pub header: BlockHeader,
pub updated_accounts: Vec<AccountUpdateDetails>,
pub created_notes: Vec<(usize, usize, NoteEnvelope)>,
pub created_notes: Vec<NoteCreated>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Related to the previous comment: this could have probably have been something like Vec<(usize, usize, NoteEnvelope)>. In general, I think we should try to avoid using protobuf types inside domain types.

A thought for the future: we should probably move the Block struct into miden-base at some point.

pub produced_nullifiers: Vec<Nullifier>,
// TODO:
// - full states for created public notes
Expand Down
21 changes: 15 additions & 6 deletions block-producer/src/block_builder/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::sync::Arc;

use async_trait::async_trait;
use miden_node_proto::generated::note::NoteCreated;
use miden_node_utils::formatting::{format_array, format_blake3_digest};
use miden_objects::notes::Nullifier;
use tracing::{debug, info, instrument};
Expand Down Expand Up @@ -82,16 +83,24 @@ where

let updated_accounts: Vec<_> =
batches.iter().flat_map(TransactionBatch::updated_accounts).collect();
let created_notes = batches

let created_notes: Vec<NoteCreated> = 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))
.flat_map(|(batch_index, batch)| {
batch.created_note_envelopes_with_details().enumerate().map(
move |(note_index, (note_envelope, details))| NoteCreated {
phklive marked this conversation as resolved.
Show resolved Hide resolved
batch_index: batch_index as u32,
note_index: note_index as u32,
note_id: Some(note_envelope.id().into()),
sender: Some(note_envelope.metadata().sender().into()),
tag: note_envelope.metadata().tag().into(),
details: details.clone(),
},
)
})
.collect();

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

Expand Down
2 changes: 1 addition & 1 deletion block-producer/src/block_builder/prover/block_witness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl BlockWitness {
.iter()
.enumerate()
.filter_map(|(batch_index, batch)| {
if batch.created_notes().next().is_none() {
if batch.created_note_envelopes_with_details().next().is_none() {
None
} else {
Some((batch_index, batch.created_notes_root()))
Expand Down
2 changes: 1 addition & 1 deletion block-producer/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl ApplyBlock for DefaultStore {
block: Some((&block.header).into()),
accounts: convert(&block.updated_accounts),
nullifiers: convert(&block.produced_nullifiers),
notes: convert(&block.created_notes),
notes: block.created_notes.clone(),
});

let _ = self
Expand Down
36 changes: 25 additions & 11 deletions block-producer/src/test_utils/block.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use miden_node_proto::domain::accounts::AccountUpdateDetails;
use miden_node_proto::{domain::accounts::AccountUpdateDetails, generated::note::NoteCreated};
use miden_objects::{
block::BlockNoteTree,
crypto::merkle::{Mmr, SimpleSmt},
Expand Down Expand Up @@ -90,7 +90,7 @@ pub struct MockBlockBuilder {
last_block_header: BlockHeader,

updated_accounts: Option<Vec<AccountUpdateDetails>>,
created_notes: Option<Vec<(usize, usize, NoteEnvelope)>>,
created_note_envelopes: Option<Vec<(usize, usize, NoteEnvelope)>>,
produced_nullifiers: Option<Vec<Nullifier>>,
}

Expand All @@ -102,7 +102,7 @@ impl MockBlockBuilder {
last_block_header: *store.last_block_header.read().await,

updated_accounts: None,
created_notes: None,
created_note_envelopes: None,
produced_nullifiers: None,
}
}
Expand All @@ -121,11 +121,11 @@ impl MockBlockBuilder {
self
}

pub fn created_notes(
pub fn created_note_envelopes(
mut self,
created_notes: Vec<(usize, usize, NoteEnvelope)>,
created_note_envelopes: Vec<(usize, usize, NoteEnvelope)>,
) -> Self {
self.created_notes = Some(created_notes);
self.created_note_envelopes = Some(created_note_envelopes);

self
}
Expand All @@ -140,15 +140,27 @@ impl MockBlockBuilder {
}

pub fn build(self) -> Block {
let created_notes = self.created_notes.unwrap_or_default();
let created_note_envelopes = self.created_note_envelopes.unwrap_or_default();

let created_notes = created_note_envelopes
.iter()
.map(|(batch_index, note_index, note_envelope)| NoteCreated {
batch_index: *batch_index as u32,
phklive marked this conversation as resolved.
Show resolved Hide resolved
note_index: *note_index as u32,
note_id: Some(note_envelope.id().into()),
tag: note_envelope.metadata().tag().into(),
sender: Some(note_envelope.metadata().sender().into()),
details: None,
})
.collect();

let header = BlockHeader::new(
self.last_block_header.hash(),
self.last_block_header.block_num() + 1,
self.store_chain_mmr.peaks(self.store_chain_mmr.forest()).unwrap().hash_peaks(),
self.store_accounts.root(),
Digest::default(),
note_created_smt_from_envelopes(created_notes.iter().cloned()).root(),
note_created_smt_from_envelopes(created_note_envelopes.iter().cloned()).root(),
Digest::default(),
Digest::default(),
ZERO,
Expand Down Expand Up @@ -177,9 +189,11 @@ pub(crate) fn note_created_smt_from_batches<'a>(
batches: impl Iterator<Item = &'a TransactionBatch>
) -> BlockNoteTree {
let note_leaf_iterator = batches.enumerate().flat_map(|(batch_idx, batch)| {
batch.created_notes().enumerate().map(move |(note_idx_in_batch, note)| {
(batch_idx, note_idx_in_batch, (note.id().into(), *note.metadata()))
})
batch.created_note_envelopes_with_details().enumerate().map(
move |(note_idx_in_batch, (note, _))| {
(batch_idx, note_idx_in_batch, (note.id().into(), *note.metadata()))
},
)
});

BlockNoteTree::with_entries(note_leaf_iterator).unwrap()
Expand Down
20 changes: 7 additions & 13 deletions proto/src/domain/notes.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
use miden_objects::notes::NoteEnvelope;

use crate::generated::note;
use crate::generated::note::{self, NoteCreated};

// NoteCreated
// ================================================================================================

impl From<&(usize, usize, NoteEnvelope)> for note::NoteCreated {
fn from((batch_idx, note_idx, note): &(usize, usize, NoteEnvelope)) -> Self {
impl From<&(usize, usize, NoteCreated)> for note::NoteCreated {
fn from((batch_idx, note_idx, note): &(usize, usize, NoteCreated)) -> Self {
phklive marked this conversation as resolved.
Show resolved Hide resolved
Self {
batch_index: *batch_idx as u32,
note_index: *note_idx as u32,
note_id: Some(note.id().into()),
sender: Some(note.metadata().sender().into()),
tag: note.metadata().tag().into(),
// This is `None` for now as this conversion is used by the block-producer
// when using `apply_block`. The block-producer has not yet been updated to support
// on-chain notes. Will be set to the correct value when the block-producer is
// up-to-date.
details: None,
note_id: note.note_id.clone(),
sender: note.sender.clone(),
tag: note.tag,
details: note.details.clone(),
}
}
}
Loading