Skip to content

Commit

Permalink
chore: move from once_cell Lazy to LazyLock (#571)
Browse files Browse the repository at this point in the history
Signed-off-by: Dori Medini <dori@starkware.co>
  • Loading branch information
dorimedini-starkware authored Aug 25, 2024
1 parent e2f2f4a commit 81075ad
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 37 deletions.
3 changes: 0 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# More info on Cargo Chef: https://github.com/LukeMathWalker/cargo-chef

# We start by creating a base image using 'clux/muslrust' with additional required tools.
FROM clux/muslrust:1.78.0-stable AS chef
FROM clux/muslrust:1.80.0-stable AS chef
WORKDIR /app
RUN apt update && apt install -y clang unzip
RUN cargo install cargo-chef
Expand Down
1 change: 0 additions & 1 deletion crates/blockifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions crates/blockifier/src/transaction/transactions_test.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -131,8 +130,8 @@ use crate::{
retdata,
};

static VERSIONED_CONSTANTS: Lazy<VersionedConstants> =
Lazy::new(VersionedConstants::create_for_testing);
static VERSIONED_CONSTANTS: LazyLock<VersionedConstants> =
LazyLock::new(VersionedConstants::create_for_testing);

#[fixture]
fn tx_initial_gas() -> u64 {
Expand Down
5 changes: 2 additions & 3 deletions crates/blockifier/src/versioned_constants.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -43,7 +42,7 @@ macro_rules! define_versioned_constants {
$(
pub(crate) const [<VERSIONED_CONSTANTS_ $variant:upper _JSON>]: &str =
include_str!($path_to_json);
static [<VERSIONED_CONSTANTS_ $variant:upper>]: Lazy<VersionedConstants> = Lazy::new(|| {
static [<VERSIONED_CONSTANTS_ $variant:upper>]: LazyLock<VersionedConstants> = LazyLock::new(|| {
serde_json::from_str([<VERSIONED_CONSTANTS_ $variant:upper _JSON>])
.expect(&format!("Versioned constants {} is malformed.", $path_to_json))
});
Expand Down
1 change: 0 additions & 1 deletion crates/papyrus_execution/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ cairo-vm.workspace = true
indexmap.workspace = true
itertools.workspace = true
lazy_static.workspace = true
once_cell.workspace = true
papyrus_common.workspace = true
papyrus_config.workspace = true
papyrus_storage.workspace = true
Expand Down
9 changes: 4 additions & 5 deletions crates/papyrus_execution/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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};
Expand Down Expand Up @@ -99,16 +98,16 @@ const INITIAL_GAS_COST: u64 = 10000000000;
/// Result type for execution functions.
pub type ExecutionResult<T> = Result<T, ExecutionError>;

static VERSIONED_CONSTANTS_13_0: Lazy<VersionedConstants> = Lazy::new(|| {
static VERSIONED_CONSTANTS_13_0: LazyLock<VersionedConstants> = 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<VersionedConstants> = Lazy::new(|| {
static VERSIONED_CONSTANTS_13_1: LazyLock<VersionedConstants> = 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<VersionedConstants> = Lazy::new(|| {
static VERSIONED_CONSTANTS_13_2: LazyLock<VersionedConstants> = LazyLock::new(|| {
VersionedConstants::try_from(Path::new("./resources/versioned_constants_13_2.json"))
.expect("Versioned constants JSON file is malformed")
});
Expand Down
5 changes: 3 additions & 2 deletions crates/papyrus_load_test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<String> = Lazy::new(|| match std::env::var("VERSION_ID") {
static RPC_VERSION_ID: LazyLock<String> = LazyLock::new(|| match std::env::var("VERSION_ID") {
Ok(version_id) => version_id,
Err(_) => unreachable!("VERSION_ID environment variable is not set"),
});
1 change: 0 additions & 1 deletion crates/starknet_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 3 additions & 2 deletions crates/starknet_api/src/block_hash/block_hash_calculator.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<Felt> = Lazy::new(|| {
static STARKNET_BLOCK_HASH0: LazyLock<Felt> = LazyLock::new(|| {
ascii_as_felt("STARKNET_BLOCK_HASH0").expect("ascii_as_felt failed for 'STARKNET_BLOCK_HASH0'")
});

Expand Down
5 changes: 3 additions & 2 deletions crates/starknet_api/src/block_hash/state_diff_hash.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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<Felt> = Lazy::new(|| {
static STARKNET_STATE_DIFF0: LazyLock<Felt> = LazyLock::new(|| {
ascii_as_felt("STARKNET_STATE_DIFF0").expect("ascii_as_felt failed for 'STARKNET_STATE_DIFF0'")
});

Expand Down
4 changes: 2 additions & 2 deletions crates/starknet_api/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<NonZeroFelt> = Lazy::new(|| {
pub static L2_ADDRESS_UPPER_BOUND: LazyLock<NonZeroFelt> = LazyLock::new(|| {
NonZeroFelt::try_from(CONTRACT_ADDRESS_DOMAIN_SIZE - Felt::from(MAX_STORAGE_ITEM_SIZE)).unwrap()
});

Expand Down
21 changes: 11 additions & 10 deletions crates/starknet_api/src/transaction_hash.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use once_cell::sync::Lazy;
use std::sync::LazyLock;

use starknet_types_core::felt::Felt;

use crate::block::BlockNumber;
Expand Down Expand Up @@ -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<Felt> =
Lazy::new(|| ascii_as_felt("declare").expect("ascii_as_felt failed for 'declare'"));
static DEPLOY: Lazy<Felt> =
Lazy::new(|| ascii_as_felt("deploy").expect("ascii_as_felt failed for 'deploy'"));
static DEPLOY_ACCOUNT: Lazy<Felt> = Lazy::new(|| {
static DECLARE: LazyLock<Felt> =
LazyLock::new(|| ascii_as_felt("declare").expect("ascii_as_felt failed for 'declare'"));
static DEPLOY: LazyLock<Felt> =
LazyLock::new(|| ascii_as_felt("deploy").expect("ascii_as_felt failed for 'deploy'"));
static DEPLOY_ACCOUNT: LazyLock<Felt> = LazyLock::new(|| {
ascii_as_felt("deploy_account").expect("ascii_as_felt failed for 'deploy_account'")
});
static INVOKE: Lazy<Felt> =
Lazy::new(|| ascii_as_felt("invoke").expect("ascii_as_felt failed for 'invoke'"));
static L1_HANDLER: Lazy<Felt> =
Lazy::new(|| ascii_as_felt("l1_handler").expect("ascii_as_felt failed for 'l1_handler'"));
static INVOKE: LazyLock<Felt> =
LazyLock::new(|| ascii_as_felt("invoke").expect("ascii_as_felt failed for 'invoke'"));
static L1_HANDLER: LazyLock<Felt> =
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");

Expand Down

0 comments on commit 81075ad

Please sign in to comment.