@@ -25,6 +25,7 @@ pub type CasmClassCache = HashMap<ClassHash, CasmContractClass>;
25
25
26
26
pub const UNINITIALIZED_CLASS_HASH : & ClassHash = b"\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 " ;
27
27
28
+ /// Represents a cached state of contract classes with optional caches.
28
29
#[ derive( Default , Clone , Debug , Eq , Getters , MutGetters , PartialEq ) ]
29
30
pub struct CachedState < T : StateReader > {
30
31
pub state_reader : Arc < T > ,
@@ -37,6 +38,7 @@ pub struct CachedState<T: StateReader> {
37
38
}
38
39
39
40
impl < T : StateReader > CachedState < T > {
41
+ /// Constructor, creates a new cached state.
40
42
pub fn new (
41
43
state_reader : Arc < T > ,
42
44
contract_class_cache : Option < ContractClassCache > ,
@@ -50,6 +52,7 @@ impl<T: StateReader> CachedState<T> {
50
52
}
51
53
}
52
54
55
+ /// Creates a CachedState for testing purposes.
53
56
pub fn new_for_testing (
54
57
state_reader : Arc < T > ,
55
58
contract_classes : Option < ContractClassCache > ,
@@ -64,6 +67,7 @@ impl<T: StateReader> CachedState<T> {
64
67
}
65
68
}
66
69
70
+ /// Sets the contract classes cache.
67
71
pub fn set_contract_classes (
68
72
& mut self ,
69
73
contract_classes : ContractClassCache ,
@@ -75,6 +79,7 @@ impl<T: StateReader> CachedState<T> {
75
79
Ok ( ( ) )
76
80
}
77
81
82
+ /// Returns the casm classes.
78
83
#[ allow( dead_code) ]
79
84
pub ( crate ) fn get_casm_classes ( & mut self ) -> Result < & CasmClassCache , StateError > {
80
85
self . casm_contract_classes
@@ -84,6 +89,7 @@ impl<T: StateReader> CachedState<T> {
84
89
}
85
90
86
91
impl < T : StateReader > StateReader for CachedState < T > {
92
+ /// Returns the class hash for a given contract address.
87
93
fn get_class_hash_at ( & self , contract_address : & Address ) -> Result < ClassHash , StateError > {
88
94
if self . cache . get_class_hash ( contract_address) . is_none ( ) {
89
95
match self . state_reader . get_class_hash_at ( contract_address) {
@@ -105,6 +111,7 @@ impl<T: StateReader> StateReader for CachedState<T> {
105
111
. cloned ( )
106
112
}
107
113
114
+ /// Returns the nonce for a given contract address.
108
115
fn get_nonce_at ( & self , contract_address : & Address ) -> Result < Felt252 , StateError > {
109
116
if self . cache . get_nonce ( contract_address) . is_none ( ) {
110
117
return self . state_reader . get_nonce_at ( contract_address) ;
@@ -115,6 +122,7 @@ impl<T: StateReader> StateReader for CachedState<T> {
115
122
. cloned ( )
116
123
}
117
124
125
+ /// Returns storage data for a given storage entry.
118
126
fn get_storage_at ( & self , storage_entry : & StorageEntry ) -> Result < Felt252 , StateError > {
119
127
if self . cache . get_storage ( storage_entry) . is_none ( ) {
120
128
match self . state_reader . get_storage_at ( storage_entry) {
@@ -140,6 +148,7 @@ impl<T: StateReader> StateReader for CachedState<T> {
140
148
}
141
149
142
150
// TODO: check if that the proper way to store it (converting hash to address)
151
+ /// Returned the compiled class hash for a given class hash.
143
152
fn get_compiled_class_hash ( & self , class_hash : & ClassHash ) -> Result < ClassHash , StateError > {
144
153
if self
145
154
. cache
@@ -156,6 +165,7 @@ impl<T: StateReader> StateReader for CachedState<T> {
156
165
. cloned ( )
157
166
}
158
167
168
+ /// Returns the contract class for a given class hash.
159
169
fn get_contract_class ( & self , class_hash : & ClassHash ) -> Result < CompiledClass , StateError > {
160
170
// This method can receive both compiled_class_hash & class_hash and return both casm and deprecated contract classes
161
171
//, which can be on the cache or on the state_reader, different cases will be described below:
@@ -170,15 +180,15 @@ impl<T: StateReader> StateReader for CachedState<T> {
170
180
. as_ref ( )
171
181
. and_then ( |x| x. get ( class_hash) )
172
182
{
173
- return Ok ( CompiledClass :: Deprecated ( Box :: new ( compiled_class. clone ( ) ) ) ) ;
183
+ return Ok ( CompiledClass :: Deprecated ( Arc :: new ( compiled_class. clone ( ) ) ) ) ;
174
184
}
175
185
// I: CASM CONTRACT CLASS : COMPILED_CLASS_HASH
176
186
if let Some ( compiled_class) = self
177
187
. casm_contract_classes
178
188
. as_ref ( )
179
189
. and_then ( |x| x. get ( class_hash) )
180
190
{
181
- return Ok ( CompiledClass :: Casm ( Box :: new ( compiled_class. clone ( ) ) ) ) ;
191
+ return Ok ( CompiledClass :: Casm ( Arc :: new ( compiled_class. clone ( ) ) ) ) ;
182
192
}
183
193
// I: CASM CONTRACT CLASS : CLASS_HASH
184
194
if let Some ( compiled_class_hash) =
@@ -189,7 +199,7 @@ impl<T: StateReader> StateReader for CachedState<T> {
189
199
. as_ref ( )
190
200
. and_then ( |m| m. get ( compiled_class_hash) )
191
201
{
192
- return Ok ( CompiledClass :: Casm ( Box :: new ( casm_class. clone ( ) ) ) ) ;
202
+ return Ok ( CompiledClass :: Casm ( Arc :: new ( casm_class. clone ( ) ) ) ) ;
193
203
}
194
204
}
195
205
// II: FETCHING FROM STATE_READER
@@ -198,6 +208,7 @@ impl<T: StateReader> StateReader for CachedState<T> {
198
208
}
199
209
200
210
impl < T : StateReader > State for CachedState < T > {
211
+ /// Stores a contract class in the cache.
201
212
fn set_contract_class (
202
213
& mut self ,
203
214
class_hash : & ClassHash ,
@@ -215,6 +226,7 @@ impl<T: StateReader> State for CachedState<T> {
215
226
Ok ( ( ) )
216
227
}
217
228
229
+ /// Deploys a new contract and updates the cache.
218
230
fn deploy_contract (
219
231
& mut self ,
220
232
deploy_contract_address : Address ,
@@ -433,15 +445,15 @@ impl<T: StateReader> State for CachedState<T> {
433
445
. as_ref ( )
434
446
. and_then ( |x| x. get ( class_hash) )
435
447
{
436
- return Ok ( CompiledClass :: Deprecated ( Box :: new ( compiled_class. clone ( ) ) ) ) ;
448
+ return Ok ( CompiledClass :: Deprecated ( Arc :: new ( compiled_class. clone ( ) ) ) ) ;
437
449
}
438
450
// I: CASM CONTRACT CLASS : COMPILED_CLASS_HASH
439
451
if let Some ( compiled_class) = self
440
452
. casm_contract_classes
441
453
. as_ref ( )
442
454
. and_then ( |x| x. get ( class_hash) )
443
455
{
444
- return Ok ( CompiledClass :: Casm ( Box :: new ( compiled_class. clone ( ) ) ) ) ;
456
+ return Ok ( CompiledClass :: Casm ( Arc :: new ( compiled_class. clone ( ) ) ) ) ;
445
457
}
446
458
// I: CASM CONTRACT CLASS : CLASS_HASH
447
459
if let Some ( compiled_class_hash) =
@@ -452,7 +464,7 @@ impl<T: StateReader> State for CachedState<T> {
452
464
. as_ref ( )
453
465
. and_then ( |m| m. get ( compiled_class_hash) )
454
466
{
455
- return Ok ( CompiledClass :: Casm ( Box :: new ( casm_class. clone ( ) ) ) ) ;
467
+ return Ok ( CompiledClass :: Casm ( Arc :: new ( casm_class. clone ( ) ) ) ) ;
456
468
}
457
469
}
458
470
// II: FETCHING FROM STATE_READER
@@ -463,7 +475,7 @@ impl<T: StateReader> State for CachedState<T> {
463
475
let compiled_class_hash = self . get_compiled_class_hash ( class_hash) ?;
464
476
self . casm_contract_classes
465
477
. as_mut ( )
466
- . and_then ( |m| m. insert ( compiled_class_hash, * class. clone ( ) ) ) ;
478
+ . and_then ( |m| m. insert ( compiled_class_hash, class. as_ref ( ) . clone ( ) ) ) ;
467
479
}
468
480
CompiledClass :: Deprecated ( ref contract) => {
469
481
self . set_contract_class ( class_hash, & contract. clone ( ) ) ?
@@ -481,6 +493,8 @@ mod tests {
481
493
482
494
use num_traits:: One ;
483
495
496
+ /// Test checks if class hashes and nonces are correctly fetched from the state reader.
497
+ /// It also tests the increment_nonce method.
484
498
#[ test]
485
499
fn get_class_hash_and_nonce_from_state_reader ( ) {
486
500
let mut state_reader = InMemoryStateReader :: new (
@@ -522,6 +536,7 @@ mod tests {
522
536
) ;
523
537
}
524
538
539
+ /// This test checks if the contract class is correctly fetched from the state reader.
525
540
#[ test]
526
541
fn get_contract_class_from_state_reader ( ) {
527
542
let mut state_reader = InMemoryStateReader :: new (
@@ -554,6 +569,7 @@ mod tests {
554
569
) ;
555
570
}
556
571
572
+ /// This test verifies the correct handling of storage in the cached state.
557
573
#[ test]
558
574
fn cached_state_storage_test ( ) {
559
575
let mut cached_state =
@@ -572,6 +588,7 @@ mod tests {
572
588
. is_zero( ) ) ;
573
589
}
574
590
591
+ /// This test checks if deploying a contract works as expected.
575
592
#[ test]
576
593
fn cached_state_deploy_contract_test ( ) {
577
594
let state_reader = Arc :: new ( InMemoryStateReader :: default ( ) ) ;
@@ -585,6 +602,7 @@ mod tests {
585
602
. is_ok( ) ) ;
586
603
}
587
604
605
+ /// This test verifies the set and get storage values in the cached state.
588
606
#[ test]
589
607
fn get_and_set_storage ( ) {
590
608
let state_reader = Arc :: new ( InMemoryStateReader :: default ( ) ) ;
@@ -611,6 +629,7 @@ mod tests {
611
629
assert_eq ! ( new_result. unwrap( ) , new_value) ;
612
630
}
613
631
632
+ /// This test ensures that an error is thrown when trying to set contract classes twice.
614
633
#[ test]
615
634
fn set_contract_classes_twice_error_test ( ) {
616
635
let state_reader = InMemoryStateReader :: new (
@@ -631,6 +650,7 @@ mod tests {
631
650
assert_matches ! ( result, StateError :: AssignedContractClassCache ) ;
632
651
}
633
652
653
+ /// This test ensures that an error is thrown if a contract address is out of range.
634
654
#[ test]
635
655
fn deploy_contract_address_out_of_range_error_test ( ) {
636
656
let state_reader = InMemoryStateReader :: new (
@@ -656,6 +676,7 @@ mod tests {
656
676
) ;
657
677
}
658
678
679
+ /// This test ensures that an error is thrown if a contract address is already in use.
659
680
#[ test]
660
681
fn deploy_contract_address_in_use_error_test ( ) {
661
682
let state_reader = InMemoryStateReader :: new (
@@ -684,6 +705,7 @@ mod tests {
684
705
) ;
685
706
}
686
707
708
+ /// This test checks if replacing a contract in the cached state works correctly.
687
709
#[ test]
688
710
fn cached_state_replace_contract_test ( ) {
689
711
let state_reader = InMemoryStateReader :: new (
@@ -713,6 +735,7 @@ mod tests {
713
735
) ;
714
736
}
715
737
738
+ /// This test verifies if the cached state's internal structures are correctly updated after applying a state update.
716
739
#[ test]
717
740
fn cached_state_apply_state_update ( ) {
718
741
let state_reader = InMemoryStateReader :: new (
@@ -757,6 +780,7 @@ mod tests {
757
780
assert ! ( cached_state. cache. class_hash_initial_values. is_empty( ) ) ;
758
781
}
759
782
783
+ /// This test calculate the number of actual storage changes.
760
784
#[ test]
761
785
fn count_actual_storage_changes_test ( ) {
762
786
let state_reader = InMemoryStateReader :: default ( ) ;
0 commit comments