-
Notifications
You must be signed in to change notification settings - Fork 349
/
proxy_impl.cairo
293 lines (264 loc) · 8.98 KB
/
proxy_impl.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
%lang starknet
from starkware.cairo.common.bool import FALSE, TRUE
from starkware.cairo.common.cairo_builtins import HashBuiltin
from starkware.cairo.common.hash_state import (
hash_finalize,
hash_init,
hash_update,
hash_update_single,
)
from starkware.cairo.common.math import assert_le, assert_not_zero
from starkware.starknet.common.syscalls import get_block_timestamp
from starkware.starknet.std_contracts.upgradability_proxy.finalizable import finalize, not_finalized
from starkware.starknet.std_contracts.upgradability_proxy.governance import only_governor
from starkware.starknet.std_contracts.upgradability_proxy.Initializable_interface import (
ExternalInitializer,
Initializable,
)
@storage_var
func upgrade_delay() -> (delay_seconds: felt) {
}
@storage_var
func impl_activation_time(key: felt) -> (ready_time: felt) {
}
@storage_var
func class_hash() -> (hash: felt) {
}
// Emitted upon adding an implementation.
@event
func implementation_added(
implementation_hash: felt,
eic_hash: felt,
init_vector_len: felt,
init_vector: felt*,
final: felt,
) {
}
// Emitted when an implementation is removed.
@event
func implementation_removed(
implementation_hash: felt,
eic_hash: felt,
init_vector_len: felt,
init_vector: felt*,
final: felt,
) {
}
// Emitted upon upgrading to an implementation.
@event
func implementation_upgraded(
implementation_hash: felt, eic_hash: felt, init_vector_len: felt, init_vector: felt*
) {
}
// Emitted upon implementation finalization.
@event
func implementation_finalized(implementation_hash: felt) {
}
@view
func implementation{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (
implementation_hash_: felt
) {
let (implementation_hash_) = class_hash.read();
return (implementation_hash_=implementation_hash_);
}
@view
func implementation_time{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
implementation_hash_: felt,
eic_hash: felt,
init_vector_len: felt,
init_vector: felt*,
final: felt,
) -> (time: felt) {
let (implementation_key) = calc_impl_key(
implementation_hash_, eic_hash, init_vector_len, init_vector, final
);
let (time) = impl_activation_time.read(implementation_key);
return (time=time);
}
@external
func add_implementation{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
implementation_hash_: felt,
eic_hash: felt,
init_vector_len: felt,
init_vector: felt*,
final: felt,
) {
alloc_locals;
only_governor();
// Prevent adding a zero_address implementation.
with_attr error_message("NOT_AN_IMPLEMENTATION") {
assert_not_zero(implementation_hash_);
}
let (now_) = get_block_timestamp();
let (upgrade_timelock) = upgrade_delay.read();
let (implementation_key) = calc_impl_key(
implementation_hash_, eic_hash, init_vector_len, init_vector, final
);
impl_activation_time.write(implementation_key, now_ + upgrade_timelock);
implementation_added.emit(
implementation_hash=implementation_hash_,
eic_hash=eic_hash,
init_vector_len=init_vector_len,
init_vector=init_vector,
final=final,
);
return ();
}
@external
func remove_implementation{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
implementation_hash_: felt,
eic_hash: felt,
init_vector_len: felt,
init_vector: felt*,
final: felt,
) {
alloc_locals;
only_governor();
let (implementation_key) = calc_impl_key(
implementation_hash_, eic_hash, init_vector_len, init_vector, final
);
let (time) = impl_activation_time.read(implementation_key);
// Quiet success if activation time is 0 (i.e. not added).
if (time == 0) {
return ();
}
impl_activation_time.write(implementation_key, 0);
implementation_removed.emit(
implementation_hash=implementation_hash_,
eic_hash=eic_hash,
init_vector_len=init_vector_len,
init_vector=init_vector,
final=final,
);
return ();
}
@external
func upgrade_to{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
implementation_hash_: felt,
eic_hash: felt,
init_vector_len: felt,
init_vector: felt*,
final: felt,
) {
// Input is as following:
// 1. implementation_hash - the class hash of the implementation contract.
// 2. eic_hash - The EIC (External Initialization Contract) class hash. Zero if no EIC used.
// 3. init_vector - initialization vector to be passed to the initializer function.
// 4. final - If TRUE - we set the implementation as finalized.
//
// Initialization flow:
// The flow depends on content of eic_hash argument.
// a. If eic_hash == 0:
// The init_vector is passed to the implementation's initialize function when applicable
// (i.e. if not already initialized).
// b. If eic_hash != 0:
// Implementation's initialize function is skipped.
// The init_vector is passed to the eic_initialize function of the EIC.
// The eic_initialize (library_call) is performed, whether the contract is initialized or not
// i.e. EIC can perform initial init, or post init reconfiguration.
alloc_locals;
only_governor();
not_finalized();
let (local now_) = get_block_timestamp();
let (implementation_key) = calc_impl_key(
implementation_hash_, eic_hash, init_vector_len, init_vector, final
);
let (local activation_time) = impl_activation_time.read(implementation_key);
// If activation time == 0
// it means that this implementation & init vector combination was not added.
with_attr error_message("UNKNOWN_IMPLEMENTATION") {
assert_not_zero(activation_time);
}
// If this is the first implementation (i.e. current_impl == 0) - we don't enforce timelock.
let (local current_impl) = implementation();
if (current_impl != 0) {
with_attr error_message("NOT_ENABLED_YET") {
assert_le(activation_time, now_);
}
tempvar range_check_ptr = range_check_ptr;
} else {
tempvar range_check_ptr = range_check_ptr;
}
// Set implementation class_hash.
set_implementation_hash(implementation_hash_);
// We emit now so that finalize emit last (if it does).
implementation_upgraded.emit(
implementation_hash=implementation_hash_,
eic_hash=eic_hash,
init_vector_len=init_vector_len,
init_vector=init_vector,
);
process_final_flag(final_flag=final, implementation_hash_=implementation_hash_);
// Finally - delegate call initialzie/eic_initialize if applicable.
// EIC path - If eic class_hash is not zero.
// Calling eic_initialize and NOT continuing to implementation initializer.
if (eic_hash != 0) {
ExternalInitializer.library_call_eic_initialize(
class_hash=eic_hash, init_vector_len=init_vector_len, init_vector=init_vector
);
return ();
}
// NON-EIC path.
// If not already initialized, call initialize on the implementation.
let (initialized_: felt) = Initializable.library_call_initialized(
class_hash=implementation_hash_
);
if (initialized_ == FALSE) {
Initializable.library_call_initialize(
class_hash=implementation_hash_,
init_vector_len=init_vector_len,
init_vector=init_vector,
);
return ();
} else {
return ();
}
}
@external
func initialize{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
init_vector_len: felt, init_vector: felt*
) {
with_attr error_message("DIRECT_CALL_PROHIBITED") {
assert 0 = 1;
}
return ();
}
func process_final_flag{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
final_flag: felt, implementation_hash_: felt
) {
if (final_flag == FALSE) {
return ();
} else {
finalize();
implementation_finalized.emit(implementation_hash=implementation_hash_);
return ();
}
}
func set_implementation_hash{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
class_hash_: felt
) {
only_governor();
class_hash.write(value=class_hash_);
return ();
}
func calc_impl_key{pedersen_ptr: HashBuiltin*}(
implementation_hash_: felt,
eic_hash: felt,
init_vector_len: felt,
init_vector: felt*,
final: felt,
) -> (res: felt) {
alloc_locals;
let hash_ptr = pedersen_ptr;
with hash_ptr {
let (hash_state_ptr) = hash_init();
let (hash_state_ptr) = hash_update_single(hash_state_ptr, implementation_hash_);
let (hash_state_ptr) = hash_update_single(hash_state_ptr, eic_hash);
let (hash_state_ptr) = hash_update_single(hash_state_ptr, init_vector_len);
let (hash_state_ptr) = hash_update(hash_state_ptr, init_vector, init_vector_len);
let (hash_state_ptr) = hash_update_single(hash_state_ptr, final);
let (res) = hash_finalize(hash_state_ptr);
let pedersen_ptr = hash_ptr;
return (res=res);
}
}