From 7678cb738915f044fe6e0ebedc8eeab0a81aa2d2 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Thu, 20 Jul 2023 18:21:49 -0700 Subject: [PATCH] caches aren't actualy optional on buffer --- web3_proxy/src/app/mod.rs | 9 ++------- web3_proxy/src/stats/stat_buffer.rs | 20 +++++++------------ .../src/sub_commands/migrate_stats_to_v2.rs | 9 +++++---- web3_proxy/tests/common/admin_deposits.rs | 2 +- .../tests/common/admin_increases_balance.rs | 2 +- web3_proxy/tests/common/anvil.rs | 1 + web3_proxy/tests/common/app.rs | 1 + web3_proxy/tests/common/create_admin.rs | 2 +- web3_proxy/tests/common/create_user.rs | 2 +- web3_proxy/tests/common/mod.rs | 3 +++ web3_proxy/tests/common/referral.rs | 2 +- web3_proxy/tests/common/rpc_key.rs | 5 ++--- web3_proxy/tests/common/stats_accounting.rs | 2 +- web3_proxy/tests/common/user_balance.rs | 2 +- 14 files changed, 28 insertions(+), 34 deletions(-) diff --git a/web3_proxy/src/app/mod.rs b/web3_proxy/src/app/mod.rs index 55a6aeef..c4ecb1c8 100644 --- a/web3_proxy/src/app/mod.rs +++ b/web3_proxy/src/app/mod.rs @@ -55,7 +55,6 @@ use tokio::sync::{broadcast, mpsc, oneshot, watch, Semaphore}; use tokio::task::JoinHandle; use tokio::time::{sleep, timeout}; use tracing::{error, info, trace, warn, Level}; -use ulid::Ulid; // TODO: make this customizable? // TODO: include GIT_REF in here. i had trouble getting https://docs.rs/vergen/latest/vergen/ to work with a workspace. also .git is in .dockerignore @@ -320,9 +319,6 @@ impl Web3ProxyApp { .build() .into(); - // Generate the instance name - let instance_hash = Ulid::new().to_string(); - // create a channel for receiving stats // we do this in a channel so we don't slow down our response to the users // stats can be saved in mysql, influxdb, both, or none @@ -332,13 +328,12 @@ impl Web3ProxyApp { 60, top_config.app.influxdb_bucket.clone(), influxdb_client.clone(), - Some(rpc_secret_key_cache.clone()), - Some(user_balance_cache.clone()), + rpc_secret_key_cache.clone(), + user_balance_cache.clone(), stat_buffer_shutdown_receiver, 1, flush_stat_buffer_sender.clone(), flush_stat_buffer_receiver, - instance_hash, )? { // since the database entries are used for accounting, we want to be sure everything is saved before exiting important_background_handles.push(spawned_stat_buffer.background_handle); diff --git a/web3_proxy/src/stats/stat_buffer.rs b/web3_proxy/src/stats/stat_buffer.rs index b9678a3e..e6d528af 100644 --- a/web3_proxy/src/stats/stat_buffer.rs +++ b/web3_proxy/src/stats/stat_buffer.rs @@ -14,6 +14,7 @@ use std::time::Duration; use tokio::sync::{broadcast, mpsc, oneshot}; use tokio::time::{interval, sleep}; use tracing::{error, info, trace, warn}; +use ulid::Ulid; #[derive(Debug, Default)] pub struct BufferedRpcQueryStats { @@ -68,13 +69,12 @@ impl StatBuffer { db_save_interval_seconds: u32, influxdb_bucket: Option, mut influxdb_client: Option, - rpc_secret_key_cache: Option, - user_balance_cache: Option, + rpc_secret_key_cache: RpcSecretKeyCache, + user_balance_cache: UserBalanceCache, shutdown_receiver: broadcast::Receiver<()>, tsdb_save_interval_seconds: u32, flush_sender: mpsc::Sender>, flush_receiver: mpsc::Receiver>, - instance_hash: String, ) -> anyhow::Result> { if influxdb_bucket.is_none() { influxdb_client = None; @@ -84,6 +84,8 @@ impl StatBuffer { let timestamp_precision = TimestampPrecision::Seconds; + let instance_hash = Ulid::new().to_string(); + let mut new = Self { accounting_db_buffer: Default::default(), billing_period_seconds, @@ -94,10 +96,10 @@ impl StatBuffer { influxdb_client, instance_hash, opt_in_timeseries_buffer: Default::default(), - rpc_secret_key_cache: rpc_secret_key_cache.unwrap(), + rpc_secret_key_cache, timestamp_precision, tsdb_save_interval_seconds, - user_balance_cache: user_balance_cache.unwrap(), + user_balance_cache, _flush_sender: flush_sender, }; @@ -437,11 +439,3 @@ impl StatBuffer { count } } - -#[cfg(test)] -mod tests { - #[test] - fn test_something() { - panic!() - } -} diff --git a/web3_proxy/src/sub_commands/migrate_stats_to_v2.rs b/web3_proxy/src/sub_commands/migrate_stats_to_v2.rs index 4fed909d..7c9640d6 100644 --- a/web3_proxy/src/sub_commands/migrate_stats_to_v2.rs +++ b/web3_proxy/src/sub_commands/migrate_stats_to_v2.rs @@ -13,6 +13,7 @@ use migration::sea_orm::{ ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter, QuerySelect, UpdateResult, }; use migration::{Expr, Value}; +use moka::future::Cache; use parking_lot::Mutex; use std::num::NonZeroU64; use std::sync::Arc; @@ -74,7 +75,8 @@ impl MigrateStatsToV2SubCommand { let (flush_sender, flush_receiver) = mpsc::channel(1); - let instance_hash = Ulid::new().to_string(); + let rpc_secret_key_cache = Cache::builder().build(); + let user_balance_cache = Cache::builder().build().into(); // Spawn the stat-sender let emitter_spawn = StatBuffer::try_spawn( @@ -83,13 +85,12 @@ impl MigrateStatsToV2SubCommand { 30, top_config.app.influxdb_bucket.clone(), influxdb_client.clone(), - None, - None, + rpc_secret_key_cache, + user_balance_cache, rpc_account_shutdown_recevier, 1, flush_sender, flush_receiver, - instance_hash, ) .context("Error spawning stat buffer")? .context("No stat buffer spawned. Maybe missing influx or db credentials?")?; diff --git a/web3_proxy/tests/common/admin_deposits.rs b/web3_proxy/tests/common/admin_deposits.rs index 96bf84c6..3ade8d2e 100644 --- a/web3_proxy/tests/common/admin_deposits.rs +++ b/web3_proxy/tests/common/admin_deposits.rs @@ -1,4 +1,4 @@ -use crate::TestApp; +use super::TestApp; use tracing::trace; use web3_proxy::frontend::users::authentication::LoginPostResponse; diff --git a/web3_proxy/tests/common/admin_increases_balance.rs b/web3_proxy/tests/common/admin_increases_balance.rs index a7a63fe3..0e3b0624 100644 --- a/web3_proxy/tests/common/admin_increases_balance.rs +++ b/web3_proxy/tests/common/admin_increases_balance.rs @@ -1,4 +1,4 @@ -use crate::TestApp; +use super::TestApp; use ethers::prelude::{LocalWallet, Signer}; use migration::sea_orm::prelude::Decimal; use tracing::info; diff --git a/web3_proxy/tests/common/anvil.rs b/web3_proxy/tests/common/anvil.rs index 2cd1bb0d..ba396093 100644 --- a/web3_proxy/tests/common/anvil.rs +++ b/web3_proxy/tests/common/anvil.rs @@ -15,6 +15,7 @@ pub struct TestAnvil { } impl TestAnvil { + #[allow(unused)] pub async fn spawn(chain_id: u64) -> Self { info!(?chain_id); diff --git a/web3_proxy/tests/common/app.rs b/web3_proxy/tests/common/app.rs index 762dec90..3c050f07 100644 --- a/web3_proxy/tests/common/app.rs +++ b/web3_proxy/tests/common/app.rs @@ -44,6 +44,7 @@ pub struct TestApp { } impl TestApp { + #[allow(unused)] pub async fn spawn( anvil: &TestAnvil, db: Option<&TestMysql>, diff --git a/web3_proxy/tests/common/create_admin.rs b/web3_proxy/tests/common/create_admin.rs index 3c553e21..d00baa81 100644 --- a/web3_proxy/tests/common/create_admin.rs +++ b/web3_proxy/tests/common/create_admin.rs @@ -1,4 +1,4 @@ -use crate::TestApp; +use super::TestApp; use ethers::prelude::{LocalWallet, Signer}; use ethers::types::Signature; use http::StatusCode; diff --git a/web3_proxy/tests/common/create_user.rs b/web3_proxy/tests/common/create_user.rs index 040cc08a..4f82d975 100644 --- a/web3_proxy/tests/common/create_user.rs +++ b/web3_proxy/tests/common/create_user.rs @@ -1,4 +1,4 @@ -use crate::TestApp; +use super::TestApp; use entities::{user, user_tier}; use ethers::prelude::{LocalWallet, Signer}; use ethers::types::Signature; diff --git a/web3_proxy/tests/common/mod.rs b/web3_proxy/tests/common/mod.rs index b76b0cc3..7f3ef0db 100644 --- a/web3_proxy/tests/common/mod.rs +++ b/web3_proxy/tests/common/mod.rs @@ -12,4 +12,7 @@ pub mod rpc_key; pub mod stats_accounting; pub mod user_balance; +pub use self::anvil::TestAnvil; pub use self::app::TestApp; +pub use self::influx::TestInflux; +pub use self::mysql::TestMysql; diff --git a/web3_proxy/tests/common/referral.rs b/web3_proxy/tests/common/referral.rs index b40a86ca..0779ba60 100644 --- a/web3_proxy/tests/common/referral.rs +++ b/web3_proxy/tests/common/referral.rs @@ -2,7 +2,7 @@ /// Includes /// - get referral link /// - getting code for referral (shared and used) -use crate::TestApp; +use super::TestApp; use tracing::info; use ulid::Ulid; use web3_proxy::frontend::users::authentication::LoginPostResponse; diff --git a/web3_proxy/tests/common/rpc_key.rs b/web3_proxy/tests/common/rpc_key.rs index d92a6fe1..d9c4f296 100644 --- a/web3_proxy/tests/common/rpc_key.rs +++ b/web3_proxy/tests/common/rpc_key.rs @@ -1,7 +1,6 @@ -use std::time::Duration; - -use crate::TestApp; +use super::TestApp; use serde::Deserialize; +use std::time::Duration; use tracing::info; use ulid::Ulid; use web3_proxy::{ diff --git a/web3_proxy/tests/common/stats_accounting.rs b/web3_proxy/tests/common/stats_accounting.rs index cef67791..b965422b 100644 --- a/web3_proxy/tests/common/stats_accounting.rs +++ b/web3_proxy/tests/common/stats_accounting.rs @@ -1,4 +1,4 @@ -use crate::common::TestApp; +use super::TestApp; use serde_json::json; use std::time::{SystemTime, UNIX_EPOCH}; use tracing::{info, trace}; diff --git a/web3_proxy/tests/common/user_balance.rs b/web3_proxy/tests/common/user_balance.rs index fd428c30..bdba47b7 100644 --- a/web3_proxy/tests/common/user_balance.rs +++ b/web3_proxy/tests/common/user_balance.rs @@ -1,4 +1,4 @@ -use crate::TestApp; +use super::TestApp; use serde_json::json; use tracing::{info, trace}; use web3_proxy::balance::Balance;