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 8 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
29 changes: 16 additions & 13 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 an iterator over created note envelopes.
pub fn created_notes(&self) -> impl Iterator<Item = &OutputNote> + '_ {
self.created_notes.iter()
}

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

Expand Down
4 changes: 3 additions & 1 deletion block-producer/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ use miden_objects::{

use crate::store::BlockInputsError;

pub(crate) type NoteBatch = Vec<(NoteEnvelope, Option<Vec<u8>>)>;
igamigo marked this conversation as resolved.
Show resolved Hide resolved

#[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
14 changes: 9 additions & 5 deletions block-producer/src/block_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use async_trait::async_trait;
use miden_node_utils::formatting::{format_array, format_blake3_digest};
use miden_objects::notes::Nullifier;
use miden_objects::{notes::Nullifier, transaction::OutputNote, utils::Serializable};
use tracing::{debug, info, instrument};

use crate::{
Expand Down Expand Up @@ -82,16 +82,20 @@ where

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

let created_notes = batches
.iter()
.enumerate()
.flat_map(|(batch_idx, batch)| {
.map(|batch| {
batch
.created_notes()
.enumerate()
.map(move |(note_idx_in_batch, note)| (batch_idx, note_idx_in_batch, *note))
.map(|note| match note {
OutputNote::Public(note) => (note.into(), Some(note.to_bytes())),
OutputNote::Private(envelope) => (*envelope, None),
})
.collect()
})
.collect();

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

Expand Down
23 changes: 22 additions & 1 deletion block-producer/src/store/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use miden_node_proto::{
errors::{ConversionError, MissingFieldHelper},
generated::{
account, digest,
note::NoteCreated,
requests::{ApplyBlockRequest, GetBlockInputsRequest, GetTransactionInputsRequest},
responses::{GetTransactionInputsResponse, NullifierTransactionInputRecord},
store::api_client as store_client,
Expand Down Expand Up @@ -136,11 +137,31 @@ impl ApplyBlock for DefaultStore {
&self,
block: &Block,
) -> Result<(), ApplyBlockError> {
let notes = block
.created_notes
.iter()
.enumerate()
.flat_map(|(batch_idx, batch)| {
batch
.iter()
.enumerate()
.map(|(note_idx_in_batch, (note, details))| NoteCreated {
batch_index: batch_idx as u32,
note_index: note_idx_in_batch as u32,
note_id: Some(note.id().into()),
sender: Some(note.metadata().sender().into()),
tag: note.metadata().tag().into(),
details: details.clone(),
})
.collect::<Vec<_>>()
})
.collect();

let request = tonic::Request::new(ApplyBlockRequest {
block: Some((&block.header).into()),
accounts: convert(&block.updated_accounts),
nullifiers: convert(&block.produced_nullifiers),
notes: convert(&block.created_notes),
notes,
});

let _ = self
Expand Down
38 changes: 24 additions & 14 deletions block-producer/src/test_utils/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use miden_objects::{

use super::MockStoreSuccess;
use crate::{
block::{Block, BlockInputs},
block::{Block, BlockInputs, NoteBatch},
block_builder::prover::{block_witness::BlockWitness, BlockProver},
store::Store,
TransactionBatch,
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<NoteBatch>>,
bobbinth marked this conversation as resolved.
Show resolved Hide resolved
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<NoteBatch>,
) -> Self {
self.created_notes = Some(created_notes);
self.created_note_envelopes = Some(created_note_envelopes);
bobbinth marked this conversation as resolved.
Show resolved Hide resolved

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

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

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_notes.iter().map(|batch| batch.iter().map(|(note, _)| *note).collect()),
)
.root(),
Digest::default(),
Digest::default(),
ZERO,
Expand All @@ -165,20 +168,27 @@ impl MockBlockBuilder {
}

pub(crate) fn note_created_smt_from_envelopes(
note_iterator: impl Iterator<Item = (usize, usize, NoteEnvelope)>
batches: impl Iterator<Item = Vec<NoteEnvelope>>
) -> BlockNoteTree {
BlockNoteTree::with_entries(note_iterator.map(|(batch_idx, note_idx_in_batch, note)| {
(batch_idx, note_idx_in_batch, (note.id().into(), *note.metadata()))
}))
.unwrap()
let note_leaf_iterator = batches.enumerate().flat_map(|(batch_idx, created_notes)| {
created_notes
.iter()
.enumerate()
.map(|(note_idx_in_batch, note)| {
(batch_idx, note_idx_in_batch, (note.id().into(), *note.metadata()))
})
.collect::<Vec<_>>()
});

BlockNoteTree::with_entries(note_leaf_iterator).unwrap()
}

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_idx, note_idx_in_batch, (note.id().into(), note.metadata()))
})
});

Expand Down
Loading
Loading