Skip to content

Replace lazy_static with LazyLock #486

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,11 @@ mod test {
use super::MultiShareBundleMerger;

use alloy_primitives::Address;
use lazy_static::lazy_static;
lazy_static! {
static ref SIGNER_1: Address = Address::random();
static ref SIGNER_2: Address = Address::random();
static ref UNKNOWN_SIGNER: Address = Address::random();
}
use std::sync::LazyLock;

static SIGNER_1: LazyLock<Address> = LazyLock::new(|| Address::random());
static SIGNER_2: LazyLock<Address> = LazyLock::new(|| Address::random());
static UNKNOWN_SIGNER: LazyLock<Address> = LazyLock::new(|| Address::random());

fn new_test_context() -> TestContext<MultiShareBundleMerger<OrderDumper>> {
let signers = vec![*SIGNER_1, *SIGNER_2];
Expand Down
32 changes: 17 additions & 15 deletions crates/rbuilder/src/building/testing/test_chain_state.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use crate::provider::RootHasher;
use crate::roothash::RootHashContext;
use crate::utils::RootHasherImpl;
use crate::{building::BlockBuildingContext, utils::Signer};
use crate::{
building::BlockBuildingContext,
provider::RootHasher,
roothash::RootHashContext,
utils::{RootHasherImpl, Signer},
};
use ahash::HashSet;
use alloy_consensus::{Block, Header, TxEip1559};
use alloy_primitives::{
keccak256, utils::parse_ether, Address, BlockHash, Bytes, TxKind as TransactionKind, B256, B64,
U256,
};
use alloy_rpc_types_beacon::events::{PayloadAttributesData, PayloadAttributesEvent};
use lazy_static::lazy_static;
use reth::{
primitives::{Account, BlockBody, Bytecode},
providers::ProviderFactory,
Expand All @@ -21,7 +22,7 @@ use reth_primitives::{Recovered, TransactionSigned};
use reth_primitives_traits::Block as _;
use reth_provider::test_utils::{create_test_provider_factory, MockNodeTypesWithDB};
use revm_primitives::SpecId;
use std::sync::Arc;
use std::sync::{Arc, LazyLock};

#[derive(Debug, Clone, Copy)]
pub enum NamedAddr {
Expand Down Expand Up @@ -542,15 +543,16 @@ fn selector(func_signature: &str) -> [u8; 4] {
}

// Selectors for functions contained in the test contract (mev-test-contract/src/MevTest.sol).
lazy_static! {
static ref INCREMENT_VALUE_SELECTOR: [u8; 4] = selector("incrementValue(uint256,uint256)");
static ref SENT_TO_SELECTOR: [u8; 4] = selector("sendTo(address)");
static ref SEND_TO_COINBASE_SELECTOR: [u8; 4] = selector("sendToCoinbase()");
static ref REVERT_SELECTOR: [u8; 4] = selector("revert()");
static ref TEST_READ_BALANCE: [u8; 4] = selector("testReadBalance(address)");
static ref TEST_EPHEMERAL_CONTRACT_DESTRUCT: [u8; 4] =
selector("testEphemeralContractDestruct(address)");
}
static INCREMENT_VALUE_SELECTOR: LazyLock<[u8; 4]> =
LazyLock::new(|| selector("incrementValue(uint256,uint256)"));
static SENT_TO_SELECTOR: LazyLock<[u8; 4]> = LazyLock::new(|| selector("sendTo(address)"));
static SEND_TO_COINBASE_SELECTOR: LazyLock<[u8; 4]> =
LazyLock::new(|| selector("sendToCoinbase()"));
static REVERT_SELECTOR: LazyLock<[u8; 4]> = LazyLock::new(|| selector("revert()"));
static TEST_READ_BALANCE: LazyLock<[u8; 4]> =
LazyLock::new(|| selector("testReadBalance(address)"));
static TEST_EPHEMERAL_CONTRACT_DESTRUCT: LazyLock<[u8; 4]> =
LazyLock::new(|| selector("testEphemeralContractDestruct(address)"));

impl TestContracts {
pub fn load() -> Self {
Expand Down
9 changes: 5 additions & 4 deletions crates/rbuilder/src/live_builder/block_list_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ pub mod test {
};

use super::{blocklist_hash, BlockList, BlockListProvider, HttpBlockListProvider};
use lazy_static::lazy_static;
use revm_primitives::{Address, B256};
use std::sync::LazyLock;
use tokio_util::sync::CancellationToken;
use url::Url;

Expand Down Expand Up @@ -318,9 +318,10 @@ pub mod test {
}

const BLOCKED_ADDRESS: &str = "0x05E0b5B40B7b66098C2161A5EE11C5740A3A7C45";
lazy_static! {
static ref BLOCKLIST_LEN_1: String = "[\"".to_string() + BLOCKED_ADDRESS + "\"]";
}

static BLOCKLIST_LEN_1: LazyLock<String> =
LazyLock::new(|| "[\"".to_string() + BLOCKED_ADDRESS + "\"]");

pub const BLOCKLIST_LEN_2: &str = r#"["0x03893a7c7463AE47D46bc7f091665f1893656003","0x01e2919679362dFBC9ee1644Ba9C6da6D6245BB1"]"#;
const EMPTY_BLOCKLIST: &str = r#"[]"#;

Expand Down
129 changes: 63 additions & 66 deletions crates/rbuilder/src/live_builder/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ use ethereum_consensus::{
state_transition::Context as ContextEth,
};
use eyre::Context;
use lazy_static::lazy_static;
use reth::revm::cached::CachedReads;
use reth_chainspec::{Chain, ChainSpec, NamedChain};
use reth_db::DatabaseEnv;
Expand All @@ -59,12 +58,12 @@ use reth_primitives::StaticFileSegment;
use reth_provider::StaticFileProviderFactory;
use serde::Deserialize;
use serde_with::{serde_as, OneOrMany};
use std::collections::HashMap;
use std::{
collections::HashMap,
fmt::Debug,
path::{Path, PathBuf},
str::FromStr,
sync::Arc,
sync::{Arc, LazyLock},
time::Duration,
};
use tracing::{info, warn};
Expand Down Expand Up @@ -625,65 +624,64 @@ fn get_signing_domain(
Ok(B256::from(&compute_builder_domain(&cl_context)?))
}

lazy_static! {
static ref DEFAULT_RELAYS: HashMap<String, RelayConfig> = {
let mut map = HashMap::new();
map.insert(
"flashbots".to_string(),
RelayConfig {
name: "flashbots".to_string(),
url: "http://k8s-default-boostrel-9f278153f5-947835446.us-east-2.elb.amazonaws.com"
.to_string(),
mode: RelayMode::Full,
submit_config: Some(RelaySubmitConfig {
use_ssz_for_submit: true,
use_gzip_for_submit: false,
optimistic: false,
interval_between_submissions_ms: Some(250),
}),
priority: Some(0),
authorization_header: None,
builder_id_header: None,
api_token_header: None,
},
);
map.insert(
"ultrasound-us".to_string(),
RelayConfig {
name: "ultrasound-us".to_string(),
url: "https://relay-builders-us.ultrasound.money".to_string(),
mode: RelayMode::Full,
submit_config: Some(RelaySubmitConfig {
use_ssz_for_submit: true,
use_gzip_for_submit: true,
optimistic: true,
interval_between_submissions_ms: None,
}),
priority: Some(0),
authorization_header: None,
builder_id_header: None,
api_token_header: None,
},
);
map.insert(
"ultrasound-eu".to_string(),
RelayConfig {
name: "ultrasound-eu".to_string(),
url: "https://relay-builders-eu.ultrasound.money".to_string(),
mode: RelayMode::Full,
submit_config: Some(RelaySubmitConfig {
use_ssz_for_submit: true,
use_gzip_for_submit: true,
optimistic: true,
interval_between_submissions_ms: None,
}),
priority: Some(0),
authorization_header: None,
builder_id_header: None,
api_token_header: None,
},
);
map.insert(
static DEFAULT_RELAYS: LazyLock<HashMap<String, RelayConfig>> = LazyLock::new(|| {
let mut map = HashMap::new();
map.insert(
"flashbots".to_string(),
RelayConfig {
name: "flashbots".to_string(),
url: "http://k8s-default-boostrel-9f278153f5-947835446.us-east-2.elb.amazonaws.com"
.to_string(),
mode: RelayMode::Full,
submit_config: Some(RelaySubmitConfig {
use_ssz_for_submit: true,
use_gzip_for_submit: false,
optimistic: false,
interval_between_submissions_ms: Some(250),
}),
priority: Some(0),
authorization_header: None,
builder_id_header: None,
api_token_header: None,
},
);
map.insert(
"ultrasound-us".to_string(),
RelayConfig {
name: "ultrasound-us".to_string(),
url: "https://relay-builders-us.ultrasound.money".to_string(),
mode: RelayMode::Full,
submit_config: Some(RelaySubmitConfig {
use_ssz_for_submit: true,
use_gzip_for_submit: true,
optimistic: true,
interval_between_submissions_ms: None,
}),
priority: Some(0),
authorization_header: None,
builder_id_header: None,
api_token_header: None,
},
);
map.insert(
"ultrasound-eu".to_string(),
RelayConfig {
name: "ultrasound-eu".to_string(),
url: "https://relay-builders-eu.ultrasound.money".to_string(),
mode: RelayMode::Full,
submit_config: Some(RelaySubmitConfig {
use_ssz_for_submit: true,
use_gzip_for_submit: true,
optimistic: true,
interval_between_submissions_ms: None,
}),
priority: Some(0),
authorization_header: None,
builder_id_header: None,
api_token_header: None,
},
);
map.insert(
"agnostic".to_string(),
RelayConfig {
name: "agnostic".to_string(),
Expand All @@ -700,7 +698,7 @@ lazy_static! {
api_token_header: None,
},
);
map.insert(
map.insert(
"playground".to_string(),
RelayConfig {
name: "playground".to_string(),
Expand All @@ -718,9 +716,8 @@ lazy_static! {
api_token_header: None,
},
);
map
};
}
map
});

#[cfg(test)]
mod test {
Expand Down
17 changes: 9 additions & 8 deletions crates/rbuilder/src/telemetry/dynamic_logs.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
//! This module provides a functionality to dynamically change the log level

use lazy_static::lazy_static;
use parking_lot::Mutex;
use std::{fs::File, path::PathBuf, sync::Arc};
use std::{
fs::File,
path::PathBuf,
sync::{Arc, LazyLock},
};
use tracing_subscriber::{
filter::Filtered, fmt, layer::SubscriberExt, reload, reload::Handle, util::SubscriberInitExt,
EnvFilter, Layer, Registry,
Expand All @@ -11,12 +14,10 @@ use tracing_subscriber::{
type BoxedLayer = Box<dyn Layer<Registry> + Send + Sync>;
type FilteredLayer = Filtered<BoxedLayer, EnvFilter, Registry>;

lazy_static! {
static ref DEFAULT_CONFIG: Arc<Mutex<LoggerConfig>> =
Arc::new(Mutex::new(LoggerConfig::default()));
static ref RELOAD_HANDLE: Arc<Mutex<Option<Handle<FilteredLayer, Registry>>>> =
Arc::new(Mutex::new(None));
}
static DEFAULT_CONFIG: LazyLock<Arc<Mutex<LoggerConfig>>> =
LazyLock::new(|| Arc::new(Mutex::new(LoggerConfig::default())));
static RELOAD_HANDLE: LazyLock<Arc<Mutex<Option<Handle<FilteredLayer, Registry>>>>> =
LazyLock::new(|| Arc::new(Mutex::new(None)));

#[derive(Debug, Clone)]
pub struct LoggerConfig {
Expand Down
14 changes: 8 additions & 6 deletions crates/rbuilder/src/utils/error_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
//! Writer limits amount of errors that can be written to the db.

use crossbeam_queue::ArrayQueue;
use lazy_static::lazy_static;
use parking_lot::Mutex;
use sqlx::{sqlite::SqliteConnectOptions, ConnectOptions, Executor, SqliteConnection};
use std::{path::Path, sync::Arc, time::Duration};
use std::{
path::Path,
sync::{Arc, LazyLock},
time::Duration,
};
use tokio_util::sync::CancellationToken;
use tracing::{error, info_span, warn};

Expand All @@ -27,10 +30,9 @@ struct ErrorEvent {
payload: String,
}

lazy_static! {
/// Not using null object pattern due to some generic on trait problems.
static ref EVENT_QUEUE: Mutex<Option<Arc<ArrayQueue<ErrorEvent>>>> = Mutex::new(None);
}
/// Not using null object pattern due to some generic on trait problems.
static EVENT_QUEUE: LazyLock<Mutex<Option<Arc<ArrayQueue<ErrorEvent>>>>> =
LazyLock::new(|| Mutex::new(None));

fn event_queue() -> Option<Arc<ArrayQueue<ErrorEvent>>> {
EVENT_QUEUE.lock().clone()
Expand Down