@@ -6,38 +6,63 @@ use starknet_api::core::ClassHash;
6
6
7
7
use super :: state_api:: State ;
8
8
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`.
10
23
pub trait VisitedPcs
11
24
where
12
25
Self : Default + Debug ,
13
26
{
14
27
/// 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.
15
31
type Pcs : Clone ;
16
32
33
+ /// Constructs a concrete implementation of the trait.
17
34
fn new ( ) -> Self ;
18
35
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.
20
37
///
21
38
/// The elements of the vector `pcs` match the type of field `pc` in
22
39
/// [`cairo_vm::vm::trace::trace_entry::RelocatedTraceEntry`]
23
40
fn insert ( & mut self , class_hash : & ClassHash , pcs : & [ usize ] ) ;
24
41
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.
26
45
fn extend ( & mut self , class_hash : & ClassHash , pcs : & Self :: Pcs ) ;
27
46
28
47
/// This function returns an iterator of `VisitedPcs`.
48
+ ///
49
+ /// One tuple is returned for each class hash recorded in `self`.
29
50
fn iter ( & self ) -> impl Iterator < Item = ( & ClassHash , & Self :: Pcs ) > ;
30
51
31
52
/// Get the recorded visited program counters for a specific `class_hash`.
32
53
fn entry ( & mut self , class_hash : ClassHash ) -> Entry < ' _ , ClassHash , Self :: Pcs > ;
33
54
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.
35
56
fn add_visited_pcs ( state : & mut dyn State , class_hash : & ClassHash , pcs : Self :: Pcs ) ;
36
57
37
- /// This function returns the program counters in a set.
58
+ /// This function transforms the internal representation of program counters into a set.
38
59
fn to_set ( pcs : Self :: Pcs ) -> HashSet < usize > ;
39
60
}
40
61
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`.
41
66
#[ derive( Debug , Default , PartialEq , Eq ) ]
42
67
pub struct VisitedPcsSet ( HashMap < ClassHash , HashSet < usize > > ) ;
43
68
impl VisitedPcs for VisitedPcsSet {
@@ -51,6 +76,10 @@ impl VisitedPcs for VisitedPcsSet {
51
76
self . 0 . entry ( * class_hash) . or_default ( ) . extend ( pcs) ;
52
77
}
53
78
79
+ fn extend ( & mut self , class_hash : & ClassHash , pcs : & Self :: Pcs ) {
80
+ self . 0 . entry ( * class_hash) . or_default ( ) . extend ( pcs) ;
81
+ }
82
+
54
83
fn iter ( & self ) -> impl Iterator < Item = ( & ClassHash , & Self :: Pcs ) > {
55
84
self . 0 . iter ( )
56
85
}
@@ -63,10 +92,6 @@ impl VisitedPcs for VisitedPcsSet {
63
92
state. add_visited_pcs ( * class_hash, & Vec :: from_iter ( pcs) ) ;
64
93
}
65
94
66
- fn extend ( & mut self , class_hash : & ClassHash , pcs : & Self :: Pcs ) {
67
- self . 0 . entry ( * class_hash) . or_default ( ) . extend ( pcs) ;
68
- }
69
-
70
95
fn to_set ( pcs : Self :: Pcs ) -> HashSet < usize > {
71
96
pcs
72
97
}
0 commit comments