From 2b1829a88212c83cf52fc5fd1062e215463e5822 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Wed, 12 Jul 2023 14:42:47 -0700 Subject: [PATCH] move db and anvil functions to their proper places --- web3_proxy/tests/common/anvil.rs | 16 +++++++- web3_proxy/tests/common/app.rs | 30 +------------- web3_proxy/tests/common/create_admin.rs | 5 ++- web3_proxy/tests/common/create_user.rs | 5 ++- web3_proxy/tests/test_admins.rs | 14 +++---- web3_proxy/tests/test_proxy.rs | 6 +-- web3_proxy/tests/test_sum_credits_used.rs | 8 ++-- web3_proxy/tests/test_users.rs | 50 +++++++++++------------ 8 files changed, 63 insertions(+), 71 deletions(-) diff --git a/web3_proxy/tests/common/anvil.rs b/web3_proxy/tests/common/anvil.rs index bf4c1b56..2cd1bb0d 100644 --- a/web3_proxy/tests/common/anvil.rs +++ b/web3_proxy/tests/common/anvil.rs @@ -1,12 +1,17 @@ // TODO: option to spawn in a dedicated thread? // TODO: option to subscribe to another anvil and copy blocks -use ethers::utils::{Anvil, AnvilInstance}; +use ethers::{ + signers::LocalWallet, + utils::{Anvil, AnvilInstance}, +}; use tracing::info; +use web3_proxy::rpcs::provider::EthersHttpProvider; /// on drop, the anvil instance will be shut down pub struct TestAnvil { pub instance: AnvilInstance, + pub provider: EthersHttpProvider, } impl TestAnvil { @@ -19,6 +24,13 @@ impl TestAnvil { // .fork("https://polygon.llamarpc.com@44300000") .spawn(); - Self { instance } + let provider = EthersHttpProvider::try_from(instance.endpoint()).unwrap(); + + Self { instance, provider } + } + + #[allow(unused)] + pub fn wallet(&self, id: usize) -> LocalWallet { + self.instance.keys()[id].clone().into() } } diff --git a/web3_proxy/tests/common/app.rs b/web3_proxy/tests/common/app.rs index 5e8ed001..6141ba2f 100644 --- a/web3_proxy/tests/common/app.rs +++ b/web3_proxy/tests/common/app.rs @@ -1,11 +1,9 @@ use super::{anvil::TestAnvil, mysql::TestMysql}; use ethers::{ prelude::{Http, Provider}, - signers::LocalWallet, types::Address, }; use hashbrown::HashMap; -use migration::sea_orm::DatabaseConnection; use parking_lot::Mutex; use serde_json::json; use std::{ @@ -30,15 +28,6 @@ use web3_proxy::{ }; pub struct TestApp { - /// anvil shuts down when this guard is dropped. - pub anvil: TestAnvil, - - /// connection to anvil. - pub anvil_provider: Provider, - - /// keep track of the database so it can be stopped on drop - pub db: Option, - /// spawn handle for the proxy. pub proxy_handle: Mutex>>>, @@ -53,7 +42,7 @@ pub struct TestApp { } impl TestApp { - pub async fn spawn(anvil: TestAnvil, db: Option) -> Self { + pub async fn spawn(anvil: &TestAnvil, db: Option<&TestMysql>) -> Self { let chain_id = anvil.instance.chain_id(); let num_workers = 2; @@ -62,9 +51,7 @@ impl TestApp { info!(%path); - let anvil_provider = Provider::::try_from(anvil.instance.endpoint()).unwrap(); - - let db_url = db.as_ref().map(|x| x.url.clone()); + let db_url = db.map(|x| x.url.clone()); // make a test TopConfig // TODO: test influx @@ -138,9 +125,6 @@ impl TestApp { let proxy_provider = Provider::::try_from(proxy_endpoint).unwrap(); Self { - anvil, - anvil_provider, - db, proxy_handle: Mutex::new(Some(handle)), proxy_provider, flush_stat_buffer_sender, @@ -148,11 +132,6 @@ impl TestApp { } } - #[allow(unused)] - pub fn db_conn(&self) -> &DatabaseConnection { - self.db.as_ref().unwrap().conn() - } - #[allow(unused)] pub async fn flush_stats(&self) -> anyhow::Result { let (tx, rx) = oneshot::channel(); @@ -180,11 +159,6 @@ impl TestApp { handle.await.unwrap().unwrap(); } } - - #[allow(unused)] - pub fn wallet(&self, id: usize) -> LocalWallet { - self.anvil.instance.keys()[id].clone().into() - } } impl Drop for TestApp { diff --git a/web3_proxy/tests/common/create_admin.rs b/web3_proxy/tests/common/create_admin.rs index 11d87f81..a181dcae 100644 --- a/web3_proxy/tests/common/create_admin.rs +++ b/web3_proxy/tests/common/create_admin.rs @@ -5,12 +5,15 @@ use tracing::info; use web3_proxy::frontend::users::authentication::{LoginPostResponse, PostLogin}; use web3_proxy::sub_commands::ChangeAdminStatusSubCommand; +use super::mysql::TestMysql; + /// Helper function to create admin /// Create user as admin #[allow(unused)] pub async fn create_user_as_admin( x: &TestApp, + db: &TestMysql, r: &reqwest::Client, admin_wallet: &LocalWallet, ) -> LoginPostResponse { @@ -65,7 +68,7 @@ pub async fn create_user_as_admin( info!("Changing the status of the admin_wallet to be an admin"); // Pass on the database into it ... - admin_status_changer.main(x.db_conn()).await.unwrap(); + admin_status_changer.main(db.conn()).await.unwrap(); // Now log him in again, because he was just signed out // Login the admin again, because he was just signed out diff --git a/web3_proxy/tests/common/create_user.rs b/web3_proxy/tests/common/create_user.rs index ea2579a9..8c75cc07 100644 --- a/web3_proxy/tests/common/create_user.rs +++ b/web3_proxy/tests/common/create_user.rs @@ -9,6 +9,8 @@ use tracing::info; use web3_proxy::errors::Web3ProxyResult; use web3_proxy::frontend::users::authentication::{LoginPostResponse, PostLogin}; +use super::mysql::TestMysql; + /// Helper function to create an "ordinary" user #[allow(unused)] pub async fn create_user( @@ -55,10 +57,11 @@ pub async fn create_user( #[allow(unused)] pub async fn set_user_tier( x: &TestApp, + db: &TestMysql, user: user::Model, tier_name: &str, ) -> Web3ProxyResult { - let db_conn = x.db_conn(); + let db_conn = db.conn(); let ut = user_tier::Entity::find() .filter(user_tier::Column::Title.like(tier_name)) diff --git a/web3_proxy/tests/test_admins.rs b/web3_proxy/tests/test_admins.rs index bee1ab95..f31d9b16 100644 --- a/web3_proxy/tests/test_admins.rs +++ b/web3_proxy/tests/test_admins.rs @@ -21,7 +21,7 @@ async fn test_admin_imitate_user() { let db = TestMysql::spawn().await; - let x = TestApp::spawn(a, Some(db)).await; + let x = TestApp::spawn(&a, Some(&db)).await; todo!(); } @@ -35,7 +35,7 @@ async fn test_admin_grant_credits() { let db = TestMysql::spawn().await; - let x = TestApp::spawn(a, Some(db)).await; + let x = TestApp::spawn(&a, Some(&db)).await; let r = reqwest::Client::builder() .timeout(Duration::from_secs(3)) @@ -43,12 +43,12 @@ async fn test_admin_grant_credits() { .unwrap(); // Setup variables that will be used - let user_wallet = x.wallet(0); - let admin_wallet = x.wallet(1); + let user_wallet = a.wallet(0); + let admin_wallet = a.wallet(1); info!(?admin_wallet); let user_login_response = create_user(&x, &r, &user_wallet, None).await; - let admin_login_response = create_user_as_admin(&x, &r, &admin_wallet).await; + let admin_login_response = create_user_as_admin(&x, &db, &r, &admin_wallet).await; info!(?admin_login_response); let increase_balance_response = admin_increase_balance( @@ -75,10 +75,10 @@ async fn test_admin_grant_credits() { #[ignore = "under construction"] #[test_log::test(tokio::test)] async fn test_admin_change_user_tier() { - let anvil = TestAnvil::spawn(31337).await; + let a = TestAnvil::spawn(31337).await; let db = TestMysql::spawn().await; - let x = TestApp::spawn(anvil, Some(db)).await; + let x = TestApp::spawn(&a, Some(&db)).await; todo!(); } diff --git a/web3_proxy/tests/test_proxy.rs b/web3_proxy/tests/test_proxy.rs index 793c70f3..4013292b 100644 --- a/web3_proxy/tests/test_proxy.rs +++ b/web3_proxy/tests/test_proxy.rs @@ -16,7 +16,7 @@ async fn it_migrates_the_db() { let a = TestAnvil::spawn(31337).await; let db = TestMysql::spawn().await; - let x = TestApp::spawn(a, Some(db)).await; + let x = TestApp::spawn(&a, Some(&db)).await; // we call flush stats more to be sure it works than because we expect it to save any stats x.flush_stats().await.unwrap(); @@ -26,9 +26,9 @@ async fn it_migrates_the_db() { async fn it_starts_and_stops() { let a = TestAnvil::spawn(31337).await; - let x = TestApp::spawn(a, None).await; + let x = TestApp::spawn(&a, None).await; - let anvil_provider = &x.anvil_provider; + let anvil_provider = &a.provider; let proxy_provider = &x.proxy_provider; let anvil_result = anvil_provider diff --git a/web3_proxy/tests/test_sum_credits_used.rs b/web3_proxy/tests/test_sum_credits_used.rs index e9f44b6c..665defa2 100644 --- a/web3_proxy/tests/test_sum_credits_used.rs +++ b/web3_proxy/tests/test_sum_credits_used.rs @@ -19,7 +19,7 @@ async fn test_sum_credits_used() { let db = TestMysql::spawn().await; - let x = TestApp::spawn(a, Some(db)).await; + let x = TestApp::spawn(&a, Some(&db)).await; let r = reqwest::Client::builder() .timeout(Duration::from_secs(3)) @@ -27,11 +27,11 @@ async fn test_sum_credits_used() { .unwrap(); // create wallets for users - let user_wallet = x.wallet(0); - let admin_wallet = x.wallet(1); + let user_wallet = a.wallet(0); + let admin_wallet = a.wallet(1); // log in to create users - let admin_login_response = create_user_as_admin(&x, &r, &admin_wallet).await; + let admin_login_response = create_user_as_admin(&x, &db, &r, &admin_wallet).await; let user_login_response = create_user(&x, &r, &user_wallet, None).await; info!("starting balance"); diff --git a/web3_proxy/tests/test_users.rs b/web3_proxy/tests/test_users.rs index e0b79500..12b085d9 100644 --- a/web3_proxy/tests/test_users.rs +++ b/web3_proxy/tests/test_users.rs @@ -42,11 +42,11 @@ async fn test_log_in_and_out() { let db = TestMysql::spawn().await; - let x = TestApp::spawn(a, Some(db)).await; + let x = TestApp::spawn(&a, Some(&db)).await; let r = reqwest::Client::new(); - let w = x.wallet(0); + let w = a.wallet(0); let login_get_url = format!("{}user/login/{:?}", x.proxy_provider.url(), w.address()); let login_message = r.get(login_get_url).send().await.unwrap(); @@ -103,18 +103,18 @@ async fn test_admin_balance_increase() { let db = TestMysql::spawn().await; - let x = TestApp::spawn(a, Some(db)).await; + let x = TestApp::spawn(&a, Some(&db)).await; let r = reqwest::Client::builder() .timeout(Duration::from_secs(20)) .build() .unwrap(); - let user_wallet = x.wallet(0); - let admin_wallet = x.wallet(1); + let user_wallet = a.wallet(0); + let admin_wallet = a.wallet(1); // Create three users, one referrer, one admin who bumps both their balances - let admin_login_response = create_user_as_admin(&x, &r, &admin_wallet).await; + let admin_login_response = create_user_as_admin(&x, &db, &r, &admin_wallet).await; let user_login_response = create_user(&x, &r, &user_wallet, None).await; // Bump both user's wallet to $20 @@ -156,18 +156,18 @@ async fn test_user_balance_decreases() { let db = TestMysql::spawn().await; - let x = TestApp::spawn(a, Some(db)).await; + let x = TestApp::spawn(&a, Some(&db)).await; let r = reqwest::Client::builder() .timeout(Duration::from_secs(20)) .build() .unwrap(); - let user_wallet = x.wallet(0); - let admin_wallet = x.wallet(1); + let user_wallet = a.wallet(0); + let admin_wallet = a.wallet(1); // Create three users, one referrer, one admin who bumps both their balances - let admin_login_response = create_user_as_admin(&x, &r, &admin_wallet).await; + let admin_login_response = create_user_as_admin(&x, &db, &r, &admin_wallet).await; let user_login_response = create_user(&x, &r, &user_wallet, None).await; // Get the rpc keys for this user @@ -264,20 +264,20 @@ async fn test_referral_bonus_non_concurrent() { let db = TestMysql::spawn().await; - let x = TestApp::spawn(a, Some(db)).await; + let x = TestApp::spawn(&a, Some(&db)).await; let r = reqwest::Client::builder() .timeout(Duration::from_secs(20)) .build() .unwrap(); - let user_wallet = x.wallet(0); - let referrer_wallet = x.wallet(1); - let admin_wallet = x.wallet(2); + let user_wallet = a.wallet(0); + let referrer_wallet = a.wallet(1); + let admin_wallet = a.wallet(2); // Create three users, one referrer, one admin who bumps both their balances let referrer_login_response = create_user(&x, &r, &referrer_wallet, None).await; - let admin_login_response = create_user_as_admin(&x, &r, &admin_wallet).await; + let admin_login_response = create_user_as_admin(&x, &db, &r, &admin_wallet).await; // Get the first user's referral link let referral_link = get_referral_code(&x, &r, &referrer_login_response).await; @@ -413,20 +413,20 @@ async fn test_referral_bonus_concurrent_referrer_only() { let db = TestMysql::spawn().await; - let x = TestApp::spawn(a, Some(db)).await; + let x = TestApp::spawn(&a, Some(&db)).await; let r = reqwest::Client::builder() .timeout(Duration::from_secs(20)) .build() .unwrap(); - let user_wallet = x.wallet(0); - let referrer_wallet = x.wallet(1); - let admin_wallet = x.wallet(2); + let user_wallet = a.wallet(0); + let referrer_wallet = a.wallet(1); + let admin_wallet = a.wallet(2); // Create three users, one referrer, one admin who bumps both their balances let referrer_login_response = create_user(&x, &r, &referrer_wallet, None).await; - let admin_login_response = create_user_as_admin(&x, &r, &admin_wallet).await; + let admin_login_response = create_user_as_admin(&x, &db, &r, &admin_wallet).await; // Get the first user's referral link let referral_link = get_referral_code(&x, &r, &referrer_login_response).await; @@ -573,20 +573,20 @@ async fn test_referral_bonus_concurrent_referrer_and_user() { let db = TestMysql::spawn().await; - let x = TestApp::spawn(a, Some(db)).await; + let x = TestApp::spawn(&a, Some(&db)).await; let r = reqwest::Client::builder() .timeout(Duration::from_secs(20)) .build() .unwrap(); - let user_wallet = x.wallet(0); - let referrer_wallet = x.wallet(1); - let admin_wallet = x.wallet(2); + let user_wallet = a.wallet(0); + let referrer_wallet = a.wallet(1); + let admin_wallet = a.wallet(2); // Create three users, one referrer, one admin who bumps both their balances let referrer_login_response = create_user(&x, &r, &referrer_wallet, None).await; - let admin_login_response = create_user_as_admin(&x, &r, &admin_wallet).await; + let admin_login_response = create_user_as_admin(&x, &db, &r, &admin_wallet).await; // Get the first user's referral link let referral_link = get_referral_code(&x, &r, &referrer_login_response).await;