Skip to content

Commit

Permalink
feat: standardize proto aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasArrachea committed Jan 10, 2025
1 parent 78ca36c commit 95502f9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 42 deletions.
16 changes: 8 additions & 8 deletions crates/proto/src/domain/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@ use miden_objects::{
use super::{convert, try_convert};
use crate::{
errors::{ConversionError, MissingFieldHelper},
generated,
generated as proto,
};

// MERKLE PATH
// ================================================================================================

impl From<&MerklePath> for generated::merkle::MerklePath {
impl From<&MerklePath> for proto::merkle::MerklePath {
fn from(value: &MerklePath) -> Self {
let siblings = value.nodes().iter().map(generated::digest::Digest::from).collect();
generated::merkle::MerklePath { siblings }
let siblings = value.nodes().iter().map(proto::digest::Digest::from).collect();
proto::merkle::MerklePath { siblings }
}
}

impl From<MerklePath> for generated::merkle::MerklePath {
impl From<MerklePath> for proto::merkle::MerklePath {
fn from(value: MerklePath) -> Self {
(&value).into()
}
}

impl TryFrom<&generated::merkle::MerklePath> for MerklePath {
impl TryFrom<&proto::merkle::MerklePath> for MerklePath {
type Error = ConversionError;

fn try_from(merkle_path: &generated::merkle::MerklePath) -> Result<Self, Self::Error> {
fn try_from(merkle_path: &proto::merkle::MerklePath) -> Result<Self, Self::Error> {
merkle_path.siblings.iter().map(Digest::try_from).collect()
}
}
Expand All @@ -38,7 +38,7 @@ impl TryFrom<&generated::merkle::MerklePath> for MerklePath {

impl From<MmrDelta> for generated::mmr::MmrDelta {
fn from(value: MmrDelta) -> Self {
let data = value.data.into_iter().map(generated::digest::Digest::from).collect();
let data = value.data.into_iter().map(proto::digest::Digest::from).collect();
generated::mmr::MmrDelta { forest: value.forest as u64, data }
}
}
Expand Down
31 changes: 15 additions & 16 deletions crates/proto/src/domain/notes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,17 @@ use crate::{
convert,
domain::blocks::BlockInclusionProof,
errors::{ConversionError, MissingFieldHelper},
generated::note::{
NoteAuthenticationInfo as NoteAuthenticationInfoProto,
NoteInclusionInBlockProof as NoteInclusionInBlockProofPb, NoteMetadata as NoteMetadataPb,
},
generated::note as proto,
try_convert,
};

impl TryFrom<NoteMetadataPb> for NoteMetadata {
impl TryFrom<proto::NoteMetadata> for NoteMetadata {
type Error = ConversionError;

fn try_from(value: NoteMetadataPb) -> Result<Self, Self::Error> {
fn try_from(value: proto::NoteMetadata) -> Result<Self, Self::Error> {
let sender = value
.sender
.ok_or_else(|| NoteMetadataPb::missing_field(stringify!(sender)))?
.ok_or_else(|| proto::NoteMetadata::missing_field(stringify!(sender)))?
.try_into()?;
let note_type = NoteType::try_from(value.note_type as u64)?;
let tag = NoteTag::from(value.tag);
Expand All @@ -35,7 +32,7 @@ impl TryFrom<NoteMetadataPb> for NoteMetadata {
}
}

impl From<NoteMetadata> for NoteMetadataPb {
impl From<NoteMetadata> for proto::NoteMetadata {
fn from(val: NoteMetadata) -> Self {
let sender = Some(val.sender().into());
let note_type = val.note_type() as u32;
Expand All @@ -53,7 +50,7 @@ impl From<NoteMetadata> for NoteMetadataPb {
}
}

impl From<(&NoteId, &NoteInclusionProof)> for NoteInclusionInBlockProofPb {
impl From<(&NoteId, &NoteInclusionProof)> for proto::NoteInclusionInBlockProof {
fn from((note_id, proof): (&NoteId, &NoteInclusionProof)) -> Self {
Self {
note_id: Some(note_id.into()),
Expand All @@ -64,18 +61,18 @@ impl From<(&NoteId, &NoteInclusionProof)> for NoteInclusionInBlockProofPb {
}
}

impl TryFrom<&NoteInclusionInBlockProofPb> for (NoteId, NoteInclusionProof) {
impl TryFrom<&proto::NoteInclusionInBlockProof> for (NoteId, NoteInclusionProof) {
type Error = ConversionError;

fn try_from(
proof: &NoteInclusionInBlockProofPb,
proof: &proto::NoteInclusionInBlockProof,
) -> Result<(NoteId, NoteInclusionProof), Self::Error> {
Ok((
Digest::try_from(
proof
.note_id
.as_ref()
.ok_or(NoteInclusionInBlockProofPb::missing_field(stringify!(note_id)))?,
.ok_or(proto::NoteInclusionInBlockProof::missing_field(stringify!(note_id)))?,
)?
.into(),
NoteInclusionProof::new(
Expand All @@ -84,7 +81,9 @@ impl TryFrom<&NoteInclusionInBlockProofPb> for (NoteId, NoteInclusionProof) {
proof
.merkle_path
.as_ref()
.ok_or(NoteInclusionInBlockProofPb::missing_field(stringify!(merkle_path)))?
.ok_or(proto::NoteInclusionInBlockProof::missing_field(stringify!(
merkle_path
)))?
.try_into()?,
)?,
))
Expand All @@ -107,7 +106,7 @@ impl NoteAuthenticationInfo {
}
}

impl From<NoteAuthenticationInfo> for NoteAuthenticationInfoProto {
impl From<NoteAuthenticationInfo> for proto::NoteAuthenticationInfo {
fn from(value: NoteAuthenticationInfo) -> Self {
Self {
note_proofs: convert(&value.note_proofs),
Expand All @@ -116,10 +115,10 @@ impl From<NoteAuthenticationInfo> for NoteAuthenticationInfoProto {
}
}

impl TryFrom<NoteAuthenticationInfoProto> for NoteAuthenticationInfo {
impl TryFrom<proto::NoteAuthenticationInfo> for NoteAuthenticationInfo {
type Error = ConversionError;

fn try_from(value: NoteAuthenticationInfoProto) -> Result<Self, Self::Error> {
fn try_from(value: proto::NoteAuthenticationInfo) -> Result<Self, Self::Error> {
let result = Self {
block_proofs: try_convert(value.block_proofs)?,
note_proofs: try_convert(&value.note_proofs)?,
Expand Down
10 changes: 5 additions & 5 deletions crates/proto/src/domain/nullifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ use miden_objects::{

use crate::{
errors::{ConversionError, MissingFieldHelper},
generated::{digest::Digest, responses::NullifierBlockInputRecord},
generated::{digest as proto, responses::NullifierBlockInputRecord},
};

// FROM NULLIFIER
// ================================================================================================

impl From<&Nullifier> for Digest {
impl From<&Nullifier> for proto::Digest {
fn from(value: &Nullifier) -> Self {
(*value).inner().into()
}
}

impl From<Nullifier> for Digest {
impl From<Nullifier> for proto::Digest {
fn from(value: Nullifier) -> Self {
value.inner().into()
}
Expand All @@ -26,10 +26,10 @@ impl From<Nullifier> for Digest {
// INTO NULLIFIER
// ================================================================================================

impl TryFrom<Digest> for Nullifier {
impl TryFrom<proto::Digest> for Nullifier {
type Error = ConversionError;

fn try_from(value: Digest) -> Result<Self, Self::Error> {
fn try_from(value: proto::Digest) -> Result<Self, Self::Error> {
let digest: RpoDigest = value.try_into()?;
Ok(digest.into())
}
Expand Down
23 changes: 10 additions & 13 deletions crates/proto/src/domain/transactions.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
use miden_objects::{crypto::hash::rpo::RpoDigest, transaction::TransactionId};

use crate::{
errors::ConversionError,
generated::{digest::Digest, transaction::TransactionId as TransactionIdPb},
};
use crate::{errors::ConversionError, generated as proto};

// FROM TRANSACTION ID
// ================================================================================================

impl From<&TransactionId> for Digest {
impl From<&TransactionId> for proto::digest::Digest {
fn from(value: &TransactionId) -> Self {
(*value).inner().into()
}
}

impl From<TransactionId> for Digest {
impl From<TransactionId> for proto::digest::Digest {
fn from(value: TransactionId) -> Self {
value.inner().into()
}
}

impl From<&TransactionId> for TransactionIdPb {
impl From<&TransactionId> for proto::transaction::TransactionId {
fn from(value: &TransactionId) -> Self {
TransactionIdPb { id: Some(value.into()) }
proto::transaction::TransactionId { id: Some(value.into()) }
}
}

impl From<TransactionId> for TransactionIdPb {
impl From<TransactionId> for proto::transaction::TransactionId {
fn from(value: TransactionId) -> Self {
(&value).into()
}
Expand All @@ -35,19 +32,19 @@ impl From<TransactionId> for TransactionIdPb {
// INTO TRANSACTION ID
// ================================================================================================

impl TryFrom<Digest> for TransactionId {
impl TryFrom<proto::digest::Digest> for TransactionId {
type Error = ConversionError;

fn try_from(value: Digest) -> Result<Self, Self::Error> {
fn try_from(value: proto::digest::Digest) -> Result<Self, Self::Error> {
let digest: RpoDigest = value.try_into()?;
Ok(digest.into())
}
}

impl TryFrom<TransactionIdPb> for TransactionId {
impl TryFrom<proto::transaction::TransactionId> for TransactionId {
type Error = ConversionError;

fn try_from(value: TransactionIdPb) -> Result<Self, Self::Error> {
fn try_from(value: proto::transaction::TransactionId) -> Result<Self, Self::Error> {
value
.id
.ok_or(ConversionError::MissingFieldInProtobufRepresentation {
Expand Down

0 comments on commit 95502f9

Please sign in to comment.