From 82b428492ed4317f36d283ab35c1a997cee8ff6d Mon Sep 17 00:00:00 2001 From: Ignacio Amigo Date: Mon, 29 Apr 2024 20:48:56 -0300 Subject: [PATCH 01/13] feat: Utilize NoteMetadata --- crates/proto/build.rs | 2 +- crates/proto/proto/note.proto | 27 ++++++---- crates/proto/src/domain/mod.rs | 1 + crates/proto/src/domain/notes.rs | 35 +++++++++++++ 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 | 61 ++++++++++++++++------ 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 | 50 +++++++++++------- crates/store/src/db/tests.rs | 24 +++++---- crates/store/src/server/api.rs | 4 +- crates/store/src/state.rs | 4 +- genesis.toml | 19 +++++++ miden-node.toml | 21 ++++++++ 21 files changed, 194 insertions(+), 107 deletions(-) create mode 100644 crates/proto/src/domain/notes.rs create mode 100644 genesis.toml create mode 100644 miden-node.toml diff --git a/crates/proto/build.rs b/crates/proto/build.rs index 9f0d51a01..4f34c1401 100644 --- a/crates/proto/build.rs +++ b/crates/proto/build.rs @@ -28,7 +28,7 @@ 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)]") + //.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..18b47c0ae 100644 --- a/crates/proto/proto/note.proto +++ b/crates/proto/proto/note.proto @@ -5,24 +5,33 @@ import "digest.proto"; import "merkle.proto"; import "account.proto"; +enum NoteType { + PUBLIC = 0; + OFF_CHAIN = 1; + ENCRYPTED = 2; +} + +message NoteMetadata { + account.AccountId sender = 1; + NoteType 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..5c04f98ed --- /dev/null +++ b/crates/proto/src/domain/notes.rs @@ -0,0 +1,35 @@ +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::new(value.aux); + + Ok(NoteMetadata::new(sender, note_type, tag, aux)?) + } +} + +impl TryInto for NoteMetadata { + type Error = ConversionError; + + fn try_into(self) -> Result { + let sender = Some(self.sender().into()); + let note_type = self.note_type() as i32; + let tag = self.tag().into(); + let aux = self.aux().into(); + + Ok(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 040dda511..8a5d41297 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..4d37519f6 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(enumeration = "NoteType", 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,36 @@ 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, } +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)] +#[repr(i32)] +pub enum NoteType { + Public = 0, + OffChain = 1, + Encrypted = 2, +} +impl NoteType { + /// String value of the enum field names used in the ProtoBuf definition. + /// + /// The values are not transformed in any way and thus are considered stable + /// (if the ProtoBuf definition does not change) and safe for programmatic use. + pub fn as_str_name(&self) -> &'static str { + match self { + NoteType::Public => "PUBLIC", + NoteType::OffChain => "OFF_CHAIN", + NoteType::Encrypted => "ENCRYPTED", + } + } + /// Creates an enum from field names used in the ProtoBuf definition. + pub fn from_str_name(value: &str) -> ::core::option::Option { + match value { + "PUBLIC" => Some(Self::Public), + "OFF_CHAIN" => Some(Self::OffChain), + "ENCRYPTED" => Some(Self::Encrypted), + _ => None, + } + } +} 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..d9f405f58 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.try_into().unwrap()), 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..fc365ee49 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, }; @@ -323,9 +323,7 @@ pub fn select_notes(conn: &mut Connection) -> Result> { batch_index, note_index, note_id, - note_type, - sender, - tag, + note_metadata, merkle_path, details FROM @@ -347,13 +345,18 @@ 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)?; + + 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 +399,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()?; + 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 +545,19 @@ 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)?; + + 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 83e327c76..e86855914 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( + (i as u64).try_into().unwrap(), + NoteType::Public, + i.into(), + Default::default(), + ) + .unwrap(), details: Some(vec![1, 2, 3]), merkle_path: MerklePath::new(vec![]), }; @@ -601,9 +605,13 @@ 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 +643,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..047bb807e 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.try_into().unwrap()), 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, }) diff --git a/genesis.toml b/genesis.toml new file mode 100644 index 000000000..a0afd9189 --- /dev/null +++ b/genesis.toml @@ -0,0 +1,19 @@ +# This is an example genesis input file for the Miden node. + +version = 1 +timestamp = 1672531200 + +[[accounts]] +type = "BasicWallet" +init_seed = "0xa123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +auth_scheme = "RpoFalcon512" +auth_seed = "0xb123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" + +[[accounts]] +type = "BasicFungibleFaucet" +init_seed = "0xc123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +auth_scheme = "RpoFalcon512" +auth_seed = "0xd123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" +token_symbol = "POL" +decimals = 12 +max_supply = 1000000 diff --git a/miden-node.toml b/miden-node.toml new file mode 100644 index 000000000..04f582981 --- /dev/null +++ b/miden-node.toml @@ -0,0 +1,21 @@ +# This is an example configuration file for the Miden node. + +[block_producer] +# port defined as: sum(ord(c)**p for (p, c) in enumerate('miden-block-producer', 1)) % 2**16 +endpoint = { host = "localhost", port = 48046 } +store_url = "http://localhost:28943" +# enables or disables the verification of transaction proofs before they are accepted into the +# transaction queue. +verify_tx_proofs = true + +[rpc] +# port defined as: sum(ord(c)**p for (p, c) in enumerate('miden-rpc', 1)) % 2**16 +endpoint = { host = "0.0.0.0", port = 57291 } +block_producer_url = "http://localhost:48046" +store_url = "http://localhost:28943" + +[store] +# port defined as: sum(ord(c)**p for (p, c) in enumerate('miden-store', 1)) % 2**16 +endpoint = { host = "localhost", port = 28943 } +database_filepath = "db/miden-store.sqlite3" +genesis_filepath = "genesis.dat" From 99c102b27e81b30fa9d718fab4f4545c1802fc61 Mon Sep 17 00:00:00 2001 From: Ignacio Amigo Date: Mon, 29 Apr 2024 20:51:26 -0300 Subject: [PATCH 02/13] remove files --- genesis.toml | 19 ------------------- miden-node.toml | 21 --------------------- 2 files changed, 40 deletions(-) delete mode 100644 genesis.toml delete mode 100644 miden-node.toml diff --git a/genesis.toml b/genesis.toml deleted file mode 100644 index a0afd9189..000000000 --- a/genesis.toml +++ /dev/null @@ -1,19 +0,0 @@ -# This is an example genesis input file for the Miden node. - -version = 1 -timestamp = 1672531200 - -[[accounts]] -type = "BasicWallet" -init_seed = "0xa123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" -auth_scheme = "RpoFalcon512" -auth_seed = "0xb123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" - -[[accounts]] -type = "BasicFungibleFaucet" -init_seed = "0xc123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" -auth_scheme = "RpoFalcon512" -auth_seed = "0xd123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef" -token_symbol = "POL" -decimals = 12 -max_supply = 1000000 diff --git a/miden-node.toml b/miden-node.toml deleted file mode 100644 index 04f582981..000000000 --- a/miden-node.toml +++ /dev/null @@ -1,21 +0,0 @@ -# This is an example configuration file for the Miden node. - -[block_producer] -# port defined as: sum(ord(c)**p for (p, c) in enumerate('miden-block-producer', 1)) % 2**16 -endpoint = { host = "localhost", port = 48046 } -store_url = "http://localhost:28943" -# enables or disables the verification of transaction proofs before they are accepted into the -# transaction queue. -verify_tx_proofs = true - -[rpc] -# port defined as: sum(ord(c)**p for (p, c) in enumerate('miden-rpc', 1)) % 2**16 -endpoint = { host = "0.0.0.0", port = 57291 } -block_producer_url = "http://localhost:48046" -store_url = "http://localhost:28943" - -[store] -# port defined as: sum(ord(c)**p for (p, c) in enumerate('miden-store', 1)) % 2**16 -endpoint = { host = "localhost", port = 28943 } -database_filepath = "db/miden-store.sqlite3" -genesis_filepath = "genesis.dat" From d4ec0f605ff21c661b582016db3f5a03b3289583 Mon Sep 17 00:00:00 2001 From: Ignacio Amigo Date: Tue, 30 Apr 2024 18:06:27 -0300 Subject: [PATCH 03/13] Fix tests --- crates/store/src/db/sql.rs | 4 +++- crates/store/src/db/tests.rs | 9 ++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/crates/store/src/db/sql.rs b/crates/store/src/db/sql.rs index fc365ee49..858ca2836 100644 --- a/crates/store/src/db/sql.rs +++ b/crates/store/src/db/sql.rs @@ -323,7 +323,9 @@ pub fn select_notes(conn: &mut Connection) -> Result> { batch_index, note_index, note_id, - note_metadata, + note_type, + sender, + tag, merkle_path, details FROM diff --git a/crates/store/src/db/tests.rs b/crates/store/src/db/tests.rs index e86855914..208c18439 100644 --- a/crates/store/src/db/tests.rs +++ b/crates/store/src/db/tests.rs @@ -605,13 +605,8 @@ fn test_notes() { block_num: block_num_1, note_index, note_id, - metadata: NoteMetadata::new( - sender, - NoteType::Public, - tag.into(), - Default::default(), - ) - .unwrap(), + metadata: NoteMetadata::new(sender, NoteType::Public, tag.into(), Default::default()) + .unwrap(), details, merkle_path: merkle_path.clone(), }; From bdbc5340c3ec253c9aa77d3cb12e7c1b6626e163 Mon Sep 17 00:00:00 2001 From: Ignacio Amigo Date: Tue, 30 Apr 2024 18:21:31 -0300 Subject: [PATCH 04/13] Fix tests --- crates/store/src/db/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/store/src/db/tests.rs b/crates/store/src/db/tests.rs index 208c18439..40109d75d 100644 --- a/crates/store/src/db/tests.rs +++ b/crates/store/src/db/tests.rs @@ -140,7 +140,7 @@ fn test_sql_select_notes() { note_index: BlockNoteIndex::new(0, i as usize), note_id: num_to_rpo_digest(i as u64), metadata: NoteMetadata::new( - (i as u64).try_into().unwrap(), + ACCOUNT_ID_OFF_CHAIN_SENDER.try_into().unwrap(), NoteType::Public, i.into(), Default::default(), From e8dfac237abeee3af4783005882b1c7a2d85db7f Mon Sep 17 00:00:00 2001 From: Ignacio Amigo Date: Tue, 30 Apr 2024 18:24:10 -0300 Subject: [PATCH 05/13] Remove unwraps --- crates/proto/src/domain/notes.rs | 16 +++++++--------- crates/store/src/db/mod.rs | 2 +- crates/store/src/server/api.rs | 2 +- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/crates/proto/src/domain/notes.rs b/crates/proto/src/domain/notes.rs index 5c04f98ed..47665cde9 100644 --- a/crates/proto/src/domain/notes.rs +++ b/crates/proto/src/domain/notes.rs @@ -21,15 +21,13 @@ impl TryFrom for NoteMetadata { } } -impl TryInto for NoteMetadata { - type Error = ConversionError; - - fn try_into(self) -> Result { - let sender = Some(self.sender().into()); - let note_type = self.note_type() as i32; - let tag = self.tag().into(); - let aux = self.aux().into(); +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 i32; + let tag = val.tag().into(); + let aux = val.aux().into(); - Ok(crate::generated::note::NoteMetadata { sender, note_type, tag, aux }) + crate::generated::note::NoteMetadata { sender, note_type, tag, aux } } } diff --git a/crates/store/src/db/mod.rs b/crates/store/src/db/mod.rs index d9f405f58..a50a2792e 100644 --- a/crates/store/src/db/mod.rs +++ b/crates/store/src/db/mod.rs @@ -60,7 +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()), - metadata: Some(note.metadata.try_into().unwrap()), + metadata: Some(note.metadata.into()), merkle_path: Some(note.merkle_path.into()), details: note.details, } diff --git a/crates/store/src/server/api.rs b/crates/store/src/server/api.rs index 047bb807e..18fe4dc50 100644 --- a/crates/store/src/server/api.rs +++ b/crates/store/src/server/api.rs @@ -143,7 +143,7 @@ impl api_server::Api for StoreApi { .map(|note| NoteSyncRecord { note_index: note.note_index.to_absolute_index() as u32, note_id: Some(note.note_id.into()), - metadata: Some(note.metadata.try_into().unwrap()), + metadata: Some(note.metadata.into()), merkle_path: Some(note.merkle_path.into()), }) .collect(); From 821707aa7d5e45d6cc96cebe988d5414b8f2cd8b Mon Sep 17 00:00:00 2001 From: Ignacio Amigo Date: Tue, 30 Apr 2024 19:12:40 -0300 Subject: [PATCH 06/13] Avoid deriving for enums --- crates/proto/build.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/crates/proto/build.rs b/crates/proto/build.rs index 4f34c1401..2ba2d3500 100644 --- a/crates/proto/build.rs +++ b/crates/proto/build.rs @@ -3,6 +3,8 @@ use std::{env, fs, path::PathBuf}; use miette::IntoDiagnostic; use prost::Message; +const DERIVES: &str = "#[derive(Eq, PartialOrd, Ord, Hash)]"; + fn main() -> miette::Result<()> { // Compute the directory of the `proto` definitions let cwd: PathBuf = env::current_dir().into_diagnostic()?; @@ -25,12 +27,30 @@ fn main() -> miette::Result<()> { let mut prost_config = prost_build::Config::new(); prost_config.skip_debug(["AccountId", "Digest"]); + + // Compile all types except enums, to avoid duplicate attributes + let mut types = Vec::new(); + for file in file_descriptors.file { + for message in file.message_type { + types.push(message.name.clone()); + } + + for service in file.service { + types.push(service.name.clone()); + } + } // Generate the stub of the user facing server from its proto file - tonic_build::configure() + let mut compile_config = tonic_build::configure() .file_descriptor_set_path(&file_descriptor_path) - //.type_attribute(".", "#[derive(Eq, PartialOrd, Ord, Hash)]") .skip_protoc_run() - .out_dir("src/generated") + .out_dir("src/generated"); + + for protobuf_type in types { + compile_config = + compile_config.enum_attribute(format!(".{}", protobuf_type.unwrap()), DERIVES); + } + + compile_config .compile_with_config(prost_config, protos, includes) .into_diagnostic()?; From 8326fb82d89d28fd120b03c8677799c4f813011c Mon Sep 17 00:00:00 2001 From: Ignacio Amigo Date: Tue, 30 Apr 2024 19:32:43 -0300 Subject: [PATCH 07/13] Revert build script --- crates/proto/build.rs | 27 +++------------------- 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 | 4 ++++ crates/proto/src/generated/requests.rs | 12 ++++++++++ crates/proto/src/generated/responses.rs | 17 ++++++++++++++ crates/proto/src/generated/smt.rs | 5 ++++ 10 files changed, 48 insertions(+), 24 deletions(-) diff --git a/crates/proto/build.rs b/crates/proto/build.rs index 2ba2d3500..31d1a140f 100644 --- a/crates/proto/build.rs +++ b/crates/proto/build.rs @@ -9,11 +9,9 @@ fn main() -> miette::Result<()> { // Compute the directory of the `proto` definitions let cwd: PathBuf = env::current_dir().into_diagnostic()?; let proto_dir: PathBuf = cwd.join("proto"); - // Compute the compiler's target file path. let out = env::var("OUT_DIR").into_diagnostic()?; let file_descriptor_path = PathBuf::from(out).join("file_descriptor_set.bin"); - // Compile the proto file for all servers APIs let protos = &[ proto_dir.join("block_producer.proto"), @@ -23,34 +21,15 @@ fn main() -> miette::Result<()> { let includes = &[proto_dir]; let file_descriptors = protox::compile(protos, includes)?; fs::write(&file_descriptor_path, file_descriptors.encode_to_vec()).into_diagnostic()?; - let mut prost_config = prost_build::Config::new(); prost_config.skip_debug(["AccountId", "Digest"]); - - // Compile all types except enums, to avoid duplicate attributes - let mut types = Vec::new(); - for file in file_descriptors.file { - for message in file.message_type { - types.push(message.name.clone()); - } - - for service in file.service { - types.push(service.name.clone()); - } - } // Generate the stub of the user facing server from its proto file - let mut compile_config = tonic_build::configure() + tonic_build::configure() .file_descriptor_set_path(&file_descriptor_path) + .type_attribute(".", "#[derive(Eq, PartialOrd, Ord, Hash)]") .skip_protoc_run() - .out_dir("src/generated"); - - for protobuf_type in types { - compile_config = - compile_config.enum_attribute(format!(".{}", protobuf_type.unwrap()), DERIVES); - } - - compile_config + .out_dir("src/generated") .compile_with_config(prost_config, protos, includes) .into_diagnostic()?; diff --git a/crates/proto/src/generated/account.rs b/crates/proto/src/generated/account.rs index eb2934242..c09f39298 100644 --- a/crates/proto/src/generated/account.rs +++ b/crates/proto/src/generated/account.rs @@ -1,4 +1,5 @@ // 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)] @@ -9,6 +10,7 @@ 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 { @@ -19,6 +21,7 @@ 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 8a5d41297..040dda511 100644 --- a/crates/proto/src/generated/block_header.rs +++ b/crates/proto/src/generated/block_header.rs @@ -1,4 +1,5 @@ // 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 b17aafc09..fe45edf66 100644 --- a/crates/proto/src/generated/digest.rs +++ b/crates/proto/src/generated/digest.rs @@ -1,5 +1,6 @@ // 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 2d2676abb..ced20e747 100644 --- a/crates/proto/src/generated/merkle.rs +++ b/crates/proto/src/generated/merkle.rs @@ -1,4 +1,5 @@ // 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 338c8199a..e477ef2f4 100644 --- a/crates/proto/src/generated/mmr.rs +++ b/crates/proto/src/generated/mmr.rs @@ -1,4 +1,5 @@ // 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 4d37519f6..30d4aa428 100644 --- a/crates/proto/src/generated/note.rs +++ b/crates/proto/src/generated/note.rs @@ -1,4 +1,5 @@ // 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 { @@ -11,6 +12,7 @@ pub struct NoteMetadata { #[prost(fixed64, tag = "4")] pub aux: u64, } +#[derive(Eq, PartialOrd, Ord, Hash)] #[allow(clippy::derive_partial_eq_without_eq)] #[derive(Clone, PartialEq, ::prost::Message)] pub struct Note { @@ -29,6 +31,7 @@ pub struct Note { #[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 { @@ -41,6 +44,7 @@ pub struct NoteSyncRecord { #[prost(message, optional, tag = "4")] pub merkle_path: ::core::option::Option, } +#[derive(Eq, PartialOrd, Ord, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)] #[repr(i32)] pub enum NoteType { diff --git a/crates/proto/src/generated/requests.rs b/crates/proto/src/generated/requests.rs index d35c9c447..39e79ca6e 100644 --- a/crates/proto/src/generated/requests.rs +++ b/crates/proto/src/generated/requests.rs @@ -1,16 +1,19 @@ // 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 { @@ -25,6 +28,7 @@ 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 { @@ -54,6 +58,7 @@ 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 { @@ -64,6 +69,7 @@ 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 { @@ -72,6 +78,7 @@ 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 { @@ -79,6 +86,7 @@ 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 { @@ -86,16 +94,20 @@ 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 4969affa8..8caf31ad1 100644 --- a/crates/proto/src/generated/responses.rs +++ b/crates/proto/src/generated/responses.rs @@ -1,7 +1,9 @@ // 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 { @@ -9,12 +11,14 @@ 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 { @@ -23,6 +27,7 @@ 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 { @@ -46,6 +51,7 @@ 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 { @@ -57,6 +63,7 @@ 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 { @@ -65,6 +72,7 @@ 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 { @@ -82,6 +90,7 @@ 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 { @@ -92,6 +101,7 @@ 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 { @@ -101,6 +111,7 @@ 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 { @@ -109,9 +120,11 @@ 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 { @@ -119,6 +132,7 @@ 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 { @@ -126,6 +140,7 @@ 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 { @@ -133,6 +148,7 @@ 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 { @@ -140,6 +156,7 @@ 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 2e22f63b1..82c78fba3 100644 --- a/crates/proto/src/generated/smt.rs +++ b/crates/proto/src/generated/smt.rs @@ -1,5 +1,6 @@ // 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 { @@ -8,6 +9,7 @@ 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 { @@ -15,6 +17,7 @@ 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 { @@ -23,6 +26,7 @@ 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 { @@ -35,6 +39,7 @@ 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 { From f0a4a200e9b67d68f7ae373aebe5331b771e8347 Mon Sep 17 00:00:00 2001 From: igamigo Date: Tue, 30 Apr 2024 19:34:15 -0300 Subject: [PATCH 08/13] Update build.rs --- crates/proto/build.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/crates/proto/build.rs b/crates/proto/build.rs index 31d1a140f..d86026b4c 100644 --- a/crates/proto/build.rs +++ b/crates/proto/build.rs @@ -3,8 +3,6 @@ use std::{env, fs, path::PathBuf}; use miette::IntoDiagnostic; use prost::Message; -const DERIVES: &str = "#[derive(Eq, PartialOrd, Ord, Hash)]"; - fn main() -> miette::Result<()> { // Compute the directory of the `proto` definitions let cwd: PathBuf = env::current_dir().into_diagnostic()?; From 4a2dc53fb149a05d2e7f45c632a16d9f722d41e3 Mon Sep 17 00:00:00 2001 From: Ignacio Amigo Date: Thu, 2 May 2024 17:13:52 -0300 Subject: [PATCH 09/13] Remove custom derives --- crates/proto/build.rs | 1 - crates/proto/proto/note.proto | 27 ++++++---- 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 | 61 ++++++++++++++++------ 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 | 46 ++++++++++------ crates/store/src/db/tests.rs | 19 +++---- crates/store/src/server/api.rs | 4 +- crates/store/src/state.rs | 4 +- 19 files changed, 145 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..18b47c0ae 100644 --- a/crates/proto/proto/note.proto +++ b/crates/proto/proto/note.proto @@ -5,24 +5,33 @@ import "digest.proto"; import "merkle.proto"; import "account.proto"; +enum NoteType { + PUBLIC = 0; + OFF_CHAIN = 1; + ENCRYPTED = 2; +} + +message NoteMetadata { + account.AccountId sender = 1; + NoteType 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..47665cde9 --- /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::new(value.aux); + + 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 i32; + 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 040dda511..8a5d41297 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..4d37519f6 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(enumeration = "NoteType", 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,36 @@ 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, } +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)] +#[repr(i32)] +pub enum NoteType { + Public = 0, + OffChain = 1, + Encrypted = 2, +} +impl NoteType { + /// String value of the enum field names used in the ProtoBuf definition. + /// + /// The values are not transformed in any way and thus are considered stable + /// (if the ProtoBuf definition does not change) and safe for programmatic use. + pub fn as_str_name(&self) -> &'static str { + match self { + NoteType::Public => "PUBLIC", + NoteType::OffChain => "OFF_CHAIN", + NoteType::Encrypted => "ENCRYPTED", + } + } + /// Creates an enum from field names used in the ProtoBuf definition. + pub fn from_str_name(value: &str) -> ::core::option::Option { + match value { + "PUBLIC" => Some(Self::Public), + "OFF_CHAIN" => Some(Self::OffChain), + "ENCRYPTED" => Some(Self::Encrypted), + _ => None, + } + } +} 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..858ca2836 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,18 @@ 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)?; + + 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 +401,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()?; + 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 +547,19 @@ 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)?; + + 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 83e327c76..40109d75d 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, }) From 29e2112a09da66f858cb84c804e57f781973db41 Mon Sep 17 00:00:00 2001 From: Ignacio Amigo Date: Fri, 3 May 2024 14:43:13 -0300 Subject: [PATCH 10/13] Address reviews --- crates/proto/build.rs | 3 +++ crates/proto/proto/note.proto | 10 +++++++--- crates/proto/src/domain/notes.rs | 16 +++++++++++++++- crates/proto/src/generated/note.rs | 12 +++++++++--- crates/store/src/db/sql.rs | 3 +++ 5 files changed, 37 insertions(+), 7 deletions(-) diff --git a/crates/proto/build.rs b/crates/proto/build.rs index d26f72d3b..016c35e74 100644 --- a/crates/proto/build.rs +++ b/crates/proto/build.rs @@ -7,9 +7,11 @@ fn main() -> miette::Result<()> { // Compute the directory of the `proto` definitions let cwd: PathBuf = env::current_dir().into_diagnostic()?; let proto_dir: PathBuf = cwd.join("proto"); + // Compute the compiler's target file path. let out = env::var("OUT_DIR").into_diagnostic()?; let file_descriptor_path = PathBuf::from(out).join("file_descriptor_set.bin"); + // Compile the proto file for all servers APIs let protos = &[ proto_dir.join("block_producer.proto"), @@ -19,6 +21,7 @@ fn main() -> miette::Result<()> { let includes = &[proto_dir]; let file_descriptors = protox::compile(protos, includes)?; fs::write(&file_descriptor_path, file_descriptors.encode_to_vec()).into_diagnostic()?; + let mut prost_config = prost_build::Config::new(); prost_config.skip_debug(["AccountId", "Digest"]); diff --git a/crates/proto/proto/note.proto b/crates/proto/proto/note.proto index 18b47c0ae..bcd6e5f5d 100644 --- a/crates/proto/proto/note.proto +++ b/crates/proto/proto/note.proto @@ -5,10 +5,14 @@ import "digest.proto"; import "merkle.proto"; import "account.proto"; +// These values should always match the values in +// https://github.com/0xPolygonMiden/miden-base/blob/next/objects/src/notes/note_type.rs#L10-L12 enum NoteType { - PUBLIC = 0; - OFF_CHAIN = 1; - ENCRYPTED = 2; + // PHANTOM variant exists so that the number representations map correctly + PHANTOM = 0; + PUBLIC = 1; + OFF_CHAIN = 2; + ENCRYPTED = 3; } message NoteMetadata { diff --git a/crates/proto/src/domain/notes.rs b/crates/proto/src/domain/notes.rs index 47665cde9..3ac4c5918 100644 --- a/crates/proto/src/domain/notes.rs +++ b/crates/proto/src/domain/notes.rs @@ -15,7 +15,7 @@ impl TryFrom for NoteMetadata { .try_into()?; let note_type = NoteType::try_from(value.note_type as u64)?; let tag = NoteTag::from(value.tag); - let aux = Felt::new(value.aux); + let aux = Felt::try_from(value.aux).map_err(|_| ConversionError::NotAValidFelt)?; Ok(NoteMetadata::new(sender, note_type, tag, aux)?) } @@ -31,3 +31,17 @@ impl From for crate::generated::note::NoteMetadata { crate::generated::note::NoteMetadata { sender, note_type, tag, aux } } } + +#[cfg(test)] +mod tests { + use miden_objects::notes::NoteType as BaseNoteType; + + use crate::generated::note::NoteType; + + #[test] + fn ensure_note_type_correct_mapping() { + assert_eq!(NoteType::Encrypted as u8, BaseNoteType::Encrypted as u8); + assert_eq!(NoteType::OffChain as u8, BaseNoteType::OffChain as u8); + assert_eq!(NoteType::Public as u8, BaseNoteType::Public as u8); + } +} diff --git a/crates/proto/src/generated/note.rs b/crates/proto/src/generated/note.rs index 4d37519f6..e4454d8e0 100644 --- a/crates/proto/src/generated/note.rs +++ b/crates/proto/src/generated/note.rs @@ -41,12 +41,16 @@ pub struct NoteSyncRecord { #[prost(message, optional, tag = "4")] pub merkle_path: ::core::option::Option, } +/// These values should always match the values in +/// #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)] #[repr(i32)] pub enum NoteType { - Public = 0, - OffChain = 1, - Encrypted = 2, + /// PHANTOM variant exists so that the number representations map correctly + Phantom = 0, + Public = 1, + OffChain = 2, + Encrypted = 3, } impl NoteType { /// String value of the enum field names used in the ProtoBuf definition. @@ -55,6 +59,7 @@ impl NoteType { /// (if the ProtoBuf definition does not change) and safe for programmatic use. pub fn as_str_name(&self) -> &'static str { match self { + NoteType::Phantom => "PHANTOM", NoteType::Public => "PUBLIC", NoteType::OffChain => "OFF_CHAIN", NoteType::Encrypted => "ENCRYPTED", @@ -63,6 +68,7 @@ impl NoteType { /// Creates an enum from field names used in the ProtoBuf definition. pub fn from_str_name(value: &str) -> ::core::option::Option { match value { + "PHANTOM" => Some(Self::Phantom), "PUBLIC" => Some(Self::Public), "OFF_CHAIN" => Some(Self::OffChain), "ENCRYPTED" => Some(Self::Encrypted), diff --git a/crates/store/src/db/sql.rs b/crates/store/src/db/sql.rs index 858ca2836..95b37b261 100644 --- a/crates/store/src/db/sql.rs +++ b/crates/store/src/db/sql.rs @@ -351,6 +351,7 @@ pub fn select_notes(conn: &mut Connection) -> Result> { let sender = column_value_as_u64(row, 5)?; let tag: u32 = row.get(6)?; + // TODO: Properly handle note metadata let metadata = NoteMetadata::new(sender.try_into()?, note_type, tag.into(), Default::default())?; @@ -487,6 +488,7 @@ pub fn select_notes_since_block_by_tag_and_sender( 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)?, @@ -551,6 +553,7 @@ pub fn select_notes_by_id(conn: &mut Connection, note_ids: &[NoteId]) -> Result< 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())?; From afbebf82b621d2f4a64bbed1a74539b93702e0d1 Mon Sep 17 00:00:00 2001 From: Ignacio Amigo Date: Fri, 3 May 2024 15:20:27 -0300 Subject: [PATCH 11/13] Remove protobuf enu --- crates/proto/proto/note.proto | 12 +-------- crates/proto/src/domain/notes.rs | 16 +----------- crates/proto/src/generated/note.rs | 39 ++---------------------------- 3 files changed, 4 insertions(+), 63 deletions(-) diff --git a/crates/proto/proto/note.proto b/crates/proto/proto/note.proto index bcd6e5f5d..e38594fef 100644 --- a/crates/proto/proto/note.proto +++ b/crates/proto/proto/note.proto @@ -5,19 +5,9 @@ import "digest.proto"; import "merkle.proto"; import "account.proto"; -// These values should always match the values in -// https://github.com/0xPolygonMiden/miden-base/blob/next/objects/src/notes/note_type.rs#L10-L12 -enum NoteType { - // PHANTOM variant exists so that the number representations map correctly - PHANTOM = 0; - PUBLIC = 1; - OFF_CHAIN = 2; - ENCRYPTED = 3; -} - message NoteMetadata { account.AccountId sender = 1; - NoteType note_type = 2; + fixed32 note_type = 2; fixed32 tag = 3; fixed64 aux = 4; } diff --git a/crates/proto/src/domain/notes.rs b/crates/proto/src/domain/notes.rs index 3ac4c5918..64913653a 100644 --- a/crates/proto/src/domain/notes.rs +++ b/crates/proto/src/domain/notes.rs @@ -24,24 +24,10 @@ impl TryFrom for NoteMetadata { 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 i32; + 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 } } } - -#[cfg(test)] -mod tests { - use miden_objects::notes::NoteType as BaseNoteType; - - use crate::generated::note::NoteType; - - #[test] - fn ensure_note_type_correct_mapping() { - assert_eq!(NoteType::Encrypted as u8, BaseNoteType::Encrypted as u8); - assert_eq!(NoteType::OffChain as u8, BaseNoteType::OffChain as u8); - assert_eq!(NoteType::Public as u8, BaseNoteType::Public as u8); - } -} diff --git a/crates/proto/src/generated/note.rs b/crates/proto/src/generated/note.rs index e4454d8e0..003e19a9f 100644 --- a/crates/proto/src/generated/note.rs +++ b/crates/proto/src/generated/note.rs @@ -4,8 +4,8 @@ pub struct NoteMetadata { #[prost(message, optional, tag = "1")] pub sender: ::core::option::Option, - #[prost(enumeration = "NoteType", tag = "2")] - pub note_type: i32, + #[prost(fixed32, tag = "2")] + pub note_type: u32, #[prost(fixed32, tag = "3")] pub tag: u32, #[prost(fixed64, tag = "4")] @@ -41,38 +41,3 @@ pub struct NoteSyncRecord { #[prost(message, optional, tag = "4")] pub merkle_path: ::core::option::Option, } -/// These values should always match the values in -/// -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)] -#[repr(i32)] -pub enum NoteType { - /// PHANTOM variant exists so that the number representations map correctly - Phantom = 0, - Public = 1, - OffChain = 2, - Encrypted = 3, -} -impl NoteType { - /// String value of the enum field names used in the ProtoBuf definition. - /// - /// The values are not transformed in any way and thus are considered stable - /// (if the ProtoBuf definition does not change) and safe for programmatic use. - pub fn as_str_name(&self) -> &'static str { - match self { - NoteType::Phantom => "PHANTOM", - NoteType::Public => "PUBLIC", - NoteType::OffChain => "OFF_CHAIN", - NoteType::Encrypted => "ENCRYPTED", - } - } - /// Creates an enum from field names used in the ProtoBuf definition. - pub fn from_str_name(value: &str) -> ::core::option::Option { - match value { - "PHANTOM" => Some(Self::Phantom), - "PUBLIC" => Some(Self::Public), - "OFF_CHAIN" => Some(Self::OffChain), - "ENCRYPTED" => Some(Self::Encrypted), - _ => None, - } - } -} From af6caac7e48df9ea31e71138b59fc2715e738103 Mon Sep 17 00:00:00 2001 From: Ignacio Amigo Date: Fri, 3 May 2024 15:58:48 -0300 Subject: [PATCH 12/13] Change protobuf type --- crates/proto/proto/note.proto | 2 +- crates/proto/src/generated/note.rs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/proto/proto/note.proto b/crates/proto/proto/note.proto index e38594fef..202d91a99 100644 --- a/crates/proto/proto/note.proto +++ b/crates/proto/proto/note.proto @@ -7,7 +7,7 @@ import "account.proto"; message NoteMetadata { account.AccountId sender = 1; - fixed32 note_type = 2; + uint32 note_type = 2; fixed32 tag = 3; fixed64 aux = 4; } diff --git a/crates/proto/src/generated/note.rs b/crates/proto/src/generated/note.rs index 003e19a9f..6e78cd467 100644 --- a/crates/proto/src/generated/note.rs +++ b/crates/proto/src/generated/note.rs @@ -4,8 +4,8 @@ pub struct NoteMetadata { #[prost(message, optional, tag = "1")] pub sender: ::core::option::Option, - #[prost(fixed32, tag = "2")] - pub note_type: u32, + #[prost(int32, tag = "2")] + pub note_type: i32, #[prost(fixed32, tag = "3")] pub tag: u32, #[prost(fixed64, tag = "4")] From 9ef0a86768655a4b8216ad2f2f1cc83b4c90afa2 Mon Sep 17 00:00:00 2001 From: igamigo Date: Fri, 3 May 2024 16:10:27 -0300 Subject: [PATCH 13/13] Improve comment --- crates/store/src/db/sql.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/store/src/db/sql.rs b/crates/store/src/db/sql.rs index 95b37b261..7ae92c221 100644 --- a/crates/store/src/db/sql.rs +++ b/crates/store/src/db/sql.rs @@ -351,7 +351,7 @@ pub fn select_notes(conn: &mut Connection) -> Result> { let sender = column_value_as_u64(row, 5)?; let tag: u32 = row.get(6)?; - // TODO: Properly handle note metadata + // TODO: Properly handle note metadata's aux field let metadata = NoteMetadata::new(sender.try_into()?, note_type, tag.into(), Default::default())?;