Skip to content

Commit

Permalink
feat: Implement NoteMetadata protobuf message (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
igamigo authored May 3, 2024
1 parent 422446f commit 410916c
Show file tree
Hide file tree
Showing 19 changed files with 113 additions and 104 deletions.
1 change: 0 additions & 1 deletion crates/proto/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 12 additions & 9 deletions crates/proto/proto/note.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions crates/proto/src/domain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod accounts;
pub mod blocks;
pub mod digest;
pub mod merkle;
pub mod notes;
pub mod nullifiers;

// UTILITIES
Expand Down
33 changes: 33 additions & 0 deletions crates/proto/src/domain/notes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use miden_objects::{
notes::{NoteMetadata, NoteTag, NoteType},
Felt,
};

use crate::errors::{ConversionError, MissingFieldHelper};

impl TryFrom<crate::generated::note::NoteMetadata> for NoteMetadata {
type Error = ConversionError;

fn try_from(value: crate::generated::note::NoteMetadata) -> Result<Self, Self::Error> {
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<NoteMetadata> 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 }
}
}
2 changes: 2 additions & 0 deletions crates/proto/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 0 additions & 3 deletions crates/proto/src/generated/account.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion crates/proto/src/generated/block_header.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
1 change: 0 additions & 1 deletion crates/proto/src/generated/digest.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
1 change: 0 additions & 1 deletion crates/proto/src/generated/merkle.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
1 change: 0 additions & 1 deletion crates/proto/src/generated/mmr.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
32 changes: 17 additions & 15 deletions crates/proto/src/generated/note.rs
Original file line number Diff line number Diff line change
@@ -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<super::account::AccountId>,
#[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 {
Expand All @@ -10,19 +21,14 @@ pub struct Note {
#[prost(message, optional, tag = "3")]
pub note_id: ::core::option::Option<super::digest::Digest>,
#[prost(message, optional, tag = "4")]
pub sender: ::core::option::Option<super::account::AccountId>,
#[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<NoteMetadata>,
#[prost(message, optional, tag = "5")]
pub merkle_path: ::core::option::Option<super::merkle::MerklePath>,
/// 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<u8>>,
}
#[derive(Eq, PartialOrd, Ord, Hash)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct NoteSyncRecord {
Expand All @@ -31,11 +37,7 @@ pub struct NoteSyncRecord {
#[prost(message, optional, tag = "2")]
pub note_id: ::core::option::Option<super::digest::Digest>,
#[prost(message, optional, tag = "3")]
pub sender: ::core::option::Option<super::account::AccountId>,
#[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<NoteMetadata>,
#[prost(message, optional, tag = "4")]
pub merkle_path: ::core::option::Option<super::merkle::MerklePath>,
}
12 changes: 0 additions & 12 deletions crates/proto/src/generated/requests.rs
Original file line number Diff line number Diff line change
@@ -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<u8>,
}
#[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<super::digest::Digest>,
}
#[derive(Eq, PartialOrd, Ord, Hash)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct GetBlockHeaderByNumberRequest {
Expand All @@ -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 {
Expand Down Expand Up @@ -58,7 +54,6 @@ pub struct SyncStateRequest {
#[prost(uint32, repeated, tag = "4")]
pub nullifiers: ::prost::alloc::vec::Vec<u32>,
}
#[derive(Eq, PartialOrd, Ord, Hash)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct GetBlockInputsRequest {
Expand All @@ -69,7 +64,6 @@ pub struct GetBlockInputsRequest {
#[prost(message, repeated, tag = "2")]
pub nullifiers: ::prost::alloc::vec::Vec<super::digest::Digest>,
}
#[derive(Eq, PartialOrd, Ord, Hash)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct GetTransactionInputsRequest {
Expand All @@ -78,36 +72,30 @@ pub struct GetTransactionInputsRequest {
#[prost(message, repeated, tag = "2")]
pub nullifiers: ::prost::alloc::vec::Vec<super::digest::Digest>,
}
#[derive(Eq, PartialOrd, Ord, Hash)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct SubmitProvenTransactionRequest {
/// Transaction encoded using miden's native format
#[prost(bytes = "vec", tag = "1")]
pub transaction: ::prost::alloc::vec::Vec<u8>,
}
#[derive(Eq, PartialOrd, Ord, Hash)]
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct GetNotesByIdRequest {
/// List of NoteId's to be queried from the database
#[prost(message, repeated, tag = "1")]
pub note_ids: ::prost::alloc::vec::Vec<super::digest::Digest>,
}
#[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 {
Expand Down
Loading

0 comments on commit 410916c

Please sign in to comment.