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

Remove unwrap from codebase. #1000

Merged
merged 1 commit into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 7 additions & 5 deletions src/state/cached_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
use cairo_vm::felt::Felt252;
use getset::{Getters, MutGetters};
use num_traits::Zero;
use starknet::core::types::FromByteArrayError;
use std::{
collections::{HashMap, HashSet},
sync::Arc,
Expand Down Expand Up @@ -274,7 +275,7 @@ impl<T: StateReader> State for CachedState<T> {
fn count_actual_storage_changes(
&mut self,
fee_token_and_sender_address: Option<(&Address, &Address)>,
) -> (usize, usize) {
) -> Result<(usize, usize), FromByteArrayError> {
let mut storage_updates = subtract_mappings(
self.cache.storage_writes.clone(),
self.cache.storage_initial_values.clone(),
Expand Down Expand Up @@ -310,14 +311,14 @@ impl<T: StateReader> State for CachedState<T> {
// Add fee transfer storage update before actually charging it, as it needs to be included in the
// calculation of the final fee.
if let Some((fee_token_address, sender_address)) = fee_token_and_sender_address {
let (sender_low_key, _) = get_erc20_balance_var_addresses(sender_address).unwrap();
let (sender_low_key, _) = get_erc20_balance_var_addresses(sender_address)?;
storage_updates.insert(
(fee_token_address.clone(), sender_low_key),
Felt252::default(),
);
}

(n_modified_contracts, storage_updates.len())
Ok((n_modified_contracts, storage_updates.len()))
}

fn get_class_hash_at(&mut self, contract_address: &Address) -> Result<ClassHash, StateError> {
Expand Down Expand Up @@ -730,8 +731,9 @@ mod tests {

(n_modified_contracts, n_storage_updates)
};
let changes =
cached_state.count_actual_storage_changes(Some((&fee_token_address, &sender_address)));
let changes = cached_state
.count_actual_storage_changes(Some((&fee_token_address, &sender_address)))
.unwrap();

assert_eq!(changes, expected_changes);
}
Expand Down
3 changes: 2 additions & 1 deletion src/state/state_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
utils::{Address, ClassHash, CompiledClassHash},
};
use cairo_vm::felt::Felt252;
use starknet::core::types::FromByteArrayError;

pub trait StateReader {
/// Returns the contract class of the given class hash or compiled class hash.
Expand Down Expand Up @@ -57,7 +58,7 @@ pub trait State {
fn count_actual_storage_changes(
&mut self,
fee_token_and_sender_address: Option<(&Address, &Address)>,
) -> (usize, usize);
) -> Result<(usize, usize), FromByteArrayError>;

fn get_class_hash_at(&mut self, contract_address: &Address) -> Result<ClassHash, StateError>;

Expand Down
2 changes: 1 addition & 1 deletion src/transaction/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl Declare {
let changes = state.count_actual_storage_changes(Some((
&block_context.starknet_os_config.fee_token_address,
&self.sender_address,
)));
)))?;
let actual_resources = calculate_tx_resources(
resources_manager,
&vec![validate_info.clone()],
Expand Down
2 changes: 1 addition & 1 deletion src/transaction/declare_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ impl DeclareV2 {
let storage_changes = state.count_actual_storage_changes(Some((
&block_context.starknet_os_config.fee_token_address,
&self.sender_address,
)));
)))?;
let actual_resources = calculate_tx_resources(
resources_manager,
&[execution_result.call_info.clone()],
Expand Down
4 changes: 2 additions & 2 deletions src/transaction/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ impl Deploy {

let resources_manager = ExecutionResourcesManager::default();

let changes = state.count_actual_storage_changes(None);
let changes = state.count_actual_storage_changes(None)?;
let actual_resources = calculate_tx_resources(
resources_manager,
&[Some(call_info.clone())],
Expand Down Expand Up @@ -245,7 +245,7 @@ impl Deploy {
block_context.validate_max_n_steps,
)?;

let changes = state.count_actual_storage_changes(None);
let changes = state.count_actual_storage_changes(None)?;
let actual_resources = calculate_tx_resources(
resources_manager,
&[call_info.clone()],
Expand Down
2 changes: 1 addition & 1 deletion src/transaction/deploy_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ impl DeployAccount {
state.count_actual_storage_changes(Some((
&block_context.starknet_os_config.fee_token_address,
&self.contract_address,
))),
)))?,
None,
0,
)
Expand Down
3 changes: 3 additions & 0 deletions src/transaction/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use cairo_vm::{
trace_errors::TraceError, vm_errors::VirtualMachineError,
},
};
use starknet::core::types::FromByteArrayError;
use thiserror::Error;

#[derive(Debug, Error)]
Expand Down Expand Up @@ -144,4 +145,6 @@ pub enum TransactionError {
UnsupportedVersion(String),
#[error("Invalid compiled class, expected class hash: {0}, but received: {1}")]
InvalidCompiledClassHash(String, String),
#[error(transparent)]
FromByteArrayError(#[from] FromByteArrayError),
}
2 changes: 1 addition & 1 deletion src/transaction/invoke_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ impl InvokeFunction {
let changes = state.count_actual_storage_changes(Some((
&block_context.starknet_os_config.fee_token_address,
&self.contract_address,
)));
)))?;
let actual_resources = calculate_tx_resources(
resources_manager,
&vec![call_info.clone(), validate_info.clone()],
Expand Down
2 changes: 1 addition & 1 deletion src/transaction/l1_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ impl L1Handler {
)?
};

let changes = state.count_actual_storage_changes(None);
let changes = state.count_actual_storage_changes(None)?;
let actual_resources = calculate_tx_resources(
resources_manager,
&[call_info.clone()],
Expand Down