-
Notifications
You must be signed in to change notification settings - Fork 38
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
#303
Changes from 27 commits
7c09397
24c05ab
c52dafe
626cb54
0d80b8b
0335339
1a4a829
0c25238
e0572ba
9fd74e3
443795e
a6bd2ea
8ce4963
a32b312
2b037a2
895a7a2
7907be9
9daef27
fd34c43
f9b4a0b
f0e7e1a
544473d
3c5a9fd
f4ec7e5
5ff0403
c1fdaaa
d36c140
f0c0af1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,8 +28,8 @@ genesis.dat | |
*.sqlite3 | ||
*.sqlite3-shm | ||
*.sqlite3-wal | ||
db/ | ||
database/ | ||
|
||
# Configs | ||
/genesis.toml | ||
/miden.toml | ||
/miden.toml |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
use std::collections::BTreeMap; | ||
|
||
use miden_node_proto::domain::accounts::UpdatedAccount; | ||
use miden_node_proto::{domain::accounts::UpdatedAccount, generated::note::NoteCreated}; | ||
use miden_objects::{ | ||
accounts::AccountId, | ||
batches::BatchNoteTree, | ||
crypto::hash::blake::{Blake3Digest, Blake3_256}, | ||
notes::{NoteEnvelope, Nullifier}, | ||
utils::serde::Serializable, | ||
Digest, MAX_NOTES_PER_BATCH, | ||
}; | ||
use tracing::instrument; | ||
|
@@ -26,9 +27,10 @@ pub struct TransactionBatch { | |
id: BatchId, | ||
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_smt: BatchNoteTree, | ||
created_notes: Vec<NoteCreated>, | ||
created_note_envelopes: Vec<NoteEnvelope>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need these envelopes anywhere else? |
||
} | ||
|
||
impl TransactionBatch { | ||
|
@@ -62,23 +64,52 @@ 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> = | ||
let (created_notes, created_notes_smt, created_note_envelopes) = { | ||
let created_note_envelopes: Vec<NoteEnvelope> = | ||
txs.iter().flat_map(|tx| tx.output_notes().iter()).cloned().collect(); | ||
|
||
let created_notes: Vec<NoteCreated> = txs | ||
.iter() | ||
.flat_map(|tx| { | ||
tx.output_notes().iter().map(|note| { | ||
if let Some(note_with_details) = tx.get_output_note_details(¬e.note_id()) | ||
{ | ||
NoteCreated { | ||
batch_index: 0, | ||
note_index: 0, | ||
note_id: Some(note.note_id().into()), | ||
sender: Some(note.metadata().sender().into()), | ||
tag: note.metadata().tag().into(), | ||
details: Some(note_with_details.to_bytes()), | ||
} | ||
} else { | ||
NoteCreated { | ||
batch_index: 0, | ||
note_index: 0, | ||
note_id: Some(note.note_id().into()), | ||
sender: Some(note.metadata().sender().into()), | ||
tag: note.metadata().tag().into(), | ||
details: None, | ||
} | ||
} | ||
}) | ||
}) | ||
.collect(); | ||
|
||
Comment on lines
+71
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will likely have to change after 0xPolygonMiden/miden-base#572. Maybe it's best to point at that branch for now to not have to redo this soonish. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This implementation missing let created_notes: Vec<NoteCreated> = txs
.iter()
.flat_map(|tx| {
tx.output_notes().iter().map(|note| {
(note, tx.get_output_note_details(¬e.note_id())
})
})
.enumerate()
.map(|(note_index, (note, details))| NoteCreated {
batch_index: 0,
note_index: note_index as u32,
note_id: Some(note.note_id().into()),
sender: Some(note.metadata().sender().into()),
tag: note.metadata().tag().into(),
details: details.map(Note::to_bytes),
})
.collect(); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the heads up, fixed the implementation. |
||
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(), | ||
created_notes, | ||
BatchNoteTree::with_contiguous_leaves( | ||
created_notes | ||
created_note_envelopes | ||
.iter() | ||
.map(|note_envelope| (note_envelope.note_id(), note_envelope.metadata())), | ||
) | ||
.map_err(|e| BuildBatchError::NotesSmtError(e, txs))?, | ||
created_note_envelopes, | ||
) | ||
}; | ||
|
||
|
@@ -88,6 +119,7 @@ impl TransactionBatch { | |
produced_nullifiers, | ||
created_notes_smt, | ||
created_notes, | ||
created_note_envelopes, | ||
}) | ||
} | ||
|
||
|
@@ -125,10 +157,16 @@ impl TransactionBatch { | |
} | ||
|
||
/// Returns an iterator over created notes. | ||
pub fn created_notes(&self) -> impl Iterator<Item = &NoteEnvelope> + '_ { | ||
pub fn created_notes(&self) -> impl Iterator<Item = &NoteCreated> + '_ { | ||
self.created_notes.iter() | ||
} | ||
|
||
#[allow(dead_code)] | ||
/// Returns an iterator over created note envelopes. | ||
pub fn created_notes_envelopes(&self) -> impl Iterator<Item = &NoteEnvelope> + '_ { | ||
self.created_note_envelopes.iter() | ||
} | ||
|
||
/// Returns the root hash of the created notes SMT. | ||
pub fn created_notes_root(&self) -> Digest { | ||
self.created_notes_smt.root() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,13 @@ use std::collections::BTreeMap; | |
use miden_node_proto::{ | ||
domain::accounts::UpdatedAccount, | ||
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, | ||
}; | ||
|
||
|
@@ -19,7 +19,7 @@ use crate::store::BlockInputsError; | |
pub struct Block { | ||
pub header: BlockHeader, | ||
pub updated_accounts: Vec<UpdatedAccount>, | ||
pub created_notes: Vec<(usize, usize, NoteEnvelope)>, | ||
pub created_notes: Vec<(usize, usize, NoteCreated)>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we put |
||
pub produced_nullifiers: Vec<Nullifier>, | ||
// TODO: | ||
// - full states for updated public accounts | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
use std::sync::Arc; | ||
use std::{sync::Arc, usize}; | ||
|
||
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}; | ||
|
@@ -79,14 +80,13 @@ where | |
|
||
let updated_accounts: Vec<_> = | ||
batches.iter().flat_map(TransactionBatch::updated_accounts).collect(); | ||
let created_notes = batches | ||
let created_notes: Vec<(usize, usize, NoteCreated)> = batches | ||
.iter() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need these two additional I think, the best solution would be to not to create created_note_envelopes_with_details: Vec<(NoteEnvelope, Option<Vec<u8>>)>, And after that we would be able to produce |
||
.enumerate() | ||
.flat_map(|(batch_idx, batch)| { | ||
batch | ||
.created_notes() | ||
.enumerate() | ||
.map(move |(note_idx_in_batch, note)| (batch_idx, note_idx_in_batch, *note)) | ||
batch.created_notes().enumerate().map(move |(note_idx_in_batch, note)| { | ||
(batch_idx, note_idx_in_batch, note.clone()) | ||
}) | ||
}) | ||
.collect(); | ||
let produced_nullifiers: Vec<Nullifier> = | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
use miden_objects::notes::NoteEnvelope; | ||
|
||
use crate::generated::note; | ||
use crate::generated::note::{self, NoteCreated}; | ||
|
||
// Note | ||
// ================================================================================================ | ||
|
@@ -20,14 +18,15 @@ impl From<note::Note> for note::NoteSyncRecord { | |
// 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure we need such conversion. |
||
Self { | ||
batch_index: batch_idx as u32, | ||
note_index: note_idx as u32, | ||
note_id: Some(note.note_id().into()), | ||
sender: Some(note.metadata().sender().into()), | ||
tag: note.metadata().tag().into(), | ||
note_id: note.note_id, | ||
sender: note.sender, | ||
tag: note.tag, | ||
details: note.details, | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems this comment is not in the right place now. And I think, it is confusing a bit, maybe it would be better to get rid of this comment at all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is also what I thought, I did not know if I had to remove it.
Removed.