Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Add documentations to syscalls/business_logic_syscall_handler module #887

Merged
merged 56 commits into from
Dec 22, 2023
Merged
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
ff02c5d
added comments to syscalls/business_logic_syscall_handler.rs
Aug 4, 2023
82836aa
Merge branch 'main' into document-syscallsbusiness_logic_syscall_hand…
Aug 10, 2023
427358b
added syscall information
Aug 10, 2023
fa945cd
Update src/syscalls/business_logic_syscall_handler.rs
fguthmann Aug 10, 2023
de39a24
Merge branch 'main' into document-syscallsbusiness_logic_syscall_hand…
fguthmann Aug 11, 2023
561c824
Merge branch 'main' into document-syscallsbusiness_logic_syscall_hand…
juanbono Oct 2, 2023
b825aec
Merge branch 'main' into document-syscallsbusiness_logic_syscall_hand…
fguthmann Oct 5, 2023
3065378
Add comments to origin/Document-transactionl1_handler module (#888)
fguthmann Oct 5, 2023
082a178
remove transactionalstatereader as it is not needed as-is (#1054)
edg-l Oct 5, 2023
6c7f6d3
Add cairo native (#943)
ElFantasma Oct 17, 2023
2cc8016
add test to check cairo 2 account contract deploy panic failing prope…
edg-l Oct 18, 2023
67d673d
update cairo native to latest revision, u128 gas, mut self (#1082)
edg-l Oct 18, 2023
c246c10
Fix `get_execution_info` syscall (#1081)
fmoletta Oct 18, 2023
4253809
cairo-native: implement testing syscalls (#1084)
edg-l Oct 19, 2023
7863d8f
cleanup erc20 test (#1087)
edg-l Oct 19, 2023
887ddc1
Fix `get_onchain_data_segment_length` (#1085)
fmoletta Oct 23, 2023
74996f7
Check that running a declare v1 yields a higher fee than simulating i…
fmoletta Oct 23, 2023
aab0807
implement display and debug trait for Address (#1080)
Oct 24, 2023
27329e9
Add recursive calls tests using `library_call` & `call_contract` sysc…
fmoletta Oct 24, 2023
7462fcf
Fix/Refactor State::count actual storage changes + Support `DeployAcc…
fmoletta Oct 24, 2023
75453ce
Add test cases for `DeployAccount` with popular account contracts usi…
fmoletta Oct 27, 2023
92c3c18
Execute `Declare` transactions using the `RpcState` + Various fixes r…
fmoletta Oct 27, 2023
c1c7967
Execute `L1Handler` transactions using the `RpcState` (#1103)
fmoletta Oct 27, 2023
4f2300d
Added a usage target to makefile (#1069)
igaray Oct 30, 2023
ca4ae46
Implement `NativeSyscallHandler::deploy` (#1106)
fmoletta Oct 30, 2023
00706fc
update cairo native to use gas consumed (#1102)
edg-l Oct 30, 2023
0a8cead
Save `SierraProgram` + `ContractEntryPoints` instead of `SierraContra…
fmoletta Oct 31, 2023
6b222e3
`get_execution_info`test (#1067)
ElFantasma Oct 31, 2023
919f7c1
Cairo native: Implement library_call syscall (#1074)
edg-l Oct 31, 2023
37911f0
Replace class native (#1105)
Oct 31, 2023
18111ab
`get_block_hash` syscall native (#1048)
igaray Oct 31, 2023
fc8114c
Add logging to syscalls (#1111)
juanbono Oct 31, 2023
0f6cdfc
Add error handling to `RpcState` (#1107)
fmoletta Nov 1, 2023
a2e886b
Keccak for Native Syscall Handler (#1055)
igamigo Nov 2, 2023
65d2d2e
Hacky cairo-native compiled program cache (#1091)
edg-l Nov 3, 2023
75ab6c8
Update examples to use Cairo 2 + add them to the workspace (#1122)
fmoletta Nov 6, 2023
cfc9a9f
Add docs for rpc state reader (#1130)
juanbono Nov 6, 2023
ecade37
Add benchs (#1129)
juanbono Nov 7, 2023
31f76bf
Add version + validation retdata checks for `InvokeFunction`, `Declar…
fmoletta Nov 9, 2023
17c54b2
Remove unwraps (#1137)
fmoletta Nov 10, 2023
80c8dea
Refactor `InvokeFunction::handle_nonce` to increment the nonce even i…
fmoletta Nov 10, 2023
d2e143a
Replace todo with error handling in `DeprecatedBLSyscallHandler::cons…
fmoletta Nov 10, 2023
19f986f
Add `skip_nonce_check` field to `DeclareV2`, `Declare` & `DeployAccou…
fmoletta Nov 10, 2023
c2bd748
Add `check_fee_balance` to `InvokeFunction`, `Declare`, `DeclareV2` &…
fmoletta Nov 10, 2023
80860bd
Replace testnet 2 tx in RpcState test with mainnet tx (#1142)
fmoletta Nov 13, 2023
6a27c04
Add global support for program caches. (#1140)
azteca1998 Nov 16, 2023
998013b
Refactor class hash (#1095)
Nov 17, 2023
5d72181
Get block hash from rpc (#1124)
Nov 17, 2023
8ce4c9e
Make contract caches shared. (#940)
azteca1998 Nov 17, 2023
ca83a40
Add a test calling a contract that uses keccak in testnet submodule …
fguthmann Nov 17, 2023
c3431bc
resolve merge conflict
Nov 20, 2023
177ed87
solve format
Nov 21, 2023
215789d
Merge branch 'main' into document-syscallsbusiness_logic_syscall_hand…
juanbono Nov 21, 2023
49e6887
Merge branch 'main' into document-syscallsbusiness_logic_syscall_hand…
pefontana Nov 24, 2023
5566427
Merge branch 'main' into document-syscallsbusiness_logic_syscall_hand…
fguthmann Dec 14, 2023
3a00e04
Merge branch 'main' into document-syscallsbusiness_logic_syscall_hand…
fguthmann Dec 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 55 additions & 3 deletions src/syscalls/business_logic_syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,28 +127,44 @@ lazy_static! {
};
}

/// Structure representing the [BusinessLogicSyscallHandler].
#[derive(Debug)]
pub struct BusinessLogicSyscallHandler<'a, S: StateReader, C: ContractClassCache> {
/// Events emitted by the current contract call.
pub(crate) events: Vec<OrderedEvent>,
/// Get the expected pointer to the syscall
pub(crate) expected_syscall_ptr: Relocatable,
/// Manages execution resources
pub(crate) resources_manager: ExecutionResourcesManager,
/// Context of the transaction being executed
pub(crate) tx_execution_context: TransactionExecutionContext,
/// Messages from L2 to L1
pub(crate) l2_to_l1_messages: Vec<OrderedL2ToL1Message>,
/// Address of the contract
pub(crate) contract_address: Address,
/// Address of the caller
pub(crate) caller_address: Address,
/// A list of dynamically allocated segments that are expected to be read-only.
pub(crate) read_only_segments: Vec<(Relocatable, MaybeRelocatable)>,
/// List of internal calls during the syscall execution
pub(crate) internal_calls: Vec<CallInfo>,
/// Context information related to the current block
pub(crate) block_context: BlockContext,
/// State of the storage related to Starknet contract
pub(crate) starknet_storage_state: ContractStorageState<'a, S, C>,
/// Indicates whether the current execution supports the "reverted" status.
pub(crate) support_reverted: bool,
/// Get the selector for the entry point of the contract.
pub(crate) entry_point_selector: Felt252,
/// Map selectors to their corresponding syscall names.
pub(crate) selector_to_syscall: &'a HashMap<Felt252, &'static str>,
pub(crate) execution_info_ptr: Option<Relocatable>,
}

// TODO: execution entry point may no be a parameter field, but there is no way to generate a default for now

impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a, S, C> {
/// Constructor creates a new [BusinessLogicSyscallHandler] instance
#[allow(clippy::too_many_arguments)]
pub fn new(
tx_execution_context: TransactionExecutionContext,
Expand Down Expand Up @@ -186,6 +202,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
}
}

/// Constructor with default values, used for testing
pub fn default_with_state(state: &'a mut CachedState<S, C>) -> Self {
BusinessLogicSyscallHandler::new_for_testing(
BlockInfo::default(),
Expand All @@ -194,20 +211,32 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
)
}

/// System calls allow a contract to requires services from the Starknet OS
/// See further documentation on https://docs.starknet.io/documentation/architecture_and_concepts/Contracts/system-calls/
/// Constructor for testing purposes
pub fn new_for_testing(
block_info: BlockInfo,
_contract_address: Address,
state: &'a mut CachedState<S, C>,
) -> Self {
let syscalls = Vec::from([
// Emits an event with a given set of keys and data.
"emit_event".to_string(),
// Deploys a new instance of a previously declared class.
"deploy".to_string(),
// Gets information about the original transaction.
"get_tx_info".to_string(),
// Sends a message to L1.
"send_message_to_l1".to_string(),
// Calls the requested function in any previously declared class.
"library_call".to_string(),
// Returns the address of the calling contract, or 0 if the call was not initiated by another contract.
"get_caller_address".to_string(),
// Gets the address of the contract who raised the system call.
"get_contract_address".to_string(),
// Returns the address of the sequencer that generated the current block.
"get_sequencer_address".to_string(),
// Gets the timestamp of the block in which the transaction is executed.
"get_block_timestamp".to_string(),
]);
let events = Vec::new();
Expand Down Expand Up @@ -250,6 +279,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
.increment_syscall_counter(syscall_name, 1);
}

/// Helper function to execute a call to a contract
fn call_contract_helper(
&mut self,
vm: &mut VirtualMachine,
Expand Down Expand Up @@ -318,6 +348,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
Ok(SyscallResponse { gas, body })
}

/// Checks if constructor entry points are empty
fn constructor_entry_points_empty(
&self,
contract_class: CompiledClass,
Expand All @@ -335,6 +366,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
}
}

/// Execute a constructor entry point
fn execute_constructor_entry_point(
&mut self,
contract_address: &Address,
Expand Down Expand Up @@ -411,10 +443,12 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
Ok(call_info.result())
}

/// Writes a value to the storage state using the specified address.
fn syscall_storage_write(&mut self, key: Felt252, value: Felt252) {
self.starknet_storage_state.write(Address(key), value)
}

/// Reads the syscall request, checks and reduces gas, executes the syscall, and writes the syscall response.
pub fn syscall(
&mut self,
vm: &mut VirtualMachine,
Expand Down Expand Up @@ -481,6 +515,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
Ok(())
}

/// Executes the specific syscall based on the request.
fn execute_syscall(
&mut self,
request: SyscallRequest,
Expand Down Expand Up @@ -527,6 +562,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
}
}

/// Returns the hash of a specific block, with an error if the block number is out of range.
fn get_block_hash(
&mut self,
vm: &mut VirtualMachine,
Expand Down Expand Up @@ -570,6 +606,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
})
}

/// Validates stop pointers and read-only segments after the syscall execution.
pub(crate) fn post_run(
&self,
runner: &mut VirtualMachine,
Expand All @@ -585,8 +622,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
self.validate_read_only_segments(runner)
}

/// Validates that there were no out of bounds writes to read-only segments and marks
/// them as accessed.
/// Validates that there were no out of bounds writes to read-only segments and marks them as accessed.
pub(crate) fn validate_read_only_segments(
&self,
vm: &mut VirtualMachine,
Expand All @@ -611,6 +647,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
}

impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a, S, C> {
/// Emit an event.
fn emit_event(
&mut self,
vm: &VirtualMachine,
Expand All @@ -630,6 +667,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
})
}

/// Returns the block number.
fn get_block_number(
&mut self,
_vm: &mut VirtualMachine,
Expand All @@ -643,6 +681,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
})
}

/// Reads the value associated with the given key from the storage state.
fn _storage_read(&mut self, key: [u8; 32]) -> Result<Felt252, StateError> {
match self
.starknet_storage_state
Expand All @@ -654,6 +693,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
}
}

/// Performs a storage write operation.
fn storage_write(
&mut self,
vm: &mut VirtualMachine,
Expand Down Expand Up @@ -753,6 +793,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
})
}

/// Executes a contract call
fn call_contract(
&mut self,
vm: &mut VirtualMachine,
Expand Down Expand Up @@ -783,6 +824,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
)
}

/// Performs a storage read operation.
fn storage_read(
&mut self,
vm: &mut VirtualMachine,
Expand Down Expand Up @@ -813,6 +855,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
})
}

/// Deploys a contract.
fn syscall_deploy(
&mut self,
vm: &VirtualMachine,
Expand Down Expand Up @@ -876,6 +919,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
Ok((contract_address, result))
}

/// Deploys a contract to the virtual machine.
fn deploy(
&mut self,
vm: &mut VirtualMachine,
Expand Down Expand Up @@ -923,6 +967,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
Ok(response)
}

/// Reads and validates syscall requests. Matches syscall names to their corresponding requests.
fn read_syscall_request(
&self,
vm: &VirtualMachine,
Expand All @@ -947,7 +992,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
)),
}
}

/// Allocate a segment in memory.
pub(crate) fn allocate_segment(
&mut self,
vm: &mut VirtualMachine,
Expand All @@ -962,6 +1007,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
Ok(segment_start)
}

/// Sends a message from L2 to L1, including the destination address and payload.
fn send_message_to_l1(
&mut self,
vm: &VirtualMachine,
Expand All @@ -984,6 +1030,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
})
}

/// Reads and validates a syscall request, and updates the expected syscall pointer offset.
fn read_and_validate_syscall_request(
&mut self,
vm: &VirtualMachine,
Expand All @@ -997,6 +1044,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
Ok(syscall_request)
}

/// Executes a library call
fn library_call(
&mut self,
vm: &mut VirtualMachine,
Expand Down Expand Up @@ -1028,6 +1076,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
)
}

/// Get the time stamp of the block.
fn get_block_timestamp(
&mut self,
_vm: &VirtualMachine,
Expand All @@ -1042,6 +1091,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
})
}

/// Replaces class at the specified address with a new one based on the request.
fn replace_class(
&mut self,
_vm: &VirtualMachine,
Expand All @@ -1058,6 +1108,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
})
}

/// Calculates the Keccak hash of a given input.
fn keccak(
&mut self,
vm: &mut VirtualMachine,
Expand Down Expand Up @@ -1107,6 +1158,7 @@ impl<'a, S: StateReader, C: ContractClassCache> BusinessLogicSyscallHandler<'a,
}

// TODO: refactor code to use this function
/// Constructs a failure response from an error message.
fn failure_from_error_msg(
&mut self,
vm: &mut VirtualMachine,
Expand Down