Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ use cairo_vm::{
types::relocatable::{MaybeRelocatable, Relocatable},
vm::vm_core::VirtualMachine,
};
use conversions::{
IntoConv,
serde::serialize::{SerializeToFeltVec, raw::RawFeltVec},
};
use conversions::serde::SerializedValue;
use conversions::{IntoConv, serde::serialize::SerializeToFeltVec};
use starknet_types_core::felt::Felt;

fn get_cheated_block_info_ptr(
Expand Down Expand Up @@ -90,8 +88,10 @@ fn get_cheated_tx_info_ptr(
new_tx_info[7] = MaybeRelocatable::Int(nonce);
}
if let Some(resource_bounds) = resource_bounds {
let (resource_bounds_start_ptr, resource_bounds_end_ptr) =
add_vec_memory_segment(&RawFeltVec::new(resource_bounds).serialize_to_vec(), vm);
let (resource_bounds_start_ptr, resource_bounds_end_ptr) = add_vec_memory_segment(
&SerializedValue::new(resource_bounds).serialize_to_vec(),
vm,
);
new_tx_info[8] = resource_bounds_start_ptr.into();
new_tx_info[9] = resource_bounds_end_ptr.into();
}
Expand Down
3 changes: 3 additions & 0 deletions crates/conversions/src/serde.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
pub mod deserialize;
pub mod serialize;
pub mod serialized_value;

pub use serialized_value::SerializedValue;
15 changes: 14 additions & 1 deletion crates/conversions/src/serde/deserialize/deserialize_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ where
{
fn deserialize(reader: &mut BufferReader<'_>) -> BufferReadResult<Self> {
let variant: Felt = reader.read()?;
let variant: usize = variant.to_usize().ok_or(BufferReadError::ParseFailed)?;
let variant = variant.to_usize().ok_or(BufferReadError::ParseFailed)?;

match variant {
0 => Ok(Some(reader.read()?)),
Expand All @@ -53,6 +53,19 @@ where
}
}

impl<T: CairoDeserialize, E: CairoDeserialize> CairoDeserialize for Result<T, E> {
fn deserialize(reader: &mut BufferReader<'_>) -> BufferReadResult<Self> {
let variant: Felt = reader.read()?;
let variant = variant.to_usize().ok_or(BufferReadError::ParseFailed)?;

match variant {
0 => Ok(Ok(reader.read()?)),
1 => Ok(Err(reader.read()?)),
_ => Err(BufferReadError::ParseFailed),
}
}
}

impl CairoDeserialize for bool {
fn deserialize(reader: &mut BufferReader<'_>) -> BufferReadResult<Self> {
let num: usize = reader.read()?;
Expand Down
2 changes: 0 additions & 2 deletions crates/conversions/src/serde/lib.rs

This file was deleted.

1 change: 0 additions & 1 deletion crates/conversions/src/serde/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use starknet_types_core::felt::Felt;

pub use cairo_serde_macros::CairoSerialize;

pub mod raw;
mod serialize_impl;

pub struct BufferWriter {
Expand Down
28 changes: 0 additions & 28 deletions crates/conversions/src/serde/serialize/raw.rs

This file was deleted.

42 changes: 42 additions & 0 deletions crates/conversions/src/serde/serialized_value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use conversions::serde::deserialize::{BufferReadResult, BufferReader, CairoDeserialize};
use conversions::serde::serialize::{BufferWriter, CairoSerialize};
use starknet_types_core::felt::Felt;

/// Represents an already serialized Vec of values.
///
/// Use this to omit adding extra felt for the length of the vector during serialization.
#[derive(Debug)]
pub struct SerializedValue<T>(pub Vec<T>)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also drop generic. Only valid representation i would say is pub struct SerializedValue(pub Vec<Felt>)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's one use case where we want to write serialized array of ResourceBounds structs into memory in serialized format and we do it as so

&SerializedValue::new(resource_bounds).serialize_to_vec(),

We can change serialized value to be a Vec<Felt> but we would either have to provide an constructor that allows creating it from other values that implement serialize (we would have to do the serialization at construction time though) or change the usage to something along the lines of

&resource_bounds
                .iter()
                .flat_map(conversions::serde::serialize::SerializeToFeltVec::serialize_to_vec)
                .collect::<Vec<_>>(),

which kind of duplicates the logic.

where
T: CairoSerialize;

impl<T> SerializedValue<T>
where
T: CairoSerialize,
{
#[must_use]
pub fn new(vec: Vec<T>) -> Self {
Self(vec)
}
}

impl<T> CairoSerialize for SerializedValue<T>
where
T: CairoSerialize,
{
fn serialize(&self, output: &mut BufferWriter) {
for e in &self.0 {
e.serialize(output);
}
}
}

impl CairoDeserialize for SerializedValue<Felt> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we need it to .read() the already serialized data that oracle returns.

fn deserialize(reader: &mut BufferReader<'_>) -> BufferReadResult<Self> {
let mut result: Vec<Felt> = Vec::new();
while let Ok(r) = reader.read_felt() {
result.push(r);
}
Ok(Self::new(result))
}
}
4 changes: 2 additions & 2 deletions crates/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ use cairo_vm::vm::errors::vm_errors::VirtualMachineError;
use cairo_vm::vm::runners::cairo_runner::{ResourceTracker, RunResources};
use cairo_vm::vm::vm_core::VirtualMachine;
use conversions::byte_array::ByteArray;
use conversions::serde::SerializedValue;
use conversions::serde::deserialize::BufferReadError;
use conversions::serde::deserialize::BufferReader;
use conversions::serde::serialize::raw::RawFeltVec;
use conversions::serde::serialize::{CairoSerialize, SerializeToFeltVec};
use indoc::indoc;
use shared::vm::VirtualMachineExt;
Expand Down Expand Up @@ -294,7 +294,7 @@ impl<Extension: ExtensionLogic> ExtendedRuntime<Extension> {
return res;
}
// it is serialized again to add `Result` discriminator
Ok(CheatcodeHandlingResult::Handled(res)) => Ok(RawFeltVec::new(res)),
Ok(CheatcodeHandlingResult::Handled(res)) => Ok(SerializedValue::new(res)),
Err(err) => Err(ByteArray::from(err.to_string().as_str())),
}
.serialize_to_vec();
Expand Down
Loading