Skip to content

Commit

Permalink
feat(objects): Implement Serializable for FungibleAsset
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippGackstatter committed Oct 7, 2024
1 parent 9dc06d7 commit 89d6964
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
9 changes: 8 additions & 1 deletion objects/src/accounts/delta/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use alloc::{
vec::Vec,
};

use vm_core::{Felt, FieldElement};

use super::{
AccountDeltaError, ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable,
};
Expand Down Expand Up @@ -293,7 +295,12 @@ impl Serializable for FungibleAssetDelta {
}

fn get_size_hint(&self) -> usize {
self.0.len().get_size_hint() + self.0.len() * FungibleAsset::SERIALIZED_SIZE
// The serialized size of a fungible asset as it is serialized here.
// Usually, it is serialized including zeroes which is less efficient - see its Serializable
// implementation.
const SERIALIZED_FUNGIBLE_ASSET_COMPACT_SIZE: usize =
Felt::ELEMENT_BYTES + core::mem::size_of::<u64>();
self.0.len().get_size_hint() + self.0.len() * SERIALIZED_FUNGIBLE_ASSET_COMPACT_SIZE
}
}

Expand Down
40 changes: 33 additions & 7 deletions objects/src/assets/fungible.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use alloc::string::ToString;
use core::fmt;

use vm_core::FieldElement;
use vm_core::utils::{ByteReader, ByteWriter, Deserializable, Serializable};
use vm_processor::DeserializationError;

use super::{
is_not_a_non_fungible_asset, parse_word, AccountId, AccountType, Asset, AssetError, Felt, Word,
Expand All @@ -28,8 +29,8 @@ impl FungibleAsset {

/// The serialized size of a [`FungibleAsset`] in bytes.
///
/// Currently an account id (felt) plus an amount (u64).
pub const SERIALIZED_SIZE: usize = Felt::ELEMENT_BYTES + core::mem::size_of::<u64>();
/// Currently an account id (felt) plus an amount (u64) padded to a word with zeroes.
pub const SERIALIZED_SIZE: usize = 32;

// CONSTRUCTOR
// --------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -145,16 +146,22 @@ impl From<FungibleAsset> for Word {
}
}

impl From<FungibleAsset> for [u8; 32] {
impl From<FungibleAsset> for [u8; FungibleAsset::SERIALIZED_SIZE] {
fn from(asset: FungibleAsset) -> Self {
let mut result = [0_u8; 32];
let mut result = [0_u8; FungibleAsset::SERIALIZED_SIZE];
let id_bytes: [u8; 8] = asset.faucet_id.into();
result[..8].copy_from_slice(&asset.amount.to_le_bytes());
result[24..].copy_from_slice(&id_bytes);
result
}
}

impl From<&FungibleAsset> for [u8; FungibleAsset::SERIALIZED_SIZE] {
fn from(value: &FungibleAsset) -> Self {
(*value).into()
}
}

impl From<FungibleAsset> for Asset {
fn from(asset: FungibleAsset) -> Self {
Asset::Fungible(asset)
Expand All @@ -175,10 +182,10 @@ impl TryFrom<Word> for FungibleAsset {
}
}

impl TryFrom<[u8; 32]> for FungibleAsset {
impl TryFrom<[u8; FungibleAsset::SERIALIZED_SIZE]> for FungibleAsset {
type Error = AssetError;

fn try_from(value: [u8; 32]) -> Result<Self, Self::Error> {
fn try_from(value: [u8; FungibleAsset::SERIALIZED_SIZE]) -> Result<Self, Self::Error> {
let word = parse_word(value)?;
Self::try_from(word)
}
Expand All @@ -189,3 +196,22 @@ impl fmt::Display for FungibleAsset {
write!(f, "{:?}", self)
}
}

impl Serializable for FungibleAsset {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
let data: [u8; FungibleAsset::SERIALIZED_SIZE] = self.into();
target.write_bytes(&data);
}

fn get_size_hint(&self) -> usize {
FungibleAsset::SERIALIZED_SIZE
}
}

impl Deserializable for FungibleAsset {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let value: Word = source.read()?;

Self::try_from(value).map_err(|err| DeserializationError::InvalidValue(err.to_string()))
}
}

0 comments on commit 89d6964

Please sign in to comment.