Skip to content

Commit

Permalink
style: standardize proto aliases (0xPolygonMiden#610)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasArrachea authored Jan 13, 2025
1 parent 78ca36c commit ecacf1c
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 105 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

### Changes

- Standardized protobuf type aliases (#609).
- [BREAKING] Added support for new two `Felt` account ID (#591).
- [BREAKING] Inverted `TransactionInputs.missing_unauthenticated_notes` to `found_missing_notes` (#509).

Expand Down
57 changes: 33 additions & 24 deletions crates/proto/src/domain/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ use miden_objects::{

use crate::{
errors::{ConversionError, MissingFieldHelper},
generated::{
account as proto,
responses::{AccountBlockInputRecord, AccountTransactionInputRecord},
},
generated as proto,
};

// ACCOUNT ID
// ================================================================================================

impl Display for proto::AccountId {
impl Display for proto::account::AccountId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "0x")?;
for byte in &self.id {
Expand All @@ -29,7 +26,7 @@ impl Display for proto::AccountId {
}
}

impl Debug for proto::AccountId {
impl Debug for proto::account::AccountId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(self, f)
}
Expand All @@ -38,13 +35,13 @@ impl Debug for proto::AccountId {
// INTO PROTO ACCOUNT ID
// ------------------------------------------------------------------------------------------------

impl From<&AccountId> for proto::AccountId {
impl From<&AccountId> for proto::account::AccountId {
fn from(account_id: &AccountId) -> Self {
(*account_id).into()
}
}

impl From<AccountId> for proto::AccountId {
impl From<AccountId> for proto::account::AccountId {
fn from(account_id: AccountId) -> Self {
Self { id: account_id.to_bytes() }
}
Expand All @@ -53,10 +50,10 @@ impl From<AccountId> for proto::AccountId {
// FROM PROTO ACCOUNT ID
// ------------------------------------------------------------------------------------------------

impl TryFrom<proto::AccountId> for AccountId {
impl TryFrom<proto::account::AccountId> for AccountId {
type Error = ConversionError;

fn try_from(account_id: proto::AccountId) -> Result<Self, Self::Error> {
fn try_from(account_id: proto::account::AccountId) -> Result<Self, Self::Error> {
AccountId::read_from_bytes(&account_id.id).map_err(|_| ConversionError::NotAValidFelt)
}
}
Expand All @@ -71,7 +68,7 @@ pub struct AccountSummary {
pub block_num: u32,
}

impl From<&AccountSummary> for proto::AccountSummary {
impl From<&AccountSummary> for proto::account::AccountSummary {
fn from(update: &AccountSummary) -> Self {
Self {
account_id: Some(update.account_id.into()),
Expand All @@ -87,7 +84,7 @@ pub struct AccountInfo {
pub details: Option<Account>,
}

impl From<&AccountInfo> for proto::AccountInfo {
impl From<&AccountInfo> for proto::account::AccountInfo {
fn from(AccountInfo { summary, details }: &AccountInfo) -> Self {
Self {
summary: Some(summary.into()),
Expand All @@ -106,7 +103,7 @@ pub struct AccountInputRecord {
pub proof: MerklePath,
}

impl From<AccountInputRecord> for AccountBlockInputRecord {
impl From<AccountInputRecord> for proto::responses::AccountBlockInputRecord {
fn from(from: AccountInputRecord) -> Self {
Self {
account_id: Some(from.account_id.into()),
Expand All @@ -116,23 +113,29 @@ impl From<AccountInputRecord> for AccountBlockInputRecord {
}
}

impl TryFrom<AccountBlockInputRecord> for AccountInputRecord {
impl TryFrom<proto::responses::AccountBlockInputRecord> for AccountInputRecord {
type Error = ConversionError;

fn try_from(account_input_record: AccountBlockInputRecord) -> Result<Self, Self::Error> {
fn try_from(
account_input_record: proto::responses::AccountBlockInputRecord,
) -> Result<Self, Self::Error> {
Ok(Self {
account_id: account_input_record
.account_id
.ok_or(AccountBlockInputRecord::missing_field(stringify!(account_id)))?
.ok_or(proto::responses::AccountBlockInputRecord::missing_field(stringify!(
account_id
)))?
.try_into()?,
account_hash: account_input_record
.account_hash
.ok_or(AccountBlockInputRecord::missing_field(stringify!(account_hash)))?
.ok_or(proto::responses::AccountBlockInputRecord::missing_field(stringify!(
account_hash
)))?
.try_into()?,
proof: account_input_record
.proof
.as_ref()
.ok_or(AccountBlockInputRecord::missing_field(stringify!(proof)))?
.ok_or(proto::responses::AccountBlockInputRecord::missing_field(stringify!(proof)))?
.try_into()?,
})
}
Expand Down Expand Up @@ -160,7 +163,7 @@ impl Display for AccountState {
}
}

impl From<AccountState> for AccountTransactionInputRecord {
impl From<AccountState> for proto::responses::AccountTransactionInputRecord {
fn from(from: AccountState) -> Self {
Self {
account_id: Some(from.account_id.into()),
Expand All @@ -169,7 +172,7 @@ impl From<AccountState> for AccountTransactionInputRecord {
}
}

impl From<AccountHeader> for proto::AccountHeader {
impl From<AccountHeader> for proto::account::AccountHeader {
fn from(from: AccountHeader) -> Self {
Self {
vault_root: Some(from.vault_root().into()),
Expand All @@ -180,18 +183,24 @@ impl From<AccountHeader> for proto::AccountHeader {
}
}

impl TryFrom<AccountTransactionInputRecord> for AccountState {
impl TryFrom<proto::responses::AccountTransactionInputRecord> for AccountState {
type Error = ConversionError;

fn try_from(from: AccountTransactionInputRecord) -> Result<Self, Self::Error> {
fn try_from(
from: proto::responses::AccountTransactionInputRecord,
) -> Result<Self, Self::Error> {
let account_id = from
.account_id
.ok_or(AccountTransactionInputRecord::missing_field(stringify!(account_id)))?
.ok_or(proto::responses::AccountTransactionInputRecord::missing_field(stringify!(
account_id
)))?
.try_into()?;

let account_hash = from
.account_hash
.ok_or(AccountTransactionInputRecord::missing_field(stringify!(account_hash)))?
.ok_or(proto::responses::AccountTransactionInputRecord::missing_field(stringify!(
account_hash
)))?
.try_into()?;

// If the hash is equal to `Digest::default()`, it signifies that this is a new account
Expand Down
14 changes: 7 additions & 7 deletions crates/proto/src/domain/digest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,23 +207,23 @@ mod test {
use hex::{FromHex, ToHex};
use proptest::prelude::*;

use crate::generated::digest::Digest;
use crate::generated::digest as proto;

#[test]
fn hex_digest() {
let digest = Digest {
let digest = proto::Digest {
d0: 3488802789098113751,
d1: 5271242459988994564,
d2: 17816570245237064784,
d3: 10910963388447438895,
};
let encoded: String = ToHex::encode_hex(&digest);
let round_trip: Result<Digest, _> = FromHex::from_hex::<&[u8]>(encoded.as_ref());
let round_trip: Result<proto::Digest, _> = FromHex::from_hex::<&[u8]>(encoded.as_ref());
assert_eq!(digest, round_trip.unwrap());

let digest = Digest { d0: 0, d1: 0, d2: 0, d3: 0 };
let digest = proto::Digest { d0: 0, d1: 0, d2: 0, d3: 0 };
let encoded: String = ToHex::encode_hex(&digest);
let round_trip: Result<Digest, _> = FromHex::from_hex::<&[u8]>(encoded.as_ref());
let round_trip: Result<proto::Digest, _> = FromHex::from_hex::<&[u8]>(encoded.as_ref());
assert_eq!(digest, round_trip.unwrap());
}

Expand All @@ -235,9 +235,9 @@ mod test {
d2: u64,
d3: u64,
) {
let digest = Digest { d0, d1, d2, d3 };
let digest = proto::Digest { d0, d1, d2, d3 };
let encoded: String = ToHex::encode_hex(&digest);
let round_trip: Result<Digest, _> = FromHex::from_hex::<&[u8]>(encoded.as_ref());
let round_trip: Result<proto::Digest, _> = FromHex::from_hex::<&[u8]>(encoded.as_ref());
assert_eq!(digest, round_trip.unwrap());
}
}
Expand Down
62 changes: 31 additions & 31 deletions crates/proto/src/domain/merkle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,47 @@ 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()
}
}

// MMR DELTA
// ================================================================================================

impl From<MmrDelta> for generated::mmr::MmrDelta {
impl From<MmrDelta> for proto::mmr::MmrDelta {
fn from(value: MmrDelta) -> Self {
let data = value.data.into_iter().map(generated::digest::Digest::from).collect();
generated::mmr::MmrDelta { forest: value.forest as u64, data }
let data = value.data.into_iter().map(proto::digest::Digest::from).collect();
proto::mmr::MmrDelta { forest: value.forest as u64, data }
}
}

impl TryFrom<generated::mmr::MmrDelta> for MmrDelta {
impl TryFrom<proto::mmr::MmrDelta> for MmrDelta {
type Error = ConversionError;

fn try_from(value: generated::mmr::MmrDelta) -> Result<Self, Self::Error> {
fn try_from(value: proto::mmr::MmrDelta) -> Result<Self, Self::Error> {
let data: Result<Vec<_>, ConversionError> =
value.data.into_iter().map(Digest::try_from).collect();

Expand All @@ -63,22 +63,22 @@ impl TryFrom<generated::mmr::MmrDelta> for MmrDelta {
// SMT LEAF
// ------------------------------------------------------------------------------------------------

impl TryFrom<generated::smt::SmtLeaf> for SmtLeaf {
impl TryFrom<proto::smt::SmtLeaf> for SmtLeaf {
type Error = ConversionError;

fn try_from(value: generated::smt::SmtLeaf) -> Result<Self, Self::Error> {
let leaf = value.leaf.ok_or(generated::smt::SmtLeaf::missing_field(stringify!(leaf)))?;
fn try_from(value: proto::smt::SmtLeaf) -> Result<Self, Self::Error> {
let leaf = value.leaf.ok_or(proto::smt::SmtLeaf::missing_field(stringify!(leaf)))?;

match leaf {
generated::smt::smt_leaf::Leaf::Empty(leaf_index) => {
proto::smt::smt_leaf::Leaf::Empty(leaf_index) => {
Ok(Self::new_empty(LeafIndex::new_max_depth(leaf_index)))
},
generated::smt::smt_leaf::Leaf::Single(entry) => {
proto::smt::smt_leaf::Leaf::Single(entry) => {
let (key, value): (Digest, Word) = entry.try_into()?;

Ok(SmtLeaf::new_single(key, value))
},
generated::smt::smt_leaf::Leaf::Multiple(entries) => {
proto::smt::smt_leaf::Leaf::Multiple(entries) => {
let domain_entries: Vec<(Digest, Word)> = try_convert(entries.entries)?;

Ok(SmtLeaf::new_multiple(domain_entries)?)
Expand All @@ -87,15 +87,15 @@ impl TryFrom<generated::smt::SmtLeaf> for SmtLeaf {
}
}

impl From<SmtLeaf> for generated::smt::SmtLeaf {
impl From<SmtLeaf> for proto::smt::SmtLeaf {
fn from(smt_leaf: SmtLeaf) -> Self {
use generated::smt::smt_leaf::Leaf;
use proto::smt::smt_leaf::Leaf;

let leaf = match smt_leaf {
SmtLeaf::Empty(leaf_index) => Leaf::Empty(leaf_index.value()),
SmtLeaf::Single(entry) => Leaf::Single(entry.into()),
SmtLeaf::Multiple(entries) => {
Leaf::Multiple(generated::smt::SmtLeafEntries { entries: convert(entries) })
Leaf::Multiple(proto::smt::SmtLeafEntries { entries: convert(entries) })
},
};

Expand All @@ -106,24 +106,24 @@ impl From<SmtLeaf> for generated::smt::SmtLeaf {
// SMT LEAF ENTRY
// ------------------------------------------------------------------------------------------------

impl TryFrom<generated::smt::SmtLeafEntry> for (Digest, Word) {
impl TryFrom<proto::smt::SmtLeafEntry> for (Digest, Word) {
type Error = ConversionError;

fn try_from(entry: generated::smt::SmtLeafEntry) -> Result<Self, Self::Error> {
fn try_from(entry: proto::smt::SmtLeafEntry) -> Result<Self, Self::Error> {
let key: Digest = entry
.key
.ok_or(generated::smt::SmtLeafEntry::missing_field(stringify!(key)))?
.ok_or(proto::smt::SmtLeafEntry::missing_field(stringify!(key)))?
.try_into()?;
let value: Word = entry
.value
.ok_or(generated::smt::SmtLeafEntry::missing_field(stringify!(value)))?
.ok_or(proto::smt::SmtLeafEntry::missing_field(stringify!(value)))?
.try_into()?;

Ok((key, value))
}
}

impl From<(Digest, Word)> for generated::smt::SmtLeafEntry {
impl From<(Digest, Word)> for proto::smt::SmtLeafEntry {
fn from((key, value): (Digest, Word)) -> Self {
Self {
key: Some(key.into()),
Expand All @@ -135,25 +135,25 @@ impl From<(Digest, Word)> for generated::smt::SmtLeafEntry {
// SMT PROOF
// ------------------------------------------------------------------------------------------------

impl TryFrom<generated::smt::SmtOpening> for SmtProof {
impl TryFrom<proto::smt::SmtOpening> for SmtProof {
type Error = ConversionError;

fn try_from(opening: generated::smt::SmtOpening) -> Result<Self, Self::Error> {
fn try_from(opening: proto::smt::SmtOpening) -> Result<Self, Self::Error> {
let path: MerklePath = opening
.path
.as_ref()
.ok_or(generated::smt::SmtOpening::missing_field(stringify!(path)))?
.ok_or(proto::smt::SmtOpening::missing_field(stringify!(path)))?
.try_into()?;
let leaf: SmtLeaf = opening
.leaf
.ok_or(generated::smt::SmtOpening::missing_field(stringify!(leaf)))?
.ok_or(proto::smt::SmtOpening::missing_field(stringify!(leaf)))?
.try_into()?;

Ok(SmtProof::new(path, leaf)?)
}
}

impl From<SmtProof> for generated::smt::SmtOpening {
impl From<SmtProof> for proto::smt::SmtOpening {
fn from(proof: SmtProof) -> Self {
let (ref path, leaf) = proof.into_parts();
Self {
Expand Down
Loading

0 comments on commit ecacf1c

Please sign in to comment.