Skip to content

Commit

Permalink
Fix: ignore missing null values when comparing PIEs
Browse files Browse the repository at this point in the history
Problem: the new implementation of `CairoPie` using the
`CairoPieAdditionalData` struct makes it hard to reproduce the exact
same behaviour as `cairo-lang` when handling builtins with no data.
While `cairo-lang` will generate a null value and include it in the JSON
file, we can only (easily) generate a null value for each builtin or for
none of them.

Solution: make the comparator script more flexible by filtering out null
values from JSON contents.
  • Loading branch information
odesenfans committed Apr 7, 2024
1 parent fdb955b commit 7773780
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 21 additions & 2 deletions vm/src/tests/cairo_pie_comparator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import sys
import json
from typing import Any
from zipfile import ZipFile
import memory_comparator

Expand All @@ -10,18 +11,36 @@

json_files = ["version.json", "metadata.json", "execution_resources.json", "additional_data.json"]


def filter_null_values(content: Any) -> Any:
if isinstance(content, dict):
return {k: v for k, v in content.items() if v is not None}

return content


def json_contents_are_equivalent(cl: Any, cv: Any) -> bool:
"""
Compares the two JSON contents. and returns whether they are equivalent.
Contents are considered equivalent if they are equal or if some keys have null values in one
content and are just missing from the other.
"""

return filter_null_values(cl) == filter_null_values(cv)


with ZipFile(filename1) as cairo_lang_pie_zip, ZipFile(filename2) as cairo_vm_pie_zip:
# Compare json files
for file in json_files:
with cairo_lang_pie_zip.open(file) as cairo_lang_file, cairo_vm_pie_zip.open(file) as cairo_vm_file:
cl_content = json.load(cairo_lang_file)
cv_content = json.load(cairo_vm_file)
if cl_content != cv_content:
if not json_contents_are_equivalent(cl_content, cv_content):
print(f"Comparison unsuccesful for {filename1}/{file} vs {filename2}/{file}")
exit(1)

# Compare binary files
with cairo_lang_pie_zip.open("memory.bin", 'r') as f1, cairo_vm_pie_zip.open("memory.bin", 'r') as f2:
with cairo_lang_pie_zip.open("memory.bin", 'r') as f1, cairo_vm_pie_zip.open("memory.bin", 'r') as f2:
memory_comparator.compare_memory_file_contents(f1.read(), f2.read())

print(f"Comparison succesful for {filename1} vs {filename2}")

0 comments on commit 7773780

Please sign in to comment.