2023-07-13 00:23:39 +03:00
|
|
|
use super::{anvil::TestAnvil, mysql::TestMysql};
|
2023-07-17 23:31:41 +03:00
|
|
|
use crate::common::influx::TestInflux;
|
2023-06-29 23:12:35 +03:00
|
|
|
use ethers::{
|
2023-07-13 00:23:39 +03:00
|
|
|
prelude::{Http, Provider},
|
2023-06-29 23:12:35 +03:00
|
|
|
types::Address,
|
|
|
|
};
|
|
|
|
use hashbrown::HashMap;
|
2023-07-06 13:51:39 +03:00
|
|
|
use serde_json::json;
|
2023-06-29 23:12:35 +03:00
|
|
|
use std::{
|
|
|
|
env,
|
|
|
|
str::FromStr,
|
|
|
|
sync::atomic::{AtomicU16, Ordering},
|
2023-07-15 04:30:01 +03:00
|
|
|
thread,
|
2023-06-29 23:12:35 +03:00
|
|
|
};
|
|
|
|
use std::{sync::Arc, time::Duration};
|
|
|
|
use tokio::{
|
2023-07-15 04:30:01 +03:00
|
|
|
runtime::Builder,
|
2023-07-06 05:24:21 +03:00
|
|
|
sync::{
|
|
|
|
broadcast::{self, error::SendError},
|
2023-07-11 09:08:06 +03:00
|
|
|
mpsc, oneshot,
|
2023-07-06 05:24:21 +03:00
|
|
|
},
|
2023-06-29 23:12:35 +03:00
|
|
|
time::{sleep, Instant},
|
|
|
|
};
|
2023-07-13 00:23:39 +03:00
|
|
|
use tracing::info;
|
2023-06-29 23:12:35 +03:00
|
|
|
use web3_proxy::{
|
|
|
|
config::{AppConfig, TopConfig, Web3RpcConfig},
|
2023-07-12 10:35:07 +03:00
|
|
|
stats::FlushedStats,
|
2023-06-29 23:12:35 +03:00
|
|
|
sub_commands::ProxydSubCommand,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub struct TestApp {
|
2023-07-15 04:30:01 +03:00
|
|
|
/// **THREAD** (not async) handle for the proxy.
|
|
|
|
/// In an Option so we can take it and not break the `impl Drop`
|
|
|
|
pub proxy_handle: Option<thread::JoinHandle<anyhow::Result<()>>>,
|
2023-06-30 00:32:05 +03:00
|
|
|
|
2023-06-29 23:12:35 +03:00
|
|
|
/// connection to the proxy that is connected to anil.
|
|
|
|
pub proxy_provider: Provider<Http>,
|
2023-06-30 00:32:05 +03:00
|
|
|
|
2023-07-06 04:18:10 +03:00
|
|
|
/// tell the app to flush stats to the database
|
2023-07-12 10:35:07 +03:00
|
|
|
flush_stat_buffer_sender: mpsc::Sender<oneshot::Sender<FlushedStats>>,
|
2023-07-06 04:18:10 +03:00
|
|
|
|
2023-06-30 00:32:05 +03:00
|
|
|
/// tell the app to shut down (use `self.stop()`).
|
|
|
|
shutdown_sender: broadcast::Sender<()>,
|
2023-06-29 23:12:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TestApp {
|
2023-07-21 04:21:49 +03:00
|
|
|
#[allow(unused)]
|
2023-07-17 23:31:41 +03:00
|
|
|
pub async fn spawn(
|
|
|
|
anvil: &TestAnvil,
|
|
|
|
db: Option<&TestMysql>,
|
|
|
|
influx: Option<&TestInflux>,
|
|
|
|
) -> Self {
|
2023-07-13 00:23:39 +03:00
|
|
|
let chain_id = anvil.instance.chain_id();
|
2023-07-22 01:02:07 +03:00
|
|
|
let num_workers = 4;
|
2023-06-29 23:12:35 +03:00
|
|
|
|
|
|
|
// TODO: move basic setup into a test fixture
|
|
|
|
let path = env::var("PATH").unwrap();
|
|
|
|
|
2023-07-12 10:35:07 +03:00
|
|
|
info!(%path);
|
2023-06-29 23:12:35 +03:00
|
|
|
|
2023-07-13 00:42:47 +03:00
|
|
|
let db_url = db.map(|x| x.url.clone());
|
2023-06-30 07:28:31 +03:00
|
|
|
|
2023-07-17 23:31:41 +03:00
|
|
|
let (influx_host, influx_org, influx_token, influx_bucket) = match influx {
|
|
|
|
None => (None, None, None, None),
|
|
|
|
Some(x) => (
|
|
|
|
Some(x.host.clone()),
|
|
|
|
Some(x.org.clone()),
|
|
|
|
Some(x.token.clone()),
|
|
|
|
Some(x.bucket.clone()),
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
2023-06-29 23:12:35 +03:00
|
|
|
// make a test TopConfig
|
|
|
|
// TODO: test influx
|
|
|
|
// TODO: test redis
|
2023-07-06 13:51:39 +03:00
|
|
|
let app_config: AppConfig = serde_json::from_value(json!({
|
2023-07-12 10:35:07 +03:00
|
|
|
"chain_id": chain_id,
|
2023-07-06 13:51:39 +03:00
|
|
|
"db_url": db_url,
|
2023-07-17 23:31:41 +03:00
|
|
|
"influxdb_host": influx_host,
|
|
|
|
"influxdb_org": influx_org,
|
|
|
|
"influxdb_token": influx_token,
|
|
|
|
"influxdb_bucket": influx_bucket,
|
2023-07-06 13:51:39 +03:00
|
|
|
"default_user_max_requests_per_period": Some(6_000_000),
|
|
|
|
"deposit_factory_contract": Address::from_str(
|
|
|
|
"4e3BC2054788De923A04936C6ADdB99A05B0Ea36",
|
|
|
|
)
|
|
|
|
.ok(),
|
|
|
|
"min_sum_soft_limit": 1,
|
|
|
|
"min_synced_rpcs": 1,
|
|
|
|
"public_requests_per_period": Some(1_000_000),
|
|
|
|
"response_cache_max_bytes": 10_u64.pow(7),
|
|
|
|
}))
|
|
|
|
.unwrap();
|
|
|
|
|
2023-07-17 23:31:41 +03:00
|
|
|
info!("App Config is: {:?}", app_config);
|
|
|
|
|
2023-06-29 23:12:35 +03:00
|
|
|
let top_config = TopConfig {
|
2023-07-06 13:51:39 +03:00
|
|
|
app: app_config,
|
2023-06-29 23:12:35 +03:00
|
|
|
balanced_rpcs: HashMap::from([(
|
|
|
|
"anvil".to_string(),
|
|
|
|
Web3RpcConfig {
|
2023-07-13 00:23:39 +03:00
|
|
|
http_url: Some(anvil.instance.endpoint()),
|
|
|
|
ws_url: Some(anvil.instance.ws_endpoint()),
|
2023-06-29 23:12:35 +03:00
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
)]),
|
2023-07-17 23:31:41 +03:00
|
|
|
// influxdb_client: influx.map(|x| x.client),
|
2023-06-29 23:12:35 +03:00
|
|
|
private_rpcs: None,
|
|
|
|
bundler_4337_rpcs: None,
|
|
|
|
extra: Default::default(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let (shutdown_sender, _shutdown_receiver) = broadcast::channel(1);
|
|
|
|
|
|
|
|
let frontend_port_arc = Arc::new(AtomicU16::new(0));
|
|
|
|
let prometheus_port_arc = Arc::new(AtomicU16::new(0));
|
|
|
|
|
2023-07-11 09:08:06 +03:00
|
|
|
let (flush_stat_buffer_sender, flush_stat_buffer_receiver) = mpsc::channel(1);
|
2023-07-06 04:18:10 +03:00
|
|
|
|
2023-06-29 23:12:35 +03:00
|
|
|
// spawn the app
|
|
|
|
// TODO: spawn in a thread so we can run from non-async tests and so the Drop impl can wait for it to stop
|
|
|
|
let handle = {
|
2023-07-15 04:30:01 +03:00
|
|
|
let frontend_port_arc = frontend_port_arc.clone();
|
|
|
|
let prometheus_port_arc = prometheus_port_arc.clone();
|
|
|
|
let flush_stat_buffer_sender = flush_stat_buffer_sender.clone();
|
|
|
|
let shutdown_sender = shutdown_sender.clone();
|
|
|
|
|
|
|
|
thread::spawn(move || {
|
|
|
|
let runtime = Builder::new_multi_thread()
|
|
|
|
.enable_all()
|
2023-07-22 01:02:07 +03:00
|
|
|
.worker_threads(num_workers)
|
2023-07-15 04:30:01 +03:00
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
runtime.block_on(ProxydSubCommand::_main(
|
|
|
|
top_config,
|
|
|
|
None,
|
|
|
|
frontend_port_arc,
|
|
|
|
prometheus_port_arc,
|
|
|
|
num_workers,
|
|
|
|
shutdown_sender,
|
|
|
|
flush_stat_buffer_sender,
|
|
|
|
flush_stat_buffer_receiver,
|
|
|
|
))
|
|
|
|
})
|
2023-06-29 23:12:35 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut frontend_port = frontend_port_arc.load(Ordering::Relaxed);
|
|
|
|
let start = Instant::now();
|
|
|
|
while frontend_port == 0 {
|
2023-06-30 07:28:31 +03:00
|
|
|
// we have to give it some time because it might have to do migrations
|
|
|
|
if start.elapsed() > Duration::from_secs(10) {
|
2023-06-29 23:12:35 +03:00
|
|
|
panic!("took too long to start!");
|
|
|
|
}
|
|
|
|
|
|
|
|
sleep(Duration::from_millis(10)).await;
|
|
|
|
frontend_port = frontend_port_arc.load(Ordering::Relaxed);
|
|
|
|
}
|
|
|
|
|
|
|
|
let proxy_endpoint = format!("http://127.0.0.1:{}", frontend_port);
|
|
|
|
|
|
|
|
let proxy_provider = Provider::<Http>::try_from(proxy_endpoint).unwrap();
|
|
|
|
|
|
|
|
Self {
|
2023-07-15 04:30:01 +03:00
|
|
|
proxy_handle: Some(handle),
|
2023-06-29 23:12:35 +03:00
|
|
|
proxy_provider,
|
2023-07-06 04:18:10 +03:00
|
|
|
flush_stat_buffer_sender,
|
2023-06-29 23:12:35 +03:00
|
|
|
shutdown_sender,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-06 05:24:21 +03:00
|
|
|
#[allow(unused)]
|
2023-07-12 10:35:07 +03:00
|
|
|
pub async fn flush_stats(&self) -> anyhow::Result<FlushedStats> {
|
2023-07-06 05:24:21 +03:00
|
|
|
let (tx, rx) = oneshot::channel();
|
|
|
|
|
2023-07-11 09:08:06 +03:00
|
|
|
self.flush_stat_buffer_sender.send(tx).await?;
|
2023-07-06 05:24:21 +03:00
|
|
|
|
|
|
|
let x = rx.await?;
|
|
|
|
|
|
|
|
Ok(x)
|
2023-07-06 04:18:10 +03:00
|
|
|
}
|
|
|
|
|
2023-06-29 23:12:35 +03:00
|
|
|
pub fn stop(&self) -> Result<usize, SendError<()>> {
|
|
|
|
self.shutdown_sender.send(())
|
|
|
|
}
|
|
|
|
|
2023-06-30 07:28:31 +03:00
|
|
|
#[allow(unused)]
|
2023-07-15 04:30:01 +03:00
|
|
|
pub fn wait_for_stop(mut self) {
|
2023-06-30 07:28:31 +03:00
|
|
|
let _ = self.stop();
|
|
|
|
|
2023-07-15 04:30:01 +03:00
|
|
|
if let Some(handle) = self.proxy_handle.take() {
|
|
|
|
handle.join().unwrap();
|
2023-06-29 23:12:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for TestApp {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
let _ = self.stop();
|
|
|
|
|
|
|
|
// TODO: do we care about waiting for it to stop? it will slow our tests down so we probably only care about waiting in some tests
|
|
|
|
}
|
|
|
|
}
|