From 09d58d7f4e0da9ed4b22293e80bb92831004cea9 Mon Sep 17 00:00:00 2001 From: Dori Medini Date: Thu, 22 Aug 2024 18:26:09 +0300 Subject: [PATCH] chore: move from once_cell Lazy to LazyLock Signed-off-by: Dori Medini --- Cargo.lock | 2 -- crates/blockifier/Cargo.toml | 1 - .../src/transaction/transactions_test.rs | 7 +++---- crates/blockifier/src/versioned_constants.rs | 5 ++--- crates/papyrus_execution/src/lib.rs | 9 ++++---- crates/papyrus_load_test/src/lib.rs | 5 +++-- crates/starknet_api/Cargo.toml | 1 - .../src/block_hash/block_hash_calculator.rs | 5 +++-- .../src/block_hash/state_diff_hash.rs | 5 +++-- crates/starknet_api/src/core.rs | 4 ++-- crates/starknet_api/src/transaction_hash.rs | 21 ++++++++++--------- 11 files changed, 31 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 37f3e4bd9ad..720df3832fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -993,7 +993,6 @@ dependencies = [ "num-integer", "num-rational 0.4.2", "num-traits 0.2.19", - "once_cell", "papyrus_config", "paste", "phf", @@ -8776,7 +8775,6 @@ dependencies = [ "hex", "indexmap 2.2.6", "itertools 0.12.1", - "once_cell", "primitive-types", "rstest", "serde", diff --git a/crates/blockifier/Cargo.toml b/crates/blockifier/Cargo.toml index 3b5166d4bc1..ff05d295a1a 100644 --- a/crates/blockifier/Cargo.toml +++ b/crates/blockifier/Cargo.toml @@ -37,7 +37,6 @@ num-bigint.workspace = true num-integer.workspace = true num-rational.workspace = true num-traits.workspace = true -once_cell.workspace = true papyrus_config.workspace = true paste.workspace = true phf.workspace = true diff --git a/crates/blockifier/src/transaction/transactions_test.rs b/crates/blockifier/src/transaction/transactions_test.rs index ba19708ece9..d2cc57470a6 100644 --- a/crates/blockifier/src/transaction/transactions_test.rs +++ b/crates/blockifier/src/transaction/transactions_test.rs @@ -1,12 +1,11 @@ use std::collections::{HashMap, HashSet}; -use std::sync::Arc; +use std::sync::{Arc, LazyLock}; use assert_matches::assert_matches; use cairo_vm::types::builtin_name::BuiltinName; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; use num_bigint::BigUint; use num_traits::Pow; -use once_cell::sync::Lazy; use pretty_assertions::assert_eq; use rstest::{fixture, rstest}; use starknet_api::core::{ChainId, ClassHash, ContractAddress, EthAddress, Nonce, PatriciaKey}; @@ -131,8 +130,8 @@ use crate::{ retdata, }; -static VERSIONED_CONSTANTS: Lazy = - Lazy::new(VersionedConstants::create_for_testing); +static VERSIONED_CONSTANTS: LazyLock = + LazyLock::new(VersionedConstants::create_for_testing); #[fixture] fn tx_initial_gas() -> u64 { diff --git a/crates/blockifier/src/versioned_constants.rs b/crates/blockifier/src/versioned_constants.rs index cb7d7086a1a..f6b50e855c5 100644 --- a/crates/blockifier/src/versioned_constants.rs +++ b/crates/blockifier/src/versioned_constants.rs @@ -1,13 +1,12 @@ use std::collections::{HashMap, HashSet}; use std::io; use std::path::Path; -use std::sync::Arc; +use std::sync::{Arc, LazyLock}; use cairo_vm::types::builtin_name::BuiltinName; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; use indexmap::{IndexMap, IndexSet}; use num_rational::Ratio; -use once_cell::sync::Lazy; use paste::paste; use serde::de::Error as DeserializationError; use serde::{Deserialize, Deserializer}; @@ -43,7 +42,7 @@ macro_rules! define_versioned_constants { $( pub(crate) const []: &str = include_str!($path_to_json); - static []: Lazy = Lazy::new(|| { + static []: LazyLock = LazyLock::new(|| { serde_json::from_str([]) .expect(&format!("Versioned constants {} is malformed.", $path_to_json)) }); diff --git a/crates/papyrus_execution/src/lib.rs b/crates/papyrus_execution/src/lib.rs index cf33b85b2c6..f08fadcebd7 100644 --- a/crates/papyrus_execution/src/lib.rs +++ b/crates/papyrus_execution/src/lib.rs @@ -23,7 +23,7 @@ use std::cell::Cell; use std::collections::BTreeMap; use std::num::NonZeroU128; use std::path::Path; -use std::sync::Arc; +use std::sync::{Arc, LazyLock}; use blockifier::blockifier::block::{pre_process_block, BlockInfo, BlockNumberHashPair, GasPrices}; use blockifier::bouncer::BouncerConfig; @@ -50,7 +50,6 @@ use cairo_vm::types::builtin_name::BuiltinName; use cairo_vm::vm::runners::cairo_runner::ExecutionResources; use execution_utils::{get_trace_constructor, induced_state_diff}; use objects::{PriceUnit, TransactionSimulationOutput}; -use once_cell::sync::Lazy; use papyrus_common::transaction_hash::get_transaction_hash; use papyrus_common::TransactionOptions; use papyrus_config::dumping::{ser_param, SerializeConfig}; @@ -99,16 +98,16 @@ const INITIAL_GAS_COST: u64 = 10000000000; /// Result type for execution functions. pub type ExecutionResult = Result; -static VERSIONED_CONSTANTS_13_0: Lazy = Lazy::new(|| { +static VERSIONED_CONSTANTS_13_0: LazyLock = LazyLock::new(|| { VersionedConstants::try_from(Path::new("./resources/versioned_constants_13_0.json")) .expect("Versioned constants JSON file is malformed") }); -static VERSIONED_CONSTANTS_13_1: Lazy = Lazy::new(|| { +static VERSIONED_CONSTANTS_13_1: LazyLock = LazyLock::new(|| { VersionedConstants::try_from(Path::new("./resources/versioned_constants_13_1.json")) .expect("Versioned constants JSON file is malformed") }); -static VERSIONED_CONSTANTS_13_2: Lazy = Lazy::new(|| { +static VERSIONED_CONSTANTS_13_2: LazyLock = LazyLock::new(|| { VersionedConstants::try_from(Path::new("./resources/versioned_constants_13_2.json")) .expect("Versioned constants JSON file is malformed") }); diff --git a/crates/papyrus_load_test/src/lib.rs b/crates/papyrus_load_test/src/lib.rs index bdc031a2a3c..e8e259cef9a 100644 --- a/crates/papyrus_load_test/src/lib.rs +++ b/crates/papyrus_load_test/src/lib.rs @@ -13,10 +13,11 @@ mod precision_test; pub mod scenarios; pub mod transactions; +use std::sync::LazyLock; use std::{env, fs}; use goose::goose::{GooseUser, TransactionError}; -use once_cell::sync::{Lazy, OnceCell}; +use once_cell::sync::OnceCell; use rand::Rng; use serde::Deserialize; use serde_json::{json, Value as jsonVal}; @@ -109,7 +110,7 @@ const TRACE_BLOCK_TRANSACTIONS_BY_HASH_WEIGHT: usize = 10; const TRACE_BLOCK_TRANSACTIONS_BY_NUMBER_WEIGHT: usize = 10; const TRACE_TRANSACTION_WEIGHT: usize = 10; -static RPC_VERSION_ID: Lazy = Lazy::new(|| match std::env::var("VERSION_ID") { +static RPC_VERSION_ID: LazyLock = LazyLock::new(|| match std::env::var("VERSION_ID") { Ok(version_id) => version_id, Err(_) => unreachable!("VERSION_ID environment variable is not set"), }); diff --git a/crates/starknet_api/Cargo.toml b/crates/starknet_api/Cargo.toml index cc04220d6c2..52597dcf4df 100644 --- a/crates/starknet_api/Cargo.toml +++ b/crates/starknet_api/Cargo.toml @@ -16,7 +16,6 @@ derive_more = "0.99.17" hex = "0.4.3" indexmap = { version = "2.1.0", features = ["serde"] } itertools = "0.12.1" -once_cell = "1.17.1" primitive-types = { version = "0.12.1", features = ["serde"] } serde = { version = "1.0.130", features = ["derive", "rc"] } serde_json = "1.0.81" diff --git a/crates/starknet_api/src/block_hash/block_hash_calculator.rs b/crates/starknet_api/src/block_hash/block_hash_calculator.rs index ae36d164b77..a4dcdf3c404 100644 --- a/crates/starknet_api/src/block_hash/block_hash_calculator.rs +++ b/crates/starknet_api/src/block_hash/block_hash_calculator.rs @@ -1,4 +1,5 @@ -use once_cell::sync::Lazy; +use std::sync::LazyLock; + use serde::{Deserialize, Serialize}; use starknet_types_core::felt::Felt; use starknet_types_core::hash::Poseidon; @@ -27,7 +28,7 @@ use crate::transaction_hash::ascii_as_felt; #[path = "block_hash_calculator_test.rs"] mod block_hash_calculator_test; -static STARKNET_BLOCK_HASH0: Lazy = Lazy::new(|| { +static STARKNET_BLOCK_HASH0: LazyLock = LazyLock::new(|| { ascii_as_felt("STARKNET_BLOCK_HASH0").expect("ascii_as_felt failed for 'STARKNET_BLOCK_HASH0'") }); diff --git a/crates/starknet_api/src/block_hash/state_diff_hash.rs b/crates/starknet_api/src/block_hash/state_diff_hash.rs index d588992bfda..9713446d6f3 100644 --- a/crates/starknet_api/src/block_hash/state_diff_hash.rs +++ b/crates/starknet_api/src/block_hash/state_diff_hash.rs @@ -1,5 +1,6 @@ +use std::sync::LazyLock; + use indexmap::IndexMap; -use once_cell::sync::Lazy; use starknet_types_core::felt::Felt; use crate::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce, StateDiffCommitment}; @@ -12,7 +13,7 @@ use crate::transaction_hash::ascii_as_felt; #[path = "state_diff_hash_test.rs"] mod state_diff_hash_test; -static STARKNET_STATE_DIFF0: Lazy = Lazy::new(|| { +static STARKNET_STATE_DIFF0: LazyLock = LazyLock::new(|| { ascii_as_felt("STARKNET_STATE_DIFF0").expect("ascii_as_felt failed for 'STARKNET_STATE_DIFF0'") }); diff --git a/crates/starknet_api/src/core.rs b/crates/starknet_api/src/core.rs index ab9f3754b7a..03147360c60 100644 --- a/crates/starknet_api/src/core.rs +++ b/crates/starknet_api/src/core.rs @@ -4,9 +4,9 @@ mod core_test; use core::fmt::Display; use std::fmt::Debug; +use std::sync::LazyLock; use derive_more::Display; -use once_cell::sync::Lazy; use primitive_types::H160; use serde::{Deserialize, Deserializer, Serialize, Serializer}; use starknet_types_core::felt::{Felt, NonZeroFelt}; @@ -117,7 +117,7 @@ pub const CONTRACT_ADDRESS_PREFIX: &str = "STARKNET_CONTRACT_ADDRESS"; /// The size of the contract address domain. pub const CONTRACT_ADDRESS_DOMAIN_SIZE: Felt = Felt::from_hex_unchecked(PATRICIA_KEY_UPPER_BOUND); /// The address upper bound; it is defined to be congruent with the storage var address upper bound. -pub static L2_ADDRESS_UPPER_BOUND: Lazy = Lazy::new(|| { +pub static L2_ADDRESS_UPPER_BOUND: LazyLock = LazyLock::new(|| { NonZeroFelt::try_from(CONTRACT_ADDRESS_DOMAIN_SIZE - Felt::from(MAX_STORAGE_ITEM_SIZE)).unwrap() }); diff --git a/crates/starknet_api/src/transaction_hash.rs b/crates/starknet_api/src/transaction_hash.rs index b7daf1fd976..b769d640726 100644 --- a/crates/starknet_api/src/transaction_hash.rs +++ b/crates/starknet_api/src/transaction_hash.rs @@ -1,4 +1,5 @@ -use once_cell::sync::Lazy; +use std::sync::LazyLock; + use starknet_types_core::felt::Felt; use crate::block::BlockNumber; @@ -35,17 +36,17 @@ const DATA_AVAILABILITY_MODE_BITS: usize = 32; const L1_GAS: &ResourceName = b"\0L1_GAS"; const L2_GAS: &ResourceName = b"\0L2_GAS"; -static DECLARE: Lazy = - Lazy::new(|| ascii_as_felt("declare").expect("ascii_as_felt failed for 'declare'")); -static DEPLOY: Lazy = - Lazy::new(|| ascii_as_felt("deploy").expect("ascii_as_felt failed for 'deploy'")); -static DEPLOY_ACCOUNT: Lazy = Lazy::new(|| { +static DECLARE: LazyLock = + LazyLock::new(|| ascii_as_felt("declare").expect("ascii_as_felt failed for 'declare'")); +static DEPLOY: LazyLock = + LazyLock::new(|| ascii_as_felt("deploy").expect("ascii_as_felt failed for 'deploy'")); +static DEPLOY_ACCOUNT: LazyLock = LazyLock::new(|| { ascii_as_felt("deploy_account").expect("ascii_as_felt failed for 'deploy_account'") }); -static INVOKE: Lazy = - Lazy::new(|| ascii_as_felt("invoke").expect("ascii_as_felt failed for 'invoke'")); -static L1_HANDLER: Lazy = - Lazy::new(|| ascii_as_felt("l1_handler").expect("ascii_as_felt failed for 'l1_handler'")); +static INVOKE: LazyLock = + LazyLock::new(|| ascii_as_felt("invoke").expect("ascii_as_felt failed for 'invoke'")); +static L1_HANDLER: LazyLock = + LazyLock::new(|| ascii_as_felt("l1_handler").expect("ascii_as_felt failed for 'l1_handler'")); const CONSTRUCTOR_ENTRY_POINT_SELECTOR: Felt = Felt::from_hex_unchecked("0x28ffe4ff0f226a9107253e17a904099aa4f63a02a5621de0576e5aa71bc5194");