Skip to content

Commit 9c8e115

Browse files
committed
chore: improved documentation for 'VisitedPcs'
1 parent 8140e88 commit 9c8e115

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

crates/blockifier/src/state/visited_pcs.rs

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,63 @@ use starknet_api::core::ClassHash;
66

77
use super::state_api::State;
88

9-
/// This trait is used in `CachedState` to record visited pcs of an entry point call.
9+
/// This trait is used in `CachedState` to record visited pcs of an entry point call. This allows
10+
/// flexible storage of program counters returned from cairo vm trace.
11+
///
12+
/// # Object Safety
13+
///
14+
/// This trait uses associated types instead of generics because only one implementation of the
15+
/// trait is required. Also, using associated types reduces the number of parameters required to be
16+
/// specified.
17+
/// The use of associated types makes the trait implementation not [object safe](https://doc.rust-lang.org/reference/items/traits.html#object-safety).
18+
///
19+
/// Self Bounds
20+
///
21+
/// - [`Default`] is required to allow a default instantiation of `CachedState`.
22+
/// - [`Debug`] is required for compatibility with other structs which derive `Debug`.
1023
pub trait VisitedPcs
1124
where
1225
Self: Default + Debug,
1326
{
1427
/// This is the type which contains visited program counters.
28+
///
29+
/// [`Clone`] is required to allow ownership of data throught cloning when receiving references
30+
/// from one of the trait methods.
1531
type Pcs: Clone;
1632

33+
/// Constructs a concrete implementation of the trait.
1734
fn new() -> Self;
1835

19-
/// The function `insert` reads the program counters returned by the cairo vm trace.
36+
/// This function records the program counters returned by the cairo vm trace.
2037
///
2138
/// The elements of the vector `pcs` match the type of field `pc` in
2239
/// [`cairo_vm::vm::trace::trace_entry::RelocatedTraceEntry`]
2340
fn insert(&mut self, class_hash: &ClassHash, pcs: &[usize]);
2441

25-
/// The function `extend` is used to extend an instance of `VisitedPcs` with another one.
42+
/// This function extends the program counters in `self` with those from another instance.
43+
///
44+
/// It is used to transfer the visited program counters from one object to another.
2645
fn extend(&mut self, class_hash: &ClassHash, pcs: &Self::Pcs);
2746

2847
/// This function returns an iterator of `VisitedPcs`.
48+
///
49+
/// One tuple is returned for each class hash recorded in `self`.
2950
fn iter(&self) -> impl Iterator<Item = (&ClassHash, &Self::Pcs)>;
3051

3152
/// Get the recorded visited program counters for a specific `class_hash`.
3253
fn entry(&mut self, class_hash: ClassHash) -> Entry<'_, ClassHash, Self::Pcs>;
3354

34-
/// Marks the given PC values as visited for the given class hash.
55+
/// Marks the given `pcs` values as visited for the given class hash.
3556
fn add_visited_pcs(state: &mut dyn State, class_hash: &ClassHash, pcs: Self::Pcs);
3657

37-
/// This function returns the program counters in a set.
58+
/// This function transforms the internal representation of program counters into a set.
3859
fn to_set(pcs: Self::Pcs) -> HashSet<usize>;
3960
}
4061

62+
/// [`VisitedPcsSet`] is the default implementation of the trait [`VisiedPcs`]. All visited program
63+
/// counters are inserted in a set and grouped by class hash.
64+
///
65+
/// This is also the structure used by the `native_blockifier`.
4166
#[derive(Debug, Default, PartialEq, Eq)]
4267
pub struct VisitedPcsSet(HashMap<ClassHash, HashSet<usize>>);
4368
impl VisitedPcs for VisitedPcsSet {
@@ -51,6 +76,10 @@ impl VisitedPcs for VisitedPcsSet {
5176
self.0.entry(*class_hash).or_default().extend(pcs);
5277
}
5378

79+
fn extend(&mut self, class_hash: &ClassHash, pcs: &Self::Pcs) {
80+
self.0.entry(*class_hash).or_default().extend(pcs);
81+
}
82+
5483
fn iter(&self) -> impl Iterator<Item = (&ClassHash, &Self::Pcs)> {
5584
self.0.iter()
5685
}
@@ -63,10 +92,6 @@ impl VisitedPcs for VisitedPcsSet {
6392
state.add_visited_pcs(*class_hash, &Vec::from_iter(pcs));
6493
}
6594

66-
fn extend(&mut self, class_hash: &ClassHash, pcs: &Self::Pcs) {
67-
self.0.entry(*class_hash).or_default().extend(pcs);
68-
}
69-
7095
fn to_set(pcs: Self::Pcs) -> HashSet<usize> {
7196
pcs
7297
}

0 commit comments

Comments
 (0)