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 #303

Closed
wants to merge 28 commits into from

Conversation

phklive
Copy link
Contributor

@phklive phklive commented Apr 5, 2024

In this PR I propose to update the block-proposer to enable addition of on-chain notes to the store through apply_block api

Closes: #141

@phklive phklive requested a review from polydez April 6, 2024 00:18
@phklive phklive marked this pull request as ready for review April 6, 2024 00:18
@phklive
Copy link
Contributor Author

phklive commented Apr 6, 2024

@hackaugusto as we discussed note and tx validation together a few days ago I have the feeling that it is a full other PR in of itself. Should we open another issue to address that thoroughly?

@phklive
Copy link
Contributor Author

phklive commented Apr 6, 2024

@bobbinth Indeed I wasn't fully getting the extent of the trouble we could get into when using the Miden client as a dep for faucet or end-to-end testing. Here is an example where not having a dep on the client to send ProvenTransactions to the Block-Producer for testing could be very useful.

That should be something to address: #291

@phklive phklive changed the base branch from next to phklive-db-onchain-note April 6, 2024 21:19
Comment on lines +71 to +98
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(&note.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();

Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

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

This implementation missing note_index field within the batch.
I would write something like this:

            let created_notes: Vec<NoteCreated> = txs
                .iter()
                .flat_map(|tx| {
                    tx.output_notes().iter().map(|note| {
                        (note, tx.get_output_note_details(&note.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();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the heads up, fixed the implementation.

Base automatically changed from phklive-db-onchain-note to next April 10, 2024 07:39
@Dominik1999
Copy link
Contributor

Nice, only this is remaining, I suppose?

@phklive
Copy link
Contributor Author

phklive commented Apr 10, 2024

Nice, only this is remaining, I suppose?

Yes exactly, rebasing atm.

@phklive
Copy link
Contributor Author

phklive commented Apr 10, 2024

This PR has been rebased on continued here: #310

@phklive phklive closed this Apr 10, 2024
@@ -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`
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Comment on lines +71 to +98
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(&note.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();

Copy link
Contributor

Choose a reason for hiding this comment

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

This implementation missing note_index field within the batch.
I would write something like this:

            let created_notes: Vec<NoteCreated> = txs
                .iter()
                .flat_map(|tx| {
                    tx.output_notes().iter().map(|note| {
                        (note, tx.get_output_note_details(&note.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();

created_notes: Vec<NoteEnvelope>,
created_notes_smt: BatchNoteTree,
created_notes: Vec<NoteCreated>,
created_note_envelopes: Vec<NoteEnvelope>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need these envelopes anywhere else?

@@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

We don't need these two additional usizes, because we already have batch_index and note_index fields in NoteCreated structure. The task is to fill them here. I would probably set note_index in the batch builder (as in my proposed piece of code) and batch_index here.

I think, the best solution would be to not to create NoteCreated in the batch builder, but just to add serialized details field in the TransactionBatch::created_note_envelopes vector:

created_note_envelopes_with_details: Vec<(NoteEnvelope, Option<Vec<u8>>)>,

And after that we would be able to produce NoteCreated here and init its batch_index and note_index fields during creation.

@@ -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)>,
Copy link
Contributor

Choose a reason for hiding this comment

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

If we put NoteCreated here, we don't need to provide additionally batch_index and note_index here.

@@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure we need such conversion.

@@ -24,6 +24,7 @@ pub static MIGRATIONS: Lazy<Migrations> = Lazy::new(|| {
sender INTEGER NOT NULL,
tag INTEGER NOT NULL,
merkle_path BLOB NOT NULL,
details BLOB NOT NULL,
Copy link
Contributor

Choose a reason for hiding this comment

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

Please, consider making details nullable.

@bobbinth bobbinth deleted the phklive-bp-support-onchain-notes branch April 29, 2024 07:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants