This repository was archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Add storage_write syscall #345
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,112 @@ | ||
pub struct BusinessLogicSyscallHandler; | ||
#![allow(dead_code)] | ||
|
||
use cairo_rs::{types::relocatable::Relocatable, vm::vm_core::VirtualMachine}; | ||
use felt::Felt252; | ||
|
||
use crate::{ | ||
business_logic::{ | ||
fact_state::state::ExecutionResourcesManager, | ||
state::{ | ||
contract_storage_state::ContractStorageState, | ||
state_api::{State, StateReader}, | ||
}, | ||
}, | ||
core::errors::syscall_handler_errors::SyscallHandlerError, | ||
utils::Address, | ||
}; | ||
|
||
use super::{ | ||
syscall_handler::SyscallHandler, | ||
syscall_info::get_syscall_size_from_name, | ||
syscall_request::{FromPtr, SyscallRequest}, | ||
syscall_response::SyscallResponse, | ||
}; | ||
|
||
pub struct BusinessLogicSyscallHandler<'a, T: Default + State + StateReader> { | ||
pub(crate) resources_manager: ExecutionResourcesManager, | ||
pub(crate) expected_syscall_ptr: Relocatable, | ||
pub(crate) starknet_storage_state: ContractStorageState<'a, T>, | ||
} | ||
|
||
impl<'a, T: Default + State + StateReader> BusinessLogicSyscallHandler<'a, T> { | ||
fn new( | ||
state: &'a mut T, | ||
contract_address: Address, | ||
resources_manager: ExecutionResourcesManager, | ||
expected_syscall_ptr: Relocatable, | ||
) -> Self { | ||
let starknet_storage_state = ContractStorageState::new(state, contract_address); | ||
Self { | ||
resources_manager, | ||
expected_syscall_ptr, | ||
starknet_storage_state, | ||
} | ||
} | ||
|
||
fn increment_syscall_count(&mut self, syscall_name: &str) { | ||
self.resources_manager | ||
.increment_syscall_counter(syscall_name, 1); | ||
} | ||
|
||
fn _storage_write(&mut self, key: Felt252, value: Felt252) { | ||
self.starknet_storage_state.write(&key.to_le_bytes(), value) | ||
} | ||
} | ||
|
||
impl<'a, T: Default + State + StateReader> SyscallHandler for BusinessLogicSyscallHandler<'a, T> { | ||
#[allow(irrefutable_let_patterns)] | ||
fn storage_write( | ||
&mut self, | ||
vm: &mut VirtualMachine, | ||
syscall_ptr: Relocatable, | ||
remaining_gas: u64, | ||
) -> Result<SyscallResponse, SyscallHandlerError> { | ||
let request = if let SyscallRequest::StorageWrite(request) = | ||
self.read_and_validate_syscall_request("storage_write", vm, syscall_ptr)? | ||
{ | ||
request | ||
} else { | ||
return Err(SyscallHandlerError::ExpectedStorageWriteSyscall); | ||
}; | ||
|
||
if request.reserved != 0.into() { | ||
return Err(SyscallHandlerError::UnsopportedAddressDomain( | ||
request.reserved, | ||
)); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. isn't the call to |
||
Ok(SyscallResponse { | ||
gas: remaining_gas, | ||
body: None, | ||
}) | ||
} | ||
|
||
fn read_and_validate_syscall_request( | ||
&mut self, | ||
syscall_name: &str, | ||
vm: &VirtualMachine, | ||
syscall_ptr: Relocatable, | ||
) -> Result<SyscallRequest, SyscallHandlerError> { | ||
self.increment_syscall_count(syscall_name); | ||
let syscall_request = self.read_syscall_request(syscall_name, vm, syscall_ptr)?; | ||
|
||
self.expected_syscall_ptr.offset += get_syscall_size_from_name(syscall_name); | ||
Ok(syscall_request) | ||
} | ||
|
||
fn read_syscall_request( | ||
&self, | ||
syscall_name: &str, | ||
vm: &VirtualMachine, | ||
syscall_ptr: Relocatable, | ||
) -> Result<SyscallRequest, SyscallHandlerError> { | ||
match syscall_name { | ||
"storage_write" => { | ||
super::syscall_request::StorageWriteRequest::from_ptr(vm, syscall_ptr) | ||
} | ||
_ => Err(SyscallHandlerError::UnknownSyscall( | ||
syscall_name.to_string(), | ||
)), | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,38 @@ | ||
pub trait SyscallHandler {} | ||
use cairo_rs::{types::relocatable::Relocatable, vm::vm_core::VirtualMachine}; | ||
|
||
use crate::core::errors::syscall_handler_errors::SyscallHandlerError; | ||
|
||
use super::{ | ||
syscall_request::{FromPtr, StorageWriteRequest, SyscallRequest}, | ||
syscall_response::SyscallResponse, | ||
}; | ||
|
||
pub(crate) trait SyscallHandler { | ||
fn read_and_validate_syscall_request( | ||
&mut self, | ||
syscall_name: &str, | ||
vm: &VirtualMachine, | ||
syscall_ptr: Relocatable, | ||
) -> Result<SyscallRequest, SyscallHandlerError>; | ||
|
||
fn storage_write( | ||
&mut self, | ||
vm: &mut VirtualMachine, | ||
syscall_ptr: Relocatable, | ||
remaining_gas: u64, | ||
) -> Result<SyscallResponse, SyscallHandlerError>; | ||
|
||
fn read_syscall_request( | ||
&self, | ||
syscall_name: &str, | ||
vm: &VirtualMachine, | ||
syscall_ptr: Relocatable, | ||
) -> Result<SyscallRequest, SyscallHandlerError> { | ||
match syscall_name { | ||
"storage_write" => StorageWriteRequest::from_ptr(vm, syscall_ptr), | ||
_ => Err(SyscallHandlerError::UnknownSyscall( | ||
syscall_name.to_string(), | ||
)), | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,46 @@ | ||
#[allow(unused)] | ||
pub(crate) enum SyscallRequest {} | ||
use cairo_rs::{types::relocatable::Relocatable, vm::vm_core::VirtualMachine}; | ||
use felt::Felt252; | ||
|
||
use crate::{core::errors::syscall_handler_errors::SyscallHandlerError, utils::get_big_int}; | ||
|
||
pub(crate) enum SyscallRequest { | ||
StorageWrite(StorageWriteRequest), | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq)] | ||
pub(crate) struct StorageWriteRequest { | ||
pub(crate) reserved: Felt252, | ||
pub(crate) key: Felt252, | ||
pub(crate) value: Felt252, | ||
} | ||
|
||
impl From<StorageWriteRequest> for SyscallRequest { | ||
fn from(storage_write_request: StorageWriteRequest) -> SyscallRequest { | ||
SyscallRequest::StorageWrite(storage_write_request) | ||
} | ||
} | ||
|
||
pub(crate) trait FromPtr { | ||
fn from_ptr( | ||
vm: &VirtualMachine, | ||
syscall_ptr: Relocatable, | ||
) -> Result<SyscallRequest, SyscallHandlerError>; | ||
} | ||
|
||
impl FromPtr for StorageWriteRequest { | ||
fn from_ptr( | ||
vm: &VirtualMachine, | ||
syscall_ptr: Relocatable, | ||
) -> Result<SyscallRequest, SyscallHandlerError> { | ||
let reserved = get_big_int(vm, syscall_ptr)?; | ||
let key = get_big_int(vm, &syscall_ptr + 1)?; | ||
let value = get_big_int(vm, &syscall_ptr + 2)?; | ||
|
||
Ok(StorageWriteRequest { | ||
reserved, | ||
key, | ||
value, | ||
} | ||
.into()) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please rename the function to
syscall_storage_write
to avoid using underscore. I just this in order to keep consistency with the changes to this things that we already did in main.