Skip to content

Commit

Permalink
Integrate RunResources logic into HintProcessor trait (lambdaclas…
Browse files Browse the repository at this point in the history
…s#1274)

* RunResources into context

Signed-off-by: Dori Medini <dori@starkware.co>

* Fix broken code under `cairo-1-hints` feature

* Add changelog entry

---------

Signed-off-by: Dori Medini <dori@starkware.co>
Co-authored-by: Dori Medini <dori@starkware.co>
  • Loading branch information
2 people authored and kariy committed Jul 4, 2023
1 parent f9195e8 commit acf65d1
Show file tree
Hide file tree
Showing 50 changed files with 223 additions and 471 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,14 @@

#### Upcoming Changes

* BREAKING: Integrate `RunResources` logic into `HintProcessor` trait [#1274](https://github.com/lambdaclass/cairo-rs/pull/1274)
* Rename trait `HintProcessor` to `HintProcessorLogic`
* Add trait `ResourceTracker`
* Trait `HintProcessor` is now `HintProcessor: HintProcessorLogic + ResourceTracker`
* `BuiltinHintProcessor::new` & `Cairo1HintProcessor::new` now receive the argumet `run_resources: RunResources`
* `HintProcessorLogic::execute_hint` no longer receives `run_resources: &mut RunResources`
* Remove argument `run_resources: &mut RunResources` from `CairoRunner::run_until_pc` & `CairoRunner::run_from_entrypoint`

* build: remove unused implicit features from cairo-vm

#### [0.6.1] - 2023-6-23
Expand Down
15 changes: 3 additions & 12 deletions hint_accountant/src/main.rs
Expand Up @@ -3,14 +3,11 @@
use cairo_vm::{
hint_processor::{
builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor,
hint_processor_definition::HintProcessor,
hint_processor_definition::HintProcessorLogic,
},
serde::deserialize_program::ApTracking,
types::exec_scope::ExecutionScopes,
vm::{
errors::hint_errors::HintError, runners::cairo_runner::RunResources,
vm_core::VirtualMachine,
},
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
with_std::collections::{HashMap, HashSet},
};
use serde::Deserialize;
Expand Down Expand Up @@ -70,13 +67,7 @@ fn run() {
.compile_hint(h, &ap_tracking_data, &reference_ids, &references)
.expect("this implementation is infallible");
matches!(
hint_executor.execute_hint(
&mut vm,
&mut exec_scopes,
&hint_data,
&constants,
&mut RunResources::default()
),
hint_executor.execute_hint(&mut vm, &mut exec_scopes, &hint_data, &constants,),
Err(HintError::UnknownHint(_)),
)
})
Expand Down
21 changes: 5 additions & 16 deletions vm/src/cairo_run.rs
Expand Up @@ -3,7 +3,7 @@ use crate::{
types::program::Program,
vm::{
errors::{cairo_run_errors::CairoRunError, vm_exception::VmException},
runners::cairo_runner::{CairoRunner, RunResources},
runners::cairo_runner::CairoRunner,
security::verify_secure_runner,
vm_core::VirtualMachine,
},
Expand Down Expand Up @@ -59,10 +59,9 @@ pub fn cairo_run(
let mut vm = VirtualMachine::new(cairo_run_config.trace_enabled);
let end = cairo_runner.initialize(&mut vm)?;
// check step calculation
let mut run_resources = RunResources::default();

cairo_runner
.run_until_pc(end, &mut run_resources, &mut vm, hint_executor)
.run_until_pc(end, &mut vm, hint_executor)
.map_err(|err| VmException::from_vm_error(&cairo_runner, &vm, err))?;
cairo_runner.end_run(false, false, &mut vm, hint_executor)?;

Expand Down Expand Up @@ -156,7 +155,7 @@ mod tests {
.map_err(CairoRunError::Runner)?;

assert!(cairo_runner
.run_until_pc(end, &mut RunResources::default(), &mut vm, hint_processor)
.run_until_pc(end, &mut vm, hint_processor)
.is_ok());

Ok((cairo_runner, vm))
Expand All @@ -176,12 +175,7 @@ mod tests {

let end = cairo_runner.initialize(&mut vm).unwrap();
assert!(cairo_runner
.run_until_pc(
end,
&mut RunResources::default(),
&mut vm,
&mut hint_processor
)
.run_until_pc(end, &mut vm, &mut hint_processor)
.is_ok());
assert!(cairo_runner.relocate(&mut vm, true).is_ok());
// `main` returns without doing nothing, but `not_main` sets `[ap]` to `1`
Expand Down Expand Up @@ -301,12 +295,7 @@ mod tests {
let mut vm = vm!();
let end = cairo_runner.initialize(&mut vm).unwrap();
assert!(cairo_runner
.run_until_pc(
end,
&mut RunResources::default(),
&mut vm,
&mut hint_processor
)
.run_until_pc(end, &mut vm, &mut hint_processor)
.is_ok());
assert!(cairo_runner.relocate(&mut vm, false).is_ok());
assert!(vm.get_relocated_trace().is_err());
Expand Down
4 changes: 1 addition & 3 deletions vm/src/hint_processor/builtin_hint_processor/bigint.rs
Expand Up @@ -100,12 +100,10 @@ mod test {
use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor;
use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;
use crate::hint_processor::builtin_hint_processor::hint_code;
use crate::hint_processor::hint_processor_definition::HintProcessor;
use crate::hint_processor::hint_processor_definition::HintReference;
use crate::hint_processor::hint_processor_definition::{HintProcessorLogic, HintReference};
use crate::stdlib::collections::HashMap;
use crate::types::exec_scope::ExecutionScopes;
use crate::utils::test_utils::*;
use crate::vm::runners::cairo_runner::RunResources;
use crate::vm::vm_core::VirtualMachine;
use assert_matches::assert_matches;
use num_bigint::BigInt;
Expand Down
Expand Up @@ -306,14 +306,13 @@ mod tests {
use super::*;
use crate::hint_processor::builtin_hint_processor::hint_code;
use crate::types::errors::math_errors::MathError;
use crate::vm::runners::cairo_runner::RunResources;
use crate::{
any_box,
hint_processor::{
builtin_hint_processor::builtin_hint_processor_definition::{
BuiltinHintProcessor, HintProcessorData,
},
hint_processor_definition::HintProcessor,
hint_processor_definition::HintProcessorLogic,
},
relocatable,
types::exec_scope::ExecutionScopes,
Expand Down
Expand Up @@ -21,10 +21,13 @@ use super::{
},
};
use crate::{
hint_processor::builtin_hint_processor::secp::ec_utils::{
ec_double_assign_new_x, ec_double_assign_new_x_v2,
hint_processor::{
builtin_hint_processor::secp::ec_utils::{
ec_double_assign_new_x, ec_double_assign_new_x_v2,
},
hint_processor_definition::HintProcessorLogic,
},
vm::runners::cairo_runner::RunResources,
vm::runners::cairo_runner::{ResourceTracker, RunResources},
};
use crate::{
hint_processor::{
Expand Down Expand Up @@ -103,7 +106,7 @@ use crate::{
verify_multiplicity_body, verify_usort,
},
},
hint_processor_definition::{HintProcessor, HintReference},
hint_processor_definition::HintReference,
},
serde::deserialize_program::ApTracking,
stdlib::{any::Any, collections::HashMap, prelude::*, rc::Rc},
Expand Down Expand Up @@ -148,31 +151,35 @@ pub struct HintFunc(
);
pub struct BuiltinHintProcessor {
pub extra_hints: HashMap<String, Rc<HintFunc>>,
run_resources: RunResources,
}
impl BuiltinHintProcessor {
pub fn new_empty() -> Self {
BuiltinHintProcessor {
extra_hints: HashMap::new(),
run_resources: RunResources::default(),
}
}

pub fn new(extra_hints: HashMap<String, Rc<HintFunc>>) -> Self {
BuiltinHintProcessor { extra_hints }
pub fn new(extra_hints: HashMap<String, Rc<HintFunc>>, run_resources: RunResources) -> Self {
BuiltinHintProcessor {
extra_hints,
run_resources,
}
}

pub fn add_hint(&mut self, hint_code: String, hint_func: Rc<HintFunc>) {
self.extra_hints.insert(hint_code, hint_func);
}
}

impl HintProcessor for BuiltinHintProcessor {
impl HintProcessorLogic for BuiltinHintProcessor {
fn execute_hint(
&mut self,
vm: &mut VirtualMachine,
exec_scopes: &mut ExecutionScopes,
hint_data: &Box<dyn Any>,
constants: &HashMap<String, Felt252>,
_run_resources: &mut RunResources,
) -> Result<(), HintError> {
let hint_data = hint_data
.downcast_ref::<HintProcessorData>()
Expand Down Expand Up @@ -805,16 +812,32 @@ impl HintProcessor for BuiltinHintProcessor {
}
}

impl ResourceTracker for BuiltinHintProcessor {
fn consume_step(&mut self) {
self.run_resources.consume_step();
}

fn consumed(&self) -> bool {
self.run_resources.consumed()
}

fn get_n_steps(&self) -> Option<usize> {
self.run_resources.get_n_steps()
}

fn run_resources(&self) -> &RunResources {
&self.run_resources
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::stdlib::any::Any;
use crate::types::relocatable::Relocatable;

use crate::vm::runners::cairo_runner::RunResources;
use crate::{
any_box,
hint_processor::hint_processor_definition::HintProcessor,
types::{exec_scope::ExecutionScopes, relocatable::MaybeRelocatable},
utils::test_utils::*,
vm::{
Expand Down Expand Up @@ -1228,7 +1251,6 @@ mod tests {
exec_scopes,
&any_box!(hint_data),
&HashMap::new(),
&mut RunResources::default(),
),
Ok(())
);
Expand All @@ -1241,7 +1263,6 @@ mod tests {
exec_scopes,
&any_box!(hint_data),
&HashMap::new(),
&mut RunResources::default(),
),
Ok(())
);
Expand Down
Expand Up @@ -368,15 +368,14 @@ pub fn u64_array_to_mayberelocatable_vec(array: &[u64]) -> Vec<MaybeRelocatable>
mod tests {
use super::*;
use crate::stdlib::string::ToString;
use crate::vm::runners::cairo_runner::RunResources;

use crate::{
any_box,
hint_processor::{
builtin_hint_processor::builtin_hint_processor_definition::{
BuiltinHintProcessor, HintProcessorData,
},
hint_processor_definition::{HintProcessor, HintReference},
hint_processor_definition::{HintProcessorLogic, HintReference},
},
types::{exec_scope::ExecutionScopes, relocatable::Relocatable},
utils::test_utils::*,
Expand Down
Expand Up @@ -259,11 +259,10 @@ mod tests {
use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;
use crate::hint_processor::builtin_hint_processor::dict_manager::Dictionary;
use crate::hint_processor::builtin_hint_processor::hint_code;
use crate::hint_processor::hint_processor_definition::HintProcessor;
use crate::hint_processor::hint_processor_definition::HintProcessorLogic;
use crate::stdlib::collections::HashMap;
use crate::types::exec_scope::ExecutionScopes;

use crate::vm::runners::cairo_runner::RunResources;
use crate::{
hint_processor::builtin_hint_processor::dict_manager::{DictManager, DictTracker},
relocatable,
Expand Down
3 changes: 1 addition & 2 deletions vm/src/hint_processor/builtin_hint_processor/ec_recover.rs
Expand Up @@ -118,15 +118,14 @@ mod tests {
use crate::hint_processor::builtin_hint_processor::hint_code;
use crate::hint_processor::hint_processor_definition::HintReference;
use crate::utils::test_utils::*;
use crate::vm::runners::cairo_runner::RunResources;
use crate::vm::vm_core::VirtualMachine;
use crate::{
any_box,
hint_processor::{
builtin_hint_processor::builtin_hint_processor_definition::{
BuiltinHintProcessor, HintProcessorData,
},
hint_processor_definition::HintProcessor,
hint_processor_definition::HintProcessorLogic,
},
types::exec_scope::ExecutionScopes,
};
Expand Down
3 changes: 1 addition & 2 deletions vm/src/hint_processor/builtin_hint_processor/ec_utils.rs
Expand Up @@ -225,15 +225,14 @@ mod tests {
use crate::any_box;
use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor;
use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;
use crate::hint_processor::hint_processor_definition::HintProcessor;
use crate::hint_processor::hint_processor_definition::HintProcessorLogic;
use crate::relocatable;
use crate::types::exec_scope::ExecutionScopes;
use crate::types::relocatable::Relocatable;
use num_traits::Zero;

use crate::hint_processor::builtin_hint_processor::hint_code;
use crate::utils::test_utils::*;
use crate::vm::runners::cairo_runner::RunResources;
use assert_matches::assert_matches;

use super::*;
Expand Down
Expand Up @@ -266,14 +266,13 @@ mod tests {
use super::*;
use crate::hint_processor::builtin_hint_processor::hint_code;
use crate::vm::errors::memory_errors::MemoryError;
use crate::vm::runners::cairo_runner::RunResources;
use crate::{
any_box,
hint_processor::{
builtin_hint_processor::builtin_hint_processor_definition::{
BuiltinHintProcessor, HintProcessorData,
},
hint_processor_definition::HintProcessor,
hint_processor_definition::HintProcessorLogic,
},
types::exec_scope::ExecutionScopes,
utils::test_utils::*,
Expand Down
Expand Up @@ -131,15 +131,14 @@ mod tests {
use super::*;
use crate::stdlib::string::ToString;
use crate::types::relocatable::Relocatable;
use crate::vm::runners::cairo_runner::RunResources;
use crate::{
any_box,
hint_processor::{
builtin_hint_processor::{
builtin_hint_processor_definition::{BuiltinHintProcessor, HintProcessorData},
hint_code,
},
hint_processor_definition::HintProcessor,
hint_processor_definition::HintProcessorLogic,
},
types::relocatable::MaybeRelocatable,
utils::test_utils::*,
Expand Down
3 changes: 1 addition & 2 deletions vm/src/hint_processor/builtin_hint_processor/garaga.rs
Expand Up @@ -29,9 +29,8 @@ mod tests {
use crate::any_box;
use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor;
use crate::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::HintProcessorData;
use crate::hint_processor::hint_processor_definition::HintProcessor;
use crate::hint_processor::hint_processor_definition::HintProcessorLogic;
use crate::types::exec_scope::ExecutionScopes;
use crate::vm::runners::cairo_runner::RunResources;
use crate::{hint_processor::builtin_hint_processor::hint_code, utils::test_utils::*};
use felt::Felt252;
use num_traits::{Bounded, One, Zero};
Expand Down
3 changes: 1 addition & 2 deletions vm/src/hint_processor/builtin_hint_processor/keccak_utils.rs
Expand Up @@ -298,15 +298,14 @@ pub fn split_output_mid_low_high(
mod tests {
use super::*;
use crate::any_box;
use crate::vm::runners::cairo_runner::RunResources;
use crate::{
hint_processor::{
builtin_hint_processor::{
builtin_hint_processor_definition::{BuiltinHintProcessor, HintProcessorData},
hint_code,
keccak_utils::HashMap,
},
hint_processor_definition::{HintProcessor, HintReference},
hint_processor_definition::{HintProcessorLogic, HintReference},
},
types::exec_scope::ExecutionScopes,
utils::test_utils::*,
Expand Down
3 changes: 1 addition & 2 deletions vm/src/hint_processor/builtin_hint_processor/math_utils.rs
Expand Up @@ -782,15 +782,14 @@ mod tests {
use super::*;
use crate::stdlib::ops::Shl;

use crate::vm::runners::cairo_runner::RunResources;
use crate::{
any_box,
hint_processor::{
builtin_hint_processor::{
builtin_hint_processor_definition::{BuiltinHintProcessor, HintProcessorData},
hint_code,
},
hint_processor_definition::HintProcessor,
hint_processor_definition::HintProcessorLogic,
},
relocatable,
types::exec_scope::ExecutionScopes,
Expand Down

0 comments on commit acf65d1

Please sign in to comment.