Skip to content

Commit

Permalink
simple syscall working
Browse files Browse the repository at this point in the history
  • Loading branch information
Okm165 committed Jun 4, 2024
1 parent 739a1dd commit 0715655
Show file tree
Hide file tree
Showing 9 changed files with 9,098 additions and 1,159 deletions.
1,282 changes: 225 additions & 1,057 deletions bootloader_input.json

Large diffs are not rendered by default.

48 changes: 15 additions & 33 deletions cairo0-bootloader/bootloader/contract/execute_entry_point.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ from starkware.cairo.common.dict_access import DictAccess
from starkware.cairo.common.find_element import find_element, search_sorted
from starkware.cairo.common.math import assert_not_zero
from starkware.cairo.common.registers import get_ap
from bootloader.contract.execute_syscalls import execute_syscalls
from starkware.starknet.builtins.segment_arena.segment_arena import (
SegmentArenaBuiltin,
validate_segment_arena,
)
from starkware.starknet.common.syscalls import TxInfo as DeprecatedTxInfo
from starkware.starknet.core.os.block_context import BlockContext
from starkware.starknet.core.os.builtins import (
BuiltinEncodings,
BuiltinParams,
Expand All @@ -31,19 +31,7 @@ from starkware.starknet.core.os.constants import (
)
from contract_class.compiled_class import CompiledClass, CompiledClassEntryPoint, CompiledClassFact
from starkware.starknet.core.os.output import OsCarriedOutputs

struct ExecutionInfo {
selector: felt,
}

// Represents the execution context during the execution of contract code.
struct ExecutionContext {
entry_point_type: felt,
calldata_size: felt,
calldata: felt*,
// Additional information about the execution.
execution_info: ExecutionInfo*,
}
from bootloader.contract.execute_syscalls import ExecutionContext

// Represents the arguments pushed to the stack before calling an entry point.
struct EntryPointCallArguments {
Expand All @@ -65,15 +53,11 @@ struct EntryPointReturnValues {

// Performs a Cairo jump to the function 'execute_syscalls'.
// This function's signature must match the signature of 'execute_syscalls'.
func call_execute_syscalls{
range_check_ptr,
syscall_ptr: felt*,
builtin_ptrs: BuiltinPointers*,
contract_state_changes: DictAccess*,
contract_class_changes: DictAccess*,
outputs: OsCarriedOutputs*,
}(block_context: BlockContext*, execution_context: ExecutionContext*, syscall_ptr_end: felt*) {
%{ print("call_execute_syscalls") %}
func call_execute_syscalls{range_check_ptr, syscall_ptr: felt*, builtin_ptrs: BuiltinPointers*}(
execution_context: ExecutionContext*, syscall_ptr_end: felt*
) {
execute_syscalls(execution_context, syscall_ptr_end);
return ();
}

// Returns the CompiledClassEntryPoint, based on 'compiled_class' and 'execution_context'.
Expand Down Expand Up @@ -128,7 +112,6 @@ func get_entry_point{range_check_ptr}(
// and execution_context.execution_info.selector.
//
// Arguments:
// block_context - a global context that is fixed throughout the block.
// execution_context - The context for the current execution.
func execute_entry_point{
range_check_ptr, builtin_ptrs: BuiltinPointers*, builtin_params: BuiltinParams*
Expand Down Expand Up @@ -181,7 +164,7 @@ func execute_entry_point{
// Use tempvar to pass the rest of the arguments to contract_entry_point().
let current_ap = ap;
tempvar args = EntryPointCallArguments(
gas_builtin=1000000,
gas_builtin=1000000000000,
syscall_ptr=syscall_ptr,
calldata_start=calldata_start,
calldata_end=calldata_end,
Expand Down Expand Up @@ -232,17 +215,16 @@ func execute_entry_point{
validate_segment_arena(segment_arena=current_segment_arena);

let builtin_ptrs = return_builtin_ptrs;
// with syscall_ptr {
// call_execute_syscalls(
// block_context=block_context,
// execution_context=execution_context,
// syscall_ptr_end=entry_point_return_values.syscall_ptr,
// );
// }
with syscall_ptr {
call_execute_syscalls(
execution_context=execution_context,
syscall_ptr_end=entry_point_return_values.syscall_ptr,
);
}

%{
print(ids.entry_point_return_values.failure_flag)
for i in range(0, 4):
for i in range(0, 5):
print(memory[ids.retdata_start + i])
%}

Expand Down
135 changes: 135 additions & 0 deletions cairo0-bootloader/bootloader/contract/execute_syscalls.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
from starkware.cairo.common.bool import FALSE
from starkware.cairo.common.cairo_builtins import BitwiseBuiltin
from starkware.cairo.common.cairo_secp.bigint import (
bigint_to_uint256,
nondet_bigint3,
uint256_to_bigint,
)
from starkware.cairo.common.dict import dict_read, dict_update
from starkware.cairo.common.dict_access import DictAccess
from starkware.cairo.common.math import (
assert_le,
assert_lt,
assert_nn,
assert_not_zero,
unsigned_div_rem,
)
from starkware.cairo.common.memcpy import memcpy
from starkware.cairo.common.segments import relocate_segment
from starkware.cairo.common.uint256 import Uint256, assert_uint256_lt, uint256_lt
from starkware.starknet.common.new_syscalls import (
CALL_CONTRACT_SELECTOR,
CallContractRequest,
CallContractResponse,
RequestHeader,
ResponseHeader,
FailureReason,
)
from starkware.starknet.core.os.builtins import (
BuiltinPointers,
NonSelectableBuiltins,
SelectableBuiltins,
)
from starkware.starknet.core.os.execution.deprecated_execute_syscalls import deploy_contract
from starkware.starknet.core.os.output import (
MessageToL1Header,
OsCarriedOutputs,
os_carried_outputs_new,
)
from starkware.starknet.core.os.state.commitment import StateEntry

struct ExecutionInfo {
selector: felt,
}

// Represents the execution context during the execution of contract code.
struct ExecutionContext {
entry_point_type: felt,
calldata_size: felt,
calldata: felt*,
// Additional information about the execution.
execution_info: ExecutionInfo*,
}

// Executes the system calls in syscall_ptr.
// The signature of the function 'call_execute_syscalls' must match this function's signature.
//
// Arguments:
// execution_context - The execution context in which the system calls need to be executed.
// syscall_ptr_end - a pointer to the end of the syscall segment.
func execute_syscalls{range_check_ptr, syscall_ptr: felt*, builtin_ptrs: BuiltinPointers*}(
execution_context: ExecutionContext*, syscall_ptr_end: felt*
) {
if (syscall_ptr == syscall_ptr_end) {
return ();
}

tempvar selector = [syscall_ptr];

assert selector = CALL_CONTRACT_SELECTOR;

execute_call_contract(caller_execution_context=execution_context);
return execute_syscalls(execution_context=execution_context, syscall_ptr_end=syscall_ptr_end);
}

// Executes a syscall that calls another contract.
func execute_call_contract{range_check_ptr, syscall_ptr: felt*, builtin_ptrs: BuiltinPointers*}(
caller_execution_context: ExecutionContext*
) {
let request_header = cast(syscall_ptr, RequestHeader*);
let syscall_ptr = syscall_ptr + RequestHeader.SIZE;

let call_contract_request = cast(syscall_ptr, CallContractRequest*);
let syscall_ptr = syscall_ptr + CallContractRequest.SIZE;

tempvar contract_address = call_contract_request.contract_address;
assert contract_address = 0;

tempvar calldata_start = call_contract_request.calldata_start;
tempvar calldata_size = call_contract_request.calldata_end - calldata_start;
assert calldata_size = 4;
assert calldata_start[0] = 0xa;
assert calldata_start[1] = 0xb;
assert calldata_start[2] = 0xc;
assert calldata_start[3] = 0xe;

let response_header = cast(syscall_ptr, ResponseHeader*);
let syscall_ptr = syscall_ptr + ResponseHeader.SIZE;

assert [response_header] = ResponseHeader(gas=1000000000000, failure_flag=0);

let call_contract_response = cast(syscall_ptr, CallContractResponse*);
// Advance syscall pointer to the next syscall.
let syscall_ptr = syscall_ptr + CallContractResponse.SIZE;

tempvar calldata_start = call_contract_response.retdata_start;
tempvar calldata_size = call_contract_response.retdata_end - calldata_start;
assert calldata_size = 4;
assert calldata_start[0] = 0xa;
assert calldata_start[1] = 0xb;
assert calldata_start[2] = 0xc;
assert calldata_start[3] = 0xe;

return ();
}

// Returns a failure response with a single felt.
@known_ap_change
func write_failure_response{syscall_ptr: felt*}(remaining_gas: felt, failure_felt: felt) {
let response_header = cast(syscall_ptr, ResponseHeader*);
// Advance syscall pointer to the response body.
let syscall_ptr = syscall_ptr + ResponseHeader.SIZE;

// Write the response header.
assert [response_header] = ResponseHeader(gas=remaining_gas, failure_flag=1);

let failure_reason: FailureReason* = cast(syscall_ptr, FailureReason*);
// Advance syscall pointer to the next syscall.
let syscall_ptr = syscall_ptr + FailureReason.SIZE;

// Write the failure reason.
tempvar start = failure_reason.start;
assert start[0] = failure_felt;
assert failure_reason.end = start + 1;
return ();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ from starkware.starknet.core.os.builtins import (
SelectableBuiltins,
update_builtin_ptrs,
)
from bootloader.contract.execute_entry_point import (
execute_entry_point,
ExecutionContext,
ExecutionInfo,
)
from bootloader.contract.execute_entry_point import execute_entry_point
from bootloader.contract.execute_syscalls import ExecutionContext, ExecutionInfo
from starkware.starknet.core.os.constants import ENTRY_POINT_TYPE_EXTERNAL

// Loads the programs and executes them.
Expand Down Expand Up @@ -93,17 +90,16 @@ func run_contract_bootloader{

local calldata: felt*;
%{ ids.calldata = segments.add() %}

assert calldata[0] = 0x3;
assert calldata[1] = 0x3;
assert calldata[2] = 0x4;
assert calldata[3] = 0x5;

local execution_info: ExecutionInfo = ExecutionInfo(selector=0x00e2054f8a912367e38a22ce773328ff8aabf8082c4120bad9ef085e1dbf29a7);
assert calldata[0] = 0x0;

local execution_info: ExecutionInfo = ExecutionInfo(
selector=0x00e2054f8a912367e38a22ce773328ff8aabf8082c4120bad9ef085e1dbf29a7
);

local execution_context: ExecutionContext = ExecutionContext(
entry_point_type=ENTRY_POINT_TYPE_EXTERNAL,
calldata_size=4,
calldata_size=1,
calldata=calldata,
execution_info=&execution_info,
);
Expand Down
Loading

0 comments on commit 0715655

Please sign in to comment.