From 3b5be728ca22645bb747bba751018d12712929e6 Mon Sep 17 00:00:00 2001 From: fmoletta <99273364+fmoletta@users.noreply.github.com> Date: Mon, 22 Jan 2024 19:35:02 +0200 Subject: [PATCH] Add doc + default impl for ResourceTracker trait (#1576) * Add doc + default impl for ResourceTracker trait * Add changelog entry --------- Co-authored-by: Juan Bono --- CHANGELOG.md | 2 ++ vm/src/vm/runners/cairo_runner.rs | 26 +++++++++++++++++++------- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64620c5850..4fa43f44d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ #### Upcoming Changes +* feat: Add doc + default impl for ResourceTracker trait [#1576] (https://github.com/lambdaclass/cairo-vm/pull/1576) + * feat: Add `air_private_input` flag to `cairo1-run` [#1559] (https://github.com/lambdaclass/cairo-vm/pull/1559) * feat: Add `args` flag to `cairo1-run` [#15551] (https://github.com/lambdaclass/cairo-vm/pull/15551) diff --git a/vm/src/vm/runners/cairo_runner.rs b/vm/src/vm/runners/cairo_runner.rs index 11cc89dc3c..2aa7362b12 100644 --- a/vm/src/vm/runners/cairo_runner.rs +++ b/vm/src/vm/runners/cairo_runner.rs @@ -88,14 +88,26 @@ pub struct RunResources { n_steps: Option, } +/// This trait is in charge of overseeing the VM's step usage in contexts where a limited amount of steps are available +/// for a single execution (which may or not involve other executions taking place in the duration of it ). +/// This is mostly used in the context of starknet, where contracts can call other contracts while sharing the same step limit. +/// For the general use case, the default implementation can be used, which ignores resource tracking altogether +/// For an example on how to implement this trait for its intended purpose check out [BuiltinHintProcessor](cairo_vm::hint_processor::builtin_hint_processor::builtin_hint_processor_definition::BuiltinHintProcessor) pub trait ResourceTracker { - fn consumed(&self) -> bool; - - fn consume_step(&mut self); - - fn get_n_steps(&self) -> Option; - - fn run_resources(&self) -> &RunResources; + /// Returns true if there are no more steps left to run + fn consumed(&self) -> bool { + false + } + /// Subtracts 1 step from the available steps + fn consume_step(&mut self) {} + /// Returns the available steps for the run + fn get_n_steps(&self) -> Option { + None + } + /// Returns a reference to the available resources + fn run_resources(&self) -> &RunResources { + &RunResources { n_steps: None } + } } impl RunResources {