starknet_in_rust 0.4.0

A Rust implementation of Starknet execution logic
Documentation
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
use crate::{
    syscalls::syscall_handler_errors::SyscallHandlerError,
    utils::{get_big_int, get_integer, get_relocatable, Address},
};
use cairo_vm::felt::Felt252;
use cairo_vm::{types::relocatable::Relocatable, vm::vm_core::VirtualMachine};

/// Enum representing different types of deprecated syscall requests
#[derive(Debug, PartialEq)]
pub(crate) enum DeprecatedSyscallRequest {
    EmitEvent(DeprecatedEmitEventRequest),
    GetTxInfo(DeprecatedGetTxInfoRequest),
    Deploy(DeprecatedDeployRequest),
    SendMessageToL1(DeprecatedSendMessageToL1SysCallRequest),
    LibraryCall(DeprecatedLibraryCallRequest),
    GetCallerAddress(DeprecatedGetCallerAddressRequest),
    GetContractAddress(DeprecatedGetContractAddressRequest),
    GetSequencerAddress(DeprecatedGetSequencerAddressRequest),
    GetBlockNumber(DeprecatedGetBlockNumberRequest),
    GetBlockTimestamp(DeprecatedGetBlockTimestampRequest),
    CallContract(DeprecatedCallContractRequest),
    GetTxSignature(DeprecatedGetTxSignatureRequest),
    StorageRead(DeprecatedStorageReadRequest),
    StorageWrite(DeprecatedStorageWriteRequest),
    ReplaceClass(DeprecatedReplaceClassRequest),
}

/// Struct representing the request for a call contract syscall
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct DeprecatedCallContractRequest {
    pub(crate) selector: Felt252,
    pub(crate) contract_address: Address,
    pub(crate) function_selector: Felt252,
    pub(crate) calldata_size: usize,
    pub(crate) calldata: Relocatable,
}

/// Struct representing the request for getting sequencer address
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct DeprecatedGetSequencerAddressRequest {
    _selector: Felt252,
}

/// Struct representing the request to emit an event
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct DeprecatedEmitEventRequest {
    pub(crate) selector: Felt252,
    pub(crate) keys_len: usize,
    pub(crate) keys: Relocatable,
    pub(crate) data_len: usize,
    pub(crate) data: Relocatable,
}

/// Struct representing the request for deployment
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct DeprecatedDeployRequest {
    // The system call selector (= DEPLOY_SELECTOR).
    pub(crate) _selector: Felt252,
    // The hash of the class to deploy.
    pub(crate) class_hash: Felt252,
    // A salt for the new contract address calculation.
    pub(crate) contract_address_salt: Felt252,
    // The size of the calldata for the constructor.
    pub(crate) constructor_calldata_size: Felt252,
    // The calldata for the constructor.
    pub(crate) constructor_calldata: Relocatable,
    // Used for deterministic contract address deployment.
    pub(crate) deploy_from_zero: usize,
}

/// Struct representing a deprecated system call request to send a message to L1.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct DeprecatedSendMessageToL1SysCallRequest {
    pub(crate) _selector: Felt252,
    pub(crate) to_address: Address,
    pub(crate) payload_size: usize,
    pub(crate) payload_ptr: Relocatable,
}

/// Struct representing a deprecated library call request.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct DeprecatedLibraryCallRequest {
    pub(crate) selector: Felt252,
    pub(crate) class_hash: Felt252,
    pub(crate) function_selector: Felt252,
    pub(crate) calldata_size: usize,
    pub(crate) calldata: Relocatable,
}

/// Struct representing a deprecated system call request to get block time stamp request.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct DeprecatedGetBlockTimestampRequest {
    pub(crate) selector: Felt252,
}

/// Struct representing a deprecated system call request to get caller address .
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct DeprecatedGetCallerAddressRequest {
    pub(crate) _selector: Felt252,
}

/// Struct representing a deprecated system call request to get transaction signature.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct DeprecatedGetTxSignatureRequest {
    pub(crate) _selector: Felt252,
}

/// Struct representing a deprecated system call request to get transaction info.
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct DeprecatedGetTxInfoRequest {
    pub(crate) selector: Felt252,
}

/// Struct representing a deprecated system call request to get contract address.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct DeprecatedGetContractAddressRequest {
    pub(crate) _selector: Felt252,
}

/// Struct representing a deprecated system call request to get block number.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct DeprecatedGetBlockNumberRequest {
    pub(crate) _selector: Felt252,
}

/// Describes the StorageRead system call format.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct DeprecatedStorageReadRequest {
    pub(crate) selector: Felt252,
    pub(crate) address: Address,
}

/// Struct representing the StorageWrite system call format.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct DeprecatedStorageWriteRequest {
    pub(crate) selector: Felt252,
    pub(crate) address: Address,
    pub(crate) value: Felt252,
}

/// Struct representing a deprecated system call request to replace class.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct DeprecatedReplaceClassRequest {
    pub(crate) class_hash: Felt252,
}

/// Struct representing a deprecated delegate call request.
#[derive(Clone, Debug, PartialEq)]
pub(crate) struct DeprecatedDelegateCallRequest {
    pub(crate) selector: Felt252,
    pub(crate) contract_address: Address,
    pub(crate) function_selector: Felt252,
    pub(crate) calldata_size: usize,
    pub(crate) calldata: Relocatable,
}

/// Implementation of a converter from different types to  DeprecatedSyscallRequest
impl From<DeprecatedEmitEventRequest> for DeprecatedSyscallRequest {
    fn from(emit_event_struct: DeprecatedEmitEventRequest) -> DeprecatedSyscallRequest {
        DeprecatedSyscallRequest::EmitEvent(emit_event_struct)
    }
}

impl From<DeprecatedDeployRequest> for DeprecatedSyscallRequest {
    fn from(deploy_request_struct: DeprecatedDeployRequest) -> DeprecatedSyscallRequest {
        DeprecatedSyscallRequest::Deploy(deploy_request_struct)
    }
}

impl From<DeprecatedSendMessageToL1SysCallRequest> for DeprecatedSyscallRequest {
    fn from(
        send_message_to_l1_sys_call: DeprecatedSendMessageToL1SysCallRequest,
    ) -> DeprecatedSyscallRequest {
        DeprecatedSyscallRequest::SendMessageToL1(send_message_to_l1_sys_call)
    }
}

impl From<DeprecatedLibraryCallRequest> for DeprecatedSyscallRequest {
    fn from(library_call_struct: DeprecatedLibraryCallRequest) -> DeprecatedSyscallRequest {
        DeprecatedSyscallRequest::LibraryCall(library_call_struct)
    }
}

impl From<DeprecatedCallContractRequest> for DeprecatedSyscallRequest {
    fn from(call_contract_request: DeprecatedCallContractRequest) -> DeprecatedSyscallRequest {
        DeprecatedSyscallRequest::CallContract(call_contract_request)
    }
}

impl From<DeprecatedGetCallerAddressRequest> for DeprecatedSyscallRequest {
    fn from(
        get_caller_address_request: DeprecatedGetCallerAddressRequest,
    ) -> DeprecatedSyscallRequest {
        DeprecatedSyscallRequest::GetCallerAddress(get_caller_address_request)
    }
}

impl From<DeprecatedGetSequencerAddressRequest> for DeprecatedSyscallRequest {
    fn from(
        get_sequencer_address_request: DeprecatedGetSequencerAddressRequest,
    ) -> DeprecatedSyscallRequest {
        DeprecatedSyscallRequest::GetSequencerAddress(get_sequencer_address_request)
    }
}

impl From<DeprecatedGetBlockTimestampRequest> for DeprecatedSyscallRequest {
    fn from(
        get_block_timestamp_request: DeprecatedGetBlockTimestampRequest,
    ) -> DeprecatedSyscallRequest {
        DeprecatedSyscallRequest::GetBlockTimestamp(get_block_timestamp_request)
    }
}

impl From<DeprecatedGetTxSignatureRequest> for DeprecatedSyscallRequest {
    fn from(get_tx_signature_request: DeprecatedGetTxSignatureRequest) -> DeprecatedSyscallRequest {
        DeprecatedSyscallRequest::GetTxSignature(get_tx_signature_request)
    }
}

impl From<DeprecatedGetTxInfoRequest> for DeprecatedSyscallRequest {
    fn from(get_tx_info_request: DeprecatedGetTxInfoRequest) -> DeprecatedSyscallRequest {
        DeprecatedSyscallRequest::GetTxInfo(get_tx_info_request)
    }
}

impl From<DeprecatedStorageReadRequest> for DeprecatedSyscallRequest {
    fn from(storage_read: DeprecatedStorageReadRequest) -> DeprecatedSyscallRequest {
        DeprecatedSyscallRequest::StorageRead(storage_read)
    }
}

impl From<DeprecatedStorageWriteRequest> for DeprecatedSyscallRequest {
    fn from(storage_write: DeprecatedStorageWriteRequest) -> DeprecatedSyscallRequest {
        DeprecatedSyscallRequest::StorageWrite(storage_write)
    }
}

impl From<DeprecatedReplaceClassRequest> for DeprecatedSyscallRequest {
    fn from(replace_class: DeprecatedReplaceClassRequest) -> DeprecatedSyscallRequest {
        DeprecatedSyscallRequest::ReplaceClass(replace_class)
    }
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~
//  FromPtr implementations
// ~~~~~~~~~~~~~~~~~~~~~~~~~

/// This trait provides functionality to convert from a raw pointer
/// to a specific deprecated system call request.
pub(crate) trait DeprecatedFromPtr {
    fn from_ptr(
        vm: &VirtualMachine,
        syscall_ptr: Relocatable,
    ) -> Result<DeprecatedSyscallRequest, SyscallHandlerError>;
}

impl DeprecatedFromPtr for DeprecatedEmitEventRequest {
    fn from_ptr(
        vm: &VirtualMachine,
        syscall_ptr: Relocatable,
    ) -> Result<DeprecatedSyscallRequest, SyscallHandlerError> {
        let selector = get_big_int(vm, syscall_ptr)?;
        let keys_len = get_integer(vm, &syscall_ptr + 1)?;
        let keys = get_relocatable(vm, &syscall_ptr + 2)?;
        let data_len = get_integer(vm, &syscall_ptr + 3)?;
        let data = get_relocatable(vm, &syscall_ptr + 4)?;

        Ok(DeprecatedEmitEventRequest {
            selector,
            keys_len,
            keys,
            data_len,
            data,
        }
        .into())
    }
}

impl DeprecatedFromPtr for DeprecatedGetTxInfoRequest {
    fn from_ptr(
        vm: &VirtualMachine,
        syscall_ptr: Relocatable,
    ) -> Result<DeprecatedSyscallRequest, SyscallHandlerError> {
        let selector = get_big_int(vm, syscall_ptr)?;

        Ok(DeprecatedGetTxInfoRequest { selector }.into())
    }
}

impl DeprecatedFromPtr for DeprecatedLibraryCallRequest {
    fn from_ptr(
        vm: &VirtualMachine,
        syscall_ptr: Relocatable,
    ) -> Result<DeprecatedSyscallRequest, SyscallHandlerError> {
        let selector = get_big_int(vm, syscall_ptr)?;
        let class_hash = get_big_int(vm, &syscall_ptr + 1)?;
        let function_selector = get_big_int(vm, &syscall_ptr + 2)?;
        let calldata_size = get_integer(vm, &syscall_ptr + 3)?;
        let calldata = get_relocatable(vm, &syscall_ptr + 4)?;
        Ok(DeprecatedLibraryCallRequest {
            selector,
            class_hash,
            function_selector,
            calldata_size,
            calldata,
        }
        .into())
    }
}

impl DeprecatedFromPtr for DeprecatedCallContractRequest {
    fn from_ptr(
        vm: &VirtualMachine,
        syscall_ptr: Relocatable,
    ) -> Result<DeprecatedSyscallRequest, SyscallHandlerError> {
        let selector = get_big_int(vm, syscall_ptr)?;
        let contract_address = Address(get_big_int(vm, &syscall_ptr + 1)?);
        let function_selector = get_big_int(vm, &syscall_ptr + 2)?;
        let calldata_size = get_integer(vm, &syscall_ptr + 3)?;
        let calldata = get_relocatable(vm, &syscall_ptr + 4)?;
        Ok(DeprecatedCallContractRequest {
            selector,
            contract_address,
            function_selector,
            calldata_size,
            calldata,
        }
        .into())
    }
}

impl DeprecatedFromPtr for DeprecatedDeployRequest {
    fn from_ptr(
        vm: &VirtualMachine,
        syscall_ptr: Relocatable,
    ) -> Result<DeprecatedSyscallRequest, SyscallHandlerError> {
        // Get syscall parameters from the Virtual Machine
        let _selector = get_big_int(vm, syscall_ptr)?;
        let class_hash = get_big_int(vm, &syscall_ptr + 1)?;
        let contract_address_salt = get_big_int(vm, &syscall_ptr + 2)?;
        let constructor_calldata_size = get_big_int(vm, &syscall_ptr + 3)?;
        let constructor_calldata = get_relocatable(vm, &syscall_ptr + 4)?;
        let deploy_from_zero = get_integer(vm, &syscall_ptr + 5)?;

        Ok(DeprecatedSyscallRequest::Deploy(DeprecatedDeployRequest {
            _selector,
            class_hash,
            contract_address_salt,
            constructor_calldata_size,
            constructor_calldata,
            deploy_from_zero,
        }))
    }
}

impl DeprecatedFromPtr for DeprecatedSendMessageToL1SysCallRequest {
    fn from_ptr(
        vm: &VirtualMachine,
        syscall_ptr: Relocatable,
    ) -> Result<DeprecatedSyscallRequest, SyscallHandlerError> {
        let _selector = get_big_int(vm, syscall_ptr)?;
        let to_address = Address(get_big_int(vm, &syscall_ptr + 1)?);
        let payload_size = get_integer(vm, &syscall_ptr + 2)?;
        let payload_ptr = get_relocatable(vm, &syscall_ptr + 3)?;

        Ok(DeprecatedSyscallRequest::SendMessageToL1(
            DeprecatedSendMessageToL1SysCallRequest {
                _selector,
                to_address,
                payload_size,
                payload_ptr,
            },
        ))
    }
}

impl DeprecatedFromPtr for DeprecatedGetCallerAddressRequest {
    fn from_ptr(
        vm: &VirtualMachine,
        syscall_ptr: Relocatable,
    ) -> Result<DeprecatedSyscallRequest, SyscallHandlerError> {
        let _selector = get_big_int(vm, syscall_ptr)?;

        Ok(DeprecatedSyscallRequest::GetCallerAddress(
            DeprecatedGetCallerAddressRequest { _selector },
        ))
    }
}

impl DeprecatedFromPtr for DeprecatedGetBlockTimestampRequest {
    fn from_ptr(
        vm: &VirtualMachine,
        syscall_ptr: Relocatable,
    ) -> Result<DeprecatedSyscallRequest, SyscallHandlerError> {
        let selector = get_big_int(vm, syscall_ptr)?;
        Ok(DeprecatedSyscallRequest::GetBlockTimestamp(
            DeprecatedGetBlockTimestampRequest { selector },
        ))
    }
}

impl DeprecatedFromPtr for DeprecatedGetSequencerAddressRequest {
    fn from_ptr(
        vm: &VirtualMachine,
        syscall_ptr: Relocatable,
    ) -> Result<DeprecatedSyscallRequest, SyscallHandlerError> {
        let _selector = get_big_int(vm, syscall_ptr)?;
        Ok(DeprecatedSyscallRequest::GetSequencerAddress(
            DeprecatedGetSequencerAddressRequest { _selector },
        ))
    }
}

impl DeprecatedFromPtr for DeprecatedGetTxSignatureRequest {
    fn from_ptr(
        vm: &VirtualMachine,
        syscall_ptr: Relocatable,
    ) -> Result<DeprecatedSyscallRequest, SyscallHandlerError> {
        let _selector = get_big_int(vm, syscall_ptr)?;
        Ok(DeprecatedSyscallRequest::GetTxSignature(
            DeprecatedGetTxSignatureRequest { _selector },
        ))
    }
}

impl DeprecatedFromPtr for DeprecatedGetBlockNumberRequest {
    fn from_ptr(
        vm: &VirtualMachine,
        syscall_ptr: Relocatable,
    ) -> Result<DeprecatedSyscallRequest, SyscallHandlerError> {
        let _selector = get_big_int(vm, syscall_ptr)?;
        Ok(DeprecatedSyscallRequest::GetBlockNumber(
            DeprecatedGetBlockNumberRequest { _selector },
        ))
    }
}

impl DeprecatedFromPtr for DeprecatedGetContractAddressRequest {
    fn from_ptr(
        vm: &VirtualMachine,
        syscall_ptr: Relocatable,
    ) -> Result<DeprecatedSyscallRequest, SyscallHandlerError> {
        let _selector = get_big_int(vm, syscall_ptr)?;

        Ok(DeprecatedSyscallRequest::GetContractAddress(
            DeprecatedGetContractAddressRequest { _selector },
        ))
    }
}

impl DeprecatedFromPtr for DeprecatedStorageReadRequest {
    fn from_ptr(
        vm: &VirtualMachine,
        syscall_ptr: Relocatable,
    ) -> Result<DeprecatedSyscallRequest, SyscallHandlerError> {
        let selector = get_big_int(vm, syscall_ptr)?;
        let address = Address(get_big_int(vm, (syscall_ptr + 1)?)?);

        Ok(DeprecatedSyscallRequest::StorageRead(
            DeprecatedStorageReadRequest { selector, address },
        ))
    }
}

impl DeprecatedFromPtr for DeprecatedStorageWriteRequest {
    fn from_ptr(
        vm: &VirtualMachine,
        syscall_ptr: Relocatable,
    ) -> Result<DeprecatedSyscallRequest, SyscallHandlerError> {
        let selector = get_big_int(vm, syscall_ptr)?;
        let address = Address(get_big_int(vm, (syscall_ptr + 1)?)?);
        let value = get_big_int(vm, (syscall_ptr + 2)?)?;

        Ok(DeprecatedSyscallRequest::StorageWrite(
            DeprecatedStorageWriteRequest {
                selector,
                address,
                value,
            },
        ))
    }
}

impl DeprecatedFromPtr for DeprecatedReplaceClassRequest {
    fn from_ptr(
        vm: &VirtualMachine,
        syscall_ptr: Relocatable,
    ) -> Result<DeprecatedSyscallRequest, SyscallHandlerError> {
        // memory[syscall_ptr] contains the selector, so we fetch the next memory cell
        let class_hash = get_big_int(vm, (syscall_ptr + 1)?)?;

        Ok(DeprecatedSyscallRequest::ReplaceClass(
            DeprecatedReplaceClassRequest { class_hash },
        ))
    }
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//  CountFields implementations
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/// This trait provides functionality to count the number of fields
/// in the struct implementing it.

pub(crate) trait CountFields {
    /// Returns the amount of fields of a struct
    fn count_fields() -> usize;
}

impl CountFields for DeprecatedGetCallerAddressRequest {
    fn count_fields() -> usize {
        1
    }
}

impl CountFields for DeprecatedGetSequencerAddressRequest {
    fn count_fields() -> usize {
        1
    }
}

impl CountFields for DeprecatedGetBlockTimestampRequest {
    fn count_fields() -> usize {
        1
    }
}

impl CountFields for DeprecatedGetTxSignatureRequest {
    fn count_fields() -> usize {
        1
    }
}

impl CountFields for DeprecatedGetBlockNumberRequest {
    fn count_fields() -> usize {
        1
    }
}

impl CountFields for DeprecatedGetContractAddressRequest {
    fn count_fields() -> usize {
        1
    }
}

impl CountFields for DeprecatedGetTxInfoRequest {
    fn count_fields() -> usize {
        1
    }
}

impl CountFields for DeprecatedStorageReadRequest {
    fn count_fields() -> usize {
        2
    }
}

impl CountFields for DeprecatedCallContractRequest {
    fn count_fields() -> usize {
        5
    }
}

impl CountFields for DeprecatedDeployRequest {
    fn count_fields() -> usize {
        6
    }
}