-
Notifications
You must be signed in to change notification settings - Fork 267
/
syscall_handler.py
1401 lines (1191 loc) · 54.7 KB
/
syscall_handler.py
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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import asyncio
import dataclasses
import functools
from abc import ABC, abstractmethod
from typing import (
Any,
Callable,
Dict,
Iterable,
Iterator,
List,
Mapping,
Optional,
Tuple,
Type,
cast,
)
import cachetools
from starkware.cairo.common.cairo_function_runner import CairoFunctionRunner
from starkware.cairo.common.cairo_secp.secp_utils import SECP256K1, SECP256R1, Curve
from starkware.cairo.common.keccak_utils.keccak_utils import keccak_f
from starkware.cairo.common.structs import CairoStructProxy
from starkware.cairo.lang.vm.memory_segments import MemorySegmentManager
from starkware.cairo.lang.vm.relocatable import MaybeRelocatable, RelocatableValue
from starkware.python.math_utils import (
EC_INFINITY,
EcInfinity,
EcPoint,
ec_safe_add,
ec_safe_mult,
safe_div,
y_squared_from_x,
)
from starkware.python.utils import (
as_non_optional,
assert_exhausted,
blockify,
execute_coroutine_threadsafe,
from_bytes,
gather_in_chunks,
safe_zip,
to_bytes,
)
from starkware.starknet.business_logic.execution.execute_entry_point_base import (
ExecuteEntryPointBase,
)
from starkware.starknet.business_logic.execution.objects import (
CallInfo,
CallResult,
CallType,
ExecutionResourcesManager,
OrderedEvent,
OrderedL2ToL1Message,
TransactionExecutionContext,
TransactionExecutionInfo,
)
from starkware.starknet.business_logic.fact_state.contract_state_objects import ContractState
from starkware.starknet.business_logic.state.state import ContractStorageState
from starkware.starknet.business_logic.state.state_api import SyncState
from starkware.starknet.business_logic.state.state_api_objects import BlockInfo
from starkware.starknet.core.os.contract_address.contract_address import (
calculate_contract_address_from_hash,
)
from starkware.starknet.core.os.syscall_utils import (
STARKNET_SYSCALLS_COMPILED_PATH,
cast_to_int,
get_selector_from_program,
get_syscall_structs,
load_program,
validate_runtime_request_type,
wrap_with_handler_exception,
)
from starkware.starknet.definitions import constants
from starkware.starknet.definitions.constants import GasCost
from starkware.starknet.definitions.data_availability_mode import DataAvailabilityMode
from starkware.starknet.definitions.error_codes import CairoErrorCode, StarknetErrorCode
from starkware.starknet.definitions.execution_mode import ExecutionMode
from starkware.starknet.definitions.general_config import StarknetGeneralConfig
from starkware.starknet.public.abi import CONSTRUCTOR_ENTRY_POINT_SELECTOR
from starkware.starknet.services.api.contract_class.contract_class import EntryPointType
from starkware.starknet.storage.starknet_storage import (
CommitmentInfo,
OsSingleStarknetStorage,
StorageLeaf,
)
from starkware.starkware_utils.error_handling import stark_assert
from starkware.storage.storage import FactFetchingContext
SyscallFullResponse = Tuple[tuple, tuple] # Response header + specific syscall response.
ExecuteSyscallCallback = Callable[
["SyscallHandlerBase", int, CairoStructProxy], SyscallFullResponse
]
KECCAK_FULL_RATE_IN_U64S = 17
def from_uint256(val: CairoStructProxy) -> int:
return val.high * 2**128 + val.low # type: ignore
def to_uint256(structs: CairoStructProxy, val: int) -> CairoStructProxy:
return structs.Uint256(low=val & (2**128 - 1), high=val >> 128) # type: ignore
@dataclasses.dataclass(frozen=True)
class SyscallInfo:
name: str
execute_callback: ExecuteSyscallCallback
request_struct: CairoStructProxy
class SyscallHandlerBase(ABC):
def __init__(
self,
segments: Optional[MemorySegmentManager],
initial_syscall_ptr: Optional[RelocatableValue],
):
# Static syscall information.
self.structs = get_syscall_structs()
self.selector_to_syscall_info = self.get_selector_to_syscall_info()
# Memory segments of the running program.
self._segments = segments
# Current syscall pointer; updated internally during the call execution.
self._syscall_ptr = initial_syscall_ptr
# Mapping from ec_point* to pythonic EcPoint.
self.ec_points: Dict[RelocatableValue, EcPoint] = {}
# A segment that holds all the ec points.
self.ec_points_segment: Optional[RelocatableValue] = None
self.ec_point_size = cast(int, self.structs.EcPoint.size)
@classmethod
@cachetools.cached(cache={})
def get_selector_to_syscall_info(cls) -> Dict[int, SyscallInfo]:
structs = get_syscall_structs()
syscalls_program = load_program(path=STARKNET_SYSCALLS_COMPILED_PATH)
get_selector = functools.partial(
get_selector_from_program, syscalls_program=syscalls_program
)
return {
get_selector("call_contract"): SyscallInfo(
name="call_contract",
execute_callback=cls.call_contract,
request_struct=structs.CallContractRequest,
),
get_selector("deploy"): SyscallInfo(
name="deploy",
execute_callback=cls.deploy,
request_struct=structs.DeployRequest,
),
get_selector("secp256k1_new"): SyscallInfo(
name="secp256k1_new",
execute_callback=cls.secp256k1_new,
request_struct=structs.Secp256k1NewRequest,
),
get_selector("secp256k1_add"): SyscallInfo(
name="secp256k1_add",
execute_callback=functools.partial(cls.secp_add, curve=SECP256K1),
request_struct=structs.Secp256k1AddRequest,
),
get_selector("secp256r1_add"): SyscallInfo(
name="secp256r1_add",
execute_callback=functools.partial(cls.secp_add, curve=SECP256R1),
request_struct=structs.Secp256r1AddRequest,
),
get_selector("secp256k1_mul"): SyscallInfo(
name="secp256k1_mul",
execute_callback=functools.partial(cls.secp_mul, curve=SECP256K1),
request_struct=structs.Secp256k1MulRequest,
),
get_selector("secp256r1_mul"): SyscallInfo(
name="secp256r1_mul",
execute_callback=functools.partial(cls.secp_mul, curve=SECP256R1),
request_struct=structs.Secp256r1MulRequest,
),
get_selector("secp256k1_get_point_from_x"): SyscallInfo(
name="secp256k1_get_point_from_x",
execute_callback=cls.secp256k1_get_point_from_x,
request_struct=structs.Secp256k1GetPointFromXRequest,
),
get_selector("secp256r1_get_point_from_x"): SyscallInfo(
name="secp256r1_get_point_from_x",
execute_callback=cls.secp256r1_get_point_from_x,
request_struct=structs.Secp256r1GetPointFromXRequest,
),
get_selector("secp256k1_get_xy"): SyscallInfo(
name="secp256k1_get_xy",
execute_callback=cls.secp_get_xy,
request_struct=structs.Secp256k1GetXyRequest,
),
get_selector("secp256r1_get_xy"): SyscallInfo(
name="secp256r1_get_xy",
execute_callback=cls.secp_get_xy,
request_struct=structs.Secp256r1GetXyRequest,
),
get_selector("secp256r1_new"): SyscallInfo(
name="secp256r1_new",
execute_callback=cls.secp256r1_new,
request_struct=structs.Secp256r1NewRequest,
),
get_selector("keccak"): SyscallInfo(
name="keccak",
execute_callback=cls.keccak,
request_struct=structs.KeccakRequest,
),
get_selector("get_block_hash"): SyscallInfo(
name="get_block_hash",
execute_callback=cls.get_block_hash,
request_struct=structs.GetBlockHashRequest,
),
get_selector("get_execution_info"): SyscallInfo(
name="get_execution_info",
execute_callback=cls.get_execution_info,
request_struct=structs.EmptyRequest,
),
get_selector("library_call"): SyscallInfo(
name="library_call",
execute_callback=cls.library_call,
request_struct=structs.LibraryCallRequest,
),
get_selector("storage_read"): SyscallInfo(
name="storage_read",
execute_callback=cls.storage_read,
request_struct=structs.StorageReadRequest,
),
get_selector("storage_write"): SyscallInfo(
name="storage_write",
execute_callback=cls.storage_write,
request_struct=structs.StorageWriteRequest,
),
get_selector("emit_event"): SyscallInfo(
name="emit_event",
execute_callback=cls.emit_event,
request_struct=structs.EmitEventRequest,
),
get_selector("replace_class"): SyscallInfo(
name="replace_class",
execute_callback=cls.replace_class,
request_struct=structs.ReplaceClassRequest,
),
get_selector("send_message_to_l1"): SyscallInfo(
name="send_message_to_l1",
execute_callback=cls.send_message_to_l1,
request_struct=structs.SendMessageToL1Request,
),
}
@property
def segments(self) -> MemorySegmentManager:
assert self._segments is not None, "segments must be set before using the SyscallHandler."
return self._segments
@property
def syscall_ptr(self) -> RelocatableValue:
assert (
self._syscall_ptr is not None
), "syscall_ptr must be set before using the SyscallHandler."
return self._syscall_ptr
def syscall(self, syscall_ptr: RelocatableValue):
"""
Executes the selected system call.
"""
self._validate_syscall_ptr(actual_syscall_ptr=syscall_ptr)
request_header = self._read_and_validate_request(request_struct=self.structs.RequestHeader)
# Validate syscall selector and request.
selector = cast_to_int(request_header.selector)
syscall_info = self.selector_to_syscall_info.get(selector)
assert (
syscall_info is not None
), f"Unsupported syscall selector {bytes.fromhex(hex(selector)[2:])!r}"
if syscall_info.name != "keccak":
self._count_syscall(syscall_name=syscall_info.name)
request = self._read_and_validate_request(request_struct=syscall_info.request_struct)
# Check and reduce gas (after validating the syscall selector for consistency with the OS).
initial_gas = cast_to_int(request_header.gas)
required_gas = self._get_required_gas(name=syscall_info.name)
if initial_gas < required_gas:
# Out of gas failure.
response_header, response = self._handle_out_of_gas(initial_gas=initial_gas)
else:
# Execute.
remaining_gas = initial_gas - required_gas
response_header, response = syscall_info.execute_callback(self, remaining_gas, request)
# Write response to the syscall segment.
self._write_response(response=response_header)
self._write_response(response=response)
# Syscalls.
def call_contract(self, remaining_gas: int, request: CairoStructProxy) -> SyscallFullResponse:
return self.call_contract_helper(
remaining_gas=remaining_gas, request=request, syscall_name="call_contract"
)
def library_call(self, remaining_gas: int, request: CairoStructProxy) -> SyscallFullResponse:
return self.call_contract_helper(
remaining_gas=remaining_gas, request=request, syscall_name="library_call"
)
def call_contract_helper(
self, remaining_gas: int, request: CairoStructProxy, syscall_name: str
) -> SyscallFullResponse:
result = self._call_contract_helper(
remaining_gas=remaining_gas, request=request, syscall_name=syscall_name
)
remaining_gas -= result.gas_consumed
response_header = self.structs.ResponseHeader(
gas=remaining_gas, failure_flag=result.failure_flag
)
retdata_start = self._allocate_segment_for_retdata(retdata=result.retdata)
retdata_end = retdata_start + len(result.retdata)
if response_header.failure_flag == 0:
response = self.structs.CallContractResponse(
retdata_start=retdata_start, retdata_end=retdata_end
)
else:
response = self.structs.FailureReason(start=retdata_start, end=retdata_end)
return response_header, response
def deploy(self, remaining_gas: int, request: CairoStructProxy) -> SyscallFullResponse:
contract_address, result = self._deploy(remaining_gas=remaining_gas, request=request)
remaining_gas -= result.gas_consumed
response_header = self.structs.ResponseHeader(
gas=remaining_gas, failure_flag=result.failure_flag
)
retdata_start = self._allocate_segment_for_retdata(retdata=result.retdata)
retdata_end = retdata_start + len(result.retdata)
if response_header.failure_flag == 0:
response = self.structs.DeployResponse(
contract_address=contract_address,
constructor_retdata_start=retdata_start,
constructor_retdata_end=retdata_end,
)
else:
response = self.structs.FailureReason(start=retdata_start, end=retdata_end)
return response_header, response
def get_block_hash(self, remaining_gas: int, request: CairoStructProxy) -> SyscallFullResponse:
"""
Executes the get_block_hash system call.
Returns the block hash of the block at given block_number.
Returns the expected block hash if the given block was created at least 10 blocks before the
current block. Otherwise, returns an error.
"""
block_number = cast_to_int(request.block_number)
# Handle out of range block number.
if self.current_block_number - block_number < constants.STORED_BLOCK_HASH_BUFFER:
return self._handle_failure(
final_gas=remaining_gas, error_code=CairoErrorCode.BLOCK_NUMBER_OUT_OF_RANGE
)
block_hash = self._get_block_hash(block_number=block_number)
response_header = self.structs.ResponseHeader(gas=remaining_gas, failure_flag=0)
response = self.structs.GetBlockHashResponse(block_hash=block_hash)
return response_header, response
def get_execution_info(
self, remaining_gas: int, request: CairoStructProxy
) -> SyscallFullResponse:
execution_info_ptr = self._get_execution_info_ptr()
response_header = self.structs.ResponseHeader(gas=remaining_gas, failure_flag=0)
response = self.structs.GetExecutionInfoResponse(execution_info=execution_info_ptr)
return response_header, response
def _secp_new(
self,
remaining_gas: int,
request: CairoStructProxy,
curve: Curve,
response_struct: CairoStructProxy,
) -> SyscallFullResponse:
x = from_uint256(request.x)
y = from_uint256(request.y)
if x >= curve.prime or y >= curve.prime:
return self._handle_failure(
final_gas=remaining_gas,
error_code=CairoErrorCode.INVALID_ARGUMENT,
)
response_header = self.structs.ResponseHeader(gas=remaining_gas, failure_flag=0)
ec_point: Optional[RelocatableValue] = None
if x == 0 and y == 0:
ec_point = self._new_ec_point(ec_point=EC_INFINITY)
else:
y_squared = y_squared_from_x(
x=x, alpha=curve.alpha, beta=curve.beta, field_prime=curve.prime
)
if (y**2 - y_squared) % curve.prime == 0:
ec_point = self._new_ec_point(ec_point=(x, y))
if ec_point is None:
response = response_struct(not_on_curve=1, ec_point=0)
else:
response = response_struct(not_on_curve=0, ec_point=ec_point)
return response_header, response
def secp256k1_new(self, remaining_gas: int, request: CairoStructProxy) -> SyscallFullResponse:
return self._secp_new(
remaining_gas=remaining_gas,
request=request,
curve=SECP256K1,
response_struct=self.structs.Secp256k1NewResponse,
)
def secp256r1_new(self, remaining_gas: int, request: CairoStructProxy) -> SyscallFullResponse:
return self._secp_new(
remaining_gas=remaining_gas,
request=request,
curve=SECP256R1,
response_struct=self.structs.Secp256r1NewResponse,
)
def secp_add(
self, remaining_gas: int, request: CairoStructProxy, curve: Curve
) -> SyscallFullResponse:
response_header = self.structs.ResponseHeader(gas=remaining_gas, failure_flag=0)
response = self.structs.SecpOpResponse(
ec_point=self._new_ec_point(
ec_point=ec_safe_add(
point1=self._get_ec_point(request.p0),
point2=self._get_ec_point(request.p1),
alpha=curve.alpha,
p=curve.prime,
)
),
)
return response_header, response
def secp_mul(
self, remaining_gas: int, request: CairoStructProxy, curve: Curve
) -> SyscallFullResponse:
response_header = self.structs.ResponseHeader(gas=remaining_gas, failure_flag=0)
response = self.structs.SecpOpResponse(
ec_point=self._new_ec_point(
ec_point=ec_safe_mult(
m=from_uint256(request.scalar),
point=self.ec_points[cast(RelocatableValue, request.p)],
alpha=curve.alpha,
p=curve.prime,
)
),
)
return response_header, response
def secp_get_point_from_x(
self,
remaining_gas: int,
request: CairoStructProxy,
curve: Curve,
) -> SyscallFullResponse:
x = from_uint256(request.x)
if x >= curve.prime:
return self._handle_failure(
final_gas=remaining_gas,
error_code=CairoErrorCode.INVALID_ARGUMENT,
)
prime = curve.prime
y_squared = y_squared_from_x(
x=x,
alpha=curve.alpha,
beta=curve.beta,
field_prime=prime,
)
y = pow(y_squared, (prime + 1) // 4, prime)
if (y & 1) != request.y_parity:
y = (-y) % prime
response_header = self.structs.ResponseHeader(gas=remaining_gas, failure_flag=0)
response = (
self.structs.SecpNewResponse(
not_on_curve=0,
ec_point=self._new_ec_point(ec_point=(x, y)),
)
if (y * y) % prime == y_squared
else self.structs.SecpNewResponse(
not_on_curve=1,
ec_point=0,
)
)
return response_header, response
def secp256k1_get_point_from_x(
self, remaining_gas: int, request: CairoStructProxy
) -> SyscallFullResponse:
return self.secp_get_point_from_x(
remaining_gas=remaining_gas, request=request, curve=SECP256K1
)
def secp256r1_get_point_from_x(
self, remaining_gas: int, request: CairoStructProxy
) -> SyscallFullResponse:
return self.secp_get_point_from_x(
remaining_gas=remaining_gas, request=request, curve=SECP256R1
)
def secp_get_xy(self, remaining_gas: int, request: CairoStructProxy) -> SyscallFullResponse:
ec_point = self.ec_points[cast(RelocatableValue, request.ec_point)]
response_header = self.structs.ResponseHeader(gas=remaining_gas, failure_flag=0)
if isinstance(ec_point, EcInfinity):
x, y = 0, 0
else:
x, y = ec_point
# Note that we can't use self.structs.SecpGetXyResponse here as it is not flat.
response = to_uint256(self.structs, x) + to_uint256(self.structs, y) # type: ignore
return response_header, response
def keccak(self, remaining_gas: int, request: CairoStructProxy) -> SyscallFullResponse:
assert isinstance(request.input_end, RelocatableValue)
assert isinstance(request.input_start, RelocatableValue)
input_len = cast(int, request.input_end - request.input_start)
if input_len % KECCAK_FULL_RATE_IN_U64S != 0:
return self._handle_failure(
final_gas=remaining_gas,
error_code=CairoErrorCode.INVALID_INPUT_LEN,
)
n_rounds = safe_div(input_len, KECCAK_FULL_RATE_IN_U64S)
gas_cost = n_rounds * GasCost.KECCAK_ROUND_COST.value
if gas_cost > remaining_gas:
return self._handle_failure(
final_gas=remaining_gas,
error_code=CairoErrorCode.OUT_OF_GAS,
)
remaining_gas -= gas_cost
self._keccak(n_rounds=n_rounds)
input_array = self._get_felt_range(
start_addr=request.input_start, end_addr=request.input_end
)
state = bytearray(200)
for chunk in blockify(input_array, chunk_size=KECCAK_FULL_RATE_IN_U64S):
for i, val in safe_zip(range(0, KECCAK_FULL_RATE_IN_U64S * 8, 8), chunk):
state[i : i + 8] = to_bytes(
value=from_bytes(value=state[i : i + 8], byte_order="little") ^ val,
length=8,
byte_order="little",
)
state = bytearray(keccak_f(state))
result = [
from_bytes(state[0:16], byte_order="little"),
from_bytes(state[16:32], byte_order="little"),
]
response_header = self.structs.ResponseHeader(gas=remaining_gas, failure_flag=0)
response = self.structs.KeccakResponse(result_low=result[0], result_high=result[1])
return response_header, response
def storage_read(self, remaining_gas: int, request: CairoStructProxy) -> SyscallFullResponse:
assert request.reserved == 0, f"Unsupported address domain: {request.reserved}."
value = self._storage_read(key=cast_to_int(request.key))
response_header = self.structs.ResponseHeader(gas=remaining_gas, failure_flag=0)
response = self.structs.StorageReadResponse(value=value)
return response_header, response
def storage_write(self, remaining_gas: int, request: CairoStructProxy) -> SyscallFullResponse:
assert request.reserved == 0, f"Unsupported address domain: {request.reserved}."
self._storage_write(key=cast_to_int(request.key), value=cast_to_int(request.value))
response_header = self.structs.ResponseHeader(gas=remaining_gas, failure_flag=0)
return response_header, tuple()
def emit_event(self, remaining_gas: int, request: CairoStructProxy) -> SyscallFullResponse:
keys = self._get_felt_range(start_addr=request.keys_start, end_addr=request.keys_end)
data = self._get_felt_range(start_addr=request.data_start, end_addr=request.data_end)
self._emit_event(keys=keys, data=data)
response_header = self.structs.ResponseHeader(gas=remaining_gas, failure_flag=0)
return response_header, tuple()
def replace_class(self, remaining_gas: int, request: CairoStructProxy) -> SyscallFullResponse:
self._replace_class(class_hash=cast_to_int(request.class_hash))
response_header = self.structs.ResponseHeader(gas=remaining_gas, failure_flag=0)
return response_header, tuple()
def send_message_to_l1(
self, remaining_gas: int, request: CairoStructProxy
) -> SyscallFullResponse:
payload = self._get_felt_range(
start_addr=cast(RelocatableValue, request.payload_start),
end_addr=cast(RelocatableValue, request.payload_end),
)
self._send_message_to_l1(to_address=cast_to_int(request.to_address), payload=payload)
response_header = self.structs.ResponseHeader(gas=remaining_gas, failure_flag=0)
return response_header, tuple()
# Application-specific syscall implementation.
@abstractmethod
def _call_contract_helper(
self, remaining_gas: int, request: CairoStructProxy, syscall_name: str
) -> CallResult:
"""
Returns the call's result.
syscall_name can be "call_contract" or "library_call".
"""
@abstractmethod
def _deploy(self, remaining_gas: int, request: CairoStructProxy) -> Tuple[int, CallResult]:
"""
Returns the address of the newly deployed contract and the constructor call's result.
Note that the result may contain failures that preceded the constructor invocation, such
as undeclared class.
"""
@abstractmethod
def _get_block_hash(self, block_number: int) -> int:
"""
Returns the block hash of the block at given block number.
"""
@abstractmethod
def _get_execution_info_ptr(self) -> RelocatableValue:
"""
Returns a pointer to the ExecutionInfo struct.
"""
@abstractmethod
def _keccak(self, n_rounds: int):
"""
Post-process for the keccak syscall.
"""
@abstractmethod
def _storage_read(self, key: int) -> int:
"""
Returns the value of the contract's storage at the given key.
"""
@abstractmethod
def _storage_write(self, key: int, value: int):
"""
Specific implementation of the storage_write syscall.
"""
@abstractmethod
def _emit_event(self, keys: List[int], data: List[int]):
"""
Specific implementation of the emit_event syscall.
"""
@abstractmethod
def _replace_class(self, class_hash: int):
"""
Specific implementation of the replace_class syscall.
"""
@abstractmethod
def _send_message_to_l1(self, to_address: int, payload: List[int]):
"""
Specific implementation of the send_message_to_l1 syscall.
"""
# Internal utilities.
def _get_required_gas(self, name: str) -> int:
"""
Returns the remaining required gas for the given syscall.
"""
total_gas_cost = GasCost[name.upper()].int_value
# Refund the base amount the was pre-charged.
return total_gas_cost - GasCost.SYSCALL_BASE.value
def _handle_failure(self, final_gas: int, error_code: CairoErrorCode) -> SyscallFullResponse:
response_header = self.structs.ResponseHeader(gas=final_gas, failure_flag=1)
data = [error_code.to_felt()]
start = self.allocate_segment(data=data)
failure_reason = self.structs.FailureReason(start=start, end=start + len(data))
return response_header, failure_reason
def _handle_out_of_gas(self, initial_gas: int) -> SyscallFullResponse:
return self._handle_failure(final_gas=initial_gas, error_code=CairoErrorCode.OUT_OF_GAS)
def _get_felt_range(self, start_addr: Any, end_addr: Any) -> List[int]:
assert isinstance(start_addr, RelocatableValue)
assert isinstance(end_addr, RelocatableValue)
assert start_addr.segment_index == end_addr.segment_index, (
"Inconsistent start and end segment indices "
f"({start_addr.segment_index} != {end_addr.segment_index})."
)
assert start_addr.offset <= end_addr.offset, (
"The start offset cannot be greater than the end offset"
f"({start_addr.offset} > {end_addr.offset})."
)
size = end_addr.offset - start_addr.offset
return self.segments.memory.get_range_as_ints(addr=start_addr, size=size)
@abstractmethod
def allocate_segment(self, data: Iterable[MaybeRelocatable]) -> RelocatableValue:
"""
Allocates and returns a new (read-only) segment with the given data.
Note that unlike MemorySegmentManager.write_arg, this function doesn't work well with
recursive input - call allocate_segment for the inner items if needed.
"""
@abstractmethod
def _allocate_segment_for_retdata(self, retdata: Iterable[int]) -> RelocatableValue:
"""
Allocates and returns a new (read-only) segment with the given retdata.
"""
def _validate_syscall_ptr(self, actual_syscall_ptr: RelocatableValue):
assert (
actual_syscall_ptr == self.syscall_ptr
), f"Bad syscall_ptr, Expected {self.syscall_ptr}, got {actual_syscall_ptr}."
def _read_and_validate_request(self, request_struct: CairoStructProxy) -> CairoStructProxy:
request = self._read_request(request_struct=request_struct)
validate_runtime_request_type(request_values=request, request_struct=request_struct)
return request
def _read_request(self, request_struct: CairoStructProxy) -> CairoStructProxy:
request = request_struct.from_ptr(memory=self.segments.memory, addr=self.syscall_ptr)
# Advance syscall pointer.
self._syscall_ptr = self.syscall_ptr + request_struct.size
return request
def _write_response(self, response: tuple):
# Write response and update syscall pointer.
self._syscall_ptr = self.segments.write_arg(ptr=self.syscall_ptr, arg=response)
def _count_syscall(self, syscall_name: str):
return
def _new_ec_point(self, ec_point: EcPoint) -> RelocatableValue:
"""
Allocates ec_points handle and stores it in the ec_points mapping.
"""
if self.ec_points_segment is None:
self.ec_points_segment = self.segments.add()
handle = self.ec_points_segment + len(self.ec_points) * self.ec_point_size
self.ec_points[handle] = ec_point
return handle
def _get_ec_point(self, handle: CairoStructProxy) -> EcPoint:
"""
Returns the ec_points corresponding to `handle`.
"""
assert isinstance(handle, RelocatableValue)
return self.ec_points[handle]
@property
@abstractmethod
def current_block_number(self) -> int:
"""
Returns the block number of the current block.
"""
class BusinessLogicSyscallHandler(SyscallHandlerBase):
"""
A handler for system calls; used by the BusinessLogic entry point execution.
"""
def __init__(
self,
state: SyncState,
resources_manager: ExecutionResourcesManager,
segments: MemorySegmentManager,
tx_execution_context: TransactionExecutionContext,
initial_syscall_ptr: RelocatableValue,
general_config: StarknetGeneralConfig,
entry_point: ExecuteEntryPointBase,
support_reverted: bool,
):
super().__init__(segments=segments, initial_syscall_ptr=initial_syscall_ptr)
# Entry point info.
self.entry_point = entry_point
self.execute_entry_point_cls: Type[ExecuteEntryPointBase] = type(entry_point)
# Configuration objects.
self.general_config = general_config
# Execution-related objects.
self.tx_execution_context = tx_execution_context
self.resources_manager = resources_manager
self.state = state
self.support_reverted = support_reverted
# The storage which the current call acts on.
self.storage = ContractStorageState(
state=state, contract_address=self.entry_point.contract_address
)
# A list of dynamically allocated segments that are expected to be read-only.
self.read_only_segments: List[Tuple[RelocatableValue, int]] = []
# Internal calls executed by the current contract call.
self.internal_calls: List[CallInfo] = []
# Events emitted by the current contract call.
self.events: List[OrderedEvent] = []
# Messages sent by the current contract call to L1.
self.l2_to_l1_messages: List[OrderedL2ToL1Message] = []
# A pointer to the Cairo ExecutionInfo struct.
self._execution_info_ptr: Optional[RelocatableValue] = None
@property
def current_block_number(self) -> int:
return self.state.block_info.block_number
# Syscalls.
def get_block_hash(self, remaining_gas: int, request: CairoStructProxy) -> SyscallFullResponse:
syscall_name = "get_block_hash"
stark_assert(
not self._is_validate_execution_mode(),
code=StarknetErrorCode.UNAUTHORIZED_ACTION_ON_VALIDATE,
message=(
f"Unauthorized syscall {syscall_name} "
f"in execution mode {self.tx_execution_context.execution_mode.name}."
),
)
return super().get_block_hash(remaining_gas=remaining_gas, request=request)
def _call_contract_helper(
self, remaining_gas: int, request: CairoStructProxy, syscall_name: str
) -> CallResult:
calldata = self._get_felt_range(
start_addr=request.calldata_start, end_addr=request.calldata_end
)
class_hash: Optional[int] = None
if syscall_name == "call_contract":
contract_address = cast_to_int(request.contract_address)
caller_address = self.entry_point.contract_address
call_type = CallType.CALL
if self._is_validate_execution_mode():
stark_assert(
self.entry_point.contract_address == contract_address,
code=StarknetErrorCode.UNAUTHORIZED_ACTION_ON_VALIDATE,
message=(
f"Unauthorized syscall {syscall_name} "
f"in execution mode {self.tx_execution_context.execution_mode.name}."
),
)
elif syscall_name == "library_call":
contract_address = self.entry_point.contract_address
caller_address = self.entry_point.caller_address
call_type = CallType.DELEGATE
class_hash = cast_to_int(request.class_hash)
else:
raise NotImplementedError(f"Unsupported call type {syscall_name}.")
call = self.execute_entry_point_cls(
call_type=call_type,
contract_address=contract_address,
entry_point_selector=cast_to_int(request.selector),
entry_point_type=EntryPointType.EXTERNAL,
calldata=calldata,
caller_address=caller_address,
initial_gas=remaining_gas,
class_hash=class_hash,
code_address=None,
)
return self.execute_entry_point(call=call)
def _deploy(self, remaining_gas: int, request: CairoStructProxy) -> Tuple[int, CallResult]:
assert request.deploy_from_zero in [0, 1], "The deploy_from_zero field must be 0 or 1."
constructor_calldata = self._get_felt_range(
start_addr=request.constructor_calldata_start, end_addr=request.constructor_calldata_end
)
class_hash = cast_to_int(request.class_hash)
# Calculate contract address.
deployer_address = self.entry_point.contract_address if request.deploy_from_zero == 0 else 0
contract_address = calculate_contract_address_from_hash(
salt=cast_to_int(request.contract_address_salt),
class_hash=class_hash,
constructor_calldata=constructor_calldata,
deployer_address=deployer_address,
)
# Instantiate the contract (may raise UNDECLARED_CLASS and CONTRACT_ADDRESS_UNAVAILABLE).
self.state.deploy_contract(contract_address=contract_address, class_hash=class_hash)
# Invoke constructor.
result = self.execute_constructor_entry_point(
contract_address=contract_address,
class_hash=class_hash,
constructor_calldata=constructor_calldata,
remaining_gas=remaining_gas,
)
return contract_address, result
def _get_block_hash(self, block_number: int) -> int:
return self.state.get_storage_at(
data_availability_mode=DataAvailabilityMode.L1,
contract_address=constants.BLOCK_HASH_CONTRACT_ADDRESS,
key=block_number,
)
def _get_execution_info_ptr(self) -> RelocatableValue:
if self._execution_info_ptr is None:
# Prepare block info.
python_block_info = self.storage.state.block_info
block_info = (
self.structs.BlockInfo(
block_number=python_block_info.block_number,
block_timestamp=python_block_info.block_timestamp,
sequencer_address=0,
)
if self._is_validate_execution_mode()
else self.structs.BlockInfo(
block_number=python_block_info.block_number,
block_timestamp=python_block_info.block_timestamp,
sequencer_address=as_non_optional(python_block_info.sequencer_address),
)
)
# Prepare transaction info.
signature = self.tx_execution_context.signature
signature_start = self.allocate_segment(data=signature)
tx_info = self.structs.TxInfo(
version=self.tx_execution_context.version,
account_contract_address=self.tx_execution_context.account_contract_address,
max_fee=self.tx_execution_context.max_fee,
signature_start=signature_start,
signature_end=signature_start + len(signature),
transaction_hash=self.tx_execution_context.transaction_hash,
chain_id=self.general_config.chain_id.value,
nonce=self.tx_execution_context.nonce,
# We only support execution of transactions with version < 3, hence we set the new
# additional fields to zero.
resource_bounds_start=0,
resource_bounds_end=0,
tip=0,
paymaster_data_start=0,
paymaster_data_end=0,
nonce_data_availability_mode=0,
fee_data_availability_mode=0,
account_deployment_data_start=0,
account_deployment_data_end=0,
)
# Gather all info.
execution_info = self.structs.ExecutionInfo(
block_info=self.allocate_segment(data=block_info),
tx_info=self.allocate_segment(data=tx_info),
caller_address=self.entry_point.caller_address,
contract_address=self.entry_point.contract_address,
selector=self.entry_point.entry_point_selector,
)
self._execution_info_ptr = self.allocate_segment(data=execution_info)
return self._execution_info_ptr
def _storage_read(self, key: int) -> int:
return self.storage.read(address=key)
def _storage_write(self, key: int, value: int):
self.storage.write(address=key, value=value)
def _emit_event(self, keys: List[int], data: List[int]):
self.events.append(
OrderedEvent(order=self.tx_execution_context.n_emitted_events, keys=keys, data=data)
)
# Update events count.
self.tx_execution_context.n_emitted_events += 1
def _replace_class(self, class_hash: int):
compiled_class_hash = self.storage.state.get_compiled_class_hash(class_hash=class_hash)