This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
/
mod.rs
732 lines (622 loc) · 23.2 KB
/
mod.rs
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
use cairo_felt::Felt252;
use cairo_vm::types::relocatable::Relocatable;
use cairo_vm::vm::vm_core::VirtualMachine;
use serde::Deserialize;
use starknet_api::block::{BlockNumber, BlockTimestamp};
use starknet_api::core::{
calculate_contract_address, ClassHash, ContractAddress, EntryPointSelector, EthAddress,
};
use starknet_api::deprecated_contract_class::EntryPointType;
use starknet_api::hash::StarkFelt;
use starknet_api::state::StorageKey;
use starknet_api::transaction::{
Calldata, ContractAddressSalt, EventContent, EventData, EventKey, L2ToL1Payload,
};
use strum_macros::EnumIter;
use self::hint_processor::{
execute_inner_call, execute_library_call, felt_to_bool, read_call_params, read_calldata,
read_felt_array, DeprecatedSyscallExecutionError, DeprecatedSyscallHintProcessor,
};
use crate::abi::constants;
use crate::execution::call_info::{MessageToL1, OrderedEvent, OrderedL2ToL1Message};
use crate::execution::entry_point::{CallEntryPoint, CallType, ConstructorContext};
use crate::execution::execution_utils::{
execute_deployment, stark_felt_from_ptr, write_maybe_relocatable, write_stark_felt,
ReadOnlySegment,
};
#[cfg(test)]
#[path = "deprecated_syscalls_test.rs"]
pub mod deprecated_syscalls_test;
pub mod hint_processor;
pub type DeprecatedSyscallResult<T> = Result<T, DeprecatedSyscallExecutionError>;
pub type WriteResponseResult = DeprecatedSyscallResult<()>;
#[derive(Clone, Copy, Debug, Deserialize, EnumIter, Eq, Hash, PartialEq)]
pub enum DeprecatedSyscallSelector {
CallContract,
DelegateCall,
DelegateL1Handler,
Deploy,
EmitEvent,
GetBlockHash,
GetBlockNumber,
GetBlockTimestamp,
GetCallerAddress,
GetContractAddress,
GetExecutionInfo,
GetSequencerAddress,
GetTxInfo,
GetTxSignature,
Keccak,
LibraryCall,
LibraryCallL1Handler,
ReplaceClass,
Secp256k1Add,
Secp256k1GetPointFromX,
Secp256k1GetXy,
Secp256k1Mul,
Secp256k1New,
Secp256r1Add,
Secp256r1GetPointFromX,
Secp256r1GetXy,
Secp256r1Mul,
Secp256r1New,
SendMessageToL1,
StorageRead,
StorageWrite,
}
impl TryFrom<StarkFelt> for DeprecatedSyscallSelector {
type Error = DeprecatedSyscallExecutionError;
fn try_from(raw_selector: StarkFelt) -> Result<Self, Self::Error> {
// Remove leading zero bytes from selector.
let selector_bytes = raw_selector.bytes();
let first_non_zero = selector_bytes.iter().position(|&byte| byte != b'\0').unwrap_or(32);
match &selector_bytes[first_non_zero..] {
b"CallContract" => Ok(Self::CallContract),
b"DelegateCall" => Ok(Self::DelegateCall),
b"DelegateL1Handler" => Ok(Self::DelegateL1Handler),
b"Deploy" => Ok(Self::Deploy),
b"EmitEvent" => Ok(Self::EmitEvent),
b"GetBlockHash" => Ok(Self::GetBlockHash),
b"GetBlockNumber" => Ok(Self::GetBlockNumber),
b"GetBlockTimestamp" => Ok(Self::GetBlockTimestamp),
b"GetCallerAddress" => Ok(Self::GetCallerAddress),
b"GetContractAddress" => Ok(Self::GetContractAddress),
b"GetExecutionInfo" => Ok(Self::GetExecutionInfo),
b"GetSequencerAddress" => Ok(Self::GetSequencerAddress),
b"GetTxInfo" => Ok(Self::GetTxInfo),
b"GetTxSignature" => Ok(Self::GetTxSignature),
b"Keccak" => Ok(Self::Keccak),
b"LibraryCall" => Ok(Self::LibraryCall),
b"LibraryCallL1Handler" => Ok(Self::LibraryCallL1Handler),
b"ReplaceClass" => Ok(Self::ReplaceClass),
b"Secp256k1Add" => Ok(Self::Secp256k1Add),
b"Secp256k1GetPointFromX" => Ok(Self::Secp256k1GetPointFromX),
b"Secp256k1GetXy" => Ok(Self::Secp256k1GetXy),
b"Secp256k1Mul" => Ok(Self::Secp256k1Mul),
b"Secp256k1New" => Ok(Self::Secp256k1New),
b"Secp256r1Add" => Ok(Self::Secp256r1Add),
b"Secp256r1GetPointFromX" => Ok(Self::Secp256r1GetPointFromX),
b"Secp256r1GetXy" => Ok(Self::Secp256r1GetXy),
b"Secp256r1Mul" => Ok(Self::Secp256r1Mul),
b"Secp256r1New" => Ok(Self::Secp256r1New),
b"SendMessageToL1" => Ok(Self::SendMessageToL1),
b"StorageRead" => Ok(Self::StorageRead),
b"StorageWrite" => Ok(Self::StorageWrite),
_ => {
Err(DeprecatedSyscallExecutionError::InvalidDeprecatedSyscallSelector(raw_selector))
}
}
}
}
pub trait SyscallRequest: Sized {
fn read(_vm: &VirtualMachine, _ptr: &mut Relocatable) -> DeprecatedSyscallResult<Self>;
}
pub trait SyscallResponse {
fn write(self, _vm: &mut VirtualMachine, _ptr: &mut Relocatable) -> WriteResponseResult;
}
// Common structs.
#[derive(Debug, Eq, PartialEq)]
pub struct EmptyRequest;
impl SyscallRequest for EmptyRequest {
fn read(_vm: &VirtualMachine, _ptr: &mut Relocatable) -> DeprecatedSyscallResult<EmptyRequest> {
Ok(EmptyRequest)
}
}
#[derive(Debug, Eq, PartialEq)]
pub struct EmptyResponse;
impl SyscallResponse for EmptyResponse {
fn write(self, _vm: &mut VirtualMachine, _ptr: &mut Relocatable) -> WriteResponseResult {
Ok(())
}
}
#[derive(Debug)]
pub struct SingleSegmentResponse {
segment: ReadOnlySegment,
}
impl SyscallResponse for SingleSegmentResponse {
fn write(self, vm: &mut VirtualMachine, ptr: &mut Relocatable) -> WriteResponseResult {
write_maybe_relocatable(vm, ptr, self.segment.length)?;
write_maybe_relocatable(vm, ptr, self.segment.start_ptr)?;
Ok(())
}
}
// CallContract syscall.
#[derive(Debug, Eq, PartialEq)]
pub struct CallContractRequest {
pub contract_address: ContractAddress,
pub function_selector: EntryPointSelector,
pub calldata: Calldata,
}
impl SyscallRequest for CallContractRequest {
fn read(
vm: &VirtualMachine,
ptr: &mut Relocatable,
) -> DeprecatedSyscallResult<CallContractRequest> {
let contract_address = ContractAddress::try_from(stark_felt_from_ptr(vm, ptr)?)?;
let (function_selector, calldata) = read_call_params(vm, ptr)?;
Ok(CallContractRequest { contract_address, function_selector, calldata })
}
}
pub type CallContractResponse = SingleSegmentResponse;
pub fn call_contract(
request: CallContractRequest,
vm: &mut VirtualMachine,
syscall_handler: &mut DeprecatedSyscallHintProcessor<'_>,
) -> DeprecatedSyscallResult<CallContractResponse> {
let storage_address = request.contract_address;
// Check that the call is legal if in Validate execution mode.
if syscall_handler.is_validate_mode() && syscall_handler.storage_address != storage_address {
return Err(DeprecatedSyscallExecutionError::InvalidSyscallInExecutionMode {
syscall_name: "call_contract".to_string(),
execution_mode: syscall_handler.execution_mode(),
});
}
let entry_point = CallEntryPoint {
class_hash: None,
code_address: Some(storage_address),
entry_point_type: EntryPointType::External,
entry_point_selector: request.function_selector,
calldata: request.calldata,
storage_address,
caller_address: syscall_handler.storage_address,
call_type: CallType::Call,
initial_gas: constants::INITIAL_GAS_COST,
};
let retdata_segment = execute_inner_call(entry_point, vm, syscall_handler)?;
Ok(CallContractResponse { segment: retdata_segment })
}
// DelegateCall syscall.
type DelegateCallRequest = CallContractRequest;
type DelegateCallResponse = CallContractResponse;
pub fn delegate_call(
request: DelegateCallRequest,
vm: &mut VirtualMachine,
syscall_handler: &mut DeprecatedSyscallHintProcessor<'_>,
) -> DeprecatedSyscallResult<DelegateCallResponse> {
let call_to_external = true;
let storage_address = request.contract_address;
let class_hash = syscall_handler.state.get_class_hash_at(storage_address)?;
let retdata_segment = execute_library_call(
syscall_handler,
vm,
class_hash,
Some(storage_address),
call_to_external,
request.function_selector,
request.calldata,
)?;
Ok(DelegateCallResponse { segment: retdata_segment })
}
// DelegateCallL1Handler syscall.
pub fn delegate_l1_handler(
request: DelegateCallRequest,
vm: &mut VirtualMachine,
syscall_handler: &mut DeprecatedSyscallHintProcessor<'_>,
) -> DeprecatedSyscallResult<DelegateCallResponse> {
let call_to_external = false;
let storage_address = request.contract_address;
let class_hash = syscall_handler.state.get_class_hash_at(storage_address)?;
let retdata_segment = execute_library_call(
syscall_handler,
vm,
class_hash,
Some(storage_address),
call_to_external,
request.function_selector,
request.calldata,
)?;
Ok(DelegateCallResponse { segment: retdata_segment })
}
// Deploy syscall.
#[derive(Debug, Eq, PartialEq)]
pub struct DeployRequest {
pub class_hash: ClassHash,
pub contract_address_salt: ContractAddressSalt,
pub constructor_calldata: Calldata,
pub deploy_from_zero: bool,
}
impl SyscallRequest for DeployRequest {
fn read(vm: &VirtualMachine, ptr: &mut Relocatable) -> DeprecatedSyscallResult<DeployRequest> {
let class_hash = ClassHash(stark_felt_from_ptr(vm, ptr)?);
let contract_address_salt = ContractAddressSalt(stark_felt_from_ptr(vm, ptr)?);
let constructor_calldata = read_calldata(vm, ptr)?;
let deploy_from_zero = stark_felt_from_ptr(vm, ptr)?;
Ok(DeployRequest {
class_hash,
contract_address_salt,
constructor_calldata,
deploy_from_zero: felt_to_bool(deploy_from_zero)?,
})
}
}
#[derive(Debug, Eq, PartialEq)]
pub struct DeployResponse {
pub contract_address: ContractAddress,
}
impl SyscallResponse for DeployResponse {
// The Cairo struct contains: `contract_address`, `constructor_retdata_size`,
// `constructor_retdata`.
// Nonempty constructor retdata is currently not supported.
fn write(self, vm: &mut VirtualMachine, ptr: &mut Relocatable) -> WriteResponseResult {
write_stark_felt(vm, ptr, *self.contract_address.0.key())?;
write_maybe_relocatable(vm, ptr, 0)?;
write_maybe_relocatable(vm, ptr, 0)?;
Ok(())
}
}
pub fn deploy(
request: DeployRequest,
_vm: &mut VirtualMachine,
syscall_handler: &mut DeprecatedSyscallHintProcessor<'_>,
) -> DeprecatedSyscallResult<DeployResponse> {
let deployer_address = syscall_handler.storage_address;
let deployer_address_for_calculation = match request.deploy_from_zero {
true => ContractAddress::default(),
false => deployer_address,
};
let deployed_contract_address = calculate_contract_address(
request.contract_address_salt,
request.class_hash,
&request.constructor_calldata,
deployer_address_for_calculation,
)?;
let ctor_context = ConstructorContext {
class_hash: request.class_hash,
code_address: Some(deployed_contract_address),
storage_address: deployed_contract_address,
caller_address: deployer_address,
};
let call_info = execute_deployment(
syscall_handler.state,
syscall_handler.resources,
syscall_handler.context,
ctor_context,
request.constructor_calldata,
constants::INITIAL_GAS_COST,
)?;
syscall_handler.inner_calls.push(call_info);
Ok(DeployResponse { contract_address: deployed_contract_address })
}
// EmitEvent syscall.
#[derive(Debug, Eq, PartialEq)]
pub struct EmitEventRequest {
pub content: EventContent,
}
impl SyscallRequest for EmitEventRequest {
// The Cairo struct contains: `keys_len`, `keys`, `data_len`, `data`·
fn read(
vm: &VirtualMachine,
ptr: &mut Relocatable,
) -> DeprecatedSyscallResult<EmitEventRequest> {
let keys = read_felt_array::<DeprecatedSyscallExecutionError>(vm, ptr)?
.into_iter()
.map(EventKey)
.collect();
let data = EventData(read_felt_array::<DeprecatedSyscallExecutionError>(vm, ptr)?);
Ok(EmitEventRequest { content: EventContent { keys, data } })
}
}
type EmitEventResponse = EmptyResponse;
pub fn emit_event(
request: EmitEventRequest,
_vm: &mut VirtualMachine,
syscall_handler: &mut DeprecatedSyscallHintProcessor<'_>,
) -> DeprecatedSyscallResult<EmitEventResponse> {
let execution_context = &mut syscall_handler.context;
let ordered_event =
OrderedEvent { order: execution_context.n_emitted_events, event: request.content };
syscall_handler.events.push(ordered_event);
execution_context.n_emitted_events += 1;
Ok(EmitEventResponse {})
}
// GetBlockNumber syscall.
type GetBlockNumberRequest = EmptyRequest;
#[derive(Debug, Eq, PartialEq)]
pub struct GetBlockNumberResponse {
pub block_number: BlockNumber,
}
impl SyscallResponse for GetBlockNumberResponse {
fn write(self, vm: &mut VirtualMachine, ptr: &mut Relocatable) -> WriteResponseResult {
write_maybe_relocatable(vm, ptr, Felt252::from(self.block_number.0))?;
Ok(())
}
}
pub fn get_block_number(
_request: GetBlockNumberRequest,
_vm: &mut VirtualMachine,
syscall_handler: &mut DeprecatedSyscallHintProcessor<'_>,
) -> DeprecatedSyscallResult<GetBlockNumberResponse> {
// TODO(Yoni, 1/5/2024): disable for validate.
Ok(GetBlockNumberResponse {
block_number: syscall_handler.context.tx_context.block_context.block_info.block_number,
})
}
// GetBlockTimestamp syscall.
type GetBlockTimestampRequest = EmptyRequest;
#[derive(Debug, Eq, PartialEq)]
pub struct GetBlockTimestampResponse {
pub block_timestamp: BlockTimestamp,
}
impl SyscallResponse for GetBlockTimestampResponse {
fn write(self, vm: &mut VirtualMachine, ptr: &mut Relocatable) -> WriteResponseResult {
write_maybe_relocatable(vm, ptr, Felt252::from(self.block_timestamp.0))?;
Ok(())
}
}
pub fn get_block_timestamp(
_request: GetBlockTimestampRequest,
_vm: &mut VirtualMachine,
syscall_handler: &mut DeprecatedSyscallHintProcessor<'_>,
) -> DeprecatedSyscallResult<GetBlockTimestampResponse> {
// TODO(Yoni, 1/5/2024): disable for validate.
Ok(GetBlockTimestampResponse {
block_timestamp: syscall_handler
.context
.tx_context
.block_context
.block_info
.block_timestamp,
})
}
// GetCallerAddress syscall.
type GetCallerAddressRequest = EmptyRequest;
type GetCallerAddressResponse = GetContractAddressResponse;
pub fn get_caller_address(
_request: GetCallerAddressRequest,
_vm: &mut VirtualMachine,
syscall_handler: &mut DeprecatedSyscallHintProcessor<'_>,
) -> DeprecatedSyscallResult<GetCallerAddressResponse> {
Ok(GetCallerAddressResponse { address: syscall_handler.caller_address })
}
// GetContractAddress syscall.
type GetContractAddressRequest = EmptyRequest;
#[derive(Debug, Eq, PartialEq)]
pub struct GetContractAddressResponse {
pub address: ContractAddress,
}
impl SyscallResponse for GetContractAddressResponse {
fn write(self, vm: &mut VirtualMachine, ptr: &mut Relocatable) -> WriteResponseResult {
write_stark_felt(vm, ptr, *self.address.0.key())?;
Ok(())
}
}
pub fn get_contract_address(
_request: GetContractAddressRequest,
_vm: &mut VirtualMachine,
syscall_handler: &mut DeprecatedSyscallHintProcessor<'_>,
) -> DeprecatedSyscallResult<GetContractAddressResponse> {
Ok(GetContractAddressResponse { address: syscall_handler.storage_address })
}
// GetSequencerAddress syscall.
type GetSequencerAddressRequest = EmptyRequest;
type GetSequencerAddressResponse = GetContractAddressResponse;
pub fn get_sequencer_address(
_request: GetSequencerAddressRequest,
_vm: &mut VirtualMachine,
syscall_handler: &mut DeprecatedSyscallHintProcessor<'_>,
) -> DeprecatedSyscallResult<GetSequencerAddressResponse> {
syscall_handler.verify_not_in_validate_mode("get_sequencer_address")?;
Ok(GetSequencerAddressResponse {
address: syscall_handler.context.tx_context.block_context.block_info.sequencer_address,
})
}
// GetTxInfo syscall.
type GetTxInfoRequest = EmptyRequest;
#[derive(Debug, Eq, PartialEq)]
pub struct GetTxInfoResponse {
pub tx_info_start_ptr: Relocatable,
}
impl SyscallResponse for GetTxInfoResponse {
fn write(self, vm: &mut VirtualMachine, ptr: &mut Relocatable) -> WriteResponseResult {
write_maybe_relocatable(vm, ptr, self.tx_info_start_ptr)?;
Ok(())
}
}
pub fn get_tx_info(
_request: GetTxInfoRequest,
vm: &mut VirtualMachine,
syscall_handler: &mut DeprecatedSyscallHintProcessor<'_>,
) -> DeprecatedSyscallResult<GetTxInfoResponse> {
let tx_info_start_ptr = syscall_handler.get_or_allocate_tx_info_start_ptr(vm)?;
Ok(GetTxInfoResponse { tx_info_start_ptr })
}
// GetTxSignature syscall.
type GetTxSignatureRequest = EmptyRequest;
type GetTxSignatureResponse = SingleSegmentResponse;
pub fn get_tx_signature(
_request: GetTxSignatureRequest,
vm: &mut VirtualMachine,
syscall_handler: &mut DeprecatedSyscallHintProcessor<'_>,
) -> DeprecatedSyscallResult<GetTxSignatureResponse> {
let start_ptr = syscall_handler.get_or_allocate_tx_signature_segment(vm)?;
let length = syscall_handler.context.tx_context.tx_info.signature().0.len();
Ok(GetTxSignatureResponse { segment: ReadOnlySegment { start_ptr, length } })
}
// LibraryCall syscall.
#[derive(Debug, Eq, PartialEq)]
pub struct LibraryCallRequest {
pub class_hash: ClassHash,
pub function_selector: EntryPointSelector,
pub calldata: Calldata,
}
impl SyscallRequest for LibraryCallRequest {
fn read(
vm: &VirtualMachine,
ptr: &mut Relocatable,
) -> DeprecatedSyscallResult<LibraryCallRequest> {
let class_hash = ClassHash(stark_felt_from_ptr(vm, ptr)?);
let (function_selector, calldata) = read_call_params(vm, ptr)?;
Ok(LibraryCallRequest { class_hash, function_selector, calldata })
}
}
type LibraryCallResponse = CallContractResponse;
pub fn library_call(
request: LibraryCallRequest,
vm: &mut VirtualMachine,
syscall_handler: &mut DeprecatedSyscallHintProcessor<'_>,
) -> DeprecatedSyscallResult<LibraryCallResponse> {
let call_to_external = true;
let retdata_segment = execute_library_call(
syscall_handler,
vm,
request.class_hash,
None,
call_to_external,
request.function_selector,
request.calldata,
)?;
Ok(LibraryCallResponse { segment: retdata_segment })
}
// LibraryCallL1Handler syscall.
pub fn library_call_l1_handler(
request: LibraryCallRequest,
vm: &mut VirtualMachine,
syscall_handler: &mut DeprecatedSyscallHintProcessor<'_>,
) -> DeprecatedSyscallResult<LibraryCallResponse> {
let call_to_external = false;
let retdata_segment = execute_library_call(
syscall_handler,
vm,
request.class_hash,
None,
call_to_external,
request.function_selector,
request.calldata,
)?;
Ok(LibraryCallResponse { segment: retdata_segment })
}
// ReplaceClass syscall.
#[derive(Debug, Eq, PartialEq)]
pub struct ReplaceClassRequest {
pub class_hash: ClassHash,
}
impl SyscallRequest for ReplaceClassRequest {
fn read(
vm: &VirtualMachine,
ptr: &mut Relocatable,
) -> DeprecatedSyscallResult<ReplaceClassRequest> {
let class_hash = ClassHash(stark_felt_from_ptr(vm, ptr)?);
Ok(ReplaceClassRequest { class_hash })
}
}
pub type ReplaceClassResponse = EmptyResponse;
pub fn replace_class(
request: ReplaceClassRequest,
_vm: &mut VirtualMachine,
syscall_handler: &mut DeprecatedSyscallHintProcessor<'_>,
) -> DeprecatedSyscallResult<ReplaceClassResponse> {
// Ensure the class is declared (by reading it).
syscall_handler.state.get_compiled_contract_class(request.class_hash)?;
syscall_handler.state.set_class_hash_at(syscall_handler.storage_address, request.class_hash)?;
Ok(ReplaceClassResponse {})
}
// SendMessageToL1 syscall.
#[derive(Debug, Eq, PartialEq)]
pub struct SendMessageToL1Request {
pub message: MessageToL1,
}
impl SyscallRequest for SendMessageToL1Request {
// The Cairo struct contains: `to_address`, `payload_size`, `payload`.
fn read(
vm: &VirtualMachine,
ptr: &mut Relocatable,
) -> DeprecatedSyscallResult<SendMessageToL1Request> {
let to_address = EthAddress::try_from(stark_felt_from_ptr(vm, ptr)?)?;
let payload = L2ToL1Payload(read_felt_array::<DeprecatedSyscallExecutionError>(vm, ptr)?);
Ok(SendMessageToL1Request { message: MessageToL1 { to_address, payload } })
}
}
type SendMessageToL1Response = EmptyResponse;
pub fn send_message_to_l1(
request: SendMessageToL1Request,
_vm: &mut VirtualMachine,
syscall_handler: &mut DeprecatedSyscallHintProcessor<'_>,
) -> DeprecatedSyscallResult<SendMessageToL1Response> {
let execution_context = &mut syscall_handler.context;
let ordered_message_to_l1 = OrderedL2ToL1Message {
order: execution_context.n_sent_messages_to_l1,
message: request.message,
};
syscall_handler.l2_to_l1_messages.push(ordered_message_to_l1);
execution_context.n_sent_messages_to_l1 += 1;
Ok(SendMessageToL1Response {})
}
// StorageRead syscall.
#[derive(Debug, Eq, PartialEq)]
pub struct StorageReadRequest {
pub address: StorageKey,
}
impl SyscallRequest for StorageReadRequest {
fn read(
vm: &VirtualMachine,
ptr: &mut Relocatable,
) -> DeprecatedSyscallResult<StorageReadRequest> {
let address = StorageKey::try_from(stark_felt_from_ptr(vm, ptr)?)?;
Ok(StorageReadRequest { address })
}
}
#[derive(Debug, Eq, PartialEq)]
pub struct StorageReadResponse {
pub value: StarkFelt,
}
impl SyscallResponse for StorageReadResponse {
fn write(self, vm: &mut VirtualMachine, ptr: &mut Relocatable) -> WriteResponseResult {
write_stark_felt(vm, ptr, self.value)?;
Ok(())
}
}
pub fn storage_read(
request: StorageReadRequest,
_vm: &mut VirtualMachine,
syscall_handler: &mut DeprecatedSyscallHintProcessor<'_>,
) -> DeprecatedSyscallResult<StorageReadResponse> {
syscall_handler.get_contract_storage_at(request.address)
}
// StorageWrite syscall.
#[derive(Debug, Eq, PartialEq)]
pub struct StorageWriteRequest {
pub address: StorageKey,
pub value: StarkFelt,
}
impl SyscallRequest for StorageWriteRequest {
fn read(
vm: &VirtualMachine,
ptr: &mut Relocatable,
) -> DeprecatedSyscallResult<StorageWriteRequest> {
let address = StorageKey::try_from(stark_felt_from_ptr(vm, ptr)?)?;
let value = stark_felt_from_ptr(vm, ptr)?;
Ok(StorageWriteRequest { address, value })
}
}
pub type StorageWriteResponse = EmptyResponse;
pub fn storage_write(
request: StorageWriteRequest,
_vm: &mut VirtualMachine,
syscall_handler: &mut DeprecatedSyscallHintProcessor<'_>,
) -> DeprecatedSyscallResult<StorageWriteResponse> {
// Read the value before the write operation in order to log it in the list of read·
// values. This is needed to correctly build the `DictAccess` entry corresponding to·
// `storage_write` syscall in the OS.
syscall_handler.get_contract_storage_at(request.address)?;
syscall_handler.set_contract_storage_at(request.address, request.value)
}