Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
32 changes: 1 addition & 31 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ jobs:
strategy:
fail-fast: false
matrix:
special_features: ["", "extensive_hints", "mod_builtin"]
special_features: ["", "mod_builtin"]
target: [ test#1, test#2, test#3, test#4, test-no_std#1, test-no_std#2, test-no_std#3, test-no_std#4, test-wasm ]
name: Run tests
runs-on: ubuntu-22.04
Expand Down Expand Up @@ -569,36 +569,6 @@ jobs:
path: lcov-test-no_std-.info
key: codecov-cache-test-no_std--${{ github.sha }}
fail-on-cache-miss: true
- name: Fetch results for tests with stdlib (w/extensive_hints; part. 1)
uses: actions/cache/restore@v3
with:
path: lcov-test#1-extensive_hints.info
key: codecov-cache-test#1-extensive_hints-${{ github.sha }}
fail-on-cache-miss: true
- name: Fetch results for tests with stdlib (w/extensive_hints; part. 2)
uses: actions/cache/restore@v3
with:
path: lcov-test#2-extensive_hints.info
key: codecov-cache-test#2-extensive_hints-${{ github.sha }}
fail-on-cache-miss: true
- name: Fetch results for tests with stdlib (w/extensive_hints; part. 3)
uses: actions/cache/restore@v3
with:
path: lcov-test#3-extensive_hints.info
key: codecov-cache-test#3-extensive_hints-${{ github.sha }}
fail-on-cache-miss: true
- name: Fetch results for tests with stdlib (w/extensive_hints; part. 4)
uses: actions/cache/restore@v3
with:
path: lcov-test#4-extensive_hints.info
key: codecov-cache-test#4-extensive_hints-${{ github.sha }}
fail-on-cache-miss: true
- name: Fetch results for tests without stdlib (w/extensive_hints)
uses: actions/cache/restore@v3
with:
path: lcov-no_std-extensive_hints.info
key: codecov-cache-test-no_std-extensive_hints-${{ github.sha }}
fail-on-cache-miss: true


- name: Upload coverage to codecov.io
Expand Down
8 changes: 5 additions & 3 deletions Cargo.lock

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

4 changes: 1 addition & 3 deletions vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,9 @@ mod_builtin = []

# Note that these features are not retro-compatible with the cairo Python VM.
test_utils = ["std", "dep:arbitrary", "starknet-types-core/arbitrary", "starknet-types-core/std"] # This feature will reference every test-oriented feature
# Allows extending the set of hints for the current vm run from within a hint.
# For a usage example checkout vm/src/tests/run_deprecated_contract_class_simplified.rs
extensive_hints = []

[dependencies]
ahash = { version = "0.8.11", default-features = false, features = ["runtime-rng"] }
zip = {version = "0.6.6", optional = true }
num-bigint = { workspace = true }
rand = { workspace = true }
Expand Down
3 changes: 1 addition & 2 deletions vm/src/hint_processor/hint_processor_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use arbitrary::Arbitrary;

pub trait HintProcessorLogic {
// Executes the hint which's data is provided by a dynamic structure previously created by compile_hint
// Note: if the `extensive_hints` feature is activated the method used by the vm to execute hints is `execute_hint_extensive`, which's default implementation calls this method.
// Note: the method used by the vm to execute hints is `execute_hint_extensive`, which's default implementation calls this method.
fn execute_hint(
&mut self,
vm: &mut VirtualMachine,
Expand Down Expand Up @@ -51,7 +51,6 @@ pub trait HintProcessorLogic {
}))
}

#[cfg(feature = "extensive_hints")]
// Executes the hint which's data is provided by a dynamic structure previously created by compile_hint
// Also returns a map of hints to be loaded after the current hint is executed
// Note: This is the method used by the vm to execute hints,
Expand Down
1 change: 0 additions & 1 deletion vm/src/tests/run_deprecated_contract_class_simplified.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![cfg(feature = "extensive_hints")]
/* This file contains a test that runs the program: cairo_programs/starknet_os_deprecated_cc.cairo
For testsing purposes, the contract ran by this program is hardcoded, with values taken from compiling:

Expand Down
82 changes: 10 additions & 72 deletions vm/src/types/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ use core::num::NonZeroUsize;
use std::path::Path;

use super::builtin_name::BuiltinName;
#[cfg(feature = "extensive_hints")]
use super::relocatable::Relocatable;
#[cfg(feature = "test_utils")]
use arbitrary::{Arbitrary, Unstructured};
Expand Down Expand Up @@ -110,10 +109,7 @@ impl<'a> Arbitrary<'a> for SharedProgramData {
pub(crate) struct HintsCollection {
hints: Vec<HintParams>,
/// This maps a PC to the range of hints in `hints` that correspond to it.
#[cfg(not(feature = "extensive_hints"))]
pub(crate) hints_ranges: Vec<HintRange>,
#[cfg(feature = "extensive_hints")]
pub(crate) hints_ranges: HashMap<Relocatable, HintRange>,
pub(crate) hints_ranges: HashMap<Relocatable, HintRange, ahash::RandomState>,
}

impl HintsCollection {
Expand All @@ -138,20 +134,13 @@ impl HintsCollection {
}

let mut hints_values = Vec::with_capacity(full_len);
#[cfg(not(feature = "extensive_hints"))]
let mut hints_ranges = vec![None; max_hint_pc + 1];
#[cfg(feature = "extensive_hints")]
let mut hints_ranges = HashMap::default();

for (pc, hs) in hints.iter().filter(|(_, hs)| !hs.is_empty()) {
let range = (
hints_values.len(),
NonZeroUsize::new(hs.len()).expect("empty vecs already filtered"),
);
#[cfg(not(feature = "extensive_hints"))]
{
hints_ranges[*pc] = Some(range);
}
#[cfg(feature = "extensive_hints")]
hints_ranges.insert(Relocatable::from((0_isize, *pc)), range);
hints_values.extend_from_slice(&hs[..]);
}
Expand All @@ -165,24 +154,11 @@ impl HintsCollection {
pub fn iter_hints(&self) -> impl Iterator<Item = &HintParams> {
self.hints.iter()
}

#[cfg(not(feature = "extensive_hints"))]
pub fn get_hint_range_for_pc(&self, pc: usize) -> Option<HintRange> {
self.hints_ranges.get(pc).cloned()
}
}

impl From<&HintsCollection> for BTreeMap<usize, Vec<HintParams>> {
fn from(hc: &HintsCollection) -> Self {
let mut hint_map = BTreeMap::new();
#[cfg(not(feature = "extensive_hints"))]
for (i, r) in hc.hints_ranges.iter().enumerate() {
let Some(r) = r else {
continue;
};
hint_map.insert(i, hc.hints[r.0..r.0 + r.1.get()].to_owned());
}
#[cfg(feature = "extensive_hints")]
for (pc, r) in hc.hints_ranges.iter() {
hint_map.insert(pc.offset, hc.hints[r.0..r.0 + r.1.get()].to_owned());
}
Expand All @@ -191,10 +167,6 @@ impl From<&HintsCollection> for BTreeMap<usize, Vec<HintParams>> {
}

/// Represents a range of hints corresponding to a PC as a tuple `(start, length)`.
#[cfg(not(feature = "extensive_hints"))]
/// Is [`None`] if the range is empty, and it is [`Some`] tuple `(start, length)` otherwise.
type HintRange = Option<(usize, NonZeroUsize)>;
#[cfg(feature = "extensive_hints")]
pub type HintRange = (usize, NonZeroUsize);

#[cfg_attr(feature = "test_utils", derive(Arbitrary))]
Expand Down Expand Up @@ -475,22 +447,6 @@ impl TryFrom<CasmContractClass> for Program {
#[cfg(test)]
impl HintsCollection {
pub fn iter(&self) -> impl Iterator<Item = (usize, &[HintParams])> {
#[cfg(not(feature = "extensive_hints"))]
let iter = self
.hints_ranges
.iter()
.enumerate()
.filter_map(|(pc, range)| {
range.and_then(|(start, len)| {
let end = start + len.get();
if end <= self.hints.len() {
Some((pc, &self.hints[start..end]))
} else {
None
}
})
});
#[cfg(feature = "extensive_hints")]
let iter = self.hints_ranges.iter().filter_map(|(pc, (start, len))| {
let end = start + len.get();
if end <= self.hints.len() {
Expand Down Expand Up @@ -554,11 +510,10 @@ mod tests {
program.shared_program_data.hints_collection.hints,
Vec::new()
);
assert!(program
.shared_program_data
.hints_collection
.hints_ranges
.is_empty());
assert_eq!(
program.shared_program_data.hints_collection.hints_ranges,
HashMap::default()
);
}

#[test]
Expand Down Expand Up @@ -601,11 +556,10 @@ mod tests {
program.shared_program_data.hints_collection.hints,
Vec::new()
);
assert!(program
.shared_program_data
.hints_collection
.hints_ranges
.is_empty());
assert_eq!(
program.shared_program_data.hints_collection.hints_ranges,
HashMap::default()
);
}

#[test]
Expand Down Expand Up @@ -660,22 +614,6 @@ mod tests {
assert_eq!(program.shared_program_data.main, None);
assert_eq!(program.shared_program_data.identifiers, HashMap::new());

#[cfg(not(feature = "extensive_hints"))]
let program_hints: HashMap<_, _> = program
.shared_program_data
.hints_collection
.hints_ranges
.iter()
.enumerate()
.filter_map(|(pc, r)| r.map(|(s, l)| (pc, (s, s + l.get()))))
.map(|(pc, (s, e))| {
(
pc,
program.shared_program_data.hints_collection.hints[s..e].to_vec(),
)
})
.collect();
#[cfg(feature = "extensive_hints")]
let program_hints: HashMap<_, _> = program
.shared_program_data
.hints_collection
Expand Down
11 changes: 8 additions & 3 deletions vm/src/types/relocatable.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::stdlib::{
fmt::{self, Display},
hash::{Hash, Hasher},
ops::{Add, AddAssign, Sub},
prelude::*,
};
Expand All @@ -15,14 +16,18 @@ use serde::{Deserialize, Serialize};
use arbitrary::Arbitrary;

#[cfg_attr(feature = "test_utils", derive(Arbitrary))]
#[derive(
Eq, Ord, Hash, PartialEq, PartialOrd, Clone, Copy, Debug, Serialize, Deserialize, Default,
)]
#[derive(Eq, Ord, PartialEq, PartialOrd, Clone, Copy, Debug, Serialize, Deserialize, Default)]
pub struct Relocatable {
pub segment_index: isize,
pub offset: usize,
}

impl Hash for Relocatable {
fn hash<H: Hasher>(&self, state: &mut H) {
(((self.segment_index as i64) << 48) | (self.offset as i64)).hash(state);
}
}

#[cfg_attr(feature = "test_utils", derive(Arbitrary))]
#[derive(Eq, Ord, Hash, PartialEq, PartialOrd, Clone, Serialize, Deserialize)]
pub enum MaybeRelocatable {
Expand Down
33 changes: 0 additions & 33 deletions vm/src/vm/runners/cairo_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,11 +661,7 @@ impl CairoRunner {
hint_processor: &mut dyn HintProcessor,
) -> Result<(), VirtualMachineError> {
let references = &self.program.shared_program_data.reference_manager;
#[cfg(not(feature = "extensive_hints"))]
let hint_data = self.get_hint_data(references, hint_processor)?;
#[cfg(feature = "extensive_hints")]
let mut hint_data = self.get_hint_data(references, hint_processor)?;
#[cfg(feature = "extensive_hints")]
let mut hint_ranges = self
.program
.shared_program_data
Expand All @@ -678,18 +674,7 @@ impl CairoRunner {
vm.step(
hint_processor,
&mut self.exec_scopes,
#[cfg(feature = "extensive_hints")]
&mut hint_data,
#[cfg(not(feature = "extensive_hints"))]
self.program
.shared_program_data
.hints_collection
.get_hint_range_for_pc(vm.run_context.pc.offset)
.and_then(|range| {
range.and_then(|(start, length)| hint_data.get(start..start + length.get()))
})
.unwrap_or(&[]),
#[cfg(feature = "extensive_hints")]
&mut hint_ranges,
&self.program.constants,
)?;
Expand All @@ -712,27 +697,13 @@ impl CairoRunner {
hint_processor: &mut dyn HintProcessor,
) -> Result<(), VirtualMachineError> {
let references = &self.program.shared_program_data.reference_manager;
#[cfg(not(feature = "extensive_hints"))]
let hint_data = self.get_hint_data(references, hint_processor)?;
#[cfg(feature = "extensive_hints")]
let mut hint_data = self.get_hint_data(references, hint_processor)?;
#[cfg(feature = "extensive_hints")]
let mut hint_ranges = self
.program
.shared_program_data
.hints_collection
.hints_ranges
.clone();
#[cfg(not(feature = "extensive_hints"))]
let hint_data = &self
.program
.shared_program_data
.hints_collection
.get_hint_range_for_pc(vm.run_context.pc.offset)
.and_then(|range| {
range.and_then(|(start, length)| hint_data.get(start..start + length.get()))
})
.unwrap_or(&[]);

for remaining_steps in (1..=steps).rev() {
if self.final_pc.as_ref() == Some(&vm.run_context.pc) {
Expand All @@ -742,11 +713,7 @@ impl CairoRunner {
vm.step(
hint_processor,
&mut self.exec_scopes,
#[cfg(feature = "extensive_hints")]
&mut hint_data,
#[cfg(not(feature = "extensive_hints"))]
hint_data,
#[cfg(feature = "extensive_hints")]
&mut hint_ranges,
&self.program.constants,
)?;
Expand Down
Loading