From 410916c9863b88934eb4dc67ff4718abefca7f85 Mon Sep 17 00:00:00 2001 From: igamigo Date: Fri, 3 May 2024 17:59:54 -0300 Subject: [PATCH] feat: Implement `NoteMetadata` protobuf message (#338) --- crates/proto/build.rs | 1 - crates/proto/proto/note.proto | 21 ++++++---- crates/proto/src/domain/mod.rs | 1 + crates/proto/src/domain/notes.rs | 33 +++++++++++++++ crates/proto/src/errors.rs | 2 + crates/proto/src/generated/account.rs | 3 -- crates/proto/src/generated/block_header.rs | 1 - crates/proto/src/generated/digest.rs | 1 - crates/proto/src/generated/merkle.rs | 1 - crates/proto/src/generated/mmr.rs | 1 - crates/proto/src/generated/note.rs | 32 +++++++------- crates/proto/src/generated/requests.rs | 12 ------ crates/proto/src/generated/responses.rs | 17 -------- crates/proto/src/generated/smt.rs | 5 --- crates/store/src/db/mod.rs | 10 ++--- crates/store/src/db/sql.rs | 49 +++++++++++++++------- crates/store/src/db/tests.rs | 19 +++++---- crates/store/src/server/api.rs | 4 +- crates/store/src/state.rs | 4 +- 19 files changed, 113 insertions(+), 104 deletions(-) create mode 100644 crates/proto/src/domain/notes.rs diff --git a/crates/proto/build.rs b/crates/proto/build.rs index 9f0d51a01..016c35e74 100644 --- a/crates/proto/build.rs +++ b/crates/proto/build.rs @@ -28,7 +28,6 @@ fn main() -> miette::Result<()> { // Generate the stub of the user facing server from its proto file tonic_build::configure() .file_descriptor_set_path(&file_descriptor_path) - .type_attribute(".", "#[derive(Eq, PartialOrd, Ord, Hash)]") .skip_protoc_run() .out_dir("src/generated") .compile_with_config(prost_config, protos, includes) diff --git a/crates/proto/proto/note.proto b/crates/proto/proto/note.proto index 6ae3229ee..202d91a99 100644 --- a/crates/proto/proto/note.proto +++ b/crates/proto/proto/note.proto @@ -5,24 +5,27 @@ import "digest.proto"; import "merkle.proto"; import "account.proto"; +message NoteMetadata { + account.AccountId sender = 1; + uint32 note_type = 2; + fixed32 tag = 3; + fixed64 aux = 4; +} + message Note { fixed32 block_num = 1; uint32 note_index = 2; digest.Digest note_id = 3; - account.AccountId sender = 4; - fixed32 tag = 5; - uint32 note_type = 6; - merkle.MerklePath merkle_path = 7; + NoteMetadata metadata = 4; + merkle.MerklePath merkle_path = 5; // This field will be present when the note is on-chain. // details contain the `Note` in a serialized format. - optional bytes details = 8; + optional bytes details = 6; } message NoteSyncRecord { uint32 note_index = 1; digest.Digest note_id = 2; - account.AccountId sender = 3; - fixed32 tag = 4; - uint32 note_type = 5; - merkle.MerklePath merkle_path = 6; + NoteMetadata metadata = 3; + merkle.MerklePath merkle_path = 4; } diff --git a/crates/proto/src/domain/mod.rs b/crates/proto/src/domain/mod.rs index 3afd115db..cb51a0eea 100644 --- a/crates/proto/src/domain/mod.rs +++ b/crates/proto/src/domain/mod.rs @@ -2,6 +2,7 @@ pub mod accounts; pub mod blocks; pub mod digest; pub mod merkle; +pub mod notes; pub mod nullifiers; // UTILITIES diff --git a/crates/proto/src/domain/notes.rs b/crates/proto/src/domain/notes.rs new file mode 100644 index 000000000..64913653a --- /dev/null +++ b/crates/proto/src/domain/notes.rs @@ -0,0 +1,33 @@ +use miden_objects::{ + notes::{NoteMetadata, NoteTag, NoteType}, + Felt, +}; + +use crate::errors::{ConversionError, MissingFieldHelper}; + +impl TryFrom for NoteMetadata { + type Error = ConversionError; + + fn try_from(value: crate::generated::note::NoteMetadata) -> Result { + let sender = value + .sender + .ok_or_else(|| crate::generated::note::NoteMetadata::missing_field("Sender"))? + .try_into()?; + let note_type = NoteType::try_from(value.note_type as u64)?; + let tag = NoteTag::from(value.tag); + let aux = Felt::try_from(value.aux).map_err(|_| ConversionError::NotAValidFelt)?; + + Ok(NoteMetadata::new(sender, note_type, tag, aux)?) + } +} + +impl From for crate::generated::note::NoteMetadata { + fn from(val: NoteMetadata) -> Self { + let sender = Some(val.sender().into()); + let note_type = val.note_type() as u32; + let tag = val.tag().into(); + let aux = val.aux().into(); + + crate::generated::note::NoteMetadata { sender, note_type, tag, aux } + } +} diff --git a/crates/proto/src/errors.rs b/crates/proto/src/errors.rs index e8bf4c598..f5fb1beff 100644 --- a/crates/proto/src/errors.rs +++ b/crates/proto/src/errors.rs @@ -17,6 +17,8 @@ pub enum ConversionError { InsufficientData { expected: usize, got: usize }, #[error("Value is not in the range 0..MODULUS")] NotAValidFelt, + #[error("Invalid note type value: {0}")] + NoteTypeError(#[from] miden_objects::NoteError), #[error("Field `{field_name}` required to be filled in protobuf representation of {entity}")] MissingFieldInProtobufRepresentation { entity: &'static str, diff --git a/crates/proto/src/generated/account.rs b/crates/proto/src/generated/account.rs index c09f39298..eb2934242 100644 --- a/crates/proto/src/generated/account.rs +++ b/crates/proto/src/generated/account.rs @@ -1,5 +1,4 @@ // This file is @generated by prost-build. -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] #[prost(skip_debug)] @@ -10,7 +9,6 @@ pub struct AccountId { #[prost(fixed64, tag = "1")] pub id: u64, } -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct AccountSummary { @@ -21,7 +19,6 @@ pub struct AccountSummary { #[prost(uint32, tag = "3")] pub block_num: u32, } -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct AccountInfo { diff --git a/crates/proto/src/generated/block_header.rs b/crates/proto/src/generated/block_header.rs index a82e214c5..f0fe4ee1d 100644 --- a/crates/proto/src/generated/block_header.rs +++ b/crates/proto/src/generated/block_header.rs @@ -1,5 +1,4 @@ // This file is @generated by prost-build. -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct BlockHeader { diff --git a/crates/proto/src/generated/digest.rs b/crates/proto/src/generated/digest.rs index fe45edf66..b17aafc09 100644 --- a/crates/proto/src/generated/digest.rs +++ b/crates/proto/src/generated/digest.rs @@ -1,6 +1,5 @@ // This file is @generated by prost-build. /// A hash digest, the result of a hash function. -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] #[prost(skip_debug)] diff --git a/crates/proto/src/generated/merkle.rs b/crates/proto/src/generated/merkle.rs index ced20e747..2d2676abb 100644 --- a/crates/proto/src/generated/merkle.rs +++ b/crates/proto/src/generated/merkle.rs @@ -1,5 +1,4 @@ // This file is @generated by prost-build. -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct MerklePath { diff --git a/crates/proto/src/generated/mmr.rs b/crates/proto/src/generated/mmr.rs index e477ef2f4..338c8199a 100644 --- a/crates/proto/src/generated/mmr.rs +++ b/crates/proto/src/generated/mmr.rs @@ -1,5 +1,4 @@ // This file is @generated by prost-build. -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct MmrDelta { diff --git a/crates/proto/src/generated/note.rs b/crates/proto/src/generated/note.rs index 12e919941..6e78cd467 100644 --- a/crates/proto/src/generated/note.rs +++ b/crates/proto/src/generated/note.rs @@ -1,5 +1,16 @@ // This file is @generated by prost-build. -#[derive(Eq, PartialOrd, Ord, Hash)] +#[allow(clippy::derive_partial_eq_without_eq)] +#[derive(Clone, PartialEq, ::prost::Message)] +pub struct NoteMetadata { + #[prost(message, optional, tag = "1")] + pub sender: ::core::option::Option, + #[prost(int32, tag = "2")] + pub note_type: i32, + #[prost(fixed32, tag = "3")] + pub tag: u32, + #[prost(fixed64, tag = "4")] + pub aux: u64, +} #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct Note { @@ -10,19 +21,14 @@ pub struct Note { #[prost(message, optional, tag = "3")] pub note_id: ::core::option::Option, #[prost(message, optional, tag = "4")] - pub sender: ::core::option::Option, - #[prost(fixed32, tag = "5")] - pub tag: u32, - #[prost(uint32, tag = "6")] - pub note_type: u32, - #[prost(message, optional, tag = "7")] + pub metadata: ::core::option::Option, + #[prost(message, optional, tag = "5")] pub merkle_path: ::core::option::Option, /// This field will be present when the note is on-chain. /// details contain the `Note` in a serialized format. - #[prost(bytes = "vec", optional, tag = "8")] + #[prost(bytes = "vec", optional, tag = "6")] pub details: ::core::option::Option<::prost::alloc::vec::Vec>, } -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct NoteSyncRecord { @@ -31,11 +37,7 @@ pub struct NoteSyncRecord { #[prost(message, optional, tag = "2")] pub note_id: ::core::option::Option, #[prost(message, optional, tag = "3")] - pub sender: ::core::option::Option, - #[prost(fixed32, tag = "4")] - pub tag: u32, - #[prost(uint32, tag = "5")] - pub note_type: u32, - #[prost(message, optional, tag = "6")] + pub metadata: ::core::option::Option, + #[prost(message, optional, tag = "4")] pub merkle_path: ::core::option::Option, } diff --git a/crates/proto/src/generated/requests.rs b/crates/proto/src/generated/requests.rs index 39e79ca6e..d35c9c447 100644 --- a/crates/proto/src/generated/requests.rs +++ b/crates/proto/src/generated/requests.rs @@ -1,19 +1,16 @@ // This file is @generated by prost-build. -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct ApplyBlockRequest { #[prost(bytes = "vec", tag = "1")] pub block: ::prost::alloc::vec::Vec, } -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct CheckNullifiersRequest { #[prost(message, repeated, tag = "1")] pub nullifiers: ::prost::alloc::vec::Vec, } -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct GetBlockHeaderByNumberRequest { @@ -28,7 +25,6 @@ pub struct GetBlockHeaderByNumberRequest { /// Specifies state updates the client is intersted in. The server will return the first block which /// contains a note matching `note_tags` or the chain tip. And the corresponding updates to /// `nullifiers` and `account_ids` for that block range. -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct SyncStateRequest { @@ -58,7 +54,6 @@ pub struct SyncStateRequest { #[prost(uint32, repeated, tag = "4")] pub nullifiers: ::prost::alloc::vec::Vec, } -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct GetBlockInputsRequest { @@ -69,7 +64,6 @@ pub struct GetBlockInputsRequest { #[prost(message, repeated, tag = "2")] pub nullifiers: ::prost::alloc::vec::Vec, } -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct GetTransactionInputsRequest { @@ -78,7 +72,6 @@ pub struct GetTransactionInputsRequest { #[prost(message, repeated, tag = "2")] pub nullifiers: ::prost::alloc::vec::Vec, } -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct SubmitProvenTransactionRequest { @@ -86,7 +79,6 @@ pub struct SubmitProvenTransactionRequest { #[prost(bytes = "vec", tag = "1")] pub transaction: ::prost::alloc::vec::Vec, } -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct GetNotesByIdRequest { @@ -94,20 +86,16 @@ pub struct GetNotesByIdRequest { #[prost(message, repeated, tag = "1")] pub note_ids: ::prost::alloc::vec::Vec, } -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct ListNullifiersRequest {} -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct ListAccountsRequest {} -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct ListNotesRequest {} /// Returns the latest state of an account with the specified ID. -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct GetAccountDetailsRequest { diff --git a/crates/proto/src/generated/responses.rs b/crates/proto/src/generated/responses.rs index 8caf31ad1..4969affa8 100644 --- a/crates/proto/src/generated/responses.rs +++ b/crates/proto/src/generated/responses.rs @@ -1,9 +1,7 @@ // This file is @generated by prost-build. -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct ApplyBlockResponse {} -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct CheckNullifiersResponse { @@ -11,14 +9,12 @@ pub struct CheckNullifiersResponse { #[prost(message, repeated, tag = "1")] pub proofs: ::prost::alloc::vec::Vec, } -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct GetBlockHeaderByNumberResponse { #[prost(message, optional, tag = "1")] pub block_header: ::core::option::Option, } -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct NullifierUpdate { @@ -27,7 +23,6 @@ pub struct NullifierUpdate { #[prost(fixed32, tag = "2")] pub block_num: u32, } -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct SyncStateResponse { @@ -51,7 +46,6 @@ pub struct SyncStateResponse { pub nullifiers: ::prost::alloc::vec::Vec, } /// An account returned as a response to the GetBlockInputs -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct AccountBlockInputRecord { @@ -63,7 +57,6 @@ pub struct AccountBlockInputRecord { pub proof: ::core::option::Option, } /// A nullifier returned as a response to the GetBlockInputs -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct NullifierBlockInputRecord { @@ -72,7 +65,6 @@ pub struct NullifierBlockInputRecord { #[prost(message, optional, tag = "2")] pub opening: ::core::option::Option, } -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct GetBlockInputsResponse { @@ -90,7 +82,6 @@ pub struct GetBlockInputsResponse { pub nullifiers: ::prost::alloc::vec::Vec, } /// An account returned as a response to the GetTransactionInputs -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct AccountTransactionInputRecord { @@ -101,7 +92,6 @@ pub struct AccountTransactionInputRecord { pub account_hash: ::core::option::Option, } /// A nullifier returned as a response to the GetTransactionInputs -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct NullifierTransactionInputRecord { @@ -111,7 +101,6 @@ pub struct NullifierTransactionInputRecord { #[prost(fixed32, tag = "2")] pub block_num: u32, } -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct GetTransactionInputsResponse { @@ -120,11 +109,9 @@ pub struct GetTransactionInputsResponse { #[prost(message, repeated, tag = "2")] pub nullifiers: ::prost::alloc::vec::Vec, } -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct SubmitProvenTransactionResponse {} -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct GetNotesByIdResponse { @@ -132,7 +119,6 @@ pub struct GetNotesByIdResponse { #[prost(message, repeated, tag = "1")] pub notes: ::prost::alloc::vec::Vec, } -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct ListNullifiersResponse { @@ -140,7 +126,6 @@ pub struct ListNullifiersResponse { #[prost(message, repeated, tag = "1")] pub nullifiers: ::prost::alloc::vec::Vec, } -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct ListAccountsResponse { @@ -148,7 +133,6 @@ pub struct ListAccountsResponse { #[prost(message, repeated, tag = "1")] pub accounts: ::prost::alloc::vec::Vec, } -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct ListNotesResponse { @@ -156,7 +140,6 @@ pub struct ListNotesResponse { #[prost(message, repeated, tag = "1")] pub notes: ::prost::alloc::vec::Vec, } -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct GetAccountDetailsResponse { diff --git a/crates/proto/src/generated/smt.rs b/crates/proto/src/generated/smt.rs index 82c78fba3..2e22f63b1 100644 --- a/crates/proto/src/generated/smt.rs +++ b/crates/proto/src/generated/smt.rs @@ -1,6 +1,5 @@ // This file is @generated by prost-build. /// An entry in a leaf. -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct SmtLeafEntry { @@ -9,7 +8,6 @@ pub struct SmtLeafEntry { #[prost(message, optional, tag = "2")] pub value: ::core::option::Option, } -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct SmtLeafEntries { @@ -17,7 +15,6 @@ pub struct SmtLeafEntries { pub entries: ::prost::alloc::vec::Vec, } /// A leaf in an SMT, sitting at depth 64. A leaf can contain 0, 1 or multiple leaf entries. -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct SmtLeaf { @@ -26,7 +23,6 @@ pub struct SmtLeaf { } /// Nested message and enum types in `SmtLeaf`. pub mod smt_leaf { - #[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Oneof)] pub enum Leaf { @@ -39,7 +35,6 @@ pub mod smt_leaf { } } /// The opening of a leaf in an SMT. -#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct SmtOpening { diff --git a/crates/store/src/db/mod.rs b/crates/store/src/db/mod.rs index 05728e35f..a50a2792e 100644 --- a/crates/store/src/db/mod.rs +++ b/crates/store/src/db/mod.rs @@ -9,7 +9,7 @@ use miden_objects::{ accounts::delta::AccountUpdateDetails, block::{BlockAccountUpdate, BlockNoteIndex}, crypto::{hash::rpo::RpoDigest, merkle::MerklePath, utils::Deserializable}, - notes::{NoteId, NoteType, Nullifier}, + notes::{NoteId, NoteMetadata, Nullifier}, BlockHeader, GENESIS_BLOCK, }; use rusqlite::vtab::array; @@ -49,9 +49,7 @@ pub struct NoteRecord { pub block_num: BlockNumber, pub note_index: BlockNoteIndex, pub note_id: RpoDigest, - pub note_type: NoteType, - pub sender: AccountId, - pub tag: u32, + pub metadata: NoteMetadata, pub details: Option>, pub merkle_path: MerklePath, } @@ -62,9 +60,7 @@ impl From for NotePb { block_num: note.block_num, note_index: note.note_index.to_absolute_index() as u32, note_id: Some(note.note_id.into()), - sender: Some(note.sender.into()), - tag: note.tag, - note_type: note.note_type as u32, + metadata: Some(note.metadata.into()), merkle_path: Some(note.merkle_path.into()), details: note.details, } diff --git a/crates/store/src/db/sql.rs b/crates/store/src/db/sql.rs index 823e64a7c..7ae92c221 100644 --- a/crates/store/src/db/sql.rs +++ b/crates/store/src/db/sql.rs @@ -7,7 +7,7 @@ use miden_objects::{ accounts::{delta::AccountUpdateDetails, Account, AccountDelta}, block::{BlockAccountUpdate, BlockNoteIndex}, crypto::{hash::rpo::RpoDigest, merkle::MerklePath}, - notes::{NoteId, Nullifier}, + notes::{NoteId, NoteMetadata, NoteType, Nullifier}, utils::serde::{Deserializable, Serializable}, BlockHeader, }; @@ -347,13 +347,19 @@ pub fn select_notes(conn: &mut Connection) -> Result> { let details_data = row.get_ref(8)?.as_blob_or_null()?; let details = details_data.map(>::read_from_bytes).transpose()?; + let note_type = row.get::<_, u8>(4)?.try_into()?; + let sender = column_value_as_u64(row, 5)?; + let tag: u32 = row.get(6)?; + + // TODO: Properly handle note metadata's aux field + let metadata = + NoteMetadata::new(sender.try_into()?, note_type, tag.into(), Default::default())?; + notes.push(NoteRecord { block_num: row.get(0)?, note_index: BlockNoteIndex::new(row.get(1)?, row.get(2)?), note_id, - note_type: row.get::<_, u8>(4)?.try_into()?, - sender: column_value_as_u64(row, 5)?, - tag: row.get(6)?, + metadata, details, merkle_path, }) @@ -396,15 +402,14 @@ pub fn insert_notes(transaction: &Transaction, notes: &[NoteRecord]) -> Result(4)?.try_into()?; + let note_type = row.get::<_, u8>(4)?; let sender = column_value_as_u64(row, 5)?; - let tag = row.get(6)?; + let tag: u32 = row.get(6)?; let merkle_path_data = row.get_ref(7)?.as_blob()?; let merkle_path = MerklePath::read_from_bytes(merkle_path_data)?; let details_data = row.get_ref(8)?.as_blob_or_null()?; let details = details_data.map(>::read_from_bytes).transpose()?; + // TODO: Properly retrieve aux + let metadata = NoteMetadata::new( + sender.try_into()?, + NoteType::try_from(note_type)?, + tag.into(), + Default::default(), + )?; + let note = NoteRecord { block_num, note_index, note_id, - note_type, - sender, - tag, details, + metadata, merkle_path, }; res.push(note); @@ -538,14 +549,20 @@ pub fn select_notes_by_id(conn: &mut Connection, note_ids: &[NoteId]) -> Result< let details_data = row.get_ref(8)?.as_blob_or_null()?; let details = details_data.map(>::read_from_bytes).transpose()?; + let note_type = row.get::<_, u8>(4)?.try_into()?; + let sender = column_value_as_u64(row, 5)?; + let tag: u32 = row.get(6)?; + + // TODO: Properly handle note metadata's aux field + let metadata = + NoteMetadata::new(sender.try_into()?, note_type, tag.into(), Default::default())?; + notes.push(NoteRecord { block_num: row.get(0)?, note_index: BlockNoteIndex::new(row.get(1)?, row.get(2)?), details, note_id: note_id.into(), - note_type: row.get::<_, u8>(4)?.try_into()?, - sender: column_value_as_u64(row, 5)?, - tag: row.get(6)?, + metadata, merkle_path, }) } diff --git a/crates/store/src/db/tests.rs b/crates/store/src/db/tests.rs index ea66e189a..d671e1a09 100644 --- a/crates/store/src/db/tests.rs +++ b/crates/store/src/db/tests.rs @@ -139,9 +139,13 @@ fn test_sql_select_notes() { block_num, note_index: BlockNoteIndex::new(0, i as usize), note_id: num_to_rpo_digest(i as u64), - note_type: NoteType::Public, - sender: i as u64, - tag: i, + metadata: NoteMetadata::new( + ACCOUNT_ID_OFF_CHAIN_SENDER.try_into().unwrap(), + NoteType::Public, + i.into(), + Default::default(), + ) + .unwrap(), details: Some(vec![1, 2, 3]), merkle_path: MerklePath::new(vec![]), }; @@ -601,9 +605,8 @@ fn test_notes() { block_num: block_num_1, note_index, note_id, - note_type: NoteType::Public, - sender: sender.into(), - tag, + metadata: NoteMetadata::new(sender, NoteType::Public, tag.into(), Default::default()) + .unwrap(), details, merkle_path: merkle_path.clone(), }; @@ -635,9 +638,7 @@ fn test_notes() { block_num: block_num_2, note_index: note.note_index, note_id: num_to_rpo_digest(3), - note_type: NoteType::OffChain, - sender: note.sender, - tag: note.tag, + metadata: note.metadata, details: None, merkle_path, }; diff --git a/crates/store/src/server/api.rs b/crates/store/src/server/api.rs index 71ccc4745..18fe4dc50 100644 --- a/crates/store/src/server/api.rs +++ b/crates/store/src/server/api.rs @@ -142,10 +142,8 @@ impl api_server::Api for StoreApi { .into_iter() .map(|note| NoteSyncRecord { note_index: note.note_index.to_absolute_index() as u32, - note_type: note.note_type as u32, note_id: Some(note.note_id.into()), - sender: Some(note.sender.into()), - tag: note.tag, + metadata: Some(note.metadata.into()), merkle_path: Some(note.merkle_path.into()), }) .collect(); diff --git a/crates/store/src/state.rs b/crates/store/src/state.rs index 2de486116..65a257b5a 100644 --- a/crates/store/src/state.rs +++ b/crates/store/src/state.rs @@ -211,9 +211,7 @@ impl State { block_num: block.header().block_num(), note_index, note_id: note.id().into(), - note_type: note.metadata().note_type(), - sender: note.metadata().sender().into(), - tag: note.metadata().tag().into(), + metadata: *note.metadata(), details, merkle_path, })