Skip to content

Commit 3f9428e

Browse files
Alon-Tipefontana
andauthored
Fixed CairoPie output builtin data serde. (#1781)
Co-authored-by: Pedro Fontana <fontana.pedro93@gmail.com>
1 parent e941395 commit 3f9428e

File tree

3 files changed

+60
-7
lines changed

3 files changed

+60
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#### Upcoming Changes
44

5+
* fix: Change (de)serialization of CairoPie's `OutputBuiltinAdditionalData`'s `PublicMemoryPage` to vectors of length 2. [#1781](https://github.com/lambdaclass/cairo-vm/pull/1781)
6+
57
* fix: Fixed deserialization issue when signature additional data is empty, and the name of the builtin range_check96 [#1785](https://github.com/lambdaclass/cairo-vm/pull/1785)
68

79
* refactor + bugfix: Improve arg handling for cairo1-run [#1782](https://github.com/lambdaclass/cairo-vm/pull/1782)

Cargo.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vm/src/vm/runners/cairo_pie.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,22 @@ pub struct PublicMemoryPage {
6262
pub size: usize,
6363
}
6464

65+
impl From<&Vec<usize>> for PublicMemoryPage {
66+
fn from(vec: &Vec<usize>) -> Self {
67+
Self {
68+
start: vec[0],
69+
size: vec[1],
70+
}
71+
}
72+
}
73+
6574
// HashMap value based on starknet/core/os/output.cairo usage
6675
pub type Attributes = HashMap<String, Vec<usize>>;
6776
pub type Pages = HashMap<usize, PublicMemoryPage>;
6877

6978
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
7079
pub struct OutputBuiltinAdditionalData {
80+
#[serde(with = "serde_impl::pages")]
7181
pub pages: Pages,
7282
pub attributes: Attributes,
7383
}
@@ -365,7 +375,7 @@ pub(super) mod serde_impl {
365375
use num_traits::Num;
366376

367377
use super::CAIRO_PIE_VERSION;
368-
use super::{CairoPieMemory, SegmentInfo};
378+
use super::{CairoPieMemory, Pages, PublicMemoryPage, SegmentInfo};
369379
#[cfg(any(target_arch = "wasm32", no_std, not(feature = "std")))]
370380
use crate::alloc::string::ToString;
371381
use crate::stdlib::prelude::{String, Vec};
@@ -537,6 +547,47 @@ pub(super) mod serde_impl {
537547
serializer.serialize_str(&string)
538548
}
539549

550+
pub mod pages {
551+
use super::*;
552+
553+
pub fn serialize<S>(pages: &Pages, serializer: S) -> Result<S::Ok, S::Error>
554+
where
555+
S: Serializer,
556+
{
557+
let mut map = serializer.serialize_map(Some(pages.len()))?;
558+
for (k, v) in pages {
559+
map.serialize_entry(&k.to_string(), &vec![v.start, v.size])?;
560+
}
561+
map.end()
562+
}
563+
564+
pub fn deserialize<'de, D>(deserializer: D) -> Result<Pages, D::Error>
565+
where
566+
D: Deserializer<'de>,
567+
{
568+
Ok(HashMap::<String, Vec<usize>>::deserialize(deserializer)?
569+
.iter()
570+
.map(|(k, v)| {
571+
if v.len() == 2 {
572+
Ok((
573+
k.parse::<usize>().map_err(|_| {
574+
D::Error::custom("Failed to deserialize page index.")
575+
})?,
576+
PublicMemoryPage::from(v),
577+
))
578+
} else {
579+
Err(D::Error::custom(
580+
"Memory page description must be of length 2.",
581+
))
582+
}
583+
})
584+
.collect::<Result<Vec<_>, _>>()
585+
.map_err(|_| D::Error::custom("PublicMemoryPage deserialization failed."))?
586+
.into_iter()
587+
.collect::<Pages>())
588+
}
589+
}
590+
540591
impl CairoPieMemory {
541592
pub fn to_bytes(&self) -> Vec<u8> {
542593
// Missing segment and memory holes can be ignored

0 commit comments

Comments
 (0)