From 4a03d2e3317fbfaa6e0e28d9d380ca3d3e211b6f Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Thu, 29 Jun 2023 23:13:22 -0700 Subject: [PATCH] loop until it works --- web3_proxy/src/relational_db.rs | 4 ++-- web3_proxy/src/rpcs/blockchain.rs | 5 +++++ web3_proxy/tests/common/app.rs | 30 ++++++++++++++++++++++++------ 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/web3_proxy/src/relational_db.rs b/web3_proxy/src/relational_db.rs index e4aa5ee3..7bbdeb35 100644 --- a/web3_proxy/src/relational_db.rs +++ b/web3_proxy/src/relational_db.rs @@ -5,7 +5,7 @@ use migration::sea_query::table::ColumnDef; use migration::{Alias, DbErr, Migrator, MigratorTrait, Table}; use std::time::Duration; use tokio::time::sleep; -use tracing::{debug, info, warn}; +use tracing::{debug, info, trace, warn}; pub use migration::sea_orm::DatabaseConnection; @@ -39,7 +39,7 @@ pub async fn get_db( .connect_timeout(Duration::from_secs(5)) .min_connections(min_connections) .max_connections(max_connections) - .sqlx_logging_level(tracing::log::LevelFilter::Warn) + .sqlx_logging_level(tracing::log::LevelFilter::Trace) .sqlx_logging(true); Database::connect(db_opt).await diff --git a/web3_proxy/src/rpcs/blockchain.rs b/web3_proxy/src/rpcs/blockchain.rs index 8ee308d6..c6314653 100644 --- a/web3_proxy/src/rpcs/blockchain.rs +++ b/web3_proxy/src/rpcs/blockchain.rs @@ -291,6 +291,11 @@ impl Web3Rpcs { // TODO: check known uncles } + if hash == &H256::zero() { + // TODO: think more about this + return Err(Web3ProxyError::UnknownBlockHash(*hash)); + } + // block not in cache. we need to ask an rpc for it let get_block_params = (*hash, false); diff --git a/web3_proxy/tests/common/app.rs b/web3_proxy/tests/common/app.rs index 5b474f7f..584edb72 100644 --- a/web3_proxy/tests/common/app.rs +++ b/web3_proxy/tests/common/app.rs @@ -23,7 +23,7 @@ use tokio::{ task::JoinHandle, time::{sleep, Instant}, }; -use tracing::{info, trace}; +use tracing::{info, trace, warn}; use web3_proxy::{ config::{AppConfig, TopConfig, Web3RpcConfig}, relational_db::get_migrated_db, @@ -192,12 +192,31 @@ impl TestApp { sleep(Duration::from_secs(1)).await; } - info!(%db_url, "db is ready for connections"); + // TODO: make sure mysql is actually ready for connections + sleep(Duration::from_secs(7)).await; + + info!(%db_url, elapsed=%start.elapsed().as_secs_f32(), "db is ready for connections. Migrating now..."); // try to migrate - let _ = get_migrated_db(db_url, 1, 1) - .await - .expect("failed migration"); + let start = Instant::now(); + let max_wait = Duration::from_secs(30); + loop { + if start.elapsed() > max_wait { + panic!("db took too long to start"); + } + + if let Err(err) = get_migrated_db(db_url.clone(), 1, 1).await { + // not connected. sleep and then try again + warn!(?err, "unable to migrate db"); + sleep(Duration::from_secs(1)).await; + continue; + } + + // it worked! yey! + break; + } + + info!(%db_url, elapsed=%start.elapsed().as_secs_f32(), "db is migrated"); Some(db_data) } else { @@ -314,7 +333,6 @@ impl Drop for TestApp { impl Drop for DbData { fn drop(&mut self) { - // TODO: this doesn't seem to run info!(%self.container_name, "killing db"); let _ = SyncCommand::new("docker")