forked from argotorg/fe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage_map.fe
More file actions
270 lines (231 loc) · 7.38 KB
/
Copy pathstorage_map.fe
File metadata and controls
270 lines (231 loc) · 7.38 KB
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
use super::effects::{Address, RawStorage}
use super::mem
use ingot::evm::ops::{keccak256, mstore, sload, sstore}
use ingot::evm::word::WordRepr
use core::{EffectRef, EffectRefMut}
/// Keys that can be written into the mapping preimage for keccak hashing.
pub trait StorageKey {
/// Writes the key bytes at `ptr` and returns the length written.
fn write_key(ptr: u256, self) -> u256
}
impl StorageKey for u256 {
#[inline(always)]
fn write_key(ptr: u256, self) -> u256 {
mstore(addr: ptr, value: self)
32
}
}
impl StorageKey for Address {
#[inline(always)]
fn write_key(ptr: u256, self) -> u256 {
u256::write_key(ptr: ptr, self.inner)
}
}
// Unsigned integer keys — widening cast to u256 zero-extends, matching
// Solidity's `mapping(uintN => V)` slot derivation (`keccak256(key ++ slot)`
// with the key left-padded to 32 bytes).
impl StorageKey for u8 {
fn write_key(ptr: u256, self) -> u256 {
u256::write_key(ptr: ptr, self.to_word())
}
}
impl StorageKey for u16 {
fn write_key(ptr: u256, self) -> u256 {
u256::write_key(ptr: ptr, self.to_word())
}
}
impl StorageKey for u32 {
fn write_key(ptr: u256, self) -> u256 {
u256::write_key(ptr: ptr, self.to_word())
}
}
impl StorageKey for u64 {
fn write_key(ptr: u256, self) -> u256 {
u256::write_key(ptr: ptr, self.to_word())
}
}
impl StorageKey for u128 {
fn write_key(ptr: u256, self) -> u256 {
u256::write_key(ptr: ptr, self.to_word())
}
}
impl StorageKey for usize {
fn write_key(ptr: u256, self) -> u256 {
u256::write_key(ptr: ptr, self.to_word())
}
}
// Signed integer keys — `to_word()` sign-extends, matching Solidity's
// `mapping(intN => V)` convention.
impl StorageKey for i8 {
fn write_key(ptr: u256, self) -> u256 {
u256::write_key(ptr: ptr, self.to_word())
}
}
impl StorageKey for i16 {
fn write_key(ptr: u256, self) -> u256 {
u256::write_key(ptr: ptr, self.to_word())
}
}
impl StorageKey for i32 {
fn write_key(ptr: u256, self) -> u256 {
u256::write_key(ptr: ptr, self.to_word())
}
}
impl StorageKey for i64 {
fn write_key(ptr: u256, self) -> u256 {
u256::write_key(ptr: ptr, self.to_word())
}
}
impl StorageKey for i128 {
fn write_key(ptr: u256, self) -> u256 {
u256::write_key(ptr: ptr, self.to_word())
}
}
impl StorageKey for i256 {
fn write_key(ptr: u256, self) -> u256 {
u256::write_key(ptr: ptr, self.to_word())
}
}
impl StorageKey for isize {
fn write_key(ptr: u256, self) -> u256 {
u256::write_key(ptr: ptr, self.to_word())
}
}
// `bool` — encoded as 1 (true) or 0 (false) in a 32-byte word.
impl StorageKey for bool {
fn write_key(ptr: u256, self) -> u256 {
u256::write_key(ptr: ptr, self.to_word())
}
}
/// A storage-backed mapping from keys `K` to values `V`.
///
/// `SALT` is the mapping's slot seed (the `slot` component in
/// `keccak256(key ++ slot)`), chosen explicitly for each mapping instance to
/// prevent collisions.
struct StorageMapSeal {}
pub struct StorageMap<K, V, const SALT: u256 = _> {
seal: StorageMapSeal,
}
impl<K, V, const SALT: u256> Copy for StorageMap<K, V, SALT> {}
#[inline(always)]
fn storagemap_storage_slot_with_salt<K>(key: K, salt: u256) -> u256
where K: StorageKey
{
// keccak256(key ++ slot) - standard Solidity mapping layout
// Use the current heap pointer as scratch to avoid clobbering live heap data.
// `mem::alloc(0)` also ensures we stay above any backend-reserved zones.
let ptr = mem::alloc(0)
let key_len = K::write_key(ptr: ptr, key)
mstore(addr: ptr + key_len, value: salt)
keccak256(offset: ptr, len: key_len + 32)
}
#[inline(always)]
fn storagemap_get_word_with_salt<K>(key: K, salt: u256) -> u256
where K: StorageKey
{
let storage_slot = storagemap_storage_slot_with_salt(key, salt)
sload(storage_slot)
}
#[inline(always)]
fn storagemap_set_word_with_salt<K>(key: K, salt: u256, word: u256)
where K: StorageKey
{
let storage_slot = storagemap_storage_slot_with_salt(key, salt)
sstore(slot: storage_slot, value: word)
}
impl<K: StorageKey, V: WordRepr, const SALT: u256> StorageMap<K, V, SALT> {
pub(ingot) fn new_unchecked() -> Self {
Self { seal: StorageMapSeal {} }
}
pub fn new() -> Self
uses (storage: mut RawStorage)
{
Self::new_unchecked()
}
#[inline(always)]
pub(ingot) fn get_unchecked(self, _ key: K) -> V {
V::from_word(storagemap_get_word_with_salt(key, salt: SALT))
}
#[inline(always)]
pub fn get(self, _ key: K) -> V {
self.get_unchecked(key)
}
#[inline(always)]
pub(ingot) fn set_unchecked(mut self, key: K, value: own V) {
storagemap_set_word_with_salt(key, salt: SALT, word: value.to_word())
}
#[inline(always)]
pub fn set(mut self, key: K, value: own V) {
self.set_unchecked(key: key, value)
}
}
impl<A, B> StorageKey for (A, B)
where A: StorageKey,
B: StorageKey {
#[inline(always)]
fn write_key(ptr: u256, self) -> u256 {
let a_len = A::write_key(ptr: ptr, self.0)
let b_len = B::write_key(ptr: ptr + a_len, self.1)
a_len + b_len
}
}
impl<A, B, C> StorageKey for (A, B, C)
where A: StorageKey,
B: StorageKey,
C: StorageKey {
#[inline(always)]
fn write_key(ptr: u256, self) -> u256 {
let a_len = A::write_key(ptr: ptr, self.0)
let b_len = B::write_key(ptr: ptr + a_len, self.1)
let c_len = C::write_key(ptr: ptr + a_len + b_len, self.2)
a_len + b_len + c_len
}
}
impl<A, B, C, D> StorageKey for (A, B, C, D)
where A: StorageKey,
B: StorageKey,
C: StorageKey,
D: StorageKey {
#[inline(always)]
fn write_key(ptr: u256, self) -> u256 {
let a_len = A::write_key(ptr: ptr, self.0)
let b_len = B::write_key(ptr: ptr + a_len, self.1)
let c_len = C::write_key(ptr: ptr + a_len + b_len, self.2)
let d_len = D::write_key(ptr: ptr + a_len + b_len + c_len, self.3)
a_len + b_len + c_len + d_len
}
}
impl<A, B, C, D, E> StorageKey for (A, B, C, D, E)
where A: StorageKey,
B: StorageKey,
C: StorageKey,
D: StorageKey,
E: StorageKey {
#[inline(always)]
fn write_key(ptr: u256, self) -> u256 {
let a_len = A::write_key(ptr: ptr, self.0)
let b_len = B::write_key(ptr: ptr + a_len, self.1)
let c_len = C::write_key(ptr: ptr + a_len + b_len, self.2)
let d_len = D::write_key(ptr: ptr + a_len + b_len + c_len, self.3)
let e_len = E::write_key(ptr: ptr + a_len + b_len + c_len + d_len, self.4)
a_len + b_len + c_len + d_len + e_len
}
}
impl<A, B, C, D, E, F> StorageKey for (A, B, C, D, E, F)
where A: StorageKey,
B: StorageKey,
C: StorageKey,
D: StorageKey,
E: StorageKey,
F: StorageKey {
#[inline(always)]
fn write_key(ptr: u256, self) -> u256 {
let a_len = A::write_key(ptr: ptr, self.0)
let b_len = B::write_key(ptr: ptr + a_len, self.1)
let c_len = C::write_key(ptr: ptr + a_len + b_len, self.2)
let d_len = D::write_key(ptr: ptr + a_len + b_len + c_len, self.3)
let e_len = E::write_key(ptr: ptr + a_len + b_len + c_len + d_len, self.4)
let f_len = F::write_key(ptr: ptr + a_len + b_len + c_len + d_len + e_len, self.5)
a_len + b_len + c_len + d_len + e_len + f_len
}
}