From c971b6756b151ab13c1e03f681eb2c0a48c21994 Mon Sep 17 00:00:00 2001 From: Martin Magnus Date: Mon, 6 Jan 2025 11:22:25 +0100 Subject: [PATCH 1/3] Replace `once-cell` and `lazy-static` with `std` types (#3208) # Description Since the functionality of `once-cell` and `lazy-static` was moved into the standard library it's the idiomatic thing to use the `std` types instead of the crates. # Changes Replaced all uses of `once-cell` and `lazy-static` with the equivalent `std` type. --- Cargo.lock | 7 ---- Cargo.toml | 2 -- crates/driver/Cargo.toml | 1 - crates/driver/src/infra/time.rs | 6 ++-- crates/e2e/src/api/zeroex.rs | 28 ++++++++-------- crates/ethrpc/Cargo.toml | 1 - crates/ethrpc/src/multicall.rs | 11 +++---- crates/model/Cargo.toml | 1 - crates/model/src/lib.rs | 24 +++++++------- crates/observe/Cargo.toml | 1 - crates/observe/src/metrics.rs | 10 ++++-- crates/shared/Cargo.toml | 1 - crates/shared/src/external_prices.rs | 10 +++--- .../shared/src/price_estimation/native/mod.rs | 5 ++- .../sources/balancer_v2/swap/fixed_point.rs | 21 ++++++------ .../swap/fixed_point/logexpmath.rs | 33 +++++++++++-------- .../sources/balancer_v2/swap/stable_math.rs | 6 ++-- .../sources/balancer_v2/swap/weighted_math.rs | 12 +++---- .../src/sources/uniswap_v2/pool_fetching.rs | 10 +++--- crates/solver/Cargo.toml | 2 -- crates/solver/src/interactions/balancer_v2.rs | 11 +++---- crates/solver/src/liquidity/slippage.rs | 12 ++++--- crates/solver/src/liquidity_collector.rs | 12 ++++--- 23 files changed, 109 insertions(+), 118 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d2bbe2848c..05cfd11c17 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1816,7 +1816,6 @@ dependencies = [ "hyper", "indexmap 2.2.6", "itertools 0.12.1", - "lazy_static", "maplit", "mimalloc", "mockall 0.12.1", @@ -2077,7 +2076,6 @@ dependencies = [ "hex", "hex-literal", "itertools 0.12.1", - "lazy_static", "maplit", "mockall 0.12.1", "observe", @@ -3118,7 +3116,6 @@ dependencies = [ "derive_more 1.0.0", "hex", "hex-literal", - "lazy_static", "maplit", "num", "number", @@ -3331,7 +3328,6 @@ dependencies = [ "atty", "console-subscriber", "futures", - "once_cell", "pin-project-lite", "prometheus", "prometheus-metric-storage", @@ -4485,7 +4481,6 @@ dependencies = [ "humantime", "indexmap 2.2.6", "itertools 0.12.1", - "lazy_static", "maplit", "mockall 0.12.1", "model", @@ -4581,14 +4576,12 @@ dependencies = [ "hex", "hex-literal", "itertools 0.12.1", - "lazy_static", "maplit", "mockall 0.12.1", "model", "num", "number", "observe", - "once_cell", "primitive-types", "prometheus", "prometheus-metric-storage", diff --git a/Cargo.toml b/Cargo.toml index f2ce7391f6..6c819517e9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,11 +28,9 @@ humantime-serde = "1.1.1" hyper = "0.14.29" indexmap = "2.2.6" itertools = "0.12.1" -lazy_static = "1.4.0" maplit = "1.0.2" mockall = "0.12.1" num = "0.4.3" -once_cell = "1.19.0" primitive-types = "0.12" prometheus = "0.13.4" prometheus-metric-storage = "0.5.0" diff --git a/crates/driver/Cargo.toml b/crates/driver/Cargo.toml index 0675c6a046..83c2a1983d 100644 --- a/crates/driver/Cargo.toml +++ b/crates/driver/Cargo.toml @@ -35,7 +35,6 @@ hex-literal = { workspace = true } humantime = { workspace = true } humantime-serde = { workspace = true } hyper = { workspace = true } -lazy_static = { workspace = true } indexmap = { workspace = true, features = ["serde"] } itertools = { workspace = true } mimalloc = { workspace = true } diff --git a/crates/driver/src/infra/time.rs b/crates/driver/src/infra/time.rs index f954ea3f64..e5bc83ee73 100644 --- a/crates/driver/src/infra/time.rs +++ b/crates/driver/src/infra/time.rs @@ -7,7 +7,7 @@ pub fn now() -> chrono::DateTime { /// During tests, the time is fixed. #[cfg(test)] pub fn now() -> chrono::DateTime { - use std::sync::OnceLock; - static TIME: OnceLock> = OnceLock::new(); - TIME.get_or_init(chrono::Utc::now).to_owned() + use std::sync::LazyLock; + static TIME: LazyLock> = LazyLock::new(chrono::Utc::now); + *TIME } diff --git a/crates/e2e/src/api/zeroex.rs b/crates/e2e/src/api/zeroex.rs index bef51dffe9..f3db4a4d73 100644 --- a/crates/e2e/src/api/zeroex.rs +++ b/crates/e2e/src/api/zeroex.rs @@ -3,17 +3,14 @@ use { autopilot::domain::eth::U256, chrono::{DateTime, NaiveDateTime, Utc}, driver::domain::eth::H256, - ethcontract::{ - common::abi::{encode, Token}, - private::lazy_static, - }, + ethcontract::common::abi::{encode, Token}, hex_literal::hex, model::DomainSeparator, shared::{ zeroex_api, zeroex_api::{Order, OrderMetadata, OrderRecord, ZeroExSignature}, }, - std::net::SocketAddr, + std::{net::SocketAddr, sync::LazyLock}, warp::{Filter, Reply}, web3::{signing, types::H160}, }; @@ -155,18 +152,19 @@ struct ZeroExDomainSeparator([u8; 32]); impl ZeroExDomainSeparator { // See pub fn new(chain_id: u64, contract_addr: H160) -> Self { - lazy_static! { - /// The EIP-712 domain name used for computing the domain separator. - static ref DOMAIN_NAME: [u8; 32] = signing::keccak256(b"ZeroEx"); + /// The EIP-712 domain name used for computing the domain separator. + static DOMAIN_NAME: LazyLock<[u8; 32]> = LazyLock::new(|| signing::keccak256(b"ZeroEx")); - /// The EIP-712 domain version used for computing the domain separator. - static ref DOMAIN_VERSION: [u8; 32] = signing::keccak256(b"1.0.0"); + /// The EIP-712 domain version used for computing the domain separator. + static DOMAIN_VERSION: LazyLock<[u8; 32]> = LazyLock::new(|| signing::keccak256(b"1.0.0")); + + /// The EIP-712 domain type used computing the domain separator. + static DOMAIN_TYPE_HASH: LazyLock<[u8; 32]> = LazyLock::new(|| { + signing::keccak256( + b"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)", + ) + }); - /// The EIP-712 domain type used computing the domain separator. - static ref DOMAIN_TYPE_HASH: [u8; 32] = signing::keccak256( - b"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)", - ); - } let abi_encode_string = encode(&[ Token::FixedBytes((*DOMAIN_TYPE_HASH).into()), Token::FixedBytes((*DOMAIN_NAME).into()), diff --git a/crates/ethrpc/Cargo.toml b/crates/ethrpc/Cargo.toml index a6e628dabc..fa6a0f9b24 100644 --- a/crates/ethrpc/Cargo.toml +++ b/crates/ethrpc/Cargo.toml @@ -16,7 +16,6 @@ async-trait = { workspace = true } futures = { workspace = true } hex = { workspace = true } hex-literal = { workspace = true } -lazy_static = { workspace = true } mockall = { workspace = true } observe = { path = "../observe" } primitive-types = { workspace = true } diff --git a/crates/ethrpc/src/multicall.rs b/crates/ethrpc/src/multicall.rs index 38d1e72f03..855d5b753e 100644 --- a/crates/ethrpc/src/multicall.rs +++ b/crates/ethrpc/src/multicall.rs @@ -8,8 +8,7 @@ use { tokens::{self, Tokenize as _}, }, hex_literal::hex, - lazy_static::lazy_static, - std::iter, + std::{iter, sync::LazyLock}, web3::{ self, api::Eth, @@ -160,12 +159,12 @@ fn decode(len: usize, return_data: Bytes) -> Vec, ExecutionError> type ReturnData = Vec<(bool, ethcontract::Bytes>)>; fn decode_return_data(len: usize, return_data: Bytes) -> Result { - lazy_static! { - static ref KIND: [ParamType; 1] = [ParamType::Array(Box::new(ParamType::Tuple(vec![ + static KIND: LazyLock<[ParamType; 1]> = LazyLock::new(|| { + [ParamType::Array(Box::new(ParamType::Tuple(vec![ ParamType::Bool, ParamType::Bytes, - ])),)]; - } + ])))] + }); let tokens = ethabi::decode(&*KIND, &return_data.0)?; let (results,) = <(ReturnData,)>::from_token(Token::Tuple(tokens))?; diff --git a/crates/model/Cargo.toml b/crates/model/Cargo.toml index d6a8f19af1..b27956955f 100644 --- a/crates/model/Cargo.toml +++ b/crates/model/Cargo.toml @@ -17,7 +17,6 @@ chrono = { workspace = true, features = ["serde", "clock"] } derive_more = { workspace = true } hex = { workspace = true, default-features = false } hex-literal = { workspace = true } -lazy_static = { workspace = true } number = { path = "../number" } num = { workspace = true } primitive-types = { workspace = true } diff --git a/crates/model/src/lib.rs b/crates/model/src/lib.rs index 044bb67bf5..34f8ab64c0 100644 --- a/crates/model/src/lib.rs +++ b/crates/model/src/lib.rs @@ -12,9 +12,8 @@ pub mod trade; use { hex::{FromHex, FromHexError}, - lazy_static::lazy_static, primitive_types::H160, - std::fmt, + std::{fmt, sync::LazyLock}, web3::{ ethabi::{encode, Token}, signing, @@ -115,18 +114,19 @@ impl std::fmt::Debug for DomainSeparator { impl DomainSeparator { pub fn new(chain_id: u64, contract_address: H160) -> Self { - lazy_static! { - /// The EIP-712 domain name used for computing the domain separator. - static ref DOMAIN_NAME: [u8; 32] = signing::keccak256(b"Gnosis Protocol"); + /// The EIP-712 domain name used for computing the domain separator. + static DOMAIN_NAME: LazyLock<[u8; 32]> = + LazyLock::new(|| signing::keccak256(b"Gnosis Protocol")); - /// The EIP-712 domain version used for computing the domain separator. - static ref DOMAIN_VERSION: [u8; 32] = signing::keccak256(b"v2"); + /// The EIP-712 domain version used for computing the domain separator. + static DOMAIN_VERSION: LazyLock<[u8; 32]> = LazyLock::new(|| signing::keccak256(b"v2")); - /// The EIP-712 domain type used computing the domain separator. - static ref DOMAIN_TYPE_HASH: [u8; 32] = signing::keccak256( - b"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)", - ); - } + /// The EIP-712 domain type used computing the domain separator. + static DOMAIN_TYPE_HASH: LazyLock<[u8; 32]> = LazyLock::new(|| { + signing::keccak256( + b"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)", + ) + }); let abi_encode_string = encode(&[ Token::Uint((*DOMAIN_TYPE_HASH).into()), Token::Uint((*DOMAIN_NAME).into()), diff --git a/crates/observe/Cargo.toml b/crates/observe/Cargo.toml index d02cbfc0ce..f2b1029ef0 100644 --- a/crates/observe/Cargo.toml +++ b/crates/observe/Cargo.toml @@ -10,7 +10,6 @@ atty = "0.2" async-trait = { workspace = true } console-subscriber = "0.3.0" futures = { workspace = true } -once_cell = { workspace = true } pin-project-lite = "0.2.14" prometheus = { workspace = true } prometheus-metric-storage = { workspace = true } diff --git a/crates/observe/src/metrics.rs b/crates/observe/src/metrics.rs index 6afb4175e0..b62023590c 100644 --- a/crates/observe/src/metrics.rs +++ b/crates/observe/src/metrics.rs @@ -1,13 +1,17 @@ use { - once_cell::sync::OnceCell, prometheus::Encoder, - std::{collections::HashMap, convert::Infallible, net::SocketAddr, sync::Arc}, + std::{ + collections::HashMap, + convert::Infallible, + net::SocketAddr, + sync::{Arc, OnceLock}, + }, tokio::task::{self, JoinHandle}, warp::{Filter, Rejection, Reply}, }; /// Global metrics registry used by all components. -static REGISTRY: OnceCell = OnceCell::new(); +static REGISTRY: OnceLock = OnceLock::new(); /// Configure global metrics registry. /// diff --git a/crates/shared/Cargo.toml b/crates/shared/Cargo.toml index 492b6bfb6e..87cb190f1f 100644 --- a/crates/shared/Cargo.toml +++ b/crates/shared/Cargo.toml @@ -34,7 +34,6 @@ hex-literal = { workspace = true } humantime = { workspace = true } indexmap = { workspace = true } itertools = { workspace = true } -lazy_static = { workspace = true } maplit = { workspace = true } mockall = { workspace = true } model = { path = "../model" } diff --git a/crates/shared/src/external_prices.rs b/crates/shared/src/external_prices.rs index f7fb0b427b..6c3dc66c84 100644 --- a/crates/shared/src/external_prices.rs +++ b/crates/shared/src/external_prices.rs @@ -9,10 +9,12 @@ use { crate::conversions::U256Ext, anyhow::{bail, Result}, ethcontract::{H160, U256}, - lazy_static::lazy_static, model::order::BUY_ETH_ADDRESS, num::{BigInt, BigRational, One as _, ToPrimitive as _}, - std::collections::{BTreeMap, HashMap}, + std::{ + collections::{BTreeMap, HashMap}, + sync::LazyLock, + }, }; /// A collection of external prices used for converting token amounts to native @@ -72,9 +74,7 @@ impl Default for ExternalPrices { } } -lazy_static! { - static ref UNIT: BigInt = BigInt::from(1_000_000_000_000_000_000_u128); -} +static UNIT: LazyLock = LazyLock::new(|| BigInt::from(1_000_000_000_000_000_000_u128)); /// Converts a token price from the orderbook API `/auction` endpoint to an /// native token exchange rate. diff --git a/crates/shared/src/price_estimation/native/mod.rs b/crates/shared/src/price_estimation/native/mod.rs index 31ca840575..9a81d44538 100644 --- a/crates/shared/src/price_estimation/native/mod.rs +++ b/crates/shared/src/price_estimation/native/mod.rs @@ -1,12 +1,11 @@ use { crate::price_estimation::{PriceEstimating, PriceEstimationError, Query}, bigdecimal::{BigDecimal, ToPrimitive}, - cached::once_cell::sync::Lazy, futures::FutureExt, model::order::OrderKind, number::nonzero::U256 as NonZeroU256, primitive_types::{H160, U256}, - std::sync::Arc, + std::sync::{Arc, LazyLock}, }; mod coingecko; @@ -19,7 +18,7 @@ pub type NativePriceEstimateResult = Result; /// Convert from normalized price to floating point price pub fn from_normalized_price(price: BigDecimal) -> Option { - static ONE_E18: Lazy = Lazy::new(|| BigDecimal::try_from(1e18).unwrap()); + static ONE_E18: LazyLock = LazyLock::new(|| BigDecimal::try_from(1e18).unwrap()); // Divide by 1e18 to reverse the multiplication by 1e18 let normalized_price = price / ONE_E18.clone(); diff --git a/crates/shared/src/sources/balancer_v2/swap/fixed_point.rs b/crates/shared/src/sources/balancer_v2/swap/fixed_point.rs index f4edd33701..94015f9ed9 100644 --- a/crates/shared/src/sources/balancer_v2/swap/fixed_point.rs +++ b/crates/shared/src/sources/balancer_v2/swap/fixed_point.rs @@ -7,13 +7,13 @@ use { super::error::Error, anyhow::{bail, ensure, Context, Result}, ethcontract::U256, - lazy_static::lazy_static, num::{BigInt, BigRational}, number::conversions::{big_int_to_u256, u256_to_big_int}, std::{ convert::TryFrom, fmt::{self, Debug, Formatter}, str::FromStr, + sync::LazyLock, }, }; @@ -27,16 +27,13 @@ mod logexpmath; /// including error codes, from which the name (Balancer Fixed Point). pub struct Bfp(U256); -lazy_static! { - static ref ONE_18: U256 = U256::exp10(18); - static ref ONE_18_BIGINT: BigInt = u256_to_big_int(&ONE_18); - static ref ZERO: Bfp = Bfp(U256::zero()); - static ref EPSILON: Bfp = Bfp(U256::one()); - static ref ONE: Bfp = Bfp(*ONE_18); - static ref TWO: Bfp = Bfp(*ONE_18 * 2); - static ref FOUR: Bfp = Bfp(*ONE_18 * 4); - static ref MAX_POW_RELATIVE_ERROR: Bfp = Bfp(10000_usize.into()); -} +static ONE_18: LazyLock = LazyLock::new(|| U256::exp10(18)); +static ONE_18_BIGINT: LazyLock = LazyLock::new(|| u256_to_big_int(&ONE_18)); +static ZERO: LazyLock = LazyLock::new(|| Bfp(U256::zero())); +static ONE: LazyLock = LazyLock::new(|| Bfp(*ONE_18)); +static TWO: LazyLock = LazyLock::new(|| Bfp(*ONE_18 * 2)); +static FOUR: LazyLock = LazyLock::new(|| Bfp(*ONE_18 * 4)); +static MAX_POW_RELATIVE_ERROR: LazyLock = LazyLock::new(|| Bfp(10000_usize.into())); impl From for Bfp { fn from(num: usize) -> Self { @@ -223,6 +220,8 @@ mod tests { num::{BigInt, One, Zero}, }; + static EPSILON: LazyLock = LazyLock::new(|| Bfp(U256::one())); + #[test] fn parsing() { assert_eq!("1".parse::().unwrap(), Bfp::one()); diff --git a/crates/shared/src/sources/balancer_v2/swap/fixed_point/logexpmath.rs b/crates/shared/src/sources/balancer_v2/swap/fixed_point/logexpmath.rs index 660049a026..302053b7f6 100644 --- a/crates/shared/src/sources/balancer_v2/swap/fixed_point/logexpmath.rs +++ b/crates/shared/src/sources/balancer_v2/swap/fixed_point/logexpmath.rs @@ -5,27 +5,32 @@ use { super::super::error::Error, ethcontract::{I256, U256}, - lazy_static::lazy_static, - std::convert::TryFrom, + std::{convert::TryFrom, sync::LazyLock}, }; /// Fixed point number stored in a type of bit size 256 that stores exactly 18 /// decimal digits. type Ufixed256x18 = U256; -lazy_static! { - static ref ONE_18: I256 = I256::exp10(18); - static ref ONE_20: I256 = I256::exp10(20); - static ref ONE_36: I256 = I256::exp10(36); - static ref UFIXED256X18_ONE: Ufixed256x18 = U256::try_from(*ONE_18).unwrap(); - static ref MAX_NATURAL_EXPONENT: I256 = ONE_18.checked_mul(I256::from(130_i128)).unwrap(); - static ref MIN_NATURAL_EXPONENT: I256 = ONE_18.checked_mul(I256::from(-41_i128)).unwrap(); - static ref LN_36_LOWER_BOUND: I256 = ONE_18.checked_sub(I256::exp10(17)).unwrap(); - static ref LN_36_UPPER_BOUND: I256 = ONE_18.checked_add(I256::exp10(17)).unwrap(); - static ref MILD_EXPONENT_BOUND: Ufixed256x18 = (U256::one() << 254_u32) +static ONE_18: LazyLock = LazyLock::new(|| I256::exp10(18)); +static ONE_20: LazyLock = LazyLock::new(|| I256::exp10(20)); +static ONE_36: LazyLock = LazyLock::new(|| I256::exp10(36)); +static UFIXED256X18_ONE: LazyLock = + LazyLock::new(|| U256::try_from(*ONE_18).unwrap()); +static MAX_NATURAL_EXPONENT: LazyLock = + LazyLock::new(|| ONE_18.checked_mul(I256::from(130_i128)).unwrap()); +static MIN_NATURAL_EXPONENT: LazyLock = + LazyLock::new(|| ONE_18.checked_mul(I256::from(-41_i128)).unwrap()); +static LN_36_LOWER_BOUND: LazyLock = + LazyLock::new(|| ONE_18.checked_sub(I256::exp10(17)).unwrap()); +static LN_36_UPPER_BOUND: LazyLock = + LazyLock::new(|| ONE_18.checked_add(I256::exp10(17)).unwrap()); +static MILD_EXPONENT_BOUND: LazyLock = LazyLock::new(|| { + (U256::one() << 254_u32) .checked_div(U256::try_from(*ONE_20).unwrap()) - .unwrap(); -} + .unwrap() +}); + fn constant_x_20(i: u32) -> I256 { match i { 2 => 3_200_000_000_000_000_000_000_i128, diff --git a/crates/shared/src/sources/balancer_v2/swap/stable_math.rs b/crates/shared/src/sources/balancer_v2/swap/stable_math.rs index bedd679b78..f7610ef2b0 100644 --- a/crates/shared/src/sources/balancer_v2/swap/stable_math.rs +++ b/crates/shared/src/sources/balancer_v2/swap/stable_math.rs @@ -6,12 +6,10 @@ use { super::error::Error, crate::sources::balancer_v2::swap::{fixed_point::Bfp, math::BalU256}, ethcontract::U256, - lazy_static::lazy_static, + std::sync::LazyLock, }; -lazy_static! { - pub static ref AMP_PRECISION: U256 = U256::from(1000); -} +pub static AMP_PRECISION: LazyLock = LazyLock::new(|| U256::from(1000)); /// https://github.com/balancer-labs/balancer-v2-monorepo/blob/9eb7e44a4e9ebbadfe3c6242a086118298cadc9f/pkg/pool-stable-phantom/contracts/StableMath.sol#L57-L119 fn calculate_invariant(amplification_parameter: U256, balances: &[Bfp]) -> Result { diff --git a/crates/shared/src/sources/balancer_v2/swap/weighted_math.rs b/crates/shared/src/sources/balancer_v2/swap/weighted_math.rs index ae9fb72268..5b0aafa701 100644 --- a/crates/shared/src/sources/balancer_v2/swap/weighted_math.rs +++ b/crates/shared/src/sources/balancer_v2/swap/weighted_math.rs @@ -5,16 +5,14 @@ use { super::{error::Error, fixed_point::Bfp}, ethcontract::U256, - lazy_static::lazy_static, + std::sync::LazyLock, }; // https://github.com/balancer-labs/balancer-v2-monorepo/blob/6c9e24e22d0c46cca6dd15861d3d33da61a60b98/pkg/core/contracts/pools/weighted/WeightedMath.sol#L36-L37 -lazy_static! { - static ref MAX_IN_RATIO: Bfp = - Bfp::from_wei(U256::exp10(17).checked_mul(3_u32.into()).unwrap()); - static ref MAX_OUT_RATIO: Bfp = - Bfp::from_wei(U256::exp10(17).checked_mul(3_u32.into()).unwrap()); -} +static MAX_IN_RATIO: LazyLock = + LazyLock::new(|| Bfp::from_wei(U256::exp10(17).checked_mul(3_u32.into()).unwrap())); +static MAX_OUT_RATIO: LazyLock = + LazyLock::new(|| Bfp::from_wei(U256::exp10(17).checked_mul(3_u32.into()).unwrap())); /// https://github.com/balancer-labs/balancer-v2-monorepo/blob/6c9e24e22d0c46cca6dd15861d3d33da61a60b98/pkg/core/contracts/pools/weighted/WeightedMath.sol#L69-L100 /// It is not possible for the following addition balance_in.add(amount_in) to diff --git a/crates/shared/src/sources/uniswap_v2/pool_fetching.rs b/crates/shared/src/sources/uniswap_v2/pool_fetching.rs index 45bc54c536..b7966663f9 100644 --- a/crates/shared/src/sources/uniswap_v2/pool_fetching.rs +++ b/crates/shared/src/sources/uniswap_v2/pool_fetching.rs @@ -10,15 +10,17 @@ use { }, model::TokenPair, num::rational::Ratio, - std::{collections::HashSet, sync::RwLock, time::Duration}, + std::{ + collections::HashSet, + sync::{LazyLock, RwLock}, + time::Duration, + }, ttl_cache::TtlCache, }; const POOL_SWAP_GAS_COST: usize = 60_000; -lazy_static::lazy_static! { - static ref POOL_MAX_RESERVES: U256 = U256::from((1u128 << 112) - 1); -} +static POOL_MAX_RESERVES: LazyLock = LazyLock::new(|| U256::from((1u128 << 112) - 1)); /// This type denotes `(reserve_a, reserve_b, token_b)` where /// `reserve_a` refers to the reserve of the excluded token. diff --git a/crates/solver/Cargo.toml b/crates/solver/Cargo.toml index aa80153249..fdb0d4d768 100644 --- a/crates/solver/Cargo.toml +++ b/crates/solver/Cargo.toml @@ -22,13 +22,11 @@ observe = { path = "../observe" } hex = { workspace = true } hex-literal = { workspace = true } itertools = { workspace = true } -lazy_static = { workspace = true } maplit = { workspace = true } mockall = { workspace = true } model = { path = "../model" } num = { workspace = true } number = { path = "../number" } -once_cell = { workspace = true } primitive-types = { workspace = true } prometheus = { workspace = true } prometheus-metric-storage = { workspace = true } diff --git a/crates/solver/src/interactions/balancer_v2.rs b/crates/solver/src/interactions/balancer_v2.rs index abf6f2b4c1..6239843568 100644 --- a/crates/solver/src/interactions/balancer_v2.rs +++ b/crates/solver/src/interactions/balancer_v2.rs @@ -6,6 +6,7 @@ use { http_solver::model::TokenAmount, interaction::{EncodedInteraction, Interaction}, }, + std::sync::LazyLock, }; #[derive(Clone, Debug)] @@ -18,12 +19,10 @@ pub struct BalancerSwapGivenOutInteraction { pub user_data: Bytes>, } -lazy_static::lazy_static! { - /// An impossibly distant future timestamp. Note that we use `0x80000...00` - /// as the value so that it is mostly 0's to save small amounts of gas on - /// calldata. - pub static ref NEVER: U256 = U256::from(1) << 255; -} +/// An impossibly distant future timestamp. Note that we use `0x80000...00` +/// as the value so that it is mostly 0's to save small amounts of gas on +/// calldata. +pub static NEVER: LazyLock = LazyLock::new(|| U256::from(1) << 255); impl BalancerSwapGivenOutInteraction { pub fn encode_swap(&self) -> EncodedInteraction { diff --git a/crates/solver/src/liquidity/slippage.rs b/crates/solver/src/liquidity/slippage.rs index a4185aa909..60940314c4 100644 --- a/crates/solver/src/liquidity/slippage.rs +++ b/crates/solver/src/liquidity/slippage.rs @@ -5,9 +5,8 @@ use { anyhow::{Context as _, Result}, ethcontract::U256, num::{BigInt, BigRational, CheckedDiv, Integer as _, ToPrimitive as _}, - once_cell::sync::OnceCell, shared::{external_prices::ExternalPrices, http_solver::model::TokenAmount}, - std::{borrow::Cow, cmp}, + std::{borrow::Cow, cmp, sync::LazyLock}, }; /// Constant maximum slippage of 10 BPS (0.1%) to use for on-chain liquidity. @@ -86,9 +85,12 @@ impl SlippageContext<'_> { impl Default for SlippageContext<'static> { fn default() -> Self { - static CONTEXT: OnceCell<(ExternalPrices, SlippageCalculator)> = OnceCell::new(); - let (prices, calculator) = CONTEXT.get_or_init(Default::default); - Self { prices, calculator } + static CONTEXT: LazyLock<(ExternalPrices, SlippageCalculator)> = + LazyLock::new(Default::default); + Self { + prices: &CONTEXT.0, + calculator: &CONTEXT.1, + } } } diff --git a/crates/solver/src/liquidity_collector.rs b/crates/solver/src/liquidity_collector.rs index 52a3fed2bc..eefd0c6ccd 100644 --- a/crates/solver/src/liquidity_collector.rs +++ b/crates/solver/src/liquidity_collector.rs @@ -2,9 +2,13 @@ use { crate::liquidity::Liquidity, anyhow::Result, model::TokenPair, - once_cell::sync::OnceCell, shared::{baseline_solver::BaseTokens, recent_block_cache::Block}, - std::{collections::HashSet, future::Future, sync::Arc, time::Duration}, + std::{ + collections::HashSet, + future::Future, + sync::{Arc, OnceLock}, + time::Duration, + }, tracing::Instrument, }; @@ -50,7 +54,7 @@ impl LiquidityCollecting for LiquidityCollector { /// succeeds. Until the liquidity source has been initialised no liquidity will /// be provided. pub struct BackgroundInitLiquiditySource { - liquidity_source: Arc>, + liquidity_source: Arc>, } impl BackgroundInitLiquiditySource { @@ -66,7 +70,7 @@ impl BackgroundInitLiquiditySource { .liquidity_enabled .with_label_values(&[label]) .set(0); - let liquidity_source = Arc::new(OnceCell::new()); + let liquidity_source = Arc::new(OnceLock::new()); let inner = liquidity_source.clone(); let inner_label = label.to_owned(); tokio::task::spawn( From 0db8f43f9810124cd1dcca45d4c53d3a4ffb9eaf Mon Sep 17 00:00:00 2001 From: Martin Magnus Date: Mon, 6 Jan 2025 11:31:11 +0100 Subject: [PATCH 2/3] Remove `ttl-cache` dependency (#3207) # Description We currently have 2 dependencies for size and time based caching strategies. Given that we only have 1 usage of `ttl-cache` I replaced it with `cached` to reduce the total number of dependencies. One minor downside is that this now requires to take one more write lock but since there shouldn't be significant contention on the lock to begin with that should be okay. --- Cargo.lock | 16 ---------------- crates/shared/Cargo.toml | 1 - .../src/sources/uniswap_v2/pool_fetching.rs | 14 ++++++-------- 3 files changed, 6 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 05cfd11c17..cdeebd27b2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2925,12 +2925,6 @@ dependencies = [ "vcpkg", ] -[[package]] -name = "linked-hash-map" -version = "0.5.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" - [[package]] name = "linux-raw-sys" version = "0.4.14" @@ -4506,7 +4500,6 @@ dependencies = [ "tokio", "tracing", "tracing-subscriber", - "ttl_cache", "url", "web3", ] @@ -5413,15 +5406,6 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" -[[package]] -name = "ttl_cache" -version = "0.5.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4189890526f0168710b6ee65ceaedf1460c48a14318ceec933cb26baa492096a" -dependencies = [ - "linked-hash-map", -] - [[package]] name = "typenum" version = "1.17.0" diff --git a/crates/shared/Cargo.toml b/crates/shared/Cargo.toml index 87cb190f1f..622f3cf536 100644 --- a/crates/shared/Cargo.toml +++ b/crates/shared/Cargo.toml @@ -22,7 +22,6 @@ contracts = { path = "../contracts" } dashmap = { workspace = true } database = { path = "../database" } derive_more = { workspace = true } -ttl_cache = "0.5" derivative = { workspace = true } ethcontract = { workspace = true } ethrpc = { path = "../ethrpc" } diff --git a/crates/shared/src/sources/uniswap_v2/pool_fetching.rs b/crates/shared/src/sources/uniswap_v2/pool_fetching.rs index b7966663f9..377ecdcc6d 100644 --- a/crates/shared/src/sources/uniswap_v2/pool_fetching.rs +++ b/crates/shared/src/sources/uniswap_v2/pool_fetching.rs @@ -2,6 +2,7 @@ use { super::pair_provider::PairProvider, crate::{baseline_solver::BaselineSolvable, ethrpc::Web3, recent_block_cache::Block}, anyhow::Result, + cached::{Cached, TimedCache}, contracts::{errors::EthcontractErrorType, IUniswapLikePair, ERC20}, ethcontract::{errors::MethodError, BlockId, H160, U256}, futures::{ @@ -15,7 +16,6 @@ use { sync::{LazyLock, RwLock}, time::Duration, }, - ttl_cache::TtlCache, }; const POOL_SWAP_GAS_COST: usize = 60_000; @@ -184,8 +184,7 @@ impl BaselineSolvable for Pool { pub struct PoolFetcher { pub pool_reader: Reader, pub web3: Web3, - pub cache_time: Duration, - pub non_existent_pools: RwLock>, + pub non_existent_pools: RwLock>, } impl PoolFetcher { @@ -193,8 +192,7 @@ impl PoolFetcher { Self { pool_reader: reader, web3, - cache_time, - non_existent_pools: RwLock::new(TtlCache::new(usize::MAX)), + non_existent_pools: RwLock::new(TimedCache::with_lifespan(cache_time.as_secs())), } } } @@ -207,8 +205,8 @@ where async fn fetch(&self, token_pairs: HashSet, at_block: Block) -> Result> { let mut token_pairs: Vec<_> = token_pairs.into_iter().collect(); { - let non_existent_pools = self.non_existent_pools.read().unwrap(); - token_pairs.retain(|pair| !non_existent_pools.contains_key(pair)); + let mut non_existent_pools = self.non_existent_pools.write().unwrap(); + token_pairs.retain(|pair| non_existent_pools.cache_get(pair).is_none()); } let block = BlockId::Number(at_block.into()); let futures = token_pairs @@ -230,7 +228,7 @@ where tracing::debug!(token_pairs = ?new_missing_pairs, "stop indexing liquidity"); let mut non_existent_pools = self.non_existent_pools.write().unwrap(); for pair in new_missing_pairs { - non_existent_pools.insert(pair, (), self.cache_time); + non_existent_pools.cache_set(pair, ()); } } Ok(pools) From 07f4db6a02f8c263319c94e018a1195ddbe96b72 Mon Sep 17 00:00:00 2001 From: Martin Magnus Date: Mon, 6 Jan 2025 11:37:28 +0100 Subject: [PATCH 3/3] Move `time` dependency into `observe` crate (#3209) # Description Moves the `time` dependency into the `observe` crate because it's only used there. Also updates it to the latest version. --- Cargo.lock | 8 ++++---- Cargo.toml | 1 - crates/observe/Cargo.toml | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index cdeebd27b2..d91055a2bd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5063,9 +5063,9 @@ dependencies = [ [[package]] name = "time" -version = "0.3.36" +version = "0.3.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" +checksum = "35e7868883861bd0e56d9ac6efcaaca0d6d5d82a2a7ec8209ff492c07cf37b21" dependencies = [ "deranged", "itoa", @@ -5086,9 +5086,9 @@ checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" [[package]] name = "time-macros" -version = "0.2.18" +version = "0.2.19" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3f252a68540fde3a3877aeea552b832b40ab9a69e318efd078774a01ddee1ccf" +checksum = "2834e6017e3e5e4b9834939793b282bc03b37a3336245fa820e35e233e2a85de" dependencies = [ "num-conv", "time-core", diff --git a/Cargo.toml b/Cargo.toml index 6c819517e9..fbbd160162 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,7 +44,6 @@ serde_with = "3.8.1" sqlx = { version = "0.7", default-features = false, features = ["runtime-tokio", "tls-native-tls", "bigdecimal", "chrono", "postgres", "macros"] } strum = { version = "0.26.2", features = ["derive"] } tempfile = "3.10.1" -time = { version = "0.3.36", features = ["macros"] } thiserror = "1.0.61" toml = "0.8.14" tokio = { version = "1.38.0", features = ["tracing"] } diff --git a/crates/observe/Cargo.toml b/crates/observe/Cargo.toml index f2b1029ef0..75ebe24279 100644 --- a/crates/observe/Cargo.toml +++ b/crates/observe/Cargo.toml @@ -13,7 +13,7 @@ futures = { workspace = true } pin-project-lite = "0.2.14" prometheus = { workspace = true } prometheus-metric-storage = { workspace = true } -time = { workspace = true } +time = { version = "0.3.37", features = ["macros"] } tokio = { workspace = true, features = [ "fs" ] } tracing = { workspace = true } tracing-subscriber = { workspace = true, features = ["env-filter", "fmt", "time"] }