-
Notifications
You must be signed in to change notification settings - Fork 530
/
storage.cairo
745 lines (651 loc) · 26.3 KB
/
storage.cairo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
use core::traits::Into;
use core::pedersen::HashState;
use core::hash::HashStateTrait;
use starknet::storage_access::StorageBaseAddress;
use starknet::SyscallResult;
use starknet::storage_access::storage_base_address_from_felt252;
/// A pointer to an address in storage, can be used to read and write values, if the generic type
/// supports it (e.g. basic types like `felt252`).
pub struct StoragePointer<T> {
pub address: StorageBaseAddress,
pub offset: u8,
}
impl StoragePointerCopy<T> of Copy<StoragePointer<T>> {}
impl StoragePointerDrop<T> of Drop<StoragePointer<T>> {}
/// Same as `StoragePointer`, but with `offset` 0, which allows for some optimizations.
pub struct StoragePointer0Offset<T> {
pub address: StorageBaseAddress,
}
impl StoragePointer0OffsetCopy<T> of Copy<StoragePointer0Offset<T>> {}
impl StoragePointer0OffsetDrop<T> of Drop<StoragePointer0Offset<T>> {}
/// Trait for converting a storage member to a `StoragePointer0Offset`.
// type instead of `T`.
pub trait StorageAsPointer<TMemberState> {
type Value;
fn as_ptr(self: @TMemberState) -> StoragePointer0Offset<Self::Value>;
}
/// Trait for accessing the values in storage using a `StoragePointer`.
pub trait StoragePointerReadAccess<T> {
type Value;
fn read(self: @T) -> Self::Value;
}
/// Trait for writing values to storage using a `StoragePointer`.
pub trait StoragePointerWriteAccess<T> {
type Value;
fn write(self: T, value: Self::Value);
}
/// Trait for reading a contract/component storage member in a specific key place.
pub trait StorageMapReadAccessTrait<TMemberState> {
type Key;
type Value;
fn read(self: TMemberState, key: Self::Key) -> Self::Value;
}
/// Trait for writing contract/component storage member in a specific key place.
pub trait StorageMapWriteAccessTrait<TMemberState> {
type Key;
type Value;
fn write(self: TMemberState, key: Self::Key, value: Self::Value);
}
/// Simple implementation of `StoragePointerReadAccess` for any type that implements `Store` for 0
/// offset.
impl StorableStoragePointer0OffsetReadAccess<
T, +starknet::Store<T>
> of StoragePointerReadAccess<StoragePointer0Offset<T>> {
type Value = T;
fn read(self: @StoragePointer0Offset<T>) -> T {
starknet::SyscallResultTrait::unwrap_syscall(starknet::Store::<T>::read(0, *self.address))
}
}
/// Simple implementation of `StoragePointerReadAccess` for any type that implements `Store` for 0
/// offset.
impl MutableStorableStoragePointer0OffsetReadAccess<
T, +MutableTrait<T>, +starknet::Store<MutableTrait::<T>::InnerType>
> of StoragePointerReadAccess<StoragePointer0Offset<T>> {
type Value = MutableTrait::<T>::InnerType;
fn read(self: @StoragePointer0Offset<T>) -> MutableTrait::<T>::InnerType {
starknet::SyscallResultTrait::unwrap_syscall(
starknet::Store::<MutableTrait::<T>::InnerType>::read(0, *self.address)
)
}
}
/// Simple implementation of `StoragePointerWriteAccess` for any type that implements `Store` for 0
/// offset.
impl StorableStoragePointer0OffsetWriteAccess<
T, +MutableTrait<T>, +starknet::Store<MutableTrait::<T>::InnerType>
> of StoragePointerWriteAccess<StoragePointer0Offset<T>> {
type Value = MutableTrait::<T>::InnerType;
fn write(self: StoragePointer0Offset<T>, value: MutableTrait::<T>::InnerType) {
starknet::SyscallResultTrait::unwrap_syscall(
starknet::Store::<MutableTrait::<T>::InnerType>::write(0, self.address, value)
)
}
}
/// Simple implementation of `StoragePointerReadAccess` for any type that implements `Store` for any
/// offset.
pub impl StorableStoragePointerReadAccess<
T, +starknet::Store<T>
> of StoragePointerReadAccess<StoragePointer<T>> {
type Value = T;
fn read(self: @StoragePointer<T>) -> T {
starknet::SyscallResultTrait::unwrap_syscall(
starknet::Store::<T>::read_at_offset(0, *self.address, *self.offset)
)
}
}
/// Simple implementation of `StoragePointerReadAccess` for any mutable type that implements `Store`
impl MutableStorableStoragePointerReadAccess<
T, +MutableTrait<T>, +starknet::Store<MutableTrait::<T>::InnerType>
> of StoragePointerReadAccess<StoragePointer<T>> {
type Value = MutableTrait::<T>::InnerType;
fn read(self: @StoragePointer<T>) -> MutableTrait::<T>::InnerType {
starknet::SyscallResultTrait::unwrap_syscall(
starknet::Store::<
MutableTrait::<T>::InnerType
>::read_at_offset(0, *self.address, *self.offset)
)
}
}
/// Simple implementation of `StoragePointerWriteAccess` for any mutable type that implements
/// `Store`.
impl MutableStorableStoragePointerWriteAccess<
T, +MutableTrait<T>, +starknet::Store<MutableTrait::<T>::InnerType>
> of StoragePointerWriteAccess<StoragePointer<T>> {
type Value = MutableTrait::<T>::InnerType;
fn write(self: StoragePointer<T>, value: MutableTrait::<T>::InnerType) {
starknet::SyscallResultTrait::unwrap_syscall(
starknet::Store::<
MutableTrait::<T>::InnerType
>::write_at_offset(0, self.address, self.offset, value)
)
}
}
/// An intermediate struct to store a hash state, in order to be able to hash multiple values and
/// get the final address.
/// Storage path should have two interfaces, if T is storable then it should implement
/// `StorageAsPointer` in order to be able to get the address of the storage path. Otherwise, if
/// T is not storable then it should implement some kind of updating trait, e.g. `StoragePathEntry`.
pub struct StoragePath<T> {
hash_state: StoragePathHashState,
}
/// The hash state of a storage path.
type StoragePathHashState = core::pedersen::HashState;
impl StoragePathCopy<T> of core::traits::Copy<StoragePath<T>> {}
impl StoragePathDrop<T> of core::traits::Drop<StoragePath<T>> {}
/// Trait for StoragePath operations.
trait StoragePathTrait<T> {
fn new(init_value: felt252) -> StoragePath<T>;
fn finalize(self: StoragePath<T>) -> StorageBaseAddress;
}
impl StoragePathImpl<T> of StoragePathTrait<T> {
fn new(init_value: felt252) -> StoragePath<T> {
StoragePath { hash_state: core::pedersen::PedersenTrait::new(init_value) }
}
fn finalize(self: StoragePath<T>) -> StorageBaseAddress {
storage_base_address_from_felt252(self.hash_state.finalize())
}
}
trait StoragePathUpdateTrait<T, Key> {
fn update(self: StoragePath<T>, value: Key) -> StoragePath<T>;
}
impl StoragePathUpdateImpl<
T, Key, +core::hash::Hash<Key, StoragePathHashState>
> of StoragePathUpdateTrait<T, Key> {
fn update(self: StoragePath<T>, value: Key) -> StoragePath<T> {
StoragePath {
hash_state: core::hash::Hash::<
Key, StoragePathHashState
>::update_state(self.hash_state, value)
}
}
}
impl StoragePathSIntoT<S, T> of core::traits::Into<StoragePath<S>, StoragePath<T>> {
fn into(self: StoragePath<S>) -> StoragePath<T> {
StoragePath::<T> { hash_state: self.hash_state }
}
}
/// Trait for creating a new `StoragePath` from a storage member.
pub trait StorageAsPath<TMemberState> {
type Value;
fn as_path(self: @TMemberState) -> StoragePath<Self::Value>;
}
/// An implementation of `StorageAsPointer` for any `StoragePath` with inner type that implements
/// `Store`.
impl StorableStoragePathAsPointer<T, +starknet::Store<T>> of StorageAsPointer<StoragePath<T>> {
type Value = T;
fn as_ptr(self: @StoragePath<T>) -> StoragePointer0Offset<T> {
StoragePointer0Offset { address: (*self).finalize() }
}
}
/// An implementation of `StorageAsPointer` for any `StoragePath` with inner type that implements
/// `Store`.
impl MutableStorableStoragePathAsPointer<
T, +MutableTrait<T>, +starknet::Store<MutableTrait::<T>::InnerType>
> of StorageAsPointer<StoragePath<T>> {
type Value = T;
fn as_ptr(self: @StoragePath<T>) -> StoragePointer0Offset<T> {
StoragePointer0Offset { address: (*self).finalize() }
}
}
/// Trait for updating the hash state with a value, using an `entry` method.
pub trait StoragePathEntry<C> {
type Key;
type Value;
fn entry(self: C, key: Self::Key) -> StoragePath<Self::Value>;
}
/// A struct that represents a map in a contract storage.
// TODO(Gil): Make it a phantom type once we can annotate a type as phantom from within another
// attribute, specifically from #[storage].
#[phantom]
pub struct Map<K, V> {}
/// A trait for making a map like type support implement the `StoragePathEntry` trait.
trait EntryInfo<T> {
type Key;
type Value;
}
impl EntryInfoImpl<K, V> of EntryInfo<Map<K, V>> {
type Key = K;
type Value = V;
}
/// Implement StoragePathEntry for any `EntryInfo` type if their key implements `Hash`.
impl EntryInfoStoragePathEntry<
T, +EntryInfo<T>, +core::hash::Hash<EntryInfo::<T>::Key, StoragePathHashState>
> of StoragePathEntry<StoragePath<T>> {
type Key = EntryInfo::<T>::Key;
type Value = EntryInfo::<T>::Value;
fn entry(self: StoragePath<T>, key: EntryInfo::<T>::Key) -> StoragePath<EntryInfo::<T>::Value> {
self.update(key).into()
}
}
/// Same as `StoragePathEntryMap`, but for Mutable<T>, forwards the Mutable wrapper onto the value
/// type.
impl MutableEntryStoragePathEntry<
T,
+MutableTrait<T>,
impl EntryImpl: EntryInfo<MutableTrait::<T>::InnerType>,
+core::hash::Hash<EntryImpl::Key, StoragePathHashState>
> of StoragePathEntry<StoragePath<T>> {
type Key = EntryImpl::Key;
type Value = Mutable<EntryImpl::Value>;
fn entry(self: StoragePath<T>, key: EntryImpl::Key) -> StoragePath<Mutable<EntryImpl::Value>> {
self.update(key).into()
}
}
/// Implement StorageMapAccessTrait for any type that implements StoragePathEntry and Store.
impl StorableEntryReadAccess<
T,
+EntryInfo<T>,
+core::hash::Hash<EntryInfo::<T>::Key, StoragePathHashState>,
+starknet::Store<EntryInfo::<T>::Value>,
> of StorageMapReadAccessTrait<StoragePath<T>> {
type Key = EntryInfo::<T>::Key;
type Value = EntryInfo::<T>::Value;
fn read(self: StoragePath<T>, key: EntryInfo::<T>::Key) -> EntryInfo::<T>::Value {
self.entry(key).as_ptr().read()
}
}
impl StorageAsPathReadForward<
T,
impl PathImpl: StorageAsPath<T>,
impl AccessImpl: StorageMapReadAccessTrait<StoragePath<PathImpl::Value>>,
+Drop<T>,
+Drop<AccessImpl::Key>,
> of StorageMapReadAccessTrait<T> {
type Key = AccessImpl::Key;
type Value = AccessImpl::Value;
#[inline(always)]
fn read(self: T, key: AccessImpl::Key) -> AccessImpl::Value {
self.as_path().read(key)
}
}
/// Implement StorageMapAccessTrait for any Mutable type that implements StoragePathEntry and
/// Store.
impl MutableStorableEntryReadAccess<
T,
+MutableTrait<T>,
+EntryInfo<MutableTrait::<T>::InnerType>,
+core::hash::Hash<EntryInfo::<MutableTrait::<T>::InnerType>::Key, StoragePathHashState>,
+starknet::Store<EntryInfo::<MutableTrait::<T>::InnerType>::Value>,
> of StorageMapReadAccessTrait<StoragePath<T>> {
type Key = EntryInfo::<MutableTrait::<T>::InnerType>::Key;
type Value = EntryInfo::<MutableTrait::<T>::InnerType>::Value;
#[inline(always)]
fn read(
self: StoragePath<T>, key: EntryInfo::<MutableTrait::<T>::InnerType>::Key
) -> EntryInfo::<MutableTrait::<T>::InnerType>::Value {
self.entry(key).as_ptr().read()
}
}
/// Implement StorageMapAccessTrait for any Mutable type that implements StoragePathEntry and
/// Store.
impl MutableStorableEntryWriteAccess<
T,
+MutableTrait<T>,
+EntryInfo<MutableTrait::<T>::InnerType>,
+core::hash::Hash<EntryInfo::<MutableTrait::<T>::InnerType>::Key, StoragePathHashState>,
+starknet::Store<EntryInfo::<MutableTrait::<T>::InnerType>::Value>,
+Drop<EntryInfo::<MutableTrait::<T>::InnerType>::Value>
> of StorageMapWriteAccessTrait<StoragePath<T>> {
type Key = EntryInfo::<MutableTrait::<T>::InnerType>::Key;
type Value = EntryInfo::<MutableTrait::<T>::InnerType>::Value;
fn write(
self: StoragePath<T>,
key: EntryInfo::<MutableTrait::<T>::InnerType>::Key,
value: EntryInfo::<MutableTrait::<T>::InnerType>::Value
) {
self.entry(key).as_ptr().write(value)
}
}
impl StorageAsPathWriteForward<
T,
impl PathImpl: StorageAsPath<T>,
impl AccessImpl: StorageMapWriteAccessTrait<StoragePath<PathImpl::Value>>,
+Drop<T>,
+Drop<AccessImpl::Key>,
+Drop<AccessImpl::Value>,
> of StorageMapWriteAccessTrait<T> {
type Key = AccessImpl::Key;
type Value = AccessImpl::Value;
fn write(self: T, key: AccessImpl::Key, value: AccessImpl::Value) {
self.as_path().write(key, value)
}
}
/// A trait that binds a storage path of a struct, and the struct storage node (a storage node is a
/// struct that all its fields are storage paths, one for each member of the original struct).
pub trait StorageNode<T> {
type NodeType;
fn storage_node(self: StoragePath<T>) -> Self::NodeType;
}
/// This makes the storage node members directly accessible from a path to the parent struct.
impl StorageNodeDeref<T, +StorageNode<T>> of core::ops::Deref<StoragePath<T>> {
type Target = StorageNode::<T>::NodeType;
fn deref(self: StoragePath<T>) -> Self::Target {
self.storage_node()
}
}
/// A mutable version of `StorageNode`, works the same way, but on `Mutable<T>`.
pub trait MutableStorageNode<T> {
type NodeType;
fn mutable_storage_node(self: StoragePath<Mutable<T>>) -> Self::NodeType;
}
/// This makes the storage node members directly accessible from a path to the parent struct.
impl MutableStorageNodeDeref<
T, +MutableStorageNode<T>
> of core::ops::Deref<StoragePath<Mutable<T>>> {
type Target = MutableStorageNode::<T>::NodeType;
fn deref(self: StoragePath<Mutable<T>>) -> Self::Target {
self.mutable_storage_node()
}
}
/// Similar to storage node, but for structs which are stored sequentially in the storage. In
/// contrast to storage node, the fields of the struct are just offsetted from the base address of
/// the struct.
pub trait SubPointers<T> {
/// The type of the storage pointers, generated for the struct T.
type SubPointersType;
/// Creates a sub pointers struct for the given storage pointer to a struct T.
fn sub_pointers(self: StoragePointer<T>) -> Self::SubPointersType;
}
/// This makes the sub-pointers members directly accessible from a pointer to the parent struct.
impl SubPointersDeref<T, +SubPointers<T>> of core::ops::Deref<StoragePointer<T>> {
type Target = SubPointers::<T>::SubPointersType;
fn deref(self: StoragePointer<T>) -> Self::Target {
self.sub_pointers()
}
}
/// A mutable version of `SubPointers`, works the same way, but on `Mutable<T>`.
pub trait MutableSubPointers<T> {
/// The type of the storage pointers, generated for the struct T.
type SubPointersType;
/// Creates a sub pointers struct for the given storage pointer to a struct T.
fn mutable_sub_pointers(self: StoragePointer<Mutable<T>>) -> Self::SubPointersType;
}
/// This makes the sub-pointers members directly accessible from a pointer to the parent struct.
impl MutableSubPointersDeref<
T, +MutableSubPointers<T>
> of core::ops::Deref<StoragePointer<Mutable<T>>> {
type Target = MutableSubPointers::<T>::SubPointersType;
fn deref(self: StoragePointer<Mutable<T>>) -> Self::Target {
self.mutable_sub_pointers()
}
}
/// Implement deref for storage paths that implements StorageAsPointer.
impl StoragePathDeref<
T, impl PointerImpl: StorageAsPointer<StoragePath<T>>
> of core::ops::Deref<StoragePath<T>> {
type Target = StoragePointer0Offset<PointerImpl::Value>;
fn deref(self: StoragePath<T>) -> StoragePointer0Offset<PointerImpl::Value> {
self.as_ptr()
}
}
/// Implement deref for StoragePointer0Offset into a StoragePointer.
impl StoragePointer0OffsetDeref<T> of core::ops::Deref<StoragePointer0Offset<T>> {
type Target = StoragePointer<T>;
fn deref(self: StoragePointer0Offset<T>) -> StoragePointer<T> {
StoragePointer::<T> { address: self.address, offset: 0 }
}
}
/// A struct for delaying the creation of a storage path, used for lazy evaluation in storage nodes.
pub struct PendingStoragePath<T> {
hash_state: StoragePathHashState,
pending_key: felt252,
}
/// A trait for creating a `PendingStoragePath` from a hash state and a key.
pub trait PendingStoragePathTrait<T, S> {
fn new(storage_path: @StoragePath<S>, pending_key: felt252) -> PendingStoragePath<T>;
}
/// An implementation of `StoragePathEntry` for `PendingStoragePath`.
impl PendingStoragePathImpl<T, S> of PendingStoragePathTrait<T, S> {
fn new(storage_path: @StoragePath<S>, pending_key: felt252) -> PendingStoragePath<T> {
PendingStoragePath { hash_state: *storage_path.hash_state, pending_key }
}
}
impl PendingStoragePathDrop<T> of Drop<PendingStoragePath<T>> {}
impl PendingStoragePathCopy<T> of Copy<PendingStoragePath<T>> {}
/// An implementation of 'StorageAsPath' for `PendingStoragePath`.
impl PendingStoragePathAsPath<T> of StorageAsPath<PendingStoragePath<T>> {
type Value = T;
fn as_path(self: @PendingStoragePath<T>) -> StoragePath<T> {
StoragePath::<
T
> { hash_state: core::hash::HashStateTrait::update(*self.hash_state, *self.pending_key) }
}
}
/// Deref pending storage path into a storage path.
impl PendingStoragePathDeref<T> of core::ops::Deref<PendingStoragePath<T>> {
type Target = StoragePath<T>;
fn deref(self: PendingStoragePath<T>) -> Self::Target {
self.as_path()
}
}
/// A struct for holding an address to initialize a storage path with. The members (not direct
/// members, but accessible using deref) of the contract state are `StorageBase` instances, with the
/// generic type representing the type of the stored member.
pub struct StorageBase<T> {
pub address: felt252,
}
/// A trait for creating the struct containing the storage base of all the members of a contract
/// state.
pub trait StorageBaseTrait<T> {
type BaseType;
type BaseMutType;
/// Creates a struct containing the storage base of all the members of a contract state. Should
/// be called from the `deref` method of the contract state.
fn storage_base(self: @T) -> Self::BaseType;
/// Creates a struct containing the storage base, with the generic type wrapped in a `Mutable`
/// type. Should be called from the `deref_mut` method of the contract state.
fn storage_base_mut(ref self: T) -> Self::BaseMutType;
}
impl StorageBaseDrop<T> of Drop<StorageBase<T>> {}
impl StorageBaseCopy<T> of Copy<StorageBase<T>> {}
impl StorageBaseAsPath<T> of StorageAsPath<StorageBase<T>> {
type Value = T;
fn as_path(self: @StorageBase<T>) -> StoragePath<T> {
StoragePathTrait::new(*self.address)
}
}
impl StorageBaseDeref<T> of core::ops::Deref<StorageBase<T>> {
type Target = StoragePath<T>;
fn deref(self: StorageBase<T>) -> Self::Target {
self.as_path()
}
}
/// Implement as_ptr for any type that implements StorageAsPath and Store.
impl StorablePathableStorageAsPointer<
T,
impl PathImpl: StorageAsPath<T>,
impl PtrImpl: StorageAsPointer<StoragePath<PathImpl::Value>>,
> of StorageAsPointer<T> {
type Value = PtrImpl::Value;
fn as_ptr(self: @T) -> StoragePointer0Offset<PtrImpl::Value> {
let path = self.as_path();
path.as_ptr()
}
}
/// Implement StoragePointerReadAccess for any type that implements StorageAsPointer and
/// StoragePointerReadAccess.
impl StorablePointerReadAccessImpl<
T,
impl PointerImpl: StorageAsPointer<T>,
impl AccessImpl: StoragePointerReadAccess<StoragePointer0Offset<PointerImpl::Value>>,
> of StoragePointerReadAccess<T> {
type Value = AccessImpl::Value;
fn read(self: @T) -> Self::Value {
self.as_ptr().read()
}
}
/// Implement StoragePointerWriteAccess for any type that implements StorageAsPointer.
impl StorablePointerWriteAccessImpl<
T,
impl PointerImpl: StorageAsPointer<T>,
impl AccessImpl: StoragePointerWriteAccess<StoragePointer0Offset<PointerImpl::Value>>,
+Drop<T>,
+Drop<AccessImpl::Value>,
> of StoragePointerWriteAccess<T> {
type Value = AccessImpl::Value;
fn write(self: T, value: Self::Value) {
let ptr: StoragePointer0Offset<PointerImpl::Value> = self.as_ptr();
ptr.write(value)
}
}
/// Implement StoragePathEntry for any type that implements StoragePath and StoragePathEntry.
impl PathableStorageEntryImpl<
T,
impl PathImpl: StorageAsPath<T>,
impl EntryImpl: StoragePathEntry<StoragePath<PathImpl::Value>>,
+Drop<T>,
+Drop<EntryImpl::Key>,
> of StoragePathEntry<T> {
type Key = EntryImpl::Key;
type Value = EntryImpl::Value;
fn entry(self: T, key: Self::Key) -> StoragePath<Self::Value> {
let path = PathImpl::as_path(@self);
EntryImpl::entry(path, key)
}
}
/// A wrapper around different storage related types, indicating that the instance is mutable,
/// i.e. originally created from a `ref` contract state.
#[phantom]
pub struct Mutable<T> {}
impl MutableDrop<T> of Drop<Mutable<T>> {}
impl MutableCopy<T> of Copy<Mutable<T>> {}
/// A trait for exposing the inner type of a `Mutable` type.
trait MutableTrait<T> {
type InnerType;
}
impl MutableImpl<T> of MutableTrait<Mutable<T>> {
type InnerType = T;
}
/// Implementation of SubPointers for core types.
#[derive(Drop, Copy)]
struct u256SubPointers {
pub low: starknet::storage::StoragePointer<u128>,
pub high: starknet::storage::StoragePointer<u128>,
}
impl u256SubPointersImpl of starknet::storage::SubPointers<u256> {
type SubPointersType = u256SubPointers;
fn sub_pointers(self: starknet::storage::StoragePointer<u256>) -> u256SubPointers {
let base_address = self.address;
let mut current_offset = self.offset;
let low_value = starknet::storage::StoragePointer::<
u128
> { address: base_address, offset: current_offset, };
current_offset = current_offset + starknet::Store::<u128>::size();
let high_value = starknet::storage::StoragePointer::<
u128
> { address: base_address, offset: current_offset, };
u256SubPointers { low: low_value, high: high_value, }
}
}
#[derive(Drop, Copy)]
struct MutableU256SubPointers {
pub low: starknet::storage::StoragePointer<Mutable<u128>>,
pub high: starknet::storage::StoragePointer<Mutable<u128>>,
}
impl MutableU256SubPointersImpl of starknet::storage::MutableSubPointers<u256> {
type SubPointersType = MutableU256SubPointers;
fn mutable_sub_pointers(
self: starknet::storage::StoragePointer<Mutable<u256>>
) -> MutableU256SubPointers {
let base_address = self.address;
let mut current_offset = self.offset;
let low_value = starknet::storage::StoragePointer::<
Mutable<u128>
> { address: base_address, offset: current_offset, };
current_offset = current_offset + starknet::Store::<u128>::size();
let high_value = starknet::storage::StoragePointer::<
Mutable<u128>
> { address: base_address, offset: current_offset, };
MutableU256SubPointers { low: low_value, high: high_value, }
}
}
/// A type to represent an array in storage. The length of the storage is stored in the storage
/// base, while the elements are stored in hash(storage_base, index).
#[phantom]
pub struct StorageArray<T> {}
impl StorageArrayDrop<T> of Drop<StorageArray<T>> {};
impl StorageArrayCopy<T> of Copy<StorageArray<T>> {};
/// Implement as_ptr for StorageArray.
impl StorageArrayAsPointer<T> of StorageAsPointer<StoragePath<StorageArray<T>>> {
type Value = u64;
fn as_ptr(self: @StoragePath<StorageArray<T>>) -> StoragePointer0Offset<u64> {
StoragePointer0Offset { address: (*self).finalize() }
}
}
/// Implement as_ptr for Mutable<StorageArray>.
impl MutableStorageArrayAsPointer<T> of StorageAsPointer<StoragePath<Mutable<StorageArray<T>>>> {
type Value = Mutable<u64>;
fn as_ptr(self: @StoragePath<Mutable<StorageArray<T>>>) -> StoragePointer0Offset<Mutable<u64>> {
StoragePointer0Offset { address: (*self).finalize() }
}
}
pub trait StorageArrayTrait<T> {
type ElementType;
fn at(self: T, index: u64) -> StoragePath<Self::ElementType>;
fn len(self: T) -> u64;
}
impl StorageArrayImpl<T> of StorageArrayTrait<StoragePath<StorageArray<T>>> {
type ElementType = T;
fn at(self: StoragePath<StorageArray<T>>, index: u64) -> StoragePath<T> {
let array_len = self.len();
assert!(index < array_len, "Index out of bounds");
self.update(index).into()
}
fn len(self: StoragePath<StorageArray<T>>) -> u64 {
self.as_ptr().read()
}
}
impl PathableStorageArrayImpl<
T,
+Drop<T>,
impl PathImpl: StorageAsPath<T>,
impl ArrayTraitImpl: StorageArrayTrait<StoragePath<PathImpl::Value>>
> of StorageArrayTrait<T> {
type ElementType = ArrayTraitImpl::ElementType;
fn at(self: T, index: u64) -> StoragePath<ArrayTraitImpl::ElementType> {
self.as_path().at(index)
}
fn len(self: T) -> u64 {
self.as_path().len()
}
}
pub trait MutableStorageArrayTrait<T> {
type ElementType;
fn at(self: T, index: u64) -> StoragePath<Mutable<Self::ElementType>>;
fn len(self: T) -> u64;
fn append(self: T) -> StoragePath<Mutable<Self::ElementType>>;
}
impl MutableStorageArrayImpl<
T, +Drop<T>
> of MutableStorageArrayTrait<StoragePath<Mutable<StorageArray<T>>>> {
type ElementType = T;
fn at(self: StoragePath<Mutable<StorageArray<T>>>, index: u64) -> StoragePath<Mutable<T>> {
let array_len = self.len();
assert!(index < array_len, "Index out of bounds");
self.update(index).into()
}
fn len(self: StoragePath<Mutable<StorageArray<T>>>) -> u64 {
self.as_ptr().read()
}
fn append(self: StoragePath<Mutable<StorageArray<T>>>) -> StoragePath<Mutable<T>> {
let array_len = self.len();
self.as_ptr().write(array_len + 1);
self.at(array_len)
}
}
impl PathableMutableStorageArrayImpl<
T,
+Drop<T>,
impl PathImpl: StorageAsPath<T>,
impl ArrayTraitImpl: MutableStorageArrayTrait<StoragePath<PathImpl::Value>>
> of MutableStorageArrayTrait<T> {
type ElementType = ArrayTraitImpl::ElementType;
fn at(self: T, index: u64) -> StoragePath<Mutable<ArrayTraitImpl::ElementType>> {
self.as_path().at(index)
}
fn len(self: T) -> u64 {
self.as_path().len()
}
fn append(self: T) -> StoragePath<Mutable<ArrayTraitImpl::ElementType>> {
self.as_path().append()
}
}