diff --git a/crates/blockifier/src/execution/syscalls/hint_processor.rs b/crates/blockifier/src/execution/syscalls/hint_processor.rs index 7bed21f96d..0d019ada08 100644 --- a/crates/blockifier/src/execution/syscalls/hint_processor.rs +++ b/crates/blockifier/src/execution/syscalls/hint_processor.rs @@ -108,8 +108,6 @@ pub const INVALID_INPUT_LENGTH_ERROR: &str = // "Invalid argument"; pub const INVALID_ARGUMENT: &str = "0x00000000000000000000000000000000496e76616c696420617267756d656e74"; -// Each resource bound contains three felts; see struct `Resource`. -pub const RESOURCE_BOUNDS_ELEMENT_SIZE: usize = 3; /// Executes StarkNet syscalls (stateful protocol hints) during the execution of an entry point /// call. @@ -314,36 +312,30 @@ impl<'a> SyscallHintProcessor<'a> { } } - // TODO(Mohammad, 20/10/2023): Refactor to use allocate_data_segment method. fn allocate_tx_resource_bounds_segment( &mut self, vm: &mut VirtualMachine, context: &CurrentAccountTransactionContext, - ) -> SyscallResult { - let flat_resource_bounds = &context + ) -> SyscallResult<(Relocatable, Relocatable)> { + let flat_resource_bounds = context .resource_bounds .0 .iter() .flat_map(|(resource, resource_bounds)| { - let resource_as_int = match resource { - Resource::L1Gas => 0, - Resource::L2Gas => 1, + let resource = match resource { + Resource::L1Gas => StarkFelt::ZERO, + Resource::L2Gas => StarkFelt::ONE, }; vec![ - resource_as_int, - resource_bounds.max_price_per_unit, - resource_bounds.max_amount as u128, + resource, + StarkFelt::from(resource_bounds.max_price_per_unit), + StarkFelt::from(resource_bounds.max_amount), ] - .into_iter() - .map(|value| MaybeRelocatable::from(Felt252::from(value))) }) .collect(); - let resource_bounds_segment_start_ptr = - self.read_only_segments.allocate(vm, flat_resource_bounds)?; - - Ok(resource_bounds_segment_start_ptr) + self.allocate_data_segment(vm, flat_resource_bounds) } fn execute_syscall( @@ -409,7 +401,6 @@ impl<'a> SyscallHintProcessor<'a> { self.increment_syscall_count_by(selector, 1); } - // TODO(Mohammad, 20/10/2023): Refactor to use allocate_data_segment method. fn allocate_execution_info_segment( &mut self, vm: &mut VirtualMachine, @@ -430,27 +421,26 @@ impl<'a> SyscallHintProcessor<'a> { Ok(execution_info_segment_start_ptr) } - // TODO(Mohammad, 20/10/2023): Refactor to use allocate_data_segment method. fn allocate_block_info_segment( &mut self, vm: &mut VirtualMachine, ) -> SyscallResult { let block_context = &self.context.block_context; - let block_info: Vec = if self.is_validate_mode() { + let block_info: Vec = if self.is_validate_mode() { vec![ - Felt252::zero().into(), + StarkFelt::ZERO, // TODO(Yoni, 1/5/2024): set the timestamp to be zero for `validate``. - Felt252::from(block_context.block_timestamp.0).into(), - Felt252::zero().into(), + StarkFelt::from(block_context.block_timestamp.0), + StarkFelt::ZERO, ] } else { vec![ - Felt252::from(block_context.block_number.0).into(), - Felt252::from(block_context.block_timestamp.0).into(), - stark_felt_to_felt(*block_context.sequencer_address.0.key()).into(), + StarkFelt::from(block_context.block_number.0), + StarkFelt::from(block_context.block_timestamp.0), + *block_context.sequencer_address.0.key(), ] }; - let block_info_segment_start_ptr = self.read_only_segments.allocate(vm, &block_info)?; + let (block_info_segment_start_ptr, _) = self.allocate_data_segment(vm, block_info)?; Ok(block_info_segment_start_ptr) } @@ -490,11 +480,8 @@ impl<'a> SyscallHintProcessor<'a> { match account_tx_context { AccountTransactionContext::Current(context) => { - let tx_resource_bounds_start_ptr = + let (tx_resource_bounds_start_ptr, tx_resource_bounds_end_ptr) = &self.allocate_tx_resource_bounds_segment(vm, &context)?; - let tx_resource_bounds_length = context.resource_bounds.0.len(); - let tx_resource_bounds_end_ptr = (*tx_resource_bounds_start_ptr - + (RESOURCE_BOUNDS_ELEMENT_SIZE * tx_resource_bounds_length))?; let (tx_paymaster_data_start_ptr, tx_paymaster_data_end_ptr) = &self.allocate_data_segment(vm, context.paymaster_data.0)?; @@ -517,15 +504,15 @@ impl<'a> SyscallHintProcessor<'a> { AccountTransactionContext::Deprecated(_) => { let zero_felt: MaybeRelocatable = Felt252::zero().into(); tx_info.extend_from_slice(&[ - zero_felt.clone(), // Empty array of resource bounds. - zero_felt.clone(), + zero_felt.clone(), // Empty segment of resource bounds (start ptr). + zero_felt.clone(), // Empty segment of resource bounds (end ptr). zero_felt.clone(), // Tip. - zero_felt.clone(), // Empty array of paymaster data. - zero_felt.clone(), + zero_felt.clone(), // Empty segment of paymaster data (start ptr). + zero_felt.clone(), // Empty segment of paymaster data (end ptr). zero_felt.clone(), // Nonce DA mode. zero_felt.clone(), // Fee DA mode. - zero_felt.clone(), // Empty array of account deployment data. - zero_felt, + zero_felt.clone(), // Empty segment of account deployment data (start ptr). + zero_felt, // Empty segment of account deployment data (end ptr). ]); } }; @@ -675,11 +662,10 @@ pub fn create_retdata_segment( syscall_handler: &mut SyscallHintProcessor<'_>, raw_retdata: &[StarkFelt], ) -> SyscallResult { - let retdata: Vec = - raw_retdata.iter().map(|&x| MaybeRelocatable::from(stark_felt_to_felt(x))).collect(); - let retdata_segment_start_ptr = syscall_handler.read_only_segments.allocate(vm, &retdata)?; + let (retdata_segment_start_ptr, _) = + syscall_handler.allocate_data_segment(vm, raw_retdata.to_vec())?; - Ok(ReadOnlySegment { start_ptr: retdata_segment_start_ptr, length: retdata.len() }) + Ok(ReadOnlySegment { start_ptr: retdata_segment_start_ptr, length: raw_retdata.len() }) } pub fn execute_library_call(