-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcomponent.cairo
384 lines (336 loc) · 13.2 KB
/
component.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
//! SPDX-License-Identifier: MIT
//!
//! Appchain - Starknet messaging component.
//!
//! Messaging between the appchain and Starknet works very similar
//! to Starknet - Ethereum messaging.
//!
//! # Starknet to Appchain message
//!
//! To send a message from Starknet to the Appchain, the current contract
//! emits an event. This event is then retrived by the Appchain sequencer
//! to execute the `l1_handler` function of the target contract.
//!
//! # Appchain to Starknet message
//!
//! To send a message from the Appchain to Starknet, the Appchain sequencer
//! gathers all the messages created by cairo contracts using the `send_message_to_l1`
//! syscall. Those messages are then sent to Starknet as part of the state update once
//! the proof is available.
//! Once taken in account during the state update, the message can be be consummed
//! on Starknet.
//!
//! As the messages are part of the state update which depends on the proof,
//! the messaging system between Appchain and Starknet is also asynchronous and
//! asymmetric as the Starknet - Ethereum messaging.
/// Errors.
mod errors {
const INVALID_NONCE: felt252 = 'INVALID_NONCE';
const INVALID_MESSAGE_TO_CONSUME: felt252 = 'INVALID_MESSAGE_TO_CONSUME';
const INVALID_MESSAGE_TO_SEAL: felt252 = 'INVALID_MESSAGE_TO_SEAL';
const NO_MESSAGE_TO_CANCEL: felt252 = 'NO_MESSAGE_TO_CANCEL';
const CANCELLATION_NOT_REQUESTED: felt252 = 'CANCELLATION_NOT_REQUESTED';
const CANCELLATION_NOT_ALLOWED_YET: felt252 = 'CANCELLATION_NOT_ALLOWED_YET';
const CANCEL_ALLOWED_TIME_OVERFLOW: felt252 = 'CANCEL_ALLOWED_TIME_OVERFLOW';
}
/// Messaging component.
///
/// Depends on `ownable` to ensure the configuration is
/// only editable by contract's owner.
#[starknet::component]
mod messaging_cpt {
use core::zeroable::Zeroable;
use openzeppelin::access::ownable::{
OwnableComponent as ownable_cpt, OwnableComponent::InternalTrait as OwnableInternal,
interface::IOwnable,
};
use piltover::messaging::{
hash, interface::IMessaging, output_process::{MessageToStarknet, MessageToAppchain},
types::{MessageToAppchainStatus, MessageToStarknetStatus, MessageHash, Nonce}
};
use starknet::ContractAddress;
use starknet::storage::Map;
use super::errors;
#[storage]
struct Storage {
/// Cancellation delay in seconds for message from Starknet to Appchain.
cancellation_delay_secs: u64,
/// Ledger of messages from Starknet to Appchain that are being cancelled.
sn_to_appc_cancellations: Map::<MessageHash, u64>,
/// The nonce for messages sent to the Appchain from Starknet.
sn_to_appc_nonce: Nonce,
/// Ledger of messages hashes sent from Starknet to the appchain.
sn_to_appc_messages: Map::<MessageHash, Nonce>,
/// Ledger of messages hashes registered from the Appchain and a refcount
/// associated to it to control messages consumption.
appc_to_sn_messages: Map::<MessageHash, felt252>,
}
#[event]
#[derive(Drop, starknet::Event)]
enum Event {
MessageSent: MessageSent,
MessageConsumed: MessageConsumed,
MessageCancellationStarted: MessageCancellationStarted,
MessageCanceled: MessageCanceled,
MessageToStarknetReceived: MessageToStarknetReceived,
MessageToAppchainSealed: MessageToAppchainSealed,
}
#[derive(Drop, starknet::Event)]
struct MessageSent {
#[key]
message_hash: MessageHash,
#[key]
from: ContractAddress,
#[key]
to: ContractAddress,
selector: felt252,
nonce: Nonce,
payload: Span<felt252>,
}
#[derive(Drop, starknet::Event)]
struct MessageConsumed {
#[key]
message_hash: MessageHash,
#[key]
from: ContractAddress,
#[key]
to: ContractAddress,
payload: Span<felt252>,
}
#[derive(Drop, starknet::Event)]
struct MessageCancellationStarted {
#[key]
message_hash: MessageHash,
#[key]
from: ContractAddress,
#[key]
to: ContractAddress,
selector: felt252,
payload: Span<felt252>,
nonce: Nonce,
}
#[derive(Drop, starknet::Event)]
struct MessageCanceled {
#[key]
message_hash: MessageHash,
#[key]
from: ContractAddress,
#[key]
to: ContractAddress,
selector: felt252,
payload: Span<felt252>,
nonce: Nonce,
}
#[derive(Drop, starknet::Event)]
struct MessageToStarknetReceived {
#[key]
message_hash: MessageHash,
#[key]
from: ContractAddress,
#[key]
to: ContractAddress,
payload: Span<felt252>,
}
#[derive(Drop, starknet::Event)]
struct MessageToAppchainSealed {
#[key]
message_hash: MessageHash,
#[key]
from: ContractAddress,
#[key]
to: ContractAddress,
selector: felt252,
nonce: Nonce,
payload: Span<felt252>,
}
#[embeddable_as(MessagingImpl)]
impl Messaging<
TContractState, +HasComponent<TContractState>
> of IMessaging<ComponentState<TContractState>> {
fn send_message_to_appchain(
ref self: ComponentState<TContractState>,
to_address: ContractAddress,
selector: felt252,
payload: Span<felt252>
) -> (MessageHash, Nonce) {
// Starts by +1 to avoid 0 as a valid nonce.
let nonce = self.sn_to_appc_nonce.read() + 1;
self.sn_to_appc_nonce.write(nonce);
let from = starknet::get_caller_address();
let message_hash = hash::compute_message_hash_sn_to_appc(
from, to_address, selector, payload, nonce
);
self
.emit(
MessageSent { message_hash, from, to: to_address, selector, nonce, payload, }
);
self.sn_to_appc_messages.write(message_hash, nonce);
(message_hash, nonce)
}
fn sn_to_appchain_messages(
self: @ComponentState<TContractState>, message_hash: MessageHash
) -> MessageToAppchainStatus {
let nonce = self.sn_to_appc_messages.read(message_hash);
if nonce == 0 {
return MessageToAppchainStatus::SealedOrNotSent;
}
return MessageToAppchainStatus::Pending(nonce);
}
fn appchain_to_sn_messages(
self: @ComponentState<TContractState>, message_hash: MessageHash
) -> MessageToStarknetStatus {
let message_count = self.appc_to_sn_messages.read(message_hash);
if (message_count == 0) {
return MessageToStarknetStatus::NothingToConsume;
}
return MessageToStarknetStatus::ReadyToConsume(message_count);
}
fn consume_message_from_appchain(
ref self: ComponentState<TContractState>,
from_address: ContractAddress,
payload: Span<felt252>
) -> MessageHash {
let to_address = starknet::get_caller_address();
let message_hash = hash::compute_message_hash_appc_to_sn(
from_address, to_address, payload
);
let count = self.appc_to_sn_messages.read(message_hash);
assert(count.is_non_zero(), errors::INVALID_MESSAGE_TO_CONSUME);
self
.emit(
MessageConsumed { message_hash, from: from_address, to: to_address, payload, }
);
self.appc_to_sn_messages.write(message_hash, count - 1);
message_hash
}
fn start_message_cancellation(
ref self: ComponentState<TContractState>,
to_address: ContractAddress,
selector: felt252,
payload: Span<felt252>,
nonce: Nonce,
) -> MessageHash {
assert(nonce.is_non_zero(), errors::INVALID_NONCE);
let from = starknet::get_caller_address();
let message_hash = hash::compute_message_hash_sn_to_appc(
from, to_address, selector, payload, nonce
);
assert(
self.sn_to_appc_messages.read(message_hash) == nonce, errors::NO_MESSAGE_TO_CANCEL
);
self.sn_to_appc_cancellations.write(message_hash, starknet::get_block_timestamp());
self
.emit(
MessageCancellationStarted {
message_hash, from, to: to_address, selector, payload, nonce
}
);
return message_hash;
}
fn cancel_message(
ref self: ComponentState<TContractState>,
to_address: ContractAddress,
selector: felt252,
payload: Span<felt252>,
nonce: felt252,
) -> MessageHash {
let from = starknet::get_caller_address();
let message_hash = hash::compute_message_hash_sn_to_appc(
from, to_address, selector, payload, nonce
);
assert(
self.sn_to_appc_messages.read(message_hash) == nonce, errors::NO_MESSAGE_TO_CANCEL
);
let request_time = self.sn_to_appc_cancellations.read(message_hash);
assert(request_time.is_non_zero(), errors::CANCELLATION_NOT_REQUESTED);
let cancel_allowed_time = request_time + self.cancellation_delay_secs.read();
assert(cancel_allowed_time >= request_time, errors::CANCEL_ALLOWED_TIME_OVERFLOW);
assert(
starknet::get_block_timestamp() >= cancel_allowed_time,
errors::CANCELLATION_NOT_ALLOWED_YET
);
self
.emit(
MessageCanceled { message_hash, from, to: to_address, selector, payload, nonce }
);
// Once canceled, no more operation can be done on this message.
self.sn_to_appc_messages.write(message_hash, 0);
return message_hash;
}
}
#[generate_trait]
impl InternalImpl<
TContractState, +HasComponent<TContractState>
> of InternalTrait<TContractState> {
/// Initializes the messaging component.
///
/// # Arguments
///
/// * `cancellation_delay_secs` - The delay in seconds for message cancellation window.
fn initialize(ref self: ComponentState<TContractState>, cancellation_delay_secs: u64) {
self.cancellation_delay_secs.write(cancellation_delay_secs);
}
/// Processes the messages to Starknet from StarknetOS output.
/// Once processed, messages are ready to be consumed using
/// `consume_message_from_appchain` entry point.
///
/// # Arguments
///
/// * `messages` - The messages to Starknet.
fn process_messages_to_starknet(
ref self: ComponentState<TContractState>, messages: Span<MessageToStarknet>,
) {
let mut messages = messages;
loop {
match messages.pop_front() {
Option::Some(m) => {
let from = *m.from_address;
let to = *m.to_address;
let payload = *m.payload;
let message_hash = hash::compute_message_hash_appc_to_sn(from, to, payload);
self.emit(MessageToStarknetReceived { message_hash, from, to, payload });
let ref_count = self.appc_to_sn_messages.read(message_hash);
self.appc_to_sn_messages.write(message_hash, ref_count + 1);
},
Option::None => { break; },
};
};
}
/// Processes the messages to Appchain from StarknetOS output.
///
/// # Arguments
///
/// * `messages` - The messages to Appchain.
fn process_messages_to_appchain(
ref self: ComponentState<TContractState>, messages: Span<MessageToAppchain>,
) {
let mut messages = messages;
loop {
match messages.pop_front() {
Option::Some(m) => {
let from = *m.from_address;
let to = *m.to_address;
let payload = *m.payload;
let selector = *m.selector;
let nonce = *m.nonce;
let message_hash = hash::compute_message_hash_sn_to_appc(
from, to, selector, payload, nonce
);
assert(
self.sn_to_appc_messages.read(message_hash).is_non_zero(),
errors::INVALID_MESSAGE_TO_SEAL
);
self.sn_to_appc_messages.write(message_hash, 0);
self
.emit(
MessageToAppchainSealed {
message_hash, from, to, selector, payload, nonce
}
);
},
Option::None => { break; },
};
};
}
}
}