From 16caf1d0fe89a8d484b6b0f5538a4784e8a60ddb Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Tue, 24 Oct 2023 21:53:41 -0700 Subject: [PATCH] dynamic max_head_block_age --- Cargo.lock | 1 - web3_proxy/src/rpcs/many.rs | 20 +++++++++++++++---- web3_proxy_cli/Cargo.toml | 5 +---- web3_proxy_cli/tests/test_admins.rs | 1 + web3_proxy_cli/tests/test_multiple_proxy.rs | 1 + web3_proxy_cli/tests/test_proxy.rs | 2 +- web3_proxy_cli/tests/test_single_proxy.rs | 1 + web3_proxy_cli/tests/test_sum_credits_used.rs | 1 + web3_proxy_cli/tests/test_users.rs | 1 + 9 files changed, 23 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 09e716d6..50b8222b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6678,7 +6678,6 @@ dependencies = [ "serde", "serde_json", "test-log", - "tokio", "tokio-console", "tracing", "tracing-subscriber", diff --git a/web3_proxy/src/rpcs/many.rs b/web3_proxy/src/rpcs/many.rs index 23c0ba31..56864355 100644 --- a/web3_proxy/src/rpcs/many.rs +++ b/web3_proxy/src/rpcs/many.rs @@ -133,10 +133,15 @@ impl Web3Rpcs { // by_name starts empty. self.apply_server_configs will add to it let by_name = RwLock::new(HashMap::new()); - let max_head_block_lag = max_head_block_lag.unwrap_or(5.into()); + let block_interval = average_block_interval(chain_id); - let max_head_block_age = - average_block_interval(chain_id).mul_f32((max_head_block_lag.as_u64() * 10) as f32); + // TODO: think about the max more for long block interval chains + let max_head_block_lag = max_head_block_lag.unwrap_or_else(|| { + U64::from(5.max((60f32 / block_interval.as_secs_f32()).round() as u64)) + }); + + // TODO: think about the max more for long block interval chains + let max_head_block_age = block_interval.mul_f32((max_head_block_lag.as_u64() * 10) as f32); let connections = Arc::new(Self { block_and_rpc_sender, @@ -600,7 +605,7 @@ impl Serialize for Web3Rpcs { where S: Serializer, { - let mut state = serializer.serialize_struct("Web3Rpcs", 5)?; + let mut state = serializer.serialize_struct("Web3Rpcs", 7)?; { let by_name = self.by_name.read(); @@ -609,6 +614,13 @@ impl Serialize for Web3Rpcs { state.serialize_field("conns", &rpcs)?; } + state.serialize_field( + "max_head_block_age_ms", + &self.max_head_block_age.as_millis(), + )?; + + state.serialize_field("max_head_block_lag", &self.max_head_block_lag)?; + { let consensus_rpcs = self.watch_ranked_rpcs.borrow().clone(); // TODO: rename synced_connections to consensus_rpcs diff --git a/web3_proxy_cli/Cargo.toml b/web3_proxy_cli/Cargo.toml index 96eaf6d4..5fe3d5b7 100644 --- a/web3_proxy_cli/Cargo.toml +++ b/web3_proxy_cli/Cargo.toml @@ -26,11 +26,8 @@ serde = { version = "1.0.189" } serde_json = { version = "1.0.107", default-features = false, features = ["raw_value"] } tokio-console = { version = "0.1.10", optional = true } tracing = "0.1" -tracing-subscriber = { version = "0.3", features = ["env-filter"] } +tracing-subscriber = { version = "0.3", default-features = false, features = ["env-filter", "fmt"] } [dev-dependencies] env_logger = { version ="0.10", default-features = false, features = ["auto-color"] } test-log = { version ="0.2.13", default-features = false, features = ["trace"] } -tokio = { version = "1.33.0", default-features = false, features = ["full", "test-util"] } -tracing = {version = "0.1", default-features = false } -tracing-subscriber = {version = "0.3", default-features = false, features = ["env-filter", "fmt"] } diff --git a/web3_proxy_cli/tests/test_admins.rs b/web3_proxy_cli/tests/test_admins.rs index 6bd0cf95..1f903c02 100644 --- a/web3_proxy_cli/tests/test_admins.rs +++ b/web3_proxy_cli/tests/test_admins.rs @@ -3,6 +3,7 @@ use std::time::Duration; use tracing::info; use web3_proxy::prelude::migration::sea_orm::prelude::Decimal; use web3_proxy::prelude::reqwest; +use web3_proxy::prelude::tokio; use web3_proxy::test_utils::mysql::TestMysql; use web3_proxy::test_utils::TestAnvil; use web3_proxy_cli::test_utils::admin_increases_balance::admin_increase_balance; diff --git a/web3_proxy_cli/tests/test_multiple_proxy.rs b/web3_proxy_cli/tests/test_multiple_proxy.rs index b3a2e445..f26b786a 100644 --- a/web3_proxy_cli/tests/test_multiple_proxy.rs +++ b/web3_proxy_cli/tests/test_multiple_proxy.rs @@ -5,6 +5,7 @@ use tracing::{info, warn}; use web3_proxy::prelude::futures::future::try_join_all; use web3_proxy::prelude::reqwest; use web3_proxy::prelude::rust_decimal::{Decimal, RoundingStrategy}; +use web3_proxy::prelude::tokio; use web3_proxy::prelude::tokio::time::sleep; use web3_proxy::rpcs::blockchain::ArcBlock; use web3_proxy::stats::FlushedStats; diff --git a/web3_proxy_cli/tests/test_proxy.rs b/web3_proxy_cli/tests/test_proxy.rs index d716500c..e647b598 100644 --- a/web3_proxy_cli/tests/test_proxy.rs +++ b/web3_proxy_cli/tests/test_proxy.rs @@ -1,6 +1,5 @@ use serde_json::Value; use std::{str::FromStr, time::Duration}; -use tokio::{task::yield_now, time::sleep}; use tracing::{info, warn}; use web3_proxy::prelude::ethers::{ prelude::{Block, Transaction, TxHash, H256, U256, U64}, @@ -9,6 +8,7 @@ use web3_proxy::prelude::ethers::{ }; use web3_proxy::prelude::http::StatusCode; use web3_proxy::prelude::reqwest; +use web3_proxy::prelude::tokio::{self, task::yield_now, time::sleep}; use web3_proxy::rpcs::blockchain::ArcBlock; use web3_proxy_cli::test_utils::{TestAnvil, TestApp, TestMysql}; diff --git a/web3_proxy_cli/tests/test_single_proxy.rs b/web3_proxy_cli/tests/test_single_proxy.rs index 9803e666..719116eb 100644 --- a/web3_proxy_cli/tests/test_single_proxy.rs +++ b/web3_proxy_cli/tests/test_single_proxy.rs @@ -5,6 +5,7 @@ use tracing::{info, warn}; use web3_proxy::prelude::futures::future::try_join_all; use web3_proxy::prelude::reqwest; use web3_proxy::prelude::rust_decimal::{Decimal, RoundingStrategy}; +use web3_proxy::prelude::tokio; use web3_proxy::rpcs::blockchain::ArcBlock; use web3_proxy_cli::test_utils::create_provider_with_rpc_key::create_provider_for_user; use web3_proxy_cli::test_utils::rpc_key::user_get_first_rpc_key; diff --git a/web3_proxy_cli/tests/test_sum_credits_used.rs b/web3_proxy_cli/tests/test_sum_credits_used.rs index 4dbfca2f..eb70c588 100644 --- a/web3_proxy_cli/tests/test_sum_credits_used.rs +++ b/web3_proxy_cli/tests/test_sum_credits_used.rs @@ -4,6 +4,7 @@ use web3_proxy::balance::Balance; use web3_proxy::prelude::ethers::prelude::U64; use web3_proxy::prelude::migration::sea_orm::prelude::Decimal; use web3_proxy::prelude::reqwest; +use web3_proxy::prelude::tokio; use web3_proxy_cli::test_utils::{ admin_increases_balance::admin_increase_balance, create_admin::create_user_as_admin, diff --git a/web3_proxy_cli/tests/test_users.rs b/web3_proxy_cli/tests/test_users.rs index 368a49ed..c48e695c 100644 --- a/web3_proxy_cli/tests/test_users.rs +++ b/web3_proxy_cli/tests/test_users.rs @@ -7,6 +7,7 @@ use web3_proxy::prelude::ethers::prelude::{Http, Provider}; use web3_proxy::prelude::ethers::{signers::Signer, types::Signature}; use web3_proxy::prelude::migration::sea_orm::prelude::Decimal; use web3_proxy::prelude::reqwest; +use web3_proxy::prelude::tokio; use web3_proxy::prelude::ulid::Ulid; use web3_proxy::rpcs::blockchain::ArcBlock; use web3_proxy_cli::test_utils::admin_deposits::get_admin_deposits;