Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dict manager #118

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
28c78f9
Add dict_manager
Juan-M-V Nov 4, 2022
e05eeb8
Create new structs
Juan-M-V Nov 10, 2022
389f73e
Merge memory segments
Juan-M-V Nov 10, 2022
6094622
Merge branch 'main' of github.com:lambdaclass/cairo-rs-py into dict-m…
Juan-M-V Nov 10, 2022
dc8d66d
Expose vm
Juan-M-V Nov 10, 2022
abfc657
Add Dict tracker and Dict
Juan-M-V Nov 14, 2022
75fbc8f
Modify structs
Juan-M-V Nov 14, 2022
0f781b4
Solve merge conflicts
Juan-M-V Nov 14, 2022
4ba36d0
Fix dereferencing ids
Juan-M-V Nov 14, 2022
8250fe8
Uncomment tests
Juan-M-V Nov 14, 2022
b568348
Fix tests
Juan-M-V Nov 14, 2022
7d31599
Clean up and modify refereces
Juan-M-V Nov 15, 2022
75c6e43
Remove todo
Juan-M-V Nov 15, 2022
c9d393f
Merge branch 'main' of github.com:lambdaclass/cairo-rs-py into dict-m…
Juan-M-V Nov 15, 2022
0d22b0b
Improve asterix stripping
Juan-M-V Nov 15, 2022
94065d3
Make fields private
Juan-M-V Nov 15, 2022
5c1dfac
Ignore clippy warnings
Juan-M-V Nov 15, 2022
f79bc1e
Fix clippy warnings
Juan-M-V Nov 16, 2022
0c6e440
Update hints_tests.py
Juan-M-V Nov 16, 2022
e034b03
Merge with main and fix errors
Juan-M-V Nov 17, 2022
525c200
Uncomment test
Juan-M-V Nov 17, 2022
255bc69
Merge branch 'main' of github.com:lambdaclass/cairo-rs-py into dict-m…
Juan-M-V Nov 18, 2022
f10e775
Add unit tests for Dict manager (#148)
MegaRedHand Nov 18, 2022
6128817
Add tests for the failing cases of PyDictManager (#151)
MegaRedHand Nov 23, 2022
2ba1024
Merge branch 'main' into dict-manager
Juan-M-V Nov 30, 2022
f20ed1a
Merge branch 'main' into dict-manager
Juan-M-V Dec 2, 2022
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
6 changes: 3 additions & 3 deletions hints_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ def test_program(program_name: str):
test_program("memcpy")
test_program("memset")
test_program("dict_new")
# test_program("dict_read") # Waiting on starkware PR
# test_program("dict_write") # ValueError: Custom Hint Error: AttributeError: 'PyTypeId' object has no attribute 'segment_index'
# test_program("dict_update") # Waiting on starkware PR
test_program("dict_read")
test_program("dict_write")
test_program("dict_update")
test_program("default_dict_new")
# test_program("squash_dict") # ValueError: Custom Hint Error: ValueError: Failed to get ids value
# test_program("dict_squash") # Custom Hint Error: AttributeError: 'PyTypeId' object has no attribute 'segment_index'
Expand Down
25 changes: 16 additions & 9 deletions src/cairo_run.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[cfg(test)]
mod test {
use crate::cairo_runner::PyCairoRunner;
use pyo3::Python;
use std::fs;

#[test]
Expand All @@ -9,9 +10,11 @@ mod test {
let program = fs::read_to_string(path).unwrap();
let mut runner =
PyCairoRunner::new(program, Some("main".to_string()), None, false).unwrap();
runner
.cairo_run_py(false, None, None, None, None, None)
.expect("Couldn't run program");
Python::with_gil(|py| {
runner
.cairo_run_py(false, None, None, None, py, None, None)
.expect("Couldn't run program");
});
}

#[test]
Expand All @@ -25,9 +28,11 @@ mod test {
false,
)
.unwrap();
runner
.cairo_run_py(false, None, None, None, None, None)
.expect("Couldn't run program");
Python::with_gil(|py| {
runner
.cairo_run_py(false, None, None, None, py, None, None)
.expect("Couldn't run program");
});
}

#[test]
Expand All @@ -36,8 +41,10 @@ mod test {
let program = fs::read_to_string(path).unwrap();
let mut runner =
PyCairoRunner::new(program, Some("main".to_string()), None, false).unwrap();
runner
.cairo_run_py(false, None, None, None, None, None)
.expect("Couldn't run program");
Python::with_gil(|py| {
runner
.cairo_run_py(false, None, None, None, py, None, None)
.expect("Couldn't run program");
});
}
}
179 changes: 105 additions & 74 deletions src/cairo_runner.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
dict_manager::PyDictManager,
relocatable::{PyMaybeRelocatable, PyRelocatable},
utils::to_py_error,
vm_core::PyVM,
Expand Down Expand Up @@ -77,12 +78,14 @@ impl PyCairoRunner {
}

#[pyo3(name = "cairo_run")]
#[allow(clippy::too_many_arguments)]
pub fn cairo_run_py(
&mut self,
print_output: bool,
trace_file: Option<&str>,
memory_file: Option<&str>,
hint_locals: Option<HashMap<String, PyObject>>,
py: Python,
static_locals: Option<HashMap<String, PyObject>>,
entrypoint: Option<&str>,
) -> PyResult<()> {
Expand All @@ -97,6 +100,10 @@ impl PyCairoRunner {
if let Some(locals) = hint_locals {
self.hint_locals = locals
}
self.hint_locals.insert(
"__dict_manager".to_string(),
PyDictManager::new().into_py(py),
);
Comment on lines +107 to +110
Copy link
Member

Choose a reason for hiding this comment

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

I'd much rather take the GIL only here.


self.pyvm.static_locals = static_locals;

Expand Down Expand Up @@ -651,9 +658,11 @@ mod test {
)
.unwrap();

runner
.cairo_run_py(false, None, None, None, None, None)
.unwrap();
Python::with_gil(|py| {
runner
.cairo_run_py(false, None, None, None, py, None, None)
.unwrap();
});
let new_segment = runner.add_segment();
assert_eq!(
new_segment,
Expand Down Expand Up @@ -684,9 +693,11 @@ mod test {
)
.unwrap();

runner
.cairo_run_py(false, None, None, None, None, None)
.unwrap();
Python::with_gil(|py| {
runner
.cairo_run_py(false, None, None, None, py, None, None)
.unwrap();
});

let expected_output: Vec<PyMaybeRelocatable> = vec![RelocatableValue(PyRelocatable {
segment_index: 2,
Expand Down Expand Up @@ -716,22 +727,22 @@ mod test {
)
.unwrap();

runner
.cairo_run_py(false, None, None, None, None, None)
.unwrap();
Python::with_gil(|py| {
runner
.cairo_run_py(false, None, None, None, py, None, None)
.unwrap();

let expected_output: Vec<PyMaybeRelocatable> = vec![
RelocatableValue(PyRelocatable {
segment_index: 2,
offset: 0,
}),
RelocatableValue(PyRelocatable {
segment_index: 3,
offset: 0,
}),
];
let expected_output: Vec<PyMaybeRelocatable> = vec![
RelocatableValue(PyRelocatable {
segment_index: 2,
offset: 0,
}),
RelocatableValue(PyRelocatable {
segment_index: 3,
offset: 0,
}),
];

Python::with_gil(|py| {
assert_eq!(
runner
.get_program_builtins_initial_stack(py)
Expand All @@ -754,9 +765,11 @@ mod test {
)
.unwrap();

runner
.cairo_run_py(false, None, None, None, None, None)
.unwrap();
Python::with_gil(|py| {
runner
.cairo_run_py(false, None, None, None, py, None, None)
.unwrap();
});

let expected_output = PyRelocatable::from((1, 8));

Expand All @@ -778,9 +791,11 @@ mod test {
false,
)
.unwrap();
runner
.cairo_run_py(false, None, None, None, None, Some("main"))
.unwrap();
Python::with_gil(|py| {
runner
.cairo_run_py(false, None, None, None, py, None, Some("main"))
.unwrap();
});
// Make a copy of the builtin in order to insert a second "fake" one
// BuiltinRunner api is private, so we can create a new one for this test
let fake_builtin = (*runner.pyvm.vm).borrow_mut().get_builtin_runners_as_mut()[0]
Expand Down Expand Up @@ -821,9 +836,11 @@ mod test {
)
.unwrap();

runner
.cairo_run_py(false, None, None, None, None, None)
.unwrap();
Python::with_gil(|py| {
runner
.cairo_run_py(false, None, None, None, py, None, None)
.unwrap();
});

let expected_output = PyRelocatable::from((1, 0));

Expand All @@ -845,9 +862,11 @@ mod test {
)
.unwrap();

runner
.cairo_run_py(false, None, None, None, None, None)
.unwrap();
Python::with_gil(|py| {
runner
.cairo_run_py(false, None, None, None, py, None, None)
.unwrap();
});

// Make a copy of the builtin in order to insert a second "fake" one
// BuiltinRunner api is private, so we can create a new one for this test
Expand Down Expand Up @@ -883,9 +902,11 @@ mod test {
)
.unwrap();

runner
.cairo_run_py(false, None, None, None, None, None)
.unwrap();
Python::with_gil(|py| {
runner
.cairo_run_py(false, None, None, None, py, None, None)
.unwrap();
});

assert_eq!(runner.pyvm.vm.borrow().get_ap(), Relocatable::from((1, 41)));
assert_eq!(
Expand Down Expand Up @@ -924,10 +945,10 @@ mod test {
let program = fs::read_to_string(path).unwrap();
let mut runner =
PyCairoRunner::new(program, Some("main".to_string()), None, false).unwrap();
runner
.cairo_run_py(false, None, None, None, None, None)
.unwrap();
Python::with_gil(|py| {
runner
.cairo_run_py(false, None, None, None, py, None, None)
.unwrap();
assert_eq!(
24,
runner
Expand All @@ -945,10 +966,10 @@ mod test {
let program = fs::read_to_string(path).unwrap();
let mut runner =
PyCairoRunner::new(program, Some("main".to_string()), None, false).unwrap();
runner
.cairo_run_py(false, None, None, None, None, None)
.unwrap();
Python::with_gil(|py| {
runner
.cairo_run_py(false, None, None, None, py, None, None)
.unwrap();
assert_eq!(
0,
runner
Expand Down Expand Up @@ -1161,9 +1182,11 @@ mod test {
false,
)
.unwrap();
runner
.cairo_run_py(false, None, None, None, None, None)
.unwrap();
Python::with_gil(|py| {
runner
.cairo_run_py(false, None, None, None, py, None, None)
.unwrap();
});
assert_eq! {
PyRelocatable::from((1,2)),
runner.get_initial_fp().unwrap()
Expand Down Expand Up @@ -1370,19 +1393,22 @@ mod test {
false,
)
.unwrap();
assert!(runner
.cairo_run_py(
false,
None,
None,
None,
Some(HashMap::from([(
"__find_element_max_size".to_string(),
Python::with_gil(|py| -> PyObject { 100.to_object(py) }),
)])),
None,
)
.is_ok());
Python::with_gil(|py| {
assert!(runner
.cairo_run_py(
false,
None,
None,
None,
py,
Some(HashMap::from([(
"__find_element_max_size".to_string(),
100.to_object(py)
),])),
None
)
.is_ok());
});
}

#[test]
Expand All @@ -1396,19 +1422,22 @@ mod test {
false,
)
.unwrap();
assert!(runner
.cairo_run_py(
false,
None,
None,
None,
Some(HashMap::from([(
"__find_element_max_size".to_string(),
Python::with_gil(|py| -> PyObject { 1.to_object(py) }),
)])),
None
)
.is_err());
Python::with_gil(|py| {
assert!(runner
.cairo_run_py(
false,
None,
None,
None,
py,
Some(HashMap::from([(
"__find_element_max_size".to_string(),
1.to_object(py)
),])),
None
)
.is_err());
});
}

#[test]
Expand All @@ -1418,9 +1447,11 @@ mod test {
let mut runner =
PyCairoRunner::new(program, None, Some("small".to_string()), false).unwrap();

runner
.cairo_run_py(false, None, None, None, None, Some("main"))
.expect("Call to PyCairoRunner::cairo_run_py() failed.");
Python::with_gil(|py| {
runner
.cairo_run_py(false, None, None, None, py, None, Some("main"))
.expect("Call to PyCairoRunner::cairo_run_py() failed.");
});
}

/// Test that `PyCairoRunner::get()` works as intended.
Expand All @@ -1437,7 +1468,7 @@ mod test {
.unwrap();

runner
.cairo_run_py(false, None, None, None, None, None)
.cairo_run_py(false, None, None, None, py, None, None)
.expect("Call to PyCairoRunner::cairo_run_py");

let mut ap = runner.get_ap().unwrap();
Expand Down
Loading