Skip to content

Commit 0dfcd0d

Browse files
authored
add business logic syscall handler helpers (#21)
* add resources manager and increment syscall cou * replace prime * add default impl * move execution resource manager to state module * add test
1 parent 979f419 commit 0dfcd0d

File tree

4 files changed

+77
-7
lines changed

4 files changed

+77
-7
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
pub mod objects;
2+
pub mod state;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
use std::collections::HashMap;
2+
3+
#[derive(Debug, Default)]
4+
pub struct ExecutionResourcesManager(HashMap<String, u32>);
5+
6+
impl ExecutionResourcesManager {
7+
pub fn increment_syscall_counter(&mut self, syscall_name: &str, amount: u32) -> Option<()> {
8+
self.0.get_mut(syscall_name).map(|val| *val += amount)
9+
}
10+
11+
pub fn get_syscall_counter(&self, syscall_name: &str) -> Option<u32> {
12+
self.0.get(syscall_name).map(ToOwned::to_owned)
13+
}
14+
}

src/core/syscalls/business_logic_syscall_handler.rs

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::rc::Rc;
33

44
use super::syscall_request::*;
55
use crate::business_logic::execution::objects::*;
6+
use crate::business_logic::execution::state::ExecutionResourcesManager;
67
use crate::core::errors::syscall_handler_errors::SyscallHandlerError;
78
use crate::core::syscalls::syscall_handler::SyscallHandler;
89
use crate::hash_utils::calculate_contract_address_from_hash;
@@ -18,7 +19,11 @@ use num_traits::{One, Zero};
1819

1920
pub struct BusinessLogicSyscallHandler {
2021
tx_execution_context: Rc<RefCell<TransactionExecutionContext>>,
22+
/// Events emitted by the current contract call.
2123
events: Rc<RefCell<Vec<OrderedEvent>>>,
24+
/// A list of dynamically allocated segments that are expected to be read-only.
25+
read_only_segments: Rc<RefCell<Vec<(Relocatable, MaybeRelocatable)>>>,
26+
resources_manager: Rc<RefCell<ExecutionResourcesManager>>,
2227
contract_address: u64,
2328
l2_to_l1_messages: Vec<OrderedL2ToL1Message>,
2429
}
@@ -27,18 +32,24 @@ impl BusinessLogicSyscallHandler {
2732
pub fn new() -> Self {
2833
let events = Rc::new(RefCell::new(Vec::new()));
2934
let tx_execution_context = Rc::new(RefCell::new(TransactionExecutionContext::new()));
35+
let read_only_segments = Rc::new(RefCell::new(Vec::new()));
36+
let resources_manager = Rc::new(RefCell::new(ExecutionResourcesManager::default()));
37+
3038
BusinessLogicSyscallHandler {
3139
events,
3240
tx_execution_context,
41+
read_only_segments,
42+
resources_manager,
3343
contract_address: 0,
3444
l2_to_l1_messages: Vec::new(),
3545
}
3646
}
37-
}
3847

39-
impl Default for BusinessLogicSyscallHandler {
40-
fn default() -> Self {
41-
Self::new()
48+
/// Increments the syscall count for a given `syscall_name` by 1.
49+
fn increment_syscall_count(&self, syscall_name: &str) {
50+
self.resources_manager
51+
.borrow_mut()
52+
.increment_syscall_counter(syscall_name, 1);
4253
}
4354
}
4455

@@ -159,6 +170,7 @@ impl SyscallHandler for BusinessLogicSyscallHandler {
159170
vm: &VirtualMachine,
160171
syscall_ptr: Relocatable,
161172
) -> Result<SyscallRequest, SyscallHandlerError> {
173+
self.increment_syscall_count(syscall_name);
162174
self.read_syscall_request(syscall_name, vm, syscall_ptr)
163175
}
164176

@@ -205,9 +217,25 @@ impl SyscallHandler for BusinessLogicSyscallHandler {
205217
fn _storage_write(&self, _address: i32, _value: i32) {
206218
todo!()
207219
}
208-
fn _allocate_segment(&self, _vm: VirtualMachine, _data: Vec<MaybeRelocatable>) -> Relocatable {
209-
todo!()
220+
221+
fn allocate_segment(
222+
&self,
223+
vm: &mut VirtualMachine,
224+
data: Vec<MaybeRelocatable>,
225+
) -> Result<Relocatable, SyscallHandlerError> {
226+
let segment_start = vm.add_memory_segment();
227+
let segment_end = vm
228+
.write_arg(&segment_start, &data)
229+
.map_err(|_| SyscallHandlerError::SegmentationFault)?;
230+
let sub = segment_end
231+
.sub(&segment_start.to_owned().into(), vm.get_prime())
232+
.map_err(|_| SyscallHandlerError::SegmentationFault)?;
233+
let segment = (segment_start.to_owned(), sub);
234+
self.read_only_segments.borrow_mut().push(segment);
235+
236+
Ok(segment_start)
210237
}
238+
211239
fn _write_syscall_response(
212240
&self,
213241
_response: Vec<i32>,
@@ -218,6 +246,12 @@ impl SyscallHandler for BusinessLogicSyscallHandler {
218246
}
219247
}
220248

249+
impl Default for BusinessLogicSyscallHandler {
250+
fn default() -> Self {
251+
Self::new()
252+
}
253+
}
254+
221255
#[cfg(test)]
222256
mod tests {
223257
use crate::bigint;
@@ -384,6 +418,21 @@ mod tests {
384418
)
385419
}
386420

421+
#[test]
422+
fn can_allocate_segment() {
423+
let mut syscall_handler = BusinessLogicSyscallHandler::new();
424+
let mut vm = vm!();
425+
let data = vec![MaybeRelocatable::Int(7.into())];
426+
427+
let segment_start = syscall_handler.allocate_segment(&mut vm, data).unwrap();
428+
let expected_value = vm
429+
.get_integer(&Relocatable::from((0, 0)))
430+
.unwrap()
431+
.into_owned();
432+
assert_eq!(Relocatable::from((0, 0)), segment_start);
433+
assert_eq!(expected_value, 7.into());
434+
}
435+
387436
#[test]
388437
fn test_send_message_to_l1_ok() {
389438
let mut syscall = BusinessLogicSyscallHandler::new();

src/core/syscalls/syscall_handler.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,13 @@ pub(crate) trait SyscallHandler {
7878
fn _get_contract_address(&self, vm: VirtualMachine, syscall_ptr: Relocatable) -> i32;
7979
fn _storage_read(&self, address: i32) -> i32;
8080
fn _storage_write(&self, address: i32, value: i32);
81-
fn _allocate_segment(&self, vm: VirtualMachine, data: Vec<MaybeRelocatable>) -> Relocatable;
81+
82+
fn allocate_segment(
83+
&self,
84+
vm: &mut VirtualMachine,
85+
data: Vec<MaybeRelocatable>,
86+
) -> Result<Relocatable, SyscallHandlerError>;
87+
8288
fn _write_syscall_response(
8389
&self,
8490
response: Vec<i32>,

0 commit comments

Comments
 (0)