From bf03729f9b749219ce10778037c39607e1a5a23a Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Thu, 28 Apr 2022 21:05:28 +0000 Subject: [PATCH] wow. it was slow because it was all the debug formatter --- src/block_watcher.rs | 9 ++++++++- src/main.rs | 14 +++++++++----- src/provider.rs | 10 +++++++++- src/provider_tiers.rs | 9 ++++++++- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/block_watcher.rs b/src/block_watcher.rs index faa6d4ac..11a5ebe2 100644 --- a/src/block_watcher.rs +++ b/src/block_watcher.rs @@ -2,6 +2,7 @@ use dashmap::DashMap; use ethers::prelude::{Block, TxHash}; use std::cmp; +use std::fmt; use std::sync::atomic::{self, AtomicU64}; use std::sync::Arc; use std::time::{SystemTime, UNIX_EPOCH}; @@ -21,7 +22,6 @@ pub enum SyncStatus { Unknown, } -#[derive(Debug)] pub struct BlockWatcher { sender: BlockWatcherSender, /// this Mutex is locked over awaits, so we want an async lock @@ -30,6 +30,13 @@ pub struct BlockWatcher { head_block_number: AtomicU64, } +impl fmt::Debug for BlockWatcher { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // TODO: the default formatter takes forever to write. this is too quiet though + write!(f, "BlockWatcher(...)") + } +} + impl BlockWatcher { pub fn new() -> Self { let (sender, receiver) = mpsc::unbounded_channel(); diff --git a/src/main.rs b/src/main.rs index 7dd16d17..ce849a00 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,11 +6,12 @@ use futures::future; use governor::clock::{Clock, QuantaClock}; use serde_json::json; use std::collections::HashMap; +use std::fmt; use std::sync::Arc; use std::time::Duration; use tokio::sync::{mpsc, watch, RwLock}; use tokio::time::sleep; -use tracing::{instrument, warn}; +use tracing::warn; use warp::Filter; // use crate::types::{BlockMap, ConnectionsMap, RpcRateLimiterMap}; @@ -26,7 +27,6 @@ static APP_USER_AGENT: &str = concat!( /// The application // TODO: this debug impl is way too verbose. make something smaller -#[derive(Debug)] struct Web3ProxyApp { /// clock used for rate limiting /// TODO: use tokio's clock (will require a different ratelimiting crate) @@ -42,8 +42,14 @@ struct Web3ProxyApp { private_rpcs_ratelimiter_lock: RwLock<()>, } +impl fmt::Debug for Web3ProxyApp { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // TODO: the default formatter takes forever to write. this is too quiet though + write!(f, "Web3ProxyApp(...)") + } +} + impl Web3ProxyApp { - #[instrument] async fn try_new( allowed_lag: u64, balanced_rpc_tiers: Vec>, @@ -149,7 +155,6 @@ impl Web3ProxyApp { /// send the request to the approriate RPCs /// TODO: dry this up - #[instrument] async fn proxy_web3_rpc( self: Arc, json_body: serde_json::Value, @@ -331,7 +336,6 @@ impl Web3ProxyApp { } } - #[instrument] async fn try_send_requests( &self, rpc_servers: Vec, diff --git a/src/provider.rs b/src/provider.rs index 98e39676..a8b3d9d3 100644 --- a/src/provider.rs +++ b/src/provider.rs @@ -2,6 +2,7 @@ use derive_more::From; use ethers::prelude::{BlockNumber, Middleware}; use futures::StreamExt; +use std::fmt; use std::time::Duration; use std::{cmp::Ordering, sync::Arc}; use tokio::time::interval; @@ -10,12 +11,19 @@ use tracing::{info, warn}; use crate::block_watcher::BlockWatcherSender; // TODO: instead of an enum, I tried to use Box, but hit https://github.com/gakonst/ethers-rs/issues/592 -#[derive(From, Debug)] +#[derive(From)] pub enum Web3Provider { Http(ethers::providers::Provider), Ws(ethers::providers::Provider), } +impl fmt::Debug for Web3Provider { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // TODO: the default formatter takes forever to write. this is too quiet though + write!(f, "Web3Provider(...)") + } +} + /// Forward functions to the inner ethers::providers::Provider impl Web3Provider { /// Send a web3 request diff --git a/src/provider_tiers.rs b/src/provider_tiers.rs index 4dfc98b1..820617e6 100644 --- a/src/provider_tiers.rs +++ b/src/provider_tiers.rs @@ -8,6 +8,7 @@ use governor::NotUntil; use governor::RateLimiter; use std::cmp; use std::collections::HashMap; +use std::fmt; use std::num::NonZeroU32; use std::sync::Arc; use tracing::{info, instrument}; @@ -23,7 +24,6 @@ type Web3RateLimiterMap = DashMap; pub type Web3ConnectionMap = DashMap; /// Load balance to the rpc -#[derive(Debug)] pub struct Web3ProviderTier { /// TODO: what type for the rpc? Vec isn't great. i think we want this to be the key for the provider and not the provider itself /// TODO: we probably want a better lock @@ -33,6 +33,13 @@ pub struct Web3ProviderTier { ratelimiters: Web3RateLimiterMap, } +impl fmt::Debug for Web3ProviderTier { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // TODO: the default formatter takes forever to write. this is too quiet though + write!(f, "Web3ProviderTier") + } +} + impl Web3ProviderTier { pub async fn try_new( servers: Vec<(&str, u32)>,