diff --git a/src/core/contract_address/deprecated_contract_address.rs b/src/core/contract_address/deprecated_contract_address.rs index 9b5b2ccfb..325902d45 100644 --- a/src/core/contract_address/deprecated_contract_address.rs +++ b/src/core/contract_address/deprecated_contract_address.rs @@ -169,7 +169,6 @@ impl serde_json::ser::Formatter for PythonDefaultFormatter { #[derive(serde::Deserialize, serde::Serialize)] #[serde(deny_unknown_fields)] - pub struct CairoContractDefinition<'a> { /// Contract ABI, which has no schema definition. pub abi: serde_json::Value, @@ -257,7 +256,7 @@ pub(crate) fn compute_hinted_class_hash( None => {} } // We don't know what this type is supposed to be, but if its missing it is null. - if let Some(serde_json::Value::Null) = vals.get_mut("flow_tracking_data") { + if vals.get("flow_tracking_data") == Some(&serde_json::Value::Null) { vals.remove("flow_tracking_data"); } diff --git a/src/definitions/block_context.rs b/src/definitions/block_context.rs index 2e8ffc9b6..2ac680271 100644 --- a/src/definitions/block_context.rs +++ b/src/definitions/block_context.rs @@ -85,7 +85,7 @@ impl StarknetOsConfig { /// * `chain_id` - [`Felt252`] of the configured chain. /// * `fee_token_address` - Address of the token used when paying fees. /// * `gas_price` - Price of gas. - pub fn new(chain_id: Felt252, fee_token_address: Address, gas_price: u128) -> Self { + pub const fn new(chain_id: Felt252, fee_token_address: Address, gas_price: u128) -> Self { StarknetOsConfig { chain_id, fee_token_address, @@ -139,7 +139,7 @@ impl BlockContext { /// Example: for block number 6351, this includes the blocks 5327, 5328, ..., 6340, 6341. /// * `enforce_l1_handler_fee` - Whether to enforce the L1 handler fee. #[allow(clippy::too_many_arguments)] - pub fn new( + pub const fn new( starknet_os_config: StarknetOsConfig, contract_storage_commitment_tree_height: u64, global_state_commitment_tree_height: u64, diff --git a/src/execution/gas_usage.rs b/src/execution/gas_usage.rs index b61419c48..f0d254dc7 100644 --- a/src/execution/gas_usage.rs +++ b/src/execution/gas_usage.rs @@ -105,7 +105,7 @@ pub fn get_message_segment_lenght( /// # Returns: /// /// The on-chain data segment length -pub fn get_onchain_data_segment_length( +pub const fn get_onchain_data_segment_length( n_modified_contracts: usize, n_storage_changes: usize, n_deployments: usize, @@ -159,7 +159,7 @@ pub fn get_log_message_to_l1_emissions_cost(l2_to_l1_messages: &[L2toL1MessageIn /// # Returns: /// /// The cost of event emissions. -pub fn get_event_emission_cost(topics: usize, l1_handler_payload_size: usize) -> usize { +pub const fn get_event_emission_cost(topics: usize, l1_handler_payload_size: usize) -> usize { GAS_PER_LOG + (topics + N_DEFAULT_TOPICS) * GAS_PER_LOG_TOPIC + l1_handler_payload_size * GAS_PER_LOG_DATA_WORD diff --git a/src/execution/mod.rs b/src/execution/mod.rs index 8a6fd6837..4acaf3fbe 100644 --- a/src/execution/mod.rs +++ b/src/execution/mod.rs @@ -494,7 +494,7 @@ pub struct TransactionExecutionInfo { } impl TransactionExecutionInfo { - pub fn new( + pub const fn new( validate_info: Option, call_info: Option, revert_error: Option, @@ -553,7 +553,7 @@ impl TransactionExecutionInfo { } } - pub fn new_without_fee_info( + pub const fn new_without_fee_info( validate_info: Option, call_info: Option, revert_error: Option, diff --git a/src/hash_utils.rs b/src/hash_utils.rs index 70da1526c..596ff1de6 100644 --- a/src/hash_utils.rs +++ b/src/hash_utils.rs @@ -125,9 +125,9 @@ pub fn compute_hash_on_elements(vec: &[Felt252]) -> Result { let felt_result = felt_vec .into_iter() .reduce(|x, y| pedersen_hash(&x, &y)) - .ok_or(HashError::FailedToComputeHash( - "Failed to compute Pedersen hash.".to_string(), - ))?; + .ok_or_else(|| { + HashError::FailedToComputeHash("Failed to compute Pedersen hash.".to_string()) + })?; let result = Felt252::from_bytes_be(&felt_result.to_bytes_be()); Ok(result) diff --git a/src/runner/mod.rs b/src/runner/mod.rs index 4788a49f5..c5e86702c 100644 --- a/src/runner/mod.rs +++ b/src/runner/mod.rs @@ -69,7 +69,7 @@ impl StarknetRunner where H: HintProcessor + HintProcessorPostRun, { - pub fn new(cairo_runner: CairoRunner, vm: VirtualMachine, hint_processor: H) -> Self { + pub const fn new(cairo_runner: CairoRunner, vm: VirtualMachine, hint_processor: H) -> Self { StarknetRunner { cairo_runner, vm, diff --git a/src/services/api/contract_classes/deprecated_contract_class.rs b/src/services/api/contract_classes/deprecated_contract_class.rs index a1ef5c367..f2ede7801 100644 --- a/src/services/api/contract_classes/deprecated_contract_class.rs +++ b/src/services/api/contract_classes/deprecated_contract_class.rs @@ -36,7 +36,7 @@ pub struct ContractEntryPoint { } impl ContractEntryPoint { - pub fn new(selector: Felt252, offset: usize) -> ContractEntryPoint { + pub const fn new(selector: Felt252, offset: usize) -> ContractEntryPoint { ContractEntryPoint { selector, offset } } } diff --git a/src/state/in_memory_state_reader.rs b/src/state/in_memory_state_reader.rs index 7a7f1c372..2d6a001f7 100644 --- a/src/state/in_memory_state_reader.rs +++ b/src/state/in_memory_state_reader.rs @@ -15,7 +15,7 @@ use std::collections::HashMap; /// /// This implementation is used for testing and debugging. /// It uses HashMaps to store the data. -#[derive(Debug, MutGetters, Getters, PartialEq, Clone, Default)] +#[derive(Debug, MutGetters, Getters, PartialEq, Eq, Clone, Default)] pub struct InMemoryStateReader { #[getset(get_mut = "pub")] pub address_to_class_hash: HashMap, @@ -39,7 +39,7 @@ impl InMemoryStateReader { /// - `class_hash_to_contract_class` - A HashMap from class hashes to their contract classes. /// - `casm_contract_classes` - A [CasmClassCache]. /// - `class_hash_to_compiled_class_hash` - A HashMap from class hashes to their compiled class hashes. - pub fn new( + pub const fn new( address_to_class_hash: HashMap, address_to_nonce: HashMap, address_to_storage: HashMap, diff --git a/src/state/mod.rs b/src/state/mod.rs index 3920165a8..6940e3c77 100644 --- a/src/state/mod.rs +++ b/src/state/mod.rs @@ -35,7 +35,7 @@ pub struct BlockInfo { } impl BlockInfo { - pub fn empty(sequencer_address: Address) -> Self { + pub const fn empty(sequencer_address: Address) -> Self { BlockInfo { block_number: 0, // To do: In cairo-lang, this value is set to -1 block_timestamp: 0, @@ -44,7 +44,7 @@ impl BlockInfo { } } - pub fn validate_legal_progress( + pub const fn validate_legal_progress( &self, next_block_info: &BlockInfo, ) -> Result<(), TransactionError> { @@ -102,7 +102,7 @@ impl ExecutionResourcesManager { } } -#[derive(Default, Clone, PartialEq, Debug, Getters)] +#[derive(Default, Clone, PartialEq, Eq, Debug, Getters)] #[getset(get = "pub")] pub struct StateDiff { pub(crate) address_to_class_hash: HashMap, @@ -112,7 +112,7 @@ pub struct StateDiff { } impl StateDiff { - pub fn new( + pub const fn new( address_to_class_hash: HashMap, address_to_nonce: HashMap, class_hash_to_compiled_class: HashMap, diff --git a/src/state/state_cache.rs b/src/state/state_cache.rs index 03efa1774..2b5876015 100644 --- a/src/state/state_cache.rs +++ b/src/state/state_cache.rs @@ -41,7 +41,7 @@ impl StateCache { #[allow(clippy::too_many_arguments)] /// Create a new StateCache with given initial and written values for testing - pub fn new( + pub const fn new( class_hash_initial_values: HashMap, compiled_class_hash_initial_values: HashMap, nonce_initial_values: HashMap, @@ -82,7 +82,7 @@ impl StateCache { /// Creates a new instance of `StateCache` for testing purposes with the provided initial values and writes. #[allow(clippy::too_many_arguments)] - pub fn new_for_testing( + pub const fn new_for_testing( class_hash_initial_values: HashMap, compiled_class_hash_initial_values: HashMap, nonce_initial_values: HashMap, diff --git a/src/syscalls/business_logic_syscall_handler.rs b/src/syscalls/business_logic_syscall_handler.rs index 61baaf6aa..095d2ad21 100644 --- a/src/syscalls/business_logic_syscall_handler.rs +++ b/src/syscalls/business_logic_syscall_handler.rs @@ -258,7 +258,7 @@ impl<'a, S: StateReader> BusinessLogicSyscallHandler<'a, S> { .map_err(|err| SyscallHandlerError::ExecutionError(err.to_string()))?; let call_info = call_info.ok_or(SyscallHandlerError::ExecutionError( - revert_error.unwrap_or("Execution error".to_string()), + revert_error.unwrap_or_else(|| "Execution error".to_string()), ))?; let retdata_maybe_reloc = call_info @@ -376,7 +376,7 @@ impl<'a, S: StateReader> BusinessLogicSyscallHandler<'a, S> { .map_err(|_| StateError::ExecutionEntryPoint())?; let call_info = call_info.ok_or(StateError::CustomError( - revert_error.unwrap_or("Execution error".to_string()), + revert_error.unwrap_or_else(|| "Execution error".to_string()), ))?; self.internal_calls.push(call_info.clone()); diff --git a/src/syscalls/deprecated_business_logic_syscall_handler.rs b/src/syscalls/deprecated_business_logic_syscall_handler.rs index 25e473abf..70ca3d101 100644 --- a/src/syscalls/deprecated_business_logic_syscall_handler.rs +++ b/src/syscalls/deprecated_business_logic_syscall_handler.rs @@ -452,7 +452,7 @@ impl<'a, S: StateReader> DeprecatedBLSyscallHandler<'a, S> { .map_err(|e| SyscallHandlerError::ExecutionError(e.to_string()))?; let call_info = call_info.ok_or(SyscallHandlerError::ExecutionError( - revert_error.unwrap_or("Execution error".to_string()), + revert_error.unwrap_or_else(|| "Execution error".to_string()), ))?; let retdata = call_info.retdata.clone(); @@ -461,7 +461,7 @@ impl<'a, S: StateReader> DeprecatedBLSyscallHandler<'a, S> { Ok(retdata) } - pub(crate) fn get_block_info(&self) -> &BlockInfo { + pub(crate) const fn get_block_info(&self) -> &BlockInfo { &self.block_context.block_info } diff --git a/src/syscalls/deprecated_syscall_response.rs b/src/syscalls/deprecated_syscall_response.rs index bc7d3523f..79fbe4be2 100644 --- a/src/syscalls/deprecated_syscall_response.rs +++ b/src/syscalls/deprecated_syscall_response.rs @@ -60,7 +60,7 @@ pub(crate) struct DeprecatedGetBlockNumberResponse { } impl DeprecatedCallContractResponse { - pub(crate) fn new(retdata_size: usize, retdata: Relocatable) -> Self { + pub(crate) const fn new(retdata_size: usize, retdata: Relocatable) -> Self { Self { retdata_size, retdata, @@ -86,19 +86,19 @@ pub(crate) struct DeprecatedDeployResponse { } impl DeprecatedGetTxInfoResponse { - pub fn new(tx_info: Relocatable) -> Self { + pub const fn new(tx_info: Relocatable) -> Self { DeprecatedGetTxInfoResponse { tx_info } } } impl DeprecatedGetBlockTimestampResponse { - pub(crate) fn new(block_timestamp: u64) -> Self { + pub(crate) const fn new(block_timestamp: u64) -> Self { DeprecatedGetBlockTimestampResponse { block_timestamp } } } impl DeprecatedGetSequencerAddressResponse { - pub(crate) fn new(sequencer_address: Address) -> Self { + pub(crate) const fn new(sequencer_address: Address) -> Self { Self { sequencer_address } } } @@ -111,7 +111,7 @@ impl DeprecatedGetCallerAddressResponse { } impl DeprecatedGetTxSignatureResponse { - pub fn new(signature: Relocatable, signature_len: usize) -> Self { + pub const fn new(signature: Relocatable, signature_len: usize) -> Self { DeprecatedGetTxSignatureResponse { signature, signature_len, @@ -119,24 +119,24 @@ impl DeprecatedGetTxSignatureResponse { } } impl DeprecatedGetContractAddressResponse { - pub fn new(contract_address: Address) -> Self { + pub const fn new(contract_address: Address) -> Self { DeprecatedGetContractAddressResponse { contract_address } } } impl DeprecatedStorageReadResponse { - pub fn new(value: Felt252) -> Self { + pub const fn new(value: Felt252) -> Self { DeprecatedStorageReadResponse { value } } } impl DeprecatedGetBlockNumberResponse { - pub(crate) fn new(block_number: u64) -> Self { + pub(crate) const fn new(block_number: u64) -> Self { Self { block_number } } } impl DeprecatedDeployResponse { - pub(crate) fn new( + pub(crate) const fn new( contract_address: Felt252, constructor_retdata_size: Felt252, constructor_retdata: Relocatable, diff --git a/src/syscalls/syscall_response.rs b/src/syscalls/syscall_response.rs index d8e39ae2c..da61935d8 100644 --- a/src/syscalls/syscall_response.rs +++ b/src/syscalls/syscall_response.rs @@ -76,7 +76,7 @@ impl SyscallResponse { // ---------------------- /// Represents the response of get_block_timestamp syscall. -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq, Eq)] pub struct GetBlockTimestampResponse { /// The block timestamp. pub timestamp: Felt252, @@ -104,7 +104,7 @@ pub struct FailureReason { } /// Represents the response of call_contract syscall -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq, Eq)] pub struct CallContractResponse { /// The retdata segment start. pub retdata_start: Relocatable, @@ -113,14 +113,14 @@ pub struct CallContractResponse { } /// Represents the response of get_block_hash syscall -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq, Eq)] pub struct GetBlockHashResponse { /// The returned hash. pub block_hash: Felt252, } /// Represents the response of the `keccak` syscall -#[derive(Clone, Debug, PartialEq)] +#[derive(Clone, Debug, PartialEq, Eq)] pub struct KeccakResponse { /// The returned hash. pub hash_low: Felt252, diff --git a/src/transaction/deploy.rs b/src/transaction/deploy.rs index b65e8559e..0c14512c7 100644 --- a/src/transaction/deploy.rs +++ b/src/transaction/deploy.rs @@ -125,7 +125,7 @@ impl Deploy { } /// Returns the class hash of the deployed contract - pub fn class_hash(&self) -> ClassHash { + pub const fn class_hash(&self) -> ClassHash { self.contract_hash } @@ -351,7 +351,7 @@ mod tests { class_hash_bytes ); - let storage_key = calculate_sn_keccak("owner".as_bytes()); + let storage_key = calculate_sn_keccak(b"owner"); assert_eq!( state diff --git a/src/transaction/invoke_function.rs b/src/transaction/invoke_function.rs index f532cd04b..e430fb39d 100644 --- a/src/transaction/invoke_function.rs +++ b/src/transaction/invoke_function.rs @@ -1174,9 +1174,7 @@ mod tests { fn test_reverted_transaction_wrong_entry_point() { let internal_invoke_function = InvokeFunction { contract_address: Address(0.into()), - entry_point_selector: Felt252::from_bytes_be(&calculate_sn_keccak( - "factorial_".as_bytes(), - )), + entry_point_selector: Felt252::from_bytes_be(&calculate_sn_keccak(b"factorial_")), entry_point_type: EntryPointType::External, calldata: vec![], tx_type: TransactionType::InvokeFunction, diff --git a/src/utils.rs b/src/utils.rs index 3f3b32caa..c6d354232 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -585,7 +585,6 @@ mod test { } #[test] - fn subtract_mappings_test() { let mut a = HashMap::new(); let mut b = HashMap::new(); @@ -634,7 +633,6 @@ mod test { } #[test] - fn to_cache_state_storage_mapping_test() { let mut storage: HashMap<(Address, ClassHash), Felt252> = HashMap::new(); let address1: Address = Address(1.into());